You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
913 B
40 lines
913 B
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
const int N = 10010;
|
|
int a[N];
|
|
int sum, avg, total;
|
|
int n;
|
|
int main() {
|
|
cin >> n;
|
|
for (int i = 1; i <= n; i++) {
|
|
cin >> a[i];
|
|
sum += a[i];
|
|
}
|
|
if (sum % n) {
|
|
cout << -1 << endl;
|
|
exit(0);
|
|
}
|
|
avg = sum / n;
|
|
for (int i = 1; i <= n; i++) {
|
|
int d = a[i] - avg;
|
|
total += abs(d);
|
|
int j = i + 1;
|
|
while (d != 0 && j <= n) {
|
|
if (d > 0 && a[j] < avg) {
|
|
int x = min(d, avg - a[j]);
|
|
a[i] -= x;
|
|
a[j] += x;
|
|
d -= x;
|
|
}
|
|
if (d < 0 && a[j] > avg) {
|
|
int x = min(-d,a[j]-avg);
|
|
a[i] += x;
|
|
a[j] -= x;
|
|
d += x;
|
|
}
|
|
j++;
|
|
}
|
|
}
|
|
cout << total << endl;
|
|
return 0;
|
|
} |