|
|
|
@ -3,8 +3,8 @@ using namespace std;
|
|
|
|
|
const int N = 10010;
|
|
|
|
|
int a[N];
|
|
|
|
|
int sum, avg, total;
|
|
|
|
|
int n;
|
|
|
|
|
int main() {
|
|
|
|
|
int n;
|
|
|
|
|
cin >> n;
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
|
cin >> a[i];
|
|
|
|
@ -15,24 +15,25 @@ int main() {
|
|
|
|
|
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) {
|
|
|
|
|
for (int j = 1 + i; j <= n; j++) {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (d < 0 && a[j] > avg) {
|
|
|
|
|
int x = min(d, a[j]-avg);
|
|
|
|
|
a[i] += x;
|
|
|
|
|
a[j] -= x;
|
|
|
|
|
d += x;
|
|
|
|
|
}
|
|
|
|
|
j++;
|
|
|
|
|
if (d == 0) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cout << total << endl;
|
|
|
|
|