Problem p03941 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 266666, mod = 998244353;
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int n, el, head[maxn], to[maxn], nxt[maxn], w[maxn];
ll lim, pre[maxn];
struct item {
  ll in, out;
};
vector<item> v[maxn];
inline bool cmp1(item i1, item i2) { return i1.in < i2.in; }
inline bool cmp2(item i1, item i2) { return i1.out < i2.out; }
inline void add(int u, int v, int w_) {
  to[++el] = v;
  nxt[el] = head[u];
  head[u] = el;
  w[el] = w_;
}
void dfs(int u, int f) {
  int ls = 0, rs = 0, dyls = 0, dyrs = 0; // nice variable
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == f)
      continue;
    dfs(v, u);
    if (!ls)
      ls = v, dyls = w[i];
    else
      rs = v;
    dyrs = w[i];
  }
  if (!ls && !rs) {
    v[u].push_back((item){0, 0});
    return;
  }
  vector<item> &l = v[ls], &r = v[rs], &ans = v[u];
  if (l.empty() || r.empty())
    return;
  FOR(i, 0, l.size() - 1) l[i].in += dyls, l[i].out += dyls;
  FOR(i, 0, r.size() - 1) r[i].in += dyrs, r[i].out += dyrs;
  sort(l.begin(), l.end(), cmp2);
  sort(r.begin(), r.end(), cmp1);
  pre[0] = r[0].out;
  FOR(i, 1, (int)r.size() - 1) pre[i] = min(pre[i - 1], r[i].out);
  int cur = r.size() - 1;
  FOR(i, 0, (int)l.size() - 1) {
    while (cur >= 0 && l[i].out + r[cur].in > lim)
      cur--;
    if (cur >= 0)
      ans.push_back((item){l[i].in, pre[cur]});
    else
      break;
  }
  sort(l.begin(), l.end(), cmp1);
  sort(r.begin(), r.end(), cmp2);
  pre[0] = r[0].in;
  FOR(i, 1, (int)r.size() - 1) pre[i] = min(pre[i - 1], r[i].in);
  cur = r.size() - 1;
  FOR(i, 0, (int)l.size() - 1) {
    while (cur >= 0 && l[i].in + r[cur].out > lim)
      cur--;
    if (cur >= 0)
      ans.push_back((item){pre[cur], l[i].out});
    else
      break;
  }
}
int main() {
  n = read();
  FOR(i, 1, n - 1) {
    int u = read(), w = read();
    add(u, i + 1, w);
    add(i + 1, u, w);
  }
  ll l = 0, r = 1e11;
  while (l < r) {
    lim = (l + r) >> 1;
    FOR(i, 1, n) v[i].clear();
    dfs(1, 0);
    if (!v[1].empty())
      r = lim;
    else
      l = lim + 1;
  }
  printf("%lld\n", l);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 266666, mod = 998244353;
#define lson o << 1, l, mid
#define rson o << 1 | 1, mid + 1, r
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define ROF(i, a, b) for (int i = (a); i >= (b); i--)
#define MEM(x, v) memset(x, v, sizeof(x))
inline int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    f |= ch == '-', ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return f ? -x : x;
}
int n, el, head[maxn], to[maxn], nxt[maxn], w[maxn];
ll lim, pre[maxn];
struct item {
  ll in, out;
};
vector<item> v[maxn];
inline bool cmp1(item i1, item i2) { return i1.in < i2.in; }
inline bool cmp2(item i1, item i2) { return i1.out < i2.out; }
inline void add(int u, int v, int w_) {
  to[++el] = v;
  nxt[el] = head[u];
  head[u] = el;
  w[el] = w_;
}
void dfs(int u, int f) {
  int ls = 0, rs = 0, dyls = 0, dyrs = 0; // nice variable
  for (int i = head[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v == f)
      continue;
    dfs(v, u);
    if (!ls)
      ls = v, dyls = w[i];
    else
      rs = v;
    dyrs = w[i];
  }
  if (!ls && !rs) {
    v[u].push_back((item){0, 0});
    return;
  }
  vector<item> &l = v[ls], &r = v[rs], &ans = v[u];
  if (l.empty() || r.empty())
    return;
  FOR(i, 0, l.size() - 1) l[i].in += dyls, l[i].out += dyls;
  FOR(i, 0, r.size() - 1) r[i].in += dyrs, r[i].out += dyrs;
  if (l.size() > r.size())
    swap(l, r);
  sort(l.begin(), l.end(), cmp2);
  sort(r.begin(), r.end(), cmp1);
  pre[0] = r[0].out;
  FOR(i, 1, (int)r.size() - 1) pre[i] = min(pre[i - 1], r[i].out);
  int cur = r.size() - 1;
  FOR(i, 0, (int)l.size() - 1) {
    while (cur >= 0 && l[i].out + r[cur].in > lim)
      cur--;
    if (cur >= 0)
      ans.push_back((item){l[i].in, pre[cur]});
    else
      break;
  }
  sort(l.begin(), l.end(), cmp1);
  sort(r.begin(), r.end(), cmp2);
  pre[0] = r[0].in;
  FOR(i, 1, (int)r.size() - 1) pre[i] = min(pre[i - 1], r[i].in);
  cur = r.size() - 1;
  FOR(i, 0, (int)l.size() - 1) {
    while (cur >= 0 && l[i].in + r[cur].out > lim)
      cur--;
    if (cur >= 0)
      ans.push_back((item){pre[cur], l[i].out});
    else
      break;
  }
}
int main() {
  n = read();
  FOR(i, 1, n - 1) {
    int u = read(), w = read();
    add(u, i + 1, w);
    add(i + 1, u, w);
  }
  ll l = 0, r = 1e11;
  while (l < r) {
    lim = (l + r) >> 1;
    FOR(i, 1, n) v[i].clear();
    dfs(1, 0);
    if (!v[1].empty())
      r = lim;
    else
      l = lim + 1;
  }
  printf("%lld\n", l);
}