T-1
待填
T-2
这题妙 啊。。。
```cpp
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
#define N 1000000
#define M 2000000
#define inf 2e9
int n,a[N+5];
int head[N+5],nex[M+5],ver[M+5],tot;
inline void add(int x,int y){
ver[++tot]=y;nex[tot]=head[x];head[x]=tot;
}
int f1[N+5],ffr1[N+5],f2[N+5],ffr2[N+5];
int g1[N+5],gfr1[N+5],g2[N+5],gfr2[N+5];
inline void init(int x){
f1[x]=0;ffr1[x]=x;f2[x]=inf,ffr2[x]=0;
g1[x]=a[x];gfr1[x]=x;g2[x]=inf,gfr2[x]=0;}
inline void update(int x,int y,int a,int b){
if(a<f1[x]){ ffr2[x]=ffr1[x];f2[x]=f1[x];f1[x]=a; } else if(a<f2[x]) ffr2[x]=y,f2[x]=a;
if(b<g1[x]){ gfr2[x]=gfr1[x];g2[x]=g1[x];g1[x]=b; } else if(b<g2[x]) gfr2[x]=y,g2[x]=b;
}
int ans;
inline void dfs_1(int x,int fa){
init(x);
for(int i=head[x];i;i=nex[i]){
int y=ver[i];if(y==fa) continue;
dfs_1(y,x);
ans=max(ans,-max(f1[x],g1[y]+a[x]));
ans=max(ans,-max(g1[x],f1[y]-a[y]));
update(x,y,f1[y]-a[y],g1[y]+a[x]);
}
}
inline void dfs_2(int x,int fa,int f,int g){
ans=max(ans,-max(f,g1[x]));ans=max(ans,-max(g,f1[x]));
for(int i=head[x];i;i=nex[i]){
int y=ver[i];if(y==fa) continue;
int ff=f-a[x],gg=g+a[x];
if(y!=ffr1[x]) ff=min(ff,f1[x]-a[x]);else if(y!=ffr2[x]) ff=min(ff,f2[x]-a[x]);
if(y!=gfr1[x]) gg=min(gg,g1[x]+a[x]);else if(y!=gfr2[x]) gg=min(gg,g2[x]-a[x]);
dfs_2(y,x,ff,gg);
}
}
int main()
{
while(~scanf("%d",&n)){
tot=0;ans=0;for(int i=1;i<=n;++i){head[i]=0;int f=read();
if(f!=0) add(f,i),add(i,f);scanf("%d",&a[i]);}
dfs_1(1,1);dfs_2(1,1,0,a[1]);printf("%d\n",ans);
}
}
```cpp
T-3
待填