C - Ticket Gate Log

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

配点 : 250

問題文

高橋君は改札機の利用履歴を集計しました。 しかし、高橋君はうっかりいくつかの入退場記録を消してしまいました。 高橋君は消してしまった記録の復元を試みようとしています。

i, o のみからなる文字列 S が与えられます。 S の任意の位置に文字を 0 文字以上挿入することで、変更後の文字列が以下の条件を満たすようにしたいです。

  • 長さが偶数であり、奇数文字目が i で偶数文字目が o である。

挿入する必要のある文字数の最小値を求めて下さい。なお、問題の制約下で、有限個の文字を適切に挿入することで、S が条件をみたすようにできることが証明できます。

制約

  • Si, o からなる長さ 1 以上 100 以下の文字列

入力

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

S

出力

答えを出力せよ。


入力例 1

ioi

出力例 1

1

3 文字目のあとに o を挿入して ioio とすることで、条件を満たすことができます。0 文字以下の挿入で条件を満たすことはできません。


入力例 2

iioo

出力例 2

2

1 文字目のあとに o を、3 文字目のあとに i を挿入することで、条件を満たすことができます。1 文字以下の挿入で条件を満たすことはできません。


入力例 3

io

出力例 3

0

S がすでに条件を満たします。

Score : 250 points

Problem Statement

Takahashi aggregated usage records from ticket gates. However, he accidentally erased some records of entering and exiting stations. He is trying to restore the erased records.

You are given a string S consisting of i and o. We want to insert zero or more characters at arbitrary positions in S so that the resulting string satisfies the following conditions:

  • Its length is even, and every odd-numbered (1st, 3rd, ...) character is i while every even-numbered (2nd, 4th, ...) character is o.

Find the minimum number of characters that need to be inserted. It can be proved under the constraints of this problem that by inserting an appropriate finite number of characters, S can be made to satisfy the conditions.

Constraints

  • S is a string of length between 1 and 100, consisting of i and o.

Input

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

S

Output

Print the answer.


Sample Input 1

ioi

Sample Output 1

1

We can insert o after the 3rd character to form ioio to satisfy the conditions. The conditions cannot be satisfied by inserting zero or fewer characters.


Sample Input 2

iioo

Sample Output 2

2

We can insert o after the 1st character and i after the 3rd character to satisfy the conditions. The conditions cannot be satisfied by inserting one or fewer characters.


Sample Input 3

io

Sample Output 3

0

S already satisfies the conditions.

D - Sum of Geometric Series

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

配点 : 200

問題文

正整数 N, M が与えられます。

X = \displaystyle\sum_{i = 0}^{M} N^i とします。X \leq 10^9 のときは X の値を、X > 10^9 のときは文字列 inf を出力してください。

制約

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq 100
  • 入力される値はすべて整数

入力

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

N M

出力

問題文の指示に従って X の値あるいは inf を出力せよ。


入力例 1

7 3

出力例 1

400

X = 1 + 7 + 49 + 343 = 400 です。400 \leq 10^9 であるため 400 を出力します。


入力例 2

1000000 2

出力例 2

inf

X = 1000001000001 > 10^9 であるため、inf を出力します。


入力例 3

999999999 1

出力例 3

1000000000

入力例 4

998244353 99

出力例 4

inf

Score : 200 points

Problem Statement

You are given two positive integers N and M.

Let X = \displaystyle\sum_{i = 0}^{M} N^i. If X \leq 10^9, print the value of X. If X > 10^9, print inf.

Constraints

  • 1 \leq N \leq 10^9
  • 1 \leq M \leq 100
  • All input values are integers.

Input

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

N M

Output

Print the value of X or inf as specified by the problem statement.


Sample Input 1

7 3

Sample Output 1

400

X = 1 + 7 + 49 + 343 = 400. Since 400 \leq 10^9, print 400.


Sample Input 2

1000000 2

Sample Output 2

inf

X = 1000001000001 > 10^9, so print inf.


Sample Input 3

999999999 1

Sample Output 3

1000000000

Sample Input 4

998244353 99

Sample Output 4

inf
E - Yamanote Line Game

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

配点 : 300

問題文

