A - Count .

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

配点 : 100

問題文

英小文字からなる文字列 S が与えられます。

ここで、英小文字に含まれるドットの個数を以下のようにして定めます。

  • 英小文字が i または j であるとき : 1
  • 英小文字が i, j のいずれでもないとき : 0

S のすべての文字に対するドットの個数の和を求めてください。

制約

  • S は長さ 1 以上 10 以下の英小文字からなる文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

aiiaj

出力例 1

3

a, i, i, a, j に含まれるドットの個数はそれぞれ 0, 1, 1, 0, 1 です。

0 + 1 + 1 + 0 + 1 = 3 であるため、3 を出力します。


入力例 2

abcedfgh

出力例 2

0

入力例 3

jjjjjj

出力例 3

6

Score : 100 points

Problem Statement

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

Here, we define the number of dots in a lowercase English letter as follows:

  • If the lowercase English letter is i or j: 1 dot
  • If the lowercase English letter is neither i nor j: 0 dots

Find the sum of the numbers of dots over all characters in S.

Constraints

  • S is a string consisting of lowercase English letters with length between 1 and 10, inclusive.

Input

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

S

Output

Print the answer.


Sample Input 1

aiiaj

Sample Output 1

3

The numbers of dots in a, i, i, a, j are 0, 1, 1, 0, 1, respectively.

We have 0 + 1 + 1 + 0 + 1 = 3, so print 3.


Sample Input 2

abcedfgh

Sample Output 2

0

Sample Input 3

jjjjjj

Sample Output 3

6
B - Music Player

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

配点 : 200

問題文

高橋君は音楽プレイヤーを持っています。はじめ、音量は 0 であり、曲は停止中です。

これから、Q 回の操作を順に行います。i 回目の操作は整数 A_i によって表され、操作の内容は以下の通りです。

  • A_i = 1 のとき、音量を 1 上げる。
  • A_i = 2 のとき、現在の音量が 1 以上であれば音量を 1 下げ、0 であれば何もしない。
  • A_i = 3 のとき、曲が停止中であれば曲を再生し、曲が再生中であれば曲を停止する。

i = 1, 2, \ldots, Q に対して、以下の問題を解いてください。

  • i 回目の操作を終えた直後に音量 3 以上で音楽が再生されているか判定せよ。

制約

  • 1 \leq Q \leq 2 \times 10^5
  • A_i \in \lbrace 1, 2, 3 \rbrace
  • 入力される値はすべて整数

入力

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

Q
A_1
A_2
\vdots
A_Q

出力

Q 行出力せよ。i 行目には、i 回目の操作を終えた直後に音量 3 以上で音楽が再生されているならば Yes を、そうでないならば No を出力せよ。


入力例 1

10
2
1
3
1
3
1
1
3
2
2

出力例 1

No
No
No
No
No
No
No
Yes
Yes
No
  • 1 回目の操作を終えた後、音量は 0 で曲は停止中です。

  • 2 回目の操作を終えた後、音量は 1 で曲は停止中です。

  • 3 回目の操作を終えた後、音量は 1 で曲は再生中です。

  • 4 回目の操作を終えた後、音量は 2 で曲は再生中です。

  • 5 回目の操作を終えた後、音量は 2 で曲は停止中です。

  • 6 回目の操作を終えた後、音量は 3 で曲は停止中です。

  • 7 回目の操作を終えた後、音量は 4 で曲は停止中です。

  • 8 回目の操作を終えた後、音量は 4 で曲は再生中です。

  • 9 回目の操作を終えた後、音量は 3 で曲は再生中です。

  • 10 回目の操作を終えた後、音量は 2 で曲は再生中です。

Score : 200 points

Problem Statement

Takahashi has a music player. Initially, the volume is 0 and the music is stopped.

From now on, Q operations will be performed in order. The i-th operation is represented by an integer A_i, which means the following:

  • If A_i = 1, increase the volume by 1.
  • If A_i = 2, if the current volume is 1 or more, decrease it by 1; if it is 0, do nothing.
  • If A_i = 3, if the music is stopped, play it; if the music is playing, stop it.

For i = 1, 2, \ldots, Q, solve the following problem:

  • Determine whether the music is playing at volume 3 or more immediately after the i-th operation.

Constraints

  • 1 \leq Q \leq 2 \times 10^5
  • A_i \in \lbrace 1, 2, 3 \rbrace
  • All input values are integers.

Input

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

Q
A_1
A_2
\vdots
A_Q

