#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 apres-ski locations (hereafter: apres) connected by slopes such that they form a connected tree rooted at the apres labeled . 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 , it is known from which apres () one arrives at it, i.e., its parent in the tree is known. This uniquely determines all slopes (slope leads to the apres labeled ).
During the day, Mia skied each slope exactly once and for each slope remembered its fun factor and speed .
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 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:
- be the fun factor of the first slope in the run
- be the fun factor of the last slope in the run
- 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 , the formula remains unchanged, and holds.
Your task is to determine the maximum hecticness of a run that Mia can ski.
Input Format
The first line contains natural numbers (), as described in the problem statement.
The second line contains integers, where the -th number represents from which apres one can reach the apres labeled ().
The third line contains integers, where the -th number represents the fun factor of the -th slope ().
The fourth line contains integers, where the -th number represents the speed of the -th slope ().
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 , 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 and . The maximum hecticness is .
Scoring
| Subtask | Points | Constraints |
|---|---|---|
| 1 | 14 | |
| 2 | 23 | For each it will hold that and . |
| 3 | 35 | |
| 4 | 38 | No additional constraints. |
京公网安备 11011102002149号