Problem p00523 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
typedef pair<LL, LL> P;
typedef pair<LL, P> PP;

LL a[2020200];
LL b[2020200];
LL n;

bool calc(LL m) {
  for (int i = 0; i < n; i++) {
    LL tmp = (i == 0 ? 0 : b[i - 1]);
    int pos1 = lower_bound(b + i, b + i + n, tmp + m) - b;
    int pos2 = lower_bound(b + i, b + i + n, b[pos1] + m) - b;
    if (b[n + i - 1] - b[pos2] >= m)
      return true;
  }
  return false;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = n; i < n * 2; i++) {
    a[i] = a[i - n];
  }
  b[0] = a[0];
  for (int i = 1; i < n * 2; i++) {
    b[i] = a[i] + b[i - 1];
  }

  LL left = 1, right = b[n - 1];
  while (right - left > 1) {
    LL mid = (right + left) / 2;
    if (calc(mid)) {
      left = mid;
    } else {
      right = mid;
    }
  }
  cout << left << endl;
  system("pause");
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr int INF = 2000000000;
constexpr int HINF = INF / 2;
constexpr double DINF = 100000000000000000.0;
constexpr long long LINF = 9223372036854775807;
constexpr long long HLINF = 4500000000000000000;
const double PI = acos(-1);
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
#define ALL(x) (x).begin(), (x).end()
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define mp make_pair
#define eb emplace_back
typedef pair<LL, LL> P;
typedef pair<LL, P> PP;

LL a[2020200];
LL b[2020200];
LL n;

bool calc(LL m) {
  for (int i = 0; i < n; i++) {
    LL tmp = (i == 0 ? 0 : b[i - 1]);
    int pos1 = lower_bound(b + i, b + i + n, tmp + m) - b;
    int pos2 = lower_bound(b + i, b + i + n, b[pos1] + m) - b;
    if (b[n + i - 1] - b[pos2] >= m)
      return true;
  }
  return false;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = n; i < n * 2; i++) {
    a[i] = a[i - n];
  }
  b[0] = a[0];
  for (int i = 1; i < n * 2; i++) {
    b[i] = a[i] + b[i - 1];
  }

  LL left = 1, right = b[n - 1];
  while (right - left > 1) {
    LL mid = (right + left) / 2;
    if (calc(mid)) {
      left = mid;
    } else {
      right = mid;
    }
  }
  cout << left << endl;
  // system("pause");
}