A - Chord

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英大文字からなる長さ 3 の文字列 S が与えられます。SACEBDFCEGDFAEGBFACGBD のいずれかと等しいとき Yes を、そうでないとき No を出力してください。

制約

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

入力

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

S

出力

SACEBDFCEGDFAEGBFACGBD のいずれかと等しいとき Yes を、そうでないとき No を出力せよ。


入力例 1

ABC

出力例 1

No

S = ABC のとき、SACEBDFCEGDFAEGBFACGBD のいずれとも等しくないので No を出力します。


入力例 2

FAC

出力例 2

Yes

入力例 3

XYX

出力例 3

No

Score : 100 points

Problem Statement

Given a length-3 string S consisting of uppercase English letters, print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.

Constraints

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

Input

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

S

Output

Print Yes if S equals one of ACE, BDF, CEG, DFA, EGB, FAC, and GBD; print No otherwise.


Sample Input 1

ABC

Sample Output 1

No

When S = ABC, S does not equal any of ACE, BDF, CEG, DFA, EGB, FAC, and GBD, so No should be printed.


Sample Input 2

FAC

Sample Output 2

Yes

Sample Input 3

XYX

Sample Output 3

No
B - New Scheme

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

8 個の整数 S_1,S_2,\dots,S_8 が与えられます。 以下の 3 つの条件が全て満たされるならば Yes を、そうでないならば No を出力してください。

  • 数列 (S_1,S_2,\dots,S_8) は広義単調増加である。すなわち、S_1 \leq S_2 \leq \dots \leq S_8 である。
  • S_1,S_2,\dots,S_8 は全て 100 以上 675 以下である。
  • S_1,S_2,\dots,S_8 は全て 25 の倍数である。

制約

  • 0\leq S_i \leq 1000
  • 入力は全て整数

入力

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

S_1 S_2 \dots S_8

出力

答えを出力せよ。


入力例 1

125 175 250 300 400 525 600 650

出力例 1

Yes

3 つの条件を全て満たしています。


入力例 2

100 250 300 400 325 575 625 675

出力例 2

No

S_4 > S_5 であるため、1 つ目の条件を満たしていません。


入力例 3

0 23 24 145 301 413 631 632

出力例 3

No

2 つ目の条件と 3 つ目の条件を満たしていません。

Score : 100 points

Problem Statement

Given eight integers S_1,S_2,\dots, and S_8, print Yes if they satisfy all of the following three conditions, and No otherwise.

  • The sequence (S_1,S_2,\dots,S_8) is monotonically non-decreasing. In other words, S_1 \leq S_2 \leq \dots \leq S_8.
  • S_1,S_2,\dots, and S_8 are all between 100 and 675, inclusive.
  • S_1,S_2,\dots, and S_8 are all multiples of 25.

Constraints

  • 0\leq S_i \leq 1000
  • All input values are integers.

Input

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

S_1 S_2 \dots S_8

Output

Print the answer.


Sample Input 1

125 175 250 300 400 525 600 650

Sample Output 1

Yes

They satisfy all of the three conditions.


Sample Input 2

100 250 300 400 325 575 625 675

Sample Output 2

No

They violate the first condition because S_4 > S_5.


Sample Input 3

0 23 24 145 301 413 631 632

Sample Output 3

No

They violate the second and third conditions.

C - Tournament Result

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 人の人が総当り戦の試合をしました。

NN 列からなる試合の結果の表 A が与えられます。Ai 行目 j 列目の要素を A_{i,j} と表します。
A_{i,j}i=j のとき - であり、それ以外のとき W, L, D のいずれかです。
A_{i,j}W, L, D であることは、人 i が人 j との試合に勝った、負けた、引き分けたことをそれぞれ表します。

与えられた表に矛盾があるかどうかを判定してください。

次のいずれかが成り立つとき、与えられた表には矛盾があるといいます。

  • ある組 (i,j) が存在して、人 i が人 j に勝ったが、人 j が人 i に負けていない
  • ある組 (i,j) が存在して、人 i が人 j に負けたが、人 j が人 i に勝っていない
  • ある組 (i,j) が存在して、人 i が人 j に引き分けたが、人 j が人 i に引き分けていない

