A - Is It a Number?

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたは、3 桁の整数を入力として受け取り、それを 2 倍して出力するプログラムの作成を頼まれた。

ところが、どうやらプログラムに入力される文字列 S に英小文字が紛れ込むことがあるようだ。そこで、その場合はエラーを出力することにした。

S3 桁の整数である場合 (0 で始まる場合を含む) はその 2 倍の値を出力し、そうでなければ error と出力するプログラムを作成せよ。

制約

  • S は長さ 3 の文字列である。
  • S の各文字は数字または英小文字である。

入力

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

S

出力

問題文で指示された通りに整数または文字列 error を出力せよ。


入力例 1

678

出力例 1

1356

整数が与えられているので、その 2 倍の値を出力する。


入力例 2

abc

出力例 2

error

整数でないものが与えられているので、error と出力する。


入力例 3

0x8

出力例 3

error

数字と英小文字が混ざって与えられる場合もある。また、言語によっては整数として解釈可能な文字列であっても、error と出力する。


入力例 4

012

出力例 4

24

S0 から始まる場合も、 10 進数の整数として扱うことに注意せよ。

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You were asked to write a program that receives a three-digit integer, multiplies it by 2, and prints the result.

It seems, however, that the string S given to the program may contain lowercase English letters. You decide to print an error message in such a case.

Write the following program: if S is a three-digit integer (possibly beginning with a 0), the program should multiply it by 2 and print the result; otherwise, the program should print error.

Constraints

  • S is a string of length 3.
  • Each character of S is a digit or a lowercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

Print an integer or a string error, as specified in Problem Statement.


Sample Input 1

678

Sample Output 1

1356

For an integer, multiply it by 2 and print the result.


Sample Input 2

abc

Sample Output 2

error

For a non-integer string, print error.


Sample Input 3

0x8

Sample Output 3

error

The input may be a combination of digits and English letters. Some languages can interpret these strings as integers, but print error in these cases, too.


Sample Input 4

012

Sample Output 4

24

Assume that S is written in decimal notation, even if S begins with a 0.

B - Up and Down

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

ある商品の N 日間の売上が整数列 A_1, A_2, \ldots, A_N として与えられる。A_i (1 \leqq i \leqq N)i 日目の売上を表す。

あなたは、2 日目以降の各日について、その日の売上が前日の売上よりどれだけ高かったか (あるいは低かったか) を出力するプログラムを作成することにした。

より具体的には、プログラムは N-1 行を出力し、i 行目 (1 \leqq i \leqq N-1) の内容は次の通りである。

  • A_{i+1}A_i と等しい場合: stay
  • A_{i+1}A_i より小さい場合: down [減少量]、ここで [減少量] は整数値 A_i - A_{i+1}
  • A_{i+1}A_i より大きい場合: up [増加量]、ここで [増加量] は整数値 A_{i+1} - A_i

このプログラムを作成せよ。

制約

  • 2 \leqq N \leqq 100,000
  • 0 \leqq A_i \leqq 1,000,000,000
  • 入力中の値はすべて整数である。

入力

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

N
A_1
A_2
:
A_{N}

出力

問題文で指示された通りに N - 1 行を出力せよ。


入力例 1

10
9
10
3
100
100
90
80
10
30
10

出力例 1

up 1
down 7
up 97
stay
down 10
down 10
down 70
up 20
down 20

N = 10 日間の売上が与えられている。出力の最初の 4 行について説明する。

  • 1 行目: A_2 = 10A_1 = 9 より 1 大きいため up 1 と出力する。
  • 2 行目: A_3 = 3A_2 = 10 より 7 小さいため down 7 と出力する。
  • 3 行目: A_4 = 100A_3 = 3 より 97 大きいため up 97 と出力する。
  • 4 行目: A_5 = 100A_4 = 100 と等しいため stay と出力する。

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a series of sales of a certain product for N days, as an integer sequence A_1, A_2, \ldots, A_N. A_i (1 \leq i \leq N) represents the sale for the i-th day.

You decide to write a program that, for each day starting with the second day, prints how much the sales increased (or decreased) compared to the previous day.

More specifically, your program should print N-1 lines. The i-th line (1 \leq i \leq N-1) should contain the following:

  • If A_{i+1} is equal to A_i: stay
  • If A_{i+1} is less than A_i: down [amount], where [amount] is the integer value A_i - A_{i+1}
  • If A_{i+1} is greater than A_i: up [amount], where [amount] is the integer value A_{i+1} - A_i

Write this program.

Constraints

  • 2 \leq N \leq 100000
  • 0 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
A_2
:
A_{N}

Output

Print N - 1 lines, as specified in Problem Statement.


Sample Input 1

10
9
10
3
100
100
90
80
10
30
10

Sample Output 1

up 1
down 7
up 97
stay
down 10
down 10
down 70
up 20
down 20

Given is a series of sales for N = 10 days. Let us explain the first four lines in the output:

  • Line 1: A_2 = 10 is greater than A_1 = 9 by 1, so print up 1.
  • Line 2: A_3 = 3 is smaller than A_2 = 10 by 7, so print down 7.
  • Line 3: A_4 = 100 is greater than A_3 = 3 by 97, so print up 97.
  • Line 4: A_5 = 100 is equal to A_4 = 100, so print stay.
C - The Third

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

6 つの相異なる整数 A, B, C, D, E, F が与えられる。

このうち 3 番目に大きい数を調べるプログラムを作成せよ。

制約

  • 1 \leqq A, B, C, D, E, F \leqq 100
  • A, B, C, D, E, F はすべて異なる。
  • 入力中の値はすべて整数である。

入力

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

A B C D E F

出力

3 番目に大きい整数を出力せよ。


入力例 1

4 18 25 20 9 13

出力例 1

18

3 番目に大きい数は 18 である。


入力例 2

95 96 97 98 99 100

出力例 2

98

入力例 3

19 92 3 35 78 1

出力例 3

35

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given are six distinct integers A, B, C, D, E, and F.

Write the program that finds out the third-largest number among them.

Constraints

  • 1 \leq A, B, C, D, E, F \leq 100
  • A, B, C, D, E, and F are all different.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D E F

Output

Print the third-largest integer.


Sample Input 1

4 18 25 20 9 13

Sample Output 1

18

The third-largest number is 18.


Sample Input 2

95 96 97 98 99 100

Sample Output 2

98

Sample Input 3

19 92 3 35 78 1

Sample Output 3

35
D - Duplicated?

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

長さ N の整数列がサーバーに保管されている。つい先ほどまで、この列には 1 から N までの整数が 1 個ずつ含まれていた。

しかし、たった今発生したトラブルにより、列のいずれか 1 個の要素が別の 1 以上 N 以下の整数に書き換えられた可能性がある。あるいは、何の書き換えも発生しなかったかもしれない。

