#include <bits/stdc++.h>
using namespace std;
const int MAX = 50010;
int n;
int a[MAX] = {0};
int i;
long long mid;
int main() {
  scanf("%d", &n);
  long long sum = 0;
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    sum += a[i];
  }
  mid = sum / n;
  long long res = 0;
  long long t = 0;
  for (i = 0; i < n; i++) {
    t = (mid - a[i]) + t;
    res += abs(t);
  }
  printf("%lld", res);
  return 0;
}
