A - A Healthy Breakfast

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

配点 : 100 点

問題文

高橋くんは、朝食にご飯、味噌汁、サラダを 1 皿ずつ食べます。

高橋くんの家のテーブルは細長いので、 3 皿を横一列に並べました。並べ方は文字列 S によって与えられ、左から i 番目の皿は S_i が R ならご飯、 S_i が M なら味噌汁、 S_i が S ならサラダです。

ご飯の皿が味噌汁の皿より左にあるかどうかを判定してください。

制約

  • |S| = 3
  • S には R M S が 1 文字ずつ含まれる

入力

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

S

出力

ご飯の皿が味噌汁の皿より左にあれば Yes 、そうでなければ No を出力してください。


入力例 1

RSM

出力例 1

Yes

ご飯の皿は左から 1 番目にあり、味噌汁の皿は左から 3 番目にあります。ご飯の皿の方が左にあるので、 Yes を出力します。


入力例 2

SMR

出力例 2

No

左から順に、サラダの皿、味噌汁の皿、ご飯の皿と並んでいます。

Score : 100 points

Problem Statement

Takahashi eats three plates for breakfast: rice, miso soup, and salad.

His table is long and narrow, so he arranged the three plates in a row. The arrangement is given by a string S, where the i-th plate from the left is rice if S_i is R, miso soup if S_i is M, and salad if S_i is S.

Determine whether the plate of rice is to the left of the plate of miso soup.

Constraints

  • |S| = 3
  • S contains one R, one M, and one S.

Input

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

S

Output

Print Yes if the plate of rice is to the left of the plate of miso soup, and No otherwise.


Sample Input 1

RSM

Sample Output 1

Yes

The plate of rice is at the 1st position from the left, and the plate of miso soup is at the 3rd position from the left. Since the plate of rice is to the left, print Yes.


Sample Input 2

SMR

Sample Output 2

No

The plates are arranged as salad, miso soup, and rice from left to right.

B - Vacation Validation

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

配点 : 100 点

問題文

o, x からなる長さ N の文字列 S と整数 L,R が与えられます。

S の L 文字目から R 文字目までが全て o であるかどうか判定してください。

制約

  • 1\leq N \leq 100
  • 1\leq L \leq R \leq N
  • S は o, x のみからなる長さ N の文字列
  • N,L,R は全て整数

入力

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

N L R
S

出力

S の L 文字目から R 文字目までが全て o なら Yes、そうでないなら No と出力せよ。


入力例 1

10 6 8
xoxxooooxo

出力例 1

Yes

S の 6 文字目から 8 文字目は全て o なので答えは Yes です。


入力例 2

9 6 8
xoxxoxoox

出力例 2

No

6 文字目が x なので答えは No です。

Score : 100 points

Problem Statement

You are given a string S of length N consisting of o and x, and integers L and R.

Determine whether all characters from the L-th through the R-th character of S are o.

Constraints

  • 1\leq N \leq 100
  • 1\leq L \leq R \leq N
  • S is a string of length N consisting of o and x.
  • N, L, and R are all integers.

Input

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

N L R
S

Output

If all characters from the L-th through the R-th character of S are o, output Yes; otherwise, output No.


Sample Input 1

10 6 8
xoxxooooxo

Sample Output 1

Yes

All characters from the 6-th through the 8-th character of S are o, so the answer is Yes.


Sample Input 2

9 6 8
xoxxoxoox

Sample Output 2

No

The 6-th character is x, so the answer is No.

C - Grid Walk

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

配点 : 200 点

問題文

H 行 W 列のグリッドがあります。グリッドの上から i 番目、左から j 番目のマスをマス (i, j) と表記します。

マス (i, j) は C_{i, j} が . のとき空きマスであり、# のとき空きマスではありません。

