A - ダンジョン探索

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

配点 : 233

問題文

高橋君はダンジョンを探索しています。

ダンジョンには N 体のモンスターが一列に並んでおり、i 番目のモンスターの強さは H_i です。高橋君は初期体力 P で、1 番目のモンスターから N 番目のモンスターまで順番に 1 体ずつ戦います。モンスターを飛ばしたり、戻って再戦したりすることはできません。

高橋君がモンスター i と戦うとき、現在の体力に応じて以下のいずれか一方が起こります:

  • 現在の体力が H_i 以上であれば、モンスターを倒すことに成功し、体力が H_i だけ 減少 する。
  • 現在の体力が H_i 未満であれば、モンスターを倒すことに失敗する。このとき、モンスターから反撃を受けた衝撃で逆に力が覚醒し、体力が H_i だけ 増加 する。モンスターは倒せないまま、次のモンスターへ進む。

体力に上限はなく、戦闘の結果体力がちょうど 0 になっても探索は続行します。なお、上記のルールにより、体力が負になることはありません。

すべてのモンスターとの戦闘が終わったとき、高橋君が倒すことに成功したモンスターの数を求めてください。

制約

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq P \leq 10^9
  • 1 \leq H_i \leq 10^9
  • 入力はすべて整数である

入力

N P
H_1 H_2 \ldots H_N
  • 1 行目には、モンスターの数を表す N と、高橋君の初期体力を表す P が、スペース区切りで与えられる。
  • 2 行目には、各モンスターの強さを表す H_1, H_2, \ldots, H_N が、スペース区切りで与えられる。

出力

高橋君が倒すことに成功したモンスターの数を 1 行で出力してください。


入力例 1

5 7
3 10 4 8 2

出力例 1

4

入力例 2

4 2
5 1 10 3

出力例 2

2

入力例 3

12 20
5 7 30 10 12 1 40 8 50 20 3 60

出力例 3

9

入力例 4

20 100
20 50 40 200 30 60 10 500 100 90 80 70 60 50 400 30 20 10 1000 5

出力例 4

15

入力例 5

1 1
1

出力例 5

1

Score : 233 pts

Problem Statement

Takahashi is exploring a dungeon.

In the dungeon, N monsters are lined up in a row, and the strength of the i-th monster is H_i. Takahashi starts with an initial health of P and fights the monsters one by one in order from the 1-st to the N-th. He cannot skip monsters or go back to fight them again.

When Takahashi fights monster i, one of the following occurs depending on his current health:

  • If his current health is greater than or equal to H_i, he successfully defeats the monster, and his health decreases by H_i.
  • If his current health is less than H_i, he fails to defeat the monster. In this case, the shock of the monster's counterattack instead awakens his power, and his health increases by H_i. The monster remains undefeated, and he proceeds to the next monster.

There is no upper limit on health, and even if his health becomes exactly 0 as a result of a battle, the exploration continues. Note that, due to the rules above, his health will never become negative.

After all battles with the monsters are finished, determine the number of monsters that Takahashi successfully defeated.

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • 1 \leq P \leq 10^9
  • 1 \leq H_i \leq 10^9
  • All input values are integers.

Input

N P
H_1 H_2 \ldots H_N
  • The first line contains N, the number of monsters, and P, Takahashi's initial health, separated by a space.
  • The second line contains H_1, H_2, \ldots, H_N, the strengths of the monsters, separated by spaces.

Output

Print the number of monsters that Takahashi successfully defeated, in a single line.


Sample Input 1

5 7
3 10 4 8 2

Sample Output 1

4

Sample Input 2

4 2
5 1 10 3

Sample Output 2

2

Sample Input 3

12 20
5 7 30 10 12 1 40 8 50 20 3 60

Sample Output 3

9

Sample Input 4

20 100
20 50 40 200 30 60 10 500 100 90 80 70 60 50 400 30 20 10 1000 5

Sample Output 4

15

Sample Input 5

1 1
1

Sample Output 5

1
B - 過信と実力

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

配点 : 333

問題文

