E - Sigma Problem

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

配点 : 300

問題文

正整数 x,y に対して f(x,y) を「(x+y)10^8 で割ったあまり」として定義します。

長さ N の正整数列 A=(A_1,\ldots,A_N) が与えられます。次の式の値を求めてください。

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j)


制約

  • 2\leq N\leq 3\times 10^5
  • 1\leq A_i < 10^8
  • 入力される数値は全て整数

入力

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

N 
A_1 \ldots A_N

出力

答えを出力せよ。


入力例 1

3
3 50000001 50000002

出力例 1

100000012
  • f(A_1,A_2)=50000004
  • f(A_1,A_3)=50000005
  • f(A_2,A_3)=3

なので、答えは f(A_1,A_2)+f(A_1,A_3)+f(A_2,A_3) = 100000012 です。

総和を 10^8 で割ったあまりを求めるわけではないことに注意してください。


入力例 2

5
1 3 99999999 99999994 1000000

出力例 2

303999988

Score: 300 points

Problem Statement

For positive integers x and y, define f(x, y) as the remainder of (x + y) divided by 10^8.

You are given a sequence of positive integers A = (A_1, \ldots, A_N) of length N. Find the value of the following expression:

\displaystyle \sum_{i=1}^{N-1}\sum_{j=i+1}^N f(A_i,A_j).


Constraints

  • 2 \leq N \leq 3\times 10^5
  • 1 \leq A_i < 10^8
  • All input values are integers.

Input

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

N 
A_1 \ldots A_N

Output

Print the answer.


Sample Input 1

3
3 50000001 50000002

Sample Output 1

100000012
  • f(A_1,A_2)=50000004
  • f(A_1,A_3)=50000005
  • f(A_2,A_3)=3

Thus, the answer is f(A_1,A_2) + f(A_1,A_3) + f(A_2,A_3) = 100000012.

Note that you are not asked to compute the remainder of the sum divided by 10^8.


Sample Input 2

5
1 3 99999999 99999994 1000000

Sample Output 2

303999988
F - Martial artist

実行時間制限: 2 sec / メモリ制限: 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 - Merge Slimes

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

配点 : 425

問題文

最初、N 種類のサイズのスライムがいます。
具体的には、1\leq i\leq N について、サイズ S_i のスライムが C_i 匹います。

高橋君はスライムの合成を好きな順番で好きなだけ(0 回でも良い)繰り返すことができます。
スライムの合成では、次のことを行います。

  • 同じ サイズの 2 匹のスライムを選ぶ。選ばれたスライムのサイズが X であったとき、新しくサイズ 2X のスライムが出現する。合成後、選ばれた元のスライムは 2 匹とも消滅する。

高橋君はスライムの匹数を最小にしたいと考えています。 高橋君がうまく合成を繰り返した時、最小で何匹にすることができるでしょうか?

制約

  • 1\leq N\leq 10^5
  • 1\leq S_i\leq 10^9
  • 1\leq C_i\leq 10^9
  • S_1,S_2,\ldots,S_N はすべて異なる。
  • 入力はすべて整数

入力

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

N
S_1 C_1
S_2 C_2
\vdots
S_N C_N

出力

高橋君が合成を繰り返した後のスライムの匹数としてあり得る最小の数を出力せよ。


入力例 1

3
3 3
5 1
6 1

出力例 1

3

最初、サイズ 3 のスライムが 3 匹、サイズ 5 のスライムが 1 匹、サイズ 6 のスライムが 1 匹います。
高橋君は次のように合成を 2 回行うことができます。

  • まず、サイズ 3 のスライム 2 匹を選んで合成を行います。サイズ 3 のスライムが 1 匹、サイズ 5 のスライムが 1 匹、サイズ 6 のスライムが 2 匹となります。
  • 次に、サイズ 6 のスライム 2 匹を選んで合成を行います。サイズ 3 のスライムが 1 匹、サイズ 5 のスライムが 1 匹、サイズ 12 のスライムが 1 匹となります。

高橋君は最初の状態からどのように合成を繰り返してもスライムを 2 匹以下にすることはできないため、3 を出力します。


入力例 2

3
1 1
2 1
3 1

出力例 2

3

高橋君は合成を行うことができません。


入力例 3

1
1000000000 1000000000

出力例 3

13

Score : 425 points

Problem Statement

Initially, there are N sizes of slimes.
Specifically, for each 1\leq i\leq N, there are C_i slimes of size S_i.