トラブル発生後の整数列 A_1, \ldots, A_N が与えられる。これを読み込み、書き換えが発生していたかを判定し、発生していた場合にはどの整数がどの整数に書き換えられたかを報告するプログラムを作成せよ。

制約

  • 1 \leqq N \leqq 200,000
  • 1 \leqq A_i \leqq N
  • A_1, \ldots, A_N は問題文中の状況と矛盾しない。

入力

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

N
A_1
A_2
:
A_N

出力

書き換えが発生していなかった場合、Correct と出力せよ。

書き換えが発生していた場合、整数 x が整数 y に書き換えられたとして、yx をこの順にスペース区切りで出力せよ。


入力例 1

6
1
5
6
3
2
6

出力例 1

6 4

元の整数列には 1 から 6 までの整数が 1 個ずつ含まれていたはずだが、入力された整数列は 62 個含み、41 個も含まない。よって、46 に書き換えられたと判定できる。


入力例 2

7
5
4
3
2
7
6
1

出力例 2

Correct

元の整数列には 1 から 7 までの整数が 1 個ずつ含まれていたはずであり、入力された整数列もこれを満たす。よって、書き換えは発生しなかったと判定できる。

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have an integer sequence of length N stored in our server. Until just a minute ago, this sequence contained one occurrence of each integer from 1 to N.

However, trouble has occurred, which may have replaced one element in the sequence with another integer between 1 and N (inclusive). It is also possible that there was no such overwrite.

Given is the integer sequence after the trouble, A_1, \ldots, A_N. Write a program that reads it and determines whether there was an overwrite. If there was an overwrite, the program should also report which integer was replaced with which integer.

Constraints

  • 1 \leq N \leq 200000
  • 1 \leq A_i \leq N
  • The sequence A_1, \ldots, A_N is consistent with the situation described in Problem Statement.

Input

Input is given from Standard Input in the following format:

N
A_1
A_2
:
A_N

Output

If there was no overwrite, print Correct.

If there was an overwrite, let us say the integer x was replaced with the integer y. Print y and x in this order, with space in between.


Sample Input 1

6
1
5
6
3
2
6

Sample Output 1

6 4

The original sequence should have contained one occurrence of each integer from 1 to 6. However, the given sequence contains two occurrences of 6 and no occurrence of 4. Thus, we can see that 4 was replaced with 6.


Sample Input 2

7
5
4
3
2
7
6
1

Sample Output 2

Correct

The original sequence should have contained one occurrence of each integer from 1 to 7, and so does the given sequence. Thus, we can see that there was no overwrite.

E - Restore

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたは、N 人のユーザが参加する SNS を運営している。彼らはユーザ 1, \ldots, N と呼ばれ、別のユーザを何人でもフォローすることができる。

しかし、あるトラブルにより、どのユーザがどのユーザをフォローしているかの情報がすべて失われてしまった。幸い、ユーザがこの SNS で行ったすべての操作のログが残っており、あなたは操作ログをもとにフォロー状況を復元しようとしている。

操作ログは Q 行からなり、その i 行目 (1 \leqq i \leqq Q) は文字列 S_i である。各ユーザは以下の 3 種類の操作を行うことができ、S_i は SNS 全体で i 番目に行われた操作に対応する。

  • フォロー: ユーザ a がユーザ b (a \neq b) をフォローする。ログには 1 a b という行が記載される。
  • フォロー全返し: ユーザ a が、その時点でユーザ a をフォローしているユーザ全員をフォローする。ログには 2 a という行が記載される。
  • フォローフォロー: その時点でユーザ a がフォローしている各ユーザ x に対し、ユーザ a が次を行う: 「その時点でユーザ x がフォローしているすべてのユーザ (ユーザ a 自身を除く) をフォローする」。ログには 3 a という行が記載される。

なお、この SNS が開設された時点では、どのユーザも誰もフォローしていなかった。また、ユーザ a がユーザ b を既にフォローしているとき、ユーザ a がユーザ b を再びフォローしても何も起こらない。

フォロー状況を復元せよ。

制約

  • 2 ≦ N ≦ 100
  • 0 ≦ Q ≦ 500
  • S_i は以下のいずれかの形式の文字列である。
    • 1 a b (1 \leqq a, b \leqq N かつ a \neq b)
    • 2 a (1 \leqq a \leqq N)
    • 3 a (1 \leqq a \leqq N)

入力

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

N Q
S_1
S_2
:
S_Q

出力

i, j (1 \leq i, j \leq N) に対し、ユーザ i がユーザ j をフォローしているとき f_{i,j} = Y, そうでないとき f_{i,j} = N として、以下の形式で出力せよ。

f_{1,1}f_{1,2} \ldots f_{1,N}
f_{2,1}f_{2,2} \ldots f_{2,N}
:
f_{N,1}f_{N,2} \ldots f_{N,N}

入力例 1

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

出力例 1

NYYNYY
NNYNNN
NNNYNN
NNNNNN
NNNNNY
YNNNYN

6 人のユーザ、ユーザ 1, \ldots, 6 が合計で 7 回の操作を行い、以下のようなログが残されている。

  • S_1: ユーザ 1 がユーザ 2 をフォローした。
  • S_2: ユーザ 2 がユーザ 3 をフォローした。
  • S_3: ユーザ 3 がユーザ 4 をフォローした。
  • S_4: ユーザ 1 がユーザ 5 をフォローした。
  • S_5: ユーザ 5 がユーザ 6 をフォローした。
  • S_6: ユーザ 1 がフォローフォローを実行した。操作が行われる時点でユーザ 1 がフォローしているユーザはユーザ 2, 5 の二人であり、ユーザ 2 はユーザ 3 を、ユーザ 5 はユーザ 6 をフォローしている。よって、ユーザ 1 はユーザ 3, 6 の二人をフォローする。
  • S_7: ユーザ 6 がフォロー全返しを実行した。操作が行われる時点でユーザ 6 をフォローしているユーザはユーザ 1, 5 の二人であり、ユーザ 6 はこの二人をフォローする。

最終的に、フォロー状況は出力例の通りとなる。

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You run a social media site with N users. The users are called User 1, \ldots, N, and they can follow an unlimited number of other users. The list of users followed by a particular user is called follow-list of that user.

However, due to trouble, the follow-lists of all users have been lost. Fortunately, all operations ever performed on the site have been recorded in the log file. You are trying to restore the follow-lists of the users from this file.

The file consists of Q lines, and the i-th line contains a string S_i. The users can perform the three kinds of operations below, and S_i corresponds to the i-th operation performed on the whole site.

  • Follow: User a follows User b (a \neq b). The log file records the line 1 a b.
  • Follow-Back-All: User a follows all users who are following User a at that moment. The log file records the line 2 a.
  • Follow-Follow: For every user x who are followed by User a at that time, User a performs the following: "follow all users (except User a itself) followed by User x." The log file records the line 3 a.