高橋君は現在マス (S_i, S_j) におり、i = 1, 2, \ldots, |X| の順に以下のルールに従って行動します。

  • X の i 文字目が L のとき、高橋君が現在いるマスの 1 つ左のマスが存在し、そのマスが空きマスならば 1 つ左のマスに移動する。そうでないならば、現在いるマスに留まる。
  • X の i 文字目が R のとき、高橋君が現在いるマスの 1 つ右のマスが存在し、そのマスが空きマスならば 1 つ右のマスに移動する。そうでないならば、現在いるマスに留まる。
  • X の i 文字目が U のとき、高橋君が現在いるマスの 1 つ上のマスが存在し、そのマスが空きマスならば 1 つ上のマスに移動する。そうでないならば、現在いるマスに留まる。
  • X の i 文字目が D のとき、高橋君が現在いるマスの 1 つ下のマスが存在し、そのマスが空きマスならば 1 つ下のマスに移動する。そうでないならば、現在いるマスに留まる。

一連の行動を終えた後高橋君がどのマスにいるか出力してください。

制約

  • 1 \leq H, W \leq 50
  • 1 \leq S_i \leq H
  • 1 \leq S_j \leq W
  • H, W, S_i, S_j は整数
  • C_{i, j} は . または #
  • C_{S_i, S_j} = .
  • X は L, R, U, D からなる長さ 1 以上 50 以下の文字列

入力

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

H W
S_i S_j
C_{1, 1}C_{1, 2}\ldotsC_{1, W}
C_{2, 1}C_{2, 2}\ldotsC_{2, W}
\vdots
C_{H, 1}C_{H, 2}\ldotsC_{H, W}
X

出力

高橋君が一連の行動を終えた後にいるマスをマス (x, y) として、x と y をこの順に空白区切りで出力せよ。


入力例 1

2 3
2 1
.#.
...
ULDRU

出力例 1

2 2

高橋君ははじめマス (2, 1) にいます。高橋君の一連の行動は以下のようになります。

  • X の 1 文字目は U であり、マス (2, 1) の 1 つ上のマスは存在し、そのマスは空きマスであるため 1 つ上のマスであるマス (1, 1) に移動する。
  • X の 2 文字目は L であり、マス (1, 1) の 1 つ左のマスは存在しないためマス (1, 1) に留まる。
  • X の 3 文字目は D であり、マス (1, 1) の 1 つ下のマスは存在し、そのマスは空きマスであるため 1 つ下のマスであるマス (2, 1) に移動する。
  • X の 4 文字目は R であり、マス (2, 1) の 1 つ右のマスは存在し、そのマスは空きマスであるため 1 つ右のマスであるマス (2, 2) に移動する。
  • X の 5 文字目は U であり、マス (2, 2) の 1 つ上のマスは存在するが、そのマスは空きマスではないためマス (2, 2) に留まる。

したがって一連の行動を終えた後に高橋君がいるマスはマス (2, 2) です。


入力例 2

4 4
4 2
....
.#..
...#
....
DUUUURULRD

出力例 2

2 4

入力例 3

6 6
1 1
.#####
######
######
######
######
######
RURLDLULLRULRDL

出力例 3

1 1

Score : 200 points

Problem Statement

There is a grid with H rows and W columns. Let (i, j) denote the cell at the i-th row from the top and j-th column from the left.

Cell (i, j) is empty if C_{i, j} is ., and not empty if C_{i, j} is #.

Takahashi is currently at cell (S_i, S_j), and he will act according to the following rules for i = 1, 2, \ldots, |X| in order.

  • If the i-th character of X is L, and the cell to the left of his current cell exists and is empty, he moves to the cell to the left. Otherwise, he stays in the current cell.
  • If the i-th character of X is R, and the cell to the right of his current cell exists and is empty, he moves to the cell to the right. Otherwise, he stays in the current cell.
  • If the i-th character of X is U, and the cell above his current cell exists and is empty, he moves to the cell above. Otherwise, he stays in the current cell.
  • If the i-th character of X is D, and the cell below his current cell exists and is empty, he moves to the cell below. Otherwise, he stays in the current cell.

Print the cell where he is after completing the series of actions.

Constraints

  • 1 \leq H, W \leq 50
  • 1 \leq S_i \leq H
  • 1 \leq S_j \leq W
  • H, W, S_i, S_j are integers.
  • C_{i, j} is . or #.
  • C_{S_i, S_j} = .
  • X is a string of length between 1 and 50, inclusive, consisting of L, R, U, D.

