A - Competition

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

スーパー高橋は、牛肉 1 パック X g を Y 円で売っています。
スーパーすぬけは、スーパー高橋より 1 g あたりの価格が安くなるように牛肉を売り出すことにしました。
スーパーすぬけでは、牛肉 1 パックは Z g です。スーパー高橋より 1 g あたりの価格が真に安くなるような、最大の販売価格 (非負整数) は何円ですか?

制約

  • 入力は全て整数
  • 1 ≤ X, Y, Z ≤ 10^3

入力

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

X Y Z

出力

答えを出力せよ。


入力例 1

100 200 100

出力例 1

199

どちらのスーパーでも 1 パック 100 g なので、スーパーすぬけはスーパー高橋より 1 円安く売れば良いです。


入力例 2

103 971 593

出力例 2

5590

スーパー高橋の 1 g あたりの価格は \frac{971}{103} = 9.4271\dots 円/g です。スーパーすぬけは、牛肉 593 g を 5590 円で売ると 1 g あたりの価格が \frac{5590}{593} = 9.4266\dots 円/g になります。


入力例 3

1000 1 1

出力例 3

0

販売価格が 0 円になっても構いません。

Score : 100 points

Problem Statement

A Supermarket Takahashi sells an X-gram beef pack for Y yen.
Another Supermarket Snuke has decided to sell a beef pack at a lower price per gram.
In Snuke, one beef pack weighs Z grams. What is the greatest possible price (a non-negative integer) for Snuke's beef pack such that it is strictly cheaper than Takahashi's beef pack per gram?

Constraints

  • All values in input are integers.
  • 1 ≤ X, Y, Z ≤ 10^3

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.


Sample Input 1

100 200 100

Sample Output 1

199

Both stores sell 100-gram packs, so Snuke can just make it one yen cheaper than that in Takahashi.


Sample Input 2

103 971 593

Sample Output 2

5590

Takahashi sells beef for \frac{971}{103} = 9.4271\dots yen per gram. Snuke can sell 593 grams of beef for 5590 yen to make it \frac{5590}{593} = 9.4266\dots yen per gram.


Sample Input 3

1000 1 1

Sample Output 3

0

The price is allowed to be 0 yen.

B - Xor of Sequences

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

狭義単調増加な整数列 A = (A_1, A_2, \dots, A_N), B = (B_1, B_2, \dots, B_M) があります。
A, B のどちらか片方にだけ出現する整数を全て求め、昇順に出力してください。

制約

  • 入力は全て整数
  • 1 \leq N, M \leq 10^3
  • 1 \leq A_1 < A_2 < \dots < A_N \leq 10^3
  • 1 \leq B_1 < B_2 < \dots < B_M \leq 10^3

入力

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

N M
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_M

出力

A, B のどちらか片方にだけ出現する整数を全て、昇順に空白区切りで出力せよ。


入力例 1

2 2
1 2
1 3

出力例 1

2 3

1AB の両方に含まれます。
2A のみに含まれます。
3B のみに含まれます。
したがって、2, 3 を出力します。


入力例 2

4 4
1 2 3 4
1 2 3 4

出力例 2


空行を出力しても、何も出力しなくても正解になります。

Score : 200 points

Problem Statement

We have two strictly increasing integer sequences A = (A_1, A_2, \dots, A_N) and B = (B_1, B_2, \dots, B_M).
Find all integers that appear in exactly one of A and B and print them in ascending order.

Constraints

  • All values in input are integers.
  • 1 \leq N, M \leq 10^3
  • 1 \leq A_1 < A_2 < \dots < A_N \leq 10^3
  • 1 \leq B_1 < B_2 < \dots < B_M \leq 10^3

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \cdots A_N
B_1 B_2 \cdots B_M

Output

Print all integers that appear in exactly one of A and B in ascending order, with space as separator.


Sample Input 1

2 2
1 2
1 3

Sample Output 1

2 3

1 is contained in both A and B;
2 is contained in only A;
3 is contained in only B.
Thus, we should print 2 and 3.


Sample Input 2