高橋君と青木君は 2 人で次の対戦ゲームをします。

高橋君が先手でゲームを始め、ゲームが終了するまでの間、 2 人は交互に 1 以上 2N+1 以下の整数を 1 つずつ宣言します。 どちらかが一度でも宣言した整数は、それ以降どちらも二度と宣言することが出来ません。 先に整数を宣言することが出来なくなった方のプレイヤーの負けとなり、負けなかった方のプレイヤーの勝ちとなります。

このゲームでは必ず高橋君が勝ちます。 高橋君の立場で実際にゲームを行い、ゲームに勝ってください。

制約

  • 1 \leq N \leq 1000
  • N は整数

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
あなたのプログラムが高橋君の立場で、ジャッジプログラムが青木君の立場でゲームを行います。

まず、あなたのプログラムに標準入力から正の整数 N が与えられます。 その後、ゲームが終了するまで下記の手順を繰り返します。

  1. あなたのプログラムが、高橋君が宣言する整数として、1 以上 2N+1 以下の整数を標準出力に出力します。(どちらかのプレイヤーによってすでに宣言されている整数を出力することは出来ません。)
  2. ジャッジプログラムによって、青木君が宣言する整数があなたのプログラムに標準入力から与えられます。(どちらかのプレイヤーによってすでに宣言されている整数が入力されることはありません。) ただし、青木君が宣言できる整数が残っていない場合は、代わりに 0 が与えられ高橋君の勝ちでゲームが終了します。

注意点

  • 出力を行うたびに標準出力をflushしてください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 高橋君の勝ちでゲームが終了したあと、あなたのプログラムは直ちに終了しなければなりません。そうしなかった場合、ジャッジ結果が AC とならない可能性があります。
  • ゲームの途中で不正な出力を行った場合(例えば、すでにどちらかのプレイヤーによって宣言されている整数を出力した場合)は不正解となりますが、そのときのジャッジ結果は不定です。WA になるとは限りません。

入出力例

入力 出力 説明
2 まず整数 N が与えられます。
1 高橋君が 1 を宣言します。
3 青木君が 3 を宣言します。
2 高橋君が 2 を宣言します。
4 青木君が 4 を宣言します。
5 高橋君が 5 を宣言します。
0 青木君が宣言できる整数が残っていないため、高橋君の勝ちでゲームが終了します。

Score : 300 points

Problem Statement

Takahashi and Aoki will play the following game against each other.

Starting from Takahashi, the two alternatingly declare an integer between 1 and 2N+1 (inclusive) until the game ends. Any integer declared by either player cannot be declared by either player again. The player who is no longer able to declare an integer loses; the player who didn't lose wins.

In this game, Takahashi will always win. Your task is to actually play the game on behalf of Takahashi and win the game.

Constraints

  • 1 \leq N \leq 1000
  • N is an integer.

Input and Output

This task is an interactive task (in which your program and the judge program interact with each other via inputs and outputs).
Your program plays the game on behalf of Takahashi, and the judge program plays the game on behalf of Aoki.

First, your program is given a positive integer N from Standard Input. Then, the following procedures are repeated until the game ends.

  1. Your program outputs an integer between 1 and 2N+1 (inclusive) to Standard Output, which defines the integer that Takahashi declares. (You cannot output an integer that is already declared by either player.)
  2. The integer that Aoki declares is given by the judge program to your program from Standard Input. (No integer that is already declared by either player will be given.) If Aoki has no more integer to declare, 0 is given instead, which means that the game ended and Takahashi won.

Notes

  • After each output, you must flush Standard Output. Otherwise, you may get TLE.
  • After the game ended and Takahashi won, the program must be terminated immediately. Otherwise, the judge does not necessarily give AC.
  • If your program outputs something that violates the rules of the game (such as an integer that has already been declared by either player), your answer is considered incorrect. In such case, the verdict is indeterminate. It does not necessarily give WA.

Sample Input and Output

Input Output Description
2 First, an integer N is given.
1 Takahashi declares an integer 1.
3 Aoki declares an integer 3.
2 Takahashi declares an integer 2.
4 Aoki declares an integer 4.
5 Takahashi declares an integer 5.
0 Aoki has no more integer to declare, so Takahashi wins, and the game ends.
F - Make Isomorphic

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