At the beginning of the site, no user was following anyone else. Also, if User a is already following User b and attempts to re-follow User b, nothing happens.

Restore the follow-lists of all users.

Constraints

  • 2 \leq N \leq 100
  • 0 \leq Q \leq 500
  • S_i is a string in one of the following formats:
    • 1 a b (1 \leq a, b \leq N and a \neq b)
    • 2 a (1 \leq a \leq N)
    • 3 a (1 \leq a \leq N)

Input

Input is given from Standard Input in the following format:

N Q
S_1
S_2
:
S_Q

Output

For each pair i, j (1 \leq i, j \leq N), let f_{i,j} = Y if User i follows User j and f_{i,j} = N otherwise. Output should be in the following format:

f_{1,1}f_{1,2} \ldots f_{1,N}
f_{2,1}f_{2,2} \ldots f_{2,N}
:
f_{N,1}f_{N,2} \ldots f_{N,N}

Sample Input 1

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

Sample Output 1

NYYNYY
NNYNNN
NNNYNN
NNNNNN
NNNNNY
YNNNYN

Six users, User 1, \ldots, 6, performed a total of seven operations, which resulted in the following log file:

  • S_1: User 1 followed User 2.
  • S_2: User 2 followed User 3.
  • S_3: User 3 followed User 4.
  • S_4: User 1 followed User 5.
  • S_5: User 5 followed User 6.
  • S_6: User 1 performed Follow-Follow. At the moment, User 1 was following two users, User 2 and 5. User 2 was following User 3, and User 5 was following User 6, so User 1 followed two users, User 3 and 6.
  • S_7: User 6 performed Follow-Back-All. At the moment, User 6 was followed by two users, User 1 and 5, so User 6 followed back these two users.

In the end, we have the follow-lists shown in the output above.

F - DoubleCamelCase Sort

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

文字列 S が与えられる。これは、1 つ以上の単語を (間に空白などを挟まずに) 連結したものである。ここで、各単語は 2 文字以上であり、最初の文字と最後の文字のみが英大文字、それ以外の文字は全て英小文字である。

これらの単語を辞書順に並べ (大文字小文字の違いは無視する)、同様に連結して出力するプログラムを作成せよ。

例えば、S = FisHDoGCaTAAAaAAbCAC とする。これは FisH, DoG, CaT, AA, AaA, AbC, AC7 つの単語を連結したものである。これらを辞書順に並べると AA, AaA, AbC, AC, CaT, DoG, FisH となるため、AAAaAAbCACCaTDoGFisH と出力すればよい。

制約

  • S は長さ 2 以上 100,000 以下の文字列である。
  • S の各文字は英大文字または英小文字である。
  • S は問題文で述べたような単語の連結である。

入力

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

S

出力

問題文で指示された通りの文字列を出力せよ。


入力例 1

FisHDoGCaTAAAaAAbCAC

出力例 1

AAAaAAbCACCaTDoGFisH

問題文で用いた例である。


入力例 2

AAAAAjhfgaBCsahdfakGZsZGdEAA

出力例 2

AAAAAAAjhfgaBCsahdfakGGdEZsZ

同じ単語が複数個存在する可能性があることに注意せよ。

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

Given is a string S, which is a concatenation of one or more words (without any extra characters such as spaces in between). Each word here is at least two characters long. The first and last characters are uppercase, and all the other characters are lowercase.

Write a program that sorts these words in lexicographical order (ignoring the case), concatenates them in the same manner as in S, and prints it.

For example, consider the case S = FisHDoGCaTAAAaAAbCAC. This string is a concatenation of seven words: FisH, DoG, CaT, AA, AaA, AbC, and AC. We sort them in the lexicographical order: AA, AaA, AbC, AC, CaT, DoG, FisH, and print AAAaAAbCACCaTDoGFisH.

Constraints

  • S is a string of length between 2 and 100000 (inclusive).
  • Each character of S is an uppercase or lowercase English letter.
  • S is a concatenation of words as described in Problem Statement.

Input

Input is given from Standard Input in the following format:

S

Output

Print a string as specified in Problem Statement.


Sample Input 1

FisHDoGCaTAAAaAAbCAC

Sample Output 1

AAAaAAbCACCaTDoGFisH

This is the example used in Problem Statement.


Sample Input 2

AAAAAjhfgaBCsahdfakGZsZGdEAA

Sample Output 2

AAAAAAAjhfgaBCsahdfakGGdEZsZ

Note that the same word may occur multiple times.

G - Division

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたは、N 人の社員、社員 1, \ldots, N3 つ以下のグループに最適に分割するプログラムを作ろうとしている。

すでに、社員のペア i, j (1 \leqq i < j \leqq N) すべてに対して、彼らを同じグループに含めた際に生じる幸福度 A_{i,j} (正とは限らない) が AI によって計算されている。

グループ分けの好ましさを、同じグループに含まれる社員のペア i, j すべてに対する A_{i,j} の総和 (そのようなペアが存在しなければ 0) と定義する。グループ分けの好ましさの最大値を求めよ。

制約

  • 2 \leqq N \leqq 10
  • -1,000,000 \leqq A_{i,j} \leqq 1,000,000
  • 入力中の値はすべて整数である。

入力

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

N
a_{1,2} a_{1,3} \ldots a_{1, N}
a_{2,3} a_{2,4} \ldots a_{2, N}
:
a_{N-1,N}

出力

グループ分けの好ましさの最大値を出力せよ。


入力例 1

6
10 10 -10 -10 -10
10 -10 -10 -10
-10 -10 -10
10 -10
-10

出力例 1

40

社員 1,2,3 からなるグループ、社員 4,5 からなるグループ、社員 6 からなるグループの 3 つのグループを作ることを考える。このとき、同じグループに含まれる社員のペアは (1,2), (1,3), (2,3), (4,5)4 つ存在し、このグループ分けの好ましさは A_{1,2} + A_{1,3} + A_{2,3} + A_{4,5} = 10 + 10 + 10 + 10 = 40 となる。これが最適なグループ分けである。


入力例 2

3
1 1
1

出力例 2

3

全員を同じグループに含めても構わない。

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You are writing a program that optimally divides N employees in your company, Employee 1, \ldots, N, into at most three groups.

For all pairs of employees i, j (1 \leq i < j \leq N), an AI has already computed the happiness A_{i,j} (not necessarily positive) that will arise if those two employees belong to the same group.

Let the desirability of a grouping be the sum of A_{i,j} over all pairs of employees i, j that belong to the same group (if no such pair exists, the desirability is 0). Find the maximum possible desirability of a grouping.

Constraints

  • 2 \leq N \leq 10
  • -10^6 \leq A_{i,j} \leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_{1,2} a_{1,3} \ldots a_{1, N}