制約

  • 2 \leq N \leq 1000
  • A_{i,i}- である
  • i\neq j のとき、A_{i,j}W, L, D のいずれかである

入力

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

N
A_{1,1}A_{1,2}\ldots A_{1,N}
A_{2,1}A_{2,2}\ldots A_{2,N}
\vdots
A_{N,1}A_{N,2}\ldots A_{N,N}

出力

与えられた表に矛盾がないとき correct、矛盾があるとき incorrect と出力せよ。


入力例 1

4
-WWW
L-DD
LD-W
LDW-

出力例 1

incorrect

3 が人 4 に勝ったにもかかわらず、人 4 も人 3 に勝ったことになっており、矛盾しています。


入力例 2

2
-D
D-

出力例 2

correct

矛盾はありません。

Score : 200 points

Problem Statement

N players played a round-robin tournament.

You are given an N-by-N table A containing the results of the matches. Let A_{i,j} denote the element at the i-th row and j-th column of A.
A_{i,j} is - if i=j, and W, L, or D otherwise.
A_{i,j} is W if Player i beat Player j, L if Player i lost to Player j, and D if Player i drew with Player j.

Determine whether the given table is contradictory.

The table is said to be contradictory when some of the following holds:

  • There is a pair (i,j) such that Player i beat Player j, but Player j did not lose to Player i;
  • There is a pair (i,j) such that Player i lost to Player j, but Player j did not beat Player i;
  • There is a pair (i,j) such that Player i drew with Player j, but Player j did not draw with Player i.

Constraints

  • 2 \leq N \leq 1000
  • A_{i,i} is -.
  • A_{i,j} is W, L, or D, for i\neq j.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}A_{1,2}\ldots A_{1,N}
A_{2,1}A_{2,2}\ldots A_{2,N}
\vdots
A_{N,1}A_{N,2}\ldots A_{N,N}

Output

If the given table is not contradictory, print correct; if it is contradictory, print incorrect.


Sample Input 1

4
-WWW
L-DD
LD-W
LDW-

Sample Output 1

incorrect

Player 3 beat Player 4, while Player 4 also beat Player 3, which is contradictory.


Sample Input 2

2
-D
D-

Sample Output 2

correct

There is no contradiction.

D - Misjudge the Time

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君は置き時計を買いました。
この時計は、現在の時刻が 24 時制で \mathrm{AB}\mathrm{CD} 分であるときに図 1 のように時刻を表します。
例えば図 2 では、時計は 758 分を示しています。

時刻の表示方法をより形式的に説明すると次のようになります。
現在の時刻が 24 時制で hm 分であるとします。ここで 24 時制とは、時間を 0 以上 23 以下の整数で、分を 0 以上 59 以下の整数で表す時刻の表現方法を言います。
h10 の位を A, 1 の位を B, m10 の位を C, 1 の位を D とします。(ただし h, m1 桁である場合は先行ゼロを追加して考えます。)
このとき時計は左上に A を、左下に B を、右上に C を、右下に D を表示します。

image

高橋君は、次の条件を満たす時刻を 見間違えやすい時刻 と呼ぶことにしました。

  • 時計の表示の右上と左下を入れ替えても、それに対応する 24 時制の時刻が存在する。

例えば 図 32013 分を示していますが、時計の表示の右上と左下を入れ替えると 213 分を意味する表示になります。よって 2013 分は見間違えやすい時刻です。

今、時計は HM 分を示しています。
(現時点も含めて)以降はじめて訪れる見間違えやすい時刻を 24 時制で答えてください。

制約

  • 0 \leq H \leq 23
  • 0 \leq M \leq 59
  • H, M は整数

入力

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

H M

出力

答えを hm 分とする。ここで h, m0 \leq h \leq 23, 0 \leq m \leq 59 である必要がある。
このとき h, m を以下の形式で出力せよ。

h m

