#include <bits/stdc++.h>
using namespace std;
const int maxn = 50000 + 10;
long long n, l, r, a[maxn], sum, ans;
int main() {
  while (cin >> n) {
    sum = ans = 0;
    l = 0;
    r = n - 1;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }
    sum /= n;
    while (l < r) {
      if (a[l] != sum) {
        if (a[l] > sum) {
          ans += a[l] - sum;
          a[l + 1] += a[l] - sum;
        } else {
          ans += sum - a[l];
          a[l + 1] -= sum - a[l];
        }
      }
      if (a[r] != sum) {
        if (a[r] > sum) {
          ans += a[r] - sum;
          a[r - 1] += a[r] - sum;
        } else {
          ans += sum - a[r];
          a[r - 1] -= sum - a[r];
        }
      }
      l++;
      r--;
    }
    cout << ans << endl;
  }
  return 0;
}
