A - Intersection

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

配点 : 100

問題文

数直線があり、高橋君はこれを赤色と青色で次のように塗りました。

  • X=L_1 から X=R_1 までをすべて赤色で塗る。
  • X=L_2 から X=R_2 までをすべて青色で塗る。

数直線のうち、赤色と青色の両方で塗られている部分の長さを求めてください。

制約

  • 0\leq L_1<R_1\leq 100
  • 0\leq L_2<R_2\leq 100
  • 入力はすべて整数

入力

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

L_1 R_1 L_2 R_2

出力

両方の色で塗られている部分の長さを整数で出力せよ。


入力例 1

0 3 1 5

出力例 1

2

X=0 から X=3 までが赤く、 X=1 から X=5 までが青く塗られています。

よって、両方の色で塗られている部分は X=1 から X=3 までであり、その長さは 2 となります。


入力例 2

0 1 4 5

出力例 2

0

両方の色で塗られている部分はありません。


入力例 3

0 3 3 7

出力例 3

0

赤色と青色で塗られている部分が接している場合でも、 両方の色で塗られている部分の長さは 0 となります。

Score : 100 points

Problem Statement

We have a number line. Takahashi painted some parts of this line, as follows:

  • First, he painted the part from X=L_1 to X=R_1 red.
  • Next, he painted the part from X=L_2 to X=R_2 blue.

Find the length of the part of the line painted both red and blue.

Constraints

  • 0\leq L_1<R_1\leq 100
  • 0\leq L_2<R_2\leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

L_1 R_1 L_2 R_2

Output

Print the length of the part of the line painted both red and blue, as an integer.


Sample Input 1

0 3 1 5

Sample Output 1

2

The part from X=0 to X=3 is painted red, and the part from X=1 to X=5 is painted blue.

Thus, the part from X=1 to X=3 is painted both red and blue, and its length is 2.


Sample Input 2

0 1 4 5

Sample Output 2

0

No part is painted both red and blue.


Sample Input 3

0 3 3 7

Sample Output 3

0

If the part painted red and the part painted blue are adjacent to each other, the length of the part painted both red and blue is 0.

B - Tournament Result

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

配点 : 200

問題文

N 人の人が総当り戦の試合をしました。

NN 列からなる試合の結果の表 A が与えられます。Ai 行目 j 列目の要素を A_{i,j} と表します。
A_{i,j}i=j のとき - であり、それ以外のとき W, L, D のいずれかです。
A_{i,j}W, L, D であることは、人 i が人 j との試合に勝った、負けた、引き分けたことをそれぞれ表します。

与えられた表に矛盾があるかどうかを判定してください。

次のいずれかが成り立つとき、与えられた表には矛盾があるといいます。

  • ある組 (i,j) が存在して、人 i が人 j に勝ったが、人 j が人 i に負けていない
  • ある組 (i,j) が存在して、人 i が人 j に負けたが、人 j が人 i に勝っていない
  • ある組 (i,j) が存在して、人 i が人 j に引き分けたが、人 j が人 i に引き分けていない

制約

  • 2 \leq N \leq 1000
  • A_{i,i}- である
  • i\neq j のとき、A_{i,j}W, L, D のいずれかである

入力

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

N
A_{1,1}A_{1,2}\ldots A_{1,N}
A_{2,1}A_{2,2}\ldots A_{2,N}
\vdots
A_{N,1}A_{N,2}\ldots A_{N,N}

出力

与えられた表に矛盾がないとき correct、矛盾があるとき incorrect と出力せよ。


入力例 1

4
-WWW
L-DD
LD-W
LDW-

出力例 1

incorrect

3 が人 4 に勝ったにもかかわらず、人 4 も人 3 に勝ったことになっており、矛盾しています。


入力例 2

2
-D
D-

出力例 2

correct

矛盾はありません。

Score : 200 points

Problem Statement

N players played a round-robin tournament.

You are given an N-by-N table A containing the results of the matches. Let A_{i,j} denote the element at the i-th row and j-th column of A.
A_{i,j} is - if i=j, and W, L, or D otherwise.
A_{i,j} is W if Player i beat Player j, L if Player i lost to Player j, and D if Player i drew with Player j.

