Under construction

Still being built. Things may change

Trade Routes

Time limit2sMemory limit1024 MB

Summary
Pick trade routes in a rooted tree of cities so each city lies on at most its limit of chosen paths, maximizing the total value brought to Rome.
Level

Medium7 of 10

Topics
Greedy, Tree, Segment tree
Solved
No attempts yet

Problem

All roads lead to Rome. Here, every road in the road network of the Roman Empire can be traveled in only one direction. Each city other than Rome has exactly one road leaving it, and by following these roads you always end up in Rome.

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

A city is part of a trade route if it lies on the unique path from the city that created the route to Rome. A city is part of its own trade route.

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

Input

The first line contains a single integer NN (2≤N≤3000002 \leq N \leq 300000), the number of cities. The cities are numbered 1 to NN, and Rome is city 1.

The next line contains N−1N-1 integers p2,p3,…,pNp_2, p_3, \ldots, p_N (1≤pi<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 (0≤bi≤N0 \leq b_i \leq N), where bib_i is the maximum number of trade routes that city ii can be part of.

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

Output

First print the maximum value Rome can receive from any valid subset of trade routes. Next print TT, the number of trade routes created, then print the TT selected cities in increasing order. If several solutions exist, print any of them.

Examples2

  1. Example 1

    Input
    7
    1 1 2 2 3 3
    2 1 2 1 1 1 1
    6 5 3 8 4 7 1
    
    Expected output
    15
    2 4 6
    
  2. Example 2

    Input
    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
    
    Expected output
    195
    4 1 2 5 8