1 #include <iostream>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cstring>
5 #define min(a, b) ((a) < (b) ? (a) : (b))
6 #define max(a, b) ((a) > (b) ? (a) : (b))
7
8 inline void swap(long long &x, long long &y)
9 {
10 long long tmp = x;x = y;y = tmp;
11 }
12
13 inline void read(long long &x)
14 {
15 x = 0;char ch = getchar(), c = ch;
16 while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();
17 while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();
18 if(c == ‘-‘)x = -x;
19 }
20
21 const long long INF = 0x3f3f3f3f;
22 const long long MAXN = 1000000 + 10;
23
24 struct Edge
25 {
26 long long u,v,next;
27 Edge(long long _u, long long _v, long long _next){u = _u;v = _v;next = _next;}
28 Edge(){}
29 }edge[MAXN << 1];
30 long long head[MAXN], cnt;
31 inline void insert(long long a, long long b)
32 {
33 edge[++cnt] = Edge(a, b, head[a]);
34 head[a] = cnt;
35 }
36
37 long long n, value[MAXN], dp[MAXN][2], huan, ban, ans, b[MAXN];
38
39 int tiaoshi;
40
41 void dfs(long long u, long long pre)
42 {
43 b[u] = 1;
44 for(register long long pos = head[u];pos;pos = edge[pos].next)
45 {
46 long long v = edge[pos].v;
47 if(pre == v)continue;
48 if(b[v])
49 {
50 huan = pos;
51 continue;
52 }
53 dfs(v, u);
54 }
55 }
56
57 void DP(long long u, long long pre)
58 {
59 dp[u][0] = 0;
60 dp[u][1] = value[u];
61 for(register long long pos = head[u];pos;pos = edge[pos].next)
62 {
63 if((pos - 1)/2 == (huan - 1)/2)continue;
64 long long v = edge[pos].v;
65 if(v == pre)continue;
66 DP(v, u);
67 dp[u][0] += max(dp[v][1], dp[v][0]);
68 dp[u][1] += dp[v][0];
69 }
70 }
71
72 int main()
73 {
74 read(n);
75 for(register long long i = 1;i <= n;++ i)
76 {
77 long long tmp1,tmp2;
78 read(tmp1), read(tmp2);
79 value[i] = tmp1;
80 insert(i, tmp2);
81 insert(tmp2, i);
82 }
83 for(register long long i = 1;i <= n;++ i)
84 {
85 if(b[i])continue;
86 dfs(i, 0);
87 long long u = edge[huan].u, v = edge[huan].v, tmp1, tmp2;
88 DP(u, 0);
89 tmp1 = dp[u][0];
90 DP(v, 0);
91 tmp2 = dp[v][0];
92 ans += max(tmp1, tmp2);
93 }
94 printf("%lld", ans);
95 return 0;
96 }