#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  long long a[100005], n;
  cin >> n;
  long long s = 0, res = 0;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    s += a[i];
  }
  s /= n;
  for (long long i = 1; i <= n; i++) {
    if (a[i] < s) {
      long long d = s - a[i];
      a[i] += d;
      a[i + 1] -= d;
      res += d;
    } else if (a[i] > s) {
      long long d = a[i] - s;
      a[i] -= d;
      a[i + 1] += d;
      res += d;
    }
  }
  cout << res << endl;
  return 0;
}
