A - Doors in the Center

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

配点 : 100 点

問題文

以下の条件を全て満たす長さ N の文字列を求めてください。

  • 各文字は - または = である
  • 回文である
  • 文字列中に = は 1 個または 2 個含まれる。 2 個含まれる場合、それらの = は隣接している

なお、そのような文字列はちょうど 1 つ存在します。

制約

  • 1 \leq N \leq 100
  • N は整数である

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

-==-

入力例 2

7

出力例 2

---=---

Score : 100 points

Problem Statement

Find a length-N string that satisfies all of the following conditions:

  • Each character is - or =.
  • It is a palindrome.
  • It contains exactly one or exactly two =s. If it contains two =s, they are adjacent.

Such a string is unique.

Constraints

  • 1 \leq N \leq 100
  • N is an integer.

Input

The input is given from Standard Input in the following format:

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

-==-

Sample Input 2

7

Sample Output 2

---=---
B - Distinct Strings

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

配点 : 100 点

問題文

英小文字のみからなる長さ 3 の文字列 S が与えられます。

S の各文字を並び替えて得られる文字列は、何種類ありますか?

制約

  • S は英小文字のみからなる長さ 3 の文字列

入力

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

S

出力

S の各文字を並び替えて得られる文字列の種類数を出力せよ。


入力例 1

aba

出力例 1

3

S= aba の各文字を並び替えて得られる文字列は、aab, aba, baa の 3 通りです。


入力例 2

ccc

出力例 2

1

S= ccc の各文字を並び替えて得られる文字列は、ccc の 1 通りのみです。


入力例 3

xyz

出力例 3

6

S= xyz の各文字を並び替えて得られる文字列は、xyz, xzy, yxz, yzx, zxy, zyx の 6 通りです。

Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of lowercase English letters.

How many different strings can be obtained by permuting the characters in S?

Constraints

  • S is a string S of length 3 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of different strings that can be obtained by permuting the characters in S.


Sample Input 1

aba

Sample Output 1

3

By permuting the characters in S= aba, three different strings can be obtained: aab, aba, baa.


Sample Input 2

ccc

Sample Output 2

1

By permuting the characters in S= ccc, just one string can be obtained: ccc.


Sample Input 3

xyz

Sample Output 3

6

By permuting the characters in S= xyz, six different strings can be obtained: xyz, xzy, yxz, yzx, zxy, zyx.

C - Caesar Cipher

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

配点 : 200 点

問題文

高橋君は英小文字からなる文字列 S を持っています。

高橋君は文字列 S に対して、下記の操作をちょうど 1 回行います。

  • まず、非負整数 K を選ぶ。
  • その後、S の各文字を K 個後ろの英小文字に変更する。

ただし、

  • a の 1 個後ろの英小文字は b であり、
  • b の 1 個後ろの英小文字は c であり、
  • c の 1 個後ろの英小文字は d であり、
  • \cdots
  • y の 1 個後ろの英小文字は z であり、
  • z の 1 個後ろの英小文字は a です。

例えば、b の 4 個後ろの英小文字は f であり、y の 3 個後ろの英小文字は b です。

文字列 T が与えられます。 高橋君が上記の操作によって S を T に一致させることができるかを判定してください。

制約

  • S と T はそれぞれ英小文字からなる長さ 1 以上 10^5 以下の文字列
  • S の長さと T の長さは等しい

入力

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

S
T

出力

高橋君が S を T に一致させることができる場合は Yes と出力し、 できない場合は No と出力せよ。


入力例 1

abc
ijk

出力例 1

Yes

高橋君が K=8 を選ぶと、

  • a は 8 個後ろの i に
  • b は 8 個後ろの j に
  • c は 8 個後ろの k に

それぞれ変更され、S と T が一致します。
高橋君が S を T に一致させることができるため Yes と出力します。


入力例 2

z
a

出力例 2

Yes

高橋君が K=1 を選ぶと S と T が一致します。
z の 1 個後ろの英小文字は a であることに注意してください。


入力例 3

ppq
qqp

出力例 3

No

高橋君は非負整数 K をどのように選んでも S を T に一致させることができません。 よって、No と出力します。


