#P15614. [ICPC 2021 Jakarta R] Happy Travelling

[ICPC 2021 Jakarta R] Happy Travelling

题目描述

A holiday is coming and Sam plans to visit his parents. There are NN cities numbered from 11 to NN. Sam lives in a campus dormitory in city 11 while his parents live in city NN.

Sam already did some research on the cities. He assigns an integer HiH_{i} to each city ii representing the "happiness value" he will get if he visits city ii, including city 11 and city NN.

As for the transportation, he found out that for every city 1i<N1 \leq i < N, there is a one-way bus that starts at city ii and stops at each city from city i+1i + 1 until city i+Tii + T_{i}; in other words, the bus will stop at each of the next TiT_{i} cities. It is guaranteed that i+TiNi + T_{i} \leq N. When Sam is at city i<Ni < N, he will board a bus that starts at city ii and alight at city jj where j(i+1,i+Ti)j \in (i + 1, i + T_{i}) while skipping all the stops between city ii and city jj (exclusive). Note that Sam cannot board a bus that does not start at city ii if he is at city ii.

Sam likes to be happy but he doesn't like being on a bus for a long time (he gets bored easily). Specifically, if he boards a bus that starts at city ii and alights at city jj (where i<ji < j), then his happiness value will be decreased by the following.

$$\left\lfloor \frac{j - i}{K} \right\rfloor \times D $$

Sam is busy preparing souvenirs for his parents and figuring out what to do when he gets there. So he needs your help computing the total maximum happiness value that he can get by carefully planning his journey from city 11 to city NN.

For example, let N=6N = 6, K=2K = 2, D=1D = 1, H1..6={8,7,8,9,0,2}H_{1..6} = \{8, -7, -8, 9, 0, 2\}, and T1..5={5,3,3,2,1}T_{1..5} = \{5, 3, 3, 2, 1\}. The maximum total happiness value that can be obtained from this example is 1818 as shown in the following plan.

  • Sam starts at city 11. He obtains a happiness value of H1=8H_{1} = 8 at city 11.
  • At city 11, Sam boards the bus that can stop at any city in [2,6][2, 6] and alights at city 44. His happiness value is decreased by $\left\lfloor \frac{4-1}{2} \right\rfloor \times 1 = 1$ due to this bus trip. He obtains a happiness value of H4=9H_{4} = 9 at city 44.
  • At city 44, Sam boards the bus that can stop at any city in [5,6][5, 6] and alights at city 55. His happiness value is decreased by $\left\lfloor \frac{5-4}{2} \right\rfloor \times 1 = 0$ due to this bus trip. He obtains a happiness value of H5=0H_{5} = 0 at city 55.
  • At city 55, Sam boards the bus that can stop at any city in [6,6][6, 6] and alights at city 66. His happiness value is decreased by $\left\lfloor \frac{6-5}{2} \right\rfloor \times 1 = 0$ due to this bus trip. He obtains a happiness value of H6=2H_{6} = 2 at city 66.

In this plan, Sam boards a bus 33 times with the total happiness value of 8+(1+9)+(0+0)+(0+2)=188 + (-1 + 9) + (0 + 0) + (0 + 2) = 18. No other plan has a better total happiness value than this in this example.

输入格式

Input begins with a line containing three integers NN KK DD (2N1000002 \leq N \leq 100\,000; 1KN1 \leq K \leq N; 0D100000 \leq D \leq 10\,000) representing the number of cities, and the parameter KK and DD as defined in the problem description, respectively. The second line contains NN integers HiH_{i} (10000Hi10000-10\,000 \leq H_{i} \leq 10\,000) representing the happiness value Sam will get if he visits city ii. The third line contains N1N-1 integers TiT_{i} (1Ti1 \leq T_{i}; i+TiNi + T_{i} \leq N) for 1i<N1 \leq i < N representing the number of next contiguous cities in which the bus that starts at city ii will stop at.

输出格式

Output contains an integer in a line representing the maximum total happiness value that can be obtained.

6 2 1
8 -7 -8 9 0 2
5 3 3 2 1
18
8 8 8
10 -5 -5 -5 -5 -5 -5 10
5 2 5 3 2 1 1
15
13 2 2
-5 -4 -4 -1 7 -6 -5 -4 -3 -2 -1 5 -7
3 10 9 8 7 6 5 4 3 2 1 1
-9

提示

Explanation for the sample input/output #2

At city 11, Sam boards the bus that can stop at any city in [2,6][2, 6] and alights at city 33. At city 33, Sam boards the bus that can stop at any city in [4,8][4, 8] and alights at city 88. The total happiness value of this plan is $10 + (\left\lfloor \frac{3-1}{8} \right\rfloor \times 8 - 5) + (\left\lfloor \frac{8-3}{8} \right\rfloor \times 8 + 10) = 8 + (0 - 5) + (0 + 10) = 15$.