高橋君が所属するプログラミングサークルには N 人のメンバーがいます。各メンバーにはレーティングがあり、メンバー i のレーティングは S_i です。

サークルでは近々チーム分けを行うことになり、青木君がメンバー間の関係を調査しています。各メンバーは自分のレーティングがどのくらいかを自己評価しており、メンバー i の自己評価値は C_i です。しかし、自己評価値は必ずしも実際のレーティングと一致するとは限りません。

青木君は、あるメンバーが別のメンバーを「見下している」ペアがどれだけ存在するかを知りたいと考えています。メンバー i がメンバー j を「見下している」とは、以下の条件を すべて 満たすことを指します:

  • i \neq j
  • メンバー i の自己評価値がメンバー j のレーティングよりも真に大きい(すなわち C_i > S_j
  • メンバー i のレーティングがメンバー j のレーティング以下である(すなわち S_i \leq S_j

つまり、実力では相手以下であるにもかかわらず、自己評価では相手の実力を超えていると過信している状況です。

「見下している」関係にある順序付きペア (i, j) の総数を求めてください。ここで、メンバー i がメンバー j を見下しているとき (i, j)1 つのペアとして数えます。メンバー i がメンバー j を見下しているかどうかと、メンバー j がメンバー i を見下しているかどうかは独立に判定され、(i, j)(j, i) は区別して数えます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9
  • 1 \leq C_i \leq 10^9
  • 入力はすべて整数である

入力

N
S_1 C_1
S_2 C_2
\vdots
S_N C_N

1 行目には、メンバーの人数を表す整数 N が与えられる。続く N 行のうち i 行目には、メンバー i のレーティング S_i と自己評価値 C_i が、スペース区切りで与えられる。

出力

「見下している」関係にある順序付きペア (i, j) の総数を 1 行で出力せよ。


入力例 1

4
10 15
12 11
8 20
15 15

出力例 1

4

入力例 2

3
10 5
20 10
30 30

出力例 2

0

入力例 3

10
100 150
120 130
120 200
80 90
200 250
150 150
90 300
300 100
200 199
50 1000

出力例 3

21

入力例 4

30
500 700
300 450
800 600
1000 1200
750 1000
200 900
600 601
900 950
100 1000
400 399
850 2000
650 700
700 650
950 1100
250 260
550 100
150 10000
1000 1000
350 800
450 460
50 51
999 1500
1 1000000000
1000000000 1
720 721
720 1000
880 879
330 1000
660 500
110 111

出力例 4

158

入力例 5

2
1 1000000000
999999999 1

出力例 5

1

Score : 333 pts

Problem Statement

The programming circle that Takahashi belongs to has N members. Each member has a rating, and the rating of member i is S_i.

The circle will soon be dividing into teams, and Aoki is investigating the relationships between members. Each member has a self-assessed value of their own rating, and the self-assessed value of member i is C_i. However, the self-assessed value does not necessarily match the actual rating.

Aoki wants to know how many pairs exist where one member "looks down on" another member. Member i "looks down on" member j if all of the following conditions are satisfied:

  • i \neq j
  • Member i's self-assessed value is strictly greater than member j's rating (i.e., C_i > S_j)
  • Member i's rating is less than or equal to member j's rating (i.e., S_i \leq S_j)

In other words, this is a situation where member i is overconfident, believing their self-assessed ability exceeds the other's actual ability, despite their own actual ability being no greater than the other's.

Find the total number of ordered pairs (i, j) that are in a "looking down on" relationship. Here, when member i looks down on member j, we count (i, j) as one pair. Whether member i looks down on member j and whether member j looks down on member i are determined independently, and (i, j) and (j, i) are counted separately.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq S_i \leq 10^9
  • 1 \leq C_i \leq 10^9
  • All inputs are integers

Input

N
S_1 C_1
S_2 C_2
\vdots
S_N C_N

The first line contains an integer N representing the number of members. The i-th of the following N lines contains member i's rating S_i and self-assessed value C_i, separated by a space.

Output

Output the total number of ordered pairs (i, j) in a "looking down on" relationship, on a single line.


Sample Input 1

4
10 15
12 11
8 20
15 15

Sample Output 1

4

Sample Input 2

3
10 5
20 10
30 30

Sample Output 2

0

Sample Input 3

10
100 150
120 130
120 200
80 90
200 250
150 150
90 300
300 100
200 199
50 1000

Sample Output 3

21

Sample Input 4

30
500 700
300 450
800 600
1000 1200
750 1000
200 900
600 601
900 950
100 1000
400 399
850 2000
650 700
700 650
950 1100
250 260
550 100
150 10000
1000 1000
350 800
450 460
50 51
999 1500
1 1000000000
1000000000 1
720 721
720 1000
880 879
330 1000
660 500
110 111

Sample Output 4

158

Sample Input 5

2
1 1000000000
999999999 1

Sample Output 5

1
C - ドミノ倒し

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

配点 : 366

問題文

高橋君は、N 個のドミノが一列に並べられたドミノ倒しのコースを作りました。ドミノには左から順に 1 から N までの番号が付けられています。

各ドミノ i1 \leq i \leq N)には「許容衝撃値」P_i と「衝撃増分」D_i が定められています。許容衝撃値とは、そのドミノが粉砕されずに正常に倒れることのできる衝撃値の上限です。

このコースでは、ドミノ 1 にボールを当てることでドミノ倒しが始まります。ボールを当てたときの初期衝撃値を S とします。ドミノ倒しは、ドミノ 1 から順に以下のように進行します。

ドミノ i に到達した時点での衝撃値を C_i とします。ドミノ 1 については C_1 = S です。

  • C_i \leq P_i の場合(衝撃値が許容衝撃値以下の場合)、ドミノ i正常に倒れます。i < N のとき、倒れたドミノ i は次のドミノ i+1 に衝撃を伝え、ドミノ i+1 に到達する衝撃値は C_{i+1} = C_i + D_i となります。
  • C_i > P_i の場合(衝撃値が許容衝撃値を超えている場合)、ドミノ i は衝撃に耐えきれず粉砕されます。粉砕されたドミノは倒れることなく砕け散るため、次のドミノに衝撃は伝わらず、ドミノ倒しはそこで終了します。粉砕されたドミノは「正常に倒れた」とはみなしません。

高橋君は Q 回の実験を行います。各実験は互いに独立であり、毎回すべてのドミノが初期配置に戻された上で行われます。各実験 j1 \leq j \leq Q)では、ドミノ 1 にぶつけるボールの初期衝撃値 S_j が与えられます。

