A - Your First Judge

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

配点 : 100 点

問題文

文字列 S が与えられるので、この文字列が Hello,World! と完全に一致するなら AC 、そうでないなら WA と出力してください。

「完全に一致する」とは?文字列 A と B が完全に一致するとは、文字列 A と B の長さが等しく、かつ全ての 1 \le i \le |A| を満たす整数 i について A の先頭から i 文字目と B の先頭から i 文字目とが(英大文字か小文字かも含めて)一致することを指します。

制約

  • 1 \le |S| \le 15
  • S は英大小文字, ,, ! のみからなる

入力

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

S

出力

答えを出力せよ。


入力例 1

Hello,World!

出力例 1

AC

文字列 S は Hello,World! と完全に一致します。


入力例 2

Hello,world!

出力例 2

WA

先頭から 7 文字目の W が、 Hello,World! では大文字ですが S では小文字です。よって S は Hello,World! と一致しません。


入力例 3

Hello!World!

出力例 3

WA

Score : 100 points

Problem Statement

Given a string S, print AC if it perfectly matches Hello,World!; otherwise, print WA.

What is a perfect match?Strings A is said to perfectly match B when the length of A is equal to that of B, and the i-th character of A is the same as the i-th character of B for every integer i such that 1 \le i \le |A|.

Constraints

  • 1 \le |S| \le 15
  • S consists of English lowercase letters, English uppercase letters, ,, and !.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

Hello,World!

Sample Output 1

AC

The string S perfectly matches Hello,World!.


Sample Input 2

Hello,world!

Sample Output 2

WA

The seventh character from the beginning should be an uppercase W in Hello,World!, but S has a lowercase w in that position. Thus, S does not match Hello,World!.


Sample Input 3

Hello!World!

Sample Output 3

WA
B - Stage Clear

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

配点 : 100 点

問題文

高橋くんはゲームで遊んでいます。 このゲームにはワールドが 8 つあり、それぞれのワールドにはステージが 8 つずつあります。 ステージには順番があり、最初のステージは 1 個めのワールドにある 1 個めのステージです。 i 個め (1\le i\le 8) のワールドにある j 個め (1\le j\le 8) のステージの次のステージは、以下のようになっています。

  • j\lt8 のとき、次のステージは i 個めのワールドにある j+1 個めのステージです。
  • i\lt8,j=8 のとき、次のステージは i+1 個めのワールドにある 1 個めのステージです。
  • i=8,j=8 のとき、次のステージはありません。このステージが最後のステージです。

i 個め (1\le i\le 8) のワールドにある j 個め (1\le j\le 8) のステージの名前は i-j です。 たとえば、最初のステージ名は 1-1 で、最後のステージ名は 8-8 です。

高橋くんが現在遊んでいるステージのステージ名 S が与えられるので、次のステージのステージ名を出力してください。

制約

  • S はいずれかのステージのステージ名である。
  • S は 8-8 ではない。

入力

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

S

出力

次のステージのステージ名を出力せよ。


入力例 1

4-2

出力例 1

4-3

4 個めのワールドの 2 個めのステージの次のステージは、同じワールドの 3 個めのステージです。


入力例 2

1-8

出力例 2

2-1

1-8 は 1 個めのワールドの最後のステージなので、次のステージは 2 個めのワールドの 1 個めのステージです。


入力例 3

3-3

出力例 3

3-4

Score : 100 points

Problem Statement

Takahashi is playing a game. This game has eight worlds, and each world has eight stages. The stages have an order, and the first stage is the 1st stage in the 1st world. The next stage after the j-th stage (1\le j\le 8) in the i-th world (1\le i\le 8) is as follows:

  • When j\lt8, the next stage is the (j+1)-th stage in the i-th world.
  • When i\lt8,j=8, the next stage is the 1st stage in the (i+1)-th world.
  • When i=8,j=8, there is no next stage. This stage is the last stage.

