var
i,j,n,final:longint;
c:char;
boss,name:array[1..100]of string;
f,son:array[1..100,0..100]of longint;
a:array[1..100]of longint;
function max(a,b:longint):longint;
begin
if a>b then
exit(a);
exit(b);
end;
procedure dp(x:longint);
var i:longint;
begin
f[x,1]:=a[x];
for i:=1 to son[x,0] do
begin
dp(son[x,i]);
f[x,1]:=f[x,1]+f[son[x,i],0];
f[x,0]:=f[x,0]+max(f[son[x,i],1],f[son[x,i],0]);
end;
end;
begin
readln(n);
for i:=1 to n do
begin
repeat
read(c);
if c<>' ' then
name[i]:=name[i]+c;
until c=' ';
read(a[i]);
read(c);
readln(boss[i]);
end;
for i:=1 to n do
if boss[i]='NOBODY' then
final:=i
else
for j:=1 to n do
if name[j]=boss[i] then
begin
son[j,0]:=son[j,0]+1;
son[j,son[j,0]]:=i;
end;
dp(final);
writeln(max(f[final,1],f[final,0]));
end.
i,j,n,final:longint;
c:char;
boss,name:array[1..100]of string;
f,son:array[1..100,0..100]of longint;
a:array[1..100]of longint;
function max(a,b:longint):longint;
begin
if a>b then
exit(a);
exit(b);
end;
procedure dp(x:longint);
var i:longint;
begin
f[x,1]:=a[x];
for i:=1 to son[x,0] do
begin
dp(son[x,i]);
f[x,1]:=f[x,1]+f[son[x,i],0];
f[x,0]:=f[x,0]+max(f[son[x,i],1],f[son[x,i],0]);
end;
end;
begin
readln(n);
for i:=1 to n do
begin
repeat
read(c);
if c<>' ' then
name[i]:=name[i]+c;
until c=' ';
read(a[i]);
read(c);
readln(boss[i]);
end;
for i:=1 to n do
if boss[i]='NOBODY' then
final:=i
else
for j:=1 to n do
if name[j]=boss[i] then
begin
son[j,0]:=son[j,0]+1;
son[j,son[j,0]]:=i;
end;
dp(final);
writeln(max(f[final,1],f[final,0]));
end.