4 4
1 2 3 4
1 2 3 4

Sample Output 2


You can print an empty line or nothing.

C - Max GCD 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

整数 A, B が与えられます。整数 x, yA ≤ x < y ≤ B となるように選ぶときの、\gcd(x, y) の最大値を求めてください。
なお、\gcd(x, y)xy の最大公約数を表します。

制約

  • A, B は整数
  • 1 ≤ A < B ≤ 2 \times 10^5

入力

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

A B

出力

答えを出力せよ。


入力例 1

2 4

出力例 1

2

A ≤ x < y ≤ B を満たす (x,y) の選び方は (2,3), (2,4), (3,4)3 つです。それぞれ最大公約数は 1, 2, 1 であるので、最大値は 2 です。


入力例 2

199999 200000

出力例 2

1

\gcd(199999, 200000) = 1 です。


入力例 3

101 139

出力例 3

34

Score : 300 points

Problem Statement

Given are integers A and B. Find the maximum possible value of \gcd(x, y) when we choose integers x and y so that A ≤ x < y ≤ B.
Here, \gcd(x, y) denotes the greatest common divisor of x and y.

Constraints

  • A and B are integers.
  • 1 ≤ A < B ≤ 2 \times 10^5

Input

Input is given from Standard Input in the following format:

A B

Output

Print the answer.


Sample Input 1

2 4

Sample Output 1

2

We have three ways to choose (x, y) such that A ≤ x < y ≤ B: (2,3), (2,4), (3,4), where the greatest common divisors are 1, 2, 1, respectively, so the maximum possible value is 2.


Sample Input 2

199999 200000

Sample Output 2

1

We have \gcd(199999, 200000) = 1.


Sample Input 3

101 139

Sample Output 3

34
D - Nowhere P

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

2 以上の整数 P が与えられます。これはあなたの嫌いな数です。

整数の列 A_1, A_2, \dots, A_N が以下の条件を満たすとき、この列を とても良い 列と呼びます。

  • 1 以上 N 以下のどの整数 i についても、A_1 + A_2 + \dots + A_iP の倍数でない

各要素が 1 以上 P - 1 以下であるような長さ N の整数列は全部で (P - 1)^N 個存在しますが、このうち とても良い 列はいくつあるでしょうか?

ただし、答えは非常に大きくなることがあるので、答えを (10^9 + 7) で割った余りを出力してください。

制約

  • N, P は整数
  • 1 ≤ N ≤ 10^9
  • 2 ≤ P ≤ 10^9

入力

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

N P

出力

答えを (10^9 + 7) で割った余りを出力せよ。


入力例 1

3 3

出力例 1

2

(1, 1, 2), (2, 2, 1)2 つが条件を満たします。


入力例 2

3 2

出力例 2

0

入力例 3

45108 2571593

出力例 3

224219544

Score : 400 points

Problem Statement

You are given a prime number P not less than 2, which you don't like.

Let's call an array of integers A_1, A_2, \dots, A_N very good if it satisfies the following condition:

  • there is no i with 1 \le i \le N and A_1 + A_2 + \dots + A_i \equiv 0 \bmod P.

Consider all (P-1)^N arrays of length N with elements from 1 to P-1. How many of them are very good?

As this number can be very big, output it modulo (10^9 + 7).

Constraints

  • N and P are integers.
  • 1 ≤ N ≤ 10^9
  • 2 ≤ P ≤ 10^9

Input

Input is given from Standard Input in the following format:

N P

Output

Print the count modulo (10^9 + 7).


Sample Input 1

3 3

Sample Output 1

2

Two arrays, (1, 1, 2) and (2, 2, 1), satisfy the condition.


Sample Input 2

3 2

Sample Output 2

0

Sample Input 3

45108 2571593

Sample Output 3

224219544
E - Level K Palindrome

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