Determine whether the given table is contradictory.

The table is said to be contradictory when some of the following holds:

  • There is a pair (i,j) such that Player i beat Player j, but Player j did not lose to Player i;
  • There is a pair (i,j) such that Player i lost to Player j, but Player j did not beat Player i;
  • There is a pair (i,j) such that Player i drew with Player j, but Player j did not draw with Player i.

Constraints

  • 2 \leq N \leq 1000
  • A_{i,i} is -.
  • A_{i,j} is W, L, or D, for i\neq j.

Input

Input is given from Standard Input in the following format:

N
A_{1,1}A_{1,2}\ldots A_{1,N}
A_{2,1}A_{2,2}\ldots A_{2,N}
\vdots
A_{N,1}A_{N,2}\ldots A_{N,N}

Output

If the given table is not contradictory, print correct; if it is contradictory, print incorrect.


Sample Input 1

4
-WWW
L-DD
LD-W
LDW-

Sample Output 1

incorrect

Player 3 beat Player 4, while Player 4 also beat Player 3, which is contradictory.


Sample Input 2

2
-D
D-

Sample Output 2

correct

There is no contradiction.

C - NewFolder(1)

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

配点 : 300

問題文

2 つの文字列 A,B に対して、A の末尾に B を連結した文字列を A+B と表します。

N 個の文字列 S_1,\ldots,S_N が与えられます。i=1,\ldots,N の順に、次の指示に従って加工して出力してください。

  • S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が存在しないならば、S_i を出力する。
  • S_1,\ldots,S_{i-1} の中に S_i と同じ文字列が X(X>0) 存在するならば、X を文字列として扱って S_i+ ( +X+ ) を出力する。

制約

  • 1 \leq N \leq 2\times 10^5
  • S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列

入力

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

N
S_1
S_2
\vdots
S_N

出力

問題文中の指示にしたがって、N 行出力せよ。


入力例 1

5
newfile
newfile
newfolder
newfile
newfolder

出力例 1

newfile
newfile(1)
newfolder
newfile(2)
newfolder(1)

入力例 2

11
a
a
a
a
a
a
a
a
a
a
a

出力例 2

a
a(1)
a(2)
a(3)
a(4)
a(5)
a(6)
a(7)
a(8)
a(9)
a(10)

Score : 300 points

Problem Statement

For two strings A and B, let A+B denote the concatenation of A and B in this order.

You are given N strings S_1,\ldots,S_N. Modify and print them as follows, in the order i=1, \ldots, N:

  • if none of S_1,\ldots,S_{i-1} is equal to S_i, print S_i;
  • if X (X>0) of S_1,\ldots,S_{i-1} are equal to S_i, print S_i+ ( +X+ ), treating X as a string.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • S_i is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

Print N lines as specified in the Problem Statement.


Sample Input 1

5
newfile
newfile
newfolder
newfile
newfolder

Sample Output 1

newfile
newfile(1)
newfolder
newfile(2)
newfolder(1)

Sample Input 2

11
a
a
a
a
a
a
a
a
a
a
a

Sample Output 2

a
a(1)
a(2)
a(3)
a(4)
a(5)
a(6)
a(7)
a(8)
a(9)
a(10)
D - Flipping and Bonus

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

配点 : 400

問題文

高橋君が N 回コイントスを行います。 また、高橋君はカウンタを持っており、最初カウンタの数値は 0 です。

i 回目のコイントスで表裏のどちらが出たかによって、次のことが起こります。

  • 表が出たとき:高橋君はカウンタの数値を 1 増やし、X_i 円もらう。
  • 裏が出たとき:高橋君はカウンタの数値を 0 に戻す。お金をもらうことは出来ない。

また、M 種類の連続ボーナスがあり、i 種類目の連続ボーナスではカウンタの数値が C_i になるたびに Y_i 円もらうことができます。

高橋君は最大で何円もらうことができるかを求めてください。

制約

  • 1\leq M\leq N\leq 5000
  • 1\leq X_i\leq 10^9
  • 1\leq C_i\leq N
  • 1\leq Y_i\leq 10^9
  • C_1,C_2,\ldots,C_M はすべて異なる。
  • 入力はすべて整数