The name of the j-th stage (1\le j\le 8) in the i-th world (1\le i\le 8) is i-j. For example, the name of the first stage is 1-1, and the name of the last stage is 8-8.

Given the stage name S of the stage Takahashi is currently playing, output the stage name of the next stage.

Constraints

  • S is the stage name of one of the stages.
  • S is not 8-8.

Input

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

S

Output

Output the stage name of the next stage.


Sample Input 1

4-2

Sample Output 1

4-3

The next stage after the 2nd stage in the 4th world is the 3rd stage in the same world.


Sample Input 2

1-8

Sample Output 2

2-1

1-8 is the last stage in the 1st world, so the next stage is the 1st stage in the 2nd world.


Sample Input 3

3-3

Sample Output 3

3-4
C - At Most 3 (Judge ver.)

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

配点 : 200 点

問題文

おもり 1, おもり 2, \dots, おもり N の N 個のおもりがあります。おもり i の重さは A_i です。
以下の条件を満たす正整数 n を 良い整数 と呼びます。

  • \bf{3} 個以下 の異なるおもりを自由に選んで、選んだおもりの重さの和を n にすることができる。

W 以下の正整数のうち、良い整数は何個ありますか?

制約

  • 1 \leq N \leq 300
  • 1 \leq W \leq 10^6
  • 1 \leq A_i \leq 10^6
  • 入力される値はすべて整数

入力

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

N W
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

2 10
1 3

出力例 1

3

おもり 1 のみを選ぶと重さの和は 1 になります。よって 1 は良い整数です。
おもり 2 のみを選ぶと重さの和は 3 になります。よって 3 は良い整数です。
おもり 1 とおもり 2 を選ぶと重さの和は 4 になります。よって 4 は良い整数です。
これら以外に良い整数は存在しません。また、1,3,4 のいずれも W 以下の整数です。よって答えは 3 個になります。


入力例 2

2 1
2 3

出力例 2

0

W 以下の良い整数は存在しません。


入力例 3

4 12
3 3 3 3

出力例 3

3

良い整数は 3,6,9 の 3 個です。
たとえばおもり 1, おもり 2, おもり 3 を選ぶと重さの和は 9 になるので、9 は良い整数です。
12 は良い整数 ではない ことに注意してください。


入力例 4

7 251
202 20 5 1 4 2 100

出力例 4

48

Score : 200 points

Problem Statement

There are N weights called Weight 1, Weight 2, \dots, Weight N. Weight i has a mass of A_i.
Let us say a positive integer n is a good integer if the following condition is satisfied:

  • We can choose at most 3 different weights so that they have a total mass of n.

How many positive integers less than or equal to W are good integers?

Constraints

  • 1 \leq N \leq 300
  • 1 \leq W \leq 10^6
  • 1 \leq A_i \leq 10^6
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N W
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

2 10
1 3

Sample Output 1

3

If we choose only Weight 1, it has a total mass of 1, so 1 is a good integer.
If we choose only Weight 2, it has a total mass of 3, so 3 is a good integer.
If we choose Weights 1 and 2, they have a total mass of 4, so 4 is a good integer.
No other integer is a good integer. Also, all of 1, 3, and 4 are integers less than or equal to W. Therefore, the answer is 3.


Sample Input 2

2 1
2 3

Sample Output 2

0

There are no good integers less than or equal to W.


Sample Input 3

4 12
3 3 3 3

Sample Output 3

3

There are 3 good integers: 3, 6, and 9.
For example, if we choose Weights 1, 2, and 3, they have a total mass of 9, so 9 is a good integer.
Note that 12 is not a good integer.


Sample Input 4

7 251
202 20 5 1 4 2 100

Sample Output 4

48
D - Pasta

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

配点 : 200 点

問題文

高橋君の家には N 本の麺からなるパスタがあり、i 本目の麺の長さは A_i です。
高橋君はこれから M 日間の食事計画を立てており、 i 日目にはパスタの麺のうち長さがちょうど B_i であるようなものを 1 本選び、食べようと考えています。 もし、1 日目から M 日目の間に 1 日でもそのような麺が無い日があれば、食事計画は失敗となります。 また、同じ麺を複数の日に食べることはできません。

