3829、大石头的搬运工
#include<bits/stdc++.h>
using namespace std;
using Pair=pair<int,int>;
using ll=long long;
//带权中位数的计算
//|x-1| + |x-3| + |x-6| + |x-13|
//求,此时x=? 结果最小,毫无疑问的是等于中位数的时候
int main() {
int n;cin >> n;
vector<Pair> a(n);
ll sw = 0;
//p初始位置,w权值
for(auto &[p,w]:a){
cin>>w>>p,sw+=w;//计算w的和
}
sort(a.begin(), a.end());//根据pair的第一个大小排序,也就是p
ll nw=0;//左侧的w数据
int x=0;//中位数
for (const auto &[p, w]: a) {
//nw*2<sw<=2*(nw+w) 找到中位数
//因为找到最小的带权中位数
if(nw*2<sw&&sw<=2*(nw+w)){
x=p;break;
}
nw+=w;
}
//10^5*10^5 > 10^8 所以使用long long
ll ans = 0;
for (const auto &[p, w]: a) {
ans+=(ll)w*abs(p-x);
}
cout <<ans<<'\n';
return 0;
}