A - Christmas Present

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

野球少年の高橋君は今年とても良い子にしていたので、サンタさんはバットかグローブのうち値段が高い方を高橋君にプレゼントすることにしました。

バットの値段が B 円、グローブの値段が G 円 (B\neq G) のとき、サンタさんが高橋君にプレゼントするのはどちらですか?

制約

  • B,G1 以上 1000 以下の相異なる整数

入力

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

B G

出力

サンタさんが高橋君にプレゼントするのがバットであるとき Bat を、グローブであるとき Glove を出力せよ。


入力例 1

300 100

出力例 1

Bat

バットの方がグローブより値段が高いので、サンタさんは高橋君にバットをプレゼントします。


入力例 2

334 343

出力例 2

Glove

グローブの方がバットより値段が高いので、サンタさんは高橋君にグローブをプレゼントします。

Score : 100 points

Problem Statement

Takahashi, a young baseball enthusiast, has been a very good boy this year, so Santa has decided to give him a bat or a glove, whichever is more expensive.

If a bat costs B yen and a glove costs G yen (B\neq G), which one will Santa give to Takahashi?

Constraints

  • B and G are different integers between 1 and 1000, inclusive.

Input

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

B G

Output

If Santa gives Takahashi a bat, print Bat; if Santa gives him a glove, print Glove.


Sample Input 1

300 100

Sample Output 1

Bat

The bat is more expensive than the glove, so Santa will give Takahashi the bat.


Sample Input 2

334 343

Sample Output 2

Glove

The glove is more expensive than the bat, so Santa will give Takahashi the glove.

B - Adjacent Squares

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

H 行、横 W 列のマス目があり、このうち上から i 個目、左から j 個目のマスを (i,j) と呼びます。
このとき、マス (R,C) に辺で隣接するマスの個数を求めてください。

ただし、ある 2 つのマス (a,b),(c,d) が辺で隣接するとは、 |a-c|+|b-d|=1 (|x|x の絶対値とする) であることを言います。

制約

  • 入力は全て整数
  • 1 \le R \le H \le 10
  • 1 \le C \le W \le 10

入力

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

H W
R C

出力

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


入力例 1

3 4
2 2

出力例 1

4

入出力例 1,2,3 に対する説明は、出力例 3 の下にまとめて示します。


入力例 2

3 4
1 3

出力例 2

3

入力例 3

3 4
3 4

出力例 3

2

H=3,W=4 のとき、マス目は以下のようになります。

  • 入力例 1 について、マス (2,2) に隣接するマスは 4 つです。
  • 入力例 2 について、マス (1,3) に隣接するマスは 3 つです。
  • 入力例 3 について、マス (3,4) に隣接するマスは 2 つです。


入力例 4

1 10
1 5

出力例 4

2

入力例 5

8 1
8 1

出力例 5

1

入力例 6

1 1
1 1

出力例 6

0

Score : 100 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
Find the number of squares that share a side with Square (R, C).

Here, two squares (a,b) and (c,d) are said to share a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).

Constraints

  • All values in input are integers.
  • 1 \le R \le H \le 10
  • 1 \le C \le W \le 10

Input

Input is given from Standard Input in the following format:

H W
R C

Output

Print the answer as an integer.


Sample Input 1

3 4
2 2

Sample Output 1

4

We will describe Sample Inputs/Outputs 1,2, and 3 at once below Sample Output 3.


Sample Input 2

3 4
1 3

Sample Output 2

3

Sample Input 3

3 4
3 4

Sample Output 3

2

When H=3 and W=4, the grid looks as follows.

  • For Sample Input 1, there are 4 squares adjacent to Square (2,2).
  • For Sample Input 2, there are 3 squares adjacent to Square (1,3).
  • For Sample Input 3, there are 2 squares adjacent to Square (3,4).


Sample Input 4

1 10
1 5

Sample Output 4

2

Sample Input 5

8 1
8 1