高橋君が食事計画を最後まで実行することは可能ですか?

制約

  • 1 \leq M \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • 入力はすべて整数である。

入力

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

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

出力

高橋君が食事計画を最後まで実行できる場合は Yes を、そうでない場合は No を出力せよ。


入力例 1

3 2
1 1 3
3 1

出力例 1

Yes

1 日目に 3 本目の麺を、2 日目に 1 本目の麺を食べれば良いので、高橋君の食事計画は実行可能です。


入力例 2

1 1
1000000000
1

出力例 2

No

長さがちょうど 1 の麺が存在する必要があります。


入力例 3

5 2
1 2 3 4 5
5 5

出力例 3

No

長さが 5 の麺は 1 本しか存在しないため、2 日目に食事をとる事が出来ません。

Score : 200 points

Problem Statement

There is pasta consisting of N noodles at Takahashi's home. The length of the i-th noodle is A_i.
Takahashi has a meal plan for the next M days. On the i-th day, he is going to choose a pasta noodle of length exactly B_i and eat it. If no such noodle is available on any day, his plan fails. Additionally, he cannot eat the same noodle on multiple days.

Can Takahashi accomplish his meal plan?

Constraints

  • 1 \leq M \leq N \leq 1000
  • 1 \leq A_i \leq 10^9
  • 1 \leq B_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

If Takahashi can accomplish his meal plan, print Yes; otherwise, print No.


Sample Input 1

3 2
1 1 3
3 1

Sample Output 1

Yes

He can eat the 3-rd noodle on the 1-st day and the 1-st noodle on the 2-nd day, so his meal plan is feasible.


Sample Input 2

1 1
1000000000
1

Sample Output 2

No

A noodle of length exactly 1 is needed.


Sample Input 3

5 2
1 2 3 4 5
5 5

Sample Output 3

No

Since there are only 1 noodle of length 5, he cannot have a meal on the 2-nd day.

E - Cash Register

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

配点 : 300 点

問題文

高橋君は、レジ打ちの仕事をしています。

レジの機械には 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 の 11 個のボタンがあります。 レジの機械には、はじめ 0 が表示されています。 ボタン 00 を押すと、表示されている数が 100 倍されます。 それ以外のボタンを押すと、表示されている数が 10 倍されたあとに、押されたボタンに書かれている数が加算されます。

高橋君は、レジに整数 S を表示させたいです。 レジに S が表示されている状態にするためには、少なくとも何回ボタンを押す必要があるか求めてください。

制約

  • 1\leq S\leq 10^{100000}
  • S は整数

入力

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

S

出力

答えを 1 行で出力せよ。


入力例 1

40004

出力例 1

4

例えば、次のように操作することでボタンを 4 回押して 40004 を表示させることができます。 はじめ、レジには 0 が表示されています。

  • ボタン 4 を押す。レジに表示されている数は 4 となる。
  • ボタン 00 を押す。レジに表示されている数は 400 となる。
  • ボタン 0 を押す。レジに表示されている数は 4000 となる。
  • ボタン 4 を押す。レジに表示されている数は 40004 となる。

3 回までボタンを押すことでレジに 40004 を表示させることはできないので、出力すべき値は 4 です。


入力例 2

1355506027

出力例 2

10

入力例 3

10888869450418352160768000001

出力例 3

27

S は 64\operatorname{bit} 整数に収まらない場合があることに注意してください。

Score : 300 points

Problem Statement

Takahashi is a cashier.

There is a cash register with 11 keys: 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The cash register initially displays 0. Whenever he types the key 00, the displayed number is multiplied by 100; whenever he types one of the others, the displayed number is multiplied by 10, and then added by the number written on the key.

Takahashi wants the cash register to display an integer S. At least how many keystrokes are required to make it display S?