なお、h, m2 桁に揃えるために先行ゼロをつけた形式で出力しても正答と見なされる。


入力例 1

1 23

出力例 1

1 23

123 分は見間違えやすい時刻です。なぜならば、時計の表示の右上と左下を入れ替えると 213 分を意味する表示になるからです。
よって答えは 123 分です。
なお、01 23 のように先行ゼロをつけた形式で出力しても正答として扱われます。


入力例 2

19 57

出力例 2

20 0

1957 分以降ではじめて訪れる見間違えやすい時刻は 200 分です。


入力例 3

20 40

出力例 3

21 0

24 時制では 240 分という表記は合法でないのに注意してください。

Score : 200 points

Problem Statement

Takahashi bought a table clock.
The clock shows the time as shown in Figure 1 at \mathrm{AB}:\mathrm{CD} in the 24-hour system.
For example, the clock in Figure 2 shows 7:58.

The format of the time is formally described as follows.
Suppose that the current time is m minutes past h in the 24-hour system. Here, the 24-hour system represents the hour by an integer between 0 and 23 (inclusive), and the minute by an integer between 0 and 59 (inclusive).
Let A be the tens digit of h, B be the ones digit of h, C be the tens digit of m, and D be the ones digit of m. (Here, if h has only one digit, we consider that it has a leading zero; the same applies to m.)
Then, the clock shows A in its top-left, B in its bottom-left, C in its top-right, and D in its bottom-right.

image

Takahashi has decided to call a time a confusing time if it satisfies the following condition:

  • after swapping the top-right and bottom-left digits on the clock, it still reads a valid time in the 24-hour system.

For example, the clock in Figure 3 shows 20:13. After swapping its top-right and bottom-left digits, it reads 21:03. Thus, 20:13 is a confusing time.

The clock now shows H:M.
Find the next confusing time (including now) in the 24-hour system.

Constraints

  • 0 \leq H \leq 23
  • 0 \leq M \leq 59
  • H and M are integers.

Input

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

H M

Output

Let h:m be the answer, where h and m must satisfy 0 \leq h \leq 23 and 0 \leq m \leq 59.
Print h and m in the following format:

h m

Your answer is considered correct even if h contains a leading zero to represent it as a 2-digit integer; the same applies to m.


Sample Input 1

1 23

Sample Output 1

1 23

1:23 is a confusing time because, after swapping its top-right and bottom-left digits on the clock, it reads 2:13.
Thus, the answer is 1:23.
Your answer is considered correct even if you print 01 23 with a leading zero.


Sample Input 2

19 57

Sample Output 2

20 0

The next confusing time after 19:57 is 20:00.


Sample Input 3

20 40

Sample Output 3

21 0

Note that 24:00 is an invalid notation in the 24-hour system.

E - Select Mul

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

整数 N が与えられます。N の各桁の数字を取り出して並べ(並べる順序は好きに変えてよい)、2 つの正整数に分離することを考えましょう。

例えば、123 という整数に対しては以下の 6 通りの分離の仕方が考えられます。

  • 123
  • 213
  • 132
  • 312
  • 231
  • 321

なお、ここで分離されたあとの 2 整数に leading zero が含まれていてはなりません。例えば、101 という整数を 1012 つに分離することはできません。また上述の「正整数に分離する」という条件より、1011102 つに分離することもできません。

適切に N を分離したとき、分離後の 2 数の積の最大値はいくらになりますか?

制約

  • N1 以上 10^9 以下の整数
  • N には 0 でない桁が 2 つ以上含まれる

入力

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

N

出力

分離後の 2 数の積の最大値を出力せよ。


入力例 1

123

出力例 1

63

問題文中にある通り、以下の 6 通りの分離の仕方が考えられます。

  • 123
  • 213
  • 132
  • 312
  • 231
  • 321

積はそれぞれ 36, 63, 26, 62, 23, 32 であり、この中の最大値は 63 です。


入力例 2

1010

出力例 2

100

考えられる分離の仕方は以下の 2 通りです。

  • 1001
  • 1010