Sample Output 5

1

Sample Input 6

1 1
1 1

Sample Output 6

0
C - Ancestor

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

N 人の人がいます。N 人の人には人 1,2,\dots,N と番号がついています。

i(2 \le i \le N) の親は人 P_i です。ここで、P_i < i が保証されます。

1 が人 N の何代前か求めてください。

制約

  • 2 \le N \le 50
  • 1 \le P_i < i(2 \le i \le N)
  • 入力は全て整数。

入力

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

N
P_2 P_3 \dots P_N

出力

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


入力例 1

3
1 2

出力例 1

2

2 は人 3 の親であるため、人 31 代前です。

1 は人 2 の親であるため、人 32 代前です。

よって解は 2 です。


入力例 2

10
1 2 3 4 5 6 7 8 9

出力例 2

9

Score : 200 points

Problem Statement

There are N people, called Person 1, Person 2, \ldots, Person N.

The parent of Person i (2 \le i \le N) is Person P_i. Here, it is guaranteed that P_i < i.

How many generations away from Person N is Person 1?

Constraints

  • 2 \le N \le 50
  • 1 \le P_i < i(2 \le i \le N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_2 P_3 \dots P_N

Output

Print the answer as a positive integer.


Sample Input 1

3
1 2

Sample Output 1

2

Person 2 is a parent of Person 3, and thus is one generation away from Person 3.

Person 1 is a parent of Person 2, and thus is two generations away from Person 3.

Therefore, the answer is 2.


Sample Input 2

10
1 2 3 4 5 6 7 8 9

Sample Output 2

9
D - Who is missing?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

整数 1, 2, \dots, N が書かれたカードが 4 枚ずつ、合計 4N 枚あります。

高橋君は、これらのカードをシャッフルしたのち 1 枚のカードを選んで抜き取り、残りの 4N - 1 枚を束にしてあなたに渡しました。渡された束の i \, (1 \leq i \leq 4N - 1) 枚目のカードには、整数 A_i が書かれています。

高橋君が抜き取ったカードに書かれていた整数を求めてください。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
  • k \, (1 \leq k \leq N) に対し、A_i = k となる i4 個以下である。
  • 入力は全て整数である。

入力

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

N
A_1 A_2 \ldots A_{4N - 1}

出力

答えを出力せよ。


入力例 1

3
1 3 2 3 3 2 2 1 1 1 2

出力例 1

3

高橋君が抜き取ったカードには 3 が書かれています。


入力例 2

1
1 1 1

出力例 2

1

入力例 3

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

出力例 3

2

Score : 200 points

Problem Statement

We have 4 cards with an integer 1 written on it, 4 cards with 2, \ldots, 4 cards with N, for a total of 4N cards.

Takahashi shuffled these cards, removed one of them, and gave you a pile of the remaining 4N-1 cards. The i-th card (1 \leq i \leq 4N - 1) of the pile has an integer A_i written on it.

Find the integer written on the card removed by Takahashi.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq A_i \leq N \, (1 \leq i \leq 4N - 1)
  • For each k \, (1 \leq k \leq N), there are at most 4 indices i such that A_i = k.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_{4N - 1}

Output

Print the answer.


Sample Input 1

3
1 3 2 3 3 2 2 1 1 1 2

Sample Output 1

3

Takahashi removed a card with 3 written on it.


Sample Input 2

1
1 1 1

Sample Output 2

1

Sample Input 3

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

Sample Output 3

2
E - Slot Strategy

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

N 個のリールからなるスロットがあります。
i 番目のリールの配列は文字列 S_i によって表されます。 ここで、S_i0, 1, \ldots, 9 がちょうど 1 回ずつ現れる長さ 10 の文字列です。

それぞれのリールには対応するボタンがついており、高橋君は各非負整数 t について、 スロットが回り始めてからちょうど t 秒後にボタンを 1 つ選んで押す(または何もしない)ことができます。
スロットが回り始めてから t 秒後に i 番目のリールに対応するボタンを押すと、 i 番目のリールは S_i(t\bmod{10})+1 文字目を表示して止まります。
ただし、t\bmod{10}t10 で割ったあまりを表します。

高橋君は全てのリールを止めた上で、表示されている文字が全て同じであるようにしたいです。
高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを求めてください。

制約

  • 2\leq N\leq 100
  • N は整数
  • S_i0, 1, \ldots, 9 がちょうど 1 回ずつ現れる長さ 10 の文字列

入力

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

N
S_1
S_2
\vdots
S_N

出力

高橋君が目標を達成できるように全てのリールを止めるまでに、スロットが回り始めてから最小で何秒かかるかを出力せよ。


入力例 1

3
1937458062
8124690357
2385760149

出力例 1

6

高橋君は次のようにそれぞれのリールを止めることでスロットが回り始めてから 6 秒後にリールに表示される文字を 8 で揃えることができます。

  • スロットの回転開始から 0 秒後に 2 番目のリールに対応するボタンを押します。2 番目のリールは S_2(0\bmod{10})+1=1 文字目である 8 を表示して止まります。
  • スロットの回転開始から 2 秒後に 3 番目のリールに対応するボタンを押します。3 番目のリールは S_3(2\bmod{10})+1=3 文字目である 8 を表示して止まります。
  • スロットの回転開始から 6 秒後に 1 番目のリールに対応するボタンを押します。1 番目のリールは S_1(6\bmod{10})+1=7 文字目である 8 を表示して止まります。

5 秒以下で全てのリールに表示されている文字を揃える方法はないため、6 を出力します。


入力例 2

5
0123456789
0123456789
0123456789
0123456789
0123456789

出力例 2

40

全てのリールを止めた上で、表示されている文字を揃える必要がある事に注意してください。

Score : 300 points

Problem Statement

There is a slot machine with N reels.
The placement of symbols on the i-th reel is represented by a string S_i of length 10 containing each of 0, 1, \ldots, 9 exactly once.

Each reel has a corresponding button. For each non-negative integer t, Takahashi can press one of the buttons of his choice (or do nothing) t seconds after the reels start spinning.
If the button for the i-th reel is pressed t seconds after the start of the spin, the i-th reel will stop to display the ((t\bmod{10})+1)-th character of S_i.
Here, t\bmod{10} denotes the remainder when t is divided by 10.

Takahashi wants to stop all reels to make them display the same character.
Find the minimum number of seconds needed to achieve his objective after the start of the spin.

Constraints

  • 2\leq N\leq 100
  • N is an integer.
  • S_i is a string of length 10 containing each of 0, 1, \ldots, 9 exactly once.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2
\vdots
S_N

Output

Print the minimum number of seconds needed to achieve Takahashi's objective after the start of the spin.


Sample Input 1

3
1937458062
8124690357
2385760149

Sample Output 1

6

Takahashi can make all reels display 8 in 6 seconds after the start of the spin by stopping them as follows.

  • 0 seconds after the start of the spin, press the button for the 2-nd reel, making it stop to display the ((0\bmod{10})+1=1)-st character of S_2, 8.
  • 2 seconds after the start of the spin, press the button for the 3-rd reel, making it stop to display the ((2\bmod{10})+1=3)-rd character of S_3, 8.
  • 6 seconds after the start of the spin, press the button for the 1-st reel, making it stop to display the ((6\bmod{10})+1=7)-th character of S_1, 8.

There is no way to make all reels display the same character in five seconds or less, so the answer is 6.


Sample Input 2

5
0123456789
0123456789
0123456789
0123456789
0123456789

Sample Output 2

40

Note that he must stop all reels to make them display the same character.

F - Belt Conveyor

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

H マス、横 W マスのグリッドがあります。上から i 行目、左から j 列目のマスを (i,j) と表します。
(i,j) には文字 G_{i,j} が書きこまれています。ここで G_{i,j}U, D, L, R のいずれかです。

あなたは (1,1) にいます。あなたは移動することができなくなるまで次の操作を繰り返します。

あなたは現在 (i,j) にいるとする。
G_{i,j}U であり、かつ i \neq 1 ならば (i-1,j) へ移動する。
G_{i,j}D であり、かつ i \neq H ならば (i+1,j) へ移動する。
G_{i,j}L であり、かつ j \neq 1 ならば (i,j-1) へ移動する。
G_{i,j}R であり、かつ j \neq W ならば (i,j+1) へ移動する。
それ以外の場合、あなたは移動することができない。

操作を終了した時点であなたがいるマスを出力してください。
ただし、あなたが操作を終了することなく無限に移動し続ける場合は -1 を出力してください。

制約

  • 1 \leq H, W \leq 500
  • G_{i,j}U, D, L, R のいずれかである。
  • H, W は整数である。

入力

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

H W
G_{1,1}G_{1,2}\dots G_{1,W}
G_{2,1}G_{2,2}\dots G_{2,W}
\vdots
G_{H,1}G_{H,2}\dots G_{H,W}

出力

操作を終了した時点であなたが (i,j) にいる場合は次の形式で出力せよ。

i j

また、無限に動き続ける場合は -1 を出力せよ。


入力例 1

2 3
RDU
LRU

出力例 1

1 3

あなたは (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3) の順に動いたあとに操作を終了します。よって答えは (1, 3) です。


入力例 2

2 3
RRD
ULL

出力例 2

-1

あなたは (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots というように無限に動き続けます。この場合は -1 を出力します。


入力例 3

9 44
RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR
RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD
DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR
DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD
RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR
RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR
RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR
RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR
RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR

出力例 3

9 5

Score : 300 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. (i, j) denotes the square at the i-th row from the top and j-th column from the left.
(i,j) has a character G_{i,j} written on it. G_{i,j} is U, D, L, or R.

You are initially at (1,1). You repeat the following operation until you cannot make a move.

Let (i,j) be the square you are currently at.
If G_{i,j} is U and i \neq 1, move to (i-1,j).
If G_{i,j} is D and i \neq H, move to (i+1,j).
If G_{i,j} is L and j \neq 1, move to (i,j-1).
If G_{i,j} is R and j \neq W, move to (i,j+1).
Otherwise, you cannot make a move.

Print the square you end up at when you cannot make a move.
If you indefinitely repeat moving, print -1 instead.

Constraints

  • 1 \leq H, W \leq 500
  • G_{i,j} is U, D, L, or R.
  • H and W are integers.

Input

Input is given from Standard Input in the following format:

H W
G_{1,1}G_{1,2}\dots G_{1,W}
G_{2,1}G_{2,2}\dots G_{2,W}
\vdots
G_{H,1}G_{H,2}\dots G_{H,W}

Output

If you end up at (i, j), print it in the following format:

i j

If you indefinitely repeat moving, print -1.


Sample Input 1

2 3
RDU
LRU

Sample Output 1

1 3

You will move as (1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (1, 3), ending up here, so the answer is (1, 3).


Sample Input 2

2 3
RRD
ULL

Sample Output 2

-1

You will indefinitely repeat moving as (1, 1) \to (1, 2) \to (1, 3) \to (2, 3) \to (2, 2) \to (2, 1) \to (1, 1) \to (1, 2) \to \dots, so -1 should be printed in this case.


Sample Input 3

9 44
RRDDDDRRRDDDRRRRRRDDDRDDDDRDDRDDDDDDRRDRRRRR
RRRDLRDRDLLLLRDRRLLLDDRDLLLRDDDLLLDRRLLLLLDD
DRDLRLDRDLRDRLDRLRDDLDDLRDRLDRLDDRLRRLRRRDRR
DDLRRDLDDLDDRLDDLDRDDRDDDDRLRRLRDDRRRLDRDRDD
RDLRRDLRDLLLLRRDLRDRRDRRRDLRDDLLLLDDDLLLLRDR
RDLLLLLRDLRDRLDDLDDRDRRDRLDRRRLDDDLDDDRDDLDR
RDLRRDLDDLRDRLRDLDDDLDDRLDRDRDLDRDLDDLRRDLRR
RDLDRRLDRLLLLDRDRLLLRDDLLLLLRDRLLLRRRRLLLDDR
RRRRDRDDRRRDDRDDDRRRDRDRDRDRRRRRRDDDRDDDDRRR

Sample Output 3

9 5
G - Flipping and Bonus

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 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.

H - Destruction

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

N 頂点 M 辺の連結無向グラフがあります。
頂点には 1 から N の番号が、辺には 1 から M の番号がついており、辺 i は頂点 A_iB_i を結んでいます。

高橋君は、このグラフから 0 個以上の辺を取り除こうとしています。

i を取り除くと、C_i \geq 0 のとき C_i の報酬を得、C_i<0 のとき |C_i| の罰金を払います。

辺を取り除いたあともグラフが連結でなければならないとき、高橋君が得られる報酬の最大値を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • -10^9 \leq C_i \leq 10^9
  • 与えられるグラフは連結である
  • 入力に含まれる値は全て整数である

入力

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

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

出力

答えを出力せよ。


入力例 1

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

出力例 1

4

4,5 を取り除くことで合計 4 の報酬を得られます。これより多くの報酬を得ることはできないため、答えは 4 となります。


入力例 2

3 3
1 2 1
2 3 0
3 1 -1

出力例 2

1

報酬が負であるような辺が存在することもあります。


入力例 3

2 3
1 2 -1
1 2 2
1 1 3

出力例 3

5

多重辺や自己ループが存在することもあります。

Score : 500 points

Problem Statement

We have a connected undirected graph with N vertices and M edges.
The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i connects Vertices A_i and B_i.

Takahashi is going to remove zero or more edges from this graph.

When removing Edge i, a reward of C_i is given if C_i \geq 0, and a fine of |C_i| is incurred if C_i<0.

Find the maximum total reward that Takahashi can get when the graph must be connected after removing edges.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • N-1 \leq M \leq 2\times 10^5
  • 1 \leq A_i,B_i \leq N
  • -10^9 \leq C_i \leq 10^9
  • The given graph is connected.
  • All values in input are integers.

Input

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

Print the answer.


Sample Input 1

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

Sample Output 1

4

Removing Edges 4 and 5 yields a total reward of 4. You cannot get any more, so the answer is 4.


Sample Input 2

3 3
1 2 1
2 3 0
3 1 -1

Sample Output 2

1

There may be edges that give a negative reward when removed.


Sample Input 3

2 3
1 2 -1
1 2 2
1 1 3

Sample Output 3

5

There may be multi-edges and self-loops.

I - Simple Operations on Sequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

長さ N2 つの整数列 A = (A_1, A_2, \ldots, A_N) および B = (B_1, B_2 \ldots, B_N) が与えられます。

整数列 A に対して、「下記の 2 つの操作のうちどちらかを行う」ということを好きな回数( 0 回でもよい)繰り返すことができます。

  • 1 \leq i \leq N を満たす整数 i を選び、A_i の値を 1 増やすか 1 減らす。この操作には X 円の費用がかかる。
  • 1 \leq i \leq N-1 を満たす整数 i を選び、A_i の値と A_{i+1} の値を入れ替える。この操作には Y 円の費用がかかる。

上記の繰り返しによって整数列 A を整数列 B に一致させるためにかかる合計費用としてあり得る最小値を出力してください。

制約

  • 2 \leq N \leq 18
  • 1 \leq X \leq 10^8
  • 1 \leq Y \leq 10^{16}
  • 1 \leq A_i, B_i \leq 10^8
  • 入力はすべて整数

入力

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

N X Y
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

AB に一致させるためにかかる合計費用としてあり得る最小値を求めよ。


入力例 1

4 3 5
4 2 5 2
6 4 2 1

出力例 1

16

はじめ、A = (4, 2, 5, 2) です。
下記の通りに操作を行うと、AB に一致させることができます。

  • X = 3 円払い、A_3 の値を 1 増やす。その結果 A = (4, 2, 6, 2) となる。
  • Y = 5 円払い、A_2 の値と A_3 の値を入れ替える。その結果 A = (4, 6, 2, 2) となる。
  • Y = 5 円払い、A_1 の値と A_2 の値を入れ替える。その結果 A = (6, 4, 2, 2) となる。
  • X = 3 円払い、A_4 の値を 1 減らす。その結果 A = (6, 4, 2, 1) = B となる。

上記の操作にかかる費用の合計は 3+5+5+3 = 16 円であり、これが最小となります。


入力例 2

5 12345 6789
1 2 3 4 5
1 2 3 4 5

出力例 2

0

AB は初めから一致しているため、一度も操作を行う必要がありません。


入力例 3

18 20719114 5117250357733867
10511029 36397527 63027379 44706927 47672230 79861204 57882493 42931589 51053644 52300688 43971370 26515475 62139996 41282303 34022578 12523039 6696497 64922712
14720753 4621362 25269832 91410838 86751784 32741849 6602693 60719353 28911226 88280613 18745325 80675202 34289776 37849132 99280042 73760634 43897718 40659077

出力例 3

13104119429316474

入力や出力が 32 bit 整数型に収まらないことがあることに注意してください。

Score : 500 points

Problem Statement

Given are two sequences of N integers each: A = (A_1, A_2, \ldots, A_N) and B = (B_1, B_2 \ldots, B_N).

On the sequence A, you can do the two operations below any number of times (possibly zero) in any order.

  • Choose an integer i satisfying 1 \leq i \leq N, and increase or decrease A_i by 1, for the cost of X yen (Japanese currency).
  • Choose an integer i satisfying 1 \leq i \leq N-1, and swap the values of A_i and A_{i+1}, for the cost of Y yen.

Print the minimum total cost needed to make the sequence A equal the sequence B by repeating the above.

Constraints

  • 2 \leq N \leq 18
  • 1 \leq X \leq 10^8
  • 1 \leq Y \leq 10^{16}
  • 1 \leq A_i, B_i \leq 10^8
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X Y
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print the minimum total cost needed to make A equal B.


Sample Input 1

4 3 5
4 2 5 2
6 4 2 1

Sample Output 1

16

Initialy, we have A = (4, 2, 5, 2).
The following sequence of operations makes A equal B.

  • Pay X = 3 yen to increase the value of A_3 by 1, making A = (4, 2, 6, 2).
  • Pay Y = 5 yen to swap the values of A_2 and A_3, making A = (4, 6, 2, 2).
  • Pay Y = 5 yen to swap the values of A_1 and A_2, making A = (6, 4, 2, 2).
  • Pay X = 3 yen to decrease the value of A_4 by 1, making A = (6, 4, 2, 1).

The total cost of these operations is 3+5+5+3 = 16 yen, which is the minimum possible.


Sample Input 2

5 12345 6789
1 2 3 4 5
1 2 3 4 5

Sample Output 2

0

A and B are equal from the beginning, so no operation is needed.


Sample Input 3

18 20719114 5117250357733867
10511029 36397527 63027379 44706927 47672230 79861204 57882493 42931589 51053644 52300688 43971370 26515475 62139996 41282303 34022578 12523039 6696497 64922712
14720753 4621362 25269832 91410838 86751784 32741849 6602693 60719353 28911226 88280613 18745325 80675202 34289776 37849132 99280042 73760634 43897718 40659077

Sample Output 3

13104119429316474

Note that values in input or output may not fit into 32-bit integer types.