Constraints

  • 1\leq S\leq 10^{100000}
  • S is an integer.

Input

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

S

Output

Print the answer in a line.


Sample Input 1

40004

Sample Output 1

4

For example, the following four keystrokes make the cash register display 40004. Initially, the cash register displays 0.

  • Type the key 4. It now displays 4.
  • Type the key 00. It now displays 400.
  • Type the key 0. It now displays 4000.
  • Type the key 4. It now displays 40004.

He cannot make it display 40004 with three or fewer keystrokes, so 4 should be printed.


Sample Input 2

1355506027

Sample Output 2

10

Sample Input 3

10888869450418352160768000001

Sample Output 3

27

Note that S may not fit into a 64-\operatorname{bit} integer type.

F - Sowing Stones

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

配点 : 300 点

問題文

1 から N まで番号がつけられた N 個のマスが一列に並んでいます。最初、 M 個のマスに石が入っており、マス X_i には A_i 個 (1 \leq i \leq M) の石が入っています。

あなたは以下の操作を好きな回数( 0 回でもよい)行うことができます。

  • マス i (1 \leq i \leq N-1) に石があるとき、マス i から石を 1 つマス i+1 に移動させる。

N 個のマスすべてに石がちょうど 1 個ずつ入っている状態にするために必要な操作回数の最小値を求めてください。ただし、不可能な場合は -1 を出力してください。

制約

  • 2 \leq N \leq 2 \times 10^{9}
  • 1 \leq M \leq 2 \times 10^{5}
  • M \leq N
  • 1 \leq X_i \leq N (1 \leq i \leq M)
  • X_i \neq X_j (1 \leq i < j \leq M)
  • 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
  • 入力は全て整数

入力

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

N M
X_1 X_2 \ldots X_M
A_1 A_2 \ldots A_M

出力

答えを出力せよ。


入力例 1

5 2
1 4
3 2

出力例 1

4

以下の 4 回の操作で、5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることができます。

  • マス 1 の石を 1 つマス 2 に移動させる。
  • マス 2 の石を 1 つマス 3 に移動させる。
  • マス 4 の石を 1 つマス 5 に移動させる。
  • マス 1 の石を 1 つマス 2 に移動させる。

また、3 回以下の操作では 5 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、4 を出力します。


入力例 2

10 3
1 4 8
4 2 4

出力例 2

-1

どのように操作を行っても 10 個のマスすべてに石がちょうど 1 個ずつ入っている状態にすることはできません。よって、-1 を出力します。

Score : 300 points

Problem Statement

There are N cells numbered from 1 to N in a row. Initially, M cells contain stones, and cell X_i contains A_i stones (1 \leq i \leq M).

You can perform the following operation any number of times (possibly zero):

  • If cell i (1 \leq i \leq N-1) contains a stone, move one stone from cell i to cell i+1.

Find the minimum number of operations required to reach a state where each of the N cells contains exactly one stone. If it is impossible, print -1.

Constraints

  • 2 \leq N \leq 2 \times 10^{9}
  • 1 \leq M \leq 2 \times 10^{5}
  • M \leq N
  • 1 \leq X_i \leq N (1 \leq i \leq M)
  • X_i \neq X_j (1 \leq i < j \leq M)
  • 1 \leq A_i \leq 2 \times 10^{9} (1 \leq i \leq M)
  • All input values are integers.

Input

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

N M
X_1 X_2 \ldots X_M
A_1 A_2 \ldots A_M

Output

Print the answer.


Sample Input 1

5 2
1 4
3 2

Sample Output 1

4

You can reach a state where each of the five cells contains exactly one stone with four operations as follows:

  • Move one stone from cell 1 to cell 2.
  • Move one stone from cell 2 to cell 3.
  • Move one stone from cell 4 to cell 5.
  • Move one stone from cell 1 to cell 2.

It is impossible to achieve the goal in three or fewer operations. Therefore, print 4.


Sample Input 2

