A - Tomorrow

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

AtCoder 国の暦では、1 年は 1 月から M 月までの M ヶ月からなり、どの月も 1 日から D 日までの D 日からなります。

AtCoder 国の暦で y 年 m 月 d 日の翌日は何年何月何日であるか求めてください。

制約

  • 1000 \leq y \leq 9000
  • 1 \leq m \leq M \leq 99
  • 1 \leq d \leq D \leq 99
  • 入力は全て整数である

入力

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

M D
y m d

出力

AtCoder 国の暦で y 年 m 月 d 日の翌日が y' 年 m' 月 d' 日であるとき、y',m',d' をこの順に空白区切りで出力せよ。


入力例 1

12 30
2023 12 30

出力例 1

2024 1 1

AtCoder 国の暦では 1 年は 12 ヶ月であり、どの月も 30 日からなります。 よって、2023 年 12 月 30 日の翌日は 2024 年 1 月 1 日になります。


入力例 2

36 72
6789 23 45

出力例 2

6789 23 46

AtCoder 国の暦では 1 年は 36 ヶ月であり、どの月も 72 日からなります。 よって、6789 年 23 月 45 日の翌日は 6789 年 23 月 46 日になります。


入力例 3

12 30
2012 6 20

出力例 3

2012 6 21

Score : 100 points

Problem Statement

In the calendar of AtCoder Kingdom, a year consists of M months from month 1 to month M, and each month consists of D days from day 1 to day D.

What day follows year y, month m, day d in this calendar?

Constraints

  • 1000 \leq y \leq 9000
  • 1 \leq m \leq M \leq 99
  • 1 \leq d \leq D \leq 99
  • All input values are integers.

Input

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

M D
y m d

Output

If the day following year y, month m, day d in the calendar of AtCoder Kingdom is year y', month m', day d', print y', m', and d' in this order, separated by spaces.


Sample Input 1

12 30
2023 12 30

Sample Output 1

2024 1 1

In the calendar of the kingdom, a year consists of 12 months, and each month consists of 30 days. Thus, the day following year 2023, month 12, day 30 is year 2024, month 1, day 1.


Sample Input 2

36 72
6789 23 45

Sample Output 2

6789 23 46

In the calendar of the kingdom, one year consists of 36 months, and each month consists of 72 days. Thus, the day following year 6789, month 23, day 45 is year 6789, month 23, day 46.


Sample Input 3

12 30
2012 6 20

Sample Output 3

2012 6 21
B - Shout Everyday

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 150 点

問題文

AtCoder 王国の住民は A 時になるとたこ焼きへの愛を叫ぶことになっています。

AtCoder 王国に住む高橋君は毎日 B 時に就寝し C 時に起床します。高橋君は、起きているときはたこ焼きへの愛を叫ぶことができ、寝ているときは叫ぶことができません。高橋君が毎日たこ焼きへの愛を叫ぶことができているか判定してください。ただし、一日は 24 時間であり、高橋君が寝ている時間は 24 時間未満であるとします。

制約

  • 0\leq A,B,C\lt 24
  • A,B,C は相異なる
  • 入力は全て整数

入力

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

A B C

出力

高橋君が毎日たこ焼きへの愛を叫ぶことができているならば Yes を、そうでないならば No を出力せよ。


入力例 1

21 8 14

出力例 1

Yes

高橋君は毎日 8 時に就寝し 14 時に起床します。21 時には起きているため、高橋君は毎日たこ焼きへの愛を叫ぶことができます。よって Yes を出力します。


入力例 2

0 21 7

出力例 2

No

高橋君は毎日 21 時に就寝し 7 時に起床します。0 時には起きていないため、高橋君は毎日たこ焼きへの愛を叫ぶことができません。よって No を出力します。


入力例 3

10 7 17

出力例 3

No

Score : 150 points

Problem Statement

In the Kingdom of AtCoder, residents are required to shout their love for takoyaki at A o'clock every day.

Takahashi, who lives in the Kingdom of AtCoder, goes to bed at B o'clock and wakes up at C o'clock every day (in the 24-hour clock). He can shout his love for takoyaki when he is awake, but cannot when he is asleep. Determine whether he can shout his love for takoyaki every day. Here, a day has 24 hours, and his sleeping time is less than 24 hours.

