A - 倉庫の出荷管理

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 266

問題文

高橋君は倉庫の出荷担当として働いています。

この倉庫には N 種類の商品が管理されており、商品 i (1 \leq i \leq N) の初期在庫数は R_i 個です。

今日は M 件の出荷依頼が順番に届きます。j 番目 (1 \leq j \leq M) の出荷依頼は、商品 F_jS_j 個出荷してほしいという内容です。

高橋君は依頼を 1 番目から M 番目まで順に処理します。各依頼について、以下のように処理を行います。

  • 商品 F_j のその時点での在庫数が S_j 以上であれば、出荷に成功し、商品 F_j の在庫数を S_j 個減らします。
  • 商品 F_j のその時点での在庫数が S_j 未満であれば、出荷に失敗し、在庫数は変化しません。在庫がある分だけを出荷する(部分出荷する)ことはありません。また、失敗した依頼が後から再処理されることもありません。

すべての出荷依頼を処理し終えた後、出荷に成功した依頼の件数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq R_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq F_j \leq N (1 \leq j \leq M)
  • 1 \leq S_j \leq 10^9 (1 \leq j \leq M)
  • 入力はすべて整数である。

入力

N M
R_1 R_2 \ldots R_N
F_1 S_1
F_2 S_2
\vdots
F_M S_M
  • 1 行目には、商品の種類数を表す整数 N と、出荷依頼の件数を表す整数 M が、スペース区切りで与えられる。
  • 2 行目には、各商品の初期在庫数を表す整数 R_1, R_2, \ldots, R_N が、スペース区切りで与えられる。
  • 3 行目から (2+M) 行目まで、各出荷依頼の内容が与えられる。
  • (2 + j) 行目 (1 \leq j \leq M) には、j 番目の依頼で指定された商品番号 F_j と、出荷数量 S_j が、スペース区切りで与えられる。

出力

出荷に成功した依頼の件数を 1 行で出力してください。


入力例 1

3 4
10 5 3
1 3
2 5
1 8
3 2

出力例 1

3

入力例 2

2 3
0 1
1 1
2 5
1 3

出力例 2

0

入力例 3

5 8
100 50 200 0 30
1 50
3 100
2 50
4 1
1 50
1 1
5 30
3 150

出力例 3

5

入力例 4

10 15
1000000000 500000000 0 100 999999999 1 1000000000 50 300 200
1 500000000
1 500000000
1 1
5 999999999
5 1
6 1
6 1
3 1
4 50
4 50
4 1
7 1000000000
8 25
8 25
10 200

出力例 4

10

入力例 5

1 1
0
1 1

出力例 5

0

Score : 266 pts

Problem Statement

Takahashi works as a shipping manager at a warehouse.

This warehouse manages N types of products, and the initial stock quantity of product i (1 \leq i \leq N) is R_i units.

Today, M shipping requests arrive in order. The j-th (1 \leq j \leq M) shipping request asks to ship S_j units of product F_j.

Takahashi processes the requests in order from the 1-st to the M-th. For each request, he performs the following:

  • If the current stock quantity of product F_j is at least S_j, the shipment succeeds, and the stock quantity of product F_j is decreased by S_j.
  • If the current stock quantity of product F_j is less than S_j, the shipment fails, and the stock quantity does not change. Partial shipment (shipping only the available amount) is not performed. Additionally, failed requests are never reprocessed later.

After all shipping requests have been processed, determine the number of requests that were successfully shipped.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq M \leq 2 \times 10^5
  • 0 \leq R_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq F_j \leq N (1 \leq j \leq M)
  • 1 \leq S_j \leq 10^9 (1 \leq j \leq M)
  • All input values are integers.

Input

