Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(ll a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(ll a) {
  write(a);
  puts("");
}
inline void wri(ll a) {
  write(a);
  putchar(' ');
}
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 100005;
int n;
ll a[N], ans = 1e18;
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read() + a[i - 1];
  for (int i = 2; i + 2 <= n; i++) {
    int pos = lower_bound(&a[1], &a[i], a[i] / 2) - a;
    if (abs(a[pos] - (a[i] - a[pos])) > abs(a[pos - 1] - (a[i] - a[pos - 1])))
      pos--;
    int ppp = lower_bound(&a[i + 1], &a[n + 1], (a[n] - a[i]) / 2 + a[i]) - a;
    if (abs(a[ppp] - a[i] - (a[n] - a[ppp])) >
        abs(a[ppp - 1] - a[i] - (a[n] - a[ppp - 1])))
      ppp--;
    ans = min(
        ans,
        max(max(a[pos], a[i] - a[pos]), max(a[ppp] - a[i], a[n] - a[ppp])) -
            min(min(a[pos], a[i] - a[pos]), min(a[ppp] - a[i], a[n] - a[ppp])));
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define sqr(x) ((x) * (x))
#define mp make_pair
#define uint unsigned
#define PI pair<int, int>
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
#define gc getchar
inline ll read() {
  ll x = 0;
  char ch = gc();
  bool positive = 1;
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      positive = 0;
  for (; isdigit(ch); ch = gc())
    x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(ll a) {
  if (a < 0) {
    a = -a;
    putchar('-');
  }
  if (a >= 10)
    write(a / 10);
  putchar('0' + a % 10);
}
inline void writeln(ll a) {
  write(a);
  puts("");
}
inline void wri(ll a) {
  write(a);
  putchar(' ');
}
inline ull rnd() { return ((ull)rand() << 30 ^ rand()) << 4 | rand() % 4; }
const int N = 200005;
int n;
ll a[N], ans = 1e18;
int main() {
  n = read();
  for (int i = 1; i <= n; i++)
    a[i] = read() + a[i - 1];
  for (int i = 2; i + 2 <= n; i++) {
    int pos = lower_bound(&a[1], &a[i], a[i] / 2) - a;
    if (abs(a[pos] - (a[i] - a[pos])) > abs(a[pos - 1] - (a[i] - a[pos - 1])))
      pos--;
    int ppp = lower_bound(&a[i + 1], &a[n + 1], (a[n] - a[i]) / 2 + a[i]) - a;
    if (abs(a[ppp] - a[i] - (a[n] - a[ppp])) >
        abs(a[ppp - 1] - a[i] - (a[n] - a[ppp - 1])))
      ppp--;
    ans = min(
        ans,
        max(max(a[pos], a[i] - a[pos]), max(a[ppp] - a[i], a[n] - a[ppp])) -
            min(min(a[pos], a[i] - a[pos]), min(a[ppp] - a[i], a[n] - a[ppp])));
  }
  cout << ans << endl;
}