各実験について、正常に倒れた最後のドミノの番号を求めてください。すべてのドミノが正常に倒れた場合は N を出力してください。ドミノ 1 すら正常に倒れなかった場合(すなわち S_j > P_1 であった場合)は 0 を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq P_i \leq 10^91 \leq i \leq N
  • 0 \leq D_i \leq 10^91 \leq i \leq N
  • 0 \leq S_j \leq 10^91 \leq j \leq Q
  • 入力はすべて整数である。

注意

衝撃値はドミノ倒しの過程で 10^9 を超える場合があります。すべてのドミノが正常に倒れる場合、衝撃値は最大で S_j + D_1 + D_2 + \cdots + D_{N-1} に達する可能性がありますが、これは 64 ビット符号付き整数の範囲に収まります。

D_N は入力として与えられますが、ドミノ N の次のドミノは存在しないため使用されません。


入力

N Q
P_1 D_1
P_2 D_2
\vdots
P_N D_N
S_1
S_2
\vdots
S_Q
  • 1 行には、ドミノの個数を表す整数 N と、実験の回数を表す整数 Q が、スペース区切りで与えられる。
  • 2 行から第 N + 1 行では、各ドミノの許容衝撃値と衝撃増分が与えられる。
  • i + 1 行(1 \leq i \leq N)では、ドミノ i の許容衝撃値 P_i と衝撃増分 D_i がスペース区切りで与えられる。
  • N + 2 行から第 N + 1 + Q 行では、各実験の初期衝撃値が与えられる。
  • N + 1 + j 行(1 \leq j \leq Q)では、j 番目の実験における初期衝撃値 S_j が与えられる。

