Problem p03941 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 300010, S = 1000010;

int n, cnt, l[N], r[N], fa[N], len[N], sz[N], c[N][2];
ll a[S], b[S], p[N], q[N], np[N], nq[N];

bool dfs(int u, ll limit) {
  int x = c[u][0], y = c[u][1];
  if (!x) {
    l[u] = r[u] = ++cnt;
    sz[u] = 1;
    a[cnt] = b[cnt] = len[u];
    return 1;
  }
  if (dfs(x, limit) && dfs(y, limit)) {
    if (sz[x] > sz[y])
      swap(x, y);
    int tp = 0;
    for (int i = l[x], j = l[y]; i <= r[x]; ++i) {
      while (j <= r[y] && b[i] + a[j] <= limit)
        ++j;
      if (j > l[y])
        p[++tp] = a[i], q[tp] = b[j - 1];
    }
    if (!tp)
      return 0;
    for (int i = tp + 1; i <= (tp << 1); ++i) {
      p[i] = q[tp - (i - tp) + 1];
      q[i] = p[tp - (i - tp) + 1];
    }
    int k = 1;
    for (int i = 1, j = tp + 1; k <= (tp << 1); ++k)
      if ((j > (tp << 1)) || i <= tp && p[i] <= p[j]) {
        np[k] = p[i];
        nq[k] = q[i++];
      } else {
        np[k] = p[j];
        nq[k] = q[j++];
      }
    l[u] = cnt + 1;
    for (int i = 1; i <= (tp << 1); ++i)
      if (i == 1 || np[i] > a[cnt] && nq[i] < b[cnt]) {
        a[++cnt] = np[i];
        b[cnt] = nq[i];
      } else if (np[i] == np[i - 1])
        b[cnt] = min(b[cnt], nq[i]);
    r[u] = cnt;
    sz[u] = r[u] - l[u] + 1;
    for (int i = l[u]; i <= r[u]; ++i) {
      a[i] += len[u];
      b[i] += len[u];
    }
  } else
    return 0;
  return 1;
}

inline bool check(ll x) {
  cnt = 0;
  return dfs(1, x);
}

int main() {
  scanf("%d", &n);
  for (int i = 2; i <= n; ++i) {
    scanf("%d%d", &fa[i], &len[i]);
    c[fa[i]][c[fa[i]][0] > 0] = i;
  }
  ll l = 0, r = 1e15, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%lld\n", r + 1);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 600010, S = 2000010;

int n, cnt, l[N], r[N], fa[N], len[N], sz[N], c[N][2];
ll a[S], b[S], p[N], q[N], np[N], nq[N];

bool dfs(int u, ll limit) {
  int x = c[u][0], y = c[u][1];
  if (!x) {
    l[u] = r[u] = ++cnt;
    sz[u] = 1;
    a[cnt] = b[cnt] = len[u];
    return 1;
  }
  if (dfs(x, limit) && dfs(y, limit)) {
    if (sz[x] > sz[y])
      swap(x, y);
    int tp = 0;
    for (int i = l[x], j = l[y]; i <= r[x]; ++i) {
      while (j <= r[y] && b[i] + a[j] <= limit)
        ++j;
      if (j > l[y])
        p[++tp] = a[i], q[tp] = b[j - 1];
    }
    if (!tp)
      return 0;
    for (int i = tp + 1; i <= (tp << 1); ++i) {
      p[i] = q[tp - (i - tp) + 1];
      q[i] = p[tp - (i - tp) + 1];
    }
    int k = 1;
    for (int i = 1, j = tp + 1; k <= (tp << 1); ++k)
      if ((j > (tp << 1)) || i <= tp && p[i] <= p[j]) {
        np[k] = p[i];
        nq[k] = q[i++];
      } else {
        np[k] = p[j];
        nq[k] = q[j++];
      }
    l[u] = cnt + 1;
    for (int i = 1; i <= (tp << 1); ++i)
      if (i == 1 || np[i] > a[cnt] && nq[i] < b[cnt]) {
        a[++cnt] = np[i];
        b[cnt] = nq[i];
      } else if (np[i] == np[i - 1])
        b[cnt] = min(b[cnt], nq[i]);
    r[u] = cnt;
    sz[u] = r[u] - l[u] + 1;
    for (int i = l[u]; i <= r[u]; ++i) {
      a[i] += len[u];
      b[i] += len[u];
    }
  } else
    return 0;
  return 1;
}

inline bool check(ll x) {
  cnt = 0;
  return dfs(1, x);
}

int main() {
  scanf("%d", &n);
  for (int i = 2; i <= n; ++i) {
    scanf("%d%d", &fa[i], &len[i]);
    c[fa[i]][c[fa[i]][0] > 0] = i;
  }
  ll l = 0, r = 1e15, mid;
  while (l <= r) {
    mid = (l + r) >> 1;
    if (check(mid))
      r = mid - 1;
    else
      l = mid + 1;
  }
  printf("%lld\n", r + 1);
}