Language: C++

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;
const int N=2*(1e5)+10;
struct Pr{
	ll in,out;
	Pr(){}
	Pr(ll x,ll y){in=x; out=y;}
};
int h[N],ch[N][2],dis[N][2];
ll rec[N];
vector<Pr> f[N];
int n,tot;
ll sum,Mx;
bool cmp1(Pr x,Pr y){return x.in<y.in;}
bool cmp2(Pr x,Pr y){return x.out<y.out;}
void merge(int x){
	int lch=ch[x][0],rch=ch[x][1];
	if (f[lch].size()>f[rch].size()) swap(lch,rch);
	int lsz=f[lch].size(),rsz=f[rch].size();
 
	sort(f[lch].begin(),f[lch].end(),cmp2);
	sort(f[rch].begin(),f[rch].end(),cmp1);
	rec[0]=f[rch][0].out;
	for (int i=1;i<rsz;++i) rec[i]=min(rec[i-1],f[rch][i].out);
	for (int i=0,j=rsz-1;i<lsz;++i){
		while (j>=0&&f[lch][i].out+f[rch][j].in>Mx) --j;
		if (j>=0) f[x].push_back(Pr(f[lch][i].in,rec[j]));
	}
 
	sort(f[lch].begin(),f[lch].end(),cmp1);
	sort(f[rch].begin(),f[rch].end(),cmp2);
	rec[0]=f[rch][0].in;
	for (int i=1;i<rsz;++i) rec[i]=min(rec[i-1],f[rch][i].in);
	for (int i=0,j=rsz-1;i<lsz;++i){
		while (j>=0&&f[rch][j].out+f[lch][i].in>Mx) --j;
		if (j>=0) f[x].push_back(Pr(rec[j],f[lch][i].out));
	}
}
void dfs(int x){
	int u,sz;
	f[x].clear();
	if (ch[x][0]==0&&ch[x][1]==0){
		f[x].push_back(Pr(0,0));
		return;
	}
	dfs(ch[x][0]);
	dfs(ch[x][1]);
	sz=f[ch[x][0]].size();
	for (int i=0;i<sz;++i) 
		f[ch[x][0]][i].in+=dis[x][0],f[ch[x][0]][i].out+=dis[x][0];
	sz=f[ch[x][1]].size();
	for (int i=0;i<sz;++i) 
		f[ch[x][1]][i].in+=dis[x][1],f[ch[x][1]][i].out+=dis[x][1];
	merge(x);
}
bool check(ll x){
	Mx=x;
	dfs(1);
	return f[1].size()>0;
}
void solve(){
	ll l=0,r=sum,mid,ans=0;
	while (l<=r){
		mid=l+r>>1;
		if (check(mid)) ans=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%lld\n",ans);
}
int read(){
	int ret=0; char ch=getchar();
	while (ch<'0'||ch>'9') ch=getchar();
	while ('0'<=ch&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
	return ret;
}
 
int main(){
#ifndef ONLINE_JUDGE
	//freopen("a.in","r",stdin);
#endif
	int x,y;
	n=read();
	memset(h,-1,sizeof(h));
	tot=0; sum=0;
	for (int i=1;i<n;++i){
		x=read(); y=read();
		if (ch[x][0]) ch[x][1]=i+1,dis[x][1]=y;
		else ch[x][0]=i+1,dis[x][0]=y;
		sum+=y;
	}
	solve();
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#define ll long long
using namespace std;
const int N=2*(1e5)+10;
struct Pr{
	ll in,out;
	Pr(){}
	Pr(int x,int y){in=x; out=y;}
};
int h[N],ch[N][2],dis[N][2];
ll rec[N];
vector<Pr> f[N];
int n,tot;
ll sum,Mx;
bool cmp1(Pr x,Pr y){return x.in<y.in;}
bool cmp2(Pr x,Pr y){return x.out<y.out;}
void merge(int x){
	int lch=ch[x][0],rch=ch[x][1];
	if (f[lch].size()>f[rch].size()) swap(lch,rch);
	int lsz=f[lch].size(),rsz=f[rch].size();

	sort(f[lch].begin(),f[lch].end(),cmp2);
	sort(f[rch].begin(),f[rch].end(),cmp1);
	rec[0]=f[rch][0].out;
	for (int i=1;i<rsz;++i) rec[i]=min(rec[i-1],f[rch][i].out);
	for (int i=0,j=rsz-1;i<lsz;++i){
		while (j>=0&&f[lch][i].out+f[rch][j].in>Mx) --j;
		if (j>=0) f[x].push_back(Pr(f[lch][i].in,rec[j]));
	}

	sort(f[lch].begin(),f[lch].end(),cmp1);
	sort(f[rch].begin(),f[rch].end(),cmp2);
	rec[0]=f[rch][0].in;
	for (int i=1;i<rsz;++i) rec[i]=min(rec[i-1],f[rch][i].in);
	for (int i=0,j=rsz-1;i<lsz;++i){
		while (j>=0&&f[rch][j].out+f[lch][i].in>Mx) --j;
		if (j>=0) f[x].push_back(Pr(rec[j],f[lch][i].out));
	}
}
void dfs(int x){
	int u,sz;
	f[x].clear();
	if (ch[x][0]==0&&ch[x][1]==0){
		f[x].push_back(Pr(0,0));
		return;
	}
	dfs(ch[x][0]);
	dfs(ch[x][1]);
	sz=f[ch[x][0]].size();
	for (int i=0;i<sz;++i) 
		f[ch[x][0]][i].in+=dis[x][0],f[ch[x][0]][i].out+=dis[x][0];
	sz=f[ch[x][1]].size();
	for (int i=0;i<sz;++i) 
		f[ch[x][1]][i].in+=dis[x][1],f[ch[x][1]][i].out+=dis[x][1];
	merge(x);
}
bool check(ll x){
	Mx=x;
	dfs(1);
	return f[1].size()>0;
}
void solve(){
	ll l=0,r=sum,mid,ans=0;
	while (l<=r){
		mid=l+r>>1;
		if (check(mid)) ans=mid,r=mid-1;
		else l=mid+1;
	}
	printf("%lld\n",ans);
}
int read(){
	int ret=0; char ch=getchar();
	while (ch<'0'||ch>'9') ch=getchar();
	while ('0'<=ch&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
	return ret;
}

int main(){
#ifndef ONLINE_JUDGE
	//freopen("a.in","r",stdin);
#endif
	int x,y;
	n=read();
	memset(h,-1,sizeof(h));
	tot=0; sum=0;
	for (int i=1;i<n;++i){
		x=read(); y=read();
		if (ch[x][0]) ch[x][1]=i+1,dis[x][1]=y;
		else ch[x][0]=i+1,dis[x][0]=y;
		sum+=y;
	}
	solve();
}


Compiler output:
a.cc:105:11: error: redefinition of 'const int N'
  105 | const int N=2*(1e5)+10;
      |           ^
a.cc:8:11: note: 'const int N' previously defined here
    8 | const int N=2*(1e5)+10;
      |           ^
a.cc:106:8: error: redefinition of 'struct Pr'
  106 | struct Pr{
      |        ^~
a.cc:9:8: note: previous definition of 'struct Pr'
    9 | struct Pr{
      |        ^~
a.cc:111:5: error: redefinition of 'int h [200010]'
  111 | int h[N],ch[N][2],dis[N][2];
      |     ^
a.cc:14:5: note: 'int h [200010]' previously declared here
   14 | int h[N],ch[N][2],dis[N][2];
      |     ^
a.cc:111:10: error: redefinition of 'int ch [200010][2]'
  111 | int h[N],ch[N][2],dis[N][2];
      |          ^~
a.cc:14:10: note: 'int ch [200010][2]' previously declared here
   14 | int h[N],ch[N][2],dis[N][2];
      |          ^~
a.cc:111:19: error: redefinition of 'int dis [200010][2]'
  111 | int h[N],ch[N][2],dis[N][2];
      |                   ^~~
a.cc:14:19: note: 'int dis [200010][2]' previously declared here
   14 | int h[N],ch[N][2],dis[N][2];
      |                   ^~~
a.cc:112:4: error: redefinition of 'long long int rec [200010]'
  112 | ll rec[N];
      |    ^~~
a.cc:15:4: note: 'long long int rec [200010]' previously declared here
   15 | ll rec[N];
      |    ^~~
a.cc:113:12: error: redefinition of 'std::vector<Pr> f [200010]'
  113 | vector<Pr> f[N];
      |            ^
a.cc:16:12: note: 'std::vector<Pr> f [200010]' previously declared here
   16 | vector<Pr> f[N];
      |            ^
a.cc:114:5: error: redefinition of 'int n'
  114 | int n,tot;
      |     ^
a.cc:17:5: note: 'int n' previously declared here
   17 | int n,tot;
      |     ^
a.cc:114:7: error: redefinition of 'int tot'
  114 | int n,tot;
      |       ^~~
a.cc:17:7: note: 'int tot' previously declared here
   17 | int n,tot;
      |       ^~~
a.cc:115:4: error: redefinition of 'long long int sum'
  115 | ll sum,Mx;
      |    ^~~
a.cc:18:4: note: 'long long int sum' previously declared here
   18 | ll sum,Mx;
      |    ^~~
a.cc:115:8: error: redefinition of 'long long int Mx'
  115 | ll sum,Mx;
      |        ^~
a.cc:18:8: note: 'long long int Mx' previously declared here
   18 | ll sum,Mx;
      |        ^~
a.cc:116:6: error: redefinition of 'bool cmp1(Pr, Pr)'
  116 | bool cmp1(Pr x,Pr y){return x.in<y.in;}
      |      ^~~~
a.cc:19:6: note: 'bool cmp1(Pr, Pr)' previously defined here
   19 | bool cmp1(Pr x,Pr y){return x.in<y.in;}
      |      ^~~~
a.cc:117:6: error: redefinition of 'bool cmp2(Pr, Pr)'
  117 | bool cmp2(Pr x,Pr y){return x.out<y.out;}
      |      ^~~~
a.cc:20:6: note: 'bool cmp2(Pr, Pr)' previously defined here
   20 | bool cmp2(Pr x,Pr y){return x.out<y.out;}
      |      ^~~~
a.cc:118:6: error: redefinition of 'void merge(int)'
  118 | void merge(int x){
      |      ^~~~~
a.cc:21:6: note: 'void merge(int)' previously defined here
   21 | void merge(int x){
      |      ^~~~~
a.cc:141:6: error: redefinition of 'void dfs(int)'
  141 | void dfs(int x){
      |      ^~~
a.cc:44:6: note: 'void dfs(int)' previously defined here
   44 | void dfs(int x){
      |      ^~~
a.cc:158:6: error: redefinition of 'bool check(long long int)'
  158 | bool check(ll x){
      |      ^~~~~
a.cc:61:6: note: 'bool check(long long int)' previously defined here
   61 | bool check(ll x){
      |      ^~~~~
a.cc:163:6: error: redefinition of 'void solve()'
  163 | void solve(){
      |      ^~~~~
a.cc:66:6: note: 'void solve()' previously defined here
   66 | void solve(){
      |      ^~~~~
a.cc:172:5: error: redefinition of 'int read()'
  172 | int read(){
      |     ^~~~
a.cc:75:5: note: 'int read()' previously defined here
   75 | int read(){
      |     ^~~~
a.cc:179:5: error: redefinition of 'int main()'
  179 | int main(){
      |     ^~~~
a.cc:82:5: note: 'int main()' previously defined here
   82 | int main(){
      |     ^~~~