Input

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

H W
S_i S_j
C_{1, 1}C_{1, 2}\ldotsC_{1, W}
C_{2, 1}C_{2, 2}\ldotsC_{2, W}
\vdots
C_{H, 1}C_{H, 2}\ldotsC_{H, W}
X

Output

Let (x, y) be the cell where Takahashi is after completing the series of actions. Print x and y, separated by a space.


Sample Input 1

2 3
2 1
.#.
...
ULDRU

Sample Output 1

2 2

Takahashi starts at cell (2, 1). His series of actions are as follows:

  • The 1st character of X is U, and the cell above (2, 1) exists and is an empty cell, so he moves to the cell above, which is (1, 1).
  • The 2nd character of X is L, and the cell to the left of (1, 1) does not exist, so he stays at (1, 1).
  • The 3rd character of X is D, and the cell below (1, 1) exists and is an empty cell, so he moves to the cell below, which is (2, 1).
  • The 4th character of X is R, and the cell to the right of (2, 1) exists and is an empty cell, so he moves to the cell to the right, which is (2, 2).
  • The 5th character of X is U, and the cell above (2, 2) exists but is not an empty cell, so he stays at (2, 2).

Therefore, after completing the series of actions, he is at cell (2, 2).


Sample Input 2

4 4
4 2
....
.#..
...#
....
DUUUURULRD

Sample Output 2

2 4

Sample Input 3

6 6
1 1
.#####
######
######
######
######
######
RURLDLULLRULRDL

Sample Output 3

1 1
D - Not All

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

配点 : 200 点

問題文

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

A の末尾の要素を削除するという操作を 0 回以上 N 回以下行うことで、以下の条件が満たされないようにしたいです。

  • 条件:A には 1 以上 M 以下の整数がすべて含まれている。

必要な操作回数の最小値を求めてください。

なお、本問題の制約下において、操作を 0 回以上 N 回以下行うことで上述の条件が満たされないようにすることが必ず可能であることが証明できます。

制約

  • 1\leq M \leq N \leq 100
  • 1\leq A_i \leq M
  • 入力は全て整数

入力

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

N M
A_1 A_2 \dots A_N

出力

条件が満たされなくなるために必要な操作回数の最小値を出力せよ。


入力例 1

5 3
3 2 3 1 2

出力例 1

2

最初、A=(3,2,3,1,2) です。このとき、A には 1 以上 3 以下の整数がすべて含まれるため条件を満たします。

A の末尾の要素を削除する操作を 1 回行うと、A=(3,2,3,1) となります。このとき、A には 1 以上 3 以下の整数がすべて含まれるため条件を満たします。

A の末尾の要素を削除する操作をもう 1 回行うと、A=(3,2,3) となります。このとき、A には 1 が含まれないため条件を満たしません。

よって、条件が満たされなくなるために必要な操作回数の最小値は 2 です。


入力例 2

4 3
1 3 1 3

出力例 2

0

A には最初から 2 が含まれず条件を満たさないため、操作を 1 回も行う必要がありません。


入力例 3

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

出力例 3

6

Score : 200 points

Problem Statement

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

Your goal is to make the following condition false by performing this operation between 0 and N times (inclusive): remove the last element of A.

  • Condition: A contains every integer from 1 through M.

Find the minimum number of operations required.

Under the constraints of this problem, it can be proved that it is always possible to make the condition false by performing the operation between 0 and N times.

Constraints

  • 1 \le M \le N \le 100
  • 1 \le A_i \le M
  • All input values are integers.

Input

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

N M
A_1 A_2 \dots A_N

Output

Output the minimum number of operations required to make the condition false.


Sample Input 1

5 3
3 2 3 1 2

Sample Output 1

2

Initially, A = (3,2,3,1,2). Since A contains every integer from 1 through 3, the condition holds.

If you perform the operation once, A = (3,2,3,1). The condition still holds.

If you perform the operation once more, A = (3,2,3). The integer 1 is missing, so the condition no longer holds.

Therefore, the minimum required number of operations is 2.


Sample Input 2

