#P51435. [JOI 2021 Final] Dungeon 3

[JOI 2021 Final] Dungeon 3

Description

There is a dungeon with N+1N + 1 floors. There are MM players in the dungeon. The floors are numbered from 11 to N+1N + 1, starting from the entrance. The players are numbered from 11 to MM.

A player uses energy to move from a floor to the next floor. The amount of energy a player uses is AiA_i if he moves from the floor ii (1iN1 \leq i \leq N) to the floor i+1i + 1. As this is a one-way dungeon, the only possible moves between floors are from the floor ii to the floor i+1i + 1 for some ii (1iN1 \leq i \leq N).

In each of the floors from the floor 11 to the floor NN, inclusive, there is a fountain of recovery. At the fountain of recovery in the floor ii (1iN1 \leq i \leq N), a player can increase his energy by 11 paying BiB_i 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 jj (1jM1 \leq j \leq M) is in the floor SjS_j. His current energy is 0. His maximum value of energy is UjU_j. He wants to move to the floor TjT_j. His energy cannot be smaller than 00 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 00 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.

N MN\ M
A1ANA_1 \ldots A_N
B1BNB_1 \ldots B_N
S1 T1 U1S_1\ T_1\ U_1
\vdots
SM TM UMS_M\ T_M\ U_M

Output

Write MM lines to the standard output. The jj-th line (1jM1 \leq j \leq M) should contain the minimum number of coins the player jj needs to move to the floor TjT_j. If it is impossible for the player jj to move to the floor TjT_j, output −1 instead.

Sample 1

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

Since the maximum value of energy of the player 11 is 33, the player 11 cannot move from the floor 22 to the floor 33. Hence the first line of output is −1.

On the other hand, the maximum value of energy of the player 22 is 44. The player 22 can move to the floor 66 by the following way.

  • In the floor 11, he pays 88 coins, and his energy becomes 44. Then he moves to the floor 22, and his energy becomes 11.
  • In the floor 22, he pays 1515 coins, and his energy becomes 44. Then he moves to the floor 33, and his energy becomes 00.
  • In the floor 33, he pays 44 coins, and his energy becomes 44. Then he moves to the floor 44, and his energy becomes 33.
  • In the floor 44, he does not pay coins. Then he moves to the floor 55, and his energy becomes 22.
  • In the floor 55, he pays 22 coins, and his energy becomes 44. Then he moves to the floor 66, and his energy becomes 00.

In total, the player 22 pays 2929 coins. Since it is impossible for the player 22 to move to the floor 66 by paying less than 2929 coins, the second line of output is 2929.

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

This sample input satisfies the constraints of the subtask 33.

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

  • 1N2×1051 \leq N \leq 2 \times 10^5.
  • 1M2×1051 \leq M \leq 2 \times 10^5.
  • 1Ai2×1051 \leq A_i \leq 2 \times 10^5 (1iN1 \leq i \leq N).
  • 1Bi2×1051 \leq B_i \leq 2 \times 10^5 (1iN1 \leq i \leq N).
  • 1Si<TiN+11 \leq S_i < T_i \leq N+1 (1jM1 \leq j \leq M).
  • 1Ui1081 \leq U_i \leq 10^8 (1jM1 \leq j \leq M).

Subtasks

  1. (1111 points) N3000N \leq 3000, M3000M \leq 3000.
  2. (1414 points) U1=U2==UMU_1 = U_2 = \ldots = U_M.
  3. (3131 points) Tj=N+1T_j = N + 1 (1jM1 \leq j \leq M).
  4. (4444 points) No additional constraints.