1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #define ll long long
5 using namespace std;
6 const int N=1000100;
7 int q[N],du[N],v[N],c[N],deep[N],fa[N],head[N],sz[N];
8 int root,other,cnt,n,tot;
9 ll ans,end,tr[N][2];
10 bool vis[N],pd[N],viss[N];
11 struct ee{int to,next;}e[N*2];
12 void ins(int u,int v){
13 e[++cnt].next=head[u],e[cnt].to=v,head[u]=cnt;du[u]++;
14 e[++cnt].next=head[v],e[cnt].to=u,head[v]=cnt;du[v]++;
15 }
16 void topsort(){
17 int l,r;l=r=0;
18 for (int i=1;i<=n;i++) if(du[i]==1) q[++r]=i,vis[i]=1;
19 while(l<r){
20 int now=q[++l];
21 for(int i=head[now];i;i=e[i].next){
22 int v=e[i].to;
23 if(du[v]>1){
24 du[v]--;
25 if(du[v]==1) q[++r]=v,vis[v]=1;
26 }
27 }
28 }
29 }
30
31 void dfs(int x,int t){
32 vis[x]=t;c[x]=t;
33 for (int i=head[x];i;i=e[i].next){
34 int v=e[i].to;
35 if(!vis[v]) dfs(v,t);
36 }
37 }
38
39
40 ll treedp(int x,int bz,int t){
41 if(tr[x][bz]!=-1) return tr[x][bz];
42 ll mid=0;bool flag=0;viss[x]=1;
43 for(int i=head[x];i;i=e[i].next){
44 int vv=e[i].to;
45 if(deep[vv]==deep[x]+1) {
46 flag=1;
47 if(bz==0)mid+=max(treedp(vv,bz^1,t),treedp(vv,bz,t));
48 else mid=mid+treedp(vv,bz^1,t);
49 }
50 }
51 if(bz==1) mid+=v[x];
52 if(!flag)tr[x][bz]=bz*v[x];else tr[x][bz]=mid;
53 return tr[x][bz];
54 }
55
56 void solve(int x,int bz,int t){
57 vis[x]=1;
58 for (int i=head[x];i;i=e[i].next){
59 int v=e[i].to;
60 if(x==root&&v==other) continue;
61 if(!vis[v]) deep[v]=deep[x]+1,solve(v,bz,t);
62 }
63 }
64
65 int main(){
66 scanf("%d",&n);int x;
67 for(int i=1;i<=n;i++){
68 scanf("%d%d",&v[i],&x);
69 sz[i]=x;
70
71 if (sz[x]!=i) ins(i,x);
72 fa[x]=i;
73 }
74 topsort();
75 for(int i=1;i<=n;i++)if(!vis[i]) dfs(i,++tot);
76 memset(tr,-1,sizeof(tr));
77 for (int i=1;i<=n;i++){
78 if(c[i]&&!pd[c[i]]) {
79 other=0;root=i;deep[root]=0;pd[c[i]]=1;
80 for(int j=head[i];j;j=e[j].next){
81 if(c[e[j].to]==c[i]) {
82 other=e[j].to;
83 break;
84 }
85 }ans=0;
86 memset(vis,0,sizeof(vis));vis[root]=1;
87 solve(root,0,c[i]);
88 ans=max(ans,treedp(root,0,c[i]));
89 root=other;deep[root]=0;
90 other=i;
91 memset(vis,0,sizeof(vis));vis[root]=1;
92 memset(tr,-1,sizeof(tr));
93 solve(root,0,c[i]);
94 ans=max(ans,treedp(root,0,c[i]));
95 end+=ans;
96 }
97 }
98 other=-1;
99 memset(vis,0,sizeof(vis));
100 for(int i=1;i<=n;i++) if(!viss[i]) {
101 root=i;solve(root,0,c[i]);
102 end+=max(treedp(root,0,0),treedp(root,1,0));}
103 printf("%lld",end);
104 }