#P7408. [JOI 2021 Final] 地牢 3 / Dungeon 3
[JOI 2021 Final] 地牢 3 / Dungeon 3
Description
There is a building with floors, numbered . There are people, numbered . Moving from floor to floor costs energy, and you can only move from floor to floor , not the other way around.
From floor to floor , there is a shop on each floor. At the shop on floor , you can pay coins to increase your energy by . You may use the shop multiple times, but you cannot make your energy exceed your energy cap. The energy cap of person is . Everyone starts with energy .
Person starts on floor , and they want to reach floor .
For each person, answer the minimum number of coins needed to reach their destination, or state that it is impossible.
Input Format
The first line contains two integers , representing the number of floors and the number of people.
The second line contains integers , representing the energy required to move.
The third line contains integers , representing the number of coins needed to buy energy at each floor’s shop.
The next lines each contain three integers , describing one person.
Output Format
Output lines. Each line contains one integer, representing the minimum number of coins person needs to reach floor .
If they cannot move to floor , output .
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
Hint
Explanation of Sample 1
Person cannot reach floor .
Person can reach floor in the following way:
- On floor , spend coins to make their energy .
- Move to floor , and the energy becomes .
- On floor , spend coins to make their energy .
- Move to floor , and the energy becomes .
- On floor , spend coins to make their energy .
- Move to floor , and the energy becomes .
- Move to floor , and the energy becomes .
- On floor , spend coins to make their energy .
- Move to floor .
In total, coins are needed.
Constraints
This problem uses bundled testdata.
- Subtask 1 (11 pts): .
- Subtask 2 (14 pts): All are equal.
- Subtask 3 (31 pts): .
- Subtask 4 (44 pts): No special constraints.
For of the testdata, , , .
Notes
Translated from The 20th Japanese Olympiad in Informatics Final Round E ダンジョン 3 English translation Dungeon 3.
Translated by ChatGPT 5
京公网安备 11011102002149号