高橋くんは、すぬけくんに日頃の感謝を込めてレベル K の回文を送ることにしました。レベル L の回文 ( L0 以上の整数 ) は以下のように定義されます。

  • 文字列 s の左右を反転させたものを \mathrm{rev}(s) と表す。
  • 文字列 ss = \mathrm{rev}(s) であるとき、回文という。
  • 空文字列と回文でない文字列はレベル 0 の回文である。
  • 任意の空でないレベル L - 1 の回文 t に対して、t, \mathrm{rev}(t) をこの順に繋げた文字列はレベル L の回文である。
  • 任意のレベル L - 1 の回文 t と任意の文字 c に対して、t, c, \mathrm{rev}(t) をこの順に繋げた文字列はレベル L の回文である。

いま、高橋くんは文字列 S を持っています。
S から 1 文字選んで別の英小文字に書き換えるということを 0 回以上繰り返すことで、S をちょうどレベル K の回文にすることができるか判定してください。また、できる場合は、S がちょうどレベル K の回文になるまでに必要な最小の書き換え回数を求めてください。

制約

  • K は整数
  • 0 ≤ K ≤ 5 \times 10^5
  • S は英小文字からなる
  • 1 ≤ |S| ≤ 5 \times 10^5

入力

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

K
S

出力

ちょうどレベル K の回文を作ることができる場合は、必要な最小の書き換え回数を出力せよ。
できない場合は、impossible と出力せよ。


入力例 1

4
aabaaaabaa

出力例 1

0

aabaaaabaa のレベルは以下のように計算できます。

  • 空文字列はレベル 0 の回文である。
  • a は (空文字列), a, (空文字列) を順に繋げた文字列だから、レベル 1 の回文である。
  • aaa, a を順に繋げた文字列だから、レベル 2 の回文である。
  • aabaaaa, b, aa を順に繋げた文字列だから、レベル 3 の回文である。
  • aabaaaabaaaabaa, aabaa を順に繋げた文字列だから、レベル 4 の回文である。

よって、aabaaaabaa は初めからレベル 4 の回文であるので、書き換える必要はありません。


入力例 2

2
aabaaaabaa

出力例 2

4

例えば、aabaaaabaaacbcaacbca に書き換えると、ちょうどレベル 2 の回文を作ることができます。

aabaaaabaa はレベル 2 の回文ではないことに注意してください。


入力例 3

3
aabaaaabaa

出力例 3

impossible

入力例 4

5
aabaaaabaa

出力例 4

impossible

入力例 5

2
acaabcbababaaac

出力例 5

6

Score : 500 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give Snuke a level-K palindrome. A level-L palindrome, where L is a non-negative integer, is defined as follows:

  • Let \mathrm{rev}(s) denote the reversal of a string s.
  • A string s is said to be a palindrome when s = \mathrm{rev}(s).
  • The empty string and a string that is not a palindrome are level-0 palindromes.
  • For any non-empty level-(L-1) palindrome t, the concatenation of t, \mathrm{rev}(t) in this order is a level-L palindrome.
  • For any level-(L-1) palindrome t and any character c, the concatenation of t, c, \mathrm{rev}(t) in this order is a level-L palindrome.

Now, Takahashi has a string S. Determine whether it is possible to make S an exactly level-K palindrome by doing the following action zero or more times: choose a character in S and change it to another lowercase English letter. If it is possible, find the minimum number of changes needed to make S a level-K palindrome.

Constraints

  • K is an integer.
  • 0 ≤ K ≤ 5 \times 10^5
  • S consists of lowercase English letters.
  • 1 ≤ |S| ≤ 5 \times 10^5

Input

Input is given from Standard Input in the following format:

K
S

Output

If it is possible to get an exactly level-K palindrome, print the minimum number of changes needed. If it is impossible, print impossible.


Sample Input 1

4
aabaaaabaa

Sample Output 1

0

We can find the level of aabaaaabaa as follows:

  • the empty string is a level-0 palindrome;
  • a is a concatenation of (empty string), a, (empty string) in this order, so it is a level-1 palindrome;
  • aa is a concatenation of a, a in this order, so it is a level-2 palindrome;
  • aabaa is a concatenation of aa, b, aa in this order, so it is a level-3 palindrome;
  • aabaaaabaa is a concatenation of aabaa, aabaa in this order, so it is a level-4 palindrome.

