#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  vector<long long> v(n);
  long long sum = 0;
  for (long long i = 0; i < (n); ++i) {
    cin >> v[i];
    sum += v[i];
  }
  sum /= n;
  long long ans = 0;
  for (long long i = 0; i < (n); ++i) {
    if (v[i] == sum) continue;
    if (v[i] > sum) {
      long long rem = v[i] - sum;
      v[i] = sum;
      v[i + 1] += rem;
      ans += rem;
      continue;
    }
    long long rem = sum - v[i];
    v[i] += rem;
    v[i + 1] -= rem;
    ans += rem;
  }
  cout << ans;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
