Language: C++

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

static const int INF = 0x3f3f3f3f;

int main() {
        int n;
        scanf("%d", &n);
        vector<vector<pair<int, int>>> g(n), dp(n);
        for (int i = 1; i < n; i ++) {
                int a, v;
                scanf("%d%d", &a, &v);
                a --;
                g[a].emplace_back(v, i);
        }
        function<void (int, int)> dfs = [&](int u, int x) {
                if (g[u].size() == 0) return;
                pair<int, int> a = g[u][0], b = g[u][1];
                dfs(a.second, x);
                dfs(b.second, x);
                int l = a.first + b.first;
                vector<pair<int, int>> v;
                if (dp[a.second].size() > dp[b.second].size()) swap(a, b);
                for (int i = 0, j = 0; i < dp[a.second].size(); i ++) {
                        while (j < dp[b.second].size() && dp[b.second][j].first + dp[a.second][i].second + l <= x) j ++;
                        if (j != 0) {
                                v.emplace_back(dp[a.second][i].first + a.first, dp[b.second][j - 1].second + b.first);
                        }
                }
                for (int i = 0, j = 0; i < dp[a.second].size(); i ++) {
                        while (j < dp[b.second].size() && dp[b.second][j].second + dp[a.second][i].first + l > x) j ++;
                        if (j != dp[b.second].size()) {
                                v.emplace_back(dp[b.second][j].first + b.first, dp[a.second][i].second + a.first);
                        }
                }
                sort(v.begin(), v.end());
                int now = INF;
                for (int i = 0; i < v.size(); i ++) {
                        if (i > 0 && v[i].first == v[i - 1].first) continue;
                        if (now > v[i].second) {
                                dp[u].push_back(v[i]);
                                now = v[i].second;
                        }
                }
        };        
        function<bool (int)> check = [&](int x) {
                for (int i = 0; i < n; i ++) if (g[i].size() == 0) dp[i].emplace_back(0, 0);
                dfs(0, x);
                int ok = dp[0].size();
                for (int i = 0; i < n; i ++) dp[i].clear();
                return ok != 0;
        };
        int lb = 0, ub = 1e9, ans = -1;
        while (lb <= ub) {
                int mid = (lb + ub) / 2;
                if (check(mid)) ans = mid, ub = mid - 1;
                else lb = mid + 1;
        }
        printf("%d\n", ans);
        return 0;
}



Compiler output:
a.cc: In function 'int main()':
a.cc:22:9: error: 'function' was not declared in this scope
   22 |         function<void (int, int)> dfs = [&](int u, int x) {
      |         ^~~~~~~~
a.cc:8:1: note: 'std::function' is defined in header '<functional>'; this is probably fixable by adding '#include <functional>'
    7 | #include <set>
  +++ |+#include <functional>
    8 | using namespace std;
a.cc:22:32: error: expression list treated as compound expression in functional cast [-fpermissive]
   22 |         function<void (int, int)> dfs = [&](int u, int x) {
      |                                ^
a.cc:22:18: error: expected primary-expression before 'void'
   22 |         function<void (int, int)> dfs = [&](int u, int x) {
      |                  ^~~~
a.cc:52:18: error: expected primary-expression before 'bool'
   52 |         function<bool (int)> check = [&](int x) {
      |                  ^~~~
a.cc:62:21: error: 'check' was not declared in this scope
   62 |                 if (check(mid)) ans = mid, ub = mid - 1;
      |                     ^~~~~