Problem p03312 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.h"
#endif

using namespace std;

#define mp make_pair
#define mt make_tuple
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define sz(a) ((int)(a).size())
#define make_unique(a) (a).resize(unique(all((x))) - (x).begin())

typedef long long ll;

template <typename T> inline bool mmin(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}
template <typename T> inline bool mmax(T &a, const T &b) {
  return a > b ? (a = b, true) : false;
}

const int shift = 10;
const int maxn = 201000;

int a[maxn];
ll s[maxn];

ll get_sum(int l, int r) {
  ll res = s[r];
  if (l) {
    res -= s[l - 1];
  }
  return res;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s[i] = a[i];
    if (i) {
      s[i] += s[i - 1];
    }
  }

  ll ans = 1e16;
  for (int cut2 = 1; cut2 <= n - 3; ++cut2) {
    int p1 = -1, p3 = -1;
    for (int i = 0; i < n; i++) {
      if (get_sum(0, i) >= get_sum(0, cut2) / 2) {
        p1 = i;
        break;
      }
    }
    for (int i = cut2 + 1; i < n; i++) {
      if (get_sum(cut2 + 1, i) >= get_sum(cut2 + 1, n - 1) / 2) {
        p3 = i;
        break;
      }
    }
    for (int cut1 = p1 - shift; cut1 <= p1 + shift; ++cut1) {
      for (int cut3 = p3 - shift; cut3 <= p3 + shift; ++cut3) {
        if (cut1 < 0 || cut3 >= n - 1 || cut2 <= cut1 || cut3 <= cut2) {
          continue;
        }
        ll s1 = get_sum(0, cut1);
        ll s2 = get_sum(cut1 + 1, cut2);
        ll s3 = get_sum(cut2 + 1, cut3);
        ll s4 = get_sum(cut3 + 1, n - 1);
        ll m1 = min(min(s1, s2), min(s3, s4));
        ll m2 = max(max(s1, s2), max(s3, s4));
        ans = min(ans, m2 - m1);
      }
    }
  }
  cout << ans << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

#ifdef DEBUG
#include "debug.h"
#endif

using namespace std;

#define mp make_pair
#define mt make_tuple
#define all(a) (a).begin(), (a).end()
#define rep(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define sz(a) ((int)(a).size())
#define make_unique(a) (a).resize(unique(all((x))) - (x).begin())

typedef long long ll;

template <typename T> inline bool mmin(T &a, const T &b) {
  return a < b ? (a = b, true) : false;
}
template <typename T> inline bool mmax(T &a, const T &b) {
  return a > b ? (a = b, true) : false;
}

const int shift = 10;
const int maxn = 201000;

int a[maxn];
ll s[maxn];

ll get_sum(int l, int r) {
  ll res = s[r];
  if (l) {
    res -= s[l - 1];
  }
  return res;
}

int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    s[i] = a[i];
    if (i) {
      s[i] += s[i - 1];
    }
  }

  ll ans = 1e16;
  for (int cut2 = 1; cut2 <= n - 3; ++cut2) {
    ll need1 = get_sum(0, cut2) / 2;
    ll need2 = get_sum(0, cut2) + get_sum(cut2 + 1, n - 1) / 2;
    int p1 = lower_bound(s, s + n, need1) - s;
    int p3 = lower_bound(s, s + n, need2) - s;
    for (int cut1 = p1 - shift; cut1 <= p1 + shift; ++cut1) {
      for (int cut3 = p3 - shift; cut3 <= p3 + shift; ++cut3) {
        if (cut1 < 0 || cut3 >= n - 1 || cut2 <= cut1 || cut3 <= cut2) {
          continue;
        }
        ll s1 = get_sum(0, cut1);
        ll s2 = get_sum(cut1 + 1, cut2);
        ll s3 = get_sum(cut2 + 1, cut3);
        ll s4 = get_sum(cut3 + 1, n - 1);
        ll m1 = min(min(s1, s2), min(s3, s4));
        ll m2 = max(max(s1, s2), max(s3, s4));
        ans = min(ans, m2 - m1);
      }
    }
  }
  cout << ans << '\n';

  return 0;
}