program sairsodo;
const MAXN=100000;
type elenco = array[1..MaXN] of int64;
var N,i,d, idmediana, idmediana1, mediana:int64;
calcolacosto,calcolacosto1, altezzainiziale, altezzainiziale1:int64;
costo, costo1, costomin, ricordacostomin:int64;
H, ricordaltezze: elenco;
uscita:boolean;
Procedure scambia (var a,b: int64);
var x:int64;
begin
x:=a;
a:=b;
b:=x;
end;
Procedure ordinamento (estremoi,estremos: int64; var v : elenco; ordinato:boolean);
var inf, sup, medio:int64;
pivot :int64;
begin
inf:=estremoi;
sup:=estremos;
medio:= (estremoi+estremos) div 2;
pivot:=v[medio];
repeat
if (ordinato) then
begin
while (v[inf]<pivot) do inf:=inf+1;
while (v[sup]>pivot) do sup:=sup-1;
end;
if inf<=sup then
begin
scambia(v[inf],v[sup]);
inf:=inf+1;
sup:=sup-1;
end;
until inf>sup;
if (estremoi<sup) then ordinamento(estremoi,sup,v,ordinato);
if (inf<estremos) then ordinamento(inf,estremos,v,ordinato);
end;
begin
(* assign(input, 'input.txt'); reset(input);
assign(output, 'output.txt'); rewrite(output);*)
readln(N);
for i:=1 to N do begin read(H[i]); ricordaltezze[i]:=H[i]; end;readln;
costo:=0; costo1:=0; d:=0; costomin:=9223372036854775807; ricordacostomin:=9223372036854775807; uscita:=false;
ordinamento (1,N,H, true);
if N mod 2 <>0 then
begin
idmediana:=(N+1) div 2;
altezzainiziale:=H[idmediana]-(idmediana-1);
if altezzainiziale<0 then altezzainiziale:=0;
for i:=1 to N do
begin
calcolacosto:=(ricordaltezze[i] - (altezzainiziale +i-1));
if calcolacosto<0 then calcolacosto:=-calcolacosto;
costo:= costo + calcolacosto ;
end;
writeln(costo);
end
else
begin
idmediana:= N div 2;
idmediana1:=idmediana+1;
mediana:= (H[idmediana]+ H[idmediana + 1]) div 2; (*altezza che corrisponde alla mediana*)
while uscita=false do
begin
altezzainiziale:=mediana-idmediana; (*vario di +-1 il valore della mediana finchè trovo il costo minore*)
altezzainiziale1:=mediana-idmediana1;
while altezzainiziale<0 do altezzainiziale:=altezzainiziale+1;
while altezzainiziale1<0 do altezzainiziale1:=altezzainiziale1+1;
if altezzainiziale<0 then altezzainiziale:=0;
if altezzainiziale1<0 then altezzainiziale1:=0;
for i:=1 to N do
begin
calcolacosto:=abs(ricordaltezze[i]-d - altezzainiziale-i+1);
costo:= costo + calcolacosto ;
calcolacosto1:=abs(ricordaltezze[i]+d - altezzainiziale1-i+1);
costo1:= costo1 + calcolacosto1 ;
end;
if (costo>=costomin) and (costo1>=costomin) then begin uscita:=true; continue; end;
if costo<costo1 then costomin:=costo
else costomin:=costo1;
writeln(costo,' ', costo1,' ', costomin,' ', altezzainiziale,' ',altezzainiziale1);
costo:=0; costo1:=0; d:=d+1;
end;
writeln(costomin);
end;
end.
cHJvZ3JhbSBzYWlyc29kbzsKY29uc3QgTUFYTj0xMDAwMDA7IAp0eXBlIGVsZW5jbyA9IGFycmF5WzEuLk1hWE5dIG9mIGludDY0Owp2YXIgTixpLGQsIGlkbWVkaWFuYSwgaWRtZWRpYW5hMSwgbWVkaWFuYTppbnQ2NDsKICAgIGNhbGNvbGFjb3N0byxjYWxjb2xhY29zdG8xLCBhbHRlenphaW5pemlhbGUsIGFsdGV6emFpbml6aWFsZTE6aW50NjQ7CiAgICBjb3N0bywgY29zdG8xLCBjb3N0b21pbiwgcmljb3JkYWNvc3RvbWluOmludDY0OwogICAgSCwgcmljb3JkYWx0ZXp6ZTogZWxlbmNvOwogICAgdXNjaXRhOmJvb2xlYW47ClByb2NlZHVyZSBzY2FtYmlhICh2YXIgYSxiOiBpbnQ2NCk7CnZhciB4OmludDY0OwpiZWdpbgogICB4Oj1hOwogICBhOj1iOwogICBiOj14OwplbmQ7ICAKUHJvY2VkdXJlIG9yZGluYW1lbnRvIChlc3RyZW1vaSxlc3RyZW1vczogaW50NjQ7IHZhciB2IDogZWxlbmNvOyBvcmRpbmF0bzpib29sZWFuKTsKdmFyIGluZiwgc3VwLCBtZWRpbzppbnQ2NDsKICAgIHBpdm90IDppbnQ2ND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wMzY4NTQ3NzU4MDc7IHVzY2l0YTo9ZmFsc2U7CglvcmRpbmFtZW50byAoMSxOLEgsIHRydWUpOwoJaWYgTiBtb2QgMiA8PjAgdGhlbiAKCSAgICAgICAgICAgICAgICAgYmVnaW4KCSAgICAgICAgICAgICAgICAgICBpZG1lZGlhbmE6PShOKzEpIGRpdiAyOwoJICAgICAgICAgICAgICAgICAgIGFsdGV6emFpbml6aWFsZTo9SFtpZG1lZGlhbmFdLShpZG1lZGlhbmEtMSk7CgkgICAgICAgICAgICAgICAgICAgaWYgYWx0ZXp6YWluaXppYWxlPDAgdGhlbiBhbHRlenphaW5pemlhbGU6PTA7CgkgICAgICAgICAgICAgICAgICAgZm9yIGk6PTEgdG8gTiBkbwoJICAgICAgICAgICAgICAgICAgICAgICAgIGJlZ2luIAoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjYWxjb2xhY29zdG86PShyaWNvcmRhbHRlenplW2ldIC0gKGFsdGV6emFpbml6aWFsZSAraS0xKSk7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIGNhbGNvbGFjb3N0bzwwIHRoZW4gY2FsY29sYWNvc3RvOj0tY2FsY29sYWNvc3RvOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb3N0bzo9IGNvc3RvICsgY2FsY29sYWNvc3RvIDsKCSAgICAgICAgICAgICAgICAgICAgICAgICBlbmQ7CgkgICAgICAgICAgICAgICAgICAgICAgd3JpdGVsbihjb3N0byk7CgkgICAgICAgICAgICAgICAgZW5kCgkgICAgICAgICAgICAgIGVsc2UgCgkgICAgICAgICAgICAgICBiZWdpbgoJICAgICAgICAgICAgICAgIGlkbWVkaWFuYTo9IE4gZGl2IDI7CgkgICAgICAgICAgICAgICAgaWRtZWRpYW5hMTo9aWRtZWRpYW5hKzE7CgkgICAgICAgICAgICAgICAgbWVkaWFuYTo9IChIW2lkbWVkaWFuYV0rIEhbaWRtZWRpYW5hICsgMV0pIGRpdiAyOyAoKmFsdGV6emEgY2hlIGNvcnJpc3BvbmRlIGFsbGEgbWVkaWFuYSopCgkgICAgICAgICAgICAgICAgCgkgICAgICAgICAgICAgICAKCSAgICAgICAgICAgICAgICB3aGlsZSB1c2NpdGE9ZmFsc2UgZG8gCgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBiZWdpbgoJICAgICAgICAgICAgICAgICAgICAgCSAgICAgICAgICAgICAgICBhbHRlenphaW5pemlhbGU6PW1lZGlhbmEtaWRtZWRpYW5hOyAoKnZhcmlvIGRpICstMSBpbCB2YWxvcmUgZGVsbGEgbWVkaWFuYSBmaW5jaMOoIHRyb3ZvIGlsIGNvc3RvIG1pbm9yZSopCgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYWx0ZXp6YWluaXppYWxlMTo9bWVkaWFuYS1pZG1lZGlhbmExOyAKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB3aGlsZSBhbHRlenphaW5pemlhbGU8MCBkbyBhbHRlenphaW5pemlhbGU6PWFsdGV6emFpbml6aWFsZSsxOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHdoaWxlIGFsdGV6emFpbml6aWFsZTE8MCBkbyBhbHRlenphaW5pemlhbGUxOj1hbHRlenphaW5pemlhbGUxKzE7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiBhbHRlenphaW5pemlhbGU8MCB0aGVuIGFsdGV6emFpbml6aWFsZTo9MDsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiBhbHRlenphaW5pemlhbGUxPDAgdGhlbiBhbHRlenphaW5pemlhbGUxOj0wOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZvciBpOj0xIHRvIE4gZG8KCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYmVnaW4gCgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNhbGNvbGFjb3N0bzo9YWJzKHJpY29yZGFsdGV6emVbaV0tZCAtIGFsdGV6emFpbml6aWFsZS1pKzEpOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb3N0bzo9IGNvc3RvICsgY2FsY29sYWNvc3RvIDsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY2FsY29sYWNvc3RvMTo9YWJzKHJpY29yZGFsdGV6emVbaV0rZCAtIGFsdGV6emFpbml6aWFsZTEtaSsxKTsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY29zdG8xOj0gY29zdG8xICsgY2FsY29sYWNvc3RvMSA7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbmQ7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYgKGNvc3RvPj1jb3N0b21pbikgYW5kIChjb3N0bzE+PWNvc3RvbWluKSB0aGVuIGJlZ2luIHVzY2l0YTo9dHJ1ZTsgY29udGludWU7IGVuZDsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIGNvc3RvPGNvc3RvMSB0aGVuIGNvc3RvbWluOj1jb3N0bwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVsc2UgY29zdG9taW46PWNvc3RvMTsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHdyaXRlbG4oY29zdG8sJyAnLCBjb3N0bzEsJyAnLCBjb3N0b21pbiwnICcsIGFsdGV6emFpbml6aWFsZSwnICcsYWx0ZXp6YWluaXppYWxlMSk7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNvc3RvOj0wOyBjb3N0bzE6PTA7IGQ6PWQrMTsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgZW5kOwoJICAgICB3cml0ZWxuKGNvc3RvbWluKTsgICAgICAgICAgCgkgIGVuZDsKZW5kLg==