Problem p03941 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1ll << 40;
struct edge {
  int v, w;
};
int n;
long long best[2200001];
vector<edge> w[220001];
vector<pair<long long, long long>> f[220001];
bool cmpinv(const pair<int, int> &a, const pair<int, int> &b) {
  return a.second < b.second;
}
void dfs(int u, long long lim) {
  f[u].clear();
  if (w[u].empty()) {
    f[u].push_back({0, 0});
    return;
  }
  for (edge e : w[u]) {
    dfs(e.v, lim);
    for (auto &x : f[e.v])
      x.first += e.w, x.second += e.w;
  }
  auto &a = f[w[u][0].v], &b = f[w[u][1].v];
  int p = a.size(), q = b.size();
  sort(a.begin(), a.end(), cmpinv);
  sort(b.begin(), b.end());
  for (int i = 0; i < q; i++)
    best[i] = min(!i ? oo : best[i - 1], b[i].second);
  for (int i = 0, j = q - 1; i < p; i++) {
    while (j >= 0 && a[i].second + b[j].first > lim)
      j--;
    if (j < 0)
      break;
    f[u].push_back({a[i].first, best[j]});
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end(), cmpinv);
  for (int i = 0; i < q; i++)
    best[i] = min(!i ? oo : best[i - 1], b[i].first);
  for (int i = 0, j = q - 1; i < p; i++) {
    while (j >= 0 && a[i].first + b[j].second > lim)
      j--;
    if (j < 0)
      break;
    f[u].push_back({best[j], a[i].second});
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 2, a, b; i <= n; i++)
    scanf("%d%d", &a, &b), w[a].push_back({i, b});
  long long l = 0, r = oo, ans = r;
  while (l <= r) {
    long long m = (l + r) / 2;
    dfs(1, m);
    if (f[1].empty())
      l = m + 1;
    else
      ans = m, r = m - 1;
  }
  printf("%lld\n", ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long oo = 1ll << 40;
struct edge {
  int v, w;
};
int n;
long long best[2200001];
vector<edge> w[220001];
vector<pair<long long, long long>> f[220001];
bool cmpinv(const pair<int, int> &a, const pair<int, int> &b) {
  return a.second < b.second;
}
void dfs(int u, long long lim) {
  f[u].clear();
  if (w[u].empty()) {
    f[u].push_back({0, 0});
    return;
  }
  for (edge e : w[u]) {
    dfs(e.v, lim);
    for (auto &x : f[e.v])
      x.first += e.w, x.second += e.w;
  }
  auto &a = f[w[u][0].v], &b = f[w[u][1].v];
  int p = a.size(), q = b.size();
  if (!p && !q)
    return;
  if (p > q)
    swap(p, q), swap(a, b);
  sort(a.begin(), a.end(), cmpinv);
  sort(b.begin(), b.end());
  for (int i = 0; i < q; i++)
    best[i] = min(!i ? oo : best[i - 1], b[i].second);
  for (int i = 0, j = q - 1; i < p; i++) {
    while (j >= 0 && a[i].second + b[j].first > lim)
      j--;
    if (j < 0)
      break;
    f[u].push_back({a[i].first, best[j]});
  }
  sort(a.begin(), a.end());
  sort(b.begin(), b.end(), cmpinv);
  for (int i = 0; i < q; i++)
    best[i] = min(!i ? oo : best[i - 1], b[i].first);
  for (int i = 0, j = q - 1; i < p; i++) {
    while (j >= 0 && a[i].first + b[j].second > lim)
      j--;
    if (j < 0)
      break;
    f[u].push_back({best[j], a[i].second});
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 2, a, b; i <= n; i++)
    scanf("%d%d", &a, &b), w[a].push_back({i, b});
  long long l = 0, r = oo, ans = r;
  while (l <= r) {
    long long m = (l + r) / 2;
    dfs(1, m);
    if (f[1].empty())
      l = m + 1;
    else
      ans = m, r = m - 1;
  }
  printf("%lld\n", ans);
}