Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, n) for (int i = a; i < n; i++)
#define mp(i, j) make_pair(i, j)
#define debug_v(v, N)                                                          \
  for (int i = 0; i < N; i++) {                                                \
    cout << v[i] << " ";                                                       \
  }                                                                            \
  cout << endl;
#define debug_vv(v, H, W)                                                      \
  for (int i = 0; i < H; i++) {                                                \
    for (int j = 0; j < W; j++) {                                              \
      cout << v[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }

typedef long long ll;
typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll a[100010];

void Main() {
  int N;
  cin >> N;
  rep(i, N) {
    ll _a;
    cin >> _a;
    _a -= (i + 1);
    a[i] = _a;
  }
  sort(a, a + N);
  ll res = 0;
  rep(i, N / 2) { res += a[N - 1 - i] - a[i]; }
  cout << res << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, n) for (int i = a; i < n; i++)
#define mp(i, j) make_pair(i, j)
#define debug_v(v, N)                                                          \
  for (int i = 0; i < N; i++) {                                                \
    cout << v[i] << " ";                                                       \
  }                                                                            \
  cout << endl;
#define debug_vv(v, H, W)                                                      \
  for (int i = 0; i < H; i++) {                                                \
    for (int j = 0; j < W; j++) {                                              \
      cout << v[i][j] << " ";                                                  \
    }                                                                          \
    cout << endl;                                                              \
  }

typedef long long ll;
typedef pair<int, int> P;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

ll a[200010];

void Main() {
  int N;
  cin >> N;
  rep(i, N) {
    ll _a;
    cin >> _a;
    _a -= (i + 1);
    a[i] = _a;
  }
  sort(a, a + N);
  ll res = 0;
  rep(i, N / 2) { res += a[N - 1 - i] - a[i]; }
  cout << res << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
}