入力例 4

atcoder
atcoder

出力例 4

Yes

高橋君が K=0 を選ぶと S と T が一致します。

Score : 200 points

Problem Statement

Takahashi has a string S consisting of lowercase English letters.

On this string, he will do the operation below just once.

  • First, choose a non-negative integer K.
  • Then, shift each character of S to the right by K (see below).

Here,

  • a shifted to the right by 1 is b;
  • b shifted to the right by 1 is c;
  • c shifted to the right by 1 is d;
  • \cdots
  • y shifted to the right by 1 is z;
  • z shifted to the right by 1 is a.

For example, b shifted to the right by 4 is f, and y shifted to the right by 3 is b.

You are given a string T. Determine whether Takahashi can make S equal T by the operation above.

Constraints

  • Each of S and T is a string of length between 1 and 10^5 (inclusive) consisting of lowercase English letters.
  • The lengths of S and T are equal.

Input

Input is given from Standard Input in the following format:

S
T

Output

If Takahashi can make S equal T, print Yes; if not, print No.


Sample Input 1

abc
ijk

Sample Output 1

Yes

When Takahashi chooses K=8,

  • a is shifted to the right by 8 and becomes i,
  • b is shifted to the right by 8 and becomes j,
  • c is shifted to the right by 8 and becomes k,

and now S and T are equal.
Therefore, he can make S equal T, so Yes should be printed.


Sample Input 2

z
a

Sample Output 2

Yes

Choosing K=1 makes S and T equal.
Note that the letter on the right of z is a.


Sample Input 3

ppq
qqp

Sample Output 3

No

There is no non-negative integer K that he can choose to make S equal T, so No should be printed.


Sample Input 4

atcoder
atcoder

Sample Output 4

Yes

Choosing K=0 makes S and T equal.

D - Piano 3

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

配点 : 200 点

問題文

高橋君は、横一列に並んだ 100 個の鍵盤からなるピアノを持っています。 このピアノの左から i 個目の鍵盤のことを鍵盤 i と呼びます。

高橋君は今から N 回にわたってこのピアノの鍵盤を一つずつ押すことでとある曲を演奏します。 i 回目に押す鍵盤は鍵盤 A_i であり、それを押す手は S_i= L のとき左手、S_i= R のとき右手です。

演奏を始める前、高橋君は両手をそれぞれ好きな鍵盤の上に置くことができ、この時点での疲労度は 0 です。 演奏中、片方の手を鍵盤 x の上から鍵盤 y の上へと動かすと疲労度が |y-x| 増加します(逆に、手の移動以外で疲労度が増加することはありません)。 なお、ある手である鍵盤を押すためには、その手がその鍵盤の上に置かれている必要があります。

演奏が終了した時点での疲労度は最小でいくつになるか求めてください。

制約

  • 1\leq N \leq 100
  • 1\leq A_i \leq 100
  • N,A_i は整数
  • S_i は L または R

入力

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

N
A_1 S_1
A_2 S_2
\vdots
A_N S_N

出力

演奏が終了した時点での疲労度の最小値を出力せよ。


入力例 1

4
3 L
6 R
9 L
1 R

出力例 1

11

例えば以下のように演奏することができます。

  • 最初、左手を鍵盤 3 の上に、右手を鍵盤 6 の上に置いておく。
  • 左手で鍵盤 3 を押す。
  • 右手で鍵盤 6 を押す。
  • 左手を鍵盤 3 の上から鍵盤 9 の上へと動かす。疲労度が |9-3|=6 増加する。
  • 右手を鍵盤 6 の上から鍵盤 1 の上へと動かす。疲労度が |1-6|=5 増加する。
  • 左手で鍵盤 9 を押す。
  • 右手で鍵盤 1 を押す。

このとき、演奏が終了した時点での疲労度は 6+5=11 であり、これが最小です。


入力例 2

3
2 L
2 L
100 L

出力例 2

98

入力例 3

8
22 L
75 L
26 R
45 R
72 R
81 R
47 L
29 R

出力例 3

188

Score : 200 points

Problem Statement