入力

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

N M
X_1 X_2 \ldots X_N
C_1 Y_1
C_2 Y_2
\vdots
C_M Y_M

出力

高橋君がもらうことのできる金額の最大値を整数で出力せよ。


入力例 1

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

出力例 1

48

順に 表, 表, 裏, 表, 表, 表 が出た時、もらえる金額は次のようになります。

  • 1 回目のコイントスで表が出る。カウンタの数値を 0 から 1 にして、2 円もらう。
  • 2 回目のコイントスで表が出る。カウンタの数値を 1 から 2 にして、7 円もらう。さらに、連続ボーナスとして 10 円もらう。
  • 3 回目のコイントスで裏が出る。カウンタの数値を 2 から 0 にする。
  • 4 回目のコイントスで表が出る。カウンタの数値を 0 から 1 にして、8 円もらう。
  • 5 回目のコイントスで表が出る。カウンタの数値を 1 から 2 にして、2 円もらう。さらに、連続ボーナスとして 10 円もらう。
  • 6 回目のコイントスで表が出る。カウンタの数値を 2 から 3 にして、8 円もらう。さらに、連続ボーナスとして 1 円もらう。

このとき高橋君は合計で 2+(7+10)+0+8+(2+10)+(8+1)=48 円もらうことができ、このときが最大です。
連続ボーナスはカウンタの数値が C_i になるたびに何度でももらえることに注意してください。
ちなみに、6 回のコイントスで全部表が出た時は 2+(7+10)+(1+1)+8+(2+5)+8=44 円しかもらうことが出来ず、この時は最大ではありません。


入力例 2

3 2
1000000000 1000000000 1000000000
1 1000000000
3 1000000000

出力例 2

5000000000

答えが 32 bit 整数型に収まらないこともあることに注意してください。

Score : 400 points

Problem Statement

Takahashi will toss a coin N times. He also has a counter, which initially shows 0.

Depending on the result of the i-th coin toss, the following happens:

  • If it heads: Takahashi increases the counter's value by 1 and receives X_i yen (Japanese currency).
  • If it tails: he resets the counter's value to 0, without receiving money.

Additionally, there are M kinds of streak bonuses. The i-th kind of streak bonus awards Y_i yen each time the counter shows C_i.

Find the maximum amount of money that Takahashi can receive.

Constraints

  • 1\leq M\leq N\leq 5000
  • 1\leq X_i\leq 10^9
  • 1\leq C_i\leq N
  • 1\leq Y_i\leq 10^9
  • C_1,C_2,\ldots,C_M are all different.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 \ldots X_N
C_1 Y_1
C_2 Y_2
\vdots
C_M Y_M

Output

Print the maximum amount of money that Takahashi can receive, as an integer.


Sample Input 1

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

Sample Output 1

48

If he gets head, head, tail, head, head, head, in this order, the following amounts of money are awarded.

  • In the 1-st coin toss, the coin heads. Change the counter's value from 0 to 1 and receive 2 yen.
  • In the 2-nd coin toss, the coin heads. Change the counter's value from 1 to 2 and receive 7 yen. Additionally, get 10 yen as a streak bonus.
  • In the 3-rd coin toss, the coin tails. Change the counter's value from 2 to 0.
  • In the 4-th coin toss, the coin heads. Change the counter's value from 0 to 1 and receive 8 yen.
  • In the 5-th coin toss, the coin heads. Change the counter's value from 1 to 2 and receive 2 yen. Additionally, get 10 yen as a streak bonus.
  • In the 6-th coin toss, the coin heads. Change the counter's value from 2 to 3 and receive 8 yen. Additionally, get 1 yen as a streak bonus.

In this case, Takahashi receives 2+(7+10)+0+8+(2+10)+(8+1)=48 yen in total, which is the maximum possible.
Note that streak bonuses are awarded any number of times each time the counter shows C_i.
As a side note, if he gets head in all 6 coin tosses, he only receives 2+(7+10)+(1+1)+8+(2+5)+8=44 yen, which is not the maximum.


