#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, k, sum = 0, arr[50001], rest, temp = 0, sol = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    sum += arr[i];
  }
  k = sum / n;
  for (int i = 0; i < n; i++) {
    temp = arr[i] - temp;
    sol += abs(k - temp);
    temp = k - temp;
  }
  cout << sol << endl;
  return 0;
}