Output

Output Q lines. The i-th line should contain Yes if the music is playing at volume 3 or more immediately after the i-th operation, and No otherwise.


Sample Input 1

10
2
1
3
1
3
1
1
3
2
2

Sample Output 1

No
No
No
No
No
No
No
Yes
Yes
No
  • After the 1-st operation, the volume is 0 and the music is stopped.

  • After the 2-nd operation, the volume is 1 and the music is stopped.

  • After the 3-rd operation, the volume is 1 and the music is playing.

  • After the 4-th operation, the volume is 2 and the music is playing.

  • After the 5-th operation, the volume is 2 and the music is stopped.

  • After the 6-th operation, the volume is 3 and the music is stopped.

  • After the 7-th operation, the volume is 4 and the music is stopped.

  • After the 8-th operation, the volume is 4 and the music is playing.

  • After the 9-th operation, the volume is 3 and the music is playing.

  • After the 10-th operation, the volume is 2 and the music is playing.

C - Peer Review

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

配点 : 300

問題文

N 人の研究者がおり、研究者には 1, 2, \ldots, N の番号が付けられています。

研究者の間には M 個の利害関係があり、i = 1, 2, \ldots, M に対して研究者 A_i と研究者 B_i は互いに利害関係にあります。

論文の査読者は、その論文の著者とは異なり、著者と利害関係にない相異なる 3 人の研究者である必要があります。

i = 1, 2, \ldots, N について以下の問題を解いてください。

  • 研究者 i が著者である論文の査読者の 3 人組として考えられるものは何通りあるか求めよ。

ただし、すべての論文は単著であるものとします。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • i \neq j のとき (A_i, B_i) \neq (A_j, B_j)
  • i \neq j のとき (A_i, B_i) \neq (B_j, A_j)
  • 入力される値はすべて整数

入力

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

出力

i = 1, 2, \ldots, N に対する答えをこの順に空白区切りで出力せよ。


入力例 1

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

出力例 1

0 1 0 4 4 10

以下、研究者の番号の集合により研究者の集合を表します。

  • 研究者 1 が著者である論文の査読者の 3 人組として考えられるものはありません。

  • 研究者 2 が著者である論文の査読者の 3 人組として考えられるものは \lbrace 4, 5, 6 \rbrace1 通りです。

  • 研究者 3 が著者である論文の査読者の 3 人組として考えられるものはありません。

  • 研究者 4 が著者である論文の査読者の 3 人組として考えられるものは \lbrace 2, 3, 5 \rbrace, \lbrace 2, 3, 6 \rbrace, \lbrace 2, 5, 6 \rbrace, \lbrace 3, 5, 6 \rbrace4 通りです。

  • 研究者 5 が著者である論文の査読者の 3 人組として考えられるものは \lbrace 1, 2, 4 \rbrace, \lbrace 1, 2, 6 \rbrace, \lbrace 1, 4, 6 \rbrace, \lbrace 2, 4, 6 \rbrace4 通りです。

  • 研究者 6 が著者である論文の査読者の 3 人組として考えられるものは \lbrace 1, 2, 3 \rbrace, \lbrace 1, 2, 4 \rbrace, \lbrace 1, 2, 5 \rbrace, \lbrace 1, 3, 4 \rbrace, \lbrace 1, 3, 5 \rbrace, \lbrace 1, 4, 5 \rbrace, \lbrace 2, 3, 4 \rbrace, \lbrace 2, 3, 5 \rbrace, \lbrace 2, 4, 5 \rbrace, \lbrace 3, 4, 5 \rbrace10 通りです。


入力例 2

7 3
1 2
3 4
5 6

出力例 2

10 10 10 10 10 10 20

入力例 3

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

出力例 3

1 0 0 1 0 1

Score : 300 points

Problem Statement

There are N researchers, numbered 1, 2, \ldots, N.

There are M conflicts of interest among the researchers; for i = 1, 2, \ldots, M, researchers A_i and B_i have a conflict of interest with each other.

The reviewers of a paper must be three distinct researchers who are different from the author of the paper and have no conflict of interest with the author.

For i = 1, 2, \ldots, N, solve the following problem:

  • Find the number of possible triples of reviewers for a paper authored by researcher i.

Assume that all papers are single-authored.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq 2 \times 10^5
  • 1 \leq A_i, B_i \leq N
  • A_i \neq B_i
  • (A_i, B_i) \neq (A_j, B_j) if i \neq j.
  • (A_i, B_i) \neq (B_j, A_j) if i \neq j.
  • All input values are integers.

