#P11097. [ROI 2022] 采购优化 (Day 1)
[ROI 2022] 采购优化 (Day 1)
Background
Translated from ROI 2022 D1T2.
Problem Description
The network of ski equipment rental stations is a tree with root node , consisting of nodes numbered from to . Each node has a rental station. The station at node needs to purchase equipment kits at a price of rubles per kit.
Let be the total number of ski equipment kits to be purchased among all rental stations in the subtree of node . According to market research, this value must satisfy .
You need to determine how many kits each rental station should buy before the season starts, so that for any subtree in the network, the total number of kits is within the range specified by the marketing staff, and the total cost of all purchased kits is minimized, or determine that it is impossible to satisfy all marketing requirements.
Input Format
Each test point contains multiple testdata. The first line contains an integer , the number of testdata. Then follow testdata.
For each testdata, the first line contains an integer (), the number of nodes in the tree.
The next line contains integers (), meaning that the parent of node is node .
The next line contains integers (), where is the price of one equipment kit at station .
The next lines each contain two integers and (), representing the constraint on the total number of equipment kits within the subtree of node .
It is guaranteed that the sum of over all testdata does not exceed .
Output Format
For each testdata, output the answer in the following format.
If it is impossible to satisfy all marketing requirements, output -1.
Otherwise, on the first line output the minimum number of rubles needed to buy ski equipment for the whole network. On the second line, output integers , where is the number of equipment kits that station needs to buy. If there are multiple ways to satisfy all marketing requirements with the minimum cost, you may output any one of them.
2
3
1 1
3 1 2
5 7
1 2
2 4
2
1
5 5
0 1
2 2
8
0 2 3
-1
Hint
Explanation of the first test case in the sample input:

。
Constraints:
Let be the number of nodes in the subtree of node .
| Subtask | Score | Special Property | |
|---|---|---|---|
| , | |||
The full constraints are given in the Input Format section.
Translated by ChatGPT 5