Do It Yourself?
Time limit10sMemory limit1024 MB
Each task can be done by its owner or any of the owner's bosses. Find the minimum sum of f_i times (tasks done by i) squared over all employees.
- Level
Hard8 of 10
- Topics
- Tree, Dynamic programming
- Solved
- No attempts yet
Problem
You are the head of a group of employees in a company, including yourself. Each employee has an employee ID, an integer from 1 to , and your ID is 1. Employees are often called by their IDs for short: #, #, and so on. Every employee other than you has exactly one immediate boss, whose ID is smaller than theirs. A boss of an employee is defined recursively as follows.
- If employee # is the immediate boss of 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. A task can be done by the employee who holds it or by any one of that employee's bosses. An employee can do any number of tasks, but doing many tasks is tiring. Employee # has a fatigability constant . If # does tasks in total, the fatigue level of # becomes .
Minimize the sum of fatigue levels of all employees.
Input
The first line contains the number of employees ().
The second line contains integers , where is the immediate boss of employee # and .
The third line contains integers , where is the fatigability constant of employee # and .
Output
Print the minimum possible sum of fatigue levels of all employees.
Hint

Figure J.1 shows the situations and solutions of the three samples.
In the first sample, the only optimal way is for every employee to do their own task. You should say 'Do it yourself!' to everyone.
In the second sample, the only optimal way is for employee # to do all the tasks. You should say 'Leave it to me!' to everyone.
In the third sample, one optimal way is as follows.
- # does the tasks of # and #. The fatigue level of # is .
- # does the task of #. The fatigue level of # is .
- # does their own task. The fatigue level of # is .
- # does nothing. The fatigue level of # is .
The sum of the fatigue levels is . Another optimal way is for #, #, and # to do their own tasks, and for # to also do the task of #.