A - Swap Odd and Even

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

英小文字からなる長さが偶数の文字列 S が与えられます。S の長さを |S|Si 文字目を S_i で表します。

i = 1, 2, \ldots, \frac{|S|}{2} の順に以下の操作を行い、すべての操作を終えた後の S を出力してください。

  • S_{2i-1}S_{2i} を入れ替える

制約

  • S は英小文字からなる長さが偶数の文字列
  • S の長さは 100 以下

入力

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

S

出力

答えを出力せよ。


入力例 1

abcdef

出力例 1

badcfe

操作を行う前は S = abcdef です。
i = 1 について操作を行うと、S_1S_2 が入れ替わるので S = bacdef になります。
i = 2 について操作を行うと、S_3S_4 が入れ替わるので S = badcef になります。
i = 3 について操作を行うと、S_5S_6 が入れ替わるので S = badcfe になります。
したがって、badcfe を出力します。


入力例 2

aaaa

出力例 2

aaaa

入力例 3

atcoderbeginnercontest

出力例 3

taocedbrgeniencrnoetts

Score : 100 points

Problem Statement

You are given a string S of even length consisting of lowercase English letters. Let |S| be the length of S, and S_i be the i-th character of S.

Perform the following operation for each i = 1, 2, \ldots, \frac{|S|}{2} in this order, and print the final S.

  • Swap S_{2i-1} and S_{2i}.

Constraints

  • S is a string of even length consisting of lowercase English letters.
  • The length of S is at most 100.

Input

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

S

Output

Print the answer.


Sample Input 1

abcdef

Sample Output 1

badcfe

Initially, S = abcdef.
Performing the operation for i = 1 swaps S_1 and S_2, making S = bacdef.
Performing the operation for i = 2 swaps S_3 and S_4, making S = badcef.
Performing the operation for i = 3 swaps S_5 and S_6, making S = badcfe.
Thus, badcfe should be printed.


Sample Input 2

aaaa

Sample Output 2

aaaa

Sample Input 3

atcoderbeginnercontest

Sample Output 3

taocedbrgeniencrnoetts
B - Jogging

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

高橋君と青木君はジョギングをすることにしました。
高橋君は「A 秒間秒速 B メートルで歩き、C 秒間休む」ことを繰り返します。
青木君は「D 秒間秒速 E メートルで歩き、F 秒間休む」ことを繰り返します。
二人が同時にジョギングを始めてから X 秒後、高橋君と青木君のうちどちらが長い距離を進んでいますか?

制約

  • 1 \leq A, B, C, D, E, F, X \leq 100
  • 入力は全て整数

入力

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

A B C D E F X

出力

二人が同時にジョギングを始めてから X 秒後時点で、高橋君の方が青木君よりも長い距離を進んでいるならば Takahashi、青木君の方が高橋君よりも長い距離を進んでいるならば Aoki、二人が同じ距離を進んでいるならば Draw と出力せよ。


入力例 1

4 3 3 6 2 5 10

出力例 1

Takahashi

二人はジョギングを始めてから 10 秒間の間、以下のように行動します。

  • 高橋君は 4 秒間歩き、3 秒間休んだ後、再び 3 秒間歩く。合計 (4 + 3) \times 3 = 21 メートル歩く。
  • 青木君は 6 秒間歩き、4 秒間休む。合計 6 \times 2 = 12 メートル歩く。

高橋君の方が長い距離を進んでいるので、Takahashi と出力します。


入力例 2

3 1 4 1 5 9 2

出力例 2

Aoki

入力例 3

1 1 1 1 1 1 1

出力例 3

Draw

Score : 100 points

Problem Statement

Takahashi and Aoki decided to jog.
Takahashi repeats the following: "walk at B meters a second for A seconds and take a rest for C seconds."
Aoki repeats the following: "walk at E meters a second for D seconds and take a rest for F seconds."
When X seconds have passed since they simultaneously started to jog, which of Takahashi and Aoki goes ahead?

Constraints

  • 1 \leq A, B, C, D, E, F, X \leq 100
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D E F X

Output

When X seconds have passed since they simultaneously started to jog, if Takahashi goes ahead of Aoki, print Takahashi; if Aoki goes ahead of Takahashi, print Aoki; if they have advanced the same distance, print Draw.


