loj#P3472. 「JOI 2021 Final」地牢 3
「JOI 2021 Final」地牢 3
Description
There is a dungeon with floors. There are players in the dungeon. The floors are numbered from to , starting from the entrance. The players are numbered from to .
A player uses energy to move from a floor to the next floor. The amount of energy a player uses is if he moves from the floor () to the floor . As this is a one-way dungeon, the only possible moves between floors are from the floor to the floor for some ().
In each of the floors from the floor to the floor , inclusive, there is a fountain of recovery. At the fountain of recovery in the floor (), a player can increase his energy by paying coins. A player can use a fountain multiple times as long as he has needed coins. However, each player has a maximum value of his energy, and his energy cannot exceed that value even if he uses a fountain of recovery.
Now the player () is in the floor . His current energy is 0. His maximum value of energy is . He wants to move to the floor . His energy cannot be smaller than along the way. How many coins does he need?
Write a program which, given the information of the dungeon and the players, determines whether it is possible for each player to move to the destination so that his energy does not become smaller than along the way. If it is possible to move, the program should calculate the minimum number of coins he needs.
Input
Read the following data from the standard input. Given values are all integers.
Output
Write lines to the standard output. The -th line () should contain the minimum number of coins the player needs to move to the floor . If it is impossible for the player to move to the floor , output −1
instead.
5 4
3 4 1 1 4
2 5 1 2 1
1 6 3
1 6 4
3 5 1
2 5 9
-1
29
3
22
10 10
1 8 9 8 1 5 7 10 6 6
10 10 2 8 10 3 9 8 3 7
2 11 28
5 11 28
7 11 28
1 11 18
3 11 18
8 11 18
4 11 11
6 11 11
10 11 11
9 11 5
208
112
179
248
158
116
234
162
42
-1
20 20
2 3 2 11 4 6 9 15 17 14 8 17 3 12 20 4 19 8 4 5
19 3 18 2 13 7 5 19 10 1 12 8 1 15 20 1 13 2 18 6
12 15 67
7 15 18
16 17 14
9 21 97
1 19 43
3 18 31
16 20 70
7 20 28
1 16 61
3 5 69
9 10 15
2 13 134
11 19 23
16 20 14
5 21 16
15 20 11
7 11 54
7 16 16
13 17 10
3 15 135
151
591
4
284
339
517
35
581
254
58
-1
178
519
-1
-1
-1
219
-1
-1
214
Constraints
- .
- .
- ().
- ().
- ().
- ().
Subtasks
- ( points) , .
- ( points) .
- ( points) ().
- ( points) No additional constraints.