出力

Q 行出力せよ。第 j 行(1 \leq j \leq Q)には、j 番目の実験において正常に倒れた最後のドミノの番号を出力せよ。すべてのドミノが正常に倒れた場合は N を出力せよ。正常に倒れたドミノが 1 つもなかった場合は 0 を出力せよ。


入力例 1

3 3
5 2
6 3
20 0
3
5
6

出力例 1

3
1
0

入力例 2

4 4
2 0
2 0
2 0
2 0
0
1
2
3

出力例 2

4
4
4
0

入力例 3

10 6
8 5
20 0
18 4
30 10
60 0
25 2
26 8
40 1
42 3
100 0
0
5
6
8
15
1

出力例 3

10
10
6
5
0
10

入力例 4

40 20
200000000 30000000
250000000 30000000
300000000 30000000
350000000 30000000
400000000 30000000
450000000 30000000
500000000 30000000
550000000 30000000
600000000 30000000
650000000 30000000
680000000 100000000
760000000 100000000
840000000 100000000
920000000 100000000
1000000000 100000000
1000000000 100000000
1000000000 100000000
1000000000 100000000
1000000000 100000000
1000000000 0
900000000 50000000
850000000 50000000
800000000 50000000
750000000 50000000
700000000 50000000
650000000 50000000
600000000 50000000
550000000 50000000
500000000 50000000
450000000 50000000
400000000 50000000
350000000 50000000
300000000 50000000
250000000 50000000
200000000 50000000
150000000 0
120000000 0
100000000 0
80000000 0
60000000 0
0
10000000
50000000
90000000
120000000
150000000
180000000
200000000
220000000
240000000
260000000
280000000
300000000
350000000
400000000
450000000
500000000
600000000
800000000
1000000000

出力例 4

18
17
17
17
16
16
16
16
0
0
0
0
0
0
0
0
0
0
0
0

入力例 5

1 5
7 1000000000
0
7
8
1000000000
6

出力例 5

1
1
0
0
1

Score : 366 pts

Problem Statement

Takahashi has built a domino toppling course with N dominoes arranged in a line. The dominoes are numbered from 1 to N from left to right.

Each domino i (1 \leq i \leq N) has a "tolerance value" P_i and an "impact increment" D_i. The tolerance value is the maximum impact value at which the domino can fall normally without being shattered.

In this course, the domino toppling begins by hitting a ball against domino 1. Let S be the initial impact value when the ball hits. The domino toppling proceeds sequentially from domino 1 as follows.

Let C_i denote the impact value when it reaches domino i. For domino 1, C_1 = S.

  • If C_i \leq P_i (the impact value is at most the tolerance value), domino i falls normally. When i < N, the fallen domino i transmits the impact to the next domino i+1, and the impact value reaching domino i+1 is C_{i+1} = C_i + D_i.
  • If C_i > P_i (the impact value exceeds the tolerance value), domino i cannot withstand the impact and is shattered. A shattered domino crumbles without falling, so no impact is transmitted to the next domino, and the domino toppling ends there. A shattered domino is not considered to have "fallen normally."

Takahashi performs Q experiments. Each experiment is independent of the others, and all dominoes are reset to their initial configuration before each experiment. In each experiment j (1 \leq j \leq Q), the initial impact value S_j of the ball hitting domino 1 is given.

For each experiment, determine the number of the last domino that fell normally. If all dominoes fell normally, output N. If even domino 1 did not fall normally (i.e., S_j > P_1), output 0.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq P_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq D_i \leq 10^9 (1 \leq i \leq N)
  • 0 \leq S_j \leq 10^9 (1 \leq j \leq Q)
  • All input values are integers.

Note

The impact value may exceed 10^9 during the domino toppling process. When all dominoes fall normally, the impact value can reach up to S_j + D_1 + D_2 + \cdots + D_{N-1}, but this fits within the range of a 64-bit signed integer.

D_N is given as input, but since there is no domino after domino N, it is not used.


Input

