PivotOJ

Trade Routes

시간 제한: 2000ms메모리 제한: 1024MB출처: ICPC Rocky Mountain Regional 2021BOJ 24763

문제

All roads lead to Rome. In this case, we mean every road in the road network in the Roman Empire can be travelled in only one direction. Each city that is not Rome has exactly one road that leaves it and by following these roads, you will always end up in Rome.

Each city, including Rome itself, may create a trade route to Rome which brings Rome some value. These values are all distinct. Each city does not want to be too congested with traders so they constrain the number of trade routes that the city can be a part of.

We say that a city is part of a trade route if it is contained in the unique path from the city that created the trade route and Rome. A city is a part of its own trade route.

Given the city constraints, determine the maximum value that can be brought to Rome by choosing a subset of cities to create trade routes.

입력

The first line of input contains a single integer NN (2N3000002 \leq N \leq 300\,000), which is the number of cities. The cities are numbered 11 to NN. Rome is city number 11.

The next line contains N1N-1 integers p2,p3,,pNp_2, p_3, \ldots, p_{N} (1pi<i1 \leq p_i < i), where pip_i is the city you reach by following the single road out of city ii.

The next line contains NN integers b1,b2,,bNb_1, b_2, \ldots, b_{N} (0biN0 \leq b_i \leq N), where bib_i is the maximum number of trade routes that city ii can be a part of.

The next line contains NN distinct integers v1,v2,,vNv_1, v_2, \ldots, v_N (0vi1090 \leq v_i \leq 10^9), which is the value brought to Rome if city ii creates a trade route.

출력

First display the maximum possible value Rome can receive from any valid subset of chosen trade routes. Next display TT, the number of trade routes created, then display the TT cities, in increasing order, that were selected.

If there are multiple possible solutions, display any of them.

예제

예제 1

입력
7
1 1 2 2 3 3
2 1 2 1 1 1 1
6 5 3 8 4 7 1
출력
15
2 4 6

예제 2

입력
9
1 1 2 3 3 4 4 4
4 4 2 4 1 0 1 1 1
100 30 10 0 50 200 12 15 13
출력
195
4 1 2 5 8
코드를 제출하려면 로그인하세요.