Input

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

N M
A_1 B_1
A_2 B_2
\vdots
A_M B_M

Output

Print the answers for i = 1, 2, \ldots, N in this order, separated by spaces.


Sample Input 1

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

Sample Output 1

0 1 0 4 4 10

Below, we represent a set of researchers by the set of their numbers.

  • There are no possible triples of reviewers for a paper authored by researcher 1.

  • The possible triple of reviewers for a paper authored by researcher 2 is \lbrace 4, 5, 6 \rbrace, which is 1 triple.

  • There are no possible triples of reviewers for a paper authored by researcher 3.

  • The possible triples of reviewers for a paper authored by researcher 4 are \lbrace 2, 3, 5 \rbrace, \lbrace 2, 3, 6 \rbrace, \lbrace 2, 5, 6 \rbrace, \lbrace 3, 5, 6 \rbrace, which is 4 triples.

  • The possible triples of reviewers for a paper authored by researcher 5 are \lbrace 1, 2, 4 \rbrace, \lbrace 1, 2, 6 \rbrace, \lbrace 1, 4, 6 \rbrace, \lbrace 2, 4, 6 \rbrace, which is 4 triples.

  • The possible triples of reviewers for a paper authored by researcher 6 are \lbrace 1, 2, 3 \rbrace, \lbrace 1, 2, 4 \rbrace, \lbrace 1, 2, 5 \rbrace, \lbrace 1, 3, 4 \rbrace, \lbrace 1, 3, 5 \rbrace, \lbrace 1, 4, 5 \rbrace, \lbrace 2, 3, 4 \rbrace, \lbrace 2, 3, 5 \rbrace, \lbrace 2, 4, 5 \rbrace, \lbrace 3, 4, 5 \rbrace, which is 10 triples.


Sample Input 2

7 3
1 2
3 4
5 6

Sample Output 2

10 10 10 10 10 10 20

Sample Input 3

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

Sample Output 3

1 0 0 1 0 1
D - Swap and Range Sum

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

配点 : 400

問題文

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

Q 個のクエリが与えられるので、順に処理してください。 各クエリは以下のいずれかの形式です。

  • 1 x : A_xA_{x+1} の値を入れ替える。
  • 2 l r : \displaystyle \sum_{l\leq i\leq r} A_i の値を求める。

制約

  • 2\leq N \leq 2\times 10^5
  • 1\leq Q \leq 5\times 10^5
  • 1\leq A_i \leq 10^4
  • 1 種類目のクエリについて、1\leq x \leq N-1
  • 2 種類目のクエリについて、1\leq l\leq r \leq N
  • 入力は全て整数

入力

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

N Q
A_1 A_2 \dots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ここで、\text{query}_ii 番目のクエリを表し、以下のいずれかの形式で与えられる。

1 x
2 l r

出力

2 種類目のクエリの個数を q として、q 行出力せよ。 i 行目 (1\leq i \leq q) には、2 種類目のクエリのうち i 個目のものに対する答えを出力せよ。


入力例 1

4 4
2 7 1 8
1 2
2 1 2
1 1
2 2 4

出力例 1

3
17
  • 1 番目のクエリでは、A_2A_3 の値を入れ替えます。これにより、A=(2,1,7,8) になります。
  • 2 番目のクエリでは、A_1+A_2 の値を求めます。答えは 2+1=3 です。
  • 3 番目のクエリでは、A_1A_2 の値を入れ替えます。これにより、A=(1,2,7,8) になります。
  • 4 番目のクエリでは、A_2+A_3+A_4 の値を求めます。答えは 2+7+8=17 です。

入力例 2

8 10
22 75 26 45 72 81 47 29
2 2 7
2 6 8
2 4 4
1 2
2 1 3
1 1
2 2 4
1 2
1 4
2 1 1

出力例 2

346
157
45
123
142
26

Score : 400 points

Problem Statement

You are given a sequence A=(A_1,A_2,\dots,A_N) of length N.

Process Q queries in order. Each query is in one of the following formats:

  • 1 x : Swap the values of A_x and A_{x+1}.
  • 2 l r : Find the value of \displaystyle \sum_{l\leq i\leq r} A_i.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 1\leq Q \leq 5\times 10^5
  • 1\leq A_i \leq 10^4
  • For queries of the first type, 1\leq x \leq N-1.
  • For queries of the second type, 1\leq l\leq r \leq N.
  • All input values are integers.