a_{2,3} a_{2,4} \ldots a_{2, N}
:
a_{N-1,N}

Output

Print the maximum possible desirability of a grouping.


Sample Input 1

6
10 10 -10 -10 -10
10 -10 -10 -10
-10 -10 -10
10 -10
-10

Sample Output 1

40

Consider making the following three groups: a group with Employee 1, 2,3, a group with Employee 4, 5, and a group with Employee 6. There are four pairs of employees belonging to the same group: (1,2), (1,3), (2,3), (4,5), for the desirability of A_{1,2} + A_{1,3} + A_{2,3} + A_{4,5} = 10 + 10 + 10 + 10 = 40. This is the optimal grouping.


Sample Input 2

3
1 1
1

Sample Output 2

3

All employees may belong to the same group.

H - Bulk Selling

Time Limit: 4 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたは、トレーディングカードを販売しようとしている。 それぞれのカードの種類には 1,...,N の番号がついている。 はじめ、カード i (1 \leqq i \leqq N) の在庫数は C_i 枚である。

あなたは、以下のような Q 件のクエリ S_1, \ldots, S_Q を順番に処理しなければならない。

  • 単品販売:カード xa 枚販売する。ただし、在庫が足りない場合は何もしない。1 x a という形式で与えられる。
  • セット販売:番号が奇数であるカードをそれぞれ a 枚ずつ販売する。ただし、一種類でも在庫が足りない場合は何もしない。2 a という形式で与えられる。
  • 全種類販売:カードを全種類 a 枚ずつ販売する。ただし、一種類でも在庫が足りない場合は何もしない。3 a という形式で与えられる。

最終的に販売するカードの合計枚数を出力せよ。

制約

  • 1 \leqq N \leqq 200,000
  • 1 \leqq C_i \leqq 10^9
  • 1 \leqq Q \leqq 200,000
  • S_i は以下のいずれかの形式の文字列である。
    • 1 x a (1 \leqq x \leqq N かつ 1 \leqq a \leqq 10^9)
    • 2 a (1 \leqq a \leqq 10^9)
    • 3 a (1 \leqq a \leqq 10^9)
  • 入力で与えられる数は全て整数である。

入力

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

N
C_1 C_2 ... C_N
Q
S_1
S_2
:
S_Q

出力

最終的に販売するカードの合計枚数を出力せよ。


入力例 1

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

出力例 1

9

最初、各カードの在庫数はそれぞれ 5,3,3,5 である。 各クエリは以下のように処理される。

  1. カード 21 枚販売する。このクエリの後、在庫数は 5,2,3,5 となる。
  2. カード 1,32 枚ずつ販売する。このクエリの後、在庫数は 3,2,1,5 となる。
  3. カード 3 の在庫が足りないため、何もしない。
  4. 在庫が足りないため、何もしない。
  5. 全種類のカードを 1 枚ずつ販売する。このクエリの後、在庫数は 2,1,0,4 となる。
  6. 在庫が足りないため、何もしない。

このように、1,2,5 番目のクエリで 1,4,4 枚のカードを販売し、合計販売枚数は 9 枚となる。


入力例 2

10
241 310 105 738 405 490 158 92 68 20
20
2 252
1 4 36
2 69
1 5 406
3 252
1 3 8
1 10 10
3 11
1 4 703
3 1
2 350
3 10
2 62
2 3
2 274
1 2 1
3 126
1 4 702
3 6
2 174

出力例 2

390

入力例 3

2
3 4
3
1 2 9
2 4
3 4

出力例 3

0

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You are going to sell trading cards. You have N kinds of cards, Card 1, \ldots, N, and you have C_i copies of Card i (1 \leq i \leq N) in stock initially.

Now, you have to process Q requests S_1, \ldots, S_Q in order. Each request is in one of the following forms:

  • Single Kind: Sell a copies of Card x, or do nothing if you do not have enough copies in stock. Given in the format 1 x a.
  • Many Kinds: Sell a copies of Card x for every odd number x between 1 and N (inclusive), or do nothing if you do not have enough copies of one or more of those kinds in stock. Given in the format 2 a.
  • All Kinds: Sell a copies of Card x for every number x between 1 and N, or do nothing if you do not have enough copies of one or more of those kinds in stock. Given in the format 3 a.

Print the total number of cards that will be sold in these requests.

Constraints

  • 1 \leq N \leq 200000
  • 1 \leq C_i \leq 10^9
  • 1 \leq Q \leq 200000
  • S_i is a string in one of the following formats:
    • 1 x a (1 \leq x \leq N and 1 \leq a \leq 10^9)
    • 2 a (1 \leq a \leq 10^9)
    • 3 a (1 \leq a \leq 10^9)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 C_2 ... C_N
Q
S_1
S_2
:
S_Q

Output

Print the total number of cards sold in the Q requests.


Sample Input 1

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

Sample Output 1

9

Initially, you have 5,3,3,5 copies of Card 1,2,3,4, respectively. Each request will be processed as follows:

  1. Sell one copy of Card 2. You have now 5,2,3,5 copies of Card 1,2,3,4 in stock.
  2. Sell two copies of Card 1 and 3 each. You have now 3,2,1,5 copies in stock.
  3. You do not have enough copies of Card 3 in stock. Do nothing.
  4. Not enough copies in stock. Do nothing.
  5. Sell one copy of every kind of card. You have now 2,1,0,4 copies in stock.
  6. Not enough copies in stock. Do nothing.

As seen above, we will sell 1, 4, 4 cards in the first, second, fifth requests, for a total of 9 cards.


Sample Input 2

10
241 310 105 738 405 490 158 92 68 20
20
2 252
1 4 36
2 69
1 5 406
3 252
1 3 8
1 10 10
3 11
1 4 703
3 1
2 350
3 10
2 62
2 3
2 274
1 2 1
3 126
1 4 702
3 6
2 174

Sample Output 2

390

Sample Input 3

2
3 4
3
1 2 9
2 4
3 4

Sample Output 3

0
I - Procurement

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたは、あるものを作るために N 種類の部品、部品 1, \ldots, N を購入しようとしている。ネット通販サイトを探すと、部品のセット販売が M 件見つかった。

i 件目のセット販売 (1 \leqq i \leqq M) の情報は、長さ N の文字列 S_i と整数 C_i の組として与えられる。S_ij 文字目 (1 \leqq j \leqq N) は、このセットが部品 j を含むとき Y、含まないとき N である。また、このセットは C_i 円で販売されている。

これらのセットのうち何個かを購入し、N 種類の部品をすべて 1 個以上手に入れるのに必要な金額を求める (または、それが不可能であることを報告する) プログラムを作成せよ。