4 3
1 3 1 3

Sample Output 2

0

Since A initially lacks the integer 2, the condition is already false, so no operation is needed.


Sample Input 3

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

Sample Output 3

6
E - One Time Swap

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

配点 : 350 点

問題文

文字列 S が与えられます。次の操作を ちょうど 1 回 行った後の文字列としてあり得るものがいくつあるか求めてください。

  • S の長さを N とする。 1\leq i<j\leq N をみたす整数の組 (i,j) を選び、S の i 文字目と j 文字目を入れ替える。

なお、この問題の制約下で操作を必ず行うことができることが証明できます。

制約

  • S は英小文字からなる長さ 2 以上 10^6 以下の文字列

入力

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

S

出力

S に対して問題文中の操作をちょうど 1 回行った後の文字列としてあり得るものの個数を出力せよ。


入力例 1

abc

出力例 1

3

S の長さは 3 であるため、1\leq i<j\leq 3 をみたす整数の組 (i,j) としては、 (1,2), (1,3), (2,3) の 3 通りが考えられます。

  • S の 1 文字目と 2 文字目を入れ替えた時、S は bac となります。
  • S の 1 文字目と 3 文字目を入れ替えた時、S は cba となります。
  • S の 2 文字目と 3 文字目を入れ替えた時、S は acb となります。

よって、abc に対して操作を行った後の文字列としては、bac, cba, acb の 3 つがあり得るため、3 を出力します。


入力例 2

aaaaa

出力例 2

1

どの 2 つの文字を入れ替えても S は aaaaa のままです。よって、操作後の文字列としてあり得るものは 1 つです。

Points: 350 points

Problem Statement

You are given a string S. Find the number of strings that can result from performing the following operation exactly once.

  • Let N be the length of S. Choose a pair of integers (i,j) such that 1\leq i<j\leq N and swap the i-th and j-th characters of S.

It can be proved that you can always perform it under the constraints of this problem.

Constraints

  • S is a string of length between 2 and 10^6, inclusive, consisting of lowercase English letters.

Input

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

S

Output

Print the number of strings that can result from performing the operation in the problem statement exactly once on S.


Sample Input 1

abc

Sample Output 1

3

The length of S is 3, so 1\leq i<j\leq 3 is satisfied by three pairs of integers (i,j): (1,2), (1,3), and (2,3).

  • Swapping the 1-st and 2-nd characters of S results in S being bac.
  • Swapping the 1-st and 3-rd characters of S results in S being cba.
  • Swapping the 2-nd and 3-rd characters of S results in S being acb.

Therefore, the operation on abc results in one of the three strings: bac, cba, and acb, so print 3.


Sample Input 2

aaaaa

Sample Output 2

1

Swapping any two characters results in S remaining aaaaa. Thus, only one string can result from the operation.

F - 1111gal password

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

配点 : 300 点

問題文

整数 N が与えられるので、以下の条件を全て満たす整数 X の個数を 998244353 で割った余りを求めてください。

  • N 桁の正整数である。
  • X の各桁を上の位から順に X_1,X_2,\dots,X_N とする。このとき以下の条件を全て満たす。
    • 全ての整数 1 \le i \le N に対して、 1 \le X_i \le 9
    • 全ての整数 1 \le i \le N-1 に対して、 |X_i-X_{i+1}| \le 1

制約

  • N は整数
  • 2 \le N \le 10^6

入力

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

N

出力

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


入力例 1

4

出力例 1

203

4 桁の整数として、例えば 1111,1234,7878,6545 が問題文中の条件を満たします。


入力例 2

2

出力例 2

25

入力例 3

1000000

出力例 3

248860093

998244353 で割った余りを求めることに注意してください。

Score : 300 points

Problem Statement

Given an integer N, find the number of integers X that satisfy all of the following conditions, modulo 998244353.

  • X is an N-digit positive integer.
  • Let X_1,X_2,\dots,X_N be the digits of X from top to bottom. They satisfy all of the following:
    • 1 \le X_i \le 9 for all integers 1 \le i \le N;
    • |X_i-X_{i+1}| \le 1 for all integers 1 \le i \le N-1.