配点 : 300

問題文

頂点 1, 頂点 2,\ldots, 頂点 NN 個の頂点からなる単純無向グラフ G,H が与えられます。 G には M _ G 本の辺があり、i 本目 (1\leq i\leq M _ G) の辺は頂点 u _ i と頂点 v _ i を結んでいます。 H には M _ H 本の辺があり、i 本目 (1\leq i\leq M _ H) の辺は頂点 a _ i と頂点 b _ i を結んでいます。

あなたは、グラフ H に対して次の操作を 0 回以上の好きな回数繰り返すことができます。

  • 1\leq i\lt j\leq N を満たす整数の組 (i,j) をひとつ選ぶ。A _ {i,j} 円を支払って、頂点 i と頂点 j を結ぶ辺がなければ追加し、あれば取り除く。

GH を同型にするために少なくとも何円支払う必要があるか求めてください。

単純無向グラフとは?

単純無向グラフとは、自己ループや多重辺を含まず、辺に向きの無いグラフのことをいいます。

グラフの同型とは?

N 頂点のグラフ GH同型であるとは、ある (1,2,\ldots,N) の並べ替え (P _ 1,P _ 2,\ldots,P _ N) が存在して、どの 1\leq i\lt j\leq N に対しても

  • G に頂点 i, 頂点 j を結ぶ辺が存在するとき、かつそのときに限り H に頂点 P _ i, 頂点 P _ j を結ぶ辺が存在する
が成り立つことをいいます。

制約

  • 1\leq N\leq8
  • 0\leq M _ G\leq\dfrac{N(N-1)}2
  • 0\leq M _ H\leq\dfrac{N(N-1)}2
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M _ G)
  • (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M _ G)
  • 1\leq a _ i\lt b _ i\leq N\ (1\leq i\leq M _ H)
  • (a _ i,b _ i)\neq(a _ j,b _ j)\ (1\leq i\lt j\leq M _ H)
  • 1\leq A _ {i,j}\leq 10 ^ 6\ (1\leq i\lt j\leq N)
  • 入力はすべて整数

入力

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

N
M _ G
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {M _ G} v _ {M _ G}
M _ H
a _ 1 b _ 1
a _ 2 b _ 2
\vdots
a _ {M _ H} b _ {M _ H}
A _ {1,2} A _ {1,3} \ldots A _ {1,N}
A _ {2,3} \ldots A _ {2,N}
\vdots
A _ {N-1,N}

出力

答えを出力せよ。


入力例 1

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

出力例 1

9

与えられたグラフはそれぞれ以下のようになります。

たとえば、H に対して次のような 4 つの操作を順に行うことで、9 円を支払ってGH を同型にすることができます。

  • (i,j)=(1,3) として操作を行う。H には頂点 1 と頂点 3 を結ぶ辺があるので、1 円を支払ってこれを取り除く。
  • (i,j)=(2,5) として操作を行う。H には頂点 2 と頂点 5 を結ぶ辺はないので、2 円を支払ってこれを追加する。
  • (i,j)=(1,5) として操作を行う。H には頂点 1 と頂点 5 を結ぶ辺があるので、1 円を支払ってこれを取り除く。
  • (i,j)=(3,5) として操作を行う。H には頂点 3 と頂点 5 を結ぶ辺はないので、5 円を支払ってこれを追加する。

操作の結果、H は以下のようになります。

支払う金額を 8 円以下にして GH を同型にすることはできないため、9 を出力してください。


入力例 2

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

出力例 2

3

たとえば、(i,j)=(2,3),(2,4),(3,4) とした 3 回の操作を行うことで GH を同型にすることができます。


入力例 3

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

出力例 3

5

たとえば、(i,j)=(4,5) とした 1 回の操作を行うことで GH を同型にすることができます。


入力例 4

2
0
0
371

出力例 4

0

GH には辺が含まれていないこともあります。 また、一度も操作をする必要がないこともあります。


入力例 5

