Language: C++

#include <bits/stdc++.h>

const int MAXN = 131083;
typedef long long LL;
typedef std::pair<LL, LL> pi;
typedef std::vector<pi> vec;
int son[MAXN][2], val[MAXN][2], n;
LL mid;

vec dfs(int u) {
	if (!son[u][0]) return vec({pi(0, 0)});
	vec lhs = dfs(son[u][0]), rhs = dfs(son[u][1]), r1, r2, r3, r4;
	for (auto & t : lhs) t.first += val[u][0], t.second += val[u][0];
	for (auto & t : rhs) t.first += val[u][1], t.second += val[u][1];
	if (lhs.size() > rhs.size()) std::swap(lhs, rhs);
	const int rs = rhs.size();
	int cur1 = 0, cur2 = rs - 1;
	for (auto t : lhs) {
		while (cur1 < rs && rhs[cur1].second + t.first > mid) ++cur1;
		while (cur2 >= 0 && rhs[cur2].first + t.second > mid) --cur2;
		if (cur1 < rs) r1.emplace_back(rhs[cur1].first, t.second);
		if (cur2 >= 0) r2.emplace_back(t.first, rhs[cur2].second);
	}
	std::reverse(r1.begin(), r1.end());
	std::merge(r1.begin(), r1.end(), r2.begin(), r2.end(), std::back_inserter(r3));
	for (auto t : r3) if (r4.empty() || t.second < r4.back().second) r4.push_back(t);
	return r4;
}
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	std::cin >> n; LL r = 0;
	for (int i = 2, t; i <= n; ++i) {
		std::cin >> t;
		int at = !!son[t][0];
		std::cin >> val[t][at]; r += val[t][at];
		son[t][at] = i;
	}
	LL ans = -1;
	for (LL l = 0; l <= r; mid = l + r >> 1, !dfs(1).empty()
			? (ans = mid, r = mid - 1) : l = mid + 1);
	std::cout << ans << std::endl;
	return 0;
}
#include <bits/stdc++.h>

const int MAXN = 131083;
typedef long long LL;
typedef std::pair<LL, LL> pi;
typedef std::vector<pi> vec;
int son[MAXN][2], val[MAXN][2], n;
LL mid;

vec dfs(int u) {
	if (!son[u][0]) return vec({pi(0, 0)});
	vec lhs = dfs(son[u][0]), rhs = dfs(son[u][1]), r1, r2, r3, r4;
	for (auto & t : lhs) t.first += val[u][0], t.second += val[u][0];
	for (auto & t : rhs) t.first += val[u][1], t.second += val[u][1];
	if (lhs.size() > rhs.size()) std::swap(lhs, rhs);
	const int rs = rhs.size();
	int cur1 = 0, cur2 = rs - 1;
	for (auto t : lhs) {
		while (cur1 < rs && rhs[cur1].second + t.first > mid) ++cur1;
		while (cur2 >= 0 && rhs[cur2].first + t.second > mid) --cur2;
		if (cur1 < rs) r1.emplace_back(rhs[cur1].first, t.second);
		if (cur2 >= 0) r2.emplace_back(t.first, rhs[cur2].second);
	}
	std::reverse(r1.begin(), r1.end());
	std::merge(r1.begin(), r1.end(), r2.begin(), r2.end(), std::back_inserter(r3));
	for (auto t : r3) if (r4.empty() || t.second < r4.back().second) r4.push_back(t);
	return r4;
}
int main() {
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	std::cin >> n; LL r = 0;
	for (int i = 2, t; i <= n; ++i) {
		std::cin >> t;
		int at = !!son[t][0];
		std::cin >> val[t][at]; r += val[t][at];
		son[t][at] = i;
	}
	LL ans = -1;
	for (LL l = 0; l <= r; mid = l + r >> 1, !dfs(1).empty()
			? (ans = mid, r = mid - 1) : l = mid + 1);
	std::cout << ans << std::endl;
	return 0;
}


Compiler output:
a.cc:46:11: error: redefinition of 'const int MAXN'
   46 | const int MAXN = 131083;
      |           ^~~~
a.cc:3:11: note: 'const int MAXN' previously defined here
    3 | const int MAXN = 131083;
      |           ^~~~
a.cc:50:5: error: redefinition of 'int son [131083][2]'
   50 | int son[MAXN][2], val[MAXN][2], n;
      |     ^~~
a.cc:7:5: note: 'int son [131083][2]' previously declared here
    7 | int son[MAXN][2], val[MAXN][2], n;
      |     ^~~
a.cc:50:19: error: redefinition of 'int val [131083][2]'
   50 | int son[MAXN][2], val[MAXN][2], n;
      |                   ^~~
a.cc:7:19: note: 'int val [131083][2]' previously declared here
    7 | int son[MAXN][2], val[MAXN][2], n;
      |                   ^~~
a.cc:50:33: error: redefinition of 'int n'
   50 | int son[MAXN][2], val[MAXN][2], n;
      |                                 ^
a.cc:7:33: note: 'int n' previously declared here
    7 | int son[MAXN][2], val[MAXN][2], n;
      |                                 ^
a.cc:51:4: error: redefinition of 'LL mid'
   51 | LL mid;
      |    ^~~
a.cc:8:4: note: 'LL mid' previously declared here
    8 | LL mid;
      |    ^~~
a.cc:53:5: error: redefinition of 'vec dfs(int)'
   53 | vec dfs(int u) {
      |     ^~~
a.cc:10:5: note: 'vec dfs(int)' previously defined here
   10 | vec dfs(int u) {
      |     ^~~
a.cc:72:5: error: redefinition of 'int main()'
   72 | int main() {
      |     ^~~~
a.cc:29:5: note: 'int main()' previously defined here
   29 | int main() {
      |     ^~~~