Sample Input 1

4 3 3 6 2 5 10

Sample Output 1

Takahashi

During the first 10 seconds after they started to jog, they move as follows.

  • Takahashi walks for 4 seconds, takes a rest for 3 seconds, and walks again for 3 seconds. As a result, he advances a total of (4 + 3) \times 3 = 21 meters.
  • Aoki walks for 6 seconds and takes a rest for 4 seconds. As a result, he advances a total of 6 \times 2 = 12 meters.

Since Takahashi goes ahead, Takahashi should be printed.


Sample Input 2

3 1 4 1 5 9 2

Sample Output 2

Aoki

Sample Input 3

1 1 1 1 1 1 1

Sample Output 3

Draw
C - 11/11

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

AtCoder 国では、1 年が N か月からなる暦を使っています。 i(1\leq i\leq N) は、i1 日から iD _ i 日までの D _ i 日からなります。

AtCoder 国において、1 年のうち日付がゾロ目になる日が何日あるか求めてください。

ただし、ij(1\leq i\leq N,1\leq j\leq D _ i) の日付がゾロ目になるとは、1 種類の数字だけを用いて ij を十進法で表すことができることをいいます。

制約

  • 1\leq N\leq100
  • 1\leq D _ i\leq100\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N
D _ 1 D _ 2 \ldots D _ N

出力

答えを出力せよ。


入力例 1

12
31 29 31 30 31 30 31 31 30 31 30 31

出力例 1

13

AtCoder 国では、 11 日、111 日、22 日、222 日、33 日、44 日、55 日、66 日、77 日、88 日、99 日、111 日、1111 日の合計 13 日の日付がゾロ目になります。


入力例 2

10
10 1 2 3 4 5 6 7 8 100

出力例 2

1

AtCoder 国では、11 日のみが日付がゾロ目になります。


入力例 3

30
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32

出力例 3

15

Score : 200 points

Problem Statement

AtCoder Kingdom uses a calendar whose year has N months. Month i (1\leq i\leq N) has D _ i days, from day 1 of month i to day D _ i of month i.

How many days in a year of AtCoder have "repdigits" dates?

Here, day j of month i (1\leq i\leq N,1\leq j\leq D _ i) is said to have a repdigit date if and only if all digits in the decimal notations of i and j are the same.

Constraints

  • 1\leq N\leq100
  • 1\leq D _ i\leq100\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N
D _ 1 D _ 2 \ldots D _ N

Output

Print the answer.


Sample Input 1

12
31 29 31 30 31 30 31 31 30 31 30 31

Sample Output 1

13

In AtCoder Kingdom, the days that have repdigit dates are January 1, January 11, February 2, February 22, March 3, April 4, May 5, June 6, July 7, August 8, September 9, November 1, and November 11, for a total of 13 days.


Sample Input 2

10
10 1 2 3 4 5 6 7 8 100

Sample Output 2

1

In AtCoder Kingdom, only January 1 has a repdigit date.


Sample Input 3

30
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32

Sample Output 3

15
D - How many?

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

a+b+c \leq S かつ a \times b \times c \leq T を満たす非負整数の組 (a,b,c) はいくつありますか?

制約

  • 0 \leq S \leq 100
  • 0 \leq T \leq 10000
  • S, T は整数である。

入力

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

S T

出力

条件を満たす非負整数の組 (a,b,c) の個数を出力せよ。


入力例 1

1 0

出力例 1

4

条件を満たす非負整数の組 (a,b,c)(0,0,0), (0,0,1), (0,1,0), (1,0,0)4 つです。


入力例 2

2 5

出力例 2

10

入力例 3

10 10

出力例 3

213

入力例 4

30 100

出力例 4

2471

Score : 200 points

Problem Statement

How many triples of non-negative integers (a, b, c) satisfy a+b+c \leq S and a \times b \times c \leq T?

Constraints

  • 0 \leq S \leq 100
  • 0 \leq T \leq 10000
  • S and T are integers.

Input

Input is given from Standard Input in the following format:

S T

Output

Print the number of triples of non-negative integers (a,b,c) satisfying the conditions.


Sample Input 1