8
13
1 8
5 7
4 6
1 5
7 8
1 6
1 2
5 8
2 6
5 6
6 7
3 7
4 8
15
3 5
1 7
4 6
3 8
7 8
1 2
5 6
1 6
1 5
1 4
2 8
2 6
2 4
4 7
1 3
7483 1694 5868 3296 9723 5299 4326
5195 4088 5871 1384 2491 6562
1149 6326 2996 9845 7557
4041 7720 1554 5060
8329 8541 3530
4652 3874
3748

出力例 5

21214

Score : 300 points

Problem Statement

You are given simple undirected graphs G and H, each with N vertices: vertices 1, 2, \ldots, N. Graph G has M_G edges, and its i-th edge (1\leq i\leq M_G) connects vertices u_i and v_i. Graph H has M_H edges, and its i-th edge (1\leq i\leq M_H) connects vertices a_i and b_i.

You can perform the following operation on graph H any number of times, possibly zero.

  • Choose a pair of integers (i,j) satisfying 1\leq i<j\leq N. Pay A_{i,j} yen, and if there is no edge between vertices i and j in H, add one; if there is, remove it.

Find the minimum total cost required to make G and H isomorphic.

What is a simple undirected graph?

A simple undirected graph is a graph without self-loops or multi-edges, where edges have no direction.

What does it mean for graphs to be isomorphic?

Two graphs G and H with N vertices are isomorphic if and only if there exists a permutation (P_1,P_2,\ldots,P_N) of (1,2,\ldots,N) such that for all 1\leq i\lt j\leq N:

  • an edge exists between vertices i and j in G if and only if an edge exists between vertices P_i and P_j in H.

Constraints

  • 1\leq N\leq8
  • 0\leq M _ G\leq\dfrac{N(N-1)}2
  • 0\leq M _ H\leq\dfrac{N(N-1)}2
  • 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M _ G)
  • (u _ i,v _ i)\neq(u _ j,v _ j)\ (1\leq i\lt j\leq M _ G)
  • 1\leq a _ i\lt b _ i\leq N\ (1\leq i\leq M _ H)
  • (a _ i,b _ i)\neq(a _ j,b _ j)\ (1\leq i\lt j\leq M _ H)
  • 1\leq A _ {i,j}\leq 10 ^ 6\ (1\leq i\lt j\leq N)
  • All input values are integers.

Input

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

N
M _ G
u _ 1 v _ 1
u _ 2 v _ 2
\vdots
u _ {M _ G} v _ {M _ G}
M _ H
a _ 1 b _ 1
a _ 2 b _ 2
\vdots
a _ {M _ H} b _ {M _ H}
A _ {1,2} A _ {1,3} \ldots A _ {1,N}
A _ {2,3} \ldots A _ {2,N}
\vdots
A _ {N-1,N}

Output

Print the answer.


Sample Input 1

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

Sample Output 1

9

The given graphs are as follows:

For example, you can perform the following four operations on H to make it isomorphic to G at a cost of 9 yen.

  • Choose (i,j)=(1,3). There is an edge between vertices 1 and 3 in H, so pay 1 yen to remove it.
  • Choose (i,j)=(2,5). There is no edge between vertices 2 and 5 in H, so pay 2 yen to add it.
  • Choose (i,j)=(1,5). There is an edge between vertices 1 and 5 in H, so pay 1 yen to remove it.
  • Choose (i,j)=(3,5). There is no edge between vertices 3 and 5 in H, so pay 5 yen to add it.

After these operations, H becomes:

You cannot make G and H isomorphic at a cost less than 9 yen, so print 9.


Sample Input 2

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

Sample Output 2

3

For example, performing the operations (i,j)=(2,3),(2,4),(3,4) on H will make it isomorphic to G.


Sample Input 3

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

Sample Output 3

5

For example, performing the operation (i,j)=(4,5) once will make G and H isomorphic.


Sample Input 4

2
0
0
371

Sample Output 4

0

Note that G and H may have no edges.

Also, it is possible that no operations are needed.


Sample Input 5

8
13
1 8
5 7
4 6
1 5
7 8
1 6
1 2
5 8
2 6
5 6
6 7
3 7
4 8
15
3 5
1 7
4 6
3 8
7 8
1 2
5 6
1 6
1 5
1 4
2 8
2 6
2 4
4 7
1 3
7483 1694 5868 3296 9723 5299 4326
5195 4088 5871 1384 2491 6562
1149 6326 2996 9845 7557
4041 7720 1554 5060
8329 8541 3530
4652 3874
3748