N M
R_1 R_2 \ldots R_N
F_1 S_1
F_2 S_2
\vdots
F_M S_M
  • The first line contains an integer N representing the number of product types and an integer M representing the number of shipping requests, separated by a space.
  • The second line contains integers R_1, R_2, \ldots, R_N representing the initial stock quantities of each product, separated by spaces.
  • From the 3-rd line to the (2+M)-th line, the contents of each shipping request are given.
  • The (2 + j)-th line (1 \leq j \leq M) contains the product number F_j specified in the j-th request and the shipping quantity S_j, separated by a space.

Output

Output the number of successfully shipped requests in a single line.


Sample Input 1

3 4
10 5 3
1 3
2 5
1 8
3 2

Sample Output 1

3

Sample Input 2

2 3
0 1
1 1
2 5
1 3

Sample Output 2

0

Sample Input 3

5 8
100 50 200 0 30
1 50
3 100
2 50
4 1
1 50
1 1
5 30
3 150

Sample Output 3

5

Sample Input 4

10 15
1000000000 500000000 0 100 999999999 1 1000000000 50 300 200
1 500000000
1 500000000
1 1
5 999999999
5 1
6 1
6 1
3 1
4 50
4 50
4 1
7 1000000000
8 25
8 25
10 200

Sample Output 4

10

Sample Input 5

1 1
0
1 1

Sample Output 5

0
B - 雪かきの回数

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

高橋君は冬の間、自宅前の道路の雪かきを担当しています。

道路は N 個の区画に分かれており、左から順に区画 1, 区画 2, \ldots, 区画 N と番号が付いています。区画 i に積もっている雪の量は A_i センチメートルです。

高橋君は除雪シャベルを使って雪かきをします。1 回の雪かきでは、1 \leq l \leq r \leq N を満たす整数の組 (l, r)1 つ選び、区画 l から区画 r までの連続するすべての区画の雪の量をそれぞれ 1 センチメートルずつ減らします。ただし、操作を行う前の時点で、区画 l, l+1, \ldots, r の中に雪の量が 0 センチメートルの区画が 1 つでもある場合、その組 (l, r) を選ぶことはできません。雪かきは何度でも行うことができ、毎回異なる組 (l, r) を選んでもよく、同じ組を複数回選ぶこともできます。

すべての区画の雪の量をちょうど 0 センチメートルにするために必要な雪かきの最小回数を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

N
A_1 A_2 \ldots A_N
  • 1 行目には、区画の個数を表す整数 N が与えられる。
  • 2 行目には、各区画に積もっている雪の量を表す N 個の整数 A_1, A_2, \ldots, A_N がスペース区切りで与えられる。

出力

すべての区画の雪の量をちょうど 0 センチメートルにするために必要な雪かきの最小回数を 1 行で出力せよ。


入力例 1

3
3 1 2

出力例 1

4

入力例 2

5
2 3 2 1 4

出力例 2

6

入力例 3

8
5 0 3 0 8 2 0 1

出力例 3

17

Score : 300 pts

Problem Statement

Takahashi is responsible for shoveling snow on the road in front of his house during winter.

The road is divided into N sections, numbered from left to right as section 1, section 2, \ldots, section N. The amount of snow accumulated in section i is A_i centimeters.

Takahashi uses a snow shovel to clear the snow. In one snow shoveling session, he chooses a pair of integers (l, r) satisfying 1 \leq l \leq r \leq N, and reduces the amount of snow in each of the consecutive sections from section l to section r by 1 centimeter. However, if at the time before the operation, any section among sections l, l+1, \ldots, r has 0 centimeters of snow, he cannot choose that pair (l, r). Snow shoveling can be performed any number of times, and he may choose a different pair (l, r) each time, or choose the same pair multiple times.

Find the minimum number of snow shoveling sessions required to make the amount of snow in all sections exactly 0 centimeters.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • All inputs are integers

Input

N
A_1 A_2 \ldots A_N
  • The first line contains an integer N representing the number of sections.
  • The second line contains N integers A_1, A_2, \ldots, A_N separated by spaces, representing the amount of snow accumulated in each section.

Output

Print in one line the minimum number of snow shoveling sessions required to make the amount of snow in all sections exactly 0 centimeters.