Takahashi can repeat slime synthesis any number of times (possibly zero) in any order.
Slime synthesis is performed as follows.

  • Choose two slimes of the same size. Let this size be X, and a new slime of size 2X appears. Then, the two original slimes disappear.

Takahashi wants to minimize the number of slimes. What is the minimum number of slimes he can end up with by an optimal sequence of syntheses?

Constraints

  • 1\leq N\leq 10^5
  • 1\leq S_i\leq 10^9
  • 1\leq C_i\leq 10^9
  • S_1,S_2,\ldots,S_N are all different.
  • All input values are integers.

Input

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

N
S_1 C_1
S_2 C_2
\vdots
S_N C_N

Output

Print the minimum possible number of slimes after Takahashi has repeated the synthesis.


Sample Input 1

3
3 3
5 1
6 1

Sample Output 1

3

Initially, there are three slimes of size 3, one of size 5, and one of size 6.
Takahashi can perform the synthesis twice as follows:

  • First, perform the synthesis by choosing two slimes of size 3. There will be one slime of size 3, one of size 5, and two of size 6.
  • Next, perform the synthesis by choosing two slimes of size 6. There will be one slime of size 3, one of size 5, and one of size 12.

No matter how he repeats the synthesis from the initial state, he cannot reduce the number of slimes to 2 or less, so you should print 3.


Sample Input 2

3
1 1
2 1
3 1

Sample Output 2

3

He cannot perform the synthesis.


Sample Input 3

1
1000000000 1000000000

Sample Output 3

13
H - Karuta

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

配点 : 500

問題文

英小文字からなる文字列が N 個与えられます。i \, (i = 1, 2, \dots, N) 番目のものを S_i と表します。

二つの文字列 x, y に対し、以下の条件を全て満たす最大の整数 n\mathrm{LCP}(x, y) と表します。

  • x, y の長さはいずれも n 以上
  • 1 以上 n 以下の全ての整数 i に対し、xi 文字目と yi 文字目が等しい

全ての i = 1, 2, \dots, N に対し、以下の値を求めてください。

  • \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j)

制約

  • 2 \leq N \leq 5 \times 10^5
  • N は整数
  • S_i は英小文字からなる長さ 1 以上の文字列 (i = 1, 2, \dots, N)
  • S_i の長さの総和は 5 \times 10^5 以下

入力

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

N
S_1
S_2
\vdots
S_N

出力

N 行出力せよ。i \, (i = 1, 2, \dots, N) 行目には、\displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j) を出力せよ。


入力例 1

3
abc
abb
aac

出力例 1

2
2
1

\mathrm{LCP}(S_1, S_2) = 2, \mathrm{LCP}(S_1, S_3) = 1, \mathrm{LCP}(S_2, S_3) = 1 です。


入力例 2

11
abracadabra
bracadabra
racadabra
acadabra
cadabra
adabra
dabra
abra
bra
ra
a

出力例 2

4
3
2
1
0
1
0
4
3
2
1

Score : 500 points

Problem Statement

You are given N strings consisting of lowercase English letters. Let S_i be the i-th (i = 1, 2, \dots, N) of them.

For two strings x and y, \mathrm{LCP}(x, y) is defined to be the maximum integer n that satisfies all of the following conditions:

  • The lengths of x and y are both at least n.
  • For all integers i between 1 and n, inclusive, the i-th character of x and that of y are equal.

Find the following value for all i = 1, 2, \dots, N:

  • \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j)

Constraints

  • 2 \leq N \leq 5 \times 10^5
  • N is an integer.
  • S_i is a string of length at least 1 consisting of lowercase English letters (i = 1, 2, \dots, N).
  • The sum of lengths of S_i is at most 5 \times 10^5.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print N lines. The i-th (i = 1, 2, \dots, N) line should contain \displaystyle \max_{i \neq j} \mathrm{LCP}(S_i, S_j).


Sample Input 1

3
abc
abb
aac

Sample Output 1

2
2
1

\mathrm{LCP}(S_1, S_2) = 2, \mathrm{LCP}(S_1, S_3) = 1, and \mathrm{LCP}(S_2, S_3) = 1.


Sample Input 2

11
abracadabra
bracadabra
racadabra
acadabra
cadabra
adabra
dabra
abra
bra
ra
a

Sample Output 2

4
3
2
1
0
1
0
4
3
2
1
I - Exchange Game

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

配点 : 500

問題文

高橋君と青木君が、数の書かれたカードを使ってゲームをします。

