Problem p03301 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; i++)
#define DREP(i, a, b) for (int i = (a), _end_ = (b); i >= _end_; i--)
#define EREP(i, u) for (int i = start[u]; i; i = e[i].next)
#define fi first
#define se second
#define mkr(a, b) make_pair(a, b)
#define SZ(A) ((int)A.size())
template <class T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
inline int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    ch = getchar(), f = -1;
  while (isdigit(ch))
    s = s * 10 + ch - '0', ch = getchar();
  return ~f ? s : -s;
}

const int maxn = 1e5 + 20;

int n, a[maxn], sum;

inline void init() {
  n = read();
  REP(i, 1, n) a[i] = read(), sum += a[i];
}

int x[maxn], y[maxn];

inline bool check(int ck) {
  int mx = 0;
  REP(i, 1, n >> 1) {
    if (x[i] + mx >= ck)
      chkmax(mx, -y[i]);
  }
  if (x[n + 1 >> 1] + mx >= ck)
    return 1;
  else
    return 0;
}

inline void doing() {
  if (~n & 1) {
    int B = 0, W = 0;
    REP(i, 1, n) if (i & 1) B += a[i];
    else W += a[i];
    if (B < W)
      swap(B, W);
    printf("%d %d\n", B, W);
  } else {
    int W = 0;
    REP(i, 1, n) if (~i & 1) W += a[i];
    REP(i, 1, n + 1 >> 1)
    x[i] = x[i - 1] + a[i * 2 - 1] - a[i * 2 - 2],
    y[i] = y[i - 1] + a[i * 2 - 1] - a[i * 2];
    int l = 0, r = sum;
    while (l < r) {
      int mid = l + r + 1 >> 1;
      if (check(mid))
        l = mid;
      else
        r = mid - 1;
    }
    printf("%d %d\n", W + l, sum - W - l);
  }
}

int main() {
  init();
  doing();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define REP(i, a, b) for (int i = (a), _end_ = (b); i <= _end_; i++)
#define DREP(i, a, b) for (int i = (a), _end_ = (b); i >= _end_; i--)
#define EREP(i, u) for (int i = start[u]; i; i = e[i].next)
#define fi first
#define se second
#define mkr(a, b) make_pair(a, b)
#define SZ(A) ((int)A.size())
template <class T> inline void chkmin(T &a, T b) {
  if (a > b)
    a = b;
}
template <class T> inline void chkmax(T &a, T b) {
  if (a < b)
    a = b;
}
inline int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch) && ch != '-')
    ch = getchar();
  if (ch == '-')
    ch = getchar(), f = -1;
  while (isdigit(ch))
    s = s * 10 + ch - '0', ch = getchar();
  return ~f ? s : -s;
}

const int maxn = 3e5 + 20;

int n, a[maxn], sum;

inline void init() {
  n = read();
  REP(i, 1, n) a[i] = read(), sum += a[i];
}

int x[maxn], y[maxn];

inline bool check(int ck) {
  int mx = 0;
  REP(i, 1, n >> 1) {
    if (x[i] + mx >= ck)
      chkmax(mx, -y[i]);
  }
  if (x[n + 1 >> 1] + mx >= ck)
    return 1;
  else
    return 0;
}

inline void doing() {
  if (~n & 1) {
    int B = 0, W = 0;
    REP(i, 1, n) if (i & 1) B += a[i];
    else W += a[i];
    if (B < W)
      swap(B, W);
    printf("%d %d\n", B, W);
  } else {
    int W = 0;
    REP(i, 1, n) if (~i & 1) W += a[i];
    REP(i, 1, n + 1 >> 1)
    x[i] = x[i - 1] + a[i * 2 - 1] - a[i * 2 - 2],
    y[i] = y[i - 1] + a[i * 2 - 1] - a[i * 2];
    int l = 0, r = sum;
    while (l < r) {
      int mid = l + r + 1 >> 1;
      if (check(mid))
        l = mid;
      else
        r = mid - 1;
    }
    printf("%d %d\n", W + l, sum - W - l);
  }
}

int main() {
  init();
  doing();
  return 0;
}