1 #include<iostream>
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 using namespace std;
6
7 #define inf (1ll<<60)
8 #define maxn 1000010
9 int n,cnt = 1,cir[maxn],w[maxn],fa[maxn],side[maxn];
10 int dfn[maxn],low[maxn],toit[maxn*2],next[maxn*2];
11 long long f[maxn][2],g[maxn][2],ans;
12
13 inline int read()
14 {
15 int x=0,f=1;char ch=getchar();
16 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
17 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
18 return x*f;
19 }
20
21 inline void add(int a,int b) { next[++cnt] = side[a]; toit[cnt] = b; side[a] = cnt; }
22
23 inline void ins(int a,int b) { add(a,b); add(b,a); }
24
25 inline void dp(int root,int last)
26 {
27 int nn = 0;
28 while (last != root) cir[++nn] = last,last = fa[last];
29 cir[++nn] = root;
30 for (int i = 1;i <= nn;++i) g[cir[i]][0] = f[cir[i]][0],g[cir[i]][1] = f[cir[i]][1];
31 g[root][1] = inf;
32 for (int i = nn-1;i;--i)
33 {
34 g[cir[i]][0] += min(g[cir[i+1]][0],g[cir[i+1]][1]);
35 g[cir[i]][1] += g[cir[i+1]][0];
36 }
37 f[root][0] = min(g[cir[1]][0],g[cir[1]][1]);
38 for (int i = 1;i <= nn;++i) g[cir[i]][0] = f[cir[i]][0],g[cir[i]][1] = f[cir[i]][1];
39 g[root][0] = inf;
40 for (int i = nn-1;i;--i)
41 {
42 g[cir[i]][0] += min(g[cir[i+1]][0],g[cir[i+1]][1]);
43 g[cir[i]][1] += g[cir[i+1]][0];
44 }
45 f[root][1] = g[cir[1]][0];
46 }
47
48 inline void dfs(int now)
49 {
50 dfn[now] = low[now] = ++cnt;
51 f[now][0] = w[now];
52 for (int i = side[now];i;i = next[i])
53 if (toit[i] != fa[now])
54 {
55 if (fa[toit[i]] == now) continue;
56 if (!dfn[toit[i]]) fa[toit[i]] = now,dfs(toit[i]);
57 low[now] = min(low[now],low[toit[i]]);
58 if (low[toit[i]] > dfn[now])
59 {
60 f[now][0] += min(f[toit[i]][0],f[toit[i]][1]);
61 f[now][1] += f[toit[i]][0];
62 }
63 }
64 for (int i = side[now];i;i = next[i])
65 if (toit[i] != fa[now] && dfn[toit[i]] > dfn[now] && fa[toit[i]] != now)
66 dp(now,toit[i]);
67 }
68
69 int main()
70 {
71 freopen("1040.in","r",stdin);
72 freopen("1040.out","w",stdout);
73 scanf("%d",&n);
74 for (int i = 1;i <= n;++i) w[i] = read(),ins(read(),i),ans += (long long)w[i];
75 for (int i = 1;i <= n;++i)
76 if (!dfn[i]) cnt = 0,dfs(i),ans -= min(f[i][0],f[i][1]);
77 printf("%lld",ans);
78 fclose(stdin); fclose(stdout);
79 return 0;
80 }