#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 11, consisting of nn nodes numbered from 11 to nn. Each node has a rental station. The station at node ii needs to purchase equipment kits at a price of cic_i rubles per kit.

Let aia_i be the total number of ski equipment kits to be purchased among all rental stations in the subtree of node ii. According to market research, this value must satisfy liairil_i \le a_i \le r_i.

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 tt, the number of testdata. Then follow tt testdata.

For each testdata, the first line contains an integer nn (1n1051 \le n \le 10^5), the number of nodes in the tree.

The next line contains n1n - 1 integers p2,p3,,pnp_2, p_3, \dots, p_n (1pi<i1 \le p_i < i), meaning that the parent of node ii is node pip_i.

The next line contains nn integers c1,,cnc_1, \dots, c_n (1ci1091 \le c_i \le 10^9), where cic_i is the price of one equipment kit at station ii.

The next nn lines each contain two integers lil_i and rir_i (0liri1090 \le l_i \le r_i \le 10^9), representing the constraint on the total number of equipment kits within the subtree of node ii.

It is guaranteed that the sum of nn over all testdata does not exceed 10510^5.

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 nn integers bib_i, where bib_i is the number of equipment kits that station ii 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:

c1b1+c2b2+c3b3=0+2+6=8c_1 b_1 + c_2 b_2 + c_3 b_3 = 0 + 2 + 6 = 8

Constraints:

Let sis_i be the number of nodes in the subtree of node ii.

Subtask Score n\sum n \le Special Property
11 2424 500500 ri1000r_i \le 1000
22 2222 50005000 risir_i \le s_i
33 1818 10510^5 li100×sil_i \le 100 \times s_iri=109r_i = 10^9
44 2121 50005000
55 1515 10510^5

The full constraints are given in the Input Format section.

Translated by ChatGPT 5