Sample Input 2

3 2
1000000000 1000000000 1000000000
1 1000000000
3 1000000000

Sample Output 2

5000000000

Note that the answer may not fit into a 32-bit integer type.

E - Many Operations

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

配点 : 500

問題文

変数 X と、X の値を変更する N 種類の操作があります。操作 i は整数の組 (T_i,A_i) で表され、意味は次の通りです。

  • T_i=1 のとき、X の値を X\ {\rm and}\ A_i に置き換える。
  • T_i=2 のとき、X の値を X\ {\rm or}\ A_i に置き換える。
  • T_i=3 のとき、X の値を X\ {\rm xor}\ A_i に置き換える。

変数 X を値 C で初期化した状態から、以下の処理を順に実行してください。

  • 操作 1 を行い、操作後の X の値を出力する。
  • 続けて、操作 1,2 を順に行い、操作後の X の値を出力する。
  • 続けて、操作 1,2,3 を順に行い、操作後の X の値を出力する。
  • \vdots
  • 続けて、操作 1,2,\ldots,N を順に行い、操作後の X の値を出力する。
{\rm and}, {\rm or}, {\rm xor} とは 非負整数 A, B{\rm and}, {\rm or}, {\rm xor} は、以下のように定義されます。
  • A\ {\rm and}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち両方が 1 であれば 1、そうでなければ 0 である。
  • A\ {\rm or}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち少なくとも一方が 1 であれば 1、そうでなければ 0 である。
  • A\ {\rm xor}\ B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうちちょうど一方が 1 であれば 1、そうでなければ 0 である。
例えば、3\ {\rm and}\ 5 = 13\ {\rm or}\ 5 = 73\ {\rm xor}\ 5 = 6 となります。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1\leq T_i \leq 3
  • 0\leq A_i \lt 2^{30}
  • 0\leq C \lt 2^{30}
  • 入力に含まれる値は全て整数である

入力

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

N C
T_1 A_1
T_2 A_2
\vdots
T_N A_N

出力

問題文中の指示に従って N 行出力せよ。


入力例 1

3 10
3 3
2 5
1 12

出力例 1

9
15
12

最初、X の値は 10 です。

  • 操作 1 を行うと X の値は 9 になります。
  • 続けて操作 1 を行うと X の値は 10 になり、さらに操作 2 を行うと 15 になります。
  • 続けて操作 1 を行うと X の値は 12 になり、さらに操作 2 を行うと 13 に、さらに続けて操作 3 を行うと 12 になります。

入力例 2

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

出力例 2

0
2
1
0
5
3
3
11
2

Score : 500 points

Problem Statement

We have a variable X and N kinds of operations that change the value of X. Operation i is represented as a pair of integers (T_i,A_i), and is the following operation:

  • if T_i=1, it replaces the value of X with X\ {\rm and}\ A_i;
  • if T_i=2, it replaces the value of X with X\ {\rm or}\ A_i;
  • if T_i=3, it replaces the value of X with X\ {\rm xor}\ A_i.

Initialize X with the value of C and execute the following procedures in order:

  • Perform Operation 1, and then print the resulting value of X.
  • Next, perform Operation 1, 2 in this order, and then print the value of X.
  • Next, perform Operation 1, 2, 3 in this order, and then print the value of X.
  • \vdots
  • Next, perform Operation 1, 2, \ldots, N in this order, and then print the value of X.
What are {\rm and}, {\rm or}, {\rm xor}?

The {\rm and}, {\rm or}, {\rm xor} of non-negative integers A and B are defined as follows:

  • When A\ {\rm and}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if both of the digits in that place of A and B are 1, and 0 otherwise.
  • When A\ {\rm or}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if at least one of the digits in that place of A and B is 1, and 0 otherwise.
  • When A\ {\rm xor}\ B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if exactly one of the digits in that place of A and B is 1, and 0 otherwise.
For example, 3\ {\rm and}\ 5 = 1, 3\ {\rm or}\ 5 = 7, and 3\ {\rm xor}\ 5 = 6.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1\leq T_i \leq 3
  • 0\leq A_i \lt 2^{30}
  • 0\leq C \lt 2^{30}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N C