Thus, aabaaaabaa is already a level-4 palindrome and needs no changes.


Sample Input 2

2
aabaaaabaa

Sample Output 2

4

We can, for example, change aabaaaabaa to acbcaacbca to get a level-2 palindrome.

Note that aabaaaabaa is not a level-2 palindrome.


Sample Input 3

3
aabaaaabaa

Sample Output 3

impossible

Sample Input 4

5
aabaaaabaa

Sample Output 4

impossible

Sample Input 5

2
acaabcbababaaac

Sample Output 5

6
F - Max Matrix

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の数列 a と、長さ M の数列 b があります。最初、a,b の要素は全て 0 です。
Q 個のクエリを処理してください。i 個目のクエリでは 3 つの整数 T_i, X_i, Y_i が与えられ、以下の処理をします。

  • T_i = 1 のとき : a_{X_i}Y_i に置き換える
  • T_i = 2 のとき : b_{X_i}Y_i に置き換える

そして、毎回のクエリの処理直後に \displaystyle \sum_{i = 1}^N \sum_{j = 1}^M \max(a_i, b_j) の値を出力してください。

制約

  • 1 \le N \le 2 \times 10^5
  • 1 \le M \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • T_i1 または 2
  • T_i = 1 ならば 1 \le X_i \le N
  • T_i = 2 ならば 1 \le X_i \le M
  • 1 \le Y_i \le 10^8
  • 入力に含まれる値は全て整数である

入力

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

N M Q
T_1 X_1 Y_1
T_2 X_2 Y_2
T_3 X_3 Y_3
\hspace{21pt} \vdots
T_Q X_Q Y_Q

出力

問題文の指示に従って Q 個の整数を改行区切りで出力せよ。


入力例 1

2 2 4
1 1 10
2 1 20
2 2 5
1 1 30

出力例 1

20
50
55
85

上から i 行目、左から j 列目に \max(a_i, b_j) を書き込んだマス目は、4 回のクエリで以下のように変化します。


入力例 2

3 3 5
1 3 10
2 1 7
1 3 5
2 2 10
2 1 1

出力例 2

30
44
31
56
42

入力例 3

200000 200000 4
2 112219 100000000
1 73821 100000000
2 26402 100000000
1 73821 100000000

出力例 3

20000000000000
39999900000000
59999800000000
59999800000000

出力する整数は 32 bit 整数型に収まらない可能性があります。

Score : 600 points

Problem Statement

We have a sequence a of length N and a sequence b of length M. Initially, every element in a and b is 0.
You are asked to process Q queries. In the i-th query, given three integers T_i, X_i, and Y_i, do the following:

  • if T_i = 1: replace a_{X_i} with Y_i;
  • if T_i = 2: replace b_{X_i} with Y_i.

Then, after processing each query, print the value \displaystyle \sum_{i = 1}^N \sum_{j = 1}^M \max(a_i, b_j).

Constraints

  • 1 \le N \le 2 \times 10^5
  • 1 \le M \le 2 \times 10^5
  • 1 \le Q \le 2 \times 10^5
  • T_i is 1 or 2.
  • If T_i = 1, 1 \le X_i \le N.
  • If T_i = 2, 1 \le X_i \le M.
  • 1 \le Y_i \le 10^8
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M Q
T_1 X_1 Y_1
T_2 X_2 Y_2
T_3 X_3 Y_3
\hspace{21pt} \vdots
T_Q X_Q Y_Q

Output

Print Q integers as instructed in Problem Statement, with newline as separator.


Sample Input 1

2 2 4
1 1 10
2 1 20
2 2 5
1 1 30

Sample Output 1

20
50
55
85

If we write \max(a_i, b_j) at the i-th row and j-th column in a grid, the four queries will change it as follows:


Sample Input 2

3 3 5
1 3 10
2 1 7
1 3 5
2 2 10
2 1 1

Sample Output 2

30
44
31
56
42

Sample Input 3

