糖果传递

P2512 [HAOI2008] 糖果传递

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define Ed for(int i=h[x];~i;i=ne[i])
#define Ls(i,l,r) for(int i=l;i<r;++i)
#define Rs(i,l,r) for(int i=l;i>r;--i)
#define Le(i,l,r) for(int i=l;i<=r;++i)
#define Re(i,l,r) for(int i=l;i>=r;--i)
#define L(i,l) for(int i=0;i<l;++i)
#define E(i,l) for(int i=1;i<=l;++i)
#define W(t) while(t--)
#define Wh while
typedef long long ll;

const int N=1000010;
int n,a[N];
ll c[N],b;
int main(){
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    #endif
    scanf("%d",&n);
    E(i, n)scanf("%d",a+i),b+=a[i];
    b/=n;
    E(i, n)c[i]=c[i-1]+a[i-1]-b;
    sort(c+1,c+1+n);
    ll ans=0;
    E(i, n)ans+=abs(c[i]-c[(n>>1)+1]);
    printf("%lld",ans);
    return 0;
}
posted @ 2023-09-25 12:59  wscqwq  阅读(3)  评论(0编辑  收藏  举报