最初、高橋君は A_1,\ldots,A_N が書かれた N 枚のカードを、青木君は B_1,\ldots,B_M が書かれた M 枚のカードを手札として持っており、場には C_1,\ldots,C_L が書かれた L 枚のカードがあります。
高橋君と青木君はゲーム中常に、相手の手札も含め、全てのカードに書かれた数を知っている状態にあります。

高橋君と青木君は、高橋君から順に次の行動を交互に行います。

  • 自分の手札から 1 枚選び場に出す。その後、出したカードに書かれていた数未満の数が書かれたカードが場にあれば、そのうち 1 枚を場から自分の手札に移して良い。

先に行動が行えなくなった方が負けであり、負けでない方が勝ちです。互いに最適に行動したとき、どちらが勝つか判定してください。

なおこのゲームは必ず有限回の行動で勝敗がつくことが証明できます。

制約

  • 1 \leq N,M,L
  • N+M+L \leq 12
  • 1 \leq A_i,B_i,C_i \leq 10^9
  • 入力は全て整数である

入力

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

N M L
A_1 \ldots A_N
B_1 \ldots B_M
C_1 \ldots C_L

出力

高橋君が勝つならば Takahashi、青木君が勝つならば Aoki と出力せよ。


入力例 1

1 1 2
2
4
1 3

出力例 1

Aoki

ゲームは例えば次のように進行します。(最適な行動とは限りません)

  • 高橋君が手札から 2 を場に出し、1 を場から自分の手札に移す。高橋君の手札は (1)、青木君の手札は (4)、場札は (2,3) となる。
  • 青木君が手札から 4 を場に出し、2 を場から自分の手札に移す。高橋君の手札は (1)、青木君の手札は (2)、場札は (3,4) となる。
  • 高橋君が手札から 1 を場に出す。高橋君の手札は ()、青木君の手札は (2)、場札は (1,3,4) となる。
  • 青木君が手札から 2 を場に出す。高橋君の手札は ()、青木君の手札は ()、場札は (1,2,3,4) となる。
  • 高橋君は行動できないため負けであり、青木君が勝ち。

入力例 2

4 4 4
98 98765 987654 987654321
987 9876 9876543 98765432
123 12345 1234567 123456789

出力例 2

Takahashi

入力例 3

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

出力例 3

Aoki

Score : 500 points

Problem Statement

Takahashi and Aoki will play a game using cards with numbers written on them.

Initially, Takahashi has N cards with numbers A_1, \ldots, A_N in his hand, Aoki has M cards with numbers B_1, \ldots, B_M in his hand, and there are L cards with numbers C_1, \ldots, C_L on the table.
Throughout the game, both Takahashi and Aoki know all the numbers on all the cards, including the opponent's hand.

Starting with Takahashi, they take turns performing the following action:

  • Choose one card from his hand and put it on the table. Then, if there is a card on the table with a number less than the number on the card he just played, he may take one such card from the table into his hand.

The player who cannot make a move first loses, and the other player wins. Determine who wins if both players play optimally.

It can be proved that the game always ends in a finite number of moves.

Constraints

  • 1 \leq N, M, L
  • N + M + L \leq 12
  • 1 \leq A_i, B_i, C_i \leq 10^9
  • All input values are integers.

Input

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

N M L
A_1 \ldots A_N
B_1 \ldots B_M
C_1 \ldots C_L

Output

Print Takahashi if Takahashi wins, and Aoki if Aoki wins.


Sample Input 1

1 1 2
2
4
1 3

Sample Output 1

Aoki

The game may proceed as follows (not necessarily optimal moves):

  • Takahashi plays 2 from his hand to the table, and takes 1 from the table into his hand. Now, Takahashi's hand is (1), Aoki's hand is (4), and the table cards are (2,3).
  • Aoki plays 4 from his hand to the table, and takes 2 into his hand. Now, Takahashi's hand is (1), Aoki's hand is (2), and the table cards are (3,4).
  • Takahashi plays 1 from his hand to the table. Now, Takahashi's hand is (), Aoki's hand is (2), and the table cards are (1,3,4).
  • Aoki plays 2 from his hand to the table. Now, Takahashi's hand is (), Aoki's hand is (), and the table cards are (1,2,3,4).
  • Takahashi cannot make a move and loses; Aoki wins.

Sample Input 2

4 4 4
98 98765 987654 987654321
987 9876 9876543 98765432
123 12345 1234567 123456789

Sample Output 2

Takahashi

Sample Input 3

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

Sample Output 3

Aoki