Sample Input 1

3
3 1 2

Sample Output 1

4

Sample Input 2

5
2 3 2 1 4

Sample Output 2

6

Sample Input 3

8
5 0 3 0 8 2 0 1

Sample Output 3

17
C - 退場する選手と順位表

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 366

問題文

高橋君は N 人の選手が参加するマラソン大会の運営をしています。選手には 1 から N までの番号が付けられており、選手 ii = 1, 2, \ldots, N)には「スタミナ値」 L_i が定められています。スタミナ値は 1 から N の順列、すなわち 1 以上 N 以下の整数がすべて異なる値として割り当てられています。

大会では、 N 人の選手が左から右へ一列に並んでスタートします。最初、選手 i は左から i 番目の位置にいます。つまり、選手の番号と初期位置は一致しています。

レースが始まると、選手たちはスタミナ値が小さい順に一人ずつリタイアしていきます。つまり、最初にスタミナ値 1 の選手がリタイアし、次にスタミナ値 2 の選手がリタイアし、……と続き、最後にスタミナ値 N の選手がリタイアします。

選手がリタイアすると、その選手は列から抜けます。残った選手たちは相対的な順序を保ったまま隙間なく詰められ、再び左から連続して並んだ状態になります。

高橋君は記録係として、各選手がリタイアする直前の時点で、その選手が残っている選手たちの中で左から何番目にいるかを記録したいと考えています。ここで「リタイア直前」とは、その選手自身もまだ列に残っている状態を指します。

スタミナ値 k の選手がリタイアする直前に、その選手自身を含む残りの選手の中で左から何番目にいたかを、 k = 1, 2, \ldots, N の順に求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq N
  • L_1, L_2, \ldots, L_N1 から N の順列である
  • 入力はすべて整数である

入力

N
L_1 L_2 \ldots L_N
  • 1 行目には、選手の人数を表す整数 N が与えられる。
  • 2 行目には、選手 ii = 1, 2, \ldots, N)のスタミナ値を表す整数 L_iN 個、スペース区切りで与えられる。

出力

N 行出力せよ。 k 行目( k = 1, 2, \ldots, N )には、スタミナ値 k の選手がリタイアする直前に、その選手自身を含む残りの選手の中で左から何番目にいたかを表す整数を出力せよ。


入力例 1

5
3 1 4 5 2

出力例 1

2
4
1
1
1

入力例 2

4
4 3 2 1

出力例 2

4
3
2
1

入力例 3

10
5 3 8 1 10 2 7 9 4 6

出力例 3

4
5
2
6
1
5
3
1
2
1

入力例 4

20
12 5 18 3 14 9 20 1 16 7 11 19 6 2 17 10 4 15 8 13

出力例 4

8
13
4
14
2
10
7
12
4
9
6
1
8
2
6
3
4
1
2
1

入力例 5

1
1

出力例 5

1

Score : 366 pts

Problem Statement

Takahashi is organizing a marathon with N participants. The players are numbered from 1 to N, and each player i (i = 1, 2, \ldots, N) has a "stamina value" L_i. The stamina values are a permutation of 1 through N, meaning they are distinct integers between 1 and N inclusive.

In the race, the N players line up in a single row from left to right at the start. Initially, player i is at the i-th position from the left. In other words, each player's number matches their initial position.

Once the race begins, players retire one by one in increasing order of their stamina values. That is, the player with stamina value 1 retires first, then the player with stamina value 2, and so on, until finally the player with stamina value N retires.

When a player retires, they leave the row. The remaining players maintain their relative order and close any gaps, forming a contiguous row from the left again.

As the record keeper, Takahashi wants to record, just before each player retires, what position from the left that player holds among the remaining players. Here, "just before retiring" means the player themselves is still in the row at that point.

For k = 1, 2, \ldots, N in order, determine the position from the left of the player with stamina value k among the remaining players (including themselves) just before that player retires.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq N
  • L_1, L_2, \ldots, L_N is a permutation of 1 through N
  • All input values are integers