Sample Output 5

21214
G - Goin' to the Zoo

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

配点 : 400

問題文

AtCoder 国には動物園が N 園あり、1 から N の番号がついています。動物園 i の入園料は C_i 円です。

鈴木さんは M 種類の動物、動物 1,\ldots,M が好きです。
動物 iK_i 園の動物園 A_{i,1},\dots, A_{i,K_i} で見ることができます。

M 種類の動物全てを 2 度以上ずつ見るために必要な入園料の合計の最小値を求めてください。
なお、同じ動物園を複数回訪れた場合、その動物園の動物は訪れた回数だけ見たとみなします。

制約

  • 1\leq N \leq 10
  • 1\leq M \leq 100
  • 0\leq C_i \leq 10^9
  • 1\leq K_i \leq N
  • 1 \leq A_{i,j} \leq N
  • j \neq j' \Longrightarrow A_{i,j}\neq A_{i,j'}
  • 入力は全て整数

入力

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

N M
C_1 \dots C_N
K_1 A_{1,1} \dots A_{1,K_1}
\vdots
K_M A_{M,1} \dots A_{M,K_M}

出力

答えを出力せよ。


入力例 1

4 3
1000 300 700 200
3 1 3 4
3 1 2 4
2 1 3

出力例 1

1800

以下のようにすることで、1800 円で動物 1,2,32 度以上ずつ見ることができます。

  • 動物園 3 に行く。入園料 700 円を払い、動物 1,3 を見る。
  • 動物園 3 に行く。入園料 700 円を払い、動物 1,3 を見る。
  • 動物園 4 に行く。入園料 200 円を払い、動物 1,2 を見る。
  • 動物園 4 に行く。入園料 200 円を払い、動物 1,2 を見る。

入力例 2

7 6
500 500 500 500 500 500 1000
3 1 2 7
3 2 3 7
3 3 4 7
3 4 5 7
3 5 6 7
3 6 1 7

出力例 2

2000

動物園 72 度行くことで、合計 2000 円で動物 1,2,3,4,5,62 度ずつ見ることができます。

Score : 400 points

Problem Statement

In the country of AtCoder there are N zoos, numbered 1 to N. The admission fee for zoo i is C_i yen.

Mr. Suzuki likes M kinds of animals, animals 1,\dots,M.
Animal i can be seen at K_i zoos, namely zoos A_{i,1},\dots,A_{i,K_i}.

Find the minimum total admission fee required to see all M kinds of animals at least twice each.
If you visit the same zoo multiple times, the animals there are considered seen once per every visit.

Constraints

  • 1 \le N \le 10
  • 1 \le M \le 100
  • 0 \le C_i \le 10^9
  • 1 \le K_i \le N
  • 1 \le A_{i,j} \le N
  • j \neq j' \Longrightarrow A_{i,j} \neq A_{i,j'}
  • All input values are integers.

Input

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

N M
C_1 \dots C_N
K_1 A_{1,1} \dots A_{1,K_1}
\vdots
K_M A_{M,1} \dots A_{M,K_M}

Output

Output the minimum total admission fee.


Sample Input 1

4 3
1000 300 700 200
3 1 3 4
3 1 2 4
2 1 3

Sample Output 1

1800

For example, the following schedule achieves seeing animals 1,2,3 at least twice each for a total of 1800 yen:

  • Go to zoo 3. Pay 700 yen and see animals 1 and 3.
  • Go to zoo 3. Pay 700 yen and see animals 1 and 3.
  • Go to zoo 4. Pay 200 yen and see animals 1 and 2.
  • Go to zoo 4. Pay 200 yen and see animals 1 and 2.

Sample Input 2

7 6
500 500 500 500 500 500 1000
3 1 2 7
3 2 3 7
3 3 4 7
3 4 5 7
3 5 6 7
3 6 1 7

Sample Output 2

2000

By visiting zoo 7 twice, you can see animals 1,2,3,4,5,6 twice each for a total of 2000 yen.