A - New Scheme

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

配点 : 100

問題文

8 個の整数 S_1,S_2,\dots,S_8 が与えられます。 以下の 3 つの条件が全て満たされるならば Yes を、そうでないならば No を出力してください。

  • 数列 (S_1,S_2,\dots,S_8) は広義単調増加である。すなわち、S_1 \leq S_2 \leq \dots \leq S_8 である。
  • S_1,S_2,\dots,S_8 は全て 100 以上 675 以下である。
  • S_1,S_2,\dots,S_8 は全て 25 の倍数である。

制約

  • 0\leq S_i \leq 1000
  • 入力は全て整数

入力

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

S_1 S_2 \dots S_8

出力

答えを出力せよ。


入力例 1

125 175 250 300 400 525 600 650

出力例 1

Yes

3 つの条件を全て満たしています。


入力例 2

100 250 300 400 325 575 625 675

出力例 2

No

S_4 > S_5 であるため、1 つ目の条件を満たしていません。


入力例 3

0 23 24 145 301 413 631 632

出力例 3

No

2 つ目の条件と 3 つ目の条件を満たしていません。

Score : 100 points

Problem Statement

Given eight integers S_1,S_2,\dots, and S_8, print Yes if they satisfy all of the following three conditions, and No otherwise.

  • The sequence (S_1,S_2,\dots,S_8) is monotonically non-decreasing. In other words, S_1 \leq S_2 \leq \dots \leq S_8.
  • S_1,S_2,\dots, and S_8 are all between 100 and 675, inclusive.
  • S_1,S_2,\dots, and S_8 are all multiples of 25.

Constraints

  • 0\leq S_i \leq 1000
  • All input values are integers.

Input

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

S_1 S_2 \dots S_8

Output

Print the answer.


Sample Input 1

125 175 250 300 400 525 600 650

Sample Output 1

Yes

They satisfy all of the three conditions.


Sample Input 2

100 250 300 400 325 575 625 675

Sample Output 2

No

They violate the first condition because S_4 > S_5.


Sample Input 3

0 23 24 145 301 413 631 632

Sample Output 3

No

They violate the second and third conditions.

B - Default Price

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

配点 : 200

問題文

高橋くんは回転寿司で N 皿の料理を食べました。 i 皿目の色は文字列 C_i で表されます。

また、料理の価格は皿の色と対応し、 i=1,\ldots,M のそれぞれについて、色が文字列 D_i の皿の料理は一皿 P_i 円です。また、D_1,\ldots,D_M のいずれとも異なる色の皿の料理は一皿 P_0 円です。

高橋くんが食べた料理の価格の合計を求めてください。

制約

  • 1\leq N,M\leq 100
  • C_i,D_i は英小文字からなる長さ 1 以上 20 以下の文字列
  • D_1,\ldots,D_M はすべて異なる
  • 1\leq P_i\leq 10000
  • N,M,P_i は整数

入力

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

N M
C_1 \ldots C_N
D_1 \ldots D_M
P_0 P_1 \ldots P_M

出力

答えを整数として出力せよ。


入力例 1

3 2
red green blue
blue red
800 1600 2800

出力例 1

5200

blue の皿は P_1 = 1600 円、red の皿は P_2 = 2800 円、green の皿は P_0 = 800 円です。

高橋くんが食べた料理の価格の合計は、2800+800+1600=5200 円です。


入力例 2

3 2
code queen atcoder
king queen
10 1 1

出力例 2

21

Score : 200 points

Problem Statement

Takahashi ate N plates of sushi at a sushi restaurant. The color of the i-th plate is represented by a string C_i.

The price of a sushi corresponds to the color of the plate. For each i=1,\ldots,M, the sushi on a plate whose color is represented by a string D_i is worth P_i yen a plate (yen is the currency of Japan). If the color does not coincide with any of D_1,\ldots, and D_M, it is worth P_0 yen a plate.

Find the total amount of the prices of sushi that Takahashi ate.

Constraints

  • 1\leq N,M\leq 100
  • C_i and D_i are strings of length between 1 and 20, inclusive, consisting of lowercase English letters.
  • D_1,\ldots, and D_M are distinct.
  • 1\leq P_i\leq 10000
  • N, M, and P_i are integers.

Input

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

N M
C_1 \ldots C_N
D_1 \ldots D_M
P_0 P_1 \ldots P_M

Output

