好神的题。
http://hzwer.com/2656.html
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#define maxn 1000010
using namespace std;
int a[maxn];
long long c[maxn];
int n,m;
long long ans,sum;
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;i++) {scanf("%d",&a[i]);sum+=a[i];}
sum/=n;
for (int i=1;i<=n;i++) c[i]=c[i-1]+a[i]-sum;
sort(c+1,c+n+1);
int mid=c[(n>>1)+1];
for (int i=1;i<=n;i++) ans+=abs(c[i]-mid);
printf("%lld\n",ans);
return 0;
}