Constraints

  • 0\leq A,B,C\lt 24
  • A, B, and C are pairwise different.
  • All input values are integers.

Input

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

A B C

Output

Print Yes if Takahashi can shout his love for takoyaki every day, and No otherwise.


Sample Input 1

21 8 14

Sample Output 1

Yes

Takahashi goes to bed at 8 o'clock and wakes up at 14 o'clock every day. He is awake at 21 o'clock, so he can shout his love for takoyaki every day. Therefore, print Yes.


Sample Input 2

0 21 7

Sample Output 2

No

Takahashi goes to bed at 21 o'clock and wakes up at 7 o'clock every day. He is not awake at 0 o'clock, so he cannot shout his love for takoyaki every day. Therefore, print No.


Sample Input 3

10 7 17

Sample Output 3

No
C - 459

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

英小文字のみからなる N 個の文字列 S_1,S_2,\ldots,S_N が与えられます。

N 個の数字 C_1,C_2,\ldots,C_N を以下のように定義します。

  • S_i の先頭の文字が a, b, c のいずれかならば C_i=2
  • S_i の先頭の文字が d, e, f のいずれかならば C_i=3
  • S_i の先頭の文字が g, h, i のいずれかならば C_i=4
  • S_i の先頭の文字が j, k, l のいずれかならば C_i=5
  • S_i の先頭の文字が m, n, o のいずれかならば C_i=6
  • S_i の先頭の文字が p, q, r, s のいずれかならば C_i=7
  • S_i の先頭の文字が t, u, v のいずれかならば C_i=8
  • S_i の先頭の文字が w, x, y, z のいずれかならば C_i=9

C_1,C_2,\ldots,C_N をこの順に連結した文字列を出力してください。

制約

  • 1\leq N\leq 10
  • N は整数
  • S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列

入力

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

N
S_1 S_2 \ldots S_N

出力

C_1,C_2,\ldots,C_N をこの順に連結した文字列を出力せよ。


入力例 1

2
algorithm heuristic

出力例 1

24
  • S_1=algorithm の先頭の文字は a であるため、C_1=2 となります。
  • S_2=heuristic の先頭の文字は h であるため、C_2=4 となります。

よって、これらを連結した 24 を出力します。


入力例 2

3
i love you

出力例 2

459

Score : 200 points

Problem Statement

You are given N strings S_1, S_2, \ldots, S_N consisting of lowercase English letters.

Define N digits C_1, C_2, \ldots, C_N as follows:

  • If the first character of S_i is one of a, b, c, then C_i= 2
  • If the first character of S_i is one of d, e, f, then C_i= 3
  • If the first character of S_i is one of g, h, i, then C_i= 4
  • If the first character of S_i is one of j, k, l, then C_i= 5
  • If the first character of S_i is one of m, n, o, then C_i= 6
  • If the first character of S_i is one of p, q, r, s, then C_i= 7
  • If the first character of S_i is one of t, u, v, then C_i= 8
  • If the first character of S_i is one of w, x, y, z, then C_i= 9

Output the string obtained by concatenating C_1, C_2, \ldots, C_N in this order.

Constraints

  • 1 \leq N \leq 10
  • N is an integer.
  • S_i is a string of length between 1 and 10, inclusive, consisting of lowercase English letters.

Input

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

N
S_1 S_2 \ldots S_N

Output

Output the string obtained by concatenating C_1, C_2, \ldots, C_N in this order.


Sample Input 1

2
algorithm heuristic

Sample Output 1

24
  • The first character of S_1= algorithm is a, so C_1= 2.
  • The first character of S_2= heuristic is h, so C_2= 4.

Thus, output 24, which is their concatenation.


Sample Input 2

3
i love you

Sample Output 2

459
D - Inverse Prefix Sum

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

整数 N と長さ N の数列 S=(S_1,\ldots,S_N) が与えられます。

長さ N の数列 A=(A_1,\ldots,A_N) であって、k=1,\ldots,N の全てについて以下の条件を満たすものを求めてください。

  • A_1+A_2+\ldots+A_k = S_k

なお、このような数列 A は必ず存在し、一意に定まります。

制約

  • 1 \leq N \leq 10
  • -10^9\leq S_i \leq 10^9
  • 入力は全て整数である

入力

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

N
S_1 \ldots S_N