いずれの場合にも積は 100 となります。


入力例 3

998244353

出力例 3

939337176

Score : 300 points

Problem Statement

You are given an integer N. Consider permuting the digits in N and separate them into two positive integers.

For example, for the integer 123, there are six ways to separate it, as follows:

  • 12 and 3,
  • 21 and 3,
  • 13 and 2,
  • 31 and 2,
  • 23 and 1,
  • 32 and 1.

Here, the two integers after separation must not contain leading zeros. For example, it is not allowed to separate the integer 101 into 1 and 01. Additionally, since the resulting integers must be positive, it is not allowed to separate 101 into 11 and 0, either.

What is the maximum possible product of the two resulting integers, obtained by the optimal separation?

Constraints

  • N is an integer between 1 and 10^9 (inclusive).
  • N contains two or more digits that are not 0.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible product of the two integers after separation.


Sample Input 1

123

Sample Output 1

63

As described in Problem Statement, there are six ways to separate it:

  • 12 and 3,
  • 21 and 3,
  • 13 and 2,
  • 31 and 2,
  • 23 and 1,
  • 32 and 1.

The products of these pairs, in this order, are 36, 63, 26, 62, 23, 32, with 63 being the maximum.


Sample Input 2

1010

Sample Output 2

100

There are two ways to separate it:

  • 100 and 1,
  • 10 and 10.

In either case, the product is 100.


Sample Input 3

998244353

Sample Output 3

939337176
F - Martial artist

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

高橋君は武術家です。 武術家の覚えられる技は N 個あり、技 1, 2, \ldots, N と名前がついています。 1 \leq i \leq N について、技 i を習得するには時間 T_i の修練が必要で、 さらに、修練の開始時点で技 A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} をすでに習得している必要があります。 ここで、1 \leq j \leq K_i について、A_{i,j} < i であることが保証されます。

高橋君は時刻 0 の時点で技を 1 つも覚えていません。 高橋君は同時に 1 つの技に対する修練しかできず、また、一度始めた修練を途中でやめることもできません。 高橋君が技 N を習得するのに必要な時間の最小値を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq K_i < i
  • 1 \leq A_{i,j} < i
  • \sum_{i=1}^N K_i \leq 2\times 10^5
  • A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} はすべて異なる。
  • 入力は全て整数である。

入力

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

N
T_1 K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
T_2 K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
T_N K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}

出力

N を習得するのに必要な時間の最小値を出力せよ。


入力例 1

3
3 0
5 1 1
7 1 1

出力例 1

10

例えば高橋君は次のように行動することができます。

  • 高橋君は時刻 0 に技 1 の修練を開始し、時刻 3 に技 1 を習得します。
  • その後、時刻 3 に技 3 の修練を開始し、時刻 10 に技 3 を習得します。

このときが最短で、高橋君が技 3 を習得するのに必要な時間は 3+7=10 となります。 技 3 の習得のためには、技 2 を習得する必要はありません。


入力例 2

5
1000000000 0
1000000000 0
1000000000 0
1000000000 0
1000000000 4 1 2 3 4

出力例 2

5000000000

答えが 32 bit 整数に収まらないことがある事に注意してください。

Score : 300 points

Problem Statement

Takahashi is a martial artist. There are N moves that a martial artist can learn, called Move 1, 2, \ldots, N. For each 1 \leq i \leq N, it takes T_i minutes of practice to learn Move i. Additionally, at the beginning of that practice, all of the Moves A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} must be already learned. Here, it is guaranteed that A_{i,j} < i for each 1 \leq j \leq K_i.

Takahashi has not learned any move at time 0. He cannot practice for more than one move simultaneously, nor can he stop a practice he has already started. Find the minimum number of minutes needed for Takahashi to learn Move N.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq T_i \leq 10^9
  • 0 \leq K_i < i
  • 1 \leq A_{i,j} < i
  • \sum_{i=1}^N K_i \leq 2\times 10^5
  • A_{i,1}, A_{i,2}, \ldots, A_{i,K_i} are all distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