Takahashi has a piano with 100 keys arranged in a row. The i-th key from the left is called key i.

He will play music by pressing N keys one by one. For the i-th press, he will press key A_i, using his left hand if S_i= L, and his right hand if S_i= R.

Before starting to play, he can place both of his hands on any keys he likes, and his fatigue level at this point is 0. During the performance, if he moves one hand from key x to key y, the fatigue level increases by |y-x| (conversely, the fatigue level does not increase for any reason other than moving hands). To press a certain key with a hand, that hand must be placed on that key.

Find the minimum possible fatigue level at the end of the performance.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq A_i \leq 100
  • N and A_i are integers.
  • S_i is L or R.

Input

The input is given from Standard Input in the following format:

N
A_1 S_1
A_2 S_2
\vdots
A_N S_N

Output

Print the minimum fatigue level at the end of the performance.


Sample Input 1

4
3 L
6 R
9 L
1 R

Sample Output 1

11

For example, the performance can be done as follows:

  • Initially, place the left hand on key 3 and the right hand on key 6.
  • Press key 3 with the left hand.
  • Press key 6 with the right hand.
  • Move the left hand from key 3 to key 9. The fatigue level increases by |9-3| = 6.
  • Move the right hand from key 6 to key 1. The fatigue level increases by |1-6| = 5.
  • Press key 9 with the left hand.
  • Press key 1 with the right hand.

In this case, the fatigue level at the end of the performance is 6+5 = 11, which is the minimum possible.


Sample Input 2

3
2 L
2 L
100 L

Sample Output 2

98

Sample Input 3

8
22 L
75 L
26 R
45 R
72 R
81 R
47 L
29 R

Sample Output 3

188
E - Gap Existence

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

配点 : 300 点

問題文

長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。

1\leq i,j \leq N である組 (i,j) であって、A_i-A_j=X となるものが存在するかどうか判定してください。

制約

  • 2 \leq N \leq 2\times 10^5
  • -10^9 \leq A_i \leq 10^9
  • -10^9 \leq X \leq 10^9
  • 入力は全て整数である

入力

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

N X
A_1 \ldots A_N

出力

1\leq i,j \leq N である組 (i,j) であって、A_i-A_j=X となるものが存在するとき Yes、存在しないとき No と出力せよ。


入力例 1

6 5
3 1 4 1 5 9

出力例 1

Yes

A_6-A_3=9-4=5 です。


入力例 2

6 -4
-2 -7 -1 -8 -2 -8

出力例 2

No

A_i-A_j=-4 となる組 (i,j) は存在しません。


入力例 3

2 0
141421356 17320508

出力例 3

Yes

A_1-A_1=0 です。

Score : 300 points

Problem Statement

You are given a sequence of N numbers: A=(A_1,\ldots,A_N).

Determine whether there is a pair (i,j) with 1\leq i,j \leq N such that A_i-A_j=X.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • -10^9 \leq A_i \leq 10^9
  • -10^9 \leq X \leq 10^9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N X
A_1 \ldots A_N

Output

Print Yes if there is a pair (i,j) with 1\leq i,j \leq N such that A_i-A_j=X, and No otherwise.


Sample Input 1

6 5
3 1 4 1 5 9

Sample Output 1

Yes

We have A_6-A_3=9-4=5.


Sample Input 2

6 -4
-2 -7 -1 -8 -2 -8

Sample Output 2

No

There is no pair (i,j) such that A_i-A_j=-4.


Sample Input 3

2 0
141421356 17320508

Sample Output 3

Yes

We have A_1-A_1=0.

F - Ideal Holidays

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

配点 : 350 点

問題文

AtCoder 王国の 1 週間は A+B 日からなり、1 日目から A 日目が休日で、A+1 日目から A+B 日目が平日です。

高橋くんは N 個の予定があり、i 番目の予定は今日から D_i 日後です。

高橋くんは今日が 1 週間の何日目かを忘れてしまいました。高橋くんの N 個の予定が全て休日である可能性があるかを判定してください。

制約

  • 1\leq N\leq 2\times 10^5
  • 1\leq A,B\leq 10^9
  • 1\leq D_1<D_2<\ldots<D_N\leq 10^9

