AcWing 122. 糖果传递
原题链接
中等
作者:
牛奶小柒Luke
,
2021-03-01 22:44:42
,
所有人可见
,
阅读 270
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 1000010;
int n;
LL a[N],c[N];
int main(){
cin >> n;
for(int i = 1;i <= n;++i){
cin >> a[i];
a[i] += a[i - 1];
}
LL b = a[n] / n;
int k = 0;
for(int i = 1;i < n;++i){
c[k++] = i * b - a[i];
}
c[k++] = 0;
nth_element(c,c + k / 2,c + k);
LL res = 0;
for(int i = 0;i < k;++i){
res += abs(c[i] - c[k / 2]);
}
cout << res << endl;
return 0;
}