Skip to content

Instantly share code, notes, and snippets.

@ateruimashin
Created July 10, 2018 04:52
Show Gist options
  • Save ateruimashin/34231996d4265cba875bd19d3cb31f24 to your computer and use it in GitHub Desktop.
Save ateruimashin/34231996d4265cba875bd19d3cb31f24 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
using ll = long long;
int main(int argc, char const *argv[]) {
int n;
cin>>n;
ll a[n];
for(int i=0;i<n;i++){
ll tmp;
cin>>tmp;
a[i]=tmp-i;
}
ll med1=0,med2=0,median=0,ans=0;
sort(a,a+n);
if(n%2==0){
med1=a[n/2-1];
med2=a[n/2];
median=(med1+med2)/2;
}else{
median=a[n/2];
}
for(int i=0;i<n;i++){
ans+=abs(a[i]-median);
}
cout<<ans<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment