Problem p03311 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define cin_desync()                                                           \
  do {                                                                         \
    ios_base::sync_with_stdio(false);                                          \
    cin.tie(0);                                                                \
  } while (0)

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

#else
#define trace(...)
#endif // ifndef ONLINE_JUDGE

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int max_N = 1e5 + 10;
int A[max_N];

ll sadness(ll b);

int N;
int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
  }

  ll low = -N, high = 1e9, best = 0;
  while (low <= high) {
    ll mid = (low + high) >> 1;
    ll curr = sadness(mid), mr = sadness(mid + 1), ml = sadness(mid - 1);

    if (mr < curr) {
      low = mid + 1;
    } else if (ml < curr) {
      high = mid - 1;
    } else {
      best = mid;
      break;
    }
  }

  cout << sadness(best) << endl;
}

ll sadness(ll b) {
  ll sad = 0;
  for (int i = 1; i <= N; ++i) {
    sad += abs(A[i] - (b + i));
  }

  return sad;
}


Fixed submission:
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

#define cin_desync()                                                           \
  do {                                                                         \
    ios_base::sync_with_stdio(false);                                          \
    cin.tie(0);                                                                \
  } while (0)

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}

#else
#define trace(...)
#endif // ifndef ONLINE_JUDGE

typedef long double ld;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int max_N = 2e5 + 10;
int A[max_N];

ll sadness(ll b);

int N;
int main() {
  cin >> N;
  for (int i = 1; i <= N; ++i) {
    cin >> A[i];
  }

  ll low = -N, high = 1e9, best = 0;
  while (low <= high) {
    ll mid = (low + high) >> 1;
    ll curr = sadness(mid), mr = sadness(mid + 1), ml = sadness(mid - 1);

    if (mr < curr) {
      low = mid + 1;
    } else if (ml < curr) {
      high = mid - 1;
    } else {
      best = mid;
      break;
    }
  }

  cout << sadness(best) << endl;
}

ll sadness(ll b) {
  ll sad = 0;
  for (int i = 1; i <= N; ++i) {
    sad += abs(A[i] - (b + i));
  }

  return sad;
}