#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <fstream>
#include <stdlib.h>
#define rep( i, l, r ) for (int i = l; i <= r; i++)
#define down( i, l, r ) for (int i = l; i >= r; i--)
#define MAX 1000009
using namespace std;
int n, num[MAX];
long long a;
int main()
{
scanf("%d", &n);
rep(i, 1, n) scanf("%d", &num[i]);
a = 0; rep(i, 1, n) a += num[i]; a /= n;
rep(i, 1, n) num[i] = a - num[i] + num[i-1];
sort(num + 1, num + n + 1);
a = num[n / 2 + 1];
rep(i, 1, n) num[i] -= a;
a = 0;
rep(i, 1, n) a += abs(num[i]);
printf("%lld", a);
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0cmluZz4KI2luY2x1ZGUgPGNtYXRoPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojaW5jbHVkZSA8ZnN0cmVhbT4KI2luY2x1ZGUgPHN0ZGxpYi5oPgoKI2RlZmluZSByZXAoIGksIGwsIHIgKSBmb3IgKGludCBpID0gbDsgaSA8PSByOyBpKyspCiNkZWZpbmUgZG93biggaSwgbCwgciApIGZvciAoaW50IGkgPSBsOyBpID49IHI7IGktLSkKI2RlZmluZSBNQVggMTAwMDAwOQoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBuLCBudW1bTUFYXTsKbG9uZyBsb25nIGE7CgppbnQgbWFpbigpCnsKCXNjYW5mKCIlZCIsICZuKTsgCglyZXAoaSwgMSwgbikgc2NhbmYoIiVkIiwgJm51bVtpXSk7CglhID0gMDsgcmVwKGksIDEsIG4pIGEgKz0gbnVtW2ldOyBhIC89IG47CglyZXAoaSwgMSwgbikgbnVtW2ldID0gYSAtIG51bVtpXSArIG51bVtpLTFdOwoJc29ydChudW0gKyAxLCBudW0gKyBuICsgMSk7CglhID0gbnVtW24gLyAyICsgMV07CglyZXAoaSwgMSwgbikgbnVtW2ldIC09IGE7CglhID0gMDsKCXJlcChpLCAxLCBuKSBhICs9IGFicyhudW1baV0pOwoJcHJpbnRmKCIlbGxkIiwgYSk7CglyZXR1cm4gMDsKfQo=