#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=100000+5;
const ll inf=0x3f3f3f3f3f3f;
struct sdf{
int to,next;
}eg[N];
int head[N],ne;
int n,in[N];
ll w[N],ans=0,mn=inf;
queue<int>q;
void add(int x,int y){
eg[++ne]={y,head[x]};
head[x]=ne;return ;
}
bool vis[N]={0};
void dfs(int pt){
vis[pt]=1;mn=min(mn,w[pt]);
for (int i=head[pt];i!=0;i=eg[i].next){
int v=eg[i].to;
if (!in[v]||vis[v])continue;
dfs(v);
}
return ;
}
int main(){
freopen ("prob1_silver_22open.in","r",stdin);
freopen ("prob1_silver_22open.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++){
int x;scanf("%d%lld",&x,&w[i]);
add(i,x);in[x]++;ans+=w[i];
}
for (int i=1;i<=n;i++){
if (!in[i])q.push(i);
}
while(!q.empty()){
int pt=q.front();q.pop();
for (int i=head[pt];i!=0;i=eg[i].next){
int v=eg[i].to;
if (!in[v])continue;
in[v]--;
if (!in[v])q.push(v);
}
}
for (int i=1;i<=n;i++){
if (in[i]&&!vis[i])mn=inf,dfs(i),ans-=mn;
}
printf("%lld\n",ans);
return 0;
}