NOI '22 Multi-Provincial Team Selection P6 - MIS

View as PDF

Submit solution

Points: 40 (partial)
Time limit: 1.0s
Memory limit: 1G

Problem type

Tommy has a binary tree rooted at 1 on n nodes. Each node has an integer weight w_i, and the father of node i = 2, 3, \dots, n is f_i. Tommy needs to disassemble this binary tree by sequentially removing edges. When an edge (u, v) is removed, the following happen, simultaneously:

  • w_u+w_v units of cost are incurred;
  • w_u and w_v are swapped.

Find the minimum units of cost necessary to disassemble the binary tree.

Constraints

2 \le n \le 5000

1 \le w_i \le 10^9

Test n \le
1 - 3 10
4 - 7 100
8 - 11 500
12 - 16 1\,000
17 - 25 5\,000

Input Specification

The first line contains a positive integer n.

The second line contains n positive integers w_1, w_2, \dots, w_n.

The third line contains n-1 positive integers f_2, f_3, \dots, f_n, describing the binary tree.

Output Specification

Output the answer.

Sample Input 1

3
2 1 3
1 1

Sample Output 1

7

Attachments


Comments

There are no comments at the moment.