Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 100010;
const LL INF = 100000000000000LL;
const LL mod = 998244353;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define For(i, a, b) for (int i = a; i <= b; ++i)

int n;
LL a[maxn];
LL t1[maxn], t2[maxn];
LL pre1[maxn], pre2[maxn];
int cmp(LL a, LL b) { return a > b; }

void solve() {
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] -= i;
  }
  sort(a + 1, a + 1 + n);
  LL cnt1 = 0, cnt2 = 0;
  pre1[0] = 0, pre2[0] = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= 0) {
      t1[++cnt1] = a[i];
      // pre1[cnt1] = pre1[cnt1 - 1] + a[i];
    } else {
      a[i] *= -1;
      t2[++cnt2] = a[i];
      // pre2[cnt2] = pre2[cnt2 - 1] + a[i];
    }
  }
  sort(t1 + 1, t1 + 1 + cnt1, cmp);
  sort(t2 + 1, t2 + 1 + cnt2, cmp);
  for (int i = 1; i <= cnt1; i++)
    pre1[i] = pre1[i - 1] + t1[i];
  for (int i = 1; i <= cnt2; i++)
    pre2[i] = pre2[i - 1] + t2[i];
  LL ans = ((1ll) << 60);
  // cout << cnt1 << endl;
  for (LL i = 1; i <= cnt1; i++) {
    // cout << t1[i] << " ";
    ans = min(ans, pre1[i] - i * t1[i] + (cnt1 - i) * t1[i] -
                       (pre1[cnt1] - pre1[i]) + cnt2 * t1[i] + pre2[cnt2]);
  } // cout << endl;
  // cout << cnt2 << endl;
  for (LL i = 1; i <= cnt2; i++) {
    // cout << t2[i] << " ";
    ans = min(ans, pre2[i] - i * t2[i] + (cnt2 - i) * t2[i] -
                       (pre2[cnt2] - pre2[i]) + cnt1 * t2[i] + pre1[cnt1]);
  } // cout << endl;
  cout << ans << endl;
}

int main() {
  int t = 1;
  while (~scanf("%d", &n)) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 1000020;
const LL INF = 100000000000000LL;
const LL mod = 998244353;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define For(i, a, b) for (int i = a; i <= b; ++i)

int n;
LL a[maxn];
LL t1[maxn], t2[maxn];
LL pre1[maxn], pre2[maxn];
int cmp(LL a, LL b) { return a > b; }

void solve() {
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    a[i] -= i;
  }
  sort(a + 1, a + 1 + n);
  LL cnt1 = 0, cnt2 = 0;
  pre1[0] = 0, pre2[0] = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] >= 0) {
      t1[++cnt1] = a[i];
      // pre1[cnt1] = pre1[cnt1 - 1] + a[i];
    } else {
      a[i] *= -1;
      t2[++cnt2] = a[i];
      // pre2[cnt2] = pre2[cnt2 - 1] + a[i];
    }
  }
  sort(t1 + 1, t1 + 1 + cnt1, cmp);
  sort(t2 + 1, t2 + 1 + cnt2, cmp);
  for (int i = 1; i <= cnt1; i++)
    pre1[i] = pre1[i - 1] + t1[i];
  for (int i = 1; i <= cnt2; i++)
    pre2[i] = pre2[i - 1] + t2[i];
  LL ans = ((1ll) << 60);
  // cout << cnt1 << endl;
  for (LL i = 1; i <= cnt1; i++) {
    // cout << t1[i] << " ";
    ans = min(ans, pre1[i] - i * t1[i] + (cnt1 - i) * t1[i] -
                       (pre1[cnt1] - pre1[i]) + cnt2 * t1[i] + pre2[cnt2]);
  } // cout << endl;
  // cout << cnt2 << endl;
  for (LL i = 1; i <= cnt2; i++) {
    // cout << t2[i] << " ";
    ans = min(ans, pre2[i] - i * t2[i] + (cnt2 - i) * t2[i] -
                       (pre2[cnt2] - pre2[i]) + cnt1 * t2[i] + pre1[cnt1]);
  } // cout << endl;
  cout << ans << endl;
}

int main() {
  int t = 1;
  while (~scanf("%d", &n)) {
    solve();
  }
  return 0;
}