#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<queue>
#define int long long
using namespace std;
typedef pair<int,int> PII;
const int N=1e5;
int h[N],ne[N],e[N],idx,d[N],w[N],n;
void add(int a,int b)
{
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void bfs()
{
// cout<<1<<endl;
int sum=0,max1=-1,kk=0;
queue<int>q;
for(int i=1;i<=n;i++)
{
if(d[i]==0)
{
q.push(i);
// cout<<w[i]<<'A'<<i<<endl;
max1=max(max1,w[i]);
kk=1;
}
}
if(kk==1)
{
sum+=max1;
}
while(q.size())
{
max1=-1;
int t=q.front();
q.pop();
for(int i=h[t];i!=-1;i=ne[i])
{
int j=e[i];
d[j]--;
if(d[j]==0)
{
q.push(j);
max1=max(max1,w[j]);
kk=1;
}
}
if(kk==1)
{
sum+=max1;
kk=0;
}
}
cout<<sum;
}
signed main()
{
int a,x;
memset(h,-1,sizeof(h));
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a;
cin>>w[a];
while(1)
{
cin>>x;
if(x==0)break;
add(x,a);
d[a]++;
}
}
bfs();
return 0;
}
有大佬能看看我的代码嘛,只过了样例。。。。。。