1 0

Sample Output 1

4

The triples (a,b,c) satisfying the conditions are (0,0,0), (0,0,1), (0,1,0), and (1,0,0) ― there are four of them.


Sample Input 2

2 5

Sample Output 2

10

Sample Input 3

10 10

Sample Output 3

213

Sample Input 4

30 100

Sample Output 4

2471
E - Index × A(Continuous ver.)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の整数列 A=(A_1,A_2,\dots,A_N) が与えられます。

長さ MA の連続部分列 B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。

注記

数列の連続部分列とは、数列の先頭から 0 個以上、末尾から 0 個以上の要素を削除して得られる列のことをいいます。

例えば (2, 3)(1, 2, 3)(1, 2, 3, 4) の連続部分列ですが、(1, 3)(3,2,1)(1, 2, 3, 4) の連続部分列ではありません。

制約

  • 1 \le M \le N \le 2 \times 10^5
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • 入力は全て整数。

入力

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

N M
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 2
5 4 -1 8

出力例 1

15

B=(A_3,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15 となります。16 以上の値を達成することはできないため、解は 15 です。

B=(A_1,A_4) などを選ぶことができないことに注意してください。


入力例 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

出力例 2

31

Score : 300 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.

Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a contiguous subarray B=(B_1,B_2,\dots,B_M) of A of length M.

Notes

A contiguous subarray of a number sequence is a sequence that is obtained by removing 0 or more initial terms and 0 or more final terms from the original number sequence.

For example, (2, 3) and (1, 2, 3) are contiguous subarrays of (1, 2, 3, 4), but (1, 3) and (3,2,1) are not contiguous subarrays of (1, 2, 3, 4).

Constraints

  • 1 \le M \le N \le 2 \times 10^5
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 2
5 4 -1 8

Sample Output 1

15

When B=(A_3,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15. Since it is impossible to achieve 16 or a larger value, the solution is 15.

Note that you are not allowed to choose, for instance, B=(A_1,A_4).


Sample Input 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

Sample Output 2

31
F - Reversible

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

ボールがいくつか刺さった棒が N 本あり、各ボールには英小文字が 1 個書かれています。

i = 1, 2, \ldots, N について、i 番目の棒に刺さった各ボールの英小文字は、文字列 S_i によって表されます。 具体的には、i 番目の棒には文字列 S_i の長さ |S_i| に等しい個数のボールが刺さっており、 刺さっているボールの英小文字を、棒のある端から順に並べたものは文字列 S_i と等しいです。

2 つの棒は、一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列と、もう一方の棒に刺さっているボールの英小文字をどちらかの端から並べた列が一致するとき、同じ棒とみなされます。 より形式的には、1 以上 N 以下の整数 i, j について、i 本目の棒と j 本目の棒は、S_iS_j と一致するか、S_iS_j を前後反転したものと一致するとき、かつそのときに限り、同じとみなされます。

N 本の棒の中に、何種類の異なる棒があるかを出力してください。

制約

  • N は整数
  • 2 \leq N \leq 2 \times 10^5
  • S_i は英小文字のみからなる文字列
  • |S_i| \geq 1
  • \sum_{i = 1}^N |S_i| \leq 2 \times 10^5

入力

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

N
S_1
S_2
\vdots
S_N

出力

答えを出力せよ。


入力例 1

6
a
abc
de
cba
de
abc

出力例 1

3
  • S_2 = abcS_4 = cba を前後反転したものと一致するため、2 番目の棒と 4 番目の棒は同じとみなされます。
  • S_2 = abcS_6 = abc と一致するため、2 番目の棒と 6 番目の棒は同じとみなされます。
  • S_3 = deS_5 = de と一致するため、3 番目の棒と 5 番目の棒は同じとみなされます。

よって、6 本の棒の中に、1 本目の棒、2 本目の棒( 4, 6 本目の棒と同じ)、3 本目の棒( 5 本目の棒と同じ)の 3 種類の異なる棒があります。

Score : 300 points

Problem Statement

There are N sticks with several balls stuck onto them. Each ball has a lowercase English letter written on it.

For each i = 1, 2, \ldots, N, the letters written on the balls stuck onto the i-th stick are represented by a string S_i. Specifically, the number of balls stuck onto the i-th stick is the length |S_i| of the string S_i, and S_i is the sequence of letters on the balls starting from one end of the stick.

Two sticks are considered the same when the sequence of letters on the balls starting from one end of one stick is equal to the sequence of letters starting from one end of the other stick. More formally, for integers i and j between 1 and N, inclusive, the i-th and j-th sticks are considered the same if and only if S_i equals S_j or its reversal.

Print the number of different sticks among the N sticks.

Constraints

  • N is an integer.
  • 2 \leq N \leq 2 \times 10^5
  • S_i is a string consisting of lowercase English letters.
  • |S_i| \geq 1
  • \sum_{i = 1}^N |S_i| \leq 2 \times 10^5

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer.


Sample Input 1

6
a
abc
de
cba
de
abc

Sample Output 1

3
  • S_2 = abc equals the reversal of S_4 = cba, so the second and fourth sticks are considered the same.
  • S_2 = abc equals S_6 = abc, so the second and sixth sticks are considered the same.
  • S_3 = de equals S_5 = de, so the third and fifth sticks are considered the same.

Therefore, there are three different sticks among the six: the first, second (same as the fourth and sixth), and third (same as the fifth).

G - "redocta".swap(i,i+1)

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

atcoder の並べ替えである文字列 S が与えられます。
この文字列 S に対して以下の操作を 0 回以上行います。

  • S 中の隣接する 2 文字を選び、入れ替える。

Satcoder にするために必要な最小の操作回数を求めてください。

制約

  • Satcoder の並べ替えである文字列

入力

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

S

出力

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


入力例 1

catredo

出力例 1

8

catredo \rightarrow [ac]tredo \rightarrow actre[od] \rightarrow actr[oe]d \rightarrow actro[de] \rightarrow act[or]de \rightarrow acto[dr]e \rightarrow a[tc]odre \rightarrow atcod[er]
という流れで操作を行うと、 8 回で Satcoder にすることができ、これが達成可能な最小の操作回数です。


入力例 2

atcoder

出力例 2

0

この場合、文字列 S は元から atcoder です。


入力例 3

redocta

出力例 3

21

Score : 400 points

Problem Statement

You are given a string S that is a permutation of atcoder.
On this string S, you will perform the following operation 0 or more times:

  • Choose two adjacent characters of S and swap them.

Find the minimum number of operations required to make S equal atcoder.

Constraints

  • S is a string that is a permutation of atcoder

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer as an integer.


Sample Input 1

catredo

Sample Output 1

8

You can make S equal atcoder in 8 operations as follows:
catredo \rightarrow [ac]tredo \rightarrow actre[od] \rightarrow actr[oe]d \rightarrow actro[de] \rightarrow act[or]de \rightarrow acto[dr]e \rightarrow a[tc]odre \rightarrow atcod[er]
This is the minimum number of operations achievable.


Sample Input 2

atcoder

Sample Output 2

0

In this case, the string S is already atcoder.


Sample Input 3

redocta

Sample Output 3

21
H - Many Operations

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 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
I - Transportation

Time Limit: 4 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

AtCoder 国には N 個の島があり、 最初、どの島にも空港・港はなく、どの島の間にも道路はありません。 王である高橋君はこれらの島の間に交通手段を用意することにしました。 具体的には、高橋君は次の操作のうち 1 つを選んで行うことを好きなだけ繰り返す事ができます。

  • 1\leq i\leq N をみたす i を選び、コスト X_i を払って、島 i に空港を建設する。
  • 1\leq i\leq N をみたす i を選び、コスト Y_i を払って、島 i に港を建設する。
  • 1\leq i\leq M をみたす i を選び、コスト Z_i を払って、島 A_i と島 B_i の間を双方向に結ぶ道路を建設する。

高橋君の目標は、任意の相異なる 2 つの島 U, V について、 島 U からはじめて次の行動のうち 1 つを選んで行うことを好きなだけ繰り返す事で、 島 V に到達することができるようにする事です。

  • S,T の両方に空港がある時、島 S から島 T まで移動する。
  • S,T の両方に港がある時、島 S から島 T まで移動する。
  • S,T を結ぶ道路が存在する時、島 S から島 T まで移動する。

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

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1\leq X_i\leq 10^9
  • 1\leq Y_i\leq 10^9
  • 1\leq A_i<B_i\leq N
  • 1\leq Z_i\leq 10^9
  • i\neq j ならば (A_i,B_i)\neq (A_j,B_j)
  • 入力は全て整数

入力

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

N M
X_1 X_2 \ldots X_N
Y_1 Y_2 \ldots Y_N
A_1 B_1 Z_1
A_2 B_2 Z_2
\vdots
A_M B_M Z_M

出力

高橋君が目標を達成するのに必要な最小コストを出力せよ。


入力例 1

4 2
1 20 4 7
20 2 20 3
1 3 5
1 4 6

出力例 1

16

高橋君は次のように交通手段を建設します。

  • コスト X_1=1 を払って、島 1 に空港を建設する。
  • コスト X_3=4 を払って、島 3 に空港を建設する。
  • コスト Y_2=2 を払って、島 2 に港を建設する。
  • コスト Y_4=3 を払って、島 4 に港を建設する。
  • コスト Z_2=6 を払って、島 1 と島 4 の間を結ぶ道路を建設する。

このとき、目標は達成されており、かかったコストは 16 となります。 コスト 15 以下で目標を達成する方法はないため、16 を出力します。


入力例 2

3 1
1 1 1
10 10 10
1 2 100

出力例 2

3

空港・港・道路のうち、一度も建設されないものがあっても構いません。


入力例 3

7 8
35 29 36 88 58 15 25
99 7 49 61 67 4 57
2 3 3
2 5 36
2 6 89
1 6 24
5 7 55
1 3 71
3 4 94
5 6 21

出力例 3

160

Score : 500 points

Problem Statement

The Republic of AtCoder has N islands. Initially, none of the islands has an airport or harbor, and there is no road between any two of them. Takahashi, the king, will provide some means of transportation between these islands. Specifically, he can do the following operations any number of times in any order.

  • Choose an integer i such that 1\leq i\leq N and pay a cost of X_i to build an airport on island i.
  • Choose an integer i such that 1\leq i\leq N and pay a cost of Y_i to build a harbor on island i.
  • Choose an integer i such that 1\leq i\leq M and pay a cost of Z_i to build a road that bidirectionally connects island A_i and island B_i.

Takahashi's objective is to make it possible, for every pair of different islands U and V, to reach island V from island U when one can perform the following actions any number of times in any order.

  • When islands S and T both have airports, go from island S to island T.
  • When islands S and T both have harbors, go from island S to island T.
  • When islands S and T are connected by a road, go from island S to island T.

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

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1\leq X_i\leq 10^9
  • 1\leq Y_i\leq 10^9
  • 1\leq A_i<B_i\leq N
  • 1\leq Z_i\leq 10^9
  • (A_i,B_i)\neq (A_j,B_j), if i\neq j.
  • All values in the input are integers.

Input

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

N M
X_1 X_2 \ldots X_N
Y_1 Y_2 \ldots Y_N
A_1 B_1 Z_1
A_2 B_2 Z_2
\vdots
A_M B_M Z_M

Output

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


Sample Input 1

4 2
1 20 4 7
20 2 20 3
1 3 5
1 4 6

Sample Output 1

16

Takahashi will build the following infrastructure.

  • Pay a cost of X_1=1 to build an airport on island 1.
  • Pay a cost of X_3=4 to build an airport on island 3.
  • Pay a cost of Y_2=2 to build a harbor on island 2.
  • Pay a cost of Y_4=3 to build a harbor on island 4.
  • Pay a cost of Z_2=6 to build a road connecting island 1 and island 4.

Then, the objective will be achieved at a cost of 16. There is no way to achieve the objective for a cost of 15 or less, so 16 should be printed.


Sample Input 2

3 1
1 1 1
10 10 10
1 2 100

Sample Output 2

3

It is not mandatory to build all three types of facilities at least once.


Sample Input 3

7 8
35 29 36 88 58 15 25
99 7 49 61 67 4 57
2 3 3
2 5 36
2 6 89
1 6 24
5 7 55
1 3 71
3 4 94
5 6 21

Sample Output 3

160