luogu#P16366. [OOI 2026] Anxiety Before the Olympiad

[OOI 2026] Anxiety Before the Olympiad

Problem Description

Before entering a closed olympiad in informatics, there are nn participants waiting for entrance, numbered from 11 to nn. It is known that every minute, one participant will be invited into the competition hall in the order of their numbers. In one minute, the first participant will enter the hall, in two minutes the second participant will enter, and so on. Thus, the ii-th participant will enter the hall ii minutes after the start of the entrance process.

Each participant has a certain level of anxiety before the olympiad. The anxiety level of each participant is given by some integer (which may be negative). Before the start of the entrance process, the anxiety level of participant ii is equal to aia_i. Every minute, the anxiety level of the participant will change by bib_i. Therefore, after xx minutes from the start of the entrance process, the anxiety level of participant ii will be equal to ai+xbia_i + x \cdot b_i.

Alexander is an experienced psychologist who decided to calm the participants waiting to enter the olympiad. To do this, Alexander talks to the participants and calms them down. He can talk to each participant no more than once. After talking to Alexander, the anxiety level of the participant becomes 00 and does not change afterward. The effectiveness\textit{effectiveness} of Alexander's work with participant ii is considered to be the level of anxiety of the participant at the moment of communication with Alexander. Thus, if Alexander talks to participant ii after tit_i minutes from the start of the entrance process into the olympiad, the effectiveness\textit{effectiveness} will be equal to ai+tibia_i + t_i \cdot b_i. Note that if the anxiety level of the participant was negative, then the effectiveness\textit{effectiveness} will also be negative.

Alexander will work with the participants in the order of their numbers. However, Alexander does not have to talk to all participants, meaning it is possible that he will not communicate with the last participants in line. Note that Alexander will talk to each participant no more than once and this must happen before the participant enters the competition hall. At the same time, Alexander can communicate with several participants at once every minute. More formally, Alexander's work process can be described as follows:

  • In total, Alexander will talk to the first kk participants in line, where kk is chosen by him.
  • For each of the first kk participants, we will fix a non-negative integer tit_i --- the time of communication with Alexander. Note that tit_i can be equal to zero, which means that Alexander talked to participant ii before the first participant was allowed into the competition hall.
  • For each ii from 11 to kk, ti<it_i < i, as Alexander must talk to the participants before they enter the competition hall.
  • For each ii from 11 to k1k - 1, titi+1t_i \le t_{i + 1}, as Alexander talks to the participants in the order of their numbers.
  • The total effectiveness\textit{total effectiveness} of Alexander's communication with the participants will be described by the following formula:
i=1k(ai+tibi)\sum_{i = 1}^k \left(a_i + t_i \cdot b_i\right)

Alexander has a predetermined plan for working with the participants. The work plan is given by a sequence of nn integers p1p_1, p2p_2, \ldots, pnp_n. For each ii, if pi1p_i \neq -1, then by the time the first ii participants are allowed into the competition hall (after ii minutes from the start of the entrance process), Alexander must talk to the first pip_i participants and no one else. In this case, it is also guaranteed that piip_i \ge i. If pi=1p_i = -1, it means that there are no restrictions on the number of participants with whom Alexander must work after ii minutes from the start of the entrance process.

More formally, if pi1p_i \neq -1, then:

  • piip_i \ge i;
  • tpi<it_{p_i} < i;
  • For any jj, such that pi<jkp_i < j \le k, it holds that tjit_j \ge i.

Help Alexander determine what the maximum possible total effectiveness\textit{total effectiveness} of his work with the participants can be while satisfying all the constraints. It is guaranteed that the answer always exists.

Input Format

The first line contains a single integer nn (1n1061 \le n \le 10^6) --- the number of participants waiting in line to enter the competition hall of the olympiad.

The next nn lines contain two integers aia_i and bib_i (109ai109-10^9 \le a_i \le 10^9, 106bi106-10^6 \le b_i \le 10^6) --- the anxiety parameters of the ii-th participant.

