#include <bits/stdc++.h>
using namespace std;
const double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230781641;
const long long int M = 1e9;
const long long int MAXN = 100200;
long long int i, j, mask;
void solve() {
  long long int n;
  cin >> n;
  vector<long long int> v(n + 1);
  long long int sum = 0;
  for (i = 0; i < n; i++) {
    cin >> v[i];
    sum += v[i];
  }
  sum /= n;
  long long int ans = 0;
  for (i = 0; i < n; i++) {
    if (v[i] < sum) {
      v[i + 1] -= (sum - v[i]);
      ans += (sum - v[i]);
      v[i] = sum;
    } else {
      v[i + 1] += (v[i] - sum);
      ans += v[i] - sum;
      v[i] = sum;
    }
  }
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long int t, test = 1;
  for (t = 1; t < test + 1; t++) {
    solve();
    cout << "\n";
  }
  return 0;
}