T_1 A_1
T_2 A_2
\vdots
T_N A_N

Output

Print N lines, as specified in the Problem Statement.


Sample Input 1

3 10
3 3
2 5
1 12

Sample Output 1

9
15
12

The initial value of X is 10.

  • Operation 1 changes X to 9.
  • Next, Operation 1 changes X to 10, and then Operation 2 changes it to 15.
  • Next, Operation 1 changes X to 12, and then Operation 2 changes it to 13, and then Operation 3 changes it to 12.

Sample Input 2

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

Sample Output 2

0
2
1
0
5
3
3
11
2
F - Sorting Color Balls

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

配点 : 500

問題文

N 個の球が左右一列に並んでいます。 左から i 番目の球の色は色 C_i であり、整数 X_i が書かれています。

高橋君の目標は球を左から右に見た時に書かれている数が非減少になるように球を並べ替えることです。 言い換えれば、高橋君の目標は、任意の 1\leq i\leq N-1 について、左から i+1 番目の球に書かれている数 が左から i 番目に書かれている数以上であるようにすることです。

高橋君は目標を達成するために、次の操作を好きなだけ( 0 回でも良い )繰り返すことができます。

1\leq i\leq N-1 をみたす整数 i を選ぶ。
左から i 番目の球と i+1 番目の球の色が異なっているならば、コストを 1 支払う。 (色が等しいならばコストを支払う必要は無い。)
左から i 番目の球と i+1 番目の球を入れ替える。

高橋君が目標を達成するために支払う必要のあるコストの最小値を求めてください。

制約

  • 2 \leq N \leq 3\times 10^5
  • 1\leq C_i\leq N
  • 1\leq X_i\leq N
  • 入力は全て整数

入力

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

N
C_1 C_2 \ldots C_N
X_1 X_2 \ldots X_N

出力

高橋君が支払う必要のあるコストの最小値を整数で出力せよ。


入力例 1

5
1 5 2 2 1
3 2 1 2 1

出力例 1

6

球の情報を (色, 整数) で表すとします。 最初の状態は (1,3), (5,2), (2,1), (2,2), (1,1) です。 例えば、高橋君は次のように操作を行うことができます。

  • 左から 1 番目の球 (色 1) と 2 番目の球 (色 5) を入れ替える。 球は左から (5,2), (1,3), (2,1), (2,2), (1,1) となる。
  • 左から 2 番目の球 (色 1) と 3 番目の球 (色 2) を入れ替える。 球は左から (5,2), (2,1), (1,3), (2,2), (1,1) となる。
  • 左から 3 番目の球 (色 1) と 4 番目の球 (色 2) を入れ替える。 球は左から (5,2), (2,1), (2,2), (1,3), (1,1) となる。
  • 左から 4 番目の球 (色 1) と 5 番目の球 (色 1) を入れ替える。 球は左から (5,2), (2,1), (2,2), (1,1), (1,3) となる。
  • 左から 3 番目の球 (色 2) と 4 番目の球 (色 1) を入れ替える。 球は左から (5,2), (2,1), (1,1), (2,2), (1,3) となる。
  • 左から 1 番目の球 (色 5) と 2 番目の球 (色 2) を入れ替える。 球は左から (2,1), (5,2), (1,1), (2,2), (1,3) となる。
  • 左から 2 番目の球 (色 5) と 3 番目の球 (色 1) を入れ替える。 球は左から (2,1), (1,1), (5,2), (2,2), (1,3) となる。

最後の操作の後で球に書かれている数は左から順に 1,1,2,2,3 となっているため、高橋君は目的を達成しています。

1,2,3,5,6,7 回目の操作にコストが 1 ずつかかるため、 このとき合計でコストは 6 かかり、このときが最小となります。
4 回目の操作では、入れ替えた球の色がともに色 1 であるためコストがかからないことに注意してください。


入力例 2

3
1 1 1
3 2 1

出力例 2

0

すべての球の色は同じであるため、球の入れ替えにコストがかかることはありません。


入力例 3

3
3 1 2
1 1 2

出力例 3

0

