就简单的均分下就好啦
1 int n; 2 3 int main() { 4 freopen("candya.in","r",stdin); 5 freopen("candya.out","w",stdout); 6 scanf("%d",&n); 7 ll sum=0; 8 for(int i=1;i<=n;i++) { 9 scanf("%lld",&a[i]); 10 sum+=a[i]; 11 } 12 ll average=sum/n; 13 for(int i=1;i<=n;i++) a[i]-=average; 14 for(int i=1;i<=n;i++) S[i]=S[i-1]+a[i]; 15 sort(S+1,S+n+1); 16 ll ans=0; 17 for(int i=1;i<=n;i++) ans+=abs(S[i]-S[(n+1)/2]); 18 cout<<ans<<endl; 19 return 0; 20 }