Print the answer as an integer.


Sample Input 1

3 2
red green blue
blue red
800 1600 2800

Sample Output 1

5200

A blue plate, red plate, and green plate are worth P_1 = 1600, P_2 = 2800, and P_0 = 800 yen, respectively.

The total amount of the prices of the sushi that he ate is 2800+800+1600=5200 yen.


Sample Input 2

3 2
code queen atcoder
king queen
10 1 1

Sample Output 2

21
C - Standings

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

配点 : 300

問題文

1 から N の番号が付いた N 人がコイントスを何回かしました。人 iA_i 回表を出し、B_i 回裏を出したこと分かっています。

i のコイントスの 成功率\displaystyle\frac{A_i}{A_i+B_i} で定義されます。人 1,\ldots,N の番号を、成功率の高い順に並び替えてください。成功率が同じ人が複数いる場合、その中では人の番号が小さい順になるように並び替えてください。

制約

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i, B_i\leq 10^9
  • A_i+B_i \geq 1
  • 入力される数値は全て整数

入力

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

N
A_1 B_1
\vdots
A_N B_N

出力

1,\ldots,N の番号を成功率の高い順に空白区切りで出力せよ。成功率が同じ人の番号は昇順に並び替えて出力せよ。


入力例 1

3
1 3
3 1
2 2

出力例 1

2 3 1

1 の成功率は 0.25、人 2 の成功率は 0.75、人 3 の成功率は 0.5 です。

成功率の高い順に並び替えると出力例の順番になります。


入力例 2

2
1 3
2 6

出力例 2

1 2

1,2 は成功率が同じなので、番号の昇順に出力することに注意してください。


入力例 3

4
999999999 1000000000
333333333 999999999
1000000000 999999997
999999998 1000000000

出力例 3

3 1 4 2

Score : 300 points

Problem Statement

N people numbered 1 through N tossed a coin several times. We know that person i's tosses resulted in A_i heads and B_i tails.

Person i's success rate of the tosses is defined by \displaystyle\frac{A_i}{A_i+B_i}. Sort people 1,\ldots,N in descending order of their success rates, with ties broken in ascending order of their assigned numbers.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i, B_i\leq 10^9
  • A_i+B_i \geq 1
  • All input values are integers.

Input

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

N
A_1 B_1
\vdots
A_N B_N

Output

Print the numbers of people 1,\ldots,N in descending order of their success rates, with ties broken in ascending order of their assigned numbers.


Sample Input 1

3
1 3
3 1
2 2

Sample Output 1

2 3 1

Person 1's success rate is 0.25, person 2's is 0.75, and person 3's is 0.5.

Sort them in descending order of their success rates to obtain the order in Sample Output.


Sample Input 2

2
1 3
2 6

Sample Output 2

1 2

Note that person 1 and 2 should be printed in ascending order of their numbers, as they have the same success rates.


Sample Input 3

4
999999999 1000000000
333333333 999999999
1000000000 999999997
999999998 1000000000

Sample Output 3

3 1 4 2
D - Snuke Maze

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

配点 : 400

問題文

HW 列のグリッドがあります。 以下、上から i 行目、左から j 列目のマスを (i,j) と表記します。 グリッドの各マスには英小文字が書かれており、(i,j) に書かれた文字は与えられる文字列 S_ij 文字目と一致します。

すぬけくんは、辺で隣接するマスに移動することを繰り返して (1,1) から (H,W) まで移動しようと思っています。 訪れるマス (最初の (1,1) と 最後の (H,W) を含む)に書かれた文字が、 訪れる順に s \rightarrow n \rightarrow u \rightarrow k \rightarrow e \rightarrow s \rightarrow n \rightarrow \dots となるような経路が存在するか判定してください。 なお、2 つのマス (i_1,j_1),(i_2,j_2)|i_1-i_2|+|j_1-j_2| = 1 を満たすとき、またそのときに限り「辺で隣接する」といいます。

より厳密には、マスの列 ((i_1,j_1),(i_2,j_2),\dots,(i_k,j_k)) であって以下の条件を全て満たすものが存在するか判定してください。

  • (i_1,j_1) = (1,1),(i_k,j_k) = (H,W)
  • すべての t\ (1 \leq t < k) について、(i_t,j_t)(i_{t+1},j_{t+1}) は辺で隣接する
  • すべての t\ (1 \leq t \leq k) について、(i_t,j_t) に書かれた文字は snuke((t-1) \bmod 5) + 1 文字目と一致する

