Problem p03312 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#define maxr 100005
#define maxn 300005
#define ll long long int
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define per(i, r, l) for (register int i = r; i >= l; i--)
#define gc()                                                                   \
  ((p1 == p2 &&                                                                \
    (p2 = (p1 = buffer) + fread(buffer, 1, maxr, stdin), p1 == p2))            \
       ? EOF                                                                   \
       : *p1++)
using namespace std;

char buffer[maxr], *p1, *p2;
template <class T> void read(T &x) {
  char ch = gc();
  x = 0;
  bool f = 1;
  while (!('0' <= ch && ch <= '9') && ch != '-')
    ch = gc();
  if (ch == '-')
    f = 0, ch = gc();
  while ('0' <= ch && ch <= '9')
    x = (x << 1) + (x << 3) + ch - '0', ch = gc();
  if (!f)
    x = -x;
}

int n;
int a[maxn];
ll pre[maxn];
int main() {
#ifndef ONLINE_JUDGE
  freopen("ec.in", "r", stdin);
#endif
  read(n);
  rep(i, 1, n) read(a[i]), pre[i] = a[i] + pre[i - 1];
  int l = 1, r = 3;
  ll res = 0x3fffffff;
  rep(mid, 2, n - 2) {
    while (abs(pre[l + 1] - (pre[mid] - pre[l + 1])) <
           abs(pre[l] - (pre[mid] - pre[l])))
      l++;
    while (abs(pre[r + 1] - pre[mid] - (pre[n] - pre[r + 1])) <
           abs(pre[r] - pre[mid] - (pre[n] - pre[r])))
      r++;
    ll mx = max(max(pre[l], pre[mid] - pre[l]),
                max(pre[r] - pre[mid], pre[n] - pre[r]));
    ll mi = min(min(pre[l], pre[mid] - pre[l]),
                min(pre[r] - pre[mid], pre[n] - pre[r]));
    res = min(res, mx - mi);
  }
  printf("%lld", res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#define maxr 100005
#define maxn 300005
#define ll long long int
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define per(i, r, l) for (register int i = r; i >= l; i--)
#define gc()                                                                   \
  ((p1 == p2 &&                                                                \
    (p2 = (p1 = buffer) + fread(buffer, 1, maxr, stdin), p1 == p2))            \
       ? EOF                                                                   \
       : *p1++)
using namespace std;

char buffer[maxr], *p1, *p2;
template <class T> void read(T &x) {
  char ch = gc();
  x = 0;
  bool f = 1;
  while (!('0' <= ch && ch <= '9') && ch != '-')
    ch = gc();
  if (ch == '-')
    f = 0, ch = gc();
  while ('0' <= ch && ch <= '9')
    x = (x << 1) + (x << 3) + ch - '0', ch = gc();
  if (!f)
    x = -x;
}

int n;
int a[maxn];
ll pre[maxn];
int main() {
  read(n);
  rep(i, 1, n) read(a[i]), pre[i] = a[i] + pre[i - 1];
  int l = 1, r = 3;
  ll res = 0x3fffffff;
  rep(mid, 2, n - 2) {
    while (abs(pre[l + 1] - (pre[mid] - pre[l + 1])) <
           abs(pre[l] - (pre[mid] - pre[l])))
      l++;
    while (abs(pre[r + 1] - pre[mid] - (pre[n] - pre[r + 1])) <
           abs(pre[r] - pre[mid] - (pre[n] - pre[r])))
      r++;
    ll mx = max(max(pre[l], pre[mid] - pre[l]),
                max(pre[r] - pre[mid], pre[n] - pre[r]));
    ll mi = min(min(pre[l], pre[mid] - pre[l]),
                min(pre[r] - pre[mid], pre[n] - pre[r]));
    res = min(res, mx - mi);
  }
  printf("%lld", res);
  return 0;
}