Problem p03941 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 100005
typedef long long ll;
#define pii pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
int ch[maxn][2];
ll val[maxn][2];
ll len[maxn];
vector<pii> v[maxn];
pii tmp[maxn << 3];
int tot;
bool dfs(int u, ll mid) {
  v[u].clear();
  if (!ch[u][0]) {
    v[u].pb(mp(len[u], len[u]));
    return 1;
  }
  int ls = ch[u][0], rs = ch[u][1];
  if ((dfs(ls, mid) | dfs(rs, mid)) == 0)
    return 0;
  tot = -1;
  if (v[rs].size() < v[ls].size())
    swap(ls, rs);
  // sort(v[ls],v[ls]+v[ls].size()); sort(v[rs],v[rs]+v[rs].size());
  for (int i = 0, j = v[rs].size() - 1; i < v[ls].size(); i++) {
    while (j >= 0 && v[ls][i].fi + v[rs][j].fi > mid)
      j--;
    if (j < 0)
      break;
    tmp[++tot] = mp(v[ls][i].se + len[u], v[rs][j].se + len[u]);
    tmp[++tot] = mp(v[rs][j].se + len[u], v[ls][i].se + len[u]);
  }
  if (tot == -1)
    return 0;
  sort(tmp, tmp + tot + 1);
  ll h = 1ll << 60;
  for (int i = 0; i <= tot; i++) {
    if (tmp[i].se >= h)
      continue;
    v[u].pb(tmp[i]);
    h = tmp[i].se;
  }
  return 1;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int f, w;
    scanf("%d%d", &f, &w);
    len[i + 1] = 1ll * w;
    if (!ch[f][0])
      ch[f][0] = i + 1, val[f][0] = 1ll * w;
    else
      ch[f][1] = i + 1, val[f][1] = 1ll * w;
  }
  ll l = 0, r = 1ll << 40, ans;
  while (l <= r) {
    ll mid = (l + r) >> 1;
    if (dfs(1, mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
    /*for(int i=1;i<=n;i++)
    {
        printf(" %d : ",i);
        for(int j=0;j<v[i].size();j++)
        {
            printf(" (%d , %d) ",v[i][j].fi,v[i][j].se);
        }
        printf("\n");
    }
    printf("\n");*/
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define maxn 150005
typedef long long ll;
#define pii pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
int ch[maxn][2];
ll val[maxn][2];
ll len[maxn];
vector<pii> v[maxn];
pii tmp[maxn << 3];
int tot;
bool dfs(int u, ll mid) {
  v[u].clear();
  if (!ch[u][0]) {
    v[u].pb(mp(len[u], len[u]));
    return 1;
  }
  int ls = ch[u][0], rs = ch[u][1];
  if ((dfs(ls, mid) | dfs(rs, mid)) == 0)
    return 0;
  tot = -1;
  if (v[rs].size() < v[ls].size())
    swap(ls, rs);
  // sort(v[ls],v[ls]+v[ls].size()); sort(v[rs],v[rs]+v[rs].size());
  for (int i = 0, j = v[rs].size() - 1; i < v[ls].size(); i++) {
    while (j >= 0 && v[ls][i].fi + v[rs][j].fi > mid)
      j--;
    if (j < 0)
      break;
    tmp[++tot] = mp(v[ls][i].se + len[u], v[rs][j].se + len[u]);
    tmp[++tot] = mp(v[rs][j].se + len[u], v[ls][i].se + len[u]);
  }
  if (tot == -1)
    return 0;
  sort(tmp, tmp + tot + 1);
  ll h = 1ll << 60;
  for (int i = 0; i <= tot; i++) {
    if (tmp[i].se >= h)
      continue;
    v[u].pb(tmp[i]);
    h = tmp[i].se;
  }
  return 1;
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int f, w;
    scanf("%d%d", &f, &w);
    len[i + 1] = 1ll * w;
    if (!ch[f][0])
      ch[f][0] = i + 1, val[f][0] = 1ll * w;
    else
      ch[f][1] = i + 1, val[f][1] = 1ll * w;
  }
  ll l = 0, r = 1ll << 40, ans;
  while (l <= r) {
    ll mid = (l + r) >> 1;
    if (dfs(1, mid))
      ans = mid, r = mid - 1;
    else
      l = mid + 1;
    /*for(int i=1;i<=n;i++)
    {
        printf(" %d : ",i);
        for(int j=0;j<v[i].size();j++)
        {
            printf(" (%d , %d) ",v[i][j].fi,v[i][j].se);
        }
        printf("\n");
    }
    printf("\n");*/
  }
  printf("%lld\n", ans);
  return 0;
}