入力

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

N A B
D_1 D_2 \ldots D_N

出力

高橋くんの N 個の予定が全て休日である可能性がある場合は Yes を、そうでない場合は No を一行に出力せよ。


入力例 1

3 2 5
1 2 9

出力例 1

Yes

入力では 1 週間は 7 日からなり、1 日目から 2 日目が休日、3 日目から 7 日目が平日です。

今日が 1 週間の 7 日目だとします。このとき、1 日後は 1 週間の 1 日目、2 日後は 1 週間の 2 日目、9 日後は 1 週間の 2 日目となり、全ての予定が休日となります。そのため、高橋くんの N 個の予定が全て休日である可能性があります。


入力例 2

2 5 10
10 15

出力例 2

No

入力例 3

4 347 347
347 700 705 710

出力例 3

Yes

Score: 350 points

Problem Statement

In the Kingdom of AtCoder, a week consists of A+B days, with the first through A-th days being holidays and the (A+1)-th through (A+B)-th being weekdays.

Takahashi has N plans, and the i-th plan is scheduled D_i days later.

He has forgotten what day of the week it is today. Determine if it is possible for all of his N plans to be scheduled on holidays.

Constraints

  • 1\leq N\leq 2\times 10^5
  • 1\leq A,B\leq 10^9
  • 1\leq D_1<D_2<\ldots<D_N\leq 10^9

Input

The input is given from Standard Input in the following format:

N A B
D_1 D_2 \ldots D_N

Output

Print Yes in a single line if it is possible for all of Takahashi's N plans to be scheduled on holidays, and No otherwise.


Sample Input 1

3 2 5
1 2 9

Sample Output 1

Yes

In this input, a week consists of seven days, with the first through second days being holidays and the third through seventh days being weekdays.

Let us assume today is the seventh day of the week. In this case, one day later would be the first day of the week, two days later would be the second day of the week, and nine days later would also be the second day of the week, making all plans scheduled on holidays. Therefore, it is possible for all of Takahashi's N plans to be scheduled on holidays.


Sample Input 2

2 5 10
10 15

Sample Output 2

No

Sample Input 3

4 347 347
347 700 705 710

Sample Output 3

Yes
G - Divide Interval

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

配点 : 450 点

問題文

非負整数 l,r\ (l<r) に対して、l 以上 r 未満の整数を順に並べた数列 (l,l+1,\ldots,r-2,r-1) を S(l,r) で表します。また、非負整数 i,j を用いて S(2^{i}j,2^{i}(j+1)) と表される数列を良い数列と呼ぶことにします。

非負整数 L,R\ (L\lt R) が与えられます。数列 S(L,R) をできるだけ少ない個数の良い数列に分割するとき、その個数と分割の方法を出力してください。より厳密には、以下を満たす非負整数の組の列 (l_1,r_1),(l_2,r_2),\ldots,(l_M,r_M) が存在するような正整数 M の最小値を求め、そのときの (l_1,r_1),(l_2,r_2),\ldots,(l_M,r_M) を出力してください。

  • L=l_1<r_1=l_2<r_2=\cdots=l_M<r_M=R
  • S(l_1,r_1),S(l_2,r_2),\ldots,S(l_M,r_M) は良い数列

なお、M が最小となるような分割方法は一通りのみ存在することが示せます。

制約

  • 0\leq L\lt R\leq 2^{60}
  • 入力は全て整数

入力

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

L R

出力

以下の形式にしたがって出力せよ。

M
l_1 r_1
\vdots
l_M r_M

(l_1,r_1),\dots,(l_M,r_M) を昇順で出力することに注意せよ。


入力例 1

3 19

出力例 1

5
3 4
4 8
8 16
16 18
18 19

S(3,19)=(3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18) です。これは以下の 5 つの良い数列に分割でき、これが個数が最小となるような分割方法です。

  • S(3,4)=S(2^0\cdot 3,2^0\cdot4)=(3)
  • S(4,8)=S(2^2\cdot 1,2^2\cdot 2)=(4,5,6,7)
  • S(8,16)=S(2^3\cdot 1,2^3\cdot 2)=(8,9,10,11,12,13,14,15)
  • S(16,18)=S(2^1\cdot 8,2^1\cdot 9)=(16,17)
  • S(18,19)=S(2^0\cdot 18,2^0\cdot 19)=(18)