出力

全ての条件を満たす数列 A=(A_1,\ldots,A_N) の各要素を、順に空白区切りで出力せよ。


入力例 1

3
3 4 8

出力例 1

3 1 4
  • A_1=3=S_1
  • A_1+A_2=3+1=4=S_2
  • A_1+A_2+A_3=3+1+4=8=S_3

であり、たしかに全ての条件を満たしています。


入力例 2

10
314159265 358979323 846264338 -327950288 419716939 -937510582 97494459 230781640 628620899 -862803482

出力例 2

314159265 44820058 487285015 -1174214626 747667227 -1357227521 1035005041 133287181 397839259 -1491424381

Score : 200 points

Problem Statement

You are given an integer N and a sequence S=(S_1,\ldots,S_N) of length N.

Find a sequence A=(A_1,\ldots,A_N) of length N that satisfies the following condition for all k=1,\ldots,N:

  • A_1+A_2+\ldots+A_k = S_k.

Such a sequence A always exists and is unique.

Constraints

  • 1 \leq N \leq 10
  • -10^9\leq S_i \leq 10^9
  • All values in the input are integers.

Input

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

N
S_1 \ldots S_N

Output

Print the elements of a sequence A=(A_1,\ldots,A_N) that satisfies all the conditions in order, separated by spaces.


Sample Input 1

3
3 4 8

Sample Output 1

3 1 4

The sequence in the output actually satisfies all the conditions:

  • A_1=3=S_1;
  • A_1+A_2=3+1=4=S_2;
  • A_1+A_2+A_3=3+1+4=8=S_3.

Sample Input 2

10
314159265 358979323 846264338 -327950288 419716939 -937510582 97494459 230781640 628620899 -862803482

Sample Output 2

314159265 44820058 487285015 -1174214626 747667227 -1357227521 1035005041 133287181 397839259 -1491424381
E - Kaiten Sushi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350 点

問題文

とある回転寿司に、1 から N までの番号が付けられた N 人の人が訪れています。 人 i の 美食度 は A_i です。

今からベルトコンベア上を M 個の寿司が流れます。 j 番目に流れる寿司の 美味しさ は B_j です。 それぞれの寿司は、人 1,2,\dots,N の前をこの順に流れていきます。 それぞれの人は、美味しさが自分の美食度以上である寿司が自分の前に流れてきたときはその寿司を取って食べ、それ以外のときは何もしません。 人 i が取って食べた寿司は、人 j\ (j > i) の前にはもう流れてきません。

M 個の寿司それぞれについて、その寿司を誰が食べるか、あるいは誰も食べないかどうかを求めてください。

制約

  • 1\leq N,M \leq 2\times 10^5
  • 1\leq A_i,B_i \leq 2\times 10^5
  • 入力は全て整数

入力

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

M 行出力せよ。 j\ (1\leq j \leq M) 行目には、j 番目に流れる寿司を食べる人がいるならばその人の番号を、そうでないならば -1 を出力せよ。


入力例 1

3 3
3 8 2
5 2 1

出力例 1

1
3
-1
  • 1 番目の寿司について、
    • まず人 1 の前を流れます。B_1 \geq A_1 なので、人 1 はこれを取って食べます。
    • 人 2,3 の前にはこの寿司は流れてきません。
  • 2 番目の寿司について、
    • まず人 1 の前を流れます。B_2 < A_1 なので、人 1 は何もしません。
    • 次に人 2 の前を流れます。B_2 < A_2 なので、人 2 は何もしません。
    • 最後に人 3 の前を流れます。B_2 \geq A_3 なので、人 3 はこれを取って食べます。
  • 3 番目の寿司について、
    • まず人 1 の前を流れます。B_3 < A_1 なので、人 1 は何もしません。
    • 次に人 2 の前を流れます。B_3 < A_2 なので、人 2 は何もしません。
    • 最後に人 3 の前を流れます。B_3 < A_3 なので、人 3 は何もしません。
    • よって、誰もこの寿司を食べません。

入力例 2

3 3
1 1 1
1 1 1

出力例 2

1
1
1

入力例 3

10 5
60 83 76 45 70 91 37 58 94 22
70 39 52 33 18

出力例 3

1
7
4
10
-1

Score: 350 points

Problem Statement