10 3
1 4 8
4 2 4

Sample Output 2

-1

No matter how you perform the operations, you cannot reach a state where all ten cells contain exactly one stone. Therefore, print -1.

G - Counting Ls

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

配点 : 400 点

問題文

N \times N のマス目が与えられます。このうち上から i 行目、左から j 列目のマスを (i,j) と書きます。
各マスの状態を表す N 個の長さ N の文字列 S_1,S_2,\dots,S_N が以下の形式で与えられます。

  • S_i の j 文字目が o であるとき、 (i,j) には o が書かれている。
  • S_i の j 文字目が x であるとき、 (i,j) には x が書かれている。

以下の条件を全て満たすマスの三つ組の個数を求めてください。

  • 組に含まれる 3 マスは相異なる。
  • 3 マス全てに o が書かれている。
  • マスのうち、丁度 2 つが同じ行にある。
  • マスのうち、丁度 2 つが同じ列にある。

但し、ふたつの三つ組は、丁度一方に含まれるマスが存在する場合のみ区別します。

制約

  • N は 2 以上 2000 以下の整数
  • S_i は長さ N の o と x からなる文字列

入力

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

N
S_1
S_2
\vdots
S_N

出力

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


入力例 1

3
ooo
oxx
xxo

出力例 1

4

以下の 4 つの三つ組が条件を満たします。

  • (1,1),(1,2),(2,1)
  • (1,1),(1,3),(2,1)
  • (1,1),(1,3),(3,3)
  • (1,2),(1,3),(3,3)

入力例 2

4
oxxx
xoxx
xxox
xxxo

出力例 2

0

入力例 3

15
xooxxooooxxxoox
oxxoxoxxxoxoxxo
oxxoxoxxxoxoxxx
ooooxooooxxoxxx
oxxoxoxxxoxoxxx
oxxoxoxxxoxoxxo
oxxoxooooxxxoox
xxxxxxxxxxxxxxx
xooxxxooxxxooox
oxxoxoxxoxoxxxo
xxxoxxxxoxoxxoo
xooxxxooxxoxoxo
xxxoxxxxoxooxxo
oxxoxoxxoxoxxxo
xooxxxooxxxooox

出力例 3

2960

Score : 400 points

Problem Statement

You are given an N \times N grid. Let (i,j) denote the cell in the i-th row from the top and the j-th column from the left.
The states of the cells are given by N strings of length N, S_1, S_2, \dots, S_N, in the following format:

  • If the j-th character of S_i is o, there is an o written in cell (i,j).
  • If the j-th character of S_i is x, there is an x written in cell (i,j).

Find the number of triples of cells that satisfy all of the following conditions:

  • The three cells in the triple are distinct.
  • All three cells have an o written in them.
  • Exactly two of the cells are in the same row.
  • Exactly two of the cells are in the same column.

Here, two triples are considered different if and only if some cell is contained in exactly one of the triples.

Constraints

  • N is an integer between 2 and 2000, inclusive.
  • S_i is a string of length N consisting of o and x.

Input

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

N
S_1
S_2
\vdots
S_N

Output

Print the answer as an integer.


Sample Input 1

3
ooo
oxx
xxo

Sample Output 1

4

The following four triples satisfy the conditions:

  • (1,1),(1,2),(2,1)
  • (1,1),(1,3),(2,1)
  • (1,1),(1,3),(3,3)
  • (1,2),(1,3),(3,3)

Sample Input 2

4
oxxx
xoxx
xxox
xxxo

Sample Output 2

0

Sample Input 3

15
xooxxooooxxxoox
oxxoxoxxxoxoxxo
oxxoxoxxxoxoxxx
ooooxooooxxoxxx
oxxoxoxxxoxoxxx
oxxoxoxxxoxoxxo
oxxoxooooxxxoox
xxxxxxxxxxxxxxx
xooxxxooxxxooox
oxxoxoxxoxoxxxo
xxxoxxxxoxoxxoo
xooxxxooxxoxoxo
xxxoxxxxoxooxxo
oxxoxoxxoxoxxxo
xooxxxooxxxooox