高橋君は一度も操作を行わずとも、目的を達成できています。

Score : 500 points

Problem Statement

There are N balls arranged from left to right. The color of the i-th ball from the left is Color C_i, and an integer X_i is written on it.

Takahashi wants to rearrange the balls so that the integers written on the balls are non-decreasing from left to right. In other words, his objective is to reach a situation where, for every 1\leq i\leq N-1, the number written on the (i+1)-th ball from the left is greater than or equal to the number written on the i-th ball from the left.

For this, Takahashi can repeat the following operation any number of times (possibly zero):

Choose an integer i such that 1\leq i\leq N-1.
If the colors of the i-th and (i+1)-th balls from the left are different, pay a cost of 1. (No cost is incurred if the colors are the same).
Swap the i-th and (i+1)-th balls from the left.

Find the minimum total cost Takahashi needs to pay to achieve his objective.

Constraints

  • 2 \leq N \leq 3\times 10^5
  • 1\leq C_i\leq N
  • 1\leq X_i\leq N
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 C_2 \ldots C_N
X_1 X_2 \ldots X_N

Output

Print the minimum total cost Takahashi needs to pay to achieve his objective, as an integer.


Sample Input 1

5
1 5 2 2 1
3 2 1 2 1

Sample Output 1

6

Let us represent a ball as (Color, Integer). The initial situation is (1,3), (5,2), (2,1), (2,2), (1,1). Here is a possible sequence of operations for Takahashi:

  • Swap the 1-st ball (Color 1) and 2-nd ball (Color 5). Now the balls are arranged in the order (5,2), (1,3), (2,1), (2,2), (1,1).
  • Swap the 2-nd ball (Color 1) and 3-rd ball (Color 2). Now the balls are arranged in the order (5,2), (2,1), (1,3), (2,2), (1,1).
  • Swap the 3-rd ball (Color 1) and 4-th ball (Color 2). Now the balls are in the order (5,2), (2,1), (2,2), (1,3), (1,1).
  • Swap the 4-th ball (Color 1) and 5-th ball (Color 1). Now the balls are in the order (5,2), (2,1), (2,2), (1,1), (1,3).
  • Swap the 3-rd ball (Color 2) and 4-th ball (Color 1). Now the balls are in the order(5,2), (2,1), (1,1), (2,2), (1,3).
  • Swap the 1-st ball (Color 5) and 2-nd ball (Color 2). Now the balls are in the order (2,1), (5,2), (1,1), (2,2), (1,3).
  • Swap the 2-nd ball (Color 5) and 3-rd ball (Color 1). Now the balls are in the order (2,1), (1,1), (5,2), (2,2), (1,3).

After the last operation, the numbers written on the balls are 1,1,2,2,3 from left to right, which achieves Takahashi's objective.

The 1-st, 2-nd, 3-rd, 5-th, 6-th, and 7-th operations incur a cost of 1 each, for a total of 6, which is the minimum. Note that the 4-th operation does not incur a cost since the balls are both in Color 1.


Sample Input 2

3
1 1 1
3 2 1

Sample Output 2

0

All balls are in the same color, so no cost is incurred in swapping balls.


Sample Input 3

3
3 1 2
1 1 2

Sample Output 3

0

Takahashi's objective is already achieved without any operation.

G - Replace

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

配点 : 600

問題文

英小文字のみからなる 2 つの文字列 S,T が与えられます。

高橋君は文字列 S から始めて、 次の K 種類の操作のうち 1 つを選んで行う事を 好きなだけ繰り返す事ができます。
i 番目の操作は次の形で与えられます。

コストを 1 支払う。 その後、文字列中に文字 C_i が含まれるとき、そのうちの 1 つを選び、文字列 A_i に置き換える。 含まれないならば何も行わない。

文字列を T と一致させるために必要な最小コストを求めてください。 ただし、T と一致させることが不可能な場合は -1 を出力してください。

制約

  • 1\leq |S|\leq |T|\leq 50
  • 1\leq K\leq 50
  • C_ia, b,\ldots, z のいずれか
  • 1\leq |A_i|\leq 50
  • S,T,A_i は英小文字のみからなる文字列
  • C_i を長さ 1 の文字列としてみた時、C_i\neq A_i
  • (C_i,A_i) はすべて異なる。