Input

N
L_1 L_2 \ldots L_N
  • The first line contains an integer N, the number of players.
  • The second line contains N integers L_i separated by spaces, representing the stamina value of player i (i = 1, 2, \ldots, N).

Output

Print N lines. The k-th line (k = 1, 2, \ldots, N) should contain an integer representing the position from the left of the player with stamina value k among the remaining players (including themselves) just before that player retires.


Sample Input 1

5
3 1 4 5 2

Sample Output 1

2
4
1
1
1

Sample Input 2

4
4 3 2 1

Sample Output 2

4
3
2
1

Sample Input 3

10
5 3 8 1 10 2 7 9 4 6

Sample Output 3

4
5
2
6
1
5
3
1
2
1

Sample Input 4

20
12 5 18 3 14 9 20 1 16 7 11 19 6 2 17 10 4 15 8 13

Sample Output 4

8
13
4
14
2
10
7
12
4
9
6
1
8
2
6
3
4
1
2
1

Sample Input 5

1
1

Sample Output 5

1
D - 製品の返送

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400

問題文

高橋君は工場の検品担当者です。ベルトコンベア上に横一列に並んだ N 個の製品を検査しています。

i 番目(1 \leq i \leq N)の製品には品質スコア A_i と重量 B_i が設定されています。

基準値を K としたとき、品質スコアが K 未満(A_i < K)である製品は不良品とみなし、すべて返送する必要があります。品質スコアが K 以上の製品は良品です。

返送する不良品は箱に入れて発送します。各不良品はちょうど 1 つの箱に入れなければならず、箱には不良品のみを入れます。空の箱は使いません。箱の個数は自由に選べますが、それぞれの箱について以下の条件を満たす必要があります。

  • 同じ箱に入れる不良品は、元の並びにおいて連続する区間を成さなければならない。すなわち、ある箱に入れる不良品の位置を l, l+1, \ldots, rl \leq r)としたとき、l から r までのすべての製品が不良品でなければならない(間に良品が存在してはならない)。

1 つの箱の送料は、その箱に含まれる不良品の重量の最大値で決まります。具体的には、箱に含まれる製品の重量 B_i の最大値がその箱の送料です。送料の合計は、すべての箱の送料の和です。

Q 個の基準値 K_1, K_2, \ldots, K_Q が与えられます。各 K_j について、不良品をすべて返送するための送料の合計の最小値を求めてください。不良品が存在しない場合、送料の合計は 0 です。

各問い合わせは互いに独立です(製品の並びは共通ですが、基準値のみが変わります)。

制約

  • 1 \leq N
  • 1 \leq Q
  • N + Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 1 \leq K_j \leq 10^9
  • 入力はすべて整数である

入力

N Q
A_1 B_1
A_2 B_2
\vdots
A_N B_N
K_1
K_2
\vdots
K_Q
  • 1 行目には、製品の数を表す N と、問い合わせの数を表す Q が、スペース区切りで与えられる。
  • 2 行目から N+1 行目では、各製品の情報が与えられる。
  • 1 + i 行目には、i 番目の製品の品質スコアを表す A_i と、重量を表す B_i が、スペース区切りで与えられる。
  • 続く Q 行では、基準値が与えられる。
  • N + 1 + j 行目には、j 番目の問い合わせの基準値を表す K_j が与えられる。

出力

Q 行出力してください。

j 行目には、基準値を K_j としたときの送料の合計の最小値を整数で出力してください。


入力例 1

5 4
5 3
2 10
4 2
1 7
6 4
1
3
5
7

出力例 1

0
17
10
10

入力例 2

4 5
3 8
3 2
1 5
4 6
1
2
3
4
5

出力例 2

0
5
5
8
8

入力例 3

12 8
8 5
3 12
10 4
1 7
6 6
2 20
9 3
5 15
4 8
7 10
11 2
3 9
1
4
6
9
12
3
10
5

出力例 3

