Problem p00523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const ll INF = LLONG_MAX / 3;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n;
  cin >> n;
  vi a(n);
  REP(i, n) cin >> a[i];
  ll lb = 0, ub = INF;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    bool ok = false, f = true;
    int div0, div1, div2;
    div0 = 0;
    int p = 0;
    ll tmpa = 0;
    while (tmpa < mid) {
      tmpa += a[p];
      p++;
      if (p == n) {
        f = false;
        break;
      }
    }
    ll tmpb = 0;
    if (f) {
      div1 = p;
      while (tmpb < mid) {
        tmpb += a[p];
        p++;
        if (p == n) {
          f = false;
          break;
        }
      }
    }
    div2 = p;
    ll tmpc = 0;
    FOR(i, p, n) tmpc += a[i];
    while (f) {
      if (tmpa >= mid && tmpb >= mid && tmpc >= mid) {
        ok = true;
        break;
      }
      if (div2 == n)
        break;
      tmpa -= a[div0];
      tmpc += a[div0];
      div0++;
      while (tmpa < mid) {
        tmpa += a[div1];
        tmpb -= a[div1];
        div1++;
        if (div1 == div2) {
          f = false;
          break;
        }
      }
      while (tmpb < mid) {
        if (div2 == n)
          break;
        tmpb += a[div2];
        tmpc -= a[div2];
        div2++;
        if (div2 == div0) {
          f = false;
          break;
        }
      }
    }
    if (ok)
      lb = mid;
    else
      ub = mid;
  }
  cout << lb << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (int)(k); i < (int)(n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const int MOD = 1000000007;
const ll INF = LLONG_MAX / 3;
const ld EPS = 1e-12;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

int main() {
  cin.sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n;
  cin >> n;
  vi a(n);
  REP(i, n) cin >> a[i];
  ll lb = 0, ub = INF;
  while (ub - lb > 1) {
    ll mid = (ub + lb) / 2;
    bool ok = false;
    REP(i, n) {
      bool f = true;
      int p = i;
      ll tmp = 0;
      while (1) {
        if (tmp >= mid)
          break;
        if (p == n) {
          f = false;
          break;
        }
        tmp += a[p];
        p++;
      }
      if (f) {
        tmp = 0;
        while (1) {
          if (tmp >= mid)
            break;
          if (p == n) {
            f = false;
            break;
          }
          tmp += a[p];
          p++;
        }
      }
      if (f) {
        tmp = 0;
        while (1) {
          if (p == n)
            p = 0;
          if (tmp >= mid)
            break;
          if (p == i) {
            f = false;
            break;
          }
          tmp += a[p];
          p++;
        }
      }
      if (f) {
        ok = true;
        break;
      }
    }
    if (ok)
      lb = mid;
    else
      ub = mid;
  }
  cout << lb << endl;
  return 0;
}