N Q
P_1 D_1
P_2 D_2
\vdots
P_N D_N
S_1
S_2
\vdots
S_Q
  • The first line contains an integer N representing the number of dominoes and an integer Q representing the number of experiments, separated by a space.
  • From the 2nd line to the (N + 1)-th line, the tolerance value and impact increment of each domino are given.
  • The (i + 1)-th line (1 \leq i \leq N) contains the tolerance value P_i and impact increment D_i of domino i, separated by a space.
  • From the (N + 2)-th line to the (N + 1 + Q)-th line, the initial impact value for each experiment is given.
  • The (N + 1 + j)-th line (1 \leq j \leq Q) contains the initial impact value S_j for the j-th experiment.

Output

Output Q lines. The j-th line (1 \leq j \leq Q) should contain the number of the last domino that fell normally in the j-th experiment. If all dominoes fell normally, output N. If no domino fell normally, output 0.


Sample Input 1

3 3
5 2
6 3
20 0
3
5
6

Sample Output 1

3
1
0

Sample Input 2

4 4
2 0
2 0
2 0
2 0
0
1
2
3

Sample Output 2

4
4
4
0

Sample Input 3

10 6
8 5
20 0
18 4
30 10
60 0
25 2
26 8
40 1
42 3
100 0
0
5
6
8
15
1

Sample Output 3

10
10
6
5
0
10

Sample Input 4

40 20
200000000 30000000
250000000 30000000
300000000 30000000
350000000 30000000
400000000 30000000
450000000 30000000
500000000 30000000
550000000 30000000
600000000 30000000
650000000 30000000
680000000 100000000
760000000 100000000
840000000 100000000
920000000 100000000
1000000000 100000000
1000000000 100000000
1000000000 100000000
1000000000 100000000
1000000000 100000000
1000000000 0
900000000 50000000
850000000 50000000
800000000 50000000
750000000 50000000
700000000 50000000
650000000 50000000
600000000 50000000
550000000 50000000
500000000 50000000
450000000 50000000
400000000 50000000
350000000 50000000
300000000 50000000
250000000 50000000
200000000 50000000
150000000 0
120000000 0
100000000 0
80000000 0
60000000 0
0
10000000
50000000
90000000
120000000
150000000
180000000
200000000
220000000
240000000
260000000
280000000
300000000
350000000
400000000
450000000
500000000
600000000
800000000
1000000000

Sample Output 4

18
17
17
17
16
16
16
16
0
0
0
0
0
0
0
0
0
0
0
0

Sample Input 5

1 5
7 1000000000
0
7
8
1000000000
6

Sample Output 5

1
1
0
0
1
D - 山岳縦走路の最長下り列

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

配点 : 400

問題文

高橋君は山岳地帯の縦走路を調査しています。この山岳地帯には N 個の地点があり、地点には 1 から N までの番号がついています。地点間は N-1 本の双方向の道で結ばれており、どの2地点間もちょうど1通りの経路で行き来できます(すなわち木構造をなしています)。

木の構造は、地点 1 を根としたときの各地点の親として与えられます。具体的には、地点 i2 \leq i \leq N)の親は地点 p_i です。

各地点 i1 \leq i \leq N)には標高 H_i が設定されています。異なる地点の標高が同じ値であることもあります。

高橋君は Q 回の経路調査を行います。各調査 j1 \leq j \leq Q)では、始点 u_j と終点 v_j の2つの地点が指定されます(u_j = v_j であることもあります)。地点 u_j から地点 v_j への木上の一意なパスを考えます。このパス上の地点を u_j から v_j へ向かう順に a_1, a_2, \ldots, a_k とします。ここで k はパス上の地点数であり、a_1 = u_ja_k = v_j です。u_j = v_j のときは k = 1a_1 = u_j です。

このパスに対応する 標高列 h_1, h_2, \ldots, h_k を、各 t1 \leq t \leq k)について h_t = H_{a_t} と定めます。

各調査について、この標高列の 最長狭義単調減少部分列の長さ を求めてください。

