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