Constraints

  • N is an integer.
  • 2 \le N \le 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer as an integer.


Sample Input 1

4

Sample Output 1

203

Some of the 4-digit integers satisfying the conditions are 1111,1234,7878,6545.


Sample Input 2

2

Sample Output 2

25

Sample Input 3

1000000

Sample Output 3

248860093

Be sure to find the count modulo 998244353.

G - Reindeer and Sleigh

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

配点 : 400 点

問題文

N 台のソリがあり、各ソリには 1,2,\ldots, N の番号がつけられています。

ソリ i を引くために必要なトナカイは R_i 匹です。

また、各トナカイが引けるソリは高々 1 台です。より厳密には、m 台のソリ i_1, i_2, \ldots, i_m を引くために必要なトナカイは \sum_{k=1}^{m} R_{i_k} 匹です。

以下の形式のクエリが Q 個与えられるので、答えを求めてください。

  • 整数 X が与えられる。トナカイが X 匹いるときに最大で何台のソリを引けるか求めよ。

制約

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq R_i \leq 10^9
  • 1 \leq X \leq 2 \times 10^{14}
  • 入力される数値はすべて整数

入力

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

N Q
R_1 R_2 \ldots R_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

各クエリは次の形式で与えられる。

X

出力

Q 行出力せよ。

i 行目には i 個目のクエリに対する答えを出力せよ。


入力例 1

4 3
5 3 11 8
16
7
1000

出力例 1

3
1
4

トナカイが 16 匹いるとき、ソリ 1,2,4 を引くことができます。

16 匹のトナカイで 4 台のソリを引くことはできないので、クエリ 1 の答えは 3 となります。


入力例 2

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

出力例 2

1
1
2
2
2
3

入力例 3

2 2
1000000000 1000000000
200000000000000
1

出力例 3

2
0

Score : 400 points

Problem Statement

There are N sleighs numbered 1,2,\ldots, N.

R_i reindeer are required to pull sleigh i.

Additionally, each reindeer can pull at most one sleigh. More precisely, \sum_{k=1}^{m} R_{i_k} reindeer are required to pull m sleighs i_1, i_2, \ldots, i_m.

Find the answer to Q queries of the following form:

  • You are given an integer X. Determine the maximum number of sleighs that can be pulled when there are X reindeer.

Constraints

  • 1 \leq N, Q \leq 2 \times 10^5
  • 1 \leq R_i \leq 10^9
  • 1 \leq X \leq 2 \times 10^{14}
  • All input values are integers.

Input

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

N Q
R_1 R_2 \ldots R_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in the following format:

X

Output

Print Q lines.

The i-th line should contain the answer to the i-th query.


Sample Input 1

4 3
5 3 11 8
16
7
1000

Sample Output 1

3
1
4

When there are 16 reindeer, sleighs 1,2,4 can be pulled.

It is impossible to pull four sleighs with 16 reindeer, so the answer to query 1 is 3.


Sample Input 2

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

Sample Output 2

1
1
2
2
2
3

Sample Input 3

2 2
1000000000 1000000000
200000000000000
1

Sample Output 3

2
0
H - Reachability in Functional Graph

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

配点 : 450 点

問題文

頂点に 1 から N の番号がついた N 頂点 N 辺の有向グラフがあります。
全ての頂点の出次数は 1 で、頂点 i から出ている辺は頂点 a_i へ伸びています。
頂点の組 (u, v) であって、頂点 u から頂点 v へ到達可能であるものの個数を求めてください。

ここで、頂点 u から頂点 v へ到達可能であるとは、ある長さ K+1 の頂点の列 w_0, w_1, \dots, w_K であって次の条件を全て満たすものが存在することをいいます。特に、u = v の時は常に到達可能です。

  • w_0 = u
  • w_K = v
  • 0 \leq i \lt K を満たす全ての i について、頂点 w_i から頂点 w_{i+1} へ伸びる辺が存在する。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq a_i \leq N
  • 入力される値は全て整数

入力

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

N
a_1 a_2 \dots a_N

出力

