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

#include <iostream>
#include <vector>

#define MIN(a, b) ((a) < (b) ? (a) : (b))

using namespace std;

int main() {
  int n;
  vector<unsigned long long> a(200001);
  unsigned long long baum, b[4][3], minb, ans = 0;
  cin >> n;
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    a[i] += a[i - 1];
    a[i + n] = a[i];
  }
  baum = a[n];
  for (int i = 1; i <= n; i++) {
    a[i + n] += baum;
  }
  for (int cut1 = 0; cut1 < n; cut1++) {
    int cut2, cut3_1, cut3_2;
    for (cut2 = cut1 + 1; cut2 < cut1 + n; cut2++) {
      if (a[cut2] - a[cut1] > baum / 3) {
        break;
      }
    }
    for (cut3_1 = cut2; cut3_1 < cut1 - 1 + n; cut3_1++) {
      if (a[cut3_1] - a[cut2 - 1] > (baum - (a[cut2 - 1] - a[cut1])) / 2) {
        break;
      }
    }
    for (cut3_2 = cut2 + 1; cut3_2 < cut1 + n; cut3_2++) {
      if (a[cut3_2] - a[cut2] > (baum - a[cut2] - a[cut1]) / 2) {
        break;
      }
    }
    b[0][0] = a[cut2 - 1] - a[cut1];
    b[0][1] = a[cut3_1 - 1] - a[cut2 - 1];
    b[0][2] = baum - b[0][0] - b[0][1];
    b[1][0] = a[cut2 - 1] - a[cut1];
    b[1][1] = a[cut3_1] - a[cut2 - 1];
    b[1][2] = baum - b[1][0] - b[1][1];
    b[2][0] = a[cut2] - a[cut1];
    b[2][1] = a[cut3_2 - 1] - a[cut2];
    b[2][2] = baum - b[2][0] - b[2][1];
    b[3][0] = a[cut2] - a[cut1];
    b[3][1] = a[cut3_2] - a[cut2];
    b[3][2] = baum - b[3][0] - b[3][1];
    for (int i = 0; i < 4; i++) {
      minb = MIN(MIN(b[i][0], b[i][1]), b[i][2]);
      if (ans < minb) {
        ans = minb;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

#define MIN(a, b) ((a) < (b) ? (a) : (b))

using namespace std;

int main() {
  int n;
  long a[100000][2] = {0}, baum, b[8][3] = {0}, minb, ans = 0;
  cin >> n;
  cin >> a[0][0];
  a[0][1] = 0;
  for (int i = 1; i < n; i++) {
    cin >> a[i][0];
    a[i][1] = a[i - 1][1] + a[i - 1][0];
  }
  baum = a[n - 1][1] + a[n - 1][0];
  for (int cut1 = 0; cut1 < n; cut1++) {
    int cut2, cut3_1, cut3_2, b1, b2_1, b2_2;
    for (cut2 = cut1 + 1; cut2 != n; ++cut2 %= n) {
      if (cut2 > cut1) {
        if ((b1 = a[cut2][1] - a[cut1][1]) > baum / 3) {
          break;
        }
      } else {
        if ((b1 = a[cut2][1] + baum - a[cut1][1]) > baum / 3) {
          break;
        }
      }
    }
    for (cut3_1 = cut2; cut3_1 != n; ++cut3_1 %= n) {
      if (cut3_1 > cut2) {
        if ((b2_1 = a[cut3_1][1] - a[cut2 - 1][1]) >
            (baum - (b1 - a[cut2 - 1][0])) / 2) {
          break;
        }
      } else {
        if ((b2_1 = a[cut3_1][1] + baum - a[cut2 - 1][1]) >
            (baum - (b1 - a[cut2 - 1][0])) / 2) {
          break;
        }
      }
    }
    for (cut3_2 = cut2 + 1; cut3_2 != n; ++cut3_2 %= n) {
      if (cut3_2 > cut2) {
        if ((b2_2 = a[cut3_2][1] - a[cut2][1]) > (baum - b1) / 2) {
          break;
        }
      } else {
        if ((b2_2 = a[cut3_2][1] + baum - a[cut2][1]) > (baum - b1) / 2) {
          break;
        }
      }
    }
    b[0][0] = b1 - a[cut2 - 1][0];
    b[0][1] = b2_1 - a[cut3_1 - 1][0];
    b[0][2] = baum - b1 - b2_1 + a[cut2 - 1][0] + a[cut3_1 - 1][0];
    b[1][0] = b1 - a[cut2 - 1][0];
    b[1][1] = b2_1;
    b[1][2] = baum - b1 - b2_1 + a[cut2 - 1][0];
    b[2][0] = b1;
    b[2][1] = b2_2 - a[cut3_2 - 1][0];
    b[2][2] = baum - b1 - b2_2 + a[cut3_2 - 1][0];
    b[3][0] = b1;
    b[3][1] = b2_2;
    b[3][2] = baum - b1 - b2_2;
    for (int i = 0; i < 4; i++) {
      minb = MIN(MIN(b[i][0], b[i][1]), b[i][2]);
      if (ans < minb) {
        ans = minb;
      }
    }
  }
  cout << ans << endl;
  return 0;
}