実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
英小文字からなる文字列 S が与えられます。
ここで、英小文字に含まれるドットの個数を以下のようにして定めます。
- 英小文字が
iまたはjであるとき : 1 個 - 英小文字が
i,jのいずれでもないとき : 0 個
S のすべての文字に対するドットの個数の和を求めてください。
制約
- S は長さ 1 以上 10 以下の英小文字からなる文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
aiiaj
出力例 1
3
a, i, i, a, j に含まれるドットの個数はそれぞれ 0, 1, 1, 0, 1 です。
0 + 1 + 1 + 0 + 1 = 3 であるため、3 を出力します。
入力例 2
abcedfgh
出力例 2
0
入力例 3
jjjjjj
出力例 3
6
Score : 100 points
Problem Statement
You are given a string S consisting of lowercase English letters.
Here, we define the number of dots in a lowercase English letter as follows:
- If the lowercase English letter is
iorj: 1 dot - If the lowercase English letter is neither
inorj: 0 dots
Find the sum of the numbers of dots over all characters in S.
Constraints
- S is a string consisting of lowercase English letters with length between 1 and 10, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
aiiaj
Sample Output 1
3
The numbers of dots in a, i, i, a, j are 0, 1, 1, 0, 1, respectively.
We have 0 + 1 + 1 + 0 + 1 = 3, so print 3.
Sample Input 2
abcedfgh
Sample Output 2
0
Sample Input 3
jjjjjj
Sample Output 3
6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
文字列が N 個与えられます。
i 番目 (1\leq i\leq N) に与えられる文字列 S _ i は Takahashi か Aoki のどちらかと等しいです。
S _ i が Takahashi と等しい i がいくつあるか求めてください。
制約
- 1\leq N\leq 100
- N は整数
- S _ i は
TakahashiかAokiのいずれか (1\leq i\leq N)
入力
入力は以下の形式で標準入力から与えられる。
N S _ 1 S _ 2 \vdots S _ N
出力
S _ i が Takahashi と等しい i の個数を整数として一行に出力せよ。
入力例 1
3 Aoki Takahashi Takahashi
出力例 1
2
S _ 2,S _ 3 の 2 つが Takahashi と等しく、S _ 1 はそうではありません。
よって、2 を出力してください。
入力例 2
2 Aoki Aoki
出力例 2
0
Takahashi と等しい S _ i が存在しないこともあります。
入力例 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
出力例 3
7
Score : 100 points
Problem Statement
You are given N strings.
The i-th string S_i (1 \leq i \leq N) is either Takahashi or Aoki.
How many i are there such that S_i is equal to Takahashi?
Constraints
- 1 \leq N \leq 100
- N is an integer.
- Each S_i is
TakahashiorAoki. (1 \leq i \leq N)
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print the count of i such that S_i is equal to Takahashi as an integer in a single line.
Sample Input 1
3 Aoki Takahashi Takahashi
Sample Output 1
2
S_2 and S_3 are equal to Takahashi, while S_1 is not.
Therefore, print 2.
Sample Input 2
2 Aoki Aoki
Sample Output 2
0
It is possible that no S_i is equal to Takahashi.
Sample Input 3
20 Aoki Takahashi Takahashi Aoki Aoki Aoki Aoki Takahashi Aoki Aoki Aoki Takahashi Takahashi Aoki Takahashi Aoki Aoki Aoki Aoki Takahashi
Sample Output 3
7
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君は、横一列に並んだ 100 個の鍵盤からなるピアノを持っています。 このピアノの左から i 個目の鍵盤のことを鍵盤 i と呼びます。
高橋君は今から N 回にわたってこのピアノの鍵盤を一つずつ押すことでとある曲を演奏します。
i 回目に押す鍵盤は鍵盤 A_i であり、それを押す手は S_i= L のとき左手、S_i= R のとき右手です。
演奏を始める前、高橋君は両手をそれぞれ好きな鍵盤の上に置くことができ、この時点での疲労度は 0 です。 演奏中、片方の手を鍵盤 x の上から鍵盤 y の上へと動かすと疲労度が |y-x| 増加します(逆に、手の移動以外で疲労度が増加することはありません)。 なお、ある手である鍵盤を押すためには、その手がその鍵盤の上に置かれている必要があります。
演奏が終了した時点での疲労度は最小でいくつになるか求めてください。
制約
- 1\leq N \leq 100
- 1\leq A_i \leq 100
- N,A_i は整数
- S_i は
LまたはR
入力
入力は以下の形式で標準入力から与えられる。
N A_1 S_1 A_2 S_2 \vdots A_N S_N
出力
演奏が終了した時点での疲労度の最小値を出力せよ。
入力例 1
4 3 L 6 R 9 L 1 R
出力例 1
11
例えば以下のように演奏することができます。
- 最初、左手を鍵盤 3 の上に、右手を鍵盤 6 の上に置いておく。
- 左手で鍵盤 3 を押す。
- 右手で鍵盤 6 を押す。
- 左手を鍵盤 3 の上から鍵盤 9 の上へと動かす。疲労度が |9-3|=6 増加する。
- 右手を鍵盤 6 の上から鍵盤 1 の上へと動かす。疲労度が |1-6|=5 増加する。
- 左手で鍵盤 9 を押す。
- 右手で鍵盤 1 を押す。
このとき、演奏が終了した時点での疲労度は 6+5=11 であり、これが最小です。
入力例 2
3 2 L 2 L 100 L
出力例 2
98
入力例 3
8 22 L 75 L 26 R 45 R 72 R 81 R 47 L 29 R
出力例 3
188
Score : 200 points
Problem Statement
Takahashi has a piano with 100 keys arranged in a row. The i-th key from the left is called key i.
He will play music by pressing N keys one by one.
For the i-th press, he will press key A_i, using his left hand if S_i= L, and his right hand if S_i= R.
Before starting to play, he can place both of his hands on any keys he likes, and his fatigue level at this point is 0. During the performance, if he moves one hand from key x to key y, the fatigue level increases by |y-x| (conversely, the fatigue level does not increase for any reason other than moving hands). To press a certain key with a hand, that hand must be placed on that key.
Find the minimum possible fatigue level at the end of the performance.
Constraints
- 1 \leq N \leq 100
- 1 \leq A_i \leq 100
- N and A_i are integers.
- S_i is
LorR.
Input
The input is given from Standard Input in the following format:
N A_1 S_1 A_2 S_2 \vdots A_N S_N
Output
Print the minimum fatigue level at the end of the performance.
Sample Input 1
4 3 L 6 R 9 L 1 R
Sample Output 1
11
For example, the performance can be done as follows:
- Initially, place the left hand on key 3 and the right hand on key 6.
- Press key 3 with the left hand.
- Press key 6 with the right hand.
- Move the left hand from key 3 to key 9. The fatigue level increases by |9-3| = 6.
- Move the right hand from key 6 to key 1. The fatigue level increases by |1-6| = 5.
- Press key 9 with the left hand.
- Press key 1 with the right hand.
In this case, the fatigue level at the end of the performance is 6+5 = 11, which is the minimum possible.
Sample Input 2
3 2 L 2 L 100 L
Sample Output 2
98
Sample Input 3
8 22 L 75 L 26 R 45 R 72 R 81 R 47 L 29 R
Sample Output 3
188
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君の勤めている会社には N 人の社員がおり、各社員には 1,2,\dots,N の社員番号が割り当てられています。 社内には M 個の部門があり、部門 1,2,\dots,M と呼ばれています。
社員番号 i の社員は、今期は部門 A_i に所属しており、来期は部門 B_i に所属します。
部門 1,2,\dots,M それぞれに対して、来期の所属人数から今期の所属人数を引いた値を求めてください。
制約
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq A_i \leq M
- 1 \leq B_i \leq M
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
M 行出力せよ。j 行目には部門 j についての答えを出力せよ。
入力例 1
5 4 1 2 2 1 3 1 2 2 2 4
出力例 1
1 -1 -1 1
- 部門 1 について、今期は社員番号 1 の 1 人が所属し、来期は社員番号 2,3 の 2 人が所属します。
- 部門 2 について、今期は社員番号 2,4,5 の 3 人が所属し、来期は社員番号 1,4 の 2 人が所属します。
- 部門 3 について、今期は社員番号 3 の 1 人が所属し、来期は誰も所属しません。
- 部門 4 について、今期は誰も所属せず、来期は社員番号 5 の 1 人が所属します。
入力例 2
10 5 3 2 3 4 1 2 2 2 4 4 3 1 3 4 4 2 3 3 3 2
出力例 2
0 4 -5 1 0
Score : 200 points
Problem Statement
The company where Takahashi works has N employees, each assigned an employee number from 1, 2, \dots, N. There are M departments in the company, called departments 1, 2, \dots, M.
Employee i belongs to department A_i this term and will belong to department B_i next term.
For each of departments 1, 2, \dots, M, find the number of members next term minus the number of members this term.
Constraints
- 1 \leq N \leq 100
- 1 \leq M \leq 100
- 1 \leq A_i \leq M
- 1 \leq B_i \leq M
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Output M lines. The j-th line should contain the answer for department j.
Sample Input 1
5 4 1 2 2 1 3 1 2 2 2 4
Sample Output 1
1 -1 -1 1
- For department 1: this term, one employee (employee number 1) belongs; next term, two employees (employee numbers 2, 3) belong.
- For department 2: this term, three employees (employee numbers 2, 4, 5) belong; next term, two employees (employee numbers 1, 4) belong.
- For department 3: this term, one employee (employee number 3) belongs; next term, no one belongs.
- For department 4: this term, no one belongs; next term, one employee (employee number 5) belongs.
Sample Input 2
10 5 3 2 3 4 1 2 2 2 4 4 3 1 3 4 4 2 3 3 3 2
Sample Output 2
0 4 -5 1 0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
1 から 2N の番号がついた 2N 人でじゃんけん大会をします。
大会は M ラウンドからなり、各ラウンドは、全ての人が 1 度ずつ参加するような 1 対 1 の N 試合からなります。
i=0,1,\ldots,M について、i ラウンド目の終了時点での順位を次のように決めます。
- i ラウンド目までの勝数が多い方が上位
- i ラウンド目までの勝数が同じときは、番号が小さい方が上位
また、i=1,\ldots,M について、i ラウンド目の各試合の組み合わせを次のように決めます。
- 各 k=1,2,\ldots,N について、i-1 ラウンド目終了時点の順位が 2k-1 位の人と 2k 位の人が試合をする
各試合では、対戦する 2 人がそれぞれ 1 度だけ手を出し、勝ち・負け・引き分けのいずれかの結果が発生します。
未来予知ができる高橋君は、人 i が j ラウンド目の試合で出す手が A_{i,j} であることを知っています。
A_{i,j} は G, C, P のいずれかであり、それぞれグー、チョキ、パーを表します。
M ラウンド目終了時点の順位を求めてください。
じゃんけんのルール
じゃんけんの結果は、2 人の出した手に応じて次のように決まります。- 一方がグーで他方がチョキのとき、グーを出した人が勝ち、チョキを出した人は負け
- 一方がチョキで他方がパーのとき、チョキを出した人が勝ち、パーを出した人は負け
- 一方がパーで他方がグーのとき、パーを出した人が勝ち、グーを出した人は負け
- 両者が同じ手を出したとき、引き分け
制約
- 1 \leq N \leq 50
- 1 \leq M \leq 100
- A_{i,j} は
G,C,Pのいずれか
入力
入力は以下の形式で標準入力から与えられる。
N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}
出力
2N 行出力せよ。
i 行目には、M ラウンド目終了時点での順位が i 位である人の番号を出力せよ。
入力例 1
2 3 GCP PPP CCC PPC
出力例 1
3 1 2 4
1 ラウンド目では人 1 と 2、3 と 4 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2 と 3、1 と 4 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 1 が勝ちます。
3 ラウンド目では人 3 と 1、2 と 4 がそれぞれ試合をし、前者の試合は人 3 が、後者の試合は人 4 が勝ちます。
よって最終的な順位は、上位から順に人 3,1,2,4 となります。
入力例 2
2 2 GC PG CG PP
出力例 2
1 2 3 4
1 ラウンド目では人 1 と 2、3 と 4 がそれぞれ試合をし、前者の試合は人 2 が、後者の試合は人 3 が勝ちます。
2 ラウンド目では人 2 と 3、1 と 4 がそれぞれ試合をし、前者の試合は引き分け、後者の試合は人 1 が勝ちます。
よって最終的な順位は、上位から順に人 1,2,3,4 となります。
Score : 300 points
Problem Statement
2N players, with ID numbers 1 through 2N, will participate in a rock-scissors-paper contest.
The contest has M rounds. Each round has N one-on-one matches, where each player plays in one of them.
For each i=0, 1, \ldots, M, the players' ranks at the end of the i-th round are determined as follows.
- A player with more wins in the first i rounds ranks higher.
- Ties are broken by ID numbers: a player with a smaller ID number ranks higher.
Additionally, for each i=1, \ldots, M, the matches in the i-th round are arranged as follows.
- For each k=1, 2, \ldots, N, a match is played between the players who rank (2k-1)-th and 2k-th at the end of the (i-1)-th round.
In each match, the two players play a hand just once, resulting in one player's win and the other's loss, or a draw.
Takahashi, who can foresee the future, knows that Player i will play A_{i, j} in their match in the j-th round, where A_{i,j} is G, C, or P.
Here, G stands for rock, C stands for scissors, and P stands for paper. (These derive from Japanese.)
Find the players' ranks at the end of the M-th round.
Rules of rock-scissors-paper
The result of a rock-scissors-paper match is determined as follows, based on the hands played by the two players.- If one player plays rock (G) and the other plays scissors (C), the player playing rock (G) wins.
- If one player plays scissors (C) and the other plays paper (P), the player playing scissors (C) wins.
- If one player plays paper (P) and the other plays rock (G), the player playing paper (P) wins.
- If the players play the same hand, the match is drawn.
Constraints
- 1 \leq N \leq 50
- 1 \leq M \leq 100
- A_{i,j} is
G,C, orP.
Input
Input is given from Standard Input in the following format:
N M
A_{1,1}A_{1,2}\ldots A_{1,M}
A_{2,1}A_{2,2}\ldots A_{2,M}
\vdots
A_{2N,1}A_{2N,2}\ldots A_{2N,M}
Output
Print 2N lines.
The i-th line should contain the ID number of the player who ranks i-th at the end of the M-th round.
Sample Input 1
2 3 GCP PPP CCC PPC
Sample Output 1
3 1 2 4
In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. Player 3 wins the former, and Player 1 wins the latter.
In the third round, matches are played between Players 3 and 1, and between Players 2 and 4. Player 3 wins the former, and Player 4 wins the latter.
Therefore, in the end, the players are ranked in the following order: 3,1,2,4, from highest to lowest.
Sample Input 2
2 2 GC PG CG PP
Sample Output 2
1 2 3 4
In the first round, matches are played between Players 1 and 2, and between Players 3 and 4. Player 2 wins the former, and Player 3 wins the latter.
In the second round, matches are played between Players 2 and 3, and between Players 1 and 4. The former is drawn, and Player 1 wins the latter.
Therefore, in the end, the players are ranked in the following order: 1,2,3,4, from highest to lowest.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 350 点
問題文
正の整数 X は、次の条件をみたすときかつその時に限り、良い整数と呼ばれます。
- 正の整数の組 (a,b) を用いて、X=2^a\times b^2 と書ける。
例えば、400 は 400=2^2\times 10^2 と書けるため、良い整数です。
正の整数 N が与えられるので、1 以上 N 以下の良い整数の個数を求めてください。
制約
- 1 \leq N \leq 10^{18}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
1 以上 N 以下の良い整数の個数を出力せよ。
入力例 1
20
出力例 1
5
1 以上 20 以下の良い整数は 2,4,8,16,18 の 5 つです。
よって、5 を出力します。
入力例 2
400
出力例 2
24
入力例 3
1234567890
出力例 3
42413
入力が 32bit 整数型に収まるとは限らないことに注意してください。
Score : 350 points
Problem Statement
A positive integer X is called a good integer if and only if it satisfies the following condition:
- There exists a pair of positive integers (a,b) such that X = 2^a \times b^2.
For example, 400 is a good integer because 400 = 2^2 \times 10^2.
Given a positive integer N, find the number of good integers between 1 and N, inclusive.
Constraints
- 1 \leq N \leq 10^{18}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the number of good integers between 1 and N, inclusive.
Sample Input 1
20
Sample Output 1
5
There are five good integers between 1 and 20: 2, 4, 8, 16, and 18.
Thus, print 5.
Sample Input 2
400
Sample Output 2
24
Sample Input 3
1234567890
Sample Output 3
42413
Note that the input might not fit in a 32-bit integer type.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
0, 1 からなる長さ N の文字列 S が与えられます。
0, 1 からなる長さ N の文字列 T は以下の条件を満たすとき、またそのときに限り 良い文字列 であると定義します。
- 1 \leq i \leq N - 1 を満たす整数 i であって、T の i 文字目と i + 1 文字目が一致するようなものがちょうど 1 つ存在する。
i = 1,2,\ldots, N について以下の操作を 1 度行うか行わないか選ぶことができます。
- S の i 文字目が
0であるとき S の i 文字目を1に、そうでないとき S の i 文字目を0に置き換える。操作を行った場合、C_i のコストがかかる。
S を良い文字列にするために必要なコストの総和の最小値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- S は長さ N の
0,1からなる文字列 - 1 \leq C_i \leq 10^9
- N, C_i は整数
入力
入力は以下の形式で標準入力から与えられる。
N S C_1 C_2 \ldots C_N
出力
答えを出力せよ。
入力例 1
5 00011 3 9 2 6 4
出力例 1
7
i = 1, 5 に対して操作を行い、i = 2, 3, 4 に対して操作を行わないことで S = 10010 となり、S は良い文字列となります。このときかかるコストは 7 であり、コスト 7 未満で S を良い文字列にすることはできないため、7 を出力します。
入力例 2
4 1001 1 2 3 4
出力例 2
0
入力例 3
11 11111100111 512298012 821282085 543342199 868532399 690830957 973970164 928915367 954764623 923012648 540375785 925723427
出力例 3
2286846953
Score: 400 points
Problem Statement
You are given a string S of length N consisting of 0 and 1.
A string T of length N consisting of 0 and 1 is a good string if and only if it satisfies the following condition:
- There is exactly one integer i such that 1 \leq i \leq N - 1 and the i-th and (i + 1)-th characters of T are the same.
For each i = 1,2,\ldots, N, you can choose whether or not to perform the following operation once:
- If the i-th character of S is
0, replace it with1, and vice versa. The cost of this operation, if performed, is C_i.
Find the minimum total cost required to make S a good string.
Constraints
- 2 \leq N \leq 2 \times 10^5
- S is a string of length N consisting of
0and1. - 1 \leq C_i \leq 10^9
- N and C_i are integers.
Input
The input is given from Standard Input in the following format:
N S C_1 C_2 \ldots C_N
Output
Print the answer.
Sample Input 1
5 00011 3 9 2 6 4
Sample Output 1
7
Performing the operation for i = 1, 5 and not performing it for i = 2, 3, 4 makes S = 10010, which is a good string. The cost incurred in this case is 7, and it is impossible to make S a good string for less than 7, so print 7.
Sample Input 2
4 1001 1 2 3 4
Sample Output 2
0
Sample Input 3
11 11111100111 512298012 821282085 543342199 868532399 690830957 973970164 928915367 954764623 923012648 540375785 925723427
Sample Output 3
2286846953
実行時間制限: 5 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
N 個のボールが左右一列に並んでいます。
左から i (1\leq i\leq N) 番目のボールは色 C_i で、価値は V_i です。
高橋君はこの列から ちょうど K 個のボールを取り除いたうえで、 残ったボールを元の順番で並べたときに同じ色のボールが隣り合わないようにしたいと考えています。 また、その条件のもとで、列に残ったボールの価値の総和をなるべく大きくしたいと考えています。
高橋君が、残ったボールの列において同じ色のボールが隣り合わないように K 個のボールを取り除くことができるか判定し、 できる場合は列に残ったボールの価値の総和としてあり得る最大の値を求めてください。
制約
- 1\leq K<N\leq 2\times 10^5
- K\leq 500
- 1\leq C_i\leq N
- 1\leq V_i\leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N K C_1 V_1 C_2 V_2 \vdots C_N V_N
出力
高橋君が同じ色のボールが隣り合わないようにK 個のボールを取り除くことができる場合は、 列に残ったボールの価値の総和としてあり得る最大値を整数で出力せよ。 できない場合は、-1 を出力せよ。
入力例 1
5 2 1 1 3 5 3 3 1 4 1 2
出力例 1
10
左から、3,5 番目のボールを取り除くと、残ったボールは左から順に色 1,3,1 であるため、
どの隣り合う 2 つのボールの色も異なり、条件をみたしています。
このとき、列に残ったボールの価値の和は V_1+V_2+V_4=1+5+4=10 です。
他にも 5 つのボールから 2 つのボールを取り除く方法であって、同じ色のボールが隣り合わないようにできるものは存在しますが、
3,5 番目のボールを取り除いた時に残ったボールの価値の和は最大となります。
よって、10 を出力します。
入力例 2
3 1 1 10 1 10 1 10
出力例 2
-1
どのようにボールを 1 つ取り除いても色 1 のボールが隣り合ってしまいます。
よって、 -1 を出力します。
入力例 3
3 1 1 1 2 2 3 3
出力例 3
5
必ずちょうど K 個のボールを取り除く必要があることに注意してください。
Score: 525 points
Problem Statement
There are N balls lined up in a row.
The i-th ball from the left is of color C_i and has a value of V_i.
Takahashi wants to remove exactly K balls from this row so that no two adjacent balls have the same color when arranging the remaining balls without changing the order. Additionally, under that condition, he wants to maximize the total value of the balls remaining in the row.
Determine if Takahashi can remove K balls so that no two adjacent balls in the remaining row have the same color. If it is possible, find the maximum possible total value of the remaining balls.
Constraints
- 1\leq K<N\leq 2\times 10^5
- K\leq 500
- 1\leq C_i\leq N
- 1\leq V_i\leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K C_1 V_1 C_2 V_2 \vdots C_N V_N
Output
If Takahashi can remove K balls so that no two adjacent balls in the remaining row have the same color, print the maximum possible total value of the remaining balls as an integer. Otherwise, print -1.
Sample Input 1
5 2 1 1 3 5 3 3 1 4 1 2
Sample Output 1
10
After removing the 3-rd and 5-th balls from the left, the remaining balls are of colors 1, 3, 1 from left to right, so no two adjacent balls have the same color, satisfying the condition.
The total value of the remaining balls is V_1+V_2+V_4=1+5+4=10.
There are other ways to remove two balls from the five balls so that no two adjacent balls have the same color, but the total value of the remaining balls is maximized when removing the 3-rd and 5-th balls.
Hence, print 10.
Sample Input 2
3 1 1 10 1 10 1 10
Sample Output 2
-1
No matter how you remove one ball, balls of color 1 will end up next to each other.
Hence, print -1.
Sample Input 3
3 1 1 1 2 2 3 3
Sample Output 3
5
Note that exactly K balls must be removed.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 個の文字列 S _ 1,S _ 2,\ldots,S _ N が与えられます。 S _ i\ (1\leq i\leq N) は英小文字からなる長さ 10 以下の空でない文字列で、互いに異なります。
先手太郎君と後手次郎君がしりとりをします。 このしりとりでは、先手太郎君と後手次郎君の手番が交互に訪れます。 はじめの手番は先手太郎君の手番です。 それぞれのプレイヤーは自分の手番において整数 i\ (1\leq i\leq N) を 1 つ選びます。 このとき、i は次の 2 つの条件を満たしていなければなりません。
- i は、しりとりが開始してからこれまでの 2 人の手番で選ばれたどの整数とも異なる
- この手番がしりとりの最初の手番であるか、直前に選ばれた整数を j として、S _ j の最後の文字と S _ i の最初の文字が等しい
条件を満たす i を選べなくなったプレイヤーの負けで、負けなかったプレイヤーの勝ちです。
2 人が最適に行動したときに勝つのはどちらかを判定してください。
制約
- 1 \leq N \leq 16
- N は整数
- S _ i\ (1\leq i\leq N) は英小文字からなる長さ 10 以下の空でない文字列
- S _ i\neq S _ j\ (1\leq i\lt j\leq N)
入力
入力は以下の形式で標準入力から与えられる。
N S_1 S_2 \vdots S_N
出力
2 人が最適に行動したとき、先手太郎君が勝つなら First、後手次郎君が勝つなら Second と出力せよ。
入力例 1
6 enum float if modint takahashi template
出力例 1
First
例えば、ゲームは以下のように進行します。 この進行例では 2 人の行動が必ずしも最適とは限らないことに注意してください。
- 先手太郎君が i=3 を選ぶ。S _ i=
ifである。 - 後手次郎君が i=2 を選ぶ。S _ i=
floatであり、ifの最後の文字とfloatの最初の文字は等しい。 - 先手太郎君が i=5 を選ぶ。S _ i=
takahashiであり、floatの最後の文字とtakahashiの最初の文字は等しい。 - 後手次郎君は i\neq2,3,5 であって S _ i の最初の文字が
iと等しいものを選べないため、負ける。
このとき、先手太郎君が勝ちます。
入力例 2
10 catch chokudai class continue copy exec havoc intrinsic static yucatec
出力例 2
Second
入力例 3
16 mnofcmzsdx lgeowlxuqm ouimgdjxlo jhwttcycwl jbcuioqbsj mdjfikdwix jhvdpuxfil peekycgxco sbvxszools xuuqebcrzp jsciwvdqzl obblxzjhco ptobhnpfpo muizaqtpgx jtgjnbtzcl sivwidaszs
出力例 3
First
Score : 500 points
Problem Statement
You are given N strings S _ 1,S _ 2,\ldots,S _ N. S _ i\ (1\leq i\leq N) is a non-empty string of length at most 10 consisting of lowercase English letters, and the strings are pairwise distinct.
Taro the First and Jiro the Second play a word-chain game. In this game, the two players take alternating turns, with Taro the First going first. In each player's turn, the player chooses an integer i\ (1\leq i\leq N), which should satisfy the following two conditions:
- i is different from any integer chosen by the two players so far since the game started;
- the current turn is the first turn of the game, or the last character of S_j equals the first character of S_i, where j is the last integer chosen.
The player who is unable to choose a conforming i loses; the other player wins.
Determine which player will win if the two players play optimally.
Constraints
- 1 \leq N \leq 16
- N is an integer.
- S _ i\ (1\leq i\leq N) is a non-empty string of length at most 10 consisting of lowercase English letters.
- S _ i\neq S _ j\ (1\leq i\lt j\leq N)
Input
The input is given from Standard Input in the following format:
N S_1 S_2 \vdots S_N
Output
Print First if Taro the First wins when the two players play optimally; print Second if Jiro the Second wins.
Sample Input 1
6 enum float if modint takahashi template
Sample Output 1
First
For example, the game progresses as follows. Note that the two players may not be playing optimally in this example.
- Taro the First chooses i=3. S _ i=
if. - Jiro the Second chooses i=2. S _ i=
float, and the last character ofifequals the first character offloat. - Taro the First chooses i=5. S _ i=
takahashi, and the last character offloatequals the first character oftakahashi. - Jiro the Second is unable to choose i\neq2,3,5 such that S _ i starts with
i, so he loses.
In this case, Taro the First wins.
Sample Input 2
10 catch chokudai class continue copy exec havoc intrinsic static yucatec
Sample Output 2
Second
Sample Input 3
16 mnofcmzsdx lgeowlxuqm ouimgdjxlo jhwttcycwl jbcuioqbsj mdjfikdwix jhvdpuxfil peekycgxco sbvxszools xuuqebcrzp jsciwvdqzl obblxzjhco ptobhnpfpo muizaqtpgx jtgjnbtzcl sivwidaszs
Sample Output 3
First