Problem p03941 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
#define ll long long
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define maxn 100005
#define pb(x) push_back(x)
using namespace std;

vector<pll> v[maxn];

pll nx[maxn], ny[maxn];

int n;

struct note {
  int lson, rson, lv, rv;
} a[maxn];

ll ans, chk;

int sx, sy;

void ins(int x, pll y) {
  if (v[x].size() == 0) {
    v[x].pb(y);
    return;
  }
  int siz = v[x].size();
  if (v[x][siz - 1].se <= y.se)
    return;
  v[x].pb(y);
}

bool dfs(int x) {
  if (a[x].lson == 0) {
    v[x].pb(mp(0, 0));
    return 1;
  }
  if (!dfs(a[x].lson))
    return 0;
  if (!dfs(a[x].rson))
    return 0;
  sx = sy = 0;
  int lson = a[x].lson, rson = a[x].rson, add = a[x].lv + a[x].rv;
  //
  int wz = v[rson].size() - 1;
  fd(i, v[lson].size() - 1, 0) {
    while (wz >= 0 && v[lson][i].se + v[rson][wz].fi + add > chk)
      wz--;
    if (wz < 0)
      break;
    sx++;
    nx[sx] = mp(v[lson][i].fi + a[x].lv, v[rson][wz].se + a[x].rv);
  }
  //
  wz = v[lson].size() - 1;
  fd(i, v[rson].size() - 1, 0) {
    while (wz >= 0 && v[rson][i].se + v[lson][wz].fi + add > chk)
      wz--;
    if (wz < 0)
      break;
    sy++;
    ny[sy] = mp(v[rson][i].fi + a[x].rv, v[lson][wz].se + a[x].lv);
  }
  //
  int wz1 = sx, wz2 = sy;
  while (wz1 && wz2) {
    if (nx[wz1].fi < ny[wz2].fi)
      ins(x, nx[wz1]), wz1--;
    else
      ins(x, ny[wz2]), wz2--;
  }
  while (wz1)
    ins(x, nx[wz1]), wz1--;
  while (wz2)
    ins(x, ny[wz2]), wz2--;
  return v[x].size();
}

int main() {
  scanf("%d", &n);
  fo(i, 2, n) {
    int father, len;
    scanf("%d%d", &father, &len);
    if (a[father].lson == 0) {
      a[father].lson = i;
      a[father].lv = len;
    } else {
      a[father].rson = i;
      a[father].rv = len;
    }
  }
  ll l = 0, r = 1e10;
  while (l <= r) {
    ll mid = (l + r) >> 1;
    chk = mid;
    fo(i, 1, n) v[i].clear();
    if (dfs(1)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define fo(i, a, b) for (int i = a; i <= b; i++)
#define fd(i, a, b) for (int i = a; i >= b; i--)
#define ll long long
#define pll pair<ll, ll>
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define maxn 140005
#define pb(x) push_back(x)
using namespace std;

vector<pll> v[maxn];

pll nx[maxn], ny[maxn];

int n;

struct note {
  int lson, rson, lv, rv;
} a[maxn];

ll ans, chk;

int sx, sy;

void ins(int x, pll y) {
  if (v[x].size() == 0) {
    v[x].pb(y);
    return;
  }
  int siz = v[x].size();
  if (v[x][siz - 1].se <= y.se)
    return;
  v[x].pb(y);
}

bool dfs(int x) {
  if (a[x].lson == 0) {
    v[x].pb(mp(0, 0));
    return 1;
  }
  if (!dfs(a[x].lson))
    return 0;
  if (!dfs(a[x].rson))
    return 0;
  sx = sy = 0;
  int lson = a[x].lson, rson = a[x].rson, add = a[x].lv + a[x].rv;
  //
  int wz = v[rson].size() - 1;
  fd(i, v[lson].size() - 1, 0) {
    while (wz >= 0 && v[lson][i].se + v[rson][wz].fi + add > chk)
      wz--;
    if (wz < 0)
      break;
    sx++;
    nx[sx] = mp(v[lson][i].fi + a[x].lv, v[rson][wz].se + a[x].rv);
  }
  //
  wz = v[lson].size() - 1;
  fd(i, v[rson].size() - 1, 0) {
    while (wz >= 0 && v[rson][i].se + v[lson][wz].fi + add > chk)
      wz--;
    if (wz < 0)
      break;
    sy++;
    ny[sy] = mp(v[rson][i].fi + a[x].rv, v[lson][wz].se + a[x].lv);
  }
  //
  int wz1 = sx, wz2 = sy;
  while (wz1 && wz2) {
    if (nx[wz1].fi < ny[wz2].fi)
      ins(x, nx[wz1]), wz1--;
    else
      ins(x, ny[wz2]), wz2--;
  }
  while (wz1)
    ins(x, nx[wz1]), wz1--;
  while (wz2)
    ins(x, ny[wz2]), wz2--;
  return v[x].size();
}

int main() {
  scanf("%d", &n);
  fo(i, 2, n) {
    int father, len;
    scanf("%d%d", &father, &len);
    if (a[father].lson == 0) {
      a[father].lson = i;
      a[father].lv = len;
    } else {
      a[father].rson = i;
      a[father].rv = len;
    }
  }
  ll l = 0, r = 1e10;
  while (l <= r) {
    ll mid = (l + r) >> 1;
    chk = mid;
    fo(i, 1, n) v[i].clear();
    if (dfs(1)) {
      ans = mid;
      r = mid - 1;
    } else
      l = mid + 1;
  }
  cout << ans;
  return 0;
}