T_1 K_1 A_{1,1} A_{1,2} \ldots A_{1,K_1}
T_2 K_2 A_{2,1} A_{2,2} \ldots A_{2,K_2}
\vdots
T_N K_N A_{N,1} A_{N,2} \ldots A_{N,K_N}

Output

Print the minimum number of minutes needed for Takahashi to learn Move N.


Sample Input 1

3
3 0
5 1 1
7 1 1

Sample Output 1

10

Here is one possible plan for Takahashi.

  • At time 0, start practicing for Move 1 to learn Move 1 at time 3.
  • Then, at time 3, start practicing for Move 3 to learn Move 3 at time 10.

Here, Takahashi spends 3+7=10 minutes to learn Move 3, which is the fastest possible. Note that he does not need to learn Move 2 to learn Move 3.


Sample Input 2

5
1000000000 0
1000000000 0
1000000000 0
1000000000 0
1000000000 4 1 2 3 4

Sample Output 2

5000000000

Note that the answer may not fit into a 32-bit integer.

G - All Assign Point Add

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

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

Q 個のクエリが与えられるので、順番にすべて処理してください。 q 番目 (1\leq q\leq Q) のクエリは以下の 3 つのいずれかの形式で、それぞれ次のようなクエリを表します。

  • 1\ x _ qA のすべての要素に x _ q を代入する。
  • 2\ i _ q\ x _ qA _ {i _ q}x _ q を加える。
  • 3\ i _ qA _ {i _ q} の値を出力する。

制約

  • 1 \leq N \leq 2\times10^5
  • 1 \leq Q \leq 2\times10^5
  • 0 \leq A _ i \leq 10^9\ (1\leq i\leq N)
  • q 番目 (1\leq q\leq Q) のクエリが 2 番目もしくは 3 番目の形式のとき、1 \leq i _ q \leq N
  • q 番目 (1\leq q\leq Q) のクエリが 1 番目もしくは 2 番目の形式のとき、0 \leq x _ q \leq 10^9
  • 3 番目の形式のクエリが存在する
  • 入力される値はすべて整数

入力

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

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

ただし、\operatorname{query}_qq 番目のクエリであり、1 x, 2 i x, 3 i の形式のいずれかで与えられる。

出力

\operatorname{query}_q3 番目の形式であるような q\ (1\leq q\leq Q) の個数を X として、X 行出力せよ。 j\ (1\leq j\leq X) 行目にはそのようなクエリのうち j 番目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

1
8
5

はじめ、A=(3,1,4,1,5) です。 それぞれのクエリでは、以下のような処理が行われます。

  • A_2=1 なので、1 を出力します。
  • A_34 を加えます。A=(3,1,8,1,5) となります。
  • A_3=8 なので、8 を出力します。
  • A の要素すべてに 1 を代入します。A=(1,1,1,1,1) となります。
  • A_34 を加えます。A=(1,1,5,1,1) となります。
  • A_3=5 なので、5 を出力します。

入力例 2

1
1000000000
8
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
3 1

出力例 2

8000000000

A の要素の値が 32\operatorname{bit} 整数に収まらない可能性があることに注意してください。


入力例 3

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

出力例 3

7
5
7
21
21
19
10

Score : 400 points

Problem Statement

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

Given Q queries, process all of them in order. The q-th (1\leq q\leq Q) query is in one of the following three formats, which represents the following queries:

  • 1\ x _ q: assign x_q to every element of A.
  • 2\ i _ q\ x _ q: add x_q to A _ {i _ q}.
  • 3\ i _ q: print the value of A _ {i _ q}.

Constraints

  • 1 \leq N \leq 2\times10^5
  • 1 \leq Q \leq 2\times10^5
  • 0 \leq A _ i \leq 10^9\ (1\leq i\leq N)
  • If the q-th (1\leq q\leq Q) query is in the second or third format, 1 \leq i _ q \leq N.
  • If the q-th (1\leq q\leq Q) query is in the first or second format, 0 \leq x _ q \leq 10^9.
  • There exists a query in the third format.
  • All values in the input are integers.

