#include <cstdio>
#include <algorithm>
using namespace std;
inline int readint(){
int n = 0;
char ch = getchar();
while(ch < ‘0‘ || ch > ‘9‘) ch = getchar();
while(ch <= ‘9‘ && ch >= ‘0‘){
n = (n << 1) + (n << 3) + ch - ‘0‘;
ch = getchar();
}
return n;
}
const int maxn = 1000000 + 10;
struct Edge{
int to, next;
bool ok;
Edge(){}
Edge(int _t, int _n): to(_t), next(_n), ok(true){}
}e[maxn << 1];
int fir[maxn] = {0}, cnt = 1;
inline void ins(int u, int v){
e[++cnt] = Edge(v, fir[u]); fir[u] = cnt;
e[++cnt] = Edge(u, fir[v]); fir[v] = cnt;
}
int val[maxn], vis[maxn] = {0};
int rt1, rt2;
long long f[maxn][2];
void dp(int u, int T){
vis[u] = T;
f[u][0] = 0;
f[u][1] = val[u];
for(int v, i = fir[u]; i; i = e[i].next){
v = e[i].to;
if(!e[i].ok || vis[v] == T) continue;
dp(v, T);
f[u][0] += max(f[v][0], f[v][1]);
f[u][1] += f[v][0];
}
}
void dfs(int u, int f){
vis[u] = 1;
for(int v, i = fir[u]; i; i = e[i].next){
v = e[i].to;
if(i ^ f){
if(vis[v]){
rt1 = u;
rt2 = v;
e[i].ok = false;
e[i ^ 1].ok = false;
}
else dfs(v, i ^ 1);
}
}
}
int main(){
int n = readint();
for(int i = 1; i <= n; i++){
val[i] = readint();
ins(i, readint());
}
long long ans = 0, t;
for(int i = 1; i <= n; i++)
if(!vis[i]){
rt1 = rt2 = 0;
dfs(i, 0);
if(!rt1){
dp(i, 2);
ans += max(f[i][0], f[i][1]);
}
else{
dp(rt1, 2);
t = f[rt1][0];
dp(rt2, 3);
t = max(t, f[rt2][0]);
ans += t;
}
}
printf("%lld\n", ans);
return 0;
}