Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define rep(N) for (int i = 0; i < N; i++)
#define repj(N) for (int j = 0; j < N; j++)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define For(I, N) for (int I = 0; I < N; I++)
#define cinone(N)                                                              \
  int N;                                                                       \
  cin >> N;
#define scanfone(N)                                                            \
  int N;                                                                       \
  cin >> N;
#define cinng(N, M)                                                            \
  int N[M];                                                                    \
  for (int yiuytvnm = 0; yiuytvnm < M; yiuytvnm++)                             \
    cin >> N[yiuytvnm];
#define scanfng(N, M)                                                          \
  int N[M];                                                                    \
  for (int qrwuoiq = 0; qrwuoiq < M; qrwuoiq++)                                \
    scanf("%d", &N[qrwuoiq]);
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
typedef unsigned long long ull;
const int inf = 103974182;
using namespace std;
int n;
int a[100005];

int main() {
  //	freopen("input.txt","r",stdin);
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i] -= i + 1;
  }
  sort(a, a + n);
  int x = a[n / 2];
  ll res = 0;
  for (int i = 0; i < n; i++) {
    res += abs(x - a[i]);
  }
  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define Endl endl
#define mp make_pair
#define rep(N) for (int i = 0; i < N; i++)
#define repj(N) for (int j = 0; j < N; j++)
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define For(I, N) for (int I = 0; I < N; I++)
#define cinone(N)                                                              \
  int N;                                                                       \
  cin >> N;
#define scanfone(N)                                                            \
  int N;                                                                       \
  cin >> N;
#define cinng(N, M)                                                            \
  int N[M];                                                                    \
  for (int yiuytvnm = 0; yiuytvnm < M; yiuytvnm++)                             \
    cin >> N[yiuytvnm];
#define scanfng(N, M)                                                          \
  int N[M];                                                                    \
  for (int qrwuoiq = 0; qrwuoiq < M; qrwuoiq++)                                \
    scanf("%d", &N[qrwuoiq]);
#define over(A)                                                                \
  {                                                                            \
    cout << A << endl;                                                         \
    exit(0);                                                                   \
  }
typedef unsigned long long ull;
const int inf = 103974182;
using namespace std;
int n;
int a[200005];

int main() {
  //	freopen("input.txt","r",stdin);
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
    a[i] -= i + 1;
  }
  sort(a, a + n);
  int x = a[n / 2];
  ll res = 0;
  for (int i = 0; i < n; i++) {
    res += abs(x - a[i]);
  }
  cout << res << endl;
  return 0;
}