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