There are N people numbered from 1 to N visiting a conveyor belt sushi restaurant. The gourmet level of person i is A_i.

Now, M pieces of sushi will be placed on the conveyor belt. The deliciousness of the j-th sushi is B_j. Each piece of sushi passes in front of people 1, 2, \dots, N in this order. Each person, when a sushi whose deliciousness is not less than their gourmet level passes in front of them, will take and eat that sushi; otherwise, they do nothing. A sushi that person i takes and eats will no longer pass in front of person j\ (j > i).

For each of the M pieces of sushi, determine who eats that sushi, or if nobody eats it.

Constraints

  • 1 \leq N, M \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq 2 \times 10^5
  • All input values are integers.

Input

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

N M
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

Output

Print M lines. The j-th line (1 \leq j \leq M) should contain the number representing the person who eats the j-th sushi, or -1 if nobody eats it.


Sample Input 1

3 3
3 8 2
5 2 1

Sample Output 1

1
3
-1
  • For the 1st sushi:
    • It first passes in front of person 1. Since B_1 \geq A_1, person 1 takes and eats it.
    • It will not pass in front of person 2 and 3.
  • For the 2nd sushi:
    • It first passes in front of person 1. Since B_2 < A_1, person 1 does nothing.
    • Next, it passes in front of person 2. Since B_2 < A_2, person 2 does nothing.
    • Finally, it passes in front of person 3. Since B_2 \geq A_3, person 3 takes and eats it.
  • For the 3rd sushi:
    • It first passes in front of person 1. Since B_3 < A_1, person 1 does nothing.
    • Next, it passes in front of person 2. Since B_3 < A_2, person 2 does nothing.
    • Finally, it passes in front of person 3. Since B_3 < A_3, person 3 does nothing.
    • Therefore, nobody eats this sushi.

Sample Input 2

3 3
1 1 1
1 1 1

Sample Output 2

1
1
1

Sample Input 3

10 5
60 83 76 45 70 91 37 58 94 22
70 39 52 33 18

Sample Output 3

1
7
4
10
-1
F - Make it Simple

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

頂点に 1 から N の、辺に 1 から M の番号がついた N 頂点 M 辺の無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結ぶ辺です。
グラフから辺を取り除いてグラフを単純にするためには、少なくとも何本の辺を取り除く必要がありますか?
ここでグラフが単純であるとは、グラフが自己ループや多重辺を含まないことをいいます。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 5 \times 10^5
  • 1 \leq u_i \leq N
  • 1 \leq v_i \leq N
  • 入力される値は全て整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

グラフを単純にするために取り除く必要がある辺の本数の最小値を出力せよ。


入力例 1

3 5
1 2
2 3
3 2
3 1
1 1

出力例 1

2

辺 3 と辺 5 を取り除くとグラフを単純にすることが出来て、これが取り除く辺の本数が最小となる選び方の 1 つです。よって答えは 2 本です。


入力例 2

1 0

出力例 2

0

入力例 3

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

出力例 3

3

Score : 300 points

Problem Statement

You are given an undirected graph with N vertices and M edges, where the vertices are numbered 1 through N and the edges are numbered 1 through M. Edge i connects vertices u_i and v_i.
To make the graph simple by removing edges, what is the minimum number of edges that must be removed?
Here, a graph is called simple if and only if it does not contain self-loops or multi-edges.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 5 \times 10^5
  • 1 \leq u_i \leq N
  • 1 \leq v_i \leq N
  • All input values are integers.

Input

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

Output

Print the minimum number of edges that must be removed to make the graph simple.


Sample Input 1

3 5
1 2
2 3
3 2
3 1
1 1

Sample Output 1

2

By removing edges 3 and 5, the graph becomes simple. This is one of the ways to remove the minimum number of edges, so the answer is 2.


Sample Input 2

1 0

Sample Output 2

0

Sample Input 3

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

Sample Output 3

3
G - Stone XOR

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

袋 1, 袋 2, \ldots, 袋 N と番号づけられた N 個の袋があります。
袋 i (1\leq i\leq N) には A_i 個の石が入っています。

高橋君は次の操作を好きなだけ(0 回でも良い)繰り返すことができます。

2 つの袋 A, B を選び、袋 A に入っている石を すべて 袋 B に入れる。