200000 200000 4
2 112219 100000000
1 73821 100000000
2 26402 100000000
1 73821 100000000

Sample Output 3

20000000000000
39999900000000
59999800000000
59999800000000

The integers to be printed may not fit into a 32-bit integer type.

G - Spanning Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点のグラフ G があり、頂点には 1, 2, \dots, N の番号がついています。はじめ、G には辺がありません。
これから G に何本か無向辺を追加します。ただし、辺を追加し終わった後、任意の i, j\,(i ≠ j) について以下の条件を満たす必要があります。

  • A_{i, j} = 1 のとき、頂点 i と頂点 j を直接結ぶ辺が存在する。
  • A_{i, j} = 0 のとき、頂点 i と頂点 j を直接結ぶ辺が存在しない。
  • A_{i, j} = -1 のとき、どちらでもよい。

辺を追加し終わった後の G としてあり得るもののうち、木であるものはいくつあるでしょうか?
ただし、答えは非常に大きくなることがあるので、答えを (10^9 + 7) で割った余りを求めてください。

制約

  • 入力は全て整数
  • 2 ≤ N ≤ 300
  • -1 ≤ A_{i, j} = A_{j, i} ≤ 1
  • A_{i, i} = 0

入力

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

N
A_{1, 1} \cdots A_{1, N}
\vdots
A_{N, 1} \cdots A_{N, N}

出力

答えを (10^9 + 7) で割った余りを出力せよ。


入力例 1

4
0 1 -1 0
1 0 -1 -1
-1 -1 0 0
0 -1 0 0

出力例 1

2

頂点 1 と頂点 2 の間には辺が必要で、頂点 1 と頂点 4 の間、頂点 3 と頂点 4 の間には辺を追加してはいけません。

したがって、以下の 2 個が条件を満たします。


入力例 2

3
0 1 1
1 0 1
1 1 0

出力例 2

0

入力例 3

3
0 0 0
0 0 0
0 0 0

出力例 3

0

入力例 4

11
0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 0 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 0 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 0 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 0 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 0 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0

出力例 4

357947677

頂点を区別するとき、11 頂点の木は全部で 11^9 個あります。

Score : 600 points

Problem Statement

We have a graph with N vertices numbered 1, 2, \dots, N. Initially, it has no edges.
Now, let us add some number of undirected edges to G so that the following condition holds for any i, j\,(i ≠ j) after addition.

  • If A_{i, j} = 1, there is an edge directly connecting Vertex i and Vertex j;
  • if A_{i, j} = 0, there is no edge directly connecting Vertex i and Vertex j;
  • if A_{i, j} = -1, either is fine.

Among the graphs that can be G after addition, how many are trees?
Since the count can be enormous, find it modulo (10^9 + 7).

Constraints

  • All values in input are integers.
  • 2 ≤ N ≤ 300
  • -1 ≤ A_{i, j} = A_{j, i} ≤ 1
  • A_{i, i} = 0

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} \cdots A_{1, N}
\vdots
A_{N, 1} \cdots A_{N, N}

Ouput

Print the count modulo (10^9 + 7).


Sample Input 1

4
0 1 -1 0
1 0 -1 -1
-1 -1 0 0
0 -1 0 0

Sample Output 1

2

We need an edge between Vertex 1 and Vertex 2, and we must not add an edge between Vertex 1 and Vertex 4 or between Vertex 3 and Vertex 4.

Thus, we have the following two valid graphs:


Sample Input 2

3
0 1 1
1 0 1
1 1 0

Sample Output 2

0

Sample Input 3

3
0 0 0
0 0 0
0 0 0

Sample Output 3

0

Sample Input 4

11
0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 0 -1 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 0 -1 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 0 -1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 0 -1 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 0 -1 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 0 -1
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0

Sample Output 4

357947677

When we distinguish the vertices, there are 11^9 trees with 11 vertices.

H - Shipping

Time Limit: 5 sec / Memory Limit: 1024 MB

配点 : 600

問題文

