#P15891. [COCI 2025/2026 #6] Skijanje

[COCI 2025/2026 #6] Skijanje

Description

Skier Mia spent a day at an unusual ski resort. The resort consists of nn apres-ski locations (hereafter: apres) connected by slopes such that they form a connected tree rooted at the apres labeled 11. Each slope is directed from the apres with a smaller number to the apres with a larger number.

The tree is defined as follows: for each apres i>1i > 1, it is known from which apres pip_i (pi<ip_i < i) one arrives at it, i.e., its parent in the tree is known. This uniquely determines all slopes (slope ii leads to the apres labeled ii).

During the day, Mia skied each slope exactly once and for each slope remembered its fun factor ziz_i and speed bib_i.

At the end of the day, Mia wants to ski down once more. Since Mia is very tired from skiing all day, she will choose a run consisting of at most kk consecutive slopes. The run must follow the direction of the slopes (from apres with smaller numbers to those with larger numbers). After finishing the run, a helicopter will pick her up and take her home.

For any chosen run, we define its hecticness as follows. Let:

  • zfirstz_{\text{first}} be the fun factor of the first slope in the run
  • zlastz_{\text{last}} be the fun factor of the last slope in the run
  • bib_i be the speeds of all slopes in that run

Then the hecticness of the run is:

$$z_{\text{last}} \cdot \left(z_{\text{last}} + \sum b_i\right) + z_{\text{first}}^2.$$

In the case k=1k = 1, the formula remains unchanged, and first=last\text{first} = \text{last} holds.

Your task is to determine the maximum hecticness of a run that Mia can ski.

Input Format

The first line contains natural numbers n,kn, k (1kn31051 \le k \le n \le 3 \cdot 10^5), as described in the problem statement.

The second line contains n1n - 1 integers, where the ii-th number represents from which apres one can reach the apres labeled i+1i + 1 (1pii1 \le p_i \le i).

The third line contains n1n - 1 integers, where the ii-th number represents the fun factor of the (i+1)(i + 1)-th slope (1zi1051 \le z_i \le 10^5).

The fourth line contains n1n - 1 integers, where the ii-th number represents the speed of the (i+1)(i + 1)-th slope (105bi105-10^5 \le b_i \le 10^5).

Output Format

In the first and only line, output a single number - the maximum hecticness of a run that Mia can ski.

5 1
1 2 2 1
5 4 8 7
6 3 9 3
200
9 2
1 2 1 1 4 3 6 5
1 3 7 8 4 1 8 2
1 -7 -1 -6 3 8 -1 6
120

Hint

Clarification of the first example: Since k=1k = 1, it follows that the maximum hecticness of a run is equal to the maximum hecticness among the individual slopes. The slopes have hecticness values of 80,44,200,80, 44, 200, and 119119. The maximum hecticness is 200200.

Scoring

Subtask Points Constraints
1 14 n1000n \le 1000
2 23 For each 1i<n1 \le i < n it will hold that zi=1z_i = 1 and bi>1b_i > 1.
3 35 n50000n \le 50000
4 38 No additional constraints.