UVA 11300(p4)----Spreading the Wealth

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=1e6+50;
int n;
LL tot;
LL c[maxn],a[maxn];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        tot=0;
        for(int i=0; i<n; i++)
        {
            scanf("%lld",&a[i]);
            tot+=a[i];
        }
        c[0]=0;
        LL ave=tot/n;
        for(int i=1; i<n; i++)
            c[i]=c[i-1]+a[i]-ave;
        sort(c,c+n);
        LL x1=c[n/2],ans=0;
        for(int i=0; i<n; i++)
            ans+=abs(x1-c[i]);
        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(UVA 11300(p4)----Spreading the Wealth)