Trade Routes
Time limit2sMemory limit1024 MB
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 (), the number of cities. The cities are numbered 1 to , and Rome is city 1.
The next line contains integers (), where is the city you reach by following the single road out of city .
The next line contains integers (), where is the maximum number of trade routes that city can be part of.
The next line contains distinct integers (), where is the value brought to Rome if city creates a trade route.
Output
First print the maximum value Rome can receive from any valid subset of trade routes. Next print , the number of trade routes created, then print the selected cities in increasing order. If several solutions exist, print any of them.