#include <bits/stdc++.h>
using namespace std;
const int maxn = 50005;
long long num[maxn];
int main() {
  long long sum = 0, ans = 0;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) cin >> num[i], sum += num[i];
  if (n == 0) {
    cout << "0" << endl;
    return 0;
  }
  sum = sum / n;
  for (int i = 0; i < n - 1; i++) {
    if (num[i] < sum) {
      ans += sum - num[i];
      num[i + 1] -= (sum - num[i]);
    } else if (num[i] > sum) {
      ans += num[i] - sum;
      num[i + 1] += num[i] - sum;
    }
    num[i] = sum;
  }
  cout << ans << endl;
  return 0;
}