Input

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

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

Here, \operatorname{query}_q denotes the q-th query, which is in one of following formats: 1 x, 2 i x, and 3 i.

Output

Print X lines, where X is the number of q's (1\leq q\leq Q) such that \operatorname{query}_q is in the third format. The j-th (1\leq j\leq X) line should contain the answer to the j-th such query.


Sample Input 1

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

Sample Output 1

1
8
5

Initially, A=(3,1,4,1,5). The queries are processed as follows:

  • A_2=1, so print 1.
  • Add 4 to A_3, making A=(3,1,8,1,5).
  • A_3=8, so print 8.
  • Assign 1 to every element of A, making A=(1,1,1,1,1).
  • Add 4 to A_3, making A=(1,1,5,1,1).
  • A_3=5, so print 5.

Sample Input 2

1
1000000000
8
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
2 1 1000000000
3 1

Sample Output 2

8000000000

Note that the elements of A may not fit into a 32-bit integer type.


Sample Input 3

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

Sample Output 3

7
5
7
21
21
19
10
H - Safety Journey

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

AtCoder国には N 個の都市があり、都市 1 , 都市 2 , \ldots , 都市 N と番号付けられています。 最初、どの 2 つの相異なる都市の間も双方向に通れる道で結ばれていましたが、老朽化が進み、これらのうち M 本の道が使えなくなってしまいました。具体的には 1\leq i \leq M について都市 U_i と都市 V_i を結ぶ道が使えなくなってしまいました。

いま、高橋君は都市 1 で始まり、都市 1 で終わる K 日間の旅をしようと考えました。都市 1 で始まり、都市 1 で終わる K 日間の旅とは、 K+1 個の都市の列 (A_0, A_1, \ldots, A_K) であって、A_0=A_K=1 をみたし、 0\leq i\leq K-1 について A_iA_{i+1} が相異なり、かつ都市 A_i と都市 A_{i+1} が現在も使用可能な道で結ばれているものを指します。

都市 1 で始まり、都市 1 で終わる K 日間の相異なる旅の数を 998244353 で割った余りを出力してください。ただし、 2 つの K 日間の旅 (A_0, A_1, \ldots, A_K)(B_0, B_1, \ldots, B_K) が相異なるとは、ある i が存在して A_i\neq B_i となることを言います。

制約

  • 2 \leq N \leq 5000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
  • 2 \leq K \leq 5000
  • 1 \leq U_i<V_i \leq N
  • (U_i, V_i) は全て互いに相異なる。
  • 入力は全て整数である。

入力

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

N M K
U_1 V_1
:
U_M V_M

出力

答えを出力せよ。


入力例 1

3 1 4
2 3

出力例 1

4

次のような 4 種類の旅が存在します。

  • (1,2,1,2,1)
  • (1,2,1,3,1)
  • (1,3,1,2,1)
  • (1,3,1,3,1)

これ以外に条件をみたすようなものは無いため、 4 を出力します。


入力例 2

3 3 3
1 2
1 3
2 3

出力例 2

0

使える道が 1 本も残っておらず、条件をみたすような旅は存在しません。


入力例 3

5 3 100
1 2
4 5
2 3

出力例 3

428417047

Score : 500 points

Problem Statement

The Republic of AtCoder has N cities, called City 1, City 2, \ldots, City N. Initially, there was a bidirectional road between every pair of different cities, but M of these roads have become unusable due to deterioration over time. More specifically, for each 1\leq i \leq M, the road connecting City U_i and City V_i has become unusable.

Takahashi will go for a K-day trip that starts and ends in City 1. Formally speaking, a K-day trip that starts and ends in City 1 is a sequence of K+1 cities (A_0, A_1, \ldots, A_K) such that A_0=A_K=1 holds and for each 0\leq i\leq K-1, A_i and A_{i+1} are different and there is still a usable road connecting City A_i and City A_{i+1}.