制約

  • 1 \leqq N \leqq 10
  • 1 \leqq M \leqq 1,000
  • S_i は長さ N の文字列である。
  • S_i の各文字は Y または N である。
  • 1 \leqq C_i \leqq 1,000,000,000
  • C_i は整数である。

入力

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

N M
S_1 C_1
S_2 C_2
:
S_M C_M

出力

すべての部品を 1 個以上手に入れることが可能であれば、それに必要な最小金額を表す整数を出力せよ。

そうでなければ、-1 と出力せよ。


入力例 1

3 4
YYY 100
YYN 20
YNY 10
NYY 25

出力例 1

30

2 番目と 3 番目のセットを購入すると、20 + 10 = 30 円で部品 1, 2, 3 すべてが揃う。


入力例 2

5 4
YNNNN 10
NYNNN 10
NNYNN 10
NNNYN 10

出力例 2

-1

部品 5 がどのセットにも含まれておらず、すべての部品を揃えることは不可能である。


入力例 3

10 14
YNNYNNNYYN 774472905
YYNNNNNYYY 75967554
NNNNNNNNNN 829389188
NNNNYYNNNN 157257407
YNNYNNYNNN 233604939
NYYNNNNNYY 40099278
NNNNYNNNNN 599672237
NNNYNNNNYY 511018842
NNNYNNYNYN 883299962
NNNNNNNNYN 883093359
NNNNNYNYNY 54742561
NYNNYYYNNY 386272705
NNNNYYNNNN 565075143
NNYNYNNNYN 123300589

出力例 3

451747367

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You want to buy N kinds of parts to make some objects: Part 1, \ldots, N. After searching online, you found M packages of parts on sale.

The i-th package (1 \leq i \leq M) is described by a string S_i of length N and an integer C_i. The j-th character (1 \leq j \leq N) of S_i is Y if the package contains Part j, and N otherwise. This package is sold for C_i yen (the currency of Japan).

Write a program that finds the minimum amount of money needed to buy some of these packages and get all N kinds of parts (or reports that it is impossible). It is fine to get more than one part of the same kind.

Constraints

  • 1 \leq N \leq 10
  • 1 \leq M \leq 1000
  • S_i is a string of length N.
  • Each character of S_i is Y or N.
  • 1 \leq C_i \leq 10^9
  • C_i is an integer.

Input

Input is given from Standard Input in the following format:

N M
S_1 C_1
S_2 C_2
:
S_M C_M

Output

If it is possible to get all N kinds of parts, print the integer representing the minimum amount of money to do so.

Otherwise, print -1.


Sample Input 1

3 4
YYY 100
YYN 20
YNY 10
NYY 25

Sample Output 1

30

If we buy the second and third packages, we get all kinds of parts 1, 2, 3 for 20 + 10 = 30 yen.


Sample Input 2

5 4
YNNNN 10
NYNNN 10
NNYNN 10
NNNYN 10

Sample Output 2

-1

Part 5 is not contained in any package, so we cannot get all kinds of parts.


Sample Input 3

10 14
YNNYNNNYYN 774472905
YYNNNNNYYY 75967554
NNNNNNNNNN 829389188
NNNNYYNNNN 157257407
YNNYNNYNNN 233604939
NYYNNNNNYY 40099278
NNNNYNNNNN 599672237
NNNYNNNNYY 511018842
NNNYNNYNYN 883299962
NNNNNNNNYN 883093359
NNNNNYNYNY 54742561
NYNNYYYNNY 386272705
NNNNYYNNNN 565075143
NNYNYNNNYN 123300589

Sample Output 3

451747367
J - Leveling

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

H マス、横 W マスの合計 H \times W 個の正方形のマスに区切られた区画がある。左下隅、右下隅、右上隅の 3 マスを除いてこれらのマスはすべて未整備で、上から i 行目、左から j 列目 (1 \leqq i \leqq H, 1 \leqq j \leqq W) のマスを整備するのに必要な費用は A_{i,j} 円である。

区画の左下隅のマスに物体が置かれている。あなたは、これをまず右下隅のマスまで移動させ、次に右上隅のマスまで移動させようとしている。

物体の移動は、物体が現在占有するマスから上下左右に隣接するマスに動かすことを繰り返して行う。このとき、物体が通るマスはすべて整備されている必要がある。一度整備を行ったマスの上は何度でも物体を通すことができる。

このとき、マスの整備に必要な最小の合計費用を求めよ。

制約

  • 2 \leqq H, W \leqq 50
  • 0 \leqq A_{i,j} \leqq 100,000
  • A_{H,1} = A_{H,W} = A_{1,W} = 0
  • 入力中の値はすべて整数である。

入力

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

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
:
A_{H,1} A_{H,2} \ldots A_{H,W}

出力

必要な最小の合計費用を表す整数を出力せよ。


入力例 1

5 6
9 9 9 9 1 0
9 9 9 9 1 9
9 9 9 1 1 1
9 1 1 1 9 1
0 1 9 9 9 0

出力例 1

10

整備費用が 1 円のマスをすべて整備するのが最適である。


入力例 2

10 10
1 2 265 1544 0 1548 4334 9846 58 0
21 0 50 44 2 388 5 0 0 4
170 0 2 1 54 1379 50 3 41 0
310 0 1 0 2163 0 226 26 3 12
151 33 0 9 0 0 0 36 365 2286
0 3 12 3 9 317 645 100 21 4
52 1 569 0 144 0 6 202 25 0
8869 19 2058 1948 1252 1002 7 1750 0 5
0 3 8 29 2 4403 0 0 0 5
0 17 93 9367 159 6 1 216 0 0

出力例 2

246

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There is a rectangular piece of land divided into a grid of H \times W squares with H horizontal rows and W vertical columns. Except for the three squares at the bottom-left, bottom-right, and top-right corners of the grid, all the squares are unpaved. Paving the square at the i-th row from the top and the j-th column from the left (1 \leq i \leq H, 1 \leq j \leq W) costs A_{i,j} yen (the currency of Japan).

We have an object in the square at the bottom-left corner of the grid. You want to carry it to the square at the bottom-right corner, then to the square at the top-right corner.

To carry the object, you need to repeat moving it from a square to another square that is horizontally or vertically adjacent to the square currently occupied. Here, all the squares that the object goes through must be paved. Once a square is paved, the object may go through that square any number of times.

Find the minimum amount of money needed to pave squares for your objective.

Constraints

  • 2 \leq H, W \leq 50
  • 0 \leq A_{i,j} \leq 100000
  • A_{H,1} = A_{H,W} = A_{1,W} = 0
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
A_{1,1} A_{1,2} \ldots A_{1,W}
A_{2,1} A_{2,2} \ldots A_{2,W}
:
A_{H,1} A_{H,2} \ldots A_{H,W}

Output

Print the integer representing the minimum amount of money.


Sample Input 1