Sample Output 3

2960
H - Sensor Optimization Dilemma 2

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

配点 : 475 点

問題文

ある製品の製造には 1,2,\dots,N の番号が付いた N 個の工程が必要です。

各工程 i について、それを処理する 2 種類の機械 S_i,T_i が売られています。

  • 機械 S_i : 1 台につき 1 日あたり製品 A_i 個分の処理ができ、 1 台 P_i 円で導入できる
  • 機械 T_i : 1 台につき 1 日あたり製品 B_i 個分の処理ができ、 1 台 Q_i 円で導入できる

それぞれの機械は 0 台以上何台でも導入できます。

機械の導入の結果、工程 i を 1 日あたり製品 W_i 個分処理できるようになったとします。
このとき、製造能力を W の最小値、すなわち \displaystyle \min^{N}_{i=1} W_i と定義します。

全体の予算が X 円のとき、達成可能な製造能力の最大値を求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 100
  • 1 \le A_i,B_i \le 100
  • 1 \le P_i,Q_i,X \le 10^7

入力

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

N X
A_1 P_1 B_1 Q_1
A_2 P_2 B_2 Q_2
\vdots
A_N P_N B_N Q_N

出力

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


入力例 1

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

出力例 1

4

例えば、次の通り機械を導入することで製造能力を 4 にすることができ、これが達成可能な最大値です。

  • 工程 1 に対し機械 S_1 を 2 台導入する。
    • 1 日あたり製品 4 個分の処理に相当し、導入に合計 10 円かかる。
  • 工程 2 に対し機械 S_2 を 1 台導入する。
    • 1 日あたり製品 1 個分の処理に相当し、導入に合計 1 円かかる。
  • 工程 2 に対し機械 T_2 を 1 台導入する。
    • 1 日あたり製品 3 個分の処理に相当し、導入に合計 3 円かかる。
  • 工程 3 に対し機械 T_3 を 2 台導入する。
    • 1 日あたり製品 4 個分の処理に相当し、導入に合計 8 円かかる。

入力例 2

1 10000000
100 1 100 1

出力例 2

1000000000

入力例 3

1 1
1 10000000 1 10000000

出力例 3

0

正の製造能力が得られない場合もあります。


入力例 4

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

出力例 4

894742

Score : 475 points

Problem Statement

The manufacturing of a certain product requires N processes numbered 1,2,\dots,N.

For each process i, there are two types of machines S_i and T_i available for purchase to handle it.

  • Machine S_i: Can process A_i products per day per unit, and costs P_i yen per unit.
  • Machine T_i: Can process B_i products per day per unit, and costs Q_i yen per unit.

You can purchase any number of each machine, possibly zero.

Suppose that process i can handle W_i products per day as a result of introducing machines.
Here, we define the production capacity as the minimum of W, that is, \displaystyle \min^{N}_{i=1} W_i.

Given a total budget of X yen, find the maximum achievable production capacity.

Constraints

  • All input values are integers.
  • 1 \le N \le 100
  • 1 \le A_i,B_i \le 100
  • 1 \le P_i,Q_i,X \le 10^7

Input

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

N X
A_1 P_1 B_1 Q_1
A_2 P_2 B_2 Q_2
\vdots
A_N P_N B_N Q_N

Output

Print the answer as an integer.


Sample Input 1

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

Sample Output 1

4

For example, by introducing machines as follows, we can achieve a production capacity of 4, which is the maximum possible.

  • For process 1, introduce 2 units of machine S_1.
    • This allows processing 4 products per day and costs a total of 10 yen.
  • For process 2, introduce 1 unit of machine S_2.
    • This allows processing 1 product per day and costs a total of 1 yen.
  • For process 2, introduce 1 unit of machine T_2.
    • This allows processing 3 products per day and costs a total of 3 yen.
  • For process 3, introduce 2 units of machine T_3.
    • This allows processing 4 products per day and costs a total of 8 yen.

