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