頂点の組 (u, v) であって、頂点 u から頂点 v へ到達可能であるものの個数を出力せよ。


入力例 1

4
2 1 1 4

出力例 1

8

頂点 1 から到達可能である頂点は頂点 1, 2 です。
頂点 2 から到達可能である頂点は頂点 1, 2 です。
頂点 3 から到達可能である頂点は頂点 1, 2, 3 です。
頂点 4 から到達可能である頂点は頂点 4 のみです。
よって、頂点の組 (u, v) であって頂点 u から頂点 v へ到達可能であるものの個数は 8 個です。
頂点 4 から出ている辺は自己ループ、すなわち頂点 4 自身へ伸びている辺である点に注意してください。


入力例 2

5
2 4 3 1 2

出力例 2

14

入力例 3

10
6 10 4 1 5 9 8 6 5 1

出力例 3

41

Score : 450 points

Problem Statement

There is a directed graph with N vertices numbered 1 to N and N edges.
The out-degree of every vertex is 1, and the edge from vertex i points to vertex a_i.
Count the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u.

Here, vertex v is reachable from vertex u if there exists a sequence of vertices w_0, w_1, \dots, w_K of length K+1 that satisfies the following conditions. In particular, if u = v, it is always reachable.

  • w_0 = u.
  • w_K = v.
  • For every 0 \leq i \lt K, there is an edge from vertex w_i to vertex w_{i+1}.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq a_i \leq N
  • All input values are integers.

Input

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

N
a_1 a_2 \dots a_N

Output

Print the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u.


Sample Input 1

4
2 1 1 4

Sample Output 1

8

The vertices reachable from vertex 1 are vertices 1, 2.
The vertices reachable from vertex 2 are vertices 1, 2.
The vertices reachable from vertex 3 are vertices 1, 2, 3.
The vertex reachable from vertex 4 is vertex 4.
Therefore, the number of pairs of vertices (u, v) such that vertex v is reachable from vertex u is 8.
Note that the edge from vertex 4 is a self-loop, that is, it points to vertex 4 itself.


Sample Input 2

5
2 4 3 1 2

Sample Output 2

14

Sample Input 3

10
6 10 4 1 5 9 8 6 5 1

Sample Output 3

41
I - Breakdown

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

配点 : 475 点

問題文

N 個の頂点と M 本の辺からなる単純な無向グラフが与えられます。 i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ辺です。 また、i = 1, 2, \ldots, N について、頂点 i には正整数 W_i が割り当てられており、A_i 個のコマが置かれています。

グラフ上にコマが存在する限り、下記の操作を繰り返します。

  • まず、グラフ上のコマを 1 個選んで取り除き、そのコマが置かれていた頂点を x とおく。
  • x に隣接するいくつかの頂点からなる(空でも良い)集合 S であって、\sum_{y \in S} W_y \lt W_x であるものを選び、S に含まれる頂点それぞれに 1 個ずつコマを置く。

操作を行う回数としてあり得る最大値を出力してください。

なお、どのように操作を行っても、有限回の操作の後にはグラフ上にコマが存在しない状態に至ることが証明出来ます。

制約

  • 入力される値はすべて整数
  • 2 \leq N \leq 5000
  • 1 \leq M \leq \min \lbrace N(N-1)/2, 5000 \rbrace
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • i \neq j \implies \lbrace u_i, v_i \rbrace \neq \lbrace u_j, v_j \rbrace
  • 1 \leq W_i \leq 5000
  • 0 \leq A_i \leq 10^9

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
W_1 W_2 \ldots W_N
A_1 A_2 \ldots A_N

出力

答えを出力せよ。


入力例 1

6 6
1 2
2 3
3 1
3 4
1 5
5 6
9 2 3 1 4 4
1 0 0 0 0 1

出力例 1

5

以下の説明では、各頂点にあるコマの個数を、数列 A = (A_1, A_2, \ldots, A_N) として表します。 はじめ、A = (1, 0, 0, 0, 0, 1) です。

