#include <bits/stdc++.h>
using namespace std;
const long long N = 2002002;
long long a[N];
long long ans, sum;
int main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  sum /= n;
  for (long long i = 0; i < n - 1; i++) {
    if (a[i] < sum) {
      ans += sum - a[i];
      a[i + 1] -= sum - a[i];
    } else if (a[i] > sum) {
      ans += a[i] - sum;
      a[i + 1] += a[i] - sum;
    }
  }
  cout << ans;
  return 0;
}
