program aa;
var
ar:array[1..1000] of integer;
br:array[1..1000] of integer;
i,j:integer;
n,k,l,x,o,u,p:integer;
f,f1:text;
begin
l:=0;
assign(f,'chess.in');assign(f1,'chess.out');
reset(f);rewrite(f1);
readln(f,n,k);
for i:=1 to n do
readln(f,ar[i]);
for i:=1 to n do
for j:=1 to n do
if (ar[i]>ar[j]) then begin
inc(l);
br[l]:=ar[i]-ar[j];
end;
for i:=1 to l-1 do
for j:=i to l do
if br[i]>br[j] then begin
x:=br[i];
br[i]:=br[j];
br[j]:=x;
end;
o:=k div 2;
u:=k mod 2;
for i:=1 to o do
p:=p+br[i];
write(f1,p*2+br[k]*u);
close(f);close(f1);
end.