用語の定義

  • h_1, h_2, \ldots, h_k部分列(subsequence) とは、1 \leq i_1 < i_2 < \cdots < i_m \leq k を満たす添字の組(m \geq 1)を選び、h_{i_1}, h_{i_2}, \ldots, h_{i_m} を元の順序のまま並べて得られる列のことです。選ぶ添字は元の列で連続している必要はありません。
  • 狭義単調減少部分列 とは、長さ m \geq 1 の部分列 h_{i_1}, h_{i_2}, \ldots, h_{i_m}1 \leq i_1 < i_2 < \cdots < i_m \leq k)であって、h_{i_1} > h_{i_2} > \cdots > h_{i_m} を満たすものを指します。特に m = 1 のとき、この条件は自動的に満たされます。
  • 最長狭義単調減少部分列の長さ とは、すべての狭義単調減少部分列の中で長さ m が最大となるものの長さです。k \geq 1 であれば長さ 1 の部分列が必ず存在するため、最長狭義単調減少部分列の長さは必ず 1 以上です。

制約

  • 1 \leq N \leq 5000
  • 1 \leq Q \leq 5000
  • 1 \leq H_i \leq 10^91 \leq i \leq N
  • 1 \leq p_i \leq i - 12 \leq i \leq N
  • 1 \leq u_j, v_j \leq N1 \leq j \leq Q
  • 入力はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N Q
H_1 H_2 \ldots H_N
p_2 p_3 \ldots p_N
u_1 v_1
u_2 v_2
\vdots
u_Q v_Q
  • 1 行目には、地点の個数 N と調査の回数 Q が、スペース区切りで与えられる。
  • 2 行目には、各地点の標高 H_1, H_2, \ldots, H_N がスペース区切りで与えられる。
  • 3 行目には、地点 i2 \leq i \leq N)の親の番号 p_2, p_3, \ldots, p_N が、この順にスペース区切りで与えられる。N = 1 の場合は親情報が存在しないため、この行は空行となる。
  • 続く Q 行のうち j 行目(1 \leq j \leq Q)には、j 番目の調査の始点 u_j と終点 v_j がスペース区切りで与えられる。

出力

Q 行出力せよ。j 行目(1 \leq j \leq Q)には、j 番目の調査における、地点 u_j から地点 v_j へのパス上の標高列の最長狭義単調減少部分列の長さを整数で出力せよ。


入力例 1

5 4
10 30 20 40 5
1 1 2 2
4 3
4 5
1 1
3 4

出力例 1

3
3
1
2

入力例 2

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

出力例 2

1
1
1
1
1

入力例 3

10 8
100 80 90 60 70 50 40 30 20 10
1 1 2 2 3 3 4 4 5
7 9
1 10
10 7
4 6
8 9
3 2
5 6
1 1

出力例 3

4
4
3
3
2
2
3
1

入力例 4

20 15
50 40 60 30 70 20 80 10 90 5 55 45 65 35 75 25 85 15 95 1
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
15 16
1 20
7 18
3 2
11 14
20 19
9 10
13 12
17 8
5 6
1 1
19 20
4 15
6 11
2 9

出力例 4

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

入力例 5

1 1
1000000000

1 1

出力例 5

1

Score : 400 pts

Problem Statement

Takahashi is surveying a traverse route through a mountainous region. There are N points in this mountainous region, numbered from 1 to N. The points are connected by N-1 bidirectional paths, and any two points can be reached from each other by exactly one route (i.e., they form a tree structure).

The tree structure is given by specifying the parent of each point when point 1 is the root. Specifically, the parent of point i (2 \leq i \leq N) is point p_i.

Each point i (1 \leq i \leq N) has an elevation H_i. Different points may have the same elevation.

Takahashi conducts Q route surveys. In each survey j (1 \leq j \leq Q), a starting point u_j and an ending point v_j are specified (it is possible that u_j = v_j). Consider the unique path on the tree from point u_j to point v_j. Let the points on this path, listed in order from u_j to v_j, be a_1, a_2, \ldots, a_k. Here, k is the number of points on the path, with a_1 = u_j and a_k = v_j. When u_j = v_j, we have k = 1 and a_1 = u_j.

