#P14823. [ICPC 2023 Yokohama R] Do It Yourself?
[ICPC 2023 Yokohama R] Do It Yourself?
题目描述
You are the head of a group of employees including you in a company. Each employee has an employee ID, which is an integer 1 through , where your ID is 1. Employees are often called by their IDs for short: , , and so on. Every employee other than you has a unique immediate boss, whose ID is smaller than his/hers. A boss of an employee is transitively defined as follows.
- If an employee is the immediate boss of an employee , then is a boss of .
- If is a boss of and is a boss of , then is a boss of .
Every employee including you is initially assigned exactly one task. That task can be done by him/herself or by any one of his/her bosses. Each employee can do an arbitrary number of tasks, but doing many tasks makes the employee too tired. Formally, each employee has an individual fatigability constant , and if does tasks in total, then the fatigue level of will become .
Your mission is to minimize the sum of fatigue levels of all the employees.
输入格式
The input consists of a single test case in the following format.
$$\begin{aligned} & n \\ & b_2 \ b_3 \ \cdots \ b_n \\ & f_1 \ f_2 \ \cdots \ f_n \\ \end{aligned}$$The integer in the first line is the number of employees, where . The second line has integers (), each of which represents the immediate boss of the employee , where . The third line has integers (), each of which is the fatigability constant of the employee , where .
输出格式
Output the minimum possible sum of fatigue levels of all the employees.
4
1 1 2
1 1 1 1
4
4
1 1 2
1 10 10 10
16
4
1 1 2
1 2 4 8
10
提示
:::align{center}

Figure J.1. Illustration of the three samples (from left to right) :::
The situations and solutions of the three samples are illustrated in Figure J.1.
For Sample Input 1, the unique optimal way is that each employee does his/her task by him/herself. That is, you should just say "Do it yourself!" to everyone.
For Sample Input 2, the unique optimal way is that the employee does all the tasks. That is, you should just say "Leave it to me!" to everyone.
For Sample Input 3, one of the optimal ways is the following.
- does the tasks of and , and then the fatigue level of will be .
- does the task of , and then the fatigue level of will be .
- does the initially assigned task, and then the fatigue level of will be .
- does nothing, and then the fatigue level of will be .
Thus, the sum of the fatigue levels is . There is another optimal way, in which the employees , , and do their initially assigned tasks by themselves and does the task of in addition.