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

#include <algorithm>
#include <iostream>
using namespace std;
long long n, a[200000], s[200000], minx, miny, A, B, C, B2, C2, sum, d, e, f[3],
    g;
int main() {
  long long Left, Right, Mid;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + a[i];
  }
  for (int i = 0; i <= n; i++) {
    A = s[i];
    B = (s[i] + sum / 3) % sum;
    C = (s[i] + sum * 2 / 3) % sum;

    Left = i;
    Right = n + 1;

    for (int j = 0; j < 100; j++) {
      Mid = (Left + Right) / 2;
      if (s[(Mid - 2) % n + 1] >= B) {
        Right = Mid;
      }
      if (s[(Mid - 1) % n + 1] < B) {
        Left = Mid + 1;
      }
    }

    B2 = s[(Mid - 1) % n + 1];
    d = (Mid - 1) % n + 1;

    Left = i;
    Right = n + 1;

    for (int j = 0; j < 100; j++) {
      Mid = (Left + Right) / 2;
      if (s[(Mid - 2) % n + 1] >= C) {
        Right = Mid;
      }
      if (s[(Mid - 1) % n + 1] < C) {
        Left = Mid + 1;
      }
    }

    C2 = s[(Mid - 1) % n + 1];
    e = (Mid - 1) % n + 1;

    f[0] = i;
    f[1] = d;
    f[2] = e;
    sort(f, f + 3);
    g = f[0];
    d = f[1];
    e = f[2];
    miny = min(s[d] - s[g], s[e] - s[d]);
    miny = min(miny, sum - (s[e] - s[g]));
    minx = max(minx, miny);

    if (d - 1 >= 0) {
      miny = min(s[d - 1] - s[g], s[e] - s[d - 1]);
      miny = min(miny, sum - (s[e] - s[g]));
      minx = max(minx, miny);
    }
    if (e - 1 >= 0) {
      miny = min(s[d] - s[g], s[e - 1] - s[d]);
      miny = min(miny, sum - (s[e - 1] - s[g]));
      minx = max(minx, miny);
    }
    if (d - 1 >= 0 && e - 1 >= 0) {
      miny = min(s[d - 1] - s[g], s[e - 1] - s[d - 1]);
      miny = min(miny, sum - (s[e - 1] - s[g]));
      minx = max(minx, miny);
    }
  }
  cout << minx << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
long long n, a[200000], s[200000], minx, miny, A, B, C, B2, C2, sum, d, e, f[3],
    g;
int main() {
  int Left, Right, Mid;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  for (int i = 1; i <= n; i++) {
    s[i] = s[i - 1] + a[i];
  }
  for (int i = 0; i <= n; i++) {
    A = s[i];
    B = (s[i] + sum / 3) % sum;
    C = (s[i] + sum * 2 / 3) % sum;

    for (int j = i; j <= n + 1; j++) {
      if (s[(j - 1) % n + 1] >= B) {
        B2 = s[(j - 1) % n + 1];
        d = (j - 1) % n + 1;
        break;
      }
    }
    for (int j = i; j <= n + 1; j++) {
      if (s[(j - 1) % n + 1] >= C) {
        C2 = s[(j - 1) % n + 1];
        e = (j - 1) % n + 1;
        break;
      }
    }

    f[0] = i;
    f[1] = d;
    f[2] = e;
    sort(f, f + 3);
    g = f[0];
    d = f[1];
    e = f[2];
    miny = min(s[d] - s[g], s[e] - s[d]);
    miny = min(miny, sum - (s[e] - s[g]));
    minx = max(minx, miny);

    if (d - 1 >= 0) {
      miny = min(s[d - 1] - s[g], s[e] - s[d - 1]);
      miny = min(miny, sum - (s[e] - s[g]));
      minx = max(minx, miny);
    }
    if (e - 1 >= 0) {
      miny = min(s[d] - s[g], s[e - 1] - s[d]);
      miny = min(miny, sum - (s[e - 1] - s[g]));
      minx = max(minx, miny);
    }
    if (d - 1 >= 0 && e - 1 >= 0) {
      miny = min(s[d - 1] - s[g], s[e - 1] - s[d - 1]);
      miny = min(miny, sum - (s[e - 1] - s[g]));
      minx = max(minx, miny);
    }
  }
  cout << minx << endl;
  return 0;
}