5 6
9 9 9 9 1 0
9 9 9 9 1 9
9 9 9 1 1 1
9 1 1 1 9 1
0 1 9 9 9 0

Sample Output 1

10

It is optimal to pave all the squares costing 1 yen to pave.


Sample Input 2

10 10
1 2 265 1544 0 1548 4334 9846 58 0
21 0 50 44 2 388 5 0 0 4
170 0 2 1 54 1379 50 3 41 0
310 0 1 0 2163 0 226 26 3 12
151 33 0 9 0 0 0 36 365 2286
0 3 12 3 9 317 645 100 21 4
52 1 569 0 144 0 6 202 25 0
8869 19 2058 1948 1252 1002 7 1750 0 5
0 3 8 29 2 4403 0 0 0 5
0 17 93 9367 159 6 1 216 0 0

Sample Output 2

246
K - Conglomerate

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

ある企業には N 人の社員、社員 1, \ldots, N が在籍する。このうち一人は社長であり、上司を持たない。その他の社員はみなちょうど一人の直属の上司を持つ。あなたのデータベースによると、社員 i (1 \leqq i \leqq N) の直属の上司は社員 p_i である (ただし社員 i が社長である場合は p_i = -1 となっている)。この上下関係に循環は存在しない。

このデータを用いて、二つの社員番号 a, b を入力すると社員 a が社員 b の部下であるかを判定するサービスを作りたい。ここで、社員 a が社員 b の部下であるとは、社員 a から直属の上司を辿っていくことで社員 b に到達できることを意味する。

サービスに入力される Q 個の社員番号の組 (a_1, b_1), \ldots, (a_Q, b_Q) が与えられる。これらのそれぞれに対して Yes または No を出力するプログラムを作成せよ。

制約

  • 2 \leqq N \leqq 150,000
  • i = 1, \ldots, N に対し、p_i = -1 または 1 \leq p_i \leq N
  • p_i = -1 なる i はちょうど 1 つ存在する。
  • 社員間の上下関係に循環は存在しない。
  • 1 \leqq Q \leqq 100,000
  • 1 \leqq a_i, b_i \leqq N
  • a_i \neq b_i

入力

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

N
p_1
p_2
:
p_N
Q
a_1 b_1
a_2 b_2
:
a_Q b_Q

出力

Q 行出力せよ。i 行目 (1 \leqq i \leqq Q) は、社員 a_i が社員 b_i の部下であれば Yes、そうでなければ No とすること。


入力例 1

7
-1
1
1
2
2
3
3
6
7 1
4 1
2 3
5 1
5 2
2 5

出力例 1

Yes
Yes
No
Yes
Yes
No

社員 1 (社長) の直属の部下が社員 2, 3、社員 2 の直属の部下が社員 4,5、社員 3 の直属の部下が社員 6,7 である。

例えば、1 個目の組 (7, 1) に対しては、社員 1 は社員 7 の直属の上司 (社員 3) の直属の上司であるため Yes と出力する。


入力例 2

20
4
11
12
-1
1
13
13
4
6
20
1
1
20
10
8
8
20
10
18
1
20
18 14
11 3
2 13
13 11
10 15
9 5
17 11
18 10
1 16
9 4
19 6
5 10
17 8
15 8
5 16
6 20
3 19
10 12
5 13
18 1

出力例 2

No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
Yes
No
Yes
No
No
No
Yes

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

A certain company has N members, Member 1, \ldots, N. One of them is the president and has no boss. Each of the other members has exactly one immediate boss. According to your database, the immediate boss of Member i (1 \leq i \leq N) is Member p_i (p_i = -1 if Member i is the president). There is no cyclic relationship among those members.

Based on this database, you want to create a web service that, when two numbers a and b representing two members are given, determines whether Member a is junior to Member b. Here, Member a is said to be junior to Member b if and only if Member b can be reached from Member a by tracing the relationship between the members in upward direction only.

Given are Q pairs of numbers (a_1, b_1), \ldots, (a_Q, b_Q), each representing two members. Write a program that prints Yes or No for each of them.

Constraints

  • 2 \leq N \leq 150000
  • For each i = 1, \ldots, N, p_i = -1 or 1 \leq p_i \leq N.
  • There exists a unique i such that p_i = -1.
  • There is no cyclic relationship among the members.
  • 1 \leq Q \leq 100000
  • 1 \leq a_i, b_i \leq N
  • a_i \neq b_i

Input

Input is given from Standard Input in the following format:

N
p_1
p_2
:
p_N
Q
a_1 b_1
a_2 b_2
:
a_Q b_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain Yes if Member a_i is junior to Member b_i, and No otherwise.


Sample Input 1

7
-1
1
1
2
2
3
3
6
7 1
4 1
2 3
5 1
5 2
2 5

Sample Output 1

Yes
Yes
No
Yes
Yes
No

The immediate juniors of Member 1 (the president) are Member 2, 3, the immediate juniors of Member 2 are Member 4, 5, and the immediate juniors of Member 3 are Member 6, 7.

For example, for the first pair (7, 1), Member 1 is the immediate boss of "the immediate boss of Member 7" (Member 3), so we print Yes.


Sample Input 2

20
4
11
12
-1
1
13
13
4
6
20
1
1
20
10
8
8
20
10
18
1
20
18 14
11 3
2 13
13 11
10 15
9 5
17 11
18 10
1 16
9 4
19 6
5 10
17 8
15 8
5 16
6 20
3 19
10 12
5 13
18 1

Sample Output 2

No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
Yes
No
Yes
No
No
No
Yes
L - Gradation

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

二次元座標平面上に、N 本の大きな塔と M 本の小さな塔が建っている。i 本目の大きな塔 (1 \leqq i \leqq N) の座標は (x_i, y_i)、色は c_i であり、i 本目の小さな塔 (1 \leqq i \leqq M) の座標は (X_i, Y_i)、色は C_i である。ここで、塔の色は整数として与えられ、1, 2, 3 がそれぞれ赤、緑、青を表す。なお、同一の座標に複数の塔が存在する可能性がある。

あなたは、二本の塔を結ぶ橋を何本か建設して、どの大きな塔から別のどの大きな塔へも何本かの橋を渡って到達できるようにしたい。(小さい塔はどのように扱ってもよい。)

同じ色の塔を結ぶ橋の建設コストは二本の塔の間のユークリッド距離に等しく、異なる色の塔を結ぶ橋の建設コストはその 10 倍である。(塔の大小は建設コストに関係しない。また、橋の交差は考慮しない。)

目的の達成に必要な最小の合計コストを求めよ。

制約

  • 2 \leqq N \leqq 30
  • 1 \leqq M \leqq 5
  • 0 \leqq x_i, y_i \leqq 1,000
  • 0 \leqq X_i, Y_i \leqq 1,000
  • 1 \leqq c_i, C_i \leqq 3
  • 入力中の値はすべて整数である。

