#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;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8ZnN0cmVhbT4gCiNpbmNsdWRlIDxzdGRpby5oPgojaW5jbHVkZSA8c3RkbGliLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG47Cgpsb25nIGxvbmcgY2FsY1ZhbChpbnQgKmRhdCwgbG9uZyBsb25nIHRlc3QpCnsKICAgICAgY291dCA8PCAiQ2FsY3ZhbCAtIGNhbGNvbG8gc3Blc2Eg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gKGNvc3RTVGVzdDxjb3N0KQogICAgICAgICAgICBqPW0tMTsKICAgICAgICBlbHNlIGlmIChjb3N0RFRlc3Q8Y29zdCkKICAgICAgICAgICAgaT1tKzE7ICAgICAgICAgICAgCiAgICB9IHdoaWxlKGNvc3RTVGVzdDxjb3N0IHx8IGNvc3REVGVzdDxjb3N0KTsKICAgIHJldHVybiBjb3N0Owp9CgoKaW50IG1haW4odm9pZCkgewogICAgdW5zaWduZWQgbG9uZyBsb25nIGNvdW50LCBwYXJ0aWFsPTAsIG1lZGlhOwogICAgbG9uZyBkb3VibGUgck1lZGlhPTA7CiAgICBpbnQgTWluPTEwMDAwMDAwMDAsIE1heD0wOwogICAgaWZzdHJlYW0gaW4oImlucHV0LnR4dCIpOwogICAgb2ZzdHJlYW0gb3V0KCJvdXRwdXQudHh0Iik7CiAgICBpbj4+bjsKICAgIGludCBkYXRpW25dOwogICAgZm9yIChpbnQgaT0wOyBpPG47IGkrKykgewogICAgICAgIGluID4+IGRhdGlbaV07CiAgICAgICAgZGF0aVtpXS09aTsgICAgIC8vIHNvdHRyYWdnbyBpbCBsaXZlbGxvIGRhIHJhZ2dpdW5nZXJlLCBsYSBzcGVzYSBzaSBjYWxjb2xhIG9yYSBwZXIgbGl2ZWxsYXJlIGluIG9yaXp6b250YWxlCiAgICAgICAgaWYgKE1pbj5kYXRpW2ldKQogICAgICAgICAgICBNaW49ZGF0aVtpXTsKICAgICAgICBpZiAoTWF4IDwgZGF0aVtpXSkKICAgICAgICAgICAgTWF4PWRhdGlbaV07CiAgICB9CiAgICBpbi5jbG9zZSgpOyAKICAgIAogICAgCiAgICBxc29ydChkYXRpLCBuLCBzaXplb2YoaW50KSwgY29tcGFyZSk7CiAgICAKICAgICBmb3IgKGludCBpPTA7IGk8bjsgaSsrKQogICAgICAgICBjb3V0IDw8IGRhdGlbaV0gPDwgIiAiOwogICAgIGNvdXQgPDwgZW5kbDsgCiAgICAKICAgIGNvdW50PSBmaW5kQk1pbkNvc3QoTWluLCBNYXgsIGRhdGkpOwogICAgCiAgICBjb3V0ICA8PCBjb3VudDsKICAgIAogICAgCiAgICByZXR1cm4gMDsKfQ==