制約

  • 2\leq H,W \leq 500
  • H,W は整数
  • S_i は英小文字からなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H

出力

問題文中の条件を満たす経路が存在するならば Yes を、存在しないならば No を出力せよ。


入力例 1

2 3
sns
euk

出力例 1

Yes

(1,1) \rightarrow (1,2) \rightarrow (2,2) \rightarrow (2,3) という経路は、訪れたマスに書かれた文字が訪れた順に s \rightarrow n \rightarrow u \rightarrow k となるため条件を満たします。


入力例 2

2 2
ab
cd

出力例 2

No

入力例 3

5 7
skunsek
nukesnu
ukeseku
nsnnesn
uekukku

出力例 3

Yes

Score : 400 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. We denote by (i,j) the cell at the i-th row from the top and j-th column from the left. Each cell in the grid has a lowercase English letter written on it. The letter written on (i,j) equals the j-th character of a given string S_i.

Snuke will repeat moving to an adjacent cell sharing a side to travel from (1,1) to (H,W). Determine if there is a path in which the letters written on the visited cells (including initial (1,1) and final (H,W)) are s \rightarrow n \rightarrow u \rightarrow k \rightarrow e \rightarrow s \rightarrow n \rightarrow \dots, in the order of visiting. Here, a cell (i_1,j_1) is said to be an adjacent cell of (i_2,j_2) sharing a side if and only if |i_1-i_2|+|j_1-j_2| = 1.

Formally, determine if there is a sequence of cells ((i_1,j_1),(i_2,j_2),\dots,(i_k,j_k)) such that:

  • (i_1,j_1) = (1,1),(i_k,j_k) = (H,W);
  • (i_{t+1},j_{t+1}) is an adjacent cell of (i_t,j_t) sharing a side, for all t\ (1 \leq t < k); and
  • the letter written on (i_t,j_t) coincides with the (((t-1) \bmod 5) + 1)-th character of snuke, for all t\ (1 \leq t \leq k).

Constraints

  • 2\leq H,W \leq 500
  • H and W are integers.
  • S_i is a string of length W consisting of lowercase English letters.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

Print Yes if there is a path satisfying the conditions in the problem statement; print No otherwise.


Sample Input 1

2 3
sns
euk

Sample Output 1

Yes

The path (1,1) \rightarrow (1,2) \rightarrow (2,2) \rightarrow (2,3) satisfies the conditions because they have s \rightarrow n \rightarrow u \rightarrow k written on them, in the order of visiting.


Sample Input 2

2 2
ab
cd

Sample Output 2

No

Sample Input 3

5 7
skunsek
nukesnu
ukeseku
nsnnesn
uekukku

Sample Output 3

Yes
E - MEX

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

配点 : 475

問題文

0,1,2 からなる長さ N の数列 A=(A_1,A_2,\dots,A_N) と、 M, E, X からなる長さ N の文字列 S=S_1S_2\dots S_N が与えられます。

1 \leq i < j < k \leq N かつ S_iS_jS_k= MEX を満たす全ての整数の組 (i,j,k) に対する \text{mex}(A_i,A_j,A_k) の総和を求めてください。 ここで、\text{mex}(A_i,A_j,A_k)A_i,A_j,A_k のいずれとも一致しない最小の非負整数を意味します。

制約

  • 3\leq N \leq 2\times 10^5
  • N は整数
  • A_i \in \lbrace 0,1,2\rbrace
  • SM, E, X からなる長さ N の文字列

入力

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

N
A_1 A_2 \dots A_N
S

出力

答えを整数として出力せよ。


入力例 1

4
1 1 0 2
MEEX

出力例 1

3

S_iS_jS_k = MEX となる i,j,k\ (1 \leq i < j < k \leq N) の組は (i,j,k)=(1,2,4),(1,3,4)2 つです。 \text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0,\text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3 より答えは 0+3=3 です。


入力例 2

3
0 0 0
XXX

出力例 2

0

入力例 3

15
1 1 2 0 0 2 0 2 0 0 0 0 0 2 2
EXMMXXXEMEXEXMM

出力例 3

13

Score : 475 points

Problem Statement

You are given a length-N sequence A=(A_1,A_2,\dots,A_N) consisting of 0, 1, and 2, and a length-N string S=S_1S_2\dots S_N consisting of M, E, and X.

