Problem p03311 (C++)

Buggy submission (status: Runtime Error):
// cwystc <--> 陈威宇是天才  --by cwy
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
const double PI = acos(-1);
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define Rep(i, a, b) for (int i = (a); i < (b); i++)
#define Per(i, a, b) for (int i = (a); i > (b); i--)
#define travel_set(it, a)                                                      \
  for (set<int>::iterator(it) = (a).begin(); (it) != (a).end(); (it)++)
#define travel_map(it, a)                                                      \
  for (map<int>::iterator(it) = (a).begin(); (it) != (a).end(); (it)++)
#define travel_vec(it, a)                                                      \
  for (vector<int>::iterator(it) = (a).begin(); (it) != (a).end(); (it)++)
#define p(x) cout << x << endl;
#define def cout << "find" << endl;
#define what_is(x) cout << #x << " is " << x << endl;
template <class T> T sqr(T x) { return x * x; }
template <class T> T alex(T x) { return (x > 0) ? x : -x; }
template <class T> void read(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while ((ch < 48 || ch > 57) && ch != 45)
    ch = getchar();
  if (ch == 45)
    f = -1, ch = getchar();
  while (ch >= 48 && ch <= 57)
    x = x * 10 + ch - 48, ch = getchar();
  x *= f;
}

//-------------------------------------------------head-------------------------------------------------

#define maxn 100005
int n, a[maxn];
int _abs(int x) { return (x < 0) ? -x : x; }
int main() {
  read(n);
  rep(i, 1, n) read(a[i]), a[i] -= i;
  sort(a + 1, a + n + 1);
  ll res = 0;
  rep(i, 1, n) res += _abs(a[i] - (a[(n + 1) / 2]));
  printf("%lld\n", res);
  return 0;
}

Fixed submission:
// cwystc <--> 陈威宇是天才  --by cwy
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
const double PI = acos(-1);
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define Rep(i, a, b) for (int i = (a); i < (b); i++)
#define Per(i, a, b) for (int i = (a); i > (b); i--)
#define travel_set(it, a)                                                      \
  for (set<int>::iterator(it) = (a).begin(); (it) != (a).end(); (it)++)
#define travel_map(it, a)                                                      \
  for (map<int>::iterator(it) = (a).begin(); (it) != (a).end(); (it)++)
#define travel_vec(it, a)                                                      \
  for (vector<int>::iterator(it) = (a).begin(); (it) != (a).end(); (it)++)
#define p(x) cout << x << endl;
#define def cout << "find" << endl;
#define what_is(x) cout << #x << " is " << x << endl;
template <class T> T sqr(T x) { return x * x; }
template <class T> T alex(T x) { return (x > 0) ? x : -x; }
template <class T> void read(T &x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while ((ch < 48 || ch > 57) && ch != 45)
    ch = getchar();
  if (ch == 45)
    f = -1, ch = getchar();
  while (ch >= 48 && ch <= 57)
    x = x * 10 + ch - 48, ch = getchar();
  x *= f;
}

//-------------------------------------------------head-------------------------------------------------

#define maxn 200005
int n, a[maxn];
int _abs(int x) { return (x < 0) ? -x : x; }
int main() {
  read(n);
  rep(i, 1, n) read(a[i]), a[i] -= i;
  sort(a + 1, a + n + 1);
  ll res = 0;
  rep(i, 1, n) res += _abs(a[i] - (a[(n + 1) / 2]));
  printf("%lld\n", res);
  return 0;
}