Input

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

N Q
A_1 A_2 \dots A_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Here, \text{query}_i represents the i-th query and is given in one of the following formats:

1 x
2 l r

Output

Let q be the number of queries of the second type, and output q lines. The i-th line (1\leq i \leq q) should contain the answer to the i-th query of the second type.


Sample Input 1

4 4
2 7 1 8
1 2
2 1 2
1 1
2 2 4

Sample Output 1

3
17
  • In the 1-st query, swap the values of A_2 and A_3. This makes A=(2,1,7,8).
  • In the 2-nd query, find the value of A_1+A_2. The answer is 2+1=3.
  • In the 3-rd query, swap the values of A_1 and A_2. This makes A=(1,2,7,8).
  • In the 4-th query, find the value of A_2+A_3+A_4. The answer is 2+7+8=17.

Sample Input 2

8 10
22 75 26 45 72 81 47 29
2 2 7
2 6 8
2 4 4
1 2
2 1 3
1 1
2 2 4
1 2
1 4
2 1 1

Sample Output 2

346
157
45
123
142
26
E - Laser Takahashi

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

配点 : 450

問題文

二次元平面上に N 体のモンスターがいます。 モンスターには 1 から N までの番号が付けられており、モンスター i がいる場所の座標は (X_i,Y_i) です。 ここで、(X_i,Y_i) \neq (0,0) です。 (なお、各モンスターは静止した点としてみなせるものとします。すなわち、モンスターは大きさを持ちません。)

この平面上の原点には高橋君が立っています。 高橋君の目からは強力なレーザーが常に照射されており、高橋君が向いている方向に存在するモンスターは即座に消滅します。 高橋君が向いている方向に複数のモンスターが存在する場合も、その全てが即座に消滅します。

青木君は、Q 個の 独立な 思考実験を行なっています。 j 個目の思考実験は以下のようなものです。

  • はじめ、高橋君はモンスター A_j がいる方向を向いている。今から高橋君は 時計回り に回転を行い、モンスター B_j がいる方向を向いた瞬間に停止する。 このとき、(モンスター A_j,B_j を含め)合計で何体のモンスターが消滅するか?なお、モンスター A_j,B_j が原点から見て同じ方向に存在する場合、高橋君は一切回転しない。

各思考実験に対する答えを求めてください。

制約

  • 2\leq N \leq 2\times 10^5
  • 1\leq Q \leq 2\times 10^5
  • -10^9\leq X_i,Y_i \leq 10^9
  • (X_i,Y_i)\neq (0,0)
  • 1\leq A_j,B_j\leq N
  • A_j\neq B_j
  • 入力は全て整数

入力

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

N Q
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

出力

Q 行出力せよ。 j 行目 (1\leq j \leq Q) には、j 個目の思考実験に対する答えを出力せよ。


入力例 1

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

出力例 1

2
5
4
2

  • 1 個目の思考実験:はじめ、高橋君はモンスター 4 がいる方向を向いています(このとき、モンスター 4 が消滅します)。 ここから時計回りに回転を続け、モンスター 1 がいる方向を向いた瞬間に停止します(このとき、モンスター 1 が消滅します)。 これら以外のモンスターがいる方向を向くことはないため、答えは 2 です。
  • 2 個目の思考実験:はじめ、高橋君はモンスター 1 がいる方向を向いています(このとき、モンスター 1 が消滅します)。 ここから時計回りに回転を続けると、途中モンスター 3,5 がいる方向を向くため、これらが消滅します。 さらに回転を続けると、途中モンスター 2 がいる方向を向くため、これが消滅します。 最終的にモンスター 4 がいる方向を向いた瞬間に停止します(このとき、モンスター 4 が消滅します)。 よって、答えは 5 です。
  • 3 個目の思考実験:モンスター 3,5,2,4 が消滅するため、答えは 4 です。
  • 4 個目の思考実験:モンスター 3,5 が消滅するため、答えは 2 です。 なお、モンスター 3,5 は原点から見て同じ方向に存在するため、高橋君は一切回転しないことに注意してください。

入力例 2

2 1
1 2
1 2
1 2

出力例 2

2

同じ座標に複数のモンスターが存在することもあります。


入力例 3

8 10
-84 -60
-100 8
77 55
-14 -10
50 -4
-63 -45
26 -17
-7 -5
3 7
2 4
8 4
8 4
7 1
1 7
6 3
4 7
4 5
2 6

出力例 3

