Supervisors

Time limit: 0.4s Memory limit: 256MB Input: Output:

Dario's company is facing a particularly hard task. To tackle it, Dario plans to select a task force of KK people in the best way possible. The choice will be influenced by the company's highly hierarchical tree-like structure.

There are NN employees, numbered from 00 to N1N-1. For each 1iN11 \le i \le N-1, employee ii has a manager Pi\text{manager} \ P_i, where Pi<iP_i < i. Dario, who is employee number 00, has no direct manager. We say that employee ii is a supervisor\text{supervisor} of employee jj if either i=ji = j or ii is the manager of a supervisor of jj. Note that this is a recursive definition. The coordinator\text{coordinator} of a pair of employees ii and jj is their lowest common ancestor in the company's hierarchy tree, i.e. the employee with the highest identifying number among those that supervise both ii and jj. This is well defined because Dario supervises everyone in the company.

Task

Moreover, each employee has a skill level SiS_i. The cumulative skill level\text{cumulative skill level} of a subset of employees T0,T1,,Tk1T_0, T_1, \ldots, T_{k - 1} is defined as the sum of the skill level of the coordinator of TiT_i and TjT_j over all pairs 0i<j<k0 \le i < j < k.

What is the maximum cumulative skill level that a task force of KK people can obtain?

Input data

The input file consists of:

  • a line containing integers N,KN, K.
  • a line containing the NN integers S0,,SN1S_{0}, \, \ldots, \, S_{N-1}.
  • a line containing the N1\mathtt{N - 1} integers P1,,PN1P_{1}, \, \ldots, \, P_{N - 1}.

Output data

The output file must contain a single line consisting of 64-bit integer ans\mathtt{ans}.

Constraints and clarifications

  • 1N2 0001 \le N \le 2 \ 000.
  • 2KN2 \le K \le N.
  • 1Si1 000 000 0001 \le S_i \le 1 \ 000 \ 000 \ 000 for each i=0N1i=0\ldots N-1.
  • 0Pi<i0 \le P_i < i for each i=1N1i=1\ldots N-1.
# Score Constraints
1 0 Examples
2 18 N20N \le 20, Si1 000S_i \le 1 \ 000 for i=0,1,,N1i = 0,1, \ldots, N-1
3 27 N100N \le 100
4 24 Pi=i1P_i = i-1 for each i=1,2,,N1i=1,2,\ldots,N-1
5 31 No additional restrictions

Example 1

stdin

5 3
4 2 5 1 3
0 0 1 1

stdout

12

Explanation

In the first sample case, the maximum cumulative skill level obtainable with a task force of 33 people is 1212.

One possible way to achieve that is to choose employees 00, 22 and 33. By doing this:

  • Employee 00 and 22's coordinator is employee 00, with a skill level of 44.
  • Employee 00 and 33's coordinator is employee 00, with a skill level of 44.
  • Employee 22 and 33's coordinator is employee 00, with a skill level of 44.

Example 2

stdin

10 5
5 9 4 4 8 9 9 8 7 3
0 0 1 3 4 0 4 0 7

stdout

84

Log in or sign up to be able to send submissions!