Find the sum of \text{mex}(A_i,A_j,A_k) over all tuples of integers (i,j,k) such that 1 \leq i < j < k \leq N and S_iS_jS_k= MEX. Here, \text{mex}(A_i,A_j,A_k) denotes the minimum non-negative integer that equals neither A_i,A_j, nor A_k.

Constraints

  • 3\leq N \leq 2\times 10^5
  • N is an integer.
  • A_i \in \lbrace 0,1,2\rbrace
  • S is a string of length N consisting of M, E, and X.

Input

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

N
A_1 A_2 \dots A_N
S

Output

Print the answer as an integer.


Sample Input 1

4
1 1 0 2
MEEX

Sample Output 1

3

The tuples (i,j,k)\ (1 \leq i < j < k \leq N) such that S_iS_jS_k = MEX are the following two: (i,j,k)=(1,2,4),(1,3,4). Since \text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0 and \text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3, the answer is 0+3=3.


Sample Input 2

3
0 0 0
XXX

Sample Output 2

0

Sample Input 3

15
1 1 2 0 0 2 0 2 0 0 0 0 0 2 2
EXMMXXXEMEXEXMM

Sample Output 3

13
F - Vouchers

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

配点 : 500

問題文

あなたは店で N 個の商品を買おうとしています。 i 個目の商品の定価は P_i 円です。

また、あなたは M 枚のクーポンを持っています。i 枚目のクーポンを使うと、定価が L_i 円以上の商品を一つ選び、その商品を定価より D_i 円低い価格で買うことができます。

ここで、一つのクーポンは一回までしか使えません。また、複数のクーポンを同じ商品に重ねて使うことはできません。

クーポンを使わなかった商品は定価で買うことになります。 N 個すべての商品を買うのに必要な最小の金額を求めてください。

制約

  • 1\leq N,M\leq 2\times 10^5
  • 1\leq P_i\leq 10^9
  • 1\leq D_i \leq L_i \leq 10^9
  • 入力される数値は全て整数

入力

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

N M
P_1 \ldots P_N
L_1 \ldots L_M
D_1 \ldots D_M

出力

答えを整数として出力せよ。


入力例 1

3 3
4 3 1
4 4 2
2 3 1

出力例 1

4

2 枚目のクーポンを 1 個目の商品に、 3 枚目のクーポンを 2 個目の商品に使うことを考えます。

このとき、1 個目の商品を 4-3=1 円、2 個目の商品を 3-1=2 円、3 個目の商品を 1 円で買うことになるので、 1+2+1=4 円で全ての商品を買うことができます。


入力例 2

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

出力例 2

37

Score : 500 points

Problem Statement

You are in a store to buy N items. The regular price of the i-th item is P_i yen (the currency in Japan).

You have M coupons. You can use the i-th coupon to buy an item whose regular price is at least L_i yen at a D_i-yen discount.

Here, each coupon can be used only once. Besides, multiple coupons cannot be used for the same item.

If no coupon is used for an item, you will buy it for a regular price. Find the minimum possible total amount of money required to buy all the N items.

Constraints

  • 1\leq N,M\leq 2\times 10^5
  • 1\leq P_i\leq 10^9
  • 1\leq D_i \leq L_i \leq 10^9
  • All input values are integers.

Input

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

N M
P_1 \ldots P_N
L_1 \ldots L_M
D_1 \ldots D_M

Output

Print the answer as an integer.


Sample Input 1

3 3
4 3 1
4 4 2
2 3 1

Sample Output 1

4

Consider using the 2-nd coupon for the 1-st item, and the 3-rd coupon for the 2-nd item.

Then, you buy the 1-st item for 4-3=1 yen, 2-nd item for 3-1=2 yen, and 3-rd item for 1 yen. Thus, you can buy all the items for 1+2+1=4 yen.


Sample Input 2

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

Sample Output 2

37
G - Minimum Xor Pair Query

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

配点 : 600

問題文

整数を書くことができる黒板があります。はじめ黒板には整数は何も書かれていません。クエリが Q 個与えられるので順に処理してください。

クエリは以下の 3 種類です。

  • 1 x : 黒板に x を一つ書き込む。

  • 2 x : 黒板に書かれた x を一つ消去する。このクエリが与えられるとき、黒板に x が一つ以上書かれていることが保証される。

  • 3 : 黒板に書かれた二つの整数のビット単位 XOR としてあり得る最小値を出力する。このクエリを処理する際、黒板に整数が二つ以上書かれていることが保証される。