3
8
4
4
5
8
6
8
7
8

Score : 450 points

Problem Statement

There are N monsters on a two-dimensional plane. The monsters are numbered from 1 to N, and the coordinates of monster i are (X_i,Y_i). Here, (X_i,Y_i) \neq (0,0). (Each monster can be regarded as a stationary point. That is, monsters have no size.)

Takahashi is standing at the origin of this plane. A powerful laser is always emitted from his eyes, instantly destroying monsters in the direction he is facing. If multiple monsters exist in the direction he is facing, all of them are instantly destroyed.

Aoki is conducting Q independent thought experiments. The j-th thought experiment is as follows:

  • Initially, Takahashi is facing the direction toward monster A_j. From now on, Takahashi will rotate clockwise and stop the moment he faces the direction toward monster B_j. Here, how many monsters in total (including monsters A_j and B_j) will be destroyed? If monsters A_j and B_j exist in the same direction from the origin, Takahashi does not rotate at all.

Find the answer to each thought experiment.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 1\leq Q \leq 2\times 10^5
  • -10^9\leq X_i,Y_i \leq 10^9
  • (X_i,Y_i)\neq (0,0)
  • 1\leq A_j,B_j\leq N
  • A_j\neq B_j
  • All input values are integers.

Input

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

N Q
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N
A_1 B_1
A_2 B_2
\vdots
A_Q B_Q

Output

Output Q lines. The j-th line (1\leq j \leq Q) should contain the answer to the j-th thought experiment.


Sample Input 1

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

Sample Output 1

2
5
4
2

  • 1-st thought experiment: Initially, Takahashi is facing the direction toward monster 4 (at this time, monster 4 is destroyed). From here, he continues to rotate clockwise and stops the moment he faces the direction toward monster 1 (at this time, monster 1 is destroyed). Since he does not face the direction toward any other monsters, the answer is 2.
  • 2-nd thought experiment: Initially, Takahashi is facing the direction toward monster 1 (at this time, monster 1 is destroyed). From here, as he continues to rotate clockwise, he faces the direction toward monsters 3 and 5 along the way, so they are destroyed. As he continues to rotate further, he faces the direction toward monster 2 along the way, so it is destroyed. Finally, he stops the moment he faces the direction toward monster 4 (at this time, monster 4 is destroyed). Therefore, the answer is 5.
  • 3-rd thought experiment: Monsters 3, 5, 2, 4 are destroyed, so the answer is 4.
  • 4-th thought experiment: Monsters 3, 5 are destroyed, so the answer is 2. Note that since monsters 3 and 5 exist in the same direction from the origin, Takahashi does not rotate at all.

Sample Input 2

2 1
1 2
1 2
1 2

Sample Output 2

2

Multiple monsters may exist at the same coordinates.


Sample Input 3

8 10
-84 -60
-100 8
77 55
-14 -10
50 -4
-63 -45
26 -17
-7 -5
3 7
2 4
8 4
8 4
7 1
1 7
6 3
4 7
4 5
2 6

Sample Output 3

3
8
4
4
5
8
6
8
7
8
F - Diagonal Separation 2

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

配点 : 500

問題文

NN 列のグリッドがあります。グリッドの上から i 行目、左から j 列目のマスをマス (i, j) と表記します。

グリッドの各マスは白または黒に塗られています。グリッドの情報は N 個の文字列 S_1, S_2, \ldots, S_N によって与えられ、S_ij 文字目が . のときマス (i, j) は白く塗られており、S_ij 文字目が # のときマス (i, j) は黒く塗られています。

あなたは、いくつかのマスの色を塗り替えて以下の条件をともに満たすようにします。

  • すべての行に対して以下の条件が成り立つ。
    • 0 \leq k \leq N を満たす整数 k が存在して、その行の左から k 個のマスは白く塗られており、その他のマスは黒く塗られている。
  • すべての列に対して以下の条件が成り立つ。
    • 0 \leq k \leq N を満たす整数 k が存在して、その列の上から k 個のマスは白く塗られており、その他のマスは黒く塗られている。

条件を満たすために塗り替える必要のあるマスの数として考えられる最小値を求めてください。

制約

  • 1 \leq N \leq 5000
  • N は整数
  • S_i., # からなる長さ N の文字列

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

3
..#
#.#
.#.

出力例 1

2

マス (2, 1) の色を白に、マス (3, 3) の色を黒に塗り替えることで条件を満たすことができます。


入力例 2