Sample Input 2

1 10000000
100 1 100 1

Sample Output 2

1000000000

Sample Input 3

1 1
1 10000000 1 10000000

Sample Output 3

0

There may be cases where a positive production capacity cannot be achieved.


Sample Input 4

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

Sample Output 4

894742
I - Colored Ball

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

配点 : 500 点

問題文

1, 2, \ldots, N の番号がついた N 個の箱があり、はじめ箱 i には色 C_i のボールが 1 つ入っています。

Q 個のクエリが与えられるので、これらを順に処理してください。

各クエリは整数の組 (a,b) によって与えられ、その内容は以下の通りです。

  • 箱 a のボールをすべて箱 b に移し、その後箱 b に何種類の色のボールが入っているかを出力する。

ただし、箱 a や箱 b が空の場合もあることに注意してください。

制約

  • 1 \leq N, Q \leq 200000
  • 1 \leq C_i \leq N
  • 1 \leq a, b \leq N
  • a \neq b
  • 入力される数値はすべて整数

入力

入力は以下の形式で標準入力から与えられる。ここで、 \text{query}_i は i 番目のクエリを意味する。

N Q
C_1 C_2 \ldots C_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

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

a b

出力

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


入力例 1

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

出力例 1

1
2
1
1
3
  • 1 番目のクエリでは、箱 1 のボールをすべて箱 2 に移します。箱 2 には色 1 のボールが 2 つ入っている状態となるため、1 を出力します。

  • 2 番目のクエリでは、箱 6 のボールをすべて箱 4 に移します。箱 4 には色 2 のボールが 1 つ、色 3 のボールが 1 つ入っている状態となるため、2 を出力します。

  • 3 番目のクエリでは、箱 5 のボールをすべて箱 1 に移します。箱 1 には色 2 のボールが 1 つ入っている状態となるため、1 を出力します。

  • 4 番目のクエリでは、箱 3 のボールをすべて箱 6 に移します。箱 6 には色 1 のボールが 1 つ入っている状態となるため、1 を出力します。

  • 5 番目のクエリでは、箱 4 のボールをすべて箱 6 に移します。箱 6 には色 1 のボールが 1 つ、色 2 のボールが 1 つ、色 3 のボールが 1 つ入っている状態となるため、3 を出力します。


入力例 2

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

出力例 2

1
2
0

Score : 500 points

Problem Statement

There are N boxes numbered 1, 2, \ldots, N. Initially, box i contains one ball of color C_i.

You are given Q queries, which you should process in order.

Each query is given by a pair of integers (a,b) and asks you to do the following:

  • Move all the balls from box a to box b, and then print the number of different colors of balls in box b.

Here, the boxes a and b may be empty.

Constraints

  • 1 \leq N, Q \leq 200000
  • 1 \leq C_i \leq N
  • 1 \leq a, b \leq N
  • a \neq b
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where \text{query}_i represents the i-th query:

N Q
C_1 C_2 \ldots C_N
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Each query is given in the following format:

a b

Output

Print Q lines. The i-th line should contain the response to the i-th query.


Sample Input 1

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

Sample Output 1

1
2
1
1
3
  • For the first query, move all the balls from box 1 to box 2. Box 2 now contains two balls of color 1, so print 1.

  • For the second query, move all the balls from box 6 to box 4. Box 4 now contains one ball of color 2 and one ball of color 3, so print 2.

  • For the third query, move all the balls from box 5 to box 1. Box 1 now contains one ball of color 2, so print 1.

  • For the fourth query, move all the balls from box 3 to box 6. Box 6 now contains one ball of color 1, so print 1.

  • For the fifth query, move all the balls from box 4 to box 6. Box 6 now contains one ball of color 1, one ball of color 2, and one ball of color 3, so print 3.


Sample Input 2

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

Sample Output 2

1
2
0