Problem p03309 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define For(i, a, b) for (int i = int(a); i < int(b); i++)
#define Dor(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define Foz(i, a) For(i, 0, a)
#define Doz(i, a) Dor(i, 0, a)
#define Trav(e, v) for (auto &e : v)
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<ii>;
template <typename T> bool ckmin(T &a, T b) { return (b < a) ? a = b, 1 : 0; }
template <typename T> bool ckmax(T &a, T b) { return (b > a) ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#ifdef XVENOM
#define errp(...) fprintf(stderr, __VA_ARGS__)
#define dbg(...) errp("[%s]: ", #__VA_ARGS__), dbg_str(__VA_ARGS__)
#else
#define errp(...)
#define dbg(...)
#endif
template <typename U, typename V> string to_string(pair<U, V>);
string to_string(string &e) { return "\"" + e + "\""; }
string to_string(char e) { return "\'" + string(1, e) + "\'"; }
string to_string(bool e) { return e ? "true" : "false"; }
template <typename T> string to_string(T e) {
  string s = "[ ";
  for (auto &x : e)
    s += to_string(x) + " ";
  return s + "]";
}
template <typename U, typename V> string to_string(pair<U, V> e) {
  return "(" + to_string(e.first) + ", " + to_string(e.second) + ")";
}
void dbg_str() { errp(" \n"); }
template <typename U, typename... V> void dbg_str(U u, V... v) {
  errp(" %s", to_string(u).c_str());
  dbg_str(v...);
}
/* --- */
const int N = 100010;
ll a[N], n, pref[N];

ll sum(int i, int j) { return pref[j] - pref[i - 1]; }

ll calc(int k) { return sum(1, k) - k * a[k] + (n - k) * a[k] - sum(k + 1, n); }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  /* --- */
  cin >> n;
  For(i, 1, n + 1) cin >> a[i];
  For(i, 1, n + 1) a[i] -= i;
  sort(a + 1, a + n + 1);
  reverse(a + 1, a + n + 1);
  pref[0] = 0;
  For(i, 1, n + 1) pref[i] = a[i] + pref[i - 1];
  ll ans = calc(1);
  For(i, 1, n + 1) ckmin(ans, calc(i));
  cout << ans << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define For(i, a, b) for (int i = int(a); i < int(b); i++)
#define Dor(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define Foz(i, a) For(i, 0, a)
#define Doz(i, a) Dor(i, 0, a)
#define Trav(e, v) for (auto &e : v)
#define pb push_back
#define mp make_pair
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<ii>;
template <typename T> bool ckmin(T &a, T b) { return (b < a) ? a = b, 1 : 0; }
template <typename T> bool ckmax(T &a, T b) { return (b > a) ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#ifdef XVENOM
#define errp(...) fprintf(stderr, __VA_ARGS__)
#define dbg(...) errp("[%s]: ", #__VA_ARGS__), dbg_str(__VA_ARGS__)
#else
#define errp(...)
#define dbg(...)
#endif
template <typename U, typename V> string to_string(pair<U, V>);
string to_string(string &e) { return "\"" + e + "\""; }
string to_string(char e) { return "\'" + string(1, e) + "\'"; }
string to_string(bool e) { return e ? "true" : "false"; }
template <typename T> string to_string(T e) {
  string s = "[ ";
  for (auto &x : e)
    s += to_string(x) + " ";
  return s + "]";
}
template <typename U, typename V> string to_string(pair<U, V> e) {
  return "(" + to_string(e.first) + ", " + to_string(e.second) + ")";
}
void dbg_str() { errp(" \n"); }
template <typename U, typename... V> void dbg_str(U u, V... v) {
  errp(" %s", to_string(u).c_str());
  dbg_str(v...);
}
/* --- */
const int N = 200010;
ll a[N], n, pref[N];

ll sum(int i, int j) { return pref[j] - pref[i - 1]; }

ll calc(int k) { return sum(1, k) - k * a[k] + (n - k) * a[k] - sum(k + 1, n); }

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  /* --- */
  cin >> n;
  For(i, 1, n + 1) cin >> a[i];
  For(i, 1, n + 1) a[i] -= i;
  sort(a + 1, a + n + 1);
  reverse(a + 1, a + n + 1);
  pref[0] = 0;
  For(i, 1, n + 1) pref[i] = a[i] + pref[i - 1];
  ll ans = calc(1);
  For(i, 1, n + 1) ckmin(ans, calc(i));
  cout << ans << '\n';
}