0
48
63
56
20
27
41
56

入力例 4

30 10
100 30
20 5
80 100
40 7
60 50
10 80
90 20
30 60
70 10
50 90
25 55
75 35
15 75
85 15
35 65
95 25
45 85
55 45
5 95
65 40
12 70
88 22
42 82
78 32
18 72
58 52
98 12
28 62
68 42
38 92
1
11
31
51
71
91
101
46
66
86

出力例 4

0
175
574
940
656
287
100
905
778
459

入力例 5

1 1
1 1000000000
1000000000

出力例 5

1000000000

Score : 400 pts

Problem Statement

Takahashi is a quality inspector at a factory. He is inspecting N products lined up in a row on a belt conveyor.

The i-th product (1 \leq i \leq N) has a quality score A_i and a weight B_i.

Given a threshold value K, any product with a quality score less than K (A_i < K) is considered defective and must be returned. Products with a quality score of K or higher are considered good.

Defective products to be returned are packed into boxes for shipping. Each defective product must be placed in exactly one box, and boxes may only contain defective products. Empty boxes are not used. The number of boxes can be chosen freely, but each box must satisfy the following condition:

  • The defective products placed in the same box must form a contiguous segment in the original arrangement. That is, if the positions of defective products placed in a box are l, l+1, \ldots, r (l \leq r), then all products from position l to r must be defective (no good products may exist in between).

The shipping cost of a single box is determined by the maximum weight among the defective products it contains. Specifically, the maximum value of B_i among the products in the box is the shipping cost of that box. The total shipping cost is the sum of the shipping costs of all boxes.

You are given Q threshold values K_1, K_2, \ldots, K_Q. For each K_j, find the minimum total shipping cost to return all defective products. If there are no defective products, the total shipping cost is 0.

Each query is independent (the arrangement of products is shared, but only the threshold value changes).

Constraints

  • 1 \leq N
  • 1 \leq Q
  • N + Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 1 \leq K_j \leq 10^9
  • All input values are integers

Input

N Q
A_1 B_1
A_2 B_2
\vdots
A_N B_N
K_1
K_2
\vdots
K_Q
  • The first line contains N, the number of products, and Q, the number of queries, separated by a space.
  • From the 2nd line to the (N+1)-th line, information about each product is given.
  • The (1 + i)-th line contains A_i, the quality score of the i-th product, and B_i, its weight, separated by a space.
  • The following Q lines give the threshold values.
  • The (N + 1 + j)-th line contains K_j, the threshold value for the j-th query.

Output

Print Q lines.

On the j-th line, print the minimum total shipping cost when the threshold value is K_j, as an integer.


Sample Input 1

5 4
5 3
2 10
4 2
1 7
6 4
1
3
5
7

Sample Output 1

0
17
10
10

Sample Input 2

4 5
3 8
3 2
1 5
4 6
1
2
3
4
5

Sample Output 2

0
5
5
8
8

Sample Input 3

12 8
8 5
3 12
10 4
1 7
6 6
2 20
9 3
5 15
4 8
7 10
11 2
3 9
1
4
6
9
12
3
10
5

Sample Output 3

0
48
63
56
20
27
41
56

Sample Input 4

30 10
100 30
20 5
80 100
40 7
60 50
10 80
90 20
30 60
70 10
50 90
25 55
75 35
15 75
85 15
35 65
95 25
45 85
55 45
5 95
65 40
12 70
88 22
42 82
78 32
18 72
58 52
98 12
28 62
68 42
38 92
1
11
31
51
71
91
101
46
66
86

Sample Output 4

0
175
574
940
656
287
100
905
778
459

Sample Input 5

1 1
1 1000000000
1000000000

Sample Output 5

1000000000
E - 会社経営シミュレーション

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 433

問題文

高橋君は N 日間を 1 サイクルとする経営計画を立てました。この計画では、各日ごとに売上、仕入れ費用、人件費が決まっています。