操作を繰り返した後の状態における次の値としてあり得るものが何個あるか求めてください。

  • 袋 i に入っている石の個数を B_i として、B_1\oplus B_2\oplus\cdots\oplus B_N の値。
    ただし、\oplus は排他的論理和を表す。
排他的論理和とは 非負整数 a,b の排他的論理和 a \oplus b は、以下のように定義されます。
a \oplus b を二進表記した際の 2^k (k \geq 0) の位の数は、 a, b を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1 、 そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります(二進表記すると 011 \oplus 101 = 110)。
一般に k 個の非負整数 x_1, x_2, \ldots, x_k の排他的論理和 x_1\oplus x_2\oplus\cdots\oplus x_k は (\cdots((x_1 \oplus x_2) \oplus x_3) \oplus \cdots \oplus x_k) と定義され、 これは x_1, x_2, \ldots, x_k の順番によらないことが証明できます。

なお、問題の制約下において、操作を繰り返した後の状態における上記の値としてあり得るものが有限個しかないことが証明できます。

制約

  • 2\leq N \leq 12
  • 1\leq A_i \leq 10^{17}
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N

出力

高橋君が操作を繰り返した後の状態における、問題文中で定義された値としてあり得るものの個数を出力せよ。


入力例 1

3
2 5 7

出力例 1

3

例えば、高橋君が袋 A, B として袋 1, 3 を選び、操作を行ったとすると、袋 1,2,3 に入っている石はそれぞれ 0,5,9 となります。
ここで高橋君が操作を終了したとき、この状態における、袋に入っている石の個数の排他的論理和は 0\oplus 5\oplus 9=12 となります。

他に、高橋君が操作を繰り返した後の状態における、袋に入っている石の個数の排他的論理和としてあり得るものは 0,14 があります。
よって、あり得る値は 0,12,14 の 3 個であるため、3 を出力します。


入力例 2

2
100000000000000000 100000000000000000

出力例 2

2

入力例 3

6
71 74 45 34 31 60

出力例 3

84

Score : 400 points

Problem Statement

There are N bags, labeled bag 1, bag 2, \ldots, bag N.
Bag i (1 \leq i \leq N) contains A_i stones.

Takahashi can perform the following operation any number of times, possibly zero:

Choose two bags A and B, and move all stones from bag A into bag B.

Find the number of different possible values for the following after repeating the operation.

  • B_1 \oplus B_2 \oplus \cdots \oplus B_N, where B_i is the final number of stones in bag i.
    Here, \oplus denotes bitwise XOR.
About bitwise XOR For non-negative integers a and b, the bitwise XOR a \oplus b is defined as follows:
In the binary representation of a \oplus b, the digit in the 2^k place (k \ge 0) is 1 if and only if exactly one of the digits in the 2^k place of a and b is 1; otherwise, it is 0.
For example, 3 \oplus 5 = 6 (in binary, 011 \oplus 101 = 110).
In general, for k non-negative integers x_1, x_2, \ldots, x_k, their bitwise XOR x_1 \oplus x_2 \oplus \cdots \oplus x_k is defined as (\cdots((x_1 \oplus x_2) \oplus x_3) \oplus \cdots) \oplus x_k, which does not depend on the order of x_1, x_2, \ldots, x_k.

It can be proved that under the constraints of this problem, the number of possible values is finite.

Constraints

  • 2 \leq N \leq 12
  • 1 \leq A_i \leq 10^{17}
  • All input values are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print the number of different possible values for B_1 \oplus B_2 \oplus \cdots \oplus B_N after repeating the operation.


Sample Input 1

3
2 5 7

Sample Output 1

3

For example, if Takahashi chooses bags 1 and 3 for the operation, then the numbers of stones in bags 1, 2, 3 become 0, 5, 9.
If he stops at this point, the XOR is 0 \oplus 5 \oplus 9 = 12.

The other possible XOR values after repeating the operation are 0 and 14.
Therefore, the possible values are 0, 12, 14; there are three values, so the output is 3.


Sample Input 2

2
100000000000000000 100000000000000000

Sample Output 2

2

Sample Input 3

6
71 74 45 34 31 60

Sample Output 3

84
H - Integers on Grid

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

縦 H 行、横 W 列のマス目があります。上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。

