#include <bits/stdc++.h>
using namespace std;
void print(int x) { cerr << x; }
void print(long x) { cerr << x; }
void print(long long x) { cerr << x; }
void print(unsigned x) { cerr << x; }
void print(unsigned long x) { cerr << x; }
void print(unsigned long long x) { cerr << x; }
void print(float x) { cerr << x; }
void print(double x) { cerr << x; }
void print(long double x) { cerr << x; }
void print(char x) { cerr << '\'' << x << '\''; }
void print(const char *x) { cerr << '\"' << x << '\"'; }
void print(const string &x) { cerr << '\"' << x << '\"'; }
void print(bool x) { cerr << (x ? "true" : "false"); }
const long long int MOD = 1e9 + 7;
const long long int INF = 1e18;
const long long int maxn = 1e6 + 4;
void answer() {
  long long int a;
  cin >> a;
  vector<long long int> v;
  long long int sum = 0;
  for (long long int i = 0; i < a; ++i) {
    long long int temp;
    cin >> temp;
    v.push_back(temp);
    sum += temp;
  }
  long long int k = sum / a;
  long long int ans = 0;
  if (a == 1) {
    cout << 0;
  } else {
    for (long long int i = 0; i < a - 1; ++i) {
      if (v[i] < k) {
        long long int real = k - v[i];
        ans += real;
        v[i + 1] -= real;
        v[i] = k;
      } else {
        long long int real = v[i] - k;
        ans += real;
        v[i + 1] += real;
        v[i] = k;
      }
    }
    cout << ans;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t-- > 0) {
    answer();
  }
  return 0;
}