AtCoder 国には、都市 1 から都市 N までの N 個の都市と運河 1 から運河 N までの N 本の運河があります。
運河 i は都市 i と都市 A_i を双方向に繋いでおり、通行料は C_i 円です。運河を通るには通行料を払う必要がありますが、1 度払えばその運河は任意の方向に何度でも使うことができます。
どの都市からどの都市へも、運河をいくつか使って辿り着けることが保証されます。

あなたは AtCoder 国で M 個の荷物配送を任されました。i 個目の荷物は都市 X_i から都市 Y_i へと運ばなければなりません。
荷物を運ぶ手段は運河以外にありませんが、あなた自身は運河を使わずとも都市間を自由に移動することができます。

M 個の荷物全てを配送するとき、払う通行料の合計として考えられる最小値を求めてください。

制約

  • 3 \le N \le 2 \times 10^5
  • 1 \le M \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i
  • 1 \le C_i \le 10^9
  • どの都市からどの都市へも、運河をいくつか使って辿り着ける
  • 1 \le X_i \le N
  • 1 \le Y_i \le N
  • X_i \neq Y_i
  • 入力に含まれる値は全て整数である

入力

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

N M
A_1 C_1
A_2 C_2
A_3 C_3
\hspace{13pt} \vdots
A_N C_N
X_1 Y_1
X_2 Y_2
X_3 Y_3
\hspace{13pt} \vdots
X_M Y_M

出力

払う通行料の合計として考えられる最小値 [円] を出力せよ。


入力例 1

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

出力例 1

10

都市と運河の配置は以下の図のようになっています。
運河を表す線に書かれている数は、その運河の通行料を表します。
図

以下のように配送すると、払う必要のある通行料は 10 円になります。

  • 1 個目の荷物 : 運河 1, 4 を使って、都市 4, 1, 3 の順に運ぶ
  • 2 個目の荷物 : 運河 3, 1 を使って、都市 2, 3, 1 の順に運ぶ

入力例 2

5 2
2 2
5 5
5 7
2 4
3 10
3 5
4 1

出力例 2

13

同じ都市の組を結ぶ運河が複数ある可能性があります。


入力例 3

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

出力例 3

26

Score : 600 points

Problem Statement

In the Republic of AtCoder, there are N cities called City 1 through City N and N canals called Canal 1 through Canal N.
Canal i connects City i and City A_i bidirectionally, and you have to pay the toll of C_i yen to go through it, but after paying the toll once, you can use it any number of times in any direction.
It is guaranteed that you can reach from any city to any city using some canals.

You are asked to deliver M cargoes in this country. The i-th cargo should be delivered from City X_i to City Y_i.
There is no way other than using the canals to deliver the cargoes, but you yourself can travel between the cities freely without using the canals.

Find the minimum total toll you have to pay to deliver all M cargoes.

Constraints

  • 3 \le N \le 2 \times 10^5
  • 1 \le M \le 2 \times 10^5
  • 1 \le A_i \le N
  • A_i \neq i
  • 1 \le C_i \le 10^9
  • It is possible to reach from any city to any city by using some canals.
  • 1 \le X_i \le N
  • 1 \le Y_i \le N
  • X_i \neq Y_i
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 C_1
A_2 C_2
A_3 C_3
\hspace{13pt} \vdots
A_N C_N
X_1 Y_1
X_2 Y_2
X_3 Y_3
\hspace{13pt} \vdots
X_M Y_M

Output

Print the minimum total toll you have to pay, as an integer.


Sample Input 1

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

Sample Output 1

10

The figure below shows the cities and canals in this country, where numbers along the lines representing canals represent the tolls:
Figure

You can deliver the cargoes as follows to make the total toll 10 yen:

  • The 1-st cargo: use Canal 1, 4 to deliver it on the route: City 4, 1, 3.
  • The 2-nd cargo: use Canal 3, 1 to deliver it on this route: City 2, 3, 1.

Sample Input 2

5 2
2 2
5 5
5 7
2 4
3 10
3 5
4 1

Sample Output 2

13

Multiple canals may connect the same pair of cities.


Sample Input 3

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

Sample Output 3

26