#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int n;
long long calcVal(int *dat, long long test)
{
// cout << "Calcval - calcolo spesa per livello: " << test << endl;
long long count=0;
for (int i=0; i<n; i++) {
count+=abs(test-dat[i]);
}
return count;
}
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
long long findBMinCost(int Min, int Max, int *data) {
long long cost, costDTest, costSTest;
int dTest, i=Min, j=Max, m;
dTest=1;
do {
m=(i+j)/2; //m=metà intervallo
// provo valori centrale, sinistro e destro
costSTest=calcVal(data, m-dTest);
cost=calcVal(data, m);
costDTest=calcVal(data, m+dTest);
// cout << "Livello " << m-dTest << " costo "<< costSTest << endl;
// cout << "Livello " << m << " costo "<< cost << endl;
// cout << "Livello " << m+dTest << " costo "<< costDTest << endl;
if (costSTest<cost)
j=m-1;
else if (costDTest<cost)
i=m+1;
} while(costSTest<cost || costDTest<cost);
return cost;
}
int main(void) {
unsigned long long count, partial=0, media;
long double rMedia=0;
int Min=1000000000, Max=0;
ifstream in("input.txt");
ofstream out("output.txt");
in>>n;
int dati[n];
for (int i=0; i<n; i++) {
in >> dati[i];
dati[i]-=i; // sottraggo il livello da raggiungere, la spesa si calcola ora per livellare in orizzontale
if (Min>dati[i])
Min=dati[i];
if (Max < dati[i])
Max=dati[i];
}
in.close();
qsort(dati, n, sizeof(int), compare);
// for (int i=0; i<n; i++)
// cout << dati[i] << " ";
// cout << endl;
count= findBMinCost(Min, Max, dati);
// cout << count;
out << count;
out.close();
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4gCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG47Cgpsb25nIGxvbmcgY2FsY1ZhbChpbnQgKmRhdCwgbG9uZyBsb25nIHRlc3QpCnsKICAgIC8vICAgICBjb3V0IDw8ICJDYWxjdmF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byAiPDwgY29zdERUZXN0IDw8IGVuZGw7CiAgICAgICAgaWYgKGNvc3RTVGVzdDxjb3N0KQogICAgICAgICAgICBqPW0tMTsKICAgICAgICBlbHNlIGlmIChjb3N0RFRlc3Q8Y29zdCkKICAgICAgICAgICAgaT1tKzE7ICAgICAgICAgICAgCiAgICB9IHdoaWxlKGNvc3RTVGVzdDxjb3N0IHx8IGNvc3REVGVzdDxjb3N0KTsKICAgIHJldHVybiBjb3N0Owp9CgoKaW50IG1haW4odm9pZCkgewogICAgdW5zaWduZWQgbG9uZyBsb25nIGNvdW50LCBwYXJ0aWFsPTAsIG1lZGlhOwogICAgbG9uZyBkb3VibGUgck1lZGlhPTA7CiAgICBpbnQgTWluPTEwMDAwMDAwMDAsIE1heD0wOwogICAgaWZzdHJlYW0gaW4oImlucHV0LnR4dCIpOwogICAgb2ZzdHJlYW0gb3V0KCJvdXRwdXQudHh0Iik7CiAgICBpbj4+bjsKICAgIGludCBkYXRpW25dOwogICAgZm9yIChpbnQgaT0wOyBpPG47IGkrKykgewogICAgICAgIGluID4+IGRhdGlbaV07CiAgICAgICAgZGF0aVtpXS09aTsgICAgIC8vIHNvdHRyYWdnbyBpbCBsaXZlbGxvIGRhIHJhZ2dpdW5nZXJlLCBsYSBzcGVzYSBzaSBjYWxjb2xhIG9yYSBwZXIgbGl2ZWxsYXJlIGluIG9yaXp6b250YWxlCiAgICAgICAgaWYgKE1pbj5kYXRpW2ldKQogICAgICAgICAgICBNaW49ZGF0aVtpXTsKICAgICAgICBpZiAoTWF4IDwgZGF0aVtpXSkKICAgICAgICAgICAgTWF4PWRhdGlbaV07CiAgICB9CiAgICBpbi5jbG9zZSgpOyAKICAgIAogICAgCiAgICBxc29ydChkYXRpLCBuLCBzaXplb2YoaW50KSwgY29tcGFyZSk7CiAgICAKLy8gICAgIGZvciAoaW50IGk9MDsgaTxuOyBpKyspCi8vICAgICAgICAgY291dCA8PCBkYXRpW2ldIDw8ICIgIjsKLy8gICAgIGNvdXQgP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