入力例 2

0 1024

出力例 2

1
0 1024

入力例 3

3940649673945088 11549545024454656

出力例 3

8
3940649673945088 3940649673949184
3940649673949184 4503599627370496
4503599627370496 9007199254740992
9007199254740992 11258999068426240
11258999068426240 11540474045136896
11540474045136896 11549270138159104
11549270138159104 11549545016066048
11549545016066048 11549545024454656

Score: 450 points

Problem Statement

For non-negative integers l and r (l < r), let S(l, r) denote the sequence (l, l+1, \ldots, r-2, r-1) formed by arranging integers from l through r-1 in order. Furthermore, a sequence is called a good sequence if and only if it can be represented as S(2^i j, 2^i (j+1)) using non-negative integers i and j.

You are given non-negative integers L and R (L < R). Divide the sequence S(L, R) into the fewest number of good sequences, and print that number of sequences and the division. More formally, find the minimum positive integer M for which there is a sequence of pairs of non-negative integers (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M) that satisfies the following, and print such (l_1, r_1), (l_2, r_2), \ldots, (l_M, r_M).

  • L = l_1 < r_1 = l_2 < r_2 = \cdots = l_M < r_M = R
  • S(l_1, r_1), S(l_2, r_2), \ldots, S(l_M, r_M) are good sequences.

It can be shown that there is only one division that minimizes M.

Constraints

  • 0 \leq L < R \leq 2^{60}
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

L R

Output

Print the answer in the following format:

M
l_1 r_1
\vdots
l_M r_M

Note that the pairs (l_1, r_1), \dots, (l_M, r_M) should be printed in ascending order.


Sample Input 1

3 19

Sample Output 1

5
3 4
4 8
8 16
16 18
18 19

S(3,19)=(3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18) can be divided into the following five good sequences, which is the minimum possible number:

  • S(3,4)=S(2^0\cdot 3,2^0\cdot4)=(3)
  • S(4,8)=S(2^2\cdot 1,2^2\cdot 2)=(4,5,6,7)
  • S(8,16)=S(2^3\cdot 1,2^3\cdot 2)=(8,9,10,11,12,13,14,15)
  • S(16,18)=S(2^1\cdot 8,2^1\cdot 9)=(16,17)
  • S(18,19)=S(2^0\cdot 18,2^0\cdot 19)=(18)

Sample Input 2

0 1024

Sample Output 2

1
0 1024

Sample Input 3

3940649673945088 11549545024454656

Sample Output 3

8
3940649673945088 3940649673949184
3940649673949184 4503599627370496
4503599627370496 9007199254740992
9007199254740992 11258999068426240
11258999068426240 11540474045136896
11540474045136896 11549270138159104
11549270138159104 11549545016066048
11549545016066048 11549545024454656
H - Path Decomposition of a Tree

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

配点 : 475 点

問題文

NK 頂点の木が与えられます。頂点には 1,2,\dots,NK の番号がついており、i 番目 (i=1,2,\dots,NK-1) の辺は頂点 u_i,v_i を双方向に結んでいます。

この木を N 本の長さ K のパスに分解できるか判定してください。より詳細には、以下を満たす N \times K 行列 P が存在するかどうか判定してください。

  • P_{1,1},\dots,P_{1,K},P_{2,1},\dots,P_{N,K} は 1,2,\dots,NK の並べ替えである。
  • 各 i=1,2,\dots,N,\;j=1,2,\dots,K-1 について、頂点 P_{i,j} と頂点 P_{i,j+1} を結ぶ辺が存在する。

制約

  • 1 \leq N
  • 1 \leq K
  • NK \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq NK
  • 与えられるグラフは木である
  • 入力はすべて整数

入力

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

N K
u_1 v_1
u_2 v_2
\vdots
u_{NK-1} v_{NK-1}

出力

N 本の長さ K のパスに分解できるなら Yes を、そうでないなら No を出力せよ。


