糖果传递

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1000010;
 4 typedef long long ll;
 5 ll a[N];
 6 int main() {
 7     ios::sync_with_stdio(false);
 8     cin.tie(0);
 9     cout.tie(0);
10     int n;
11     cin >> n;
12     ll sum = 0;
13     for (int i = 1; i <= n; i++) {
14         cin >> a[i];
15         sum += a[i];
16     }
17     sum /= n;
18     for (int i = n; i > 1; i--) {
19         a[i] = sum - a[i] + a[i + 1];
20     }
21     a[1] = 0;
22     sort(a + 1, a + 1 + n);
23     ll res = 0;
24     for (int i = 1; i <= n; i++) {
25         res += abs(a[i] - a[(n + 1) / 2]);
26     }
27     cout << res << endl;
28     return 0;
29 }

 

posted @ 2020-11-24 11:13  kyk333  阅读(72)  评论(0编辑  收藏  举报