prithivMLmods/Coder-Stat huggingface dataset:
Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define maxn 100020
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define inf 1e18
typedef long long ll;
typedef pair<int, int> pr;

int a[maxn], n;
ll sum[maxn], ans;

inline ll cal(ll a, ll b, ll c, ll d) {
  return max(max(a, b), max(c, d)) - min(min(a, b), min(c, d));
}
int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &a[i]), sum[i] = sum[i - 1] + a[i];
  int l = 1, r = 2;
  ans = inf;
  rep(i, 2, n - 1) {
    while (l < i && sum[l] < sum[i] - sum[l])
      l++;
    while (r < n && sum[r] - sum[i] < sum[n] - sum[r])
      r++;
    ans = min(ans,
              cal(sum[l], sum[i] - sum[l], sum[r] - sum[i], sum[n] - sum[r]));
    if (l > 1)
      ans = min(ans, cal(sum[l - 1], sum[i] - sum[l - 1], sum[r] - sum[i],
                         sum[n] - sum[r]));
    if (r > i + 1)
      ans = min(ans, cal(sum[l], sum[i] - sum[l], sum[r - 1] - sum[i],
                         sum[n] - sum[r - 1]));
    if (l > 1 && r > i + 1)
      ans = min(ans, cal(sum[l - 1], sum[i] - sum[l - 1], sum[r - 1] - sum[i],
                         sum[n] - sum[r - 1]));
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define maxn 200020
#define rep(i, l, r) for (register int i = l; i <= r; i++)
#define repd(i, r, l) for (register int i = r; i >= l; i--)
#define rvc(i, S) for (register int i = 0; i < (int)S.size(); i++)
#define rvcd(i, S) for (register int i = ((int)S.size()) - 1; i >= 0; i--)
#define fore(i, x) for (register int i = head[x]; i; i = e[i].next)
#define pb push_back
#define prev prev_
#define stack stack_
#define mp make_pair
#define fi first
#define se second
#define inf 1e18
typedef long long ll;
typedef pair<int, int> pr;

int a[maxn], n;
ll sum[maxn], ans;

inline ll cal(ll a, ll b, ll c, ll d) {
  return max(max(a, b), max(c, d)) - min(min(a, b), min(c, d));
}
int main() {
  scanf("%d", &n);
  rep(i, 1, n) scanf("%d", &a[i]), sum[i] = sum[i - 1] + a[i];
  int l = 1, r = 2;
  ans = inf;
  rep(i, 2, n - 1) {
    while (l < i && sum[l] < sum[i] - sum[l])
      l++;
    while (r < n && sum[r] - sum[i] < sum[n] - sum[r])
      r++;
    ans = min(ans,
              cal(sum[l], sum[i] - sum[l], sum[r] - sum[i], sum[n] - sum[r]));
    if (l > 1)
      ans = min(ans, cal(sum[l - 1], sum[i] - sum[l - 1], sum[r] - sum[i],
                         sum[n] - sum[r]));
    if (r > i + 1)
      ans = min(ans, cal(sum[l], sum[i] - sum[l], sum[r - 1] - sum[i],
                         sum[n] - sum[r - 1]));
    if (l > 1 && r > i + 1)
      ans = min(ans, cal(sum[l - 1], sum[i] - sum[l - 1], sum[r - 1] - sum[i],
                         sum[n] - sum[r - 1]));
  }
  cout << ans << endl;
}
Problem p03312 (C++)

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

const int MAXN = 200005;

int a[MAXN], n;
long long sum[MAXN];

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  long long ans = 1e18;
  auto check = [](int i, int j, int k) {
    return abs((sum[j] - sum[i - 1]) - (sum[k] - sum[j]));
  };
  auto calc = [](int i, int j, int k) {
    long long mx = max(max(max(sum[i], sum[j] - sum[i]), sum[k] - sum[j]),
                       sum[n] - sum[k]);
    long long mn = min(min(min(sum[i], sum[j] - sum[i]), sum[k] - sum[j]),
                       sum[n] - sum[k]);
    return mx - mn;
  };
  for (int i = 2; i < n - 1; i++) {
    int a = 1, d = i - 1, b, c;
    while (d - a >= 3) {
      b = a + (d - a) / 3, c = a + (d - a) / 3 * 2;
      if (check(1, b, i) < check(1, c, i))
        d = c;
      else
        a = b;
    }
    int x = i + 1, w = n - 1, y, z;
    while (w - x >= 3) {
      y = x + (w - x) / 3, z = x + (w - x) / 3 * 2;
      if (check(i + 1, y, n) < check(i + 1, z, n))
        w = z;
      else
        x = y;
    }
    for (int j = a; j <= b; j++)
      for (int k = x; k <= w; k++)
        ans = min(ans, calc(j, i, k));
  }
  cout << ans << endl;
  return 0;
}


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

const int MAXN = 200005;

int a[MAXN], n;
long long sum[MAXN];

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    sum[i] = sum[i - 1] + a[i];
  }
  long long ans = 1e18;
  auto check = [](int i, int j, int k) {
    return abs((sum[j] - sum[i - 1]) - (sum[k] - sum[j]));
  };
  auto calc = [](int i, int j, int k) {
    long long mx = max(max(max(sum[i], sum[j] - sum[i]), sum[k] - sum[j]),
                       sum[n] - sum[k]);
    long long mn = min(min(min(sum[i], sum[j] - sum[i]), sum[k] - sum[j]),
                       sum[n] - sum[k]);
    return mx - mn;
  };
  for (int i = 2; i < n - 1; i++) {
    int a = 1, d = i - 1, b, c;
    while (d - a >= 3) {
      b = a + (d - a) / 3, c = a + (d - a) / 3 * 2;
      if (check(1, b, i) < check(1, c, i))
        d = c;
      else
        a = b;
    }
    int x = i + 1, w = n - 1, y, z;
    while (w - x >= 3) {
      y = x + (w - x) / 3, z = x + (w - x) / 3 * 2;
      if (check(i + 1, y, n) < check(i + 1, z, n))
        w = z;
      else
        x = y;
    }
    for (int j = a; j <= d; j++)
      for (int k = x; k <= w; k++)
        ans = min(ans, calc(j, i, k));
  }
  cout << ans << endl;
  return 0;
}
Problem p03312 (C++)

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