それぞれのマスには整数が書かれています。 i = 1, 2, \ldots, N について、マス (r_i, c_i) には正整数 a_i が書かれており、それら以外のマスには 0 が書かれています。

はじめ、マス (R, C) にコマが置かれています。 高橋君は、コマを「いま置かれているマスから別のマスに移動させる」ことを好きな回数だけ行うことができます。 ただし、コマを移動する際には下記の 2 つの条件をともに満たさなければなりません。

  • 移動先のマスに書かれている整数は、移動前のマスに書かれている整数より真に大きい。
  • 移動先のマスは移動前のマスと同じ行にある、または、移動先のマスは移動前のマスと同じ列にある。

i = 1, 2, \ldots, N のそれぞれについて、(R, C) = (r_i, c_i) の場合に高橋君がコマの移動を行うことができる回数の最大値を出力してください。

制約

  • 2 \leq H, W \leq 2 \times 10^5
  • 1 \leq N \leq \min(2 \times 10^5, HW)
  • 1 \leq r_i \leq H
  • 1 \leq c_i \leq W
  • 1 \leq a_i \leq 10^9
  • i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
  • 入力はすべて整数

入力

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

H W N
r_1 c_1 a_1
r_2 c_2 a_2
\vdots
r_N c_N a_N

出力

N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には (R, C) = (r_i, c_i) の場合に高橋君がコマの移動を行うことができる回数の最大値を出力せよ。


入力例 1

3 3 7
1 1 4
1 2 7
2 1 3
2 3 5
3 1 2
3 2 5
3 3 5

出力例 1

1
0
2
0
3
1
0

マス目に書かれた整数は下記の通りです。

4 7 0
3 0 5
2 5 5
  • (R, C) = (r_1, c_1) = (1, 1) の場合、(1, 1) \rightarrow (1, 2) と移動すると、コマの移動を 1 回行うことができます。
  • (R, C) = (r_2, c_2) = (1, 2) の場合、一度もコマの移動を行うことができません。
  • (R, C) = (r_3, c_3) = (2, 1) の場合、(2, 1) \rightarrow (1, 1) \rightarrow (1, 2) と移動すると、コマの移動を 2 回行うことができます。
  • (R, C) = (r_4, c_4) = (2, 3) の場合、一度もコマの移動を行うことができません。
  • (R, C) = (r_5, c_5) = (3, 1) の場合、(3, 1) \rightarrow (2, 1) \rightarrow (1, 1) \rightarrow (1, 2) と移動すると、コマの移動を 3 回行うことができます。
  • (R, C) = (r_6, c_6) = (3, 2) の場合、(3, 2) \rightarrow (1, 2) と移動すると、コマの移動を 1 回行うことができます。
  • (R, C) = (r_7, c_7) = (3, 3) の場合、一度もコマの移動を行うことができません。

入力例 2

5 7 20
2 7 8
2 6 4
4 1 9
1 5 4
2 2 7
5 5 2
1 7 2
4 6 6
1 4 1
2 1 10
5 6 9
5 3 3
3 7 9
3 6 3
4 3 4
3 3 10
4 2 1
3 5 4
1 2 6
4 7 9

出力例 2

2
4
1
5
3
6
6
2
7
0
0
4
1
5
3
0
5
2
4
0

Score : 500 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.

Each square contains an integer. For each i = 1, 2, \ldots, N, the square (r_i, c_i) contains a positive integer a_i. The other square contains a 0.

Initially, there is a piece on the square (R, C). Takahashi can move the piece to a square other than the square it occupies now, any number of times. However, when moving the piece, both of the following conditions must be satisfied.

  • The integer written on the square to which the piece is moved is strictly greater than the integer written on the square from which the piece is moved.
  • The squares to and from which the piece is moved are in the same row or the same column.

For each i = 1, 2, \ldots, N, print the maximum number of times Takahashi can move the piece when (R, C) = (r_i, c_i).

Constraints

  • 2 \leq H, W \leq 2 \times 10^5
  • 1 \leq N \leq \min(2 \times 10^5, HW)
  • 1 \leq r_i \leq H
  • 1 \leq c_i \leq W
  • 1 \leq a_i \leq 10^9
  • i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
r_1 c_1 a_1
r_2 c_2 a_2
\vdots
r_N c_N a_N

Output