Print the number of different K-day trips that start and end in City 1, modulo 998244353. Here, two K-day trips (A_0, A_1, \ldots, A_K) and (B_0, B_1, \ldots, B_K) are said to be different when there exists an i such that A_i\neq B_i.

Constraints

  • 2 \leq N \leq 5000
  • 0 \leq M \leq \min\left( \frac{N(N-1)}{2},5000 \right)
  • 2 \leq K \leq 5000
  • 1 \leq U_i<V_i \leq N
  • All pairs (U_i, V_i) are pairwise distinct.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K
U_1 V_1
:
U_M V_M

Output

Print the answer.


Sample Input 1

3 1 4
2 3

Sample Output 1

4

There are four different trips as follows.

  • (1,2,1,2,1)
  • (1,2,1,3,1)
  • (1,3,1,2,1)
  • (1,3,1,3,1)

No other trip is valid, so we should print 4.


Sample Input 2

3 3 3
1 2
1 3
2 3

Sample Output 2

0

No road remains usable, so there is no valid trip.


Sample Input 3

5 3 100
1 2
4 5
2 3

Sample Output 3

428417047
I - Fishing

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

数直線上を N 匹の魚が泳いでいます。

i の重さは W_i であり、時刻 0 に座標 X_i にいて、正方向に速さ V_i で移動しています。

高橋君は 0 以上の実数 t を自由に選び、時刻 t に一度だけ以下の行動を行います。
行動:実数 x を自由に選ぶ。現在の座標が x 以上 x+A 以下である魚を全て捕まえる。

捕まえることができる魚の重さの合計の最大値を求めてください。

制約

  • 1 \leq N \leq 2000
  • 1 \leq A \leq 10^4
  • 1 \leq W_i\leq 10^4
  • 0 \leq X_i\leq 10^4
  • 1 \leq V_i\leq 10^4
  • 入力は全て整数

入力

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

N A
W_1 X_1 V_1
W_2 X_2 V_2
\vdots
W_N X_N V_N

出力

答えを出力せよ。


入力例 1

3 10
100 0 100
1 10 30
10 20 10

出力例 1

111

時刻 0.25 に魚 1,2,3 はそれぞれ座標 25, 17.5, 22.5 にいます。よって、この時刻に x=16 として行動すると全ての魚を捕まえることができます。


入力例 2

3 10
100 100 100
1 10 30
10 20 10

出力例 2

100

時刻 0x=100 として行動するのが最適解の一つです。


入力例 3

4 10
1000 100 10
100 99 1
10 0 100
1 1 1

出力例 3

1110

時刻 1x=100 として行動するのが最適解の一つです。

Score : 500 points

Problem Statement

On a number line, there are N fish swimming.

Fish i, which has a weight of W_i, is at the coordinate X_i at time 0 and moves at a speed of V_i in the positive direction.

Takahashi will choose an arbitrary real number t greater than or equal to 0 and do the following action at time t just once.
Action: Choose an arbitrary real number x. Catch all fish whose coordinates are between x and x+A, inclusive.

Find the maximum total weight of fish that he can catch.

Constraints

  • 1 \leq N \leq 2000
  • 1 \leq A \leq 10^4
  • 1 \leq W_i\leq 10^4
  • 0 \leq X_i\leq 10^4
  • 1 \leq V_i\leq 10^4
  • All values in the input are integers.

Input

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

N A
W_1 X_1 V_1
W_2 X_2 V_2
\vdots
W_N X_N V_N

Output

Print the answer.


Sample Input 1

3 10
100 0 100
1 10 30
10 20 10

Sample Output 1

111

At time 0.25, fish 1, 2, and 3 are at the coordinates 25, 17.5, and 22.5, respectively. Thus, the action done at this time with x=16 catches all the fish.


Sample Input 2

3 10
100 100 100
1 10 30
10 20 10

Sample Output 2

100

One optimal choice is to do the action at time 0 with x=100.


Sample Input 3

4 10
1000 100 10
100 99 1
10 0 100
1 1 1

Sample Output 3

1110

One optimal choice is to do the action at time 1 with x=100.