const
fi='gather.inp';
fo='gather.out';
var
f:text;
a:array[1..1000000] of longint;
m,s:int64;
i,j,n:longint;
procedure nhap;
        begin
        assign(f,fi);
        reset(f);
        readln(f,n);
        for i:=1 to n do read(f,a[i]);
        close(f);
        end;
procedure sort(l,r:longint);
        var
        i,j,tam,x:longint;
        begin
        i:=l;
        j:=r;
        x:=a[l+random(r-l+1)];
        repeat
        while a[i]<x do inc(i);
        while a[j]>x do dec(j);
        if i<=j then
                begin
                tam:=a[i];
                a[i]:=a[j];
                a[j]:=tam;
                inc(i);
                dec(j);
                end;
        until i>j;
        if i<r then sort(i,r);
        if l<j then sort(l,j);
        end;
procedure xuat;
        begin
        assign(f,fo);
        rewrite(f);
        sort(1,n);
        m:=a[(n+1) div 2];
        s:=0;
        for i:=1 to n do s:=s+abs(a[i]-m);
        write(f,s);
        close(f);
        end;
begin
nhap;
xuat;
end.
