#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, Media = 0, Balance = 0;
  cin >> n;
  vector<long long int> v;
  for (long long int i = 0; i < n; ++i) {
    long long int a;
    cin >> a;
    v.push_back(a);
    Media += a;
  }
  Media = Media / n;
  for (long long int i = 0; i < n; ++i) {
    long long int ValorAB = 0;
    if (v[i] < Media) {
      ValorAB = abs(v[i] - Media);
      v[i] += ValorAB;
      v[i + 1] -= ValorAB;
      Balance += ValorAB;
    } else if (v[i] > Media) {
      ValorAB = abs(v[i] - Media);
      v[i] -= ValorAB;
      v[i + 1] += ValorAB;
      Balance += ValorAB;
    } else
      continue;
  }
  cout << Balance;
  return 0;
}
