Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define X first
#define Y second
#define MP make_pair
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pdd pt;
typedef vector<int> vi;

const int N = 1e5 + 500;
const int M = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int LOG = 18;
const int OFF = (1 << LOG);

ll a[N], m1[N][2], m2[N][2], m1s[N][2], m2s[N][2], bst = 0;
int n;

int main() {
  scanf("%d", &n);
  ll sum = 0, st = 0, cur = 0;
  ll sol = (ll)INF * INF;
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
    sum += a[i];
    while (cur <= sum / 2 && st < i) {
      cur += a[st];
      st++;
    }
    m1[i][0] = cur;
    m2[i][0] = sum - cur;
    m1[i][1] = cur - a[st - 1];
    m2[i][1] = sum - cur + a[st - 1];
  }
  st = n - 1;
  sum = 0;
  cur = 0;
  for (int i = n - 1; i >= 0; i--) {
    sum += a[i];
    while (cur <= sum / 2 && st > i) {
      cur += a[st];
      st--;
    }
    m1s[i][0] = cur;
    m2s[i][0] = sum - cur;
    m1s[i][1] = cur - a[st + 1];
    m2s[i][1] = sum - cur + a[st + 1];
  }
  for (int i = 1; i < n - 2; i++) {
    for (int k = 0; k < 2; k++) {
      for (int l = 0; l < 2; l++) {
        ll mx = max(max(m1[i][k], m2[i][k]), max(m1s[i + 1][l], m2s[i + 1][l]));
        ll mi = min(min(m1[i][k], m2[i][k]), min(m1s[i + 1][l], m2s[i + 1][l]));
        sol = min(sol, mx - mi);
      }
    }
  }
  printf("%lld\n", sol);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#define X first
#define Y second
#define MP make_pair
#define PB push_back

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef pdd pt;
typedef vector<int> vi;

const int N = 3e5 + 500;
const int M = 1e6 + 500;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int LOG = 18;
const int OFF = (1 << LOG);

ll a[N], m1[N][2], m2[N][2], m1s[N][2], m2s[N][2], bst = 0;
int n;

int main() {
  scanf("%d", &n);
  ll sum = 0, st = 0, cur = 0;
  ll sol = (ll)INF * INF;
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
    sum += a[i];
    while (cur <= sum / 2 && st < i) {
      cur += a[st];
      st++;
    }
    m1[i][0] = cur;
    m2[i][0] = sum - cur;
    m1[i][1] = cur - a[st - 1];
    m2[i][1] = sum - cur + a[st - 1];
  }
  st = n - 1;
  sum = 0;
  cur = 0;
  for (int i = n - 1; i >= 0; i--) {
    sum += a[i];
    while (cur <= sum / 2 && st > i) {
      cur += a[st];
      st--;
    }
    m1s[i][0] = cur;
    m2s[i][0] = sum - cur;
    m1s[i][1] = cur - a[st + 1];
    m2s[i][1] = sum - cur + a[st + 1];
  }
  for (int i = 1; i < n - 2; i++) {
    for (int k = 0; k < 2; k++) {
      for (int l = 0; l < 2; l++) {
        ll mx = max(max(m1[i][k], m2[i][k]), max(m1s[i + 1][l], m2s[i + 1][l]));
        ll mi = min(min(m1[i][k], m2[i][k]), min(m1s[i + 1][l], m2s[i + 1][l]));
        sol = min(sol, mx - mi);
      }
    }
  }
  printf("%lld\n", sol);
  return 0;
}