5
..#.#
#..##
###.#
.###.
#....

出力例 2

9

Score : 500 points

Problem Statement

There is a grid with N rows and N columns. The square at the i-th row from the top and j-th column from the left is denoted as square (i, j).

Each square in the grid is painted white or black. The information of the grid is given by N strings S_1, S_2, \ldots, S_N. If the j-th character of S_i is ., square (i, j) is painted white; if the j-th character of S_i is #, square (i, j) is painted black.

You will repaint some squares so that both of the following conditions are satisfied:

  • For every row, the following condition holds:
    • There exists an integer k satisfying 0 \leq k \leq N such that the leftmost k squares of that row are painted white and the other squares are painted black.
  • For every column, the following condition holds:
    • There exists an integer k satisfying 0 \leq k \leq N such that the topmost k squares of that column are painted white and the other squares are painted black.

Find the minimum number of squares that need to be repainted to satisfy the conditions.

Constraints

  • 1 \leq N \leq 5000
  • N is an integer.
  • S_i is a string of length N consisting of . and #.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

3
..#
#.#
.#.

Sample Output 1

2

You can satisfy the conditions by repainting square (2, 1) white and square (3, 3) black.


Sample Input 2

5
..#.#
#..##
###.#
.###.
#....

Sample Output 2

9
G - Lightweight Knapsack

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

配点 : 600

問題文

N 種類のアイテムがあります。 i 種類目のアイテムの 重みW_i価値V_i であり、あなたはこれを K_i 個持っています。

これらの K_1+\dots+K_N 個のアイテムの中から、重みの総和が C を超えないようにいくつか(0 個でもよい)を選ぶとき、選んだアイテムの価値の総和が最大でいくつになるか求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • 1\leq C \leq 2\times 10^9
  • 1\leq W_i \leq 3
  • 1\leq V_i \leq 10^9
  • 1\leq K_i \leq 10^9
  • 入力は全て整数

入力

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

N C
W_1 V_1 K_1
W_2 V_2 K_2
\vdots
W_N V_N K_N

出力

答えを出力せよ。


入力例 1

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

出力例 1

16

1 種類目のアイテムを 1 個、2 種類目のアイテムを 2 個、3 種類目のアイテムを 1 個選ぶと、重みの総和は 3\times 1+1\times 2+2\times 1=7\ (\leq C)、 価値の総和は 5\times 1+2\times 2+7\times 1=16 となり、これが最大です。


入力例 2

2 1
3 442 442
2 442 442

出力例 2

0

アイテムを一つも選ぶことができません。


入力例 3

15 913575467
1 60505998 818008580
2 121011861 138996221
3 181517958 501899080
1 60506027 840594328
3 181517875 350034067
1 60505924 155374934
3 181517816 910748511
1 60506042 545531545
3 181517877 797829355
3 181517837 164163676
1 60505894 353195922
1 60505912 954291757
1 60506022 160449218
3 181517873 404011431
1 60506043 782177068

出力例 3

55276836358648682

Score : 600 points

Problem Statement

There are N types of items. The i-th type of item has weight W_i and value V_i, and you have K_i of them.

When choosing some (possibly zero) items from these K_1+\dots+K_N items so that the total weight does not exceed C, find the maximum possible total value of the chosen items.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\leq C \leq 2\times 10^9
  • 1\leq W_i \leq 3
  • 1\leq V_i \leq 10^9
  • 1\leq K_i \leq 10^9
  • All input values are integers.

Input

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

N C
W_1 V_1 K_1
W_2 V_2 K_2
\vdots
W_N V_N K_N

Output

Print the answer.


Sample Input 1

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

Sample Output 1

16

If you choose 1 item of the 1-st type, 2 items of the 2-nd type, and 1 item of the 3-rd type, the total weight is 3\times 1+1\times 2+2\times 1=7\ (\leq C) and the total value is 5\times 1+2\times 2+7\times 1=16, which is the maximum.


Sample Input 2

2 1
3 442 442
2 442 442

Sample Output 2

0

You cannot choose any items.


Sample Input 3

15 913575467
1 60505998 818008580
2 121011861 138996221
3 181517958 501899080
1 60506027 840594328
3 181517875 350034067
1 60505924 155374934
3 181517816 910748511
1 60506042 545531545
3 181517877 797829355
3 181517837 164163676
1 60505894 353195922
1 60505912 954291757
1 60506022 160449218
3 181517873 404011431
1 60506043 782177068

Sample Output 3

55276836358648682