#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<long long int> a;
  long long int n;
  cin >> n;
  long long int sum = 0, m, ans = 0, count = 0;
  ;
  for (long long int i = 0; i < n; i++) {
    long long int x;
    cin >> x;
    sum = sum + x;
    a.push_back(x);
  }
  m = sum / n;
  for (long long int i = 0; i < a.size() - 1; i++) {
    count = m - a[i];
    ans = ans + fabs(count);
    a[i] = m;
    a[i + 1] = a[i + 1] - count;
  }
  cout << ans;
  return 0;
}
