Problem p03312 (C++)

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

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> ii;
typedef vector<pair<ll, ll>> vii;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<pair<long long, long long>> vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<ll, pair<ll, ll>> iii;
#define PI 3.141592653589793
#define mod (ll)(1000 * 1000 * 1000 + 7)
// # define N 300005
template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
template <typename T> T pow(T a, T b, ll m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = ((ans % m) * (a % m)) % m;
    b /= 2;
    a = ((a % m) * (a % m)) % m;
  }
  return ans % m;
}
#define INF (ll)1e18
#define LG 20
typedef pair<ll, pair<ll, ll>> pii;
#define INF1 (ll)1e9 + 2
#define maxn 200005
#define offset 1000005

/*************************************************
struct Coupon {
    ll l;
    ll r;
    ll id;

    Coupon(ll l, ll r, ll id): l(l), r(r), id(id) {} // Initialization

    bool operator<(const Coupon& c) { // Sorting
        return l < c.l;
    }
};

**************************************************/
ll cf[maxn];
ll a[maxn];

int main() {
  IOS;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cf[i] = cf[i - 1] + a[i];
  }

  int x1 = 1;
  int x3 = 3;
  ll ans = INF;
  for (int i = 2; i < n; i++) {
    while (x1 < n - 3 && abs(cf[x1] - (cf[i] - cf[x1])) >
                             abs((cf[x1 + 1] - (cf[i] - cf[x1 + 1]))))
      x1++;
    while (x3 < n - 1 && abs((cf[x3] - cf[i]) - (cf[n] - cf[x3])) >
                             abs((cf[x3 + 1] - cf[i]) - (cf[n] - cf[x3 + 1])))
      x3++;
    ll d1 = cf[x1];
    ll d2 = cf[i] - cf[x1];
    ll d3 = cf[x3] - cf[i];
    ll d4 = cf[n] - cf[x3];
    trace4(d1, d2, d3, d4);
    trace4(x1, i, x3, n);
    ans = min(ans, max(d1, Max(d2, d3, d4)) - min(d1, Min(d2, d3, d4)));
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define endl "\n"
#define Max(x, y, z) max(x, max(y, z))
#define Min(x, y, z) min(x, min(y, z))
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<long long, long long> pll;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef pair<ll, ll> ii;
typedef vector<pair<ll, ll>> vii;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<pair<long long, long long>> vpll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef pair<ll, pair<ll, ll>> iii;
#define PI 3.141592653589793
#define mod (ll)(1000 * 1000 * 1000 + 7)
// # define N 300005
template <typename T> T gcd(T a, T b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
template <typename T> T pow(T a, T b, ll m) {
  T ans = 1;
  while (b > 0) {
    if (b % 2 == 1)
      ans = ((ans % m) * (a % m)) % m;
    b /= 2;
    a = ((a % m) * (a % m)) % m;
  }
  return ans % m;
}
#define INF (ll)1e18
#define LG 20
typedef pair<ll, pair<ll, ll>> pii;
#define INF1 (ll)1e9 + 2
#define maxn 200005
#define offset 1000005

/*************************************************
struct Coupon {
    ll l;
    ll r;
    ll id;

    Coupon(ll l, ll r, ll id): l(l), r(r), id(id) {} // Initialization

    bool operator<(const Coupon& c) { // Sorting
        return l < c.l;
    }
};

**************************************************/
ll cf[maxn];
ll a[maxn];

int main() {
  IOS;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    cf[i] = cf[i - 1] + a[i];
  }

  int x1 = 1;
  int x3 = 3;
  ll ans = INF;
  for (int i = 2; i < n; i++) {
    while (x1 < n - 3 && abs(cf[x1] - (cf[i] - cf[x1])) >
                             abs((cf[x1 + 1] - (cf[i] - cf[x1 + 1]))))
      x1++;
    while (x3 < n - 1 && abs((cf[x3] - cf[i]) - (cf[n] - cf[x3])) >
                             abs((cf[x3 + 1] - cf[i]) - (cf[n] - cf[x3 + 1])))
      x3++;
    ll d1 = cf[x1];
    ll d2 = cf[i] - cf[x1];
    ll d3 = cf[x3] - cf[i];
    ll d4 = cf[n] - cf[x3];
    // trace4(d1,d2,d3,d4);
    // trace4(x1,i,x3,n);
    ans = min(ans, max(d1, Max(d2, d3, d4)) - min(d1, Min(d2, d3, d4)));
  }
  cout << ans;
}