ビット単位 XOR とは 非負整数 A, B のビット単位 XOR 、A \oplus B は、以下のように定義されます。
  • A \oplus B を二進表記した際の 2^k (k \geq 0) の位の数は、A, B を二進表記した際の 2^k の位の数のうち一方のみが 1 であれば 1、そうでなければ 0 である。
例えば、3 \oplus 5 = 6 となります (二進表記すると: 011 \oplus 101 = 110)。

制約

  • 1\leq Q \leq 3\times 10^5
  • 0\leq x < 2 ^{30}
  • クエリ 2 が与えられるとき、黒板に x が一つ以上書かれている
  • クエリ 3 が与えられるとき、黒板に整数が二つ以上書かれている
  • 入力される数値は全て整数

入力

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

i 番目のクエリ \mathrm{query}_i では、まずクエリの種類 c_i1, 2, 3 のいずれか)が与えられる。 c_i = 1, c_i = 2 の場合はさらに整数 x が追加で与えられる。

すなわち、各クエリは以下に示す 3 つの形式のいずれかである。

1 x
2 x
3

出力

c_i=3 を満たすクエリの回数を q として q 行出力せよ。

j\ (1\leq j\leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。


入力例 1

9
1 2
1 10
3
1 3
3
2 2
3
1 10
3

出力例 1

8
1
9
0

1 番目のクエリ処理後、黒板には 2 が一つ書かれています。

2 番目のクエリ処理後、黒板には 2,10 が一つずつ書かれています。

3 番目のクエリ処理時、黒板に書かれた二つの数のビット単位 XOR として考えられる最小値は 2\oplus 10=8 です。

4 番目のクエリ処理後、黒板には 2,3,10 が一つずつ書かれています。

5 番目のクエリ処理時、黒板に書かれた二つの数のビット単位 XOR として考えられる最小値は 2\oplus 3=1 です。

6 番目のクエリ処理後、黒板には 3,10 が一つずつ書かれています。

7 番目のクエリ処理時、黒板に書かれた二つの数のビット単位 XOR として考えられる最小値は 3\oplus 10=9 です。

8 番目のクエリ処理後、黒板には 3 が一つ、 10 が二つ書かれています。

9 番目のクエリ処理時、黒板に書かれた二つの数のビット単位 XOR として考えられる最小値は 10\oplus 10=0 です。

Score : 600 points

Problem Statement

There is a blackboard on which you can write integers. Initially, no integer is written on the blackboard. Given Q queries, process them in order.

The query is of one of the following three kinds:

  • 1 x : write an x on the blackboard.

  • 2 x : erase an x from the blackboard. At the point this query is given, it is guaranteed that at least one x is written on the blackboard.

  • 3 : print the minimum possible bitwise XOR of two of the integers written on the blackboard. At the point this query is processed, it is guaranteed that at least two integers are written on the blackboard.

What is bitwise XOR? The bitwise XOR of non-negative integers A and B, A \oplus B, is defined as follows.
  • When A \oplus B is written in binary, the 2^ks place (k \geq 0) is 1 if exactly one of the 2^ks places of A and B is 1, and 0 otherwise.
For instance, 3 \oplus 5 = 6 (in binary: 011 \oplus 101 = 110).

Constraints

  • 1\leq Q \leq 3\times 10^5
  • 0\leq x < 2 ^{30}
  • When a query 2 is given, at least one x is written on the blackboard.
  • When a query 3 is given, at least two integers are written on the blackboard.
  • All input values are integers.

Input

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

Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

In the i-th query, \mathrm{query}_i, the kind of query, c_i (one of 1, 2, or 3), is first given. If c_i = 1 or c_i = 2, an integer x is additionally given.

In other words, each query is in one of the following three formats.

1 x
2 x
3

Output

Print q lines, where q is the number of queries such that c_i=3.

The j-th (1\leq j\leq q) line should contain the answer to the j-th such query.


Sample Input 1

9
1 2
1 10
3
1 3
3
2 2
3
1 10
3

Sample Output 1

8
1
9
0

After processing the 1-st query, a 2 is written on the blackboard.

After processing the 2-nd query, a 2 and a 10 are written on the blackboard.

When processing the 3-rd query, the minimum possible bitwise XOR of two of the integers written on the backboard is 2\oplus 10=8.

After processing the 4-th query, a 2, a 3, and a 10 are written on the blackboard.

When processing the 5-th query, the minimum possible bitwise XOR of two of the integers written on the backboard is 2\oplus 3=1.

After processing the 6-th query, a 3 and a 10 are written on the blackboard.

When processing the 7-th query, the minimum possible bitwise XOR of two of the integers written on the backboard is 3\oplus 10=9.

After processing the 8-th query, a 3 and two 10s are written on the blackboard.

When processing the 9-th query, the minimum possible bitwise XOR of two of the integers written on the backboard is 10\oplus 10=0.

Ex - Make Q

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

配点 : 625

問題文

N 頂点 M 辺の単純無向グラフがあり、最初全ての辺は白く塗られています。 頂点には 1 から N までの番号が、辺には 1 から M までの番号がそれぞれ付けられています。 辺 i は頂点 A_i と頂点 B_i を結んでおり、この辺を黒く塗るのにかかるコストは C_i です。

4 本以上の辺を黒く塗ることで以下の条件を全て満たすようにすることを「Q を作る」といいます。

  • 黒く塗られた辺のうちある 1 本以外は、1 つの単純サイクルをなす。
  • 黒く塗られた辺のうち上記のサイクルに含まれない 1 本は、そのサイクルに含まれる頂点と含まれない頂点を結ぶ。

Q を作ることが可能かどうか判定し、可能ならば Q を作るのに必要な最小の総コストを求めてください。

制約

  • 4\leq N \leq 300
  • 4\leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i < B_i \leq N
  • i \neq j ならば (A_i,B_i) \neq (A_j,B_j)
  • 1 \leq C_i \leq 10^5
  • 入力は全て整数

入力

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

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

出力

Q を作ることが可能ならば Q を作るのに必要な最小の総コストを、不可能ならば -1 を出力せよ。


入力例 1

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

出力例 1

15

2,3,4,5,6 を黒く塗ると、

  • 2,4,5,61 つの単純サイクルをなす
  • 3 が頂点 3(上記のサイクルに含まれる)と頂点 1(上記のサイクルに含まれない)を結ぶ

ため、総コスト 4+5+3+2+1=15 で Q を作ることができます。 他の方法で Q を作っても総コストは 15 以上かかるため、答えは 15 です。


入力例 2

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

出力例 2

-1

入力例 3

6 15
2 6 48772
2 4 36426
1 6 94325
3 6 3497
2 3 60522
4 5 63982
4 6 4784
1 2 14575
5 6 68417
1 5 7775
3 4 33447
3 5 90629
1 4 47202
1 3 90081
2 5 79445

出力例 3

78154

Score : 625 points

Problem Statement

There is a simple undirected graph with N vertices and M edges. The edges are initially painted white. The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i connects vertex A_i and vertex B_i, and the cost required to paint it black is C_i.

"Making a Q" means painting four or more edges so that:

  • all but one of the edges painted black form a simple cycle, and
  • the edge painted black not forming the cycle connects a vertex on the cycle and another not on the cycle.

Determine if one can make a Q. If one can, find the minimum total cost required to make a Q.

Constraints

  • 4\leq N \leq 300
  • 4\leq M \leq \frac{N(N-1)}{2}
  • 1 \leq A_i < B_i \leq N
  • (A_i,B_i) \neq (A_j,B_j), if i \neq j.
  • 1 \leq C_i \leq 10^5
  • All input values are integers.

Input

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

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

Output

If one can make a Q, print the minimum total cost required to do so; otherwise, print -1.


Sample Input 1

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

Sample Output 1

15

By painting edges 2,3,4,5, and 6,

  • edges 2,4,5, and 6 forms a simple cycle, and
  • edge 3 connects vertex 3 (on the cycle) and vertex 1 (not on the cycle),

so one can make a Q with a total cost of 4+5+3+2+1=15. Making a Q in another way costs 15 or greater, so the answer is 15.


Sample Input 2

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

Sample Output 2

-1

Sample Input 3

6 15
2 6 48772
2 4 36426
1 6 94325
3 6 3497
2 3 60522
4 5 63982
4 6 4784
1 2 14575
5 6 68417
1 5 7775
3 4 33447
3 5 90629
1 4 47202
1 3 90081
2 5 79445

Sample Output 3

78154