#include <bits/stdc++.h>
using namespace std;
long long s = 0, ans = 0;
long long a[50005];
int main() {
  int i, n;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    s += a[i];
  }
  s /= n;
  for (i = 1; i <= n; i++) {
    if (s - a[i] >= 0)
      ans += s - a[i];
    else
      ans += a[i] - s;
    a[i + 1] -= s - a[i];
    a[i] += s - a[i];
  }
  cout << ans;
  return 0;
}