The elevation sequence corresponding to this path is defined as h_1, h_2, \ldots, h_k, where h_t = H_{a_t} for each t (1 \leq t \leq k).

For each survey, find the length of the longest strictly decreasing subsequence of this elevation sequence.

Definitions

  • A subsequence of a sequence h_1, h_2, \ldots, h_k is a sequence obtained by choosing a set of indices (m \geq 1) satisfying 1 \leq i_1 < i_2 < \cdots < i_m \leq k, and listing h_{i_1}, h_{i_2}, \ldots, h_{i_m} in their original order. The chosen indices do not need to be consecutive in the original sequence.
  • A strictly decreasing subsequence is a subsequence h_{i_1}, h_{i_2}, \ldots, h_{i_m} of length m \geq 1 (1 \leq i_1 < i_2 < \cdots < i_m \leq k) satisfying h_{i_1} > h_{i_2} > \cdots > h_{i_m}. In particular, when m = 1, this condition is automatically satisfied.
  • The length of the longest strictly decreasing subsequence is the maximum length m among all strictly decreasing subsequences. Since a subsequence of length 1 always exists when k \geq 1, the length of the longest strictly decreasing subsequence is always at least 1.

Constraints

  • 1 \leq N \leq 5000
  • 1 \leq Q \leq 5000
  • 1 \leq H_i \leq 10^9 (1 \leq i \leq N)
  • 1 \leq p_i \leq i - 1 (2 \leq i \leq N)
  • 1 \leq u_j, v_j \leq N (1 \leq j \leq Q)
  • All input values are integers.

Input

The input is given from standard input in the following format:

N Q
H_1 H_2 \ldots H_N
p_2 p_3 \ldots p_N
u_1 v_1
u_2 v_2
\vdots
u_Q v_Q
  • The first line contains the number of points N and the number of surveys Q, separated by a space.
  • The second line contains the elevations H_1, H_2, \ldots, H_N of each point, separated by spaces.
  • The third line contains the parent numbers p_2, p_3, \ldots, p_N of points i (2 \leq i \leq N), in this order, separated by spaces. When N = 1, there is no parent information, so this line is an empty line.
  • In the following Q lines, the j-th line (1 \leq j \leq Q) contains the starting point u_j and ending point v_j of the j-th survey, separated by a space.

Output

Output Q lines. On the j-th line (1 \leq j \leq Q), output as an integer the length of the longest strictly decreasing subsequence of the elevation sequence along the path from point u_j to point v_j in the j-th survey.


Sample Input 1

5 4
10 30 20 40 5
1 1 2 2
4 3
4 5
1 1
3 4

Sample Output 1

3
3
1
2

Sample Input 2

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

Sample Output 2

1
1
1
1
1

Sample Input 3

10 8
100 80 90 60 70 50 40 30 20 10
1 1 2 2 3 3 4 4 5
7 9
1 10
10 7
4 6
8 9
3 2
5 6
1 1

Sample Output 3

4
4
3
3
2
2
3
1

Sample Input 4

20 15
50 40 60 30 70 20 80 10 90 5 55 45 65 35 75 25 85 15 95 1
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10
15 16
1 20
7 18
3 2
11 14
20 19
9 10
13 12
17 8
5 6
1 1
19 20
4 15
6 11
2 9

Sample Output 4

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

Sample Input 5

1 1
1000000000

1 1

Sample Output 5

1
E - 研究グループの編成

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

配点 : 433

問題文

高橋君は大学の研究室の幹事として、 N 人の学生を研究グループに分ける作業を担当しています。学生には 1 から N までの番号が付けられています。

各学生 i には「専門スコア」 W_i が設定されています。 2 人の学生 i , ji \neq j )は、 \gcd(W_i, W_j) \geq K であるとき、かつそのときに限り「協力可能」であると定めます。ここで \gcd は最大公約数を表します。