入力

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

S
T
K
C_1 A_1
C_2 A_2
\vdots
C_K A_K

出力

ST と一致させるために必要な最小コストを出力せよ。 ただし、T と一致させることが不可能な場合は -1 を出力せよ。


入力例 1

ab
cbca
3
a b
b ca
a efg

出力例 1

4

高橋君は S=ab から始めて、次のように 4 回操作を行う事で、 T=cbca を作ることができます。

  • ab1 文字目 a を選んで b に置き換える ( 1 番目の操作) 。文字列は bb となる。
  • bb2 文字目 b を選んで ca に置き換える ( 2 番目の操作) 。文字列は bca となる。
  • bca1 文字目 b を選んで ca に置き換える ( 2 番目の操作) 。文字列は caca となる。
  • caca2 文字目 a を選んで b に置き換える ( 1 番目の操作) 。文字列は cbca となる。

各操作においてコストが 1 かかるため、必要なコストは 4 となり、このときが最小です。


入力例 2

a
aaaaa
2
a aa
a aaa

出力例 2

2

a\toaaa\toaaaaa とした時、必要なコストは 2 となり、 このときが最小です。


入力例 3

a
z
1
a abc

出力例 3

-1

どのように操作を行っても、S=a から T=z を作る事は出来ません。

Score : 600 points

Problem Statement

You are given two strings S and T consisting of lowercase English letters.

Takahashi starts with the string S. He can perform K kinds of operations any number of times in any order.
The i-th operation is the following:

Pay a cost of 1. Then, if the current string contains the character C_i, choose one of its occurrences and replace it with the string A_i. Otherwise, do nothing.

Find the minimum total cost needed to make the string equal T. If it is impossible to do so, print -1.

Constraints

  • 1\leq |S|\leq |T|\leq 50
  • 1\leq K\leq 50
  • C_i is a, b,\ldots, or z.
  • 1\leq |A_i|\leq 50
  • S, T, and A_i are strings consisting of lowercase English letters.
  • C_i\neq A_i, regarding C_i as a string of length 1.
  • All pairs (C_i,A_i) are distinct.

Input

Input is given from Standard Input in the following format:

S
T
K
C_1 A_1
C_2 A_2
\vdots
C_K A_K

Output

Print the minimum total cost needed to make the string equal T. If it is impossible to do so, print -1.


Sample Input 1

ab
cbca
3
a b
b ca
a efg

Sample Output 1

4

Starting with S=ab, Takahashi can make T=cbca in four operations as follows:

  • Replace the 1-st character a in ab with b (Operation of the 1-st kind). The string is now bb.
  • Replace the 2-nd character b in bb with ca (Operation of the 2-nd kind). The string is now bca.
  • Replace the 1-st character b in bca with ca (Operation of the 2-nd kind). The string is now caca.
  • Replace the 2-nd character a in caca with b (Operation of the 1-st kind). The string is now cbca.

Each operation incurs a cost of 1, for a total of 4, which is the minimum possible.


Sample Input 2

a
aaaaa
2
a aa
a aaa

Sample Output 2

2

Two operations a \to aaa \to aaaaa incur a cost of 2, which is the minimum possible.


Sample Input 3

a
z
1
a abc

Sample Output 3

-1

No sequence of operations makes T=z from S=a.

Ex - Game on Graph

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

配点 : 600

問題文

N 頂点 M 辺の有向グラフがあります。辺 i は頂点 A_i から B_i への有向辺で、重みが C_i です。

最初、頂点 v に駒が置かれています。高橋君と青木君が交互に次のように駒を動かすゲームを行います。

  • 駒が置かれている頂点から出る辺が存在しないとき、ゲームを終了する。
  • 駒が置かれている頂点から出る辺が存在するとき、そのうちいずれかの辺を選び、選んだ辺に沿って駒を移動する。

