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