研究グループの編成は、以下のルールに従わなければなりません。

  • 同じグループに属する任意の 2 人の学生は、直接または間接的に「協力可能」な関係で繋がっていなければならない。具体的には、同じグループに属する任意の 2 人の学生 a , b に対し、学生の列 a = p_1, p_2, \ldots, p_m = b が存在して、隣り合う p_kp_{k+1} がすべて「協力可能」なペアでなければならない。
  • 逆に、直接または間接的に「協力可能」な関係で繋がっている学生は、必ず同じグループに所属しなければならない。

つまり、各グループに所属する学生の集合は、「協力可能」の関係によって定まる連結成分と一致します。

高橋君は、各グループに所属する学生の専門スコアの合計をそのグループの「総合力」と呼んでいます。すべてのグループのうち、総合力が最大であるグループの総合力を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^6
  • 1 \leq W_i \leq 10^6
  • 入力はすべて整数である

入力

N K
W_1 W_2 \ldots W_N
  • 1 行目には、学生の人数を表す N と、協力可能の基準値を表す K が、スペース区切りで与えられる。
  • 2 行目には、各学生の専門スコアを表す W_1, W_2, \ldots, W_N が、スペース区切りで与えられる。

出力

すべてのグループの総合力の最大値を 1 行で出力せよ。


入力例 1

6 3
6 10 15 7 22 25

出力例 1

56

入力例 2

4 10
2 3 5 7

出力例 2

7

入力例 3

18 6
12 18 25 35 49 77 22 33 55 65 91 14 26 39 52 64 96 160

出力例 3

558

入力例 4

50 50000
100000 200000 300000 400000 500000 600000 700000 800000 900000 1000000 99991 199982 299973 399964 499955 599946 699937 799928 899919 999910 65536 131072 196608 262144 327680 393216 458752 524288 589824 655360 70001 140002 210003 280004 350005 420006 490007 560008 630009 700010 1 2 3 49999 50021 75011 99989 123457 234567 345679

出力例 4

5500000

入力例 5

1 1000000
1

出力例 5

1

Score : 433 pts

Problem Statement

Takahashi is in charge of dividing N students into research groups as the coordinator of a university laboratory. The students are numbered from 1 to N.

Each student i has a "specialization score" W_i. Two students i and j (i \neq j) are defined to be "compatible" if and only if \gcd(W_i, W_j) \geq K. Here, \gcd denotes the greatest common divisor.

The formation of the research groups must follow these rules:

  • Any two students belonging to the same group must be directly or indirectly connected through "compatible" relationships. Specifically, for any two students a and b in the same group, there must exist a sequence of students a = p_1, p_2, \ldots, p_m = b such that all adjacent pairs p_k and p_{k+1} are "compatible" pairs.
  • Conversely, students who are directly or indirectly connected through "compatible" relationships must belong to the same group.

In other words, the set of students in each group corresponds to a connected component determined by the "compatible" relationship.

Takahashi calls the sum of the specialization scores of the students in each group the "total strength" of that group. Find the maximum total strength among all groups.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^6
  • 1 \leq W_i \leq 10^6
  • All input values are integers.

Input

N K
W_1 W_2 \ldots W_N
  • The first line contains N, the number of students, and K, the threshold for compatibility, separated by a space.
  • The second line contains W_1, W_2, \ldots, W_N, representing the specialization scores of the students, separated by spaces.

Output

Print the maximum total strength among all groups in a single line.


Sample Input 1

6 3
6 10 15 7 22 25

Sample Output 1

56

Sample Input 2

4 10
2 3 5 7

Sample Output 2

7

Sample Input 3

18 6
12 18 25 35 49 77 22 33 55 65 91 14 26 39 52 64 96 160

Sample Output 3

558

Sample Input 4

50 50000
100000 200000 300000 400000 500000 600000 700000 800000 900000 1000000 99991 199982 299973 399964 499955 599946 699937 799928 899919 999910 65536 131072 196608 262144 327680 393216 458752 524288 589824 655360 70001 140002 210003 280004 350005 420006 490007 560008 630009 700010 1 2 3 49999 50021 75011 99989 123457 234567 345679

Sample Output 4

5500000

Sample Input 5

1 1000000
1

Sample Output 5

1