program sairsodo;
const MAXN=100000;
type elenco = array[1..MaXN] of int64;
var N,i,d, idmediana, idmediana1, mediana, mediana1: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*)
mediana1:=(H[idmediana1]+ H[idmediana1 + 1]) div 2;
while uscita=false do
begin
altezzainiziale:=mediana-idmediana; (*vario di +-1 il valore della mediana finchè trovo il costo minore*)
altezzainiziale1:=mediana1-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)) or ((costo=costomin) and (costo1=costomin)) then begin uscita:=true; continue;end;
if costo<costo1 then costomin:=costo
else costomin:=costo1;
writeln (altezzainiziale,' ',altezzainiziale1,' ',costo,' ',costo1,' ', mediana,' ',mediana1);
costo:=0; costo1:=0; d:=d+1;
end;
writeln(costomin);
end;
end.
cHJvZ3JhbSBzYWlyc29kbzsgCmNvbnN0IE1BWE49MTAwMDAwOyAKdHlwZSBlbGVuY28gPSBhcnJheVsxLi5NYVhOXSBvZiBpbnQ2NDsKdmFyIE4saSxkLCBpZG1lZGlhbmEsIGlkbWVkaWFuYTEsIG1lZGlhbmEsIG1lZGlhbmExOmludDY0OwogICAgY2FsY29sYWNvc3RvLGNhbGNvbGFjb3N0bzEsIGFsdGV6emFpbml6aWFsZSwgYWx0ZXp6YWluaXppYWxlMTppbnQ2NDsKICAgIGNvc3RvLCBjb3N0bzEsIGNvc3RvbWluLCByaWNvcmRhY29zdG9taW46aW50NjQ7CiAgICBILCByaWNvcmRhbHRlenplOiBlbGVuY287CiAgICB1c2NpdGE6Ym9vbGVhbjsKUHJvY2VkdXJlIHNjYW1iaWEgKHZhciBhLGI6IGludDY0KTsKdmFyIHg6aW50NjQ7CmJlZ2luCiAgIHg6PWE7CiAgIGE6PWI7CiAgIGI6PXg7CmVuZDsgIApQcm9jZWR1cmUgb3JkaW5hbWVudG8gKGVzdHJlbW9pLGVzdHJlbW9zOiBpbnQ2NDsgdmFyIHYgOiBlbGVuY287IG9yZGluYXRvOmJvb2xlYW4pOwp2YXIgaW5mLCBzdXAsIG1lZGlvOmludDY0OwogICAgcGl2b3QgOmludDY0OwpiZWdpbgogICAgaW5mOj1lc3RyZW1vaTsKICAgIHN1cDo9ZXN0cmVtb3M7CiAgICBtZWRpbzo9IChlc3RyZW1vaStlc3RyZW1vcykgZGl2IDI7CiAgICBwaXZvdDo9dlttZWRpb107CiAgICByZXBlYXQKICAgICAgaWYgKG9yZGluYXRvKSB0aGVuCiAgICAgICAgIGJlZ2luCiAgICAgICAgICAgIHdoaWxlICh2W2luZl08cGl2b3QpIGRvICBpbmY6PWluZisxOwogICAgICAgICAgICB3aGlsZSAodltzdXBdPnBpdm90KSBkbyAgc3VwOj1zdXAtMTsKICAgICAgICAgZW5kOwogICAgICBpZiBpbmY8PXN1cCB0aGVuCiAgICAgICBiZWdpbgogICAgICAgICBzY2FtYmlhKHZbaW5mXSx2W3N1cF0pOwogICAgICAgICBpbmY6PWluZisxOwogICAgICAgICBzdXA6PXN1cC0xOwogICAgICAgZW5kOwogICAgdW50aWwgaW5mPnN1cDsKICAgIGlmIChlc3RyZW1vaTxzdXApIHRoZW4gb3JkaW5hbWVudG8oZXN0cmVtb2ksc3VwLHYsb3JkaW5hdG8pOwogICAgaWYgKGluZjxlc3RyZW1vcykgdGhlbiBvcmRpbmFtZW50byhpbmYsZXN0cmVtb3MsdixvcmRpbmF0byk7CmVuZDsKCmJlZ2luCiAgICgqYXNzaWduKGlucHV0LCAgJ2lucHV0LnR4dCcpOyAgcmVzZXQoaW5wdXQpOwogICAgYXNzaWduKG91dHB1dCwgJ291dHB1dC50eHQnKTsgcmV3cml0ZShvdXRwdXQpOyopCglyZWFkbG4oTik7Cglmb3IgaTo9MSB0byBOIGRvIGJlZ2luIHJlYWQoSFtpXSk7IHJpY29yZGFsdGV6emVbaV06PUhbaV07IGVuZDtyZWFkbG47Cgljb3N0bzo9MDsgY29zdG8xOj0wOyBkOj0wOyBjb3N0b21pbjo9OTIyMzM3MjAzNjg1NDc3NTgwNzsgcmljb3JkYWNvc3RvbWluOj05MjIzMzcyMDM2ODU0Nzc1ODA3OyB1c2NpdGE6PWZhbHNlOwoJb3JkaW5hbWVudG8gKDEsTixILCB0cnVlKTsKCWlmIE4gbW9kIDIgPD4wIHRoZW4gCgkgICAgICAgICAgICAgICAgIGJlZ2luCgkgICAgICAgICAgICAgICAgICAgaWRtZWRpYW5hOj0oTisxKSBkaXYgMjsKCSAgICAgICAgICAgICAgICAgICBhbHRlenphaW5pemlhbGU6PUhbaWRtZWRpYW5hXS0oaWRtZWRpYW5hLTEpOwoJICAgICAgICAgICAgICAgICAgIGlmIGFsdGV6emFpbml6aWFsZTwwIHRoZW4gYWx0ZXp6YWluaXppYWxlOj0wOwoJICAgICAgICAgICAgICAgICAgIGZvciBpOj0xIHRvIE4gZG8KCSAgICAgICAgICAgICAgICAgICAgICAgICBiZWdpbiAKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY2FsY29sYWNvc3RvOj0ocmljb3JkYWx0ZXp6ZVtpXSAtIChhbHRlenphaW5pemlhbGUgK2ktMSkpOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiBjYWxjb2xhY29zdG88MCB0aGVuIGNhbGNvbGFjb3N0bzo9LWNhbGNvbGFjb3N0bzsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY29zdG86PSBjb3N0byArIGNhbGNvbGFjb3N0byA7CgkgICAgICAgICAgICAgICAgICAgICAgICAgZW5kOwoJICAgICAgICAgICAgICAgICAgICAgIHdyaXRlbG4oY29zdG8pOwoJICAgICAgICAgICAgICAgIGVuZAoJICAgICAgICAgICAgICBlbHNlIAoJICAgICAgICAgICAgICAgYmVnaW4KCSAgICAgICAgICAgICAgICBpZG1lZGlhbmE6PSBOIGRpdiAyOwoJICAgICAgICAgICAgICAgaWRtZWRpYW5hMTo9aWRtZWRpYW5hKzE7CgkgICAgICAgICAgICAgICAgbWVkaWFuYTo9IChIW2lkbWVkaWFuYV0rIEhbaWRtZWRpYW5hICsgMV0pIGRpdiAyOyAoKmFsdGV6emEgY2hlIGNvcnJpc3BvbmRlIGFsbGEgbWVkaWFuYSopCgkgICAgICAgICAgICAgICAgbWVkaWFuYTE6PShIW2lkbWVkaWFuYTFdKyBIW2lkbWVkaWFuYTEgKyAxXSkgZGl2IDI7CgkgICAgICAgICAgICAgICAgd2hpbGUgdXNjaXRhPWZhbHNlIGRvIAoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYmVnaW4KCSAgICAgICAgICAgICAgICAgICAgIAkgICAgICAgICAgICAgICAgYWx0ZXp6YWluaXppYWxlOj1tZWRpYW5hLWlkbWVkaWFuYTsgKCp2YXJpbyBkaSArLTEgaWwgdmFsb3JlIGRlbGxhIG1lZGlhbmEgZmluY2jDqCB0cm92byBpbCBjb3N0byBtaW5vcmUqKQoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGFsdGV6emFpbml6aWFsZTE6PW1lZGlhbmExLWlkbWVkaWFuYTE7IAoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHdoaWxlIGFsdGV6emFpbml6aWFsZTwwIGRvIGFsdGV6emFpbml6aWFsZTo9YWx0ZXp6YWluaXppYWxlKzE7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgd2hpbGUgYWx0ZXp6YWluaXppYWxlMTwwIGRvIGFsdGV6emFpbml6aWFsZTE6PWFsdGV6emFpbml6aWFsZTErMTsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiBhbHRlenphaW5pemlhbGU8MCB0aGVuIGFsdGV6emFpbml6aWFsZTo9MDsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiBhbHRlenphaW5pemlhbGUxPDAgdGhlbiBhbHRlenphaW5pemlhbGUxOj0wOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZvciBpOj0xIHRvIE4gZG8KCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgYmVnaW4gCgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNhbGNvbGFjb3N0bzo9YWJzKHJpY29yZGFsdGV6emVbaV0tZCAtIGFsdGV6emFpbml6aWFsZS1pKzEpOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjb3N0bzo9IGNvc3RvICsgY2FsY29sYWNvc3RvIDsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY2FsY29sYWNvc3RvMTo9YWJzKHJpY29yZGFsdGV6emVbaV0rZCAtIGFsdGV6emFpbml6aWFsZTEtaSsxKTsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY29zdG8xOj0gY29zdG8xICsgY2FsY29sYWNvc3RvMSA7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbmQ7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiAoKGNvc3RvPmNvc3RvbWluKSBhbmQgKGNvc3RvMT5jb3N0b21pbikpIG9yICgoY29zdG89Y29zdG9taW4pIGFuZCAoY29zdG8xPWNvc3RvbWluKSkgdGhlbiBiZWdpbiB1c2NpdGE6PXRydWU7ICBjb250aW51ZTtlbmQ7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpZiBjb3N0bzxjb3N0bzEgdGhlbiBjb3N0b21pbjo9Y29zdG8KCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbHNlIGNvc3RvbWluOj1jb3N0bzE7CgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHdyaXRlbG4gKGFsdGV6emFpbml6aWFsZSwnICcsYWx0ZXp6YWluaXppYWxlMSwnICcsY29zdG8sJyAnLGNvc3RvMSwnICcsIG1lZGlhbmEsJyAnLG1lZGlhbmExKTsKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgY29zdG86PTA7IGNvc3RvMTo9MDsgZDo9ZCsxOwoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBlbmQ7CgkgICAgIHdyaXRlbG4oY29zdG9taW4pOyAgICAgICAgICAKCSAgZW5kOwplbmQuCg==