入力例 1

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

出力例 1

Yes

頂点 1,2 からなるパス、頂点 3,4 からなるパス、頂点 5,6 からなるパスに分解することができます。


入力例 2

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

出力例 2

No

Score : 475 points

Problem Statement

You are given a tree with NK vertices. The vertices are numbered 1,2,\dots,NK, and the i-th edge (i=1,2,\dots,NK-1) connects vertices u_i and v_i bidirectionally.

Determine whether this tree can be decomposed into N paths, each of length K. More precisely, determine whether there exists an N \times K matrix P satisfying the following:

  • P_{1,1}, \dots, P_{1,K}, P_{2,1}, \dots, P_{N,K} is a permutation of 1,2,\dots,NK.
  • For each i=1,2,\dots,N and j=1,2,\dots,K-1, there is an edge connecting vertices P_{i,j} and P_{i,j+1}.

Constraints

  • 1 \leq N
  • 1 \leq K
  • NK \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq NK
  • The given graph is a tree.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N K
u_1 v_1
u_2 v_2
\vdots
u_{NK-1} v_{NK-1}

Output

If it is possible to decompose the tree into N paths each of length K, print Yes. Otherwise, print No.


Sample Input 1

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

Sample Output 1

Yes

It can be decomposed into a path with vertices 1,2, a path with vertices 3,4, and a path with vertices 5,6.


Sample Input 2

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

Sample Output 2

No
I - Egoism

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

配点 : 550 点

問題文

AtCoder 牧場では馬がみずから水浴びをします。後片づけをする馬もいれば、散らかしたままにする馬もいます。

N 頭の馬がおり、1 から N までの番号が付けられています。馬 i (1 \leq i \leq N) は機嫌 A_i と丁寧さ B_i をもちます(ここで、B_i は 1, 2 のいずれか)。

夜になると、N 頭の馬が 1 回ずつ水浴びをします。j 回目 (1 \leq j \leq N) に水浴びをする馬の番号を p_j とおくと、馬 p_j の満足度が以下で与えられます。

  • j \geq 2 の場合、馬 p_j の機嫌と馬 p_{j-1} の丁寧さの積。
  • j = 1 の場合、馬 p_j の機嫌。

これから Q 日にわたって毎日 1 つずつクエリが与えられるので、順に処理してください。k 日目 (1 \leq k \leq Q) のクエリは以下の通りです。

  • 馬 W_k の機嫌を X_k に、丁寧さを Y_k に変更する(ここで、Y_k は 1, 2 のいずれか)。その後、その夜に N 頭の馬が水浴びをする順番を任意に決められるとき、その夜の N 頭の水浴びの満足度の総和が最大でいくらになるかを求める。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^6 (1 \leq i \leq N)
  • B_i は 1,2 のいずれか (1 \leq i \leq N)
  • 1 \leq W_k \leq N (1 \leq k \leq Q)
  • 1 \leq X_k \leq 10^6 (1 \leq k \leq Q)
  • Y_k は 1,2 のいずれか (1 \leq k \leq Q)
  • 入力される値はすべて整数

入力

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

N Q
A_1 B_1
\vdots
A_N B_N
W_1 X_1 Y_1
\vdots
W_Q X_Q Y_Q

出力

Q 行出力せよ。k 行目 (1 \leq k \leq Q) には k 日目のクエリの答えを出力せよ。


入力例 1

4 4
1 2
10 1
3 1
7 2
2 7 1
1 3 1
2 2 1
3 1000000 2

出力例 1

32
27
18
2000015

1 日目のクエリにおいて、馬 3,1,4,2 の順に水浴びすることで、各馬の満足度は以下のようになります。

  • 馬 3 の満足度は 3
  • 馬 1 の満足度は 1 \times 1 = 1
  • 馬 4 の満足度は 7 \times 2 = 14
  • 馬 2 の満足度は 7 \times 2 = 14

このときの満足度の総和は 32 です。