経営計画の i 日目 (1 \leq i \leq N) では、その日の終了時に資金が A_i - B_i - C_i 円だけ増加します。ここで A_i は売上、B_i は仕入れ費用、C_i は人件費を表します。(増加量が負の場合、資金は減少します。)

この経営計画は N 日目の翌日に再び 1 日目に戻り、同じ計画を無限に繰り返します。

ある日の終了時点で資金が 0 円未満(負)になった場合、会社は倒産したとみなします。

Q 個の質問が与えられます。j 番目の質問では、高橋君が経営計画の L_j 日目から経営を始め、L_j 日目の処理が行われる前の時点での資金が S_j 円であるとします。

その後、経営計画の順に従って毎日経営を行います。すなわち、経営計画上の日は L_j, L_j+1, \ldots, N, 1, 2, \ldots の順に進みます。経営を行った最初の日(経営計画の L_j 日目)を「経営開始から 1 日目」と数えます。

各質問について、経営開始から何日目の終了時点で初めて倒産するかを出力してください。無限に経営を続けても倒産しない場合は 0 を出力してください。

制約

  • 1 \leq N
  • 1 \leq Q
  • N + Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_i \leq 10^9
  • 0 \leq C_i \leq 10^9
  • 1 \leq L_j \leq N
  • 0 \leq S_j \leq 10^{12}
  • 入力はすべて整数である

入力

N Q
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N
L_1 S_1
L_2 S_2
\vdots
L_Q S_Q
  • 1 行目には、経営計画のサイクル日数を表す整数 N と、質問の個数を表す整数 Q が、スペース区切りで与えられる。
  • 続く N 行では、経営計画の各日の情報が与えられる。
  • 1 + i 行目 (1 \leq i \leq N) では、i 日目の売上を表す A_i、仕入れ費用を表す B_i、人件費を表す C_i が、スペース区切りで与えられる。
  • 続く Q 行では、各質問の情報が与えられる。
  • 1 + N + j 行目 (1 \leq j \leq Q) では、j 番目の質問における経営開始日を表す L_j と、初期資金を表す S_j が、スペース区切りで与えられる。

出力

Q 行出力してください。

j 行目には、j 番目の質問について、初めて倒産するのが経営開始から何日目の終了時点であるかを整数で出力してください。倒産しない場合は 0 を出力してください。


入力例 1

3 5
10 3 2
4 7 5
8 4 3
1 0
1 10
2 7
3 1
2 20

出力例 1

2
14
1
3
22

入力例 2

4 4
5 2 3
10 4 1
0 0 0
7 3 2
1 0
2 1
3 100
4 5

出力例 2

0
0
0
0

入力例 3

8 8
100 30 20
20 50 10
0 40 30
80 20 20
10 5 5
50 60 20
200 100 50
30 20 40
1 0
2 25
3 100
4 10
5 60
6 5
7 1000
8 15

出力例 3

3
1
17
8
7
1
269
1

入力例 4

20 15
1000000000 400000000 300000000
200000000 500000000 100000000
0 600000000 500000000
750000000 250000000 250000000
123456789 123456789 0
900000000 100000000 100000000
10000000 20000000 30000000
500000000 600000000 100000000
1000000000 0 0
0 0 0
300000000 100000000 250000000
800000000 900000000 50000000
450000000 200000000 100000000
100000000 300000000 400000000
600000000 100000000 100000000
700000000 350000000 350000000
1 1000000000 0
999999999 1 1
250000000 250000000 250000000
400000000 100000000 200000000
1 0
1 1000000000000
3 500000000
5 123456789
7 10000000
9 0
10 100
12 800000000
14 1500000000
16 0
18 999999999999
20 300000000
4 700000000
11 250000000
15 50

出力例 4

2
0
1
19
1
9
2
6
10
2
0
4
0
4
3

入力例 5

1 5
0 1000000000 1000000000
1 0
1 1
1 1999999999
1 2000000000
1 1000000000000

出力例 5

1
1
1
2
501

Score : 433 pts

Problem Statement

