#include <bits/stdc++.h>
using namespace std;
int n;
long long k[50000], ans = 0, avg = 0;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 0; i < (n); ++i) {
    cin >> k[i];
    avg += k[i];
  }
  avg /= n;
  for (int i = 0; i < (n); ++i) {
    ans += abs(k[i] - avg);
    if (k[i] > avg) {
      k[i + 1] += abs(k[i] - avg);
    } else if (k[i] < avg) {
      k[i + 1] -= abs(k[i] - avg);
    }
  }
  cout << ans;
  return 0;
}