入力

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

N M
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N
X_1 Y_1 C_1
X_2 Y_2 C_2
:
X_M Y_M C_M

出力

目的の達成に必要な最小の合計コストを表す実数を出力せよ。ジャッジの出力との絶対誤差または相対誤差が 10^{-6} 以下であれば正解と判定される。


入力例 1

3 1
0 0 1
0 1 1
1 0 1
1 1 1

出力例 1

2.000000000000

1 番目の大きな塔と 2 番目の大きな塔、1 番目の大きな塔と 3 番目の大きな塔をそれぞれ直接結ぶべきである。合計コストは 1 + 1 = 2 となる。


入力例 2

3 1
0 10 1
10 0 2
10 20 3
10 10 1

出力例 2

210.000000000000

小さな塔をそれぞれの大きな塔と結ぶべきである。合計コストは 10 + 10 \times 10 + 10 \times 10 = 210 となる。

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There are N large towers and M small towers built on a two-dimensional coordinate plane. The i-th large tower (1 \leq i \leq N) has the coordinate (x_i, y_i) and the color c_i, and the i-th small tower (1 \leq i \leq N) has the coordinate (X_i, Y_i) and the color C_i. Here, the color of a tower is given as an integer, where 1, 2, 3 stand for red, green, blue, respectively. There may be multiple towers at the same coordinates.

You want to construct some bridges connecting two towers so that any large tower can be reached from any other large tower using some number of bridges. (The small towers can be treated in any way you like.)

The cost needed to construct a bridge connecting two towers is equal to the Euclidean distance between them, multiplied by 10 if the colors of the towers are different. (The cost does not depend on the sizes of the towers. Also, we do not consider the crossing of bridges.)

Find the minimum total cost needed to achieve your objective.

Constraints

  • 2 \leq N \leq 30
  • 1 \leq M \leq 5
  • 0 \leq x_i, y_i \leq 1000
  • 0 \leq X_i, Y_i \leq 1000
  • 1 \leq c_i, C_i \leq 3
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N
X_1 Y_1 C_1
X_2 Y_2 C_2
:
X_M Y_M C_M

Output

Print a real number representing the minimum total cost needed to achieve the objective. Your output is considered correct if the absolute or relative error from the judge's output is at most 10^{-6}.


Sample Input 1

3 1
0 0 1
0 1 1
1 0 1
1 1 1

Sample Output 1

2.000000000000

We should directly connect the first and second large towers, then the first and third large towers, for a total cost of 1 + 1 = 2.


Sample Input 2

3 1
0 10 1
10 0 2
10 20 3
10 10 1

Sample Output 2

210.000000000000

We should connect the small tower to each of the large towers, for a total cost of 10 + 10 \times 10 + 10 \times 10 = 210.

M - Auto Choice

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

あなたはソーシャルゲームを運営している。このゲームには多数のモンスターが登場し、各モンスターは質量と魔力の二つのパラメータを持つ。

あるユーザは N 体のモンスターを所持し、i 体目の所持モンスター (1 \leqq i \leqq N) の質量は A_i、魔力は B_i である。

また、これと別に M 体のお助けモンスターがおり、i 体目のお助けモンスター (1 \leqq i \leqq M) の質量は C_i、魔力は D_i である。

ユーザは、これらの N + M 体のモンスターからちょうど 5 体を選び、それらを合成することができる。ただし、お助けモンスターは 1 体までしか選べない。ここで、合成後のモンスターの強さを (使用されたモンスターの魔力の和) / (使用されたモンスターの質量の和) と定義する。

あなたは、合成後のモンスターの強さが最大になるように自動でモンスターを選ぶ機能を実装したい。一度だけ合成を行うとき、合成後のモンスターの強さの最大値を求めるプログラムを作成せよ。

制約

  • 5 \leqq N \leqq 1,000
  • 1 \leqq M \leqq 100
  • 1 \leqq A_i, B_i \leqq 100,000
  • 1 \leqq C_i, D_i \leqq 100,000
  • 入力中の値はすべて整数である。

入力

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

N M
A_1 B_1
A_2 B_2
:
A_N B_N
C_1 D_1
C_2 D_2
:
C_M D_M

出力

合成後のモンスターの強さの最大値を表す実数を出力せよ。ジャッジの出力との絶対誤差または相対誤差が 10^{-6} 以下であれば正解と判定される。


入力例 1

6 2
10 30
20 60
10 10
30 100
50 140
40 120
10 3
30 1

出力例 1

3.0000000000000

お助けモンスターを使わず、1,2,4,5,6 番目の所持モンスターを選ぶのが最適である。


入力例 2

6 2
1 20
1 3
32 100
1 1
1 2
2 5
10 100
96 874

出力例 2

9.0000000000000

今回は強力なお助けモンスターがおり、1 体使うべきである。

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

You run a social game, which features many monsters. Each monster has two parameters: Mass and Magic Power (MP).

A user owns N monsters. The Mass and MP of the i-th monster owned (1 \leq i \leq N) are A_i and B_i, respectively.

Additionally, there are M helper monsters. The Mass and MP of the i-th helper monster (1 \leq i \leq M) are C_i and D_i, respectively.

The user can choose exactly five out of these N + M monsters and combine them into one new monster. However, among those five monsters, there should be at most one helper monster. Let us define the strength of the new monster as the sum of the MPs of the monsters used, divided by the sum of the Masses of the monsters used.

You want to implement a function that automatically chooses monsters to combine so that the strength of the new monster is maximized. Write a program to find the maximum possible strength of the new monster when combining monsters only once.

Constraints

  • 5 \leq N \leq 1000
  • 1 \leq M \leq 100
  • 1 \leq A_i, B_i \leq 100000
  • 1 \leq C_i, D_i \leq 100000
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_N B_N
C_1 D_1
C_2 D_2
:
C_N D_N

Output

Print a real number representing the maximum possible strength of the new monster. Your output is considered correct if the absolute or relative error from the judge's output is at most 10^{-6}.


Sample Input 1

6 2
10 30
20 60
10 10
30 100
50 140
40 120
10 3
30 1

Sample Output 1

3.0000000000000

It is optimal to do without the helper monsters and choose the 1-st, 2-nd, 4-th, 5-th, and 6-th monsters owned.


Sample Input 2

6 2
1 20
1 3
32 100
1 1
1 2
2 5
10 100
96 874

Sample Output 2

9.0000000000000

We have powerful helper monsters this time and should use one.

N - Land Clearing

Time Limit: 4 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

W の門がある。以下、この門を線分とみなし、線分上の任意の点は 0 以上 W 以下の座標をもつとする。

この門の上に N 個の石が落ちている。i 個目の石 (1 \leqq i \leqq N) は区間 (l_i, r_i) を占有し、撤去するのに必要な金額は p_i 円である。同じ地点が複数の石によって占有されている可能性もある。

