https://www.acmicpc.net/problem/2056
import java.io.*;
import java.util.*;
public class Main {
public static BufferedReader br;
public static BufferedWriter bw;
public static int N;
public static int[] inDeg;
public static int[] time;
public static ArrayList<ArrayList<Integer>> edge = new ArrayList<>();
public static class Point implements Comparable<Point>{
int n, t;
public Point(int n, int t) {
this.n = n;
this.t = t;
}
@Override
public int compareTo(Point o) {
if (this.t > o.t) return 1;
else return -1;
}
}
public static int solve() {
PriorityQueue<Point> q = new PriorityQueue<>();
int result = 0;
for (int i = 1; i <= N; i++)
if (inDeg[i] == 0) q.add(new Point(i, time[i]));
while(!q.isEmpty()) {
Point cur = q.poll();
result = Math.max(result, cur.t);
for (int i = 0; i < edge.get(cur.n).size(); i++) {
int y = edge.get(cur.n).get(i);
if (--inDeg[y] == 0)
q.add(new Point(y, cur.t + time[y]));
}
}
return result;
}
public static void input() throws IOException {
N = Integer.parseInt(br.readLine());
//초기화
inDeg = new int[N+1];
time = new int[N+1];
for (int i = 0; i <= N; i++)
edge.add(new ArrayList<>());
//
for (int i = 1; i <= N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
time[i] = Integer.parseInt(st.nextToken());
inDeg[i] = Integer.parseInt(st.nextToken());
for (int j = 0; j < inDeg[i]; j++)
edge.get(Integer.parseInt(st.nextToken())).add(i);
}
}
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
input();
bw.write(solve() + "\n");
bw.flush();
bw.close();
bw.close();
}
}