ゲームは高橋君から始め、高橋君はゲームが終了するまでに通った辺の重みの和を小さくしようとし、青木君は大きくしようとします。
2 人が目指すものはより厳密には、次の通りです。
高橋君は、ゲームを有限回の操作で終了させることを最優先し、それが可能ならば、ゲームが終了するまでに通る辺の重みの和を小さくしようとします。
青木君は、ゲームを有限回の操作で終了させないことを最優先し、それが不可能ならば、ゲームが終了するまでに通る辺の重みの和を大きくしようとします。
(駒が同じ辺を複数回通った場合は、重みはその回数だけ加算されるものとします。)

2 人が最善を尽くしたときゲームが有限回の操作で終了するか判定し、終了するならば、ゲームが終了するまでに通る辺の重みの和を求めてください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 0 \leq M \leq 2\times 10^5
  • 1 \leq v \leq N
  • 1 \leq A_i,B_i \leq N
  • 多重辺は存在しない。すなわち i\neq j のとき (A_i,B_i)\neq(A_j,B_j)
  • 自己辺は存在しない。すなわち A_i\neq B_i
  • 0 \leq C_i \leq 10^9
  • 入力に含まれる値は全て整数

入力

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

N M v
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

出力

2 人が最善を尽くしたとき、ゲームが有限回の操作で終了しないならば INFINITY と出力せよ。
有限回の操作で終了するならば、ゲームが終了するまでに通る辺の重みの和を出力せよ。


入力例 1

7 6 1
1 2 1
1 3 10
2 4 100
2 5 102
3 6 20
3 7 30

出力例 1

40

まず高橋君は頂点 3 に駒を動かし、次に青木君が頂点 7 に駒を動かし、ゲームが終了します。
ゲームが終了するまでに通る辺の重みの和は 10+30=40 になります。


入力例 2

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

出力例 2

INFINITY

有限回の操作でゲームは終了しません。


入力例 3

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

出力例 3

5

1\to 2 \to 3 \to 1 \to 2\to 4 と駒は動かされます。

Score : 600 points

Problem Statement

We have a directed graph with N vertices and M edges. Edge i is directed from Vertex A_i to B_i and has a weight of C_i.

Initially, there is a piece on Vertex v. Takahashi and Aoki will play a game where they alternate turns moving the piece as follows:

  • If there is no edge that goes from the vertex on which the piece is placed, end the game.
  • If there are edges that go from the vertex on which the piece is placed, choose one of those edges and move the piece along that edge.

Takahashi goes first. Takahashi tries to minimize the total weight of the edges traversed by the piece, and Aoki tries to maximize it.
More formally, their objectives are as follows.
Takahashi gives the first priority to ending the game in a finite number of moves. If this is possible, he tries to minimize the total weight of the edges traversed by the piece.
Aoki gives the first priority to preventing the game from ending in a finite number of moves. If this is impossible, he tries to maximize the total weight of the edges traversed by the piece.
(If the piece traverses the same edge multiple times, the weight is added that number of times.)

Determine whether the game ends in a finite number of moves when both players play optimally. If it ends, find the total weight of the edges traversed by the piece.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 0 \leq M \leq 2\times 10^5
  • 1 \leq v \leq N
  • 1 \leq A_i,B_i \leq N
  • There is no multi-edges. That is, (A_i,B_i)\neq(A_j,B_j) for i\neq j.
  • There is no self-loops. That is, A_i\neq B_i.
  • 0 \leq C_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M v
A_1 B_1 C_1
A_2 B_2 C_2
\vdots
A_M B_M C_M

Output

If the game does not end in a finite number of moves when both players play optimally, print INFINITY.
If the game ends in a finite number of moves, print the total weight of the edges traversed by the piece.


Sample Input 1

7 6 1
1 2 1
1 3 10
2 4 100
2 5 102
3 6 20
3 7 30

Sample Output 1

40

First, Takahashi will move the piece to Vertex 3. Next, Aoki will move the piece to Vertex 7, and the game will end.
The total weight of the edges traversed by the piece will be 10+30=40.


Sample Input 2

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

Sample Output 2

INFINITY

The game will not end in a finite number of moves.


Sample Input 3

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

Sample Output 3

5

The piece will go 1\to 2 \to 3 \to 1 \to 2\to 4.