var
a,s:array[0..105]of longint;
ave,sum,mid,n,i,j,ans,t:longint;
procedure qsort(l,r:longint);
var i,j,xx:longint;
begin
i:=l; j:=r;
xx:=s[(l+r)>>1];
repeat
while s[i]<xx do inc(i);
while s[j]>xx do dec(j);
if i<=j then
begin
if i<>j then
begin
s[i]:=s[i] xor s[j];
s[j]:=s[i] xor s[j];
s[i]:=s[i] xor s[j];
end;
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end;
function abs(x:longint):longint;
begin
if x>0 then exit(x);
exit(-x);
end;
begin
assign(input,'overload.in'); reset(input);
assign(output,'overload.out'); rewrite(output);
readln(n);
sum:=0;
for i:=1 to n do
begin
read(a[i]);
sum:=sum+a[i];
end;
ave:=sum div n;
s[0]:=0;
for i:=1 to n do
s[i]:=s[i-1]+a[i]-ave;
qsort(1,n-1);
mid:=s[n div 2];
ans:=abs(mid);
for i:=1 to n-1 do
ans:=ans+abs(s[i]-mid);
writeln(ans);
readln;
close(input); close(output);
end.