1 #include <bits/stdc++.h>
2 #define nmax 1000010
3
4 using namespace std;
5 typedef long long ll;
6 vector <int> g[nmax];
7 int n, in, a, b, cnt;
8 ll d[nmax][2]={0}; // dp[u][1] = sum dp[v][0] + zl[u] dp[u][0] = sum max(dp[v][0],dp[v][1]+x[v])
9 int zl[nmax], vis[nmax]={0};
10
11 void dfs(int u, int fa){
12 d[u][1] = zl[u];
13 for (int i=0; i<g[u].size(); i++) {
14 int v = g[u][i];
15 if(v==fa || v==0) continue;
16 dfs(v, u);
17 d[u][1] += d[v][0];
18 d[u][0] += max(d[v][0], d[v][1]);
19 }
20 }
21
22 void fr(int u, int fa){
23 cnt++;
24 vis[u] = 1;
25 for (int i=0; i<g[u].size(); i++) {
26 int v = g[u][i];
27 if(v == fa || v == 0) continue;
28 if( vis[v] ) { a=u; b=v; }
29 else fr(v, u);
30 }
31 }
32
33 inline void del(int x, int y){
34 for (int i=0; i<g[x].size(); i++) if( g[x][i] == y ) { g[x][i] = 0; break; }
35 }
36
37 inline void init(int u, int fa){
38 d[u][0] = d[u][1] = 0;
39 for (int i=0; i<g[u].size(); i++) {
40 int v = g[u][i];
41 if( v==fa || v==0 ) continue;
42 init(v, u);
43 }
44 }
45
46 int main(){
47 cin >> n;
48 for (int i=1; i<=n; i++) {
49 scanf("%d%d", &zl[i], &in);
50 g[in].push_back(i);
51 g[i].push_back(in);
52 }
53 ll ans=0, ta;
54 for (int i=1; i<=n; i++) {
55 if(vis[i]) continue;
56 cnt = 0; //这个树套环的节点个数
57 fr(i, 0);
58 del(a, b);
59 del(b, a);
60 dfs(a, 0);
61 ta = d[a][0];
62 init(i, 0);
63 dfs(b, 0);
64 ta = max(ta, d[b][0] );
65 init(i, 0);
66 ans += ta;
67 }
68 cout << ans << endl;
69 return 0;
70 }