int a[100010];
long long sum[100010];

long long get_s(int l, int r) { return sum[r] - sum[l - 1]; }

bool check(int a, int b, int c) {
  return abs(get_s(a, b) - get_s(b + 1, c)) >
         abs(get_s(a, b + 1) - get_s(b + 2, c));
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  int l = 1, r = 3;
  long long ans = 0x3f3f3f3f3f3f3f3f;
  for (int i = 2; i < n; i++) {
    while (l < i - 1 && check(1, l, i))
      l++;
    while (r < n - 1 && check(i + 1, r, n))
      r++;
    long long minj = min(min(get_s(1, l), get_s(l + 1, i)),
                         min(get_s(i + 1, r), get_s(r + 1, n)));
    long long maxj = max(max(get_s(1, l), get_s(l + 1, i)),
                         max(get_s(i + 1, r), get_s(r + 1, n)));
    ans = min(ans, maxj - minj);
  }
  printf("%lld\n", ans);
  return 0;
}


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

int a[200010];
long long sum[200010];

long long get_s(int l, int r) { return sum[r] - sum[l - 1]; }

bool check(int a, int b, int c) {
  return abs(get_s(a, b) - get_s(b + 1, c)) >
         abs(get_s(a, b + 1) - get_s(b + 2, c));
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    sum[i] = sum[i - 1] + a[i];
  }
  int l = 1, r = 3;
  long long ans = 0x3f3f3f3f3f3f3f3f;
  for (int i = 2; i < n; i++) {
    while (l < i - 1 && check(1, l, i))
      l++;
    while (r < n - 1 && check(i + 1, r, n))
      r++;
    long long minj = min(min(get_s(1, l), get_s(l + 1, i)),
                         min(get_s(i + 1, r), get_s(r + 1, n)));
    long long maxj = max(max(get_s(1, l), get_s(l + 1, i)),
                         max(get_s(i + 1, r), get_s(r + 1, n)));
    ans = min(ans, maxj - minj);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03312 (C++)

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

using namespace std;
#define ll long long

ll sumA[100000000];

ll cut(ll *A, ll i, ll j, ll precut) {
  ll sum = 0, medium = 0;
  ll cutpoint;
  ll min;
  ll cur;
  ll temp;
  if (i == 0)
    cur = 0;
  else
    cur = sumA[i - 1];
  for (int k = precut - 1; k < j - 1; k++) {
    temp = llabs((sumA[k] - cur) - (sumA[j - 1] - sumA[k]));
    if (k == precut - 1) {
      min = temp;
      cutpoint = k;
    } else if (min > temp) {
      min = temp;
      cutpoint = k;
    }
  }
  return cutpoint + 1;
}

ll F(ll N, ll i, ll j, ll k) {
  ll sum[4];
  for (int m = 0; m < 4; m++)
    sum[m] = 0;
  sum[0] = sumA[i - 1];
  sum[1] = sumA[j - 1] - sumA[i - 1];
  sum[2] = sumA[k - 1] - sumA[j - 1];
  sum[3] = sumA[N - 1] - sumA[k - 1];
  ll min = sum[0], max = sum[0];
  for (int m = 1; m < 4; m++) {
    if (min > sum[m])
      min = sum[m];
    if (max < sum[m])
      max = sum[m];
  }
  return max - min;
}

int main() {
  ll N;
  ll *A;
  ll sum = 0;
  cin >> N;
  A = (ll *)malloc(N * sizeof(*A));
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    if (i == 0)
      sumA[i] += A[i];
    else
      sumA[i] += A[i] + sumA[i - 1];
  }
  ll medium = sumA[N - 1] / 4;
  ll s = 0;
  ll P[2] = {1, 3};
  ll min = 0, temp;
  for (int i = 2; i <= N - 2; i++) {
    P[0] = cut(A, 0, i, P[0]);
    P[1] = cut(A, i, N, P[1]);
    ll temp = F(N, P[0], i, P[1]);
    if (i == 2)
      min = temp;
    else if (min > temp)
      min = temp;
  }
  cout << min << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
#define ll long long

ll sumA[100000000];

ll cut(ll *A, ll i, ll j, ll precut) {
  ll sum = 0, medium = 0;
  ll cutpoint;
  ll min;
  ll cur;
  ll temp;
  if (i == 0)
    cur = 0;
  else
    cur = sumA[i - 1];
  for (int k = precut - 1; k < j - 1; k++) {
    if (k != precut - 1 &&
        temp < llabs((sumA[k] - cur) - (sumA[j - 1] - sumA[k])))
      break;
    temp = llabs((sumA[k] - cur) - (sumA[j - 1] - sumA[k]));
    if (k == precut - 1) {
      min = temp;
      cutpoint = k;
    } else if (min > temp) {
      min = temp;
      cutpoint = k;
    }
  }
  return cutpoint + 1;
}

ll F(ll N, ll i, ll j, ll k) {
  ll sum[4];
  for (int m = 0; m < 4; m++)
    sum[m] = 0;
  sum[0] = sumA[i - 1];
  sum[1] = sumA[j - 1] - sumA[i - 1];
  sum[2] = sumA[k - 1] - sumA[j - 1];
  sum[3] = sumA[N - 1] - sumA[k - 1];
  ll min = sum[0], max = sum[0];
  for (int m = 1; m < 4; m++) {
    if (min > sum[m])
      min = sum[m];
    if (max < sum[m])
      max = sum[m];
  }
  return max - min;
}

int main() {
  ll N;
  ll *A;
  ll sum = 0;
  cin >> N;
  A = (ll *)malloc(N * sizeof(*A));
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    if (i == 0)
      sumA[i] += A[i];
    else
      sumA[i] += A[i] + sumA[i - 1];
  }
  ll medium = sumA[N - 1] / 4;
  ll s = 0;
  ll P[2] = {1, 3};
  ll min = 0, temp;
  for (int i = 2; i <= N - 2; i++) {
    P[0] = cut(A, 0, i, P[0]);
    P[1] = cut(A, i, N, P[1]);
    ll temp = F(N, P[0], i, P[1]);
    if (i == 2)
      min = temp;
    else if (min > temp)
      min = temp;
  }
  cout << min << "\n";
  return 0;
}
Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define F(i, n) for (int i = 1; i <= (int)n; i++)
#define FF(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define pi pair<int, int>
#define re(x) (int)x.size()
#define y1 u228
using namespace std;
typedef long long ll;
const int N = 1e5 + 228;
const int big = 2e9;
const int md = 1e9 + 7;
int n, i, k;
ll a[N], pr[N], ans;
vector<ll> v;
ll go(int i, int j, int k) { return abs(pr[k] - pr[j] - (pr[j] - pr[i - 1])); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
  cin >> n;
  ans = 9e18;
  F(i, n) cin >> a[i];
  F(i, n) pr[i] = pr[i - 1] + a[i];
  i = k = 1;
  F(j, n) {
    while (i < j && go(1, i, j) > go(1, i + 1, j))
      i++;
    while (k < n && go(j + 1, k, n) > go(j + 1, k + 1, n))
      k++;
    v.resize(0);
    v.pb(pr[i]);
    v.pb(pr[j] - pr[i]);
    v.pb(pr[k] - pr[j]);
    v.pb(pr[n] - pr[k]);
    sort(all(v));
    ans = min(ans, v[re(v) - 1] - v[0]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define F(i, n) for (int i = 1; i <= (int)n; i++)
#define FF(i, n) for (int i = 0; i < (int)n; i++)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define pi pair<int, int>
#define re(x) (int)x.size()
#define y1 u228
using namespace std;
typedef long long ll;
const int N = 2e5 + 228;
const int big = 2e9;
const int md = 1e9 + 7;
int n, i, k;
ll a[N], pr[N], ans;
vector<ll> v;
ll go(int i, int j, int k) { return abs(pr[k] - pr[j] - (pr[j] - pr[i - 1])); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.txt","r",stdin);
  cin >> n;
  ans = 9e18;
  F(i, n) cin >> a[i];
  F(i, n) pr[i] = pr[i - 1] + a[i];
  i = k = 1;
  F(j, n) {
    while (i < j && go(1, i, j) > go(1, i + 1, j))
      i++;
    while (k < n && go(j + 1, k, n) > go(j + 1, k + 1, n))
      k++;
    v.resize(0);
    v.pb(pr[i]);
    v.pb(pr[j] - pr[i]);
    v.pb(pr[k] - pr[j]);
    v.pb(pr[n] - pr[k]);
    sort(all(v));
    ans = min(ans, v[re(v) - 1] - v[0]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

#define int long long
const int N = 1e5 + 5, INF = 1e18;
int n, a[N], pref[N], ans = INF;

int sum(int l, int r) { return pref[r] - pref[l - 1]; }
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pref[i] = pref[i - 1] + a[i];
  }
  for (int i = 3; i <= n - 1; i++) {
    // if (i != 5) continue;
    int pos1, pos2, ans1 = INF, ans2 = INF;

    int l = 1, r = i - 1;
    while (l < r) {
      int mid = (l + r) / 2;
      int tmp = 2 * sum(1, mid) - sum(1, i - 1);
      if (abs(tmp) < ans1) {
        ans1 = abs(tmp);
        pos1 = mid;
      }
      if (tmp < 0)
        l = mid + 1;
      else
        r = mid;
    }
    l = i, r = n;
    while (l < r) {
      int mid = (l + r) / 2;
      int tmp = 2 * sum(i, mid) - sum(i, n);
      // cout << mid << ' ' << tmp << endl;
      if (abs(tmp) < ans2) {
        ans2 = abs(tmp);
        pos2 = mid;
      }
      if (tmp < 0)
        l = mid + 1;
      else
        r = mid;
    }
    int sum1 = sum(1, pos1), sum2 = sum(pos1 + 1, i - 1), sum3 = sum(i, pos2),
        sum4 = sum(pos2 + 1, n);
    // cout << pos1 << ' ' << i << ' ' << pos2 << endl;
    // cout << sum1 << ' ' << sum2 << ' ' << sum3 << ' ' << sum4 << endl;
    ans = min(ans, max(sum1, max(sum2, max(sum3, sum4))) -
                       min(sum1, min(sum2, min(sum3, sum4))));
  }
  cout << ans;
}


Fixed submission:
#include <iostream>
using namespace std;

#define int long long
const int N = 2e5 + 5, INF = 1e18;
int n, a[N], pref[N], ans = INF;

int sum(int l, int r) { return pref[r] - pref[l - 1]; }
signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    pref[i] = pref[i - 1] + a[i];
  }
  for (int i = 3; i <= n - 1; i++) {
    // if (i != 5) continue;
    int pos1, pos2, ans1 = INF, ans2 = INF;

    int l = 1, r = i - 1;
    while (l < r) {
      int mid = (l + r) / 2;
      int tmp = 2 * sum(1, mid) - sum(1, i - 1);
      if (abs(tmp) < ans1) {
        ans1 = abs(tmp);
        pos1 = mid;
      }
      if (tmp < 0)
        l = mid + 1;
      else
        r = mid;
    }
    l = i, r = n;
    while (l < r) {
      int mid = (l + r) / 2;
      int tmp = 2 * sum(i, mid) - sum(i, n);
      // cout << mid << ' ' << tmp << endl;
      if (abs(tmp) < ans2) {
        ans2 = abs(tmp);
        pos2 = mid;
      }
      if (tmp < 0)
        l = mid + 1;
      else
        r = mid;
    }
    int sum1 = sum(1, pos1), sum2 = sum(pos1 + 1, i - 1), sum3 = sum(i, pos2),
        sum4 = sum(pos2 + 1, n);
    // cout << pos1 << ' ' << i << ' ' << pos2 << endl;
    // cout << sum1 << ' ' << sum2 << ' ' << sum3 << ' ' << sum4 << endl;
    ans = min(ans, max(sum1, max(sum2, max(sum3, sum4))) -
                       min(sum1, min(sum2, min(sum3, sum4))));
  }
  cout << ans;
}
Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
typedef long long ll;
int n;
int a[maxn], prep[maxn], sufp[maxn];
ll ans = 1e18;
ll pre[maxn], suf[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);

  int cur = 1;
  pre[1] = a[1];
  for (int i = 2; i <= n; ++i) {
    pre[i] = pre[i - 1] + a[i];
    while (pre[cur] * 2 <= pre[i])
      ++cur;
    prep[i] = cur - 1;
  }

  cur = n;
  suf[n] = a[n];
  for (int i = n - 1; i; --i) {
    suf[i] = suf[i + 1] + a[i];
    while (suf[cur] * 2 <= suf[i])
      --cur;
    sufp[i] = cur - 1;
  }

  for (int i = 3; i < n; ++i) {
    int l2 = i - 1;
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k < 2; ++k) {
        int l1 = prep[i - 1] + j;
        int l3 = sufp[i] + k;
        if (l1 < l2 && l2 < l3 && l3 < n) {
          ll x1 = pre[l1], x2 = pre[l2] - pre[l1], x3 = pre[l3] - pre[l2],
             x4 = pre[n] - pre[l3];
          // cout << i << ' ' << x1 << ' ' << x2 << ' ' << x3 << ' ' << x4 <<
          // endl;
          ans = min(ans, max(max(x1, x2), max(x3, x4)) -
                             min(min(x1, x2), min(x3, x4)));
        }
      }
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
typedef long long ll;
int n;
int a[maxn], prep[maxn], sufp[maxn];
ll ans = 1e18;
ll pre[maxn], suf[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]);

  int cur = 1;
  pre[1] = a[1];
  for (int i = 2; i <= n; ++i) {
    pre[i] = pre[i - 1] + a[i];
    while (pre[cur] * 2 <= pre[i])
      ++cur;
    prep[i] = cur - 1;
  }

  cur = n;
  suf[n] = a[n];
  for (int i = n - 1; i; --i) {
    suf[i] = suf[i + 1] + a[i];
    while (suf[cur] * 2 <= suf[i])
      --cur;
    sufp[i] = cur - 1;
  }

  for (int i = 3; i < n; ++i) {
    int l2 = i - 1;
    for (int j = 0; j < 2; ++j)
      for (int k = 0; k < 2; ++k) {
        int l1 = prep[i - 1] + j;
        int l3 = sufp[i] + k;
        if (l1 < l2 && l2 < l3 && l3 < n) {
          ll x1 = pre[l1], x2 = pre[l2] - pre[l1], x3 = pre[l3] - pre[l2],
             x4 = pre[n] - pre[l3];
          // cout << i << ' ' << x1 << ' ' << x2 << ' ' << x3 << ' ' << x4 <<
          // endl;
          ans = min(ans, max(max(x1, x2), max(x3, x4)) -
                             min(min(x1, x2), min(x3, x4)));
        }
      }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03312 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

#define INF 1000000000000000000LL
using ll = long long int;
int n;
std::vector<ll> a;
std::vector<ll> b;

ll sum(int from, int until) { return b[until] - b[from]; }

int search(int from, int until) {
  int l = from; // dummy
  ll tmp = INF;
  for (int i = from + 1; i <= until - 1; i++) {
    ll diff = abs(sum(from, i) - sum(i, until));
    if (diff < tmp) {
      l = i;
      tmp = diff;
    }
  }

  return l;
}

int bs(int from, int until) {
  int index = from; // dummy
  ll mini = INF;

  int l = from + 1;
  int r = until;

  while (l < r) {
    int mid = l + (r - l) / 2;
    ll det = sum(from, mid) - sum(mid, until);

    if (abs(det) < mini) {
      index = mid;
      mini = abs(det);
    }

    if (det > 0) { // 前の方が大きい
      r = mid;
    } else if (det < 0) {
      l = mid + 1;
    } else {
      break;
    }
  }

  return index;
}

int main() {
  using namespace std;

  cin >> n;
  a.reserve(n);
  b.reserve(n + 1);

  for (int i = 0; i < n; i++) {
    ll tmp;
    cin >> a[i];
  }

  b.push_back(0);
  ll accum = 0;
  for (int i = 0; i < n; i++) {
    accum += a[i];
    b[i + 1] = accum;
  }

  ll minimum = INF;
  for (int c = 2; c <= n - 2; c++) {
    int l = search(0, c);
    int r = search(c, n);

    ll mn = min(sum(0, l), min(sum(l, c), min(sum(c, r), sum(r, n))));
    ll mx = max(sum(0, l), max(sum(l, c), max(sum(c, r), sum(r, n))));

    minimum = min(minimum, mx - mn);
  }

  cout << minimum << endl;

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>

#define INF 1000000000000000000LL
using ll = long long int;
int n;
std::vector<ll> a;
std::vector<ll> b;

ll sum(int from, int until) { return b[until] - b[from]; }

int search(int from, int until) {
  int l = from; // dummy
  ll tmp = INF;
  for (int i = from + 1; i <= until - 1; i++) {
    ll diff = abs(sum(from, i) - sum(i, until));
    if (diff < tmp) {
      l = i;
      tmp = diff;
    }
  }

  return l;
}

int bs(int from, int until) {
  int index = from; // dummy
  ll mini = INF;

  int l = from + 1;
  int r = until;

  while (l < r) {
    int mid = l + (r - l) / 2;
    ll det = sum(from, mid) - sum(mid, until);

    if (abs(det) < mini) {
      index = mid;
      mini = abs(det);
    }

    if (det > 0) { // 前の方が大きい
      r = mid;
    } else if (det < 0) {
      l = mid + 1;
    } else {
      break;
    }
  }

  return index;
}

int main() {
  using namespace std;

  cin >> n;
  a.reserve(n);
  b.reserve(n + 1);

  for (int i = 0; i < n; i++) {
    ll tmp;
    cin >> a[i];
  }

  b.push_back(0);
  ll accum = 0;
  for (int i = 0; i < n; i++) {
    accum += a[i];
    b[i + 1] = accum;
  }

  ll minimum = INF;
  for (int c = 2; c <= n - 2; c++) {
    int l = bs(0, c);
    int r = bs(c, n);

    ll mn = min(sum(0, l), min(sum(l, c), min(sum(c, r), sum(r, n))));
    ll mx = max(sum(0, l), max(sum(l, c), max(sum(c, r), sum(r, n))));

    minimum = min(minimum, mx - mn);
  }

  cout << minimum << endl;

  return 0;
}
Problem p03312 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;

#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define FOR(i, s, e) for (int i = int(s); i < int(e); i++)
#define REP(i, x) FOR(i, 0, x)
#define CLR(a) memset((a), 0, sizeof(a))

const ll MOD = 1e+9 + 7;
const double PI = acos(-1.0);

int a[100001];
ll s[100002];

int get_mid_point(int l, int r, int c) {
  ll diff_min = s[r] - s[l];
  FOR(i, c, r + 1) {
    ll sl = s[i] - s[l], sr = s[r] - s[i];
    ll diff = abs(sl - sr);
    if (diff > diff_min) {
      return i - 1;
    } else {
      diff_min = diff;
    }
  }
  return r;
}

int main() {
  int n;
  cin >> n;
  s[0] = 0;
  REP(i, n) {
    cin >> a[i];
    s[i + 1] = a[i];
    s[i + 1] += s[i];
  }
  int pq = 0, rs = 3;
  ll ret = s[n];
  FOR(i, 2, n) {
    pq = get_mid_point(0, i, pq);
    rs = get_mid_point(i, n, rs);
    ll P = s[pq] - s[0], Q = s[i] - s[pq], R = s[rs] - s[i], S = s[n] - s[rs];
    ll diff = max(max(P, Q), max(R, S)) - min(min(P, Q), min(R, S));
    ret = min(ret, diff);
  }
  cout << ret << "\n";
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;

#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define FOR(i, s, e) for (int i = int(s); i < int(e); i++)
#define REP(i, x) FOR(i, 0, x)
#define CLR(a) memset((a), 0, sizeof(a))

const ll MOD = 1e+9 + 7;
const double PI = acos(-1.0);

int a[200010];
ll s[200010];

int get_mid_point(int l, int r, int c) {
  ll diff_min = s[r] - s[l];
  FOR(i, c, r + 1) {
    ll sl = s[i] - s[l], sr = s[r] - s[i];
    ll diff = abs(sl - sr);
    if (diff > diff_min) {
      return i - 1;
    } else {
      diff_min = diff;
    }
  }
  return r;
}

int main() {
  int n;
  cin >> n;
  s[0] = 0;
  REP(i, n) {
    cin >> a[i];
    s[i + 1] = a[i];
    s[i + 1] += s[i];
  }
  int pq = 0, rs = 3;
  ll ret = s[n];
  FOR(i, 2, n) {
    pq = get_mid_point(0, i, pq);
    rs = get_mid_point(i, n, rs);
    ll P = s[pq] - s[0], Q = s[i] - s[pq], R = s[rs] - s[i], S = s[n] - s[rs];
    ll diff = max(max(P, Q), max(R, S)) - min(min(P, Q), min(R, S));
    ret = min(ret, diff);
  }
  cout << ret << "\n";
}
Problem p03312 (C++)

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

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repi(i, a, b) for (int i = (a); i < int(b); i++)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ull << (b))

using namespace std;
using i32 = int;
using i64 = long long;
using f64 = double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
using vf64 = vector<f64>;
using vstr = vector<string>;

template <typename T, typename S> void amax(T &x, S y) {
  if (x < y)
    x = y;
}
template <typename T, typename S> void amin(T &x, S y) {
  if (y < x)
    x = y;
}

i32 n;
vi64 a, S;

i32 idx(i32 a, i32 b) {
  i32 l = a, r = b;
  while (true) {
    i32 m = (l + r) / 2;
    i64 x = abs((S[b] - S[m - 1]) - (S[m - 1] - S[a]));
    i64 y = abs((S[b] - S[m]) - (S[m] - S[a]));
    i64 z = abs((S[b] - S[m + 1]) - (S[m + 1] - S[a]));
    if (x >= y && y <= z)
      return m;
    if (x <= y)
      r = m;
    else
      l = m;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(16);
  cin >> n;
  a = vi64(n);
  rep(i, n) cin >> a[i];
  S = vi64(n + 1);
  partial_sum(all(a), S.begin() + 1);
  i64 ans = 1e18;
  repi(k, 1, n) {
    i32 a = idx(0, k);
    i32 b = idx(k, n);
    vi64 v = {S[a], S[k] - S[a], S[b] - S[k], S[n] - S[b]};
    sort(all(v));
    amin(ans, v[3] - v[0]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repi(i, a, b) for (int i = (a); i < int(b); i++)
#define all(a) (a).begin(), (a).end()
#define bit(b) (1ull << (b))

using namespace std;
using i32 = int;
using i64 = long long;
using f64 = double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;
using vf64 = vector<f64>;
using vstr = vector<string>;

template <typename T, typename S> void amax(T &x, S y) {
  if (x < y)
    x = y;
}
template <typename T, typename S> void amin(T &x, S y) {
  if (y < x)
    x = y;
}

i32 n;
vi64 a, S;

i32 idx(i32 a, i32 b) {
  i32 l = a, r = b;
  while (true) {
    i32 m = (l + r) / 2;
    i64 x = abs((S[b] - S[m - 1]) - (S[m - 1] - S[a]));
    i64 y = abs((S[b] - S[m]) - (S[m] - S[a]));
    i64 z = abs((S[b] - S[m + 1]) - (S[m + 1] - S[a]));
    if (x >= y && y <= z)
      return m;
    if (x <= y)
      r = m;
    else
      l = m;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(16);
  cin >> n;
  a = vi64(n);
  rep(i, n) cin >> a[i];
  S = vi64(n + 1);
  partial_sum(all(a), S.begin() + 1);
  i64 ans = 1e18;
  repi(k, 2, n - 1) {
    i32 a = idx(0, k);
    i32 b = idx(k, n);
    vi64 v = {S[a], S[k] - S[a], S[b] - S[k], S[n] - S[b]};
    sort(all(v));
    amin(ans, v[3] - v[0]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03313 (C++)

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

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 18;
int dp[N][1 << N][2];

void add(int a[2], int b[2]) {
  for (int i = 0; i < 2; i++) {
    if (b[i] > a[0]) {
      a[1] = a[0];
      a[0] = b[i];
    } else if (b[i] > a[1]) {
      a[1] = b[i];
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  memset(dp, -1, sizeof(dp));

  for (int i = 0; i < (1 << n); i++) {
    cin >> dp[0][i][0];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      dp[i + 1][j][0] = dp[i][j][0];
      dp[i + 1][j][1] = dp[i][j][1];
      if ((j >> i) & 1)
        add(dp[i + 1][j], dp[i][j ^ (1 << i)]);
    }
  }

  int mx = 0;
  for (int i = 1; i < (1 << n); i++) {
    mx = max(mx, dp[n][i][0] + dp[n][i][1]);
    cout << mx << "\n";
  }

  return 0;
}

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

#define ll long long
#define all(aaa) aaa.begin(), aaa.end()

const int N = 18;
int dp[N + 1][1 << N][2];

void add(int a[2], int b[2]) {
  for (int i = 0; i < 2; i++) {
    if (b[i] > a[0]) {
      a[1] = a[0];
      a[0] = b[i];
    } else if (b[i] > a[1]) {
      a[1] = b[i];
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int n;
  cin >> n;

  memset(dp, -1, sizeof(dp));

  for (int i = 0; i < (1 << n); i++) {
    cin >> dp[0][i][0];
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      dp[i + 1][j][0] = dp[i][j][0];
      dp[i + 1][j][1] = dp[i][j][1];
      if ((j >> i) & 1)
        add(dp[i + 1][j], dp[i][j ^ (1 << i)]);
    }
  }

  int mx = 0;
  for (int i = 1; i < (1 << n); i++) {
    mx = max(mx, dp[n][i][0] + dp[n][i][1]);
    cout << mx << "\n";
  }

  return 0;
}
Problem p03313 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \
  for (ll i = (m); i <= n; i++) {                                              \
    cout << (x[i]) << " ";                                                     \
  }                                                                            \
  cout << endl;

ll n, m, a[(1LL << 20)], dp[(1LL << 20)][2], ans[(1LL << 20)];

void setv(ll x, ll at) {
  if (a[at] > a[dp[x][0]]) {
    dp[x][1] = dp[x][0];
    dp[x][0] = at;
  } else {
    if (at != dp[x][0] && a[at] > a[dp[x][1]]) {
      dp[x][1] = at;
    }
  }
}

void setp(ll x, ll u) {
  setv(x, dp[u][0]);
  setv(x, dp[u][1]);
}

ll aa(ll x) { return a[dp[x][0]] + a[dp[x][1]]; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  m = (1LL << n);
  zep(i, 0, m) { cin >> a[i]; }
  a[m] = -INF;
  zep(i, 0, m) {
    dp[i][0] = i;
    dp[i][1] = (1LL << m);
  }
  ans[0] = -INF;

  zep(i, 0, m) {
    zep(j, 0, 20) {
      if (i & (1LL << j)) {
        setp(i, (i & ~(1LL << j)));
      }
    }
  }

  zep(i, 1, m) {
    ans[i] = max(ans[i - 1], aa(i));
    print(ans[i])
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i <= (n); i++)
#define zep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x, m, n)                                                        \
  for (ll i = (m); i <= n; i++) {                                              \
    cout << (x[i]) << " ";                                                     \
  }                                                                            \
  cout << endl;

ll n, m, a[(1LL << 20)], dp[(1LL << 20)][2], ans[(1LL << 20)];

void setv(ll x, ll at) {
  if (a[at] > a[dp[x][0]]) {
    dp[x][1] = dp[x][0];
    dp[x][0] = at;
  } else {
    if (at != dp[x][0] && a[at] > a[dp[x][1]]) {
      dp[x][1] = at;
    }
  }
}

void setp(ll x, ll u) {
  setv(x, dp[u][0]);
  setv(x, dp[u][1]);
}

ll aa(ll x) { return a[dp[x][0]] + a[dp[x][1]]; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  m = (1LL << n);
  zep(i, 0, m) { cin >> a[i]; }
  a[m] = -INF;
  zep(i, 0, m) {
    dp[i][0] = i;
    dp[i][1] = m;
  }
  ans[0] = -INF;

  zep(i, 0, m) {
    zep(j, 0, 20) {
      if (i & (1LL << j)) {
        setp(i, (i & ~(1LL << j)));
      }
    }
  }

  zep(i, 1, m) {
    ans[i] = max(ans[i - 1], aa(i));
    print(ans[i])
  }
  return 0;
}
Problem p03313 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;
using namespace std;
const int N = 18;
int n, xval[N][2];

namespace utils {
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
template <class T> inline void apn(T &x, const T y) { x = x < y ? x : y; }
template <class T> inline void apx(T &x, const T y) { x = x > y ? x : y; }
inline int nxi() {
  int x = 0;
  char c;
  while (((c = getchar()) > '9' || c < '0') && c != '-')
    ;
  const bool f = (c == '-') && (c = getchar());
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return f ? -x : x;
}
} // namespace utils
using namespace utils;

void apl(int x[2], int v) {
  if (x[0] < v)
    swap(x[0], v);
  if (x[1] < v)
    swap(x[1], v);
}

int main() {
  n = nxi();
  for (int i = 0; i < 1 << n; ++i) {
    xval[i][0] = nxi();
  }
  for (int i = 1; i < 1 << n; i <<= 1) {
    for (int j = 0; j < 1 << n; j += i << 1) {
      for (int k = 0; k < i; ++k) {
        apl(xval[j + k + i], xval[j + k][0]);
        apl(xval[j + k + i], xval[j + k][1]);
      }
    }
  }
  int res = 0;
  for (int i = 1; i < 1 << n; ++i) {
    apx(res, xval[i][0] + xval[i][1]);
    printf("%d\n", res);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
typedef long long lint;
using namespace std;
const int N = 18;
int n, xval[1 << N][2];

namespace utils {
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
template <class T> inline void apn(T &x, const T y) { x = x < y ? x : y; }
template <class T> inline void apx(T &x, const T y) { x = x > y ? x : y; }
inline int nxi() {
  int x = 0;
  char c;
  while (((c = getchar()) > '9' || c < '0') && c != '-')
    ;
  const bool f = (c == '-') && (c = getchar());
  while (x = x * 10 - 48 + c, (c = getchar()) >= '0' && c <= '9')
    ;
  return f ? -x : x;
}
} // namespace utils
using namespace utils;

void apl(int x[2], int v) {
  if (x[0] < v)
    swap(x[0], v);
  if (x[1] < v)
    swap(x[1], v);
}

int main() {
  n = nxi();
  for (int i = 0; i < 1 << n; ++i) {
    xval[i][0] = nxi();
  }
  for (int i = 1; i < 1 << n; i <<= 1) {
    for (int j = 0; j < 1 << n; j += i << 1) {
      for (int k = 0; k < i; ++k) {
        apl(xval[j + k + i], xval[j + k][0]);
        apl(xval[j + k + i], xval[j + k][1]);
      }
    }
  }
  int res = 0;
  for (int i = 1; i < 1 << n; ++i) {
    apx(res, xval[i][0] + xval[i][1]);
    printf("%d\n", res);
  }
  return 0;
}
Problem p03313 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <complex>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B, C, L, R;
string S, T;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
const long double EPS = 1e-9;
typedef pair<ll, ll> P;

typedef vector<P> vp;
typedef vector<vp> matP;

int main() {
  cin >> N;
  ll num = (1LL << N);
  vec a(num);
  rep(i, num) cin >> a[i];
  a.push_back(-1); // a[num]で最小値に飛ぶ
  mat dp(num, vec(2, num));
  rep(i, num) {
    if (i == 0) {
      dp[0][0] = 0;
    } else {
      dp[i][0] = i;
      ll &n1 = dp[i][0], &n2 = dp[i][1];
      for (int j = ((i - 1) & i);; j = ((j - 1) & i)) {
        rep(k, 2) {
          ll tp = dp[j][k];
          if ((tp == n1) || (tp == n2))
            continue;
          if (a[n1] <= a[tp]) {
            n2 = n1;
            n1 = tp;
          } else if (a[n2] < a[tp]) {
            n2 = tp;
          }
        }
        if (j == 0)
          break;
      }
    }
  }
  ll ans = -1;
  reps(i, 1, num) {
    ans = max(ans, a[dp[i][0]] + a[dp[i][1]]);
    cout << ans << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <complex>
#include <iomanip>
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, K, Q, A, B, C, L, R;
string S, T;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
const long double EPS = 1e-9;
typedef pair<ll, ll> P;

typedef vector<P> vp;
typedef vector<vp> matP;

int main() {
  cin >> N;
  ll num = (1LL << N);
  vec a(num);
  rep(i, num) cin >> a[i];
  a.push_back(-1); // a[num]で最小値に飛ぶ
  mat dp(num, vec(2, num));
  rep(i, num) {
    if (i == 0) {
      dp[0][0] = 0;
    } else {
      dp[i][0] = i;
      ll &n1 = dp[i][0], &n2 = dp[i][1];
      // for(int j = ((i - 1) & i); ; j = ((j - 1) & i)){
      rep(bit, 18) {
        if (((i >> bit) & 1) == 0)
          continue;
        int j = i - (1LL << bit);
        rep(k, 2) {
          ll tp = dp[j][k];
          if ((tp == n1) || (tp == n2))
            continue;
          if (a[n1] <= a[tp]) {
            n2 = n1;
            n1 = tp;
          } else if (a[n2] < a[tp]) {
            n2 = tp;
          }
        }
        if (j == 0)
          break;
      }
    }
  }
  ll ans = -1;
  reps(i, 1, num) {
    ans = max(ans, a[dp[i][0]] + a[dp[i][1]]);
    cout << ans << endl;
  }
}
Problem p03313 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[1 << n];
  for (int i = 0; i < 1 << n; i++)
    cin >> a[i];
  int b[1 << n], c[1 << n];
  b[0] = 0;
  for (int s = 1; s < 1 << n; s++) {
    int p = s, q = -1;
    for (int i = 0; i < n; i++) {
      if (s & (1 << i)) {
        int t = s ^ (1 << i);
        if (c[t] >= 0 && a[p] < a[c[t]]) {
          p = b[t];
          q = c[t];
        } else if (a[p] < a[b[t]]) {
          q = p;
          p = b[t];
          assert(p != q);
        } else if (q == -1 || a[q] < a[b[t]] && p != b[t])
          q = b[t];
        else if (q == -1 || a[q] < a[c[t]] && p != c[t])
          q = c[t];
      }
    }
    b[s] = p;
    c[s] = q;
  }
  int acc = -1;
  for (int s = 1; s < 1 << n; s++) {
    assert(b[s] != c[s]);
    acc = max(acc, a[b[s]] + a[c[s]]);
    cout << acc << endl;
  }
}


Fixed submission:
#include <cassert>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[1 << n];
  for (int i = 0; i < 1 << n; i++)
    cin >> a[i];
  int b[1 << n], c[1 << n] = {-1};
  b[0] = 0;
  for (int s = 1; s < 1 << n; s++) {
    int p = s, q = -1;
    for (int i = 0; i < n; i++) {
      if (s & (1 << i)) {
        int t = s ^ (1 << i);
        if (c[t] >= 0 && a[p] < a[c[t]]) {
          p = b[t];
          q = c[t];
        } else if (a[p] < a[b[t]]) {
          q = p;
          p = b[t];
          assert(p != q);
        } else if (q == -1 || a[q] < a[b[t]] && p != b[t])
          q = b[t];
        else if (q == -1 || a[q] < a[c[t]] && p != c[t])
          q = c[t];
      }
    }
    b[s] = p;
    c[s] = q;
  }
  int acc = -1;
  for (int s = 1; s < 1 << n; s++) {
    assert(b[s] != c[s]);
    acc = max(acc, a[b[s]] + a[c[s]]);
    cout << acc << endl;
  }
}
Problem p03313 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200100
const ll mod = 1e9 + 7;

multiset<int> g[N];
int num[N], a[N];

int n, m;
int main() {
  ios::sync_with_stdio(0);
  while (cin >> n) {
    int k = 1 << n;
    for (int i = 0; i < k; i++)
      cin >> a[i], g[i].insert(a[i]);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        if ((j >> i) & 1) {
          for (int it : g[j ^ (1 << i)])
            g[j].insert(it);
        }
        while (g[j].size() > 2)
          g[j].erase(g[j].begin());
      }
    }
    int ans = 0;
    for (int i = 1; i < k; i++) {
      int res = *g[i].begin() + *g[i].rbegin();
      ans = max(ans, res);
      cout << ans << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 1 << 20
const ll mod = 1e9 + 7;

multiset<int> g[N];
int num[N], a[N];

int n, m;
int main() {
  ios::sync_with_stdio(0);
  while (cin >> n) {
    int k = 1 << n;
    for (int i = 0; i < k; i++)
      cin >> a[i], g[i].insert(a[i]);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < k; j++) {
        if ((j >> i) & 1) {
          for (int it : g[j ^ (1 << i)])
            g[j].insert(it);
        }
        while (g[j].size() > 2)
          g[j].erase(g[j].begin());
      }
    }
    int ans = 0;
    for (int i = 1; i < k; i++) {
      int res = *g[i].begin() + *g[i].rbegin();
      ans = max(ans, res);
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p03313 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

struct Number {
  int a, max{}, second{};
  int sum_max{};
};

int main() {
  int n;
  scanf("%d", &n);

  // std::vector<Number> num(1 << n);
  Number num[256];
  for (int i{}; i < 1 << n; i++)
    scanf("%lld", &num[i].a);

  for (int k{1}; k < 1 << n; k++) {
    num[k].max = k;
    num[k].second = 0;
    if (num[k].second > num[k].max)
      std::swap(num[k].second, num[k].max);
    for (int i{}; 1 << i <= k; i++) {
      if (~k & (1 << i) || k == 1 << i)
        continue;
      int sub[2];
      sub[0] = num[k - (1 << i)].max;
      sub[1] = num[k - (1 << i)].second;
      if (sub[0] == num[k].max)
        std::swap(sub[0], sub[1]);
      else if (num[sub[0]].a >= num[num[k].max].a) {
        num[k].second = num[k].max;
        num[k].max = sub[0];
        std::swap(sub[0], sub[1]);
      }
      if (sub[0] != num[k].second && num[sub[0]].a >= num[num[k].second].a)
        num[k].second = sub[0];
    }
    num[k].sum_max =
        std::max(num[num[k].max].a + num[num[k].second].a, num[k - 1].sum_max);
    printf("%d\n", num[k].sum_max);
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

struct Number {
  int a, max{}, second{};
  int sum_max{};
};

int main() {
  int n;
  scanf("%d", &n);

  std::vector<Number> num(1 << n);
  for (int i{}; i < 1 << n; i++)
    scanf("%lld", &num[i].a);

  for (int k{1}; k < 1 << n; k++) {
    num[k].max = k;
    num[k].second = 0;
    if (num[k].second > num[k].max)
      std::swap(num[k].second, num[k].max);
    for (int i{}; 1 << i <= k; i++) {
      if (~k & (1 << i) || k == 1 << i)
        continue;
      int sub[2];
      sub[0] = num[k - (1 << i)].max;
      sub[1] = num[k - (1 << i)].second;
      if (sub[0] == num[k].max)
        std::swap(sub[0], sub[1]);
      else if (num[sub[0]].a >= num[num[k].max].a) {
        num[k].second = num[k].max;
        num[k].max = sub[0];
        std::swap(sub[0], sub[1]);
      }
      if (sub[0] != num[k].second && num[sub[0]].a >= num[num[k].second].a)
        num[k].second = sub[0];
    }
    num[k].sum_max =
        std::max(num[num[k].max].a + num[num[k].second].a, num[k - 1].sum_max);
    printf("%d\n", num[k].sum_max);
  }

  return 0;
}
Problem p03313 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define rer(i, a, b) for (ll i = ll(a) - 1; i >= ll(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;

const ll MOD = 1000000007;
int N;
P A[20];
ll dp[1 << 18];
P getmax(P x, P y) {
  ll a[4];
  a[0] = x.fr;
  a[1] = x.sc;
  a[2] = y.fr;
  a[3] = y.sc;
  sort(a, a + 4);
  return P(a[3], a[2]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  rep(i, 0, 1 << N) {
    ll a;
    cin >> a;
    A[i] = P(a, 0);
  }
  rep(i, 0, N) {
    rep(j, 0, 1 << N) {
      if (j & (1 << i)) {
        A[j] = getmax(A[j], A[j ^ (1 << i)]);
      }
    }
  }
  dp[1] = A[1].fr + A[1].sc;
  cout << dp[1] << "\n";
  rep(i, 2, 1 << N) {
    dp[i] = max(dp[i - 1], A[i].fr + A[i].sc);
    cout << dp[i] << "\n";
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define rer(i, a, b) for (ll i = ll(a) - 1; i >= ll(b); i--)
#define sz(v) (int)(v).size()
#define pb push_back
#define sc second
#define fr first
#define sor(v) sort(v.begin(), v.end())
#define rev(s) reverse(s.begin(), s.end())
#define lb(vec, a) lower_bound(vec.begin(), vec.end(), a)
#define ub(vec, a) upper_bound(vec.begin(), vec.end(), a)
#define uniq(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;

const ll MOD = 1000000007;
int N;
P A[1 << 18];
ll dp[1 << 18];
P getmax(P x, P y) {
  ll a[4];
  a[0] = x.fr;
  a[1] = x.sc;
  a[2] = y.fr;
  a[3] = y.sc;
  sort(a, a + 4);
  return P(a[3], a[2]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  rep(i, 0, 1 << N) {
    ll a;
    cin >> a;
    A[i] = P(a, 0);
  }
  rep(i, 0, N) {
    rep(j, 0, 1 << N) {
      if (j & (1 << i)) {
        A[j] = getmax(A[j], A[j ^ (1 << i)]);
      }
    }
  }
  dp[1] = A[1].fr + A[1].sc;
  cout << dp[1] << "\n";
  rep(i, 2, 1 << N) {
    dp[i] = max(dp[i - 1], A[i].fr + A[i].sc);
    cout << dp[i] << "\n";
  }
}