Problem p03312 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N 200000

int main() {
  long long a[N + 1];
  int n;
  int x, y, z;
  long long p, q, r, s;
  long long mi, ma, ans;
  scanf("%d", &n);
  a[0] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &p);
    a[i + 1] = a[i] + p;
  }

  x = 1;
  z = 3;
  ans = 10000000000000000;
  for (y = 2; y < n - 1; y++) {
    for (; x < y - 1; x++) {
      if (a[y] + a[0] < (2 * a[x]))
        break;
    }
    for (; z < n - 1; r++) {
      if (a[y] + a[n] < (2 * a[z]))
        break;
    }

    if (1 < x && (y + 1) < z) {
      p = a[x - 1] - a[0];
      q = a[y] - a[x - 1];
      r = a[z - 1] - a[y];
      s = a[n] - a[z - 1];
      mi = min(min(p, q), min(r, s));
      ma = max(max(p, q), max(r, s));
      ans = min(ans, ma - mi);
    }
    if ((y + 1) < z) {
      p = a[x] - a[0];
      q = a[y] - a[x];
      r = a[z - 1] - a[y];
      s = a[n] - a[z - 1];
      mi = min(min(p, q), min(r, s));
      ma = max(max(p, q), max(r, s));
      ans = min(ans, ma - mi);
    }
    if (1 < x) {
      p = a[x - 1] - a[0];
      q = a[y] - a[x - 1];
      r = a[z] - a[y];
      s = a[n] - a[z];
      mi = min(min(p, q), min(r, s));
      ma = max(max(p, q), max(r, s));
      ans = min(ans, ma - mi);
    }

    p = a[x] - a[0];
    q = a[y] - a[x];
    r = a[z] - a[y];
    s = a[n] - a[z];
    mi = min(min(p, q), min(r, s));
    ma = max(max(p, q), max(r, s));
    ans = min(ans, ma - mi);
  }

  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define MOD 1000000007
#define f(i, n) for (int i = 0; i < int(n); i++)
#define N 200000

int main() {
  long long a[N + 1];
  int n;
  int x, y, z;
  long long p, q, r, s;
  long long mi, ma, ans;
  scanf("%d", &n);
  a[0] = 0;
  for (int i = 0; i < n; i++) {
    scanf("%d", &p);
    a[i + 1] = a[i] + p;
  }

  x = 1;
  z = 3;
  ans = 10000000000000000;
  for (y = 2; y < n - 1; y++) {
    for (; x < y - 1; x++) {
      if (a[y] + a[0] < (2 * a[x]))
        break;
    }
    for (; z < n - 1; z++) {
      if (a[y] + a[n] < (2 * a[z]))
        break;
    }

    if (1 < x && (y + 1) < z) {
      p = a[x - 1] - a[0];
      q = a[y] - a[x - 1];
      r = a[z - 1] - a[y];
      s = a[n] - a[z - 1];
      mi = min(min(p, q), min(r, s));
      ma = max(max(p, q), max(r, s));
      ans = min(ans, ma - mi);
    }
    if ((y + 1) < z) {
      p = a[x] - a[0];
      q = a[y] - a[x];
      r = a[z - 1] - a[y];
      s = a[n] - a[z - 1];
      mi = min(min(p, q), min(r, s));
      ma = max(max(p, q), max(r, s));
      ans = min(ans, ma - mi);
    }
    if (1 < x) {
      p = a[x - 1] - a[0];
      q = a[y] - a[x - 1];
      r = a[z] - a[y];
      s = a[n] - a[z];
      mi = min(min(p, q), min(r, s));
      ma = max(max(p, q), max(r, s));
      ans = min(ans, ma - mi);
    }

    p = a[x] - a[0];
    q = a[y] - a[x];
    r = a[z] - a[y];
    s = a[n] - a[z];
    mi = min(min(p, q), min(r, s));
    ma = max(max(p, q), max(r, s));
    ans = min(ans, ma - mi);
  }

  printf("%lld\n", ans);

  return 0;
}