下記の手順で操作を行うことを考えます。

  • 頂点 1 にあるコマを 1 個取り除き、頂点 2, 3 にコマを 1 個ずつ置く。その結果、A = (0, 1, 1, 0, 0, 1) となる。
  • 頂点 2 にあるコマを 1 個取り除く。その結果、A = (0, 0, 1, 0, 0, 1) となる。
  • 頂点 6 にあるコマを 1 個取り除く。その結果、A = (0, 0, 1, 0, 0, 0) となる。
  • 頂点 3 にあるコマを 1 個取り除き、頂点 2 にコマを 1 個置く。その結果、A = (0, 1, 0, 0, 0, 0) となる。
  • 頂点 2 にあるコマを 1 個取り除く。その結果、A = (0, 0, 0, 0, 0, 0) となる。

上記の手順で操作を行う回数は 5 回であり、これが操作を行う回数としてあり得る最大値です。


入力例 2

2 1
1 2
1 2
0 0

出力例 2

0

この入力例では、はじめからグラフ上にコマが存在しません。


入力例 3

10 20
4 8
1 10
1 7
5 9
9 10
8 10
7 5
1 4
7 3
8 7
2 8
5 8
4 2
5 1
7 2
8 3
3 4
8 9
7 10
2 3
25 5 1 1 16 5 98 3 21 1
35 39 32 11 35 37 14 29 36 1

出力例 3

1380

Score: 475 points

Problem Statement

You are given a simple undirected graph consisting of N vertices and M edges. For i = 1, 2, \ldots, M, the i-th edge connects vertices u_i and v_i. Also, for i = 1, 2, \ldots, N, vertex i is assigned a positive integer W_i, and there are A_i pieces placed on it.

As long as there are pieces on the graph, repeat the following operation:

  • First, choose and remove one piece from the graph, and let x be the vertex on which the piece was placed.
  • Choose a (possibly empty) set S of vertices adjacent to x such that \sum_{y \in S} W_y \lt W_x, and place one piece on each vertex in S.

Print the maximum number of times the operation can be performed.

It can be proved that, regardless of how the operation is performed, there will be no pieces on the graph after a finite number of iterations.

Constraints

  • All input values are integers.
  • 2 \leq N \leq 5000
  • 1 \leq M \leq \min \lbrace N(N-1)/2, 5000 \rbrace
  • 1 \leq u_i, v_i \leq N
  • u_i \neq v_i
  • i \neq j \implies \lbrace u_i, v_i \rbrace \neq \lbrace u_j, v_j \rbrace
  • 1 \leq W_i \leq 5000
  • 0 \leq A_i \leq 10^9

Input

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M
W_1 W_2 \ldots W_N
A_1 A_2 \ldots A_N

Output

Print the answer.


Sample Input 1

6 6
1 2
2 3
3 1
3 4
1 5
5 6
9 2 3 1 4 4
1 0 0 0 0 1

Sample Output 1

5

In the following explanation, let A = (A_1, A_2, \ldots, A_N) represent the numbers of pieces on the vertices. Initially, A = (1, 0, 0, 0, 0, 1).

Consider performing the operation as follows:

  • Remove one piece from vertex 1 and place one piece each on vertices 2 and 3. Now, A = (0, 1, 1, 0, 0, 1).
  • Remove one piece from vertex 2. Now, A = (0, 0, 1, 0, 0, 1).
  • Remove one piece from vertex 6. Now, A = (0, 0, 1, 0, 0, 0).
  • Remove one piece from vertex 3 and place one piece on vertex 2. Now, A = (0, 1, 0, 0, 0, 0).
  • Remove one piece from vertex 2. Now, A = (0, 0, 0, 0, 0, 0).

In this procedure, the operation is performed five times, which is the maximum possible number of times.


Sample Input 2

2 1
1 2
1 2
0 0

Sample Output 2

0

In this sample input, there are no pieces on the graph from the beginning.


Sample Input 3

10 20
4 8
1 10
1 7
5 9
9 10
8 10
7 5
1 4
7 3
8 7
2 8
5 8
4 2
5 1
7 2
8 3
3 4
8 9
7 10
2 3
25 5 1 1 16 5 98 3 21 1
35 39 32 11 35 37 14 29 36 1

Sample Output 3

1380