2 日目のクエリにおいて、馬 4,2,1,3 の順に水浴びすることで、各馬の満足度は以下のようになります。

  • 馬 4 の満足度は 7
  • 馬 2 の満足度は 7 \times 2 = 14
  • 馬 1 の満足度は 3 \times 1 = 3
  • 馬 3 の満足度は 3 \times 1 = 3

このときの満足度の総和は 27 です。

3 日目のクエリにおいて、馬 4,3,2,1 の順に水浴びすることで、満足度の総和は 18 になります。

4 日目のクエリにおいて、馬 4,3,1,2 の順に水浴びすることで、満足度の総和は 2000015 になります。


入力例 2

10 10
340019 2
598908 1
177330 2
575439 2
916653 2
451275 2
762769 2
36625 2
273231 2
367619 2
8 334230 2
8 835378 1
4 777076 2
6 61501 1
4 516395 1
4 35678 2
5 751493 1
3 815798 1
2 369777 2
5 941470 2

出力例 2

9417616
10146681
10549955
9708906
8847525
8463663
7860112
8606740
8516097
9236070

Score : 550 points

Problem Statement

At AtCoder Ranch, horses bathe themselves. Some horses clean up afterward, while others leave a mess.

There are N horses, numbered 1 to N. Horse i (1 \leq i \leq N) has a mood A_i and a tidiness B_i (where B_i is 1 or 2).

At night, the N horses bathe once each. Let p_j be the number of the horse that bathes in the j-th turn (1 \leq j \leq N). The satisfaction of horse p_j is given as follows:

  • If j \geq 2, the product of the mood of horse p_j and the tidiness of horse p_{j-1}.
  • If j = 1, the mood of horse p_j.

You will be given Q queries, one for each day over Q days. Process them in order. The query for the k-th day (1 \leq k \leq Q) is as follows:

  • Change the mood of horse W_k to X_k and its tidiness to Y_k (where Y_k is 1 or 2). Then, find the maximum possible total satisfaction of the N horses' baths that night if you can decide the order in which the N horses bathe arbitrarily.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq Q \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^6 (1 \leq i \leq N)
  • B_i is 1 or 2. (1 \leq i \leq N)
  • 1 \leq W_k \leq N (1 \leq k \leq Q)
  • 1 \leq X_k \leq 10^6 (1 \leq k \leq Q)
  • Y_k is 1 or 2. (1 \leq k \leq Q)
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N Q
A_1 B_1
\vdots
A_N B_N
W_1 X_1 Y_1
\vdots
W_Q X_Q Y_Q

Output

Output Q lines. The k-th line (1 \leq k \leq Q) should contain the answer to the query for the k-th day.


Sample Input 1

4 4
1 2
10 1
3 1
7 2
2 7 1
1 3 1
2 2 1
3 1000000 2

Sample Output 1

32
27
18
2000015

For the query on the first day, if the horses bathe in the order 3,1,4,2, the satisfaction of each horse is as follows:

  • Horse 3's satisfaction is 3
  • Horse 1's satisfaction is 1 \times 1 = 1
  • Horse 4's satisfaction is 7 \times 2 = 14
  • Horse 2's satisfaction is 7 \times 2 = 14

The total satisfaction in this case is 32.

For the query on the second day, if the horses bathe in the order 4,2,1,3, the satisfaction of each horse is as follows:

  • Horse 4's satisfaction is 7
  • Horse 2's satisfaction is 7 \times 2 = 14
  • Horse 1's satisfaction is 3 \times 1 = 3
  • Horse 3's satisfaction is 3 \times 1 = 3

The total satisfaction in this case is 27.

For the query on the third day, if the horses bathe in the order 4,3,2,1, the total satisfaction is 18.

For the query on the fourth day, if the horses bathe in the order 4,3,1,2, the total satisfaction is 2000015.


Sample Input 2

10 10
340019 2
598908 1
177330 2
575439 2
916653 2
451275 2
762769 2
36625 2
273231 2
367619 2
8 334230 2
8 835378 1
4 777076 2
6 61501 1
4 516395 1
4 35678 2
5 751493 1
3 815798 1
2 369777 2
5 941470 2

Sample Output 2

9417616
10146681
10549955
9708906
8847525
8463663
7860112
8606740
8516097
9236070