Pagini recente » Cod sursa (job #1899899) | Cod sursa (job #931191) | Cod sursa (job #308586) | Cod sursa (job #2864306) | Cod sursa (job #81896)
Cod sursa(job #81896)
program barman;
const
fin='barman.in';
fout='barman.out';
nmax=600;
var
aa,a,at,obt:array[1..nmax] of longint;
min:longint;
i,j,x,y,n,k,next,vnext:longint;
begin
assign(input,fin);
reset(input);
readln(n);
for i:=1 to n do
read(a[i]);
close(input);
assign(output,fout);
rewrite(output);
aa:=a;
for i:=1 to n-1 do
for j:=i+1 to n do
if aa[i]>aa[j] then
begin
x:=aa[i];aa[i]:=aa[j];aa[j]:=x;
end;
at:=a;
min:=maxlongint;
for i:=1 to n do
begin
for j:=i to n do
obt[j-i+1]:=aa[j];
for j:=1 to i-1 do
obt[n-i+1+j]:=aa[j];
a:=at;
x:=0;
for j:=1 to n do
if a[j]<>obt[j] then
begin
next:=j;
vnext:=a[j];
break;
end;
while vnext<>0 do
begin
k:=next;
inc(x,10);
repeat
inc(k);
if k=n+1 then k:=1;
until (a[k]<>obt[k])and(obt[k]=vnext);
y:=k;
k:=next;
repeat
dec(k);
if k=0 then k:=n;
until (a[k]<>obt[k])and(obt[k]=vnext);
if abs(y-next)<abs(k-next) then
k:=y;
inc(x,abs(next-k));
inc(x,10);
vnext:=a[k];
a[k]:=a[next];
a[next]:=0;
next:=k;
end;
if x<min then
min:=x;
end;
writeln(min);
close(output);
end.