The following line contains nn integers p1p_1, p2p_2, \ldots, pnp_n (ipini \le p_i \le n or pi=1p_i = -1) --- the description of the work plan for Alexander.

It is guaranteed that for any pair 1i<jn1 \le i < j \le n, it holds that pipjp_i \le p_j, if pi1p_i \ne -1 and pj1p_j \ne -1.

Output Format

Output a single number --- the maximum possible total effectiveness\textit{total effectiveness} of Alexander's work.

It can be shown that Alexander will always be able to perform his work while adhering to all additional constraints and the work plan.

4
3 -6
4 -10
-7 -3
-3 6
3 3 -1 -1
15
4
-6 -1
-5 14
0 10
-30 2
2 3 -1 -1
-1
4
-6 -1
-5 14
0 10
-30 2
-1 -1 -1 -1
23

Hint

Note

In the first test example, it is optimal to choose k=4k=4 and t={0,0,0,3}t = \{0, 0, 0, 3\}. Then the total effectiveness\textit{total effectiveness} will be:

$$\left(3 + t_0 \cdot (-6)\right) + \left(4 + t_1 \cdot (-3)\right) + \left(-7 + t_2 \cdot (-3) \right) + \left(-3 + t_4 \cdot 6\right) =$$$$= \left(3 + 0 \cdot (-6)\right) + \left(4 + 0 \cdot (-3)\right) + \left(-7 + 0 \cdot (-3) \right) + \left(-3 + 3 \cdot 6\right) = 15$$

In the second test example, it is optimal to choose k=3k=3 and t={0,0,1}t = \{0, 0, 1\}. Then the total effectiveness\textit{total effectiveness} will be:

$$\left(-6 + t_0 \cdot (-1)\right) + \left(-5 + t_1 \cdot 14\right) + \left(0 + t_2 \cdot 10\right) = \left(-6 + 0 \cdot (-1)\right) + \left(-5 + 0 \cdot 14\right) + \left(0 + 1 \cdot 10\right) = -1$$

In the third test example, it is optimal to choose k=3k=3 and t={0,1,2}t = \{0, 1, 2\}. Then the total effectiveness\textit{total effectiveness} will be:

$$\left(-6 + t_0 \cdot (-1)\right) + \left(-5 + t_1 \cdot 14\right) + \left(0 + t_2 \cdot 10\right) = \left(-6 + 0 \cdot (-1)\right) + \left(-5 + 1 \cdot 14\right) + \left(0 + 2 \cdot 10\right) = 23$$

Scoring

The tests for this problem consist of fourteen groups. Points for each group are awarded only if all tests of the group and all tests of some previous groups are passed. Note that passing the tests from the statement is not required for some groups. Offline testing\textbf{Offline testing} means that the results of testing your solution on this group will only be available after the end of the competition. The final score for each group is equal to the maximum score obtained for that group of tests across all submissions.

Group Points Additional constraints < Necessary groups Comment
nn bib_i pip_i
00 -- -- -- -- Tests from the statement.
11 66 n100n \le 100 pi=1p_i=-1
22 ^ -- 00 -- 11 ^
33 77 n5000n \le 5000 -- pi=1p_i=-1 11
44 66 ^ -- 00 -- 33 ^
55 77 -- bi0b_i \le 0 pi=1p_i=-1 --
66 55 ^ -- 55 ^
77 -- bi0b_i \ge 0 pi=1p_i=-1 --
88 55 ^ -- 77 ^
99 -- bibi+1b_i \le b_{i+1} pi=1p_i=-1 --
1010 88 ^ -- 99 ^
1111 1010 n100000n \le 100\,000 -- pi=1p_i=-1 -- Number bi>0b_i > 0 no more than 10
1212 77 ^ -- 1111 ^
1313 99 -- pi=1p_i=-1 11, 33, 55, 77, 99, 1111 Offline testing
1414 88 ^ -- 00 -- 1313 ^