車が通れるように、あなたは何個か石を取り除き、この門の上に石が存在しない長さ C の区間が存在するようにしたい。この目的を達成するために必要な最小の金額を求めるプログラムを作成せよ。

制約

  • 1 \leqq N \leqq 100,000
  • 10 \leqq W \leqq 1,000,000,000
  • 1 \leqq C \leqq W
  • 0 \leqq l_i < r_i \leqq W
  • 1 \leqq p_i \leqq 1,000,000,000
  • 入力中の値はすべて整数である。

入力

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

N W C
l_1 r_1 p_1
l_2 r_2 p_2
:
l_N r_N p_N

出力

目標の達成に必要な最小金額を表す整数を出力せよ。(石を取り除く必要がないときは 0 と出力せよ。)


入力例 1

3 10 5
1 3 100
8 10 123
4 6 3

出力例 1

3

3 円を支払って 3 番目の石を撤去すると、長さ 5 の区間 [3, 8] に石が存在しなくなる。これが最適な選択である。


入力例 2

22 30 10
0 30 1000000000
0 30 1000000000
0 30 1000000000
7 30 261806
6 19 1
5 18 1238738
12 28 84
10 14 5093
9 20 9
15 26 8739840
6 8 240568
14 19 198
2 4 1102
1 29 5953283
9 20 183233
9 13 44580
6 23 787237159
12 14 49
28 29 9020727
14 20 318783
2 19 9862194
9 30 166652

出力例 2

3805189325

必要な金額が 32 bit 整数型に収まらないことがある。

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

There is a gate of width W. Below, we will regard this gate as a segment, and assume that a point on this segment has a coordinate between 0 and W (inclusive).

We have N stones scattered on the gate. The i-th stone (1 \leq i \leq N) occupies the segment (l_i, r_i), and it costs p_i yen (the currency of Japan) to remove it. Multiple stones may occupy the same point.

You want to allow a car to pass through this gate by removing some stones so that there will be a segment of length C not overlapping with a stone. Write a program to find the minimum amount of money to do so.

Constraints

  • 1 \leq N \leq 100000
  • 10 \leq W \leq 10^9
  • 1 \leq C \leq W
  • 0 \leq l_i < r_i \leq W
  • 1 \leq p_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N W C
l_1 r_1 p_1
l_2 r_2 p_2
:
l_N r_N p_N

Output

Print the integer representing the minimum amount of money needed to achieve the objective (or print 0 if no stone needs to be removed).


Sample Input 1

3 10 5
1 3 100
8 10 123
4 6 3

Sample Output 1

3

If we remove the third stone for 3 yen, no stone will overlap the segment [3, 8] of length 5. This is the optimal choice.


Sample Input 2

22 30 10
0 30 1000000000
0 30 1000000000
0 30 1000000000
7 30 261806
6 19 1
5 18 1238738
12 28 84
10 14 5093
9 20 9
15 26 8739840
6 8 240568
14 19 198
2 4 1102
1 29 5953283
9 20 183233
9 13 44580
6 23 787237159
12 14 49
28 29 9020727
14 20 318783
2 19 9862194
9 30 166652

Sample Output 2

3805189325

The amount of money may not fit into a 32-bit integer type.

O - Endurance

Time Limit: 2 sec / Memory Limit: 1024 MB

注意

この問題に対する言及は、2019年12月29日 05:00 JST まで禁止されています。言及がなされた場合、賠償が請求される可能性があります。

試験後に総合得点や認定級を公表するのは構いませんが、どの問題が解けたかなどの情報は発信しないようにお願いします。

問題文

偏りのない 6 面さいころが N 個あり、i 番目のさいころ (1 \leqq i \leqq N)j 番目の面 (1 \leqq j \leqq 6) には整数 A_{i,j} が書かれている。

高橋君は、1 個のさいころを選んで 1 回振る、という操作を繰り返す。ただし、2 回目以降の操作で、前回の操作で出た目より小さいか同じ目が出てしまったら、操作をやめる。各回にどのさいころを振るかは、前回に出た目を見てから選ぶことができる。

高橋君は、できるだけさいころを多く振りたいと考えている。操作が行われる回数の期待値が最大化されるような選択が行われたときの操作回数の期待値を求めよ。

制約

  • 1 \leqq N \leqq 30,000
  • 1 \leqq A_{i,j} \leqq {10}^{9}
  • A_{i,j} はすべて異なる。
  • 入力中の値はすべて整数である。

入力

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

N
A_{1,1} A_{1,2} A_{1,3} A_{1,4} A_{1, 5} A_{1, 6}
A_{2,1} A_{2,2} A_{2,3} A_{2,4} A_{2, 5} A_{2, 6}
:
A_{N,1} A_{N,2} A_{N,3} A_{N,4} A_{N, 5} A_{N, 6}

出力

操作回数の期待値を表す実数を出力せよ。ジャッジの出力との絶対誤差または相対誤差が 10^{-6} 以下であれば正解と判定される。


入力例 1

2
1 2 3 4 5 6
7 8 9 10 11 12

出力例 1

3.64925355954377

入力例 2

3
12 237 374 111 247 234
857 27 98 65 83 90
764 60 999 11 7 4

出力例 2

3.42188884244970

Warning

Do not make any mention of this problem until December 29, 2019, 05:00 a.m. JST. In case of violation, compensation may be demanded.

After the examination, you can reveal your total score and grade to others, but nothing more (for example, which problems you solved).

Problem Statement

We have N unbiased six-sided dice. The j-th face (1 \leq j \leq 6) of the i-th die (1 \leq i \leq N) has an integer A_{i,j} written on it.

Takahashi repeats the following operation: choose a die and toss it. However, in the second and subsequent operations, if the die shows a number that is less than or equal to the number shown in the previous operation, the process ends. The die to toss each time can be chosen after seeing the previous number shown.

Takahashi wants to toss a die as many times as possible. Find the expected number of operations done when the choices are made to maximize this number.

Constraints

  • 1 \leq N \leq 30000
  • 1 \leq A_{i,j} \leq {10}^{9}
  • A_{i,j} are all different.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_{1,1} A_{1,2} A_{1,3} A_{1,4} A_{1, 5} A_{1, 6}
A_{2,1} A_{2,2} A_{2,3} A_{2,4} A_{2, 5} A_{2, 6}
:
A_{N,1} A_{N,2} A_{N,3} A_{N,4} A_{N, 5} A_{N, 6}

Output

Print a real number representing the expected number of operations done. Your output is considered correct if the absolute or relative error from the judge's output is at most 10^{-6}.


Sample Input 1

2
1 2 3 4 5 6
7 8 9 10 11 12

Sample Output 1

3.64925355954377