Print N lines. For each i = 1, 2, \ldots, N, the i-th line should contain the maximum number of times Takahashi can move the piece when (R, C) = (r_i, c_i).


Sample Input 1

3 3 7
1 1 4
1 2 7
2 1 3
2 3 5
3 1 2
3 2 5
3 3 5

Sample Output 1

1
0
2
0
3
1
0

The grid contains the following integers.

4 7 0
3 0 5
2 5 5
  • When (R, C) = (r_1, c_1) = (1, 1), you can move the piece once by moving it as (1, 1) \rightarrow (1, 2).
  • When (R, C) = (r_2, c_2) = (1, 2), you cannot move the piece at all.
  • When (R, C) = (r_3, c_3) = (2, 1), you can move the piece twice by moving it as (2, 1) \rightarrow (1, 1) \rightarrow (1, 2).
  • When (R, C) = (r_4, c_4) = (2, 3), you cannot move the piece at all.
  • When (R, C) = (r_5, c_5) = (3, 1), you can move the piece three times by moving it as (3, 1) \rightarrow (2, 1) \rightarrow (1, 1) \rightarrow (1, 2).
  • When (R, C) = (r_6, c_6) = (3, 2), you can move the piece once by moving it as (3, 2) \rightarrow (1, 2).
  • When (R, C) = (r_7, c_7) = (3, 3), you cannot move the piece at all.

Sample Input 2

5 7 20
2 7 8
2 6 4
4 1 9
1 5 4
2 2 7
5 5 2
1 7 2
4 6 6
1 4 1
2 1 10
5 6 9
5 3 3
3 7 9
3 6 3
4 3 4
3 3 10
4 2 1
3 5 4
1 2 6
4 7 9

Sample Output 2

2
4
1
5
3
6
6
2
7
0
0
4
1
5
3
0
5
2
4
0
I - Ignore Operations

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

高橋君は整数 x を持っています。はじめ、x = 0 です。

N 個の操作があります。i \, (1 \leq i \leq N) 個目の操作は整数 t_i, y_i を用いて以下のように表されます。

  • t_i = 1 のとき、x を y_i で置き換える。
  • t_i = 2 のとき、x を x + y_i で置き換える。

高橋君は 0 個以上 K 個以下の好きな個数の操作を無視することができます。残った操作を一度ずつ順序を変えずに行ったとき、最終的な x の値としてあり得る最大値を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq N
  • t_i \in \{1,2\} \, (1 \leq i \leq N)
  • |y_i| \leq 10^9 \, (1 \leq i \leq N)
  • 入力は全て整数

入力

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

N K
t_1 y_1
\vdots
t_N y_N

出力

答えを出力せよ。


入力例 1

5 1
2 4
2 -3
1 2
2 1
2 -3

出力例 1

3

5 個目の操作を無視すると、x は 0 \rightarrow 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 と変化し、最終的な x の値は 3 となります。これが最大です。


入力例 2

1 0
2 -1000000000

出力例 2

-1000000000

入力例 3

10 3
2 3
2 -1
1 4
2 -1
2 5
2 -9
2 2
1 -6
2 5
2 -3

出力例 3

15

Score : 500 points

Problem Statement

Takahashi has an integer x. Initially, x = 0.

There are N operations. The i-th operation (1 \leq i \leq N) is represented by two integers t_i and y_i as follows:

  • If t_i = 1, replace x with y_i.
  • If t_i = 2, replace x with x + y_i.

Takahashi may skip any number between 0 and K (inclusive) of the operations. When he performs the remaining operations once each without changing the order, find the maximum possible final value of x.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq K \leq N
  • t_i \in \{1,2\} \, (1 \leq i \leq N)
  • |y_i| \leq 10^9 \, (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
t_1 y_1
\vdots
t_N y_N

Output

Print the answer.


Sample Input 1

5 1
2 4
2 -3
1 2
2 1
2 -3

Sample Output 1

3

If he skips the 5-th operation, x changes as 0 \rightarrow 4 \rightarrow 1 \rightarrow 2 \rightarrow 3, so x results in 3. This is the maximum.


Sample Input 2

1 0
2 -1000000000

Sample Output 2

-1000000000

Sample Input 3

10 3
2 3
2 -1
1 4
2 -1
2 5
2 -9
2 2
1 -6
2 5
2 -3

Sample Output 3

15