#include <bits/stdc++.h>
using namespace std;
const int M = 50 * 1000 + 100;
long long a[M];
vector<int> v;
long long n, k, ans, p;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    k += a[i];
  }
  for (int i = 0; i < n; i++)
    if (a[i] > k / n) v.push_back(i);
  for (int i = 0; i < n; i++)
    if (a[i] > k / n) {
      if (v[p] == i) p++;
      a[i + 1] += a[i] - k / n;
      ans += a[i] - k / n;
      a[i] = k / n;
    } else if (a[i] < k / n) {
      int h = v[p];
      ans += (k / n - a[i]) * (h - i);
      a[h] -= k / n - a[i];
      a[i] = k / n;
      if (a[h] <= k / n) p++;
    }
  cout << ans << endl;
  return 0;
}