Takahashi has created a management plan that cycles every N days. In this plan, the revenue, purchasing costs, and labor costs are determined for each day.

On day i (1 \leq i \leq N) of the management plan, at the end of that day, the funds increase by A_i - B_i - C_i yen, where A_i represents revenue, B_i represents purchasing costs, and C_i represents labor costs. (If the increase is negative, the funds decrease.)

This management plan returns to day 1 on the day after day N, and the same plan repeats infinitely.

If at the end of any day the funds become less than 0 yen (negative), the company is considered to have gone bankrupt.

Q queries are given. In the j-th query, Takahashi starts management from day L_j of the management plan, and his funds are S_j yen at the point before day L_j's processing occurs.

After that, management is conducted daily following the order of the management plan. That is, the days in the management plan proceed in the order L_j, L_j+1, \ldots, N, 1, 2, \ldots. The first day on which management is conducted (day L_j of the management plan) is counted as "day 1 since the start of management."

For each query, output on which day since the start of management the company goes bankrupt for the first time (at the end of that day). If the company never goes bankrupt no matter how long management continues, output 0.

Constraints

  • 1 \leq N
  • 1 \leq Q
  • N + Q \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 0 \leq B_i \leq 10^9
  • 0 \leq C_i \leq 10^9
  • 1 \leq L_j \leq N
  • 0 \leq S_j \leq 10^{12}
  • All inputs are integers

Input

N Q
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_N B_N C_N
L_1 S_1
L_2 S_2
\vdots
L_Q S_Q
  • The first line contains an integer N representing the number of days in the management plan cycle and an integer Q representing the number of queries, separated by a space.
  • The following N lines give information about each day of the management plan.
  • The (1 + i)-th line (1 \leq i \leq N) contains A_i representing the revenue on day i, B_i representing the purchasing costs, and C_i representing the labor costs, separated by spaces.
  • The following Q lines give information about each query.
  • The (1 + N + j)-th line (1 \leq j \leq Q) contains L_j representing the starting day of management for the j-th query and S_j representing the initial funds, separated by a space.

Output

Output Q lines.

On the j-th line, output an integer representing on which day since the start of management the company first goes bankrupt (at the end of that day) for the j-th query. If the company does not go bankrupt, output 0.


Sample Input 1

3 5
10 3 2
4 7 5
8 4 3
1 0
1 10
2 7
3 1
2 20

Sample Output 1

2
14
1
3
22

Sample Input 2

4 4
5 2 3
10 4 1
0 0 0
7 3 2
1 0
2 1
3 100
4 5

Sample Output 2

0
0
0
0

Sample Input 3

8 8
100 30 20
20 50 10
0 40 30
80 20 20
10 5 5
50 60 20
200 100 50
30 20 40
1 0
2 25
3 100
4 10
5 60
6 5
7 1000
8 15

Sample Output 3

3
1
17
8
7
1
269
1

Sample Input 4

20 15
1000000000 400000000 300000000
200000000 500000000 100000000
0 600000000 500000000
750000000 250000000 250000000
123456789 123456789 0
900000000 100000000 100000000
10000000 20000000 30000000
500000000 600000000 100000000
1000000000 0 0
0 0 0
300000000 100000000 250000000
800000000 900000000 50000000
450000000 200000000 100000000
100000000 300000000 400000000
600000000 100000000 100000000
700000000 350000000 350000000
1 1000000000 0
999999999 1 1
250000000 250000000 250000000
400000000 100000000 200000000
1 0
1 1000000000000
3 500000000
5 123456789
7 10000000
9 0
10 100
12 800000000
14 1500000000
16 0
18 999999999999
20 300000000
4 700000000
11 250000000
15 50

Sample Output 4

2
0
1
19
1
9
2
6
10
2
0
4
0
4
3

Sample Input 5

1 5
0 1000000000 1000000000
1 0
1 1
1 1999999999
1 2000000000
1 1000000000000

Sample Output 5

1
1
1
2
501