A - Shift

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

長さ N の数列 A = (A_1, A_2, \dots, A_N) が与えられます。
あなたは次の操作をちょうど K 回行います。

  • A の先頭の要素を取り除き、A の末尾に 0 を挿入する。

操作を行った後の A の要素をすべて出力してください。

制約

  • 1 \leq N \leq 100
  • 1 \leq K \leq 100
  • 1 \leq A_i \leq 100
  • 入力される値はすべて整数

入力

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

N K
A_1 A_2 \dots A_N

出力

操作を行った後の A の要素を空白区切りで 1 行に出力せよ。


入力例 1

3 2
2 7 8

出力例 1

8 0 0

操作を行う前は A = (2, 7, 8) です。
操作を 1 回行った時点では A = (7, 8, 0) です。
操作を 2 回行った時点では A = (8, 0, 0) です。
よって (8, 0, 0) が答えです。


入力例 2

3 4
9 9 9

出力例 2

0 0 0

入力例 3

9 5
1 2 3 4 5 6 7 8 9

出力例 3

6 7 8 9 0 0 0 0 0

Score : 100 points

Problem Statement

You are given a sequence A = (A_1, A_2, \dots, A_N) of length N.
You perform the following operation exactly K times:

  • remove the initial element of A and append a 0 to the tail of A.

Print all the elements of A after the operations.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq K \leq 100
  • 1 \leq A_i \leq 100
  • All values in the input are integers.

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the elements of A after the operations in one line, separated by spaces.


Sample Input 1

3 2
2 7 8

Sample Output 1

8 0 0

Before the operations, A = (2, 7, 8).
After performing the operation once, A = (7, 8, 0).
After performing the operation twice, A = (8, 0, 0).
Thus, (8, 0, 0) is the answer.


Sample Input 2

3 4
9 9 9

Sample Output 2

0 0 0

Sample Input 3

9 5
1 2 3 4 5 6 7 8 9

Sample Output 3

6 7 8 9 0 0 0 0 0
B - Potions

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100

問題文

ナオヒロ君はモンスターを飼っています。モンスターの現在の体力は H です。
また、ナオヒロ君は N 種類の傷薬を持っています。傷薬は効き目の弱い順に 1 から N までの番号がついています。
傷薬 n をモンスターに与えると、モンスターの体力が P_n 増加します。ここで、P_1 \lt P_2 \lt \dots \lt P_N が成り立ちます。

ナオヒロ君は傷薬を 1 つモンスターに与えることで、モンスターの体力を X 以上にしたいです。
目標を達成できる傷薬のうち最も効き目の弱いものの番号を出力してください。(制約下においてそのような傷薬が存在することが保証されています。)

制約

  • 2 \leq N \leq 100
  • 1 \leq H \lt X \leq 999
  • 1 \leq P_1 \lt P_2 \lt \dots \lt P_N = 999
  • 入力される値はすべて整数

入力

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

N H X
P_1 P_2 \dots P_N

出力

目標を達成できる傷薬のうち最も効き目の弱いものの番号を出力せよ。


入力例 1

3 100 200
50 200 999

出力例 1

2

それぞれの傷薬をモンスターに 1 つ与えたときのモンスターの体力の変化は以下の通りです。

  • 傷薬 1 をモンスターに与えるとモンスターの体力は 100 + 50 = 150 になります。
  • 傷薬 2 をモンスターに与えるとモンスターの体力は 100 + 200 = 300 になります。
  • 傷薬 3 をモンスターに与えるとモンスターの体力は 100 + 999 = 1099 になります。

与えた後に体力が X = 200 以上になっている傷薬は、傷薬 2 と傷薬 3 です。このうち最も効き目の弱い傷薬である傷薬 2 が答えになります。


入力例 2

2 10 21
10 999

出力例 2

2

入力例 3

10 500 999
38 420 490 585 613 614 760 926 945 999

出力例 3

4

Score : 100 points

Problem Statement

Naohiro has a monster. The monster's current health is H.
He also has N kinds of potions, numbered from 1 to N in ascending order of effectiveness.
If you give the monster potion n, its health will increase by P_n. Here, P_1 \lt P_2 \lt \dots \lt P_N.

He wants to increase the monster's health to X or above by giving it one of the potions.
Print the number of the least effective potion that can achieve the purpose. (The constraints guarantee that such a potion exists.)

Constraints

  • 2 \leq N \leq 100
  • 1 \leq H \lt X \leq 999
  • 1 \leq P_1 \lt P_2 \lt \dots \lt P_N = 999
  • All input values are integers.

Input

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

N H X
P_1 P_2 \dots P_N

Output

Print the number of the least effective potion that can achieve the purpose.


Sample Input 1

3 100 200
50 200 999

Sample Output 1

2

Below is the change in the monster's health when one of the potions is given to the monster.

  • If potion 1 is given, the monster's health becomes 100 + 50 = 150.
  • If potion 2 is given, the monster's health becomes 100 + 200 = 300.
  • If potion 3 is given, the monster's health becomes 100 + 999 = 1099.

The potions that increase the monster's health to at least X = 200 are potions 2 and 3. The answer is the least effective of them, which is potion 2.


Sample Input 2

2 10 21
10 999

Sample Output 2

2

Sample Input 3

10 500 999
38 420 490 585 613 614 760 926 945 999

Sample Output 3

4
C - Nice Grid

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

次の図に示す、各マスが黒または白に塗られた縦 15\times15 列のグリッドにおいて、 上から R 行目、左から C 列目のマスが何色かを出力して下さい。

制約

  • 1 \leq R, C \leq 15
  • R, C は整数

入力

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

R C

出力

図のグリッドにおいて上から R 行目、左から C 列目のマスが黒色の場合は black と、白色の場合は white と出力せよ。 ジャッジは英小文字と英大文字を厳密に区別することに注意せよ。


入力例 1

3 5

出力例 1

black

図のグリッドにおいて上から 3 行目、左から 5 列目のマスは黒色です。 よって、black と出力します。


入力例 2

4 5

出力例 2

white

図のグリッドにおいて上から 4 行目、左から 5 列目のマスは白色です。 よって、white と出力します。

Score : 200 points

Problem Statement

Print the color of the cell at the R-th row from the top and C-th column from the left in the following grid with 15 vertical rows and 15 horizontal columns.

Constraints

  • 1 \leq R, C \leq 15
  • R and C are integers.

Input

Input is given from Standard Input in the following format:

R C

Output

In the grid above, if the color of the cell at the R-th row from the top and C-th column from the left is black, then print black; if the cell is white, then print white. Note that the judge is case-sensitive.


Sample Input 1

3 5

Sample Output 1

black

In the grid above, the cell at the 3-rd row from the top and 5-th column from the left is black. Thus, black should be printed.


Sample Input 2

4 5

Sample Output 2

white

In the grid above, the cell at the 4-th row from the top and 5-th column from the left is white. Thus, white should be printed.

D - Dentist Aoki

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200

問題文

高橋君には、穴 1,2,\dots,N1 本ずつ、全部で N 本の歯が生えています。
歯医者の青木君は、これらの歯と穴に対して、 Q 回の治療を行います。
i 回目の治療では、穴 T_i を治療します。治療内容は次の通りです。

  • T_i に歯が生えている場合、穴 T_i から歯を抜く。
  • そうでない ( 穴 T_i に歯が生えていない) 場合、穴 T_i に歯を生やす。

全ての治療が終わった後、高橋君に生えている歯の本数は何本ですか?

制約

  • 入力は全て整数
  • 1 \le N,Q \le 1000
  • 1 \le T_i \le N

入力

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

N Q
T_1 T_2 \dots T_Q

出力

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


入力例 1

30 6
2 9 18 27 18 9

出力例 1

28

高橋君には最初 30 本の歯が生えており、青木君は 6 回の治療を行います。

  • 1 回目の治療では穴 2 を治療します。 穴 2 に歯が生えているため、歯を抜きます。
  • 2 回目の治療では穴 9 を治療します。 穴 9 に歯が生えているため、歯を抜きます。
  • 3 回目の治療では穴 18 を治療します。 穴 18 に歯が生えているため、歯を抜きます。
  • 4 回目の治療では穴 27 を治療します。 穴 27 に歯が生えているため、歯を抜きます。
  • 5 回目の治療では穴 18 を治療します。 穴 18 に歯が生えていないため、歯を生やします。
  • 6 回目の治療では穴 9 を治療します。 穴 9 に歯が生えていないため、歯を生やします。

最終的な歯の本数は 28 本です。


入力例 2

1 7
1 1 1 1 1 1 1

出力例 2

0

入力例 3

9 20
9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8

出力例 3

5

Score: 200 points

Problem Statement

Takahashi has N teeth, one in each of the holes numbered 1, 2, \dots, N.
Dentist Aoki will perform Q treatments on these teeth and holes.
In the i-th treatment, hole T_i is treated as follows:

  • If there is a tooth in hole T_i, remove the tooth from hole T_i.
  • If there is no tooth in hole T_i (i.e., the hole is empty), grow a tooth in hole T_i.

After all treatments are completed, how many teeth does Takahashi have?

Constraints

  • All input values are integers.
  • 1 \le N, Q \le 1000
  • 1 \le T_i \le N

Input

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

N Q
T_1 T_2 \dots T_Q

Output

Print the number of teeth as an integer.


Sample Input 1

30 6
2 9 18 27 18 9

Sample Output 1

28

Initially, Takahashi has 30 teeth, and Aoki performs six treatments.

  • In the first treatment, hole 2 is treated. There is a tooth in hole 2, so it is removed.
  • In the second treatment, hole 9 is treated. There is a tooth in hole 9, so it is removed.
  • In the third treatment, hole 18 is treated. There is a tooth in hole 18, so it is removed.
  • In the fourth treatment, hole 27 is treated. There is a tooth in hole 27, so it is removed.
  • In the fifth treatment, hole 18 is treated. There is no tooth in hole 18, so a tooth is grown.
  • In the sixth treatment, hole 9 is treated. There is no tooth in hole 9, so a tooth is grown.

The final count of teeth is 28.


Sample Input 2

1 7
1 1 1 1 1 1 1

Sample Output 2

0

Sample Input 3

9 20
9 5 1 2 2 2 8 9 2 1 6 2 6 5 8 7 8 5 9 8

Sample Output 3

5
E - Candy Tribulation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 350

問題文

あなたは小さな飴と大きな飴の 2 種類の飴を無尽蔵に持っています。 小さな飴の重量は X グラム、大きな飴の重量は Y グラムであり、小さな飴よりも大きな飴のほうが重い (すなわち、X < Y) です。

N 人の子供がいます。子供たちには 1 から N までの番号が付けられています。

あなたは、以下の条件を満たすように飴を配ることにしました。

  • i=1,\dots,N について、子供 i には 2 種類の飴を合計でちょうど A_i 個配る。
  • N 人の子供それぞれに配る飴の総重量はすべて等しい。

条件を満たす配り方が存在するかを判定してください。存在する場合はそのような配り方における、大きな飴を配る個数としてあり得る最大値を求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X < Y \leq 10^9
  • 入力される値はすべて整数

入力

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

N X Y
A_1 \dots A_N

出力

条件を満たす配り方が存在しない場合、-1 を出力せよ。

条件を満たす配り方が存在する場合、そのような配り方における、大きな飴を配る個数としてあり得る最大値を出力せよ。


入力例 1

3 6 8
11 10 13

出力例 1

18

以下のように飴を配ることで、それぞれの子供に配る飴の総重量がすべて等しくなります。

  • 子供 1 には、小さな飴を 4 個、大きな飴を 7 個配る。総重量は 6 \times 4 + 8 \times 7 = 80 グラム。
  • 子供 2 には、小さな飴を 0 個、大きな飴を 10 個配る。総重量は 6 \times 0 + 8 \times 10 = 80 グラム。
  • 子供 3 には、小さな飴を 12 個、大きな飴を 1 個配る。総重量は 6 \times 12 + 8 \times 1 = 80 グラム。

この配り方では、大きな飴を合計 18 個配っています。

条件を満たす配り方で、18 個より多く大きな飴を配る方法は存在しません。よって、答えは 18 です。


入力例 2

2 3 4
3 5

出力例 2

-1

条件を満たす配り方は存在しません。


入力例 3

8 4 32
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

出力例 3

8000000000

答えは 32bit 整数に収まらないことがあります。

Score : 350 points

Problem Statement

You have an unlimited supply of two types of candies: small candies and large candies. The weight of a small candy is X grams, and the weight of a large candy is Y grams. Large candies are heavier than small candies (that is, X < Y).

There are N children, numbered 1 to N.

You have decided to distribute candies so that the following conditions are satisfied:

  • For i=1,\dots,N, child i receives exactly A_i candies in total of the two types.
  • The total weights of candies distributed to the N children are all equal.

Determine whether there exists a distribution method that satisfies the conditions. If it exists, find the maximum possible value for the number of large candies distributed.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 10^9
  • 1 \leq X < Y \leq 10^9
  • All input values are integers.

Input

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

N X Y
A_1 \dots A_N

Output

If there is no distribution method that satisfies the conditions, output -1.

If there exists a distribution method that satisfies the conditions, output the maximum possible value for the number of large candies distributed in such a distribution method.


Sample Input 1

3 6 8
11 10 13

Sample Output 1

18

You can distribute candies as follows so that the total weights of candies distributed to the children are all equal.

  • Child 1 receives 4 small candies and 7 large candies. The total weight is 6 \times 4 + 8 \times 7 = 80 grams.
  • Child 2 receives 0 small candies and 10 large candies. The total weight is 6 \times 0 + 8 \times 10 = 80 grams.
  • Child 3 receives 12 small candies and 1 large candy. The total weight is 6 \times 12 + 8 \times 1 = 80 grams.

In this distribution method, a total of 18 large candies are distributed.

There is no distribution method that satisfies the conditions and distributes more than 18 large candies. Therefore, the answer is 18.


Sample Input 2

2 3 4
3 5

Sample Output 2

-1

There is no distribution method that satisfies the conditions.


Sample Input 3

8 4 32
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

8000000000

The answer may not fit in a 32-bit integer.

F - Sum of Product

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

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

\displaystyle \sum_{1\leq i< j\leq N} A_iA_j の値を求めてください。

制約

  • 2\leq N \leq 3\times 10^5
  • 1\leq A_i \leq 10^4
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

3
4 2 3

出力例 1

26

\displaystyle \sum_{1\leq i< j\leq N} A_iA_j=A_1A_2+A_1A_3+A_2A_3=4\cdot 2+4\cdot 3+2\cdot 3=26 です。


入力例 2

2
9 45

出力例 2

405

入力例 3

10
7781 8803 8630 9065 8831 9182 8593 7660 7548 8617

出力例 3

3227530139

Score : 300 points

Problem Statement

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

Compute the value of \displaystyle \sum_{1\leq i< j\leq N} A_iA_j.

Constraints

  • 2 \le N \le 3 \times 10^5
  • 1 \le A_i \le 10^4
  • 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

Output the answer.


Sample Input 1

3
4 2 3

Sample Output 1

26

We have \displaystyle \sum_{1\leq i< j\leq N} A_iA_j=A_1A_2+A_1A_3+A_2A_3=4\cdot 2+4\cdot 3+2\cdot 3=26.


Sample Input 2

2
9 45

Sample Output 2

405

Sample Input 3

10
7781 8803 8630 9065 8831 9182 8593 7660 7548 8617

Sample Output 3

3227530139
G - Sum of Differences

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

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

\displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} |A_i - B_j| の値を 998244353 で割ったあまりを求めてください。

制約

  • 1 \leq N,M \leq 3 \times 10^5
  • 1 \leq A_i, B_j < 998244353
  • 入力される値はすべて整数

入力

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

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

出力

答えを 1 行に出力せよ。


入力例 1

4 2
1 6 9 2
3 1

出力例 1

26

答えは |1-3| + |1-1| + |6-3| + |6-1| + |9-3| + |9-1| + |2-3| + |2-1| = 2+0+3+5+6+8+1+1 = 26 です。


入力例 2

8 8
185991676 311812083 311812083 84357963 185991676 185991676 724020528 369175631
455049197 387671868 4361724 724020528 724020528 455049197 455049197 724020528

出力例 2

529117255

Score : 400 points

Problem Statement

You are given a length-N sequence of positive integers A = (A_1, A_2, \dots, A_N) and a length-M sequence of positive integers B = (B_1, B_2, \dots, B_M).

Find the value of \displaystyle \sum_{i=1}^{N} \sum_{j=1}^{M} |A_i - B_j|, modulo 998244353.

Constraints

  • 1 \leq N,M \leq 3 \times 10^5
  • 1 \leq A_i, B_j < 998244353
  • All input values are integers.

Input

The 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

Output the answer in one line.


Sample Input 1

4 2
1 6 9 2
3 1

Sample Output 1

26

The answer is |1-3| + |1-1| + |6-3| + |6-1| + |9-3| + |9-1| + |2-3| + |2-1| = 2+0+3+5+6+8+1+1 = 26.


Sample Input 2

8 8
185991676 311812083 311812083 84357963 185991676 185991676 724020528 369175631
455049197 387671868 4361724 724020528 724020528 455049197 455049197 724020528

Sample Output 2

529117255
H - Shift String

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450

問題文

0, 1 からなる長さの等しい文字列 A,B が与えられます。

A に対して以下の操作を 0 回以上何度でも行うことができます。

  • A の先頭の文字を末尾に移動させる。

A=B とするために必要な最小の操作回数を求めてください。
但し、どのように操作しても A=B とできない場合、代わりに -1 と出力してください。

T 個のテストケースが与えられるので、それぞれについて答えを求めてください。

制約

  • 1 \le T \le 10000
  • A,B0, 1 からなる文字列
  • 2 \le |A|=|B| \le 10^6
  • ひとつの入力について、 |A| の総和は 10^6 を超えない

入力

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

各テストケースは以下の形式で与えられる。

A
B

出力

T 行出力せよ。

i 行目には i 番目のテストケースについて、答えを出力せよ。


入力例 1

5
1010001
1000110
000
111
01010
01010
0101
0011
100001101110000001010110110001
101100011000011011100000010101

出力例 1

2
-1
0
-1
22

この入力には 5 個のテストケースが含まれます。

  • 1 番目のテストケースについて、 A= 1010001B= 1000110 です。
    • A に操作を 2 回行うと A1010001 \rightarrow 0100011 \rightarrow 1000110 となり、 A=B とできます。
  • 2 番目のテストケースについて、どのように操作を行っても 000111 にすることはできません。
  • 3 番目のテストケースについて、はじめから A=B です。

Score : 450 points

Problem Statement

You are given strings A and B of equal length consisting of 0 and 1.

You can perform the following operation on A zero or more times.

  • Move the first character of A to the end.

Find the minimum number of operations required to make A=B.
If it is impossible to make A=B no matter how you operate, print -1 instead.

You are given T test cases; find the answer for each of them.

Constraints

  • 1 \le T \le 10000
  • A and B are strings consisting of 0 and 1.
  • 2 \le |A|=|B| \le 10^6
  • For a single input, the sum of |A| does not exceed 10^6.

Input

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

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

Each test case is given in the following format:

A
B

Output

Print T lines.

The i-th line should contain the answer for the i-th test case.


Sample Input 1

5
1010001
1000110
000
111
01010
01010
0101
0011
100001101110000001010110110001
101100011000011011100000010101

Sample Output 1

2
-1
0
-1
22

This input contains five test cases.

  • For the first test case, A= 1010001 and B= 1000110.
    • By performing the operation on A twice, A becomes 1010001 \rightarrow 0100011 \rightarrow 1000110, which makes A=B.
  • For the second test case, no matter how you perform the operation, you cannot change 000 to 111.
  • For the third test case, A=B from the beginning.
I - Simultaneous Swap

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

長さ N の数列 A=(A_1,A_2,\ldots,A_N), B=(B_1,B_2,\ldots,B_N) が与えられます。

高橋君は次の操作を好きなだけ (0 回でも良い) 繰り返す事ができます。

1 以上 N 以下の、どの 2 つも互いに相異なる 3 つの整数 i,j,k を選ぶ。
Ai 番目の要素と j 番目の要素を交換し、Bi 番目の要素と k 番目の要素を交換する。

高橋君がうまく操作を繰り返すことによって、 AB を一致させる事が可能ならば Yes を、不可能ならば No を出力してください。
ただし、AB が一致しているとは、任意の 1\leq i\leq N について Ai 番目の要素と Bi 番目の要素が等しいことを言います。

制約

  • 3 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i\leq N
  • 入力はすべて整数

入力

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

出力

操作を繰り返すことによって、高橋君が AB を一致させる事が可能ならば Yes を、不可能ならば No を出力せよ。


入力例 1

3
1 2 1
1 1 2

出力例 1

Yes

(i,j,k)=(1,2,3) として 1 回操作を行うことで、A_1A_2B_1B_3 がそれぞれ交換され、
A,B はともに (2,1,1) となって一致します。よって、Yes を出力します。


入力例 2

3
1 2 2
1 1 2

出力例 2

No

どのように操作を行っても AB を一致させることはできません。よって、No を出力します。


入力例 3

5
1 2 3 2 1
3 2 2 1 1

出力例 3

Yes

入力例 4

8
1 2 3 4 5 6 7 8
7 8 5 6 4 3 1 2

出力例 4

No

Score : 500 points

Problem Statement

You are given two sequences of N numbers: A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).

Takahashi can repeat the following operation any number of times (possibly zero).

Choose three pairwise distinct integers i, j, and k between 1 and N.
Swap the i-th and j-th elements of A, and swap the i-th and k-th elements of B.

If there is a way for Takahashi to repeat the operation to make A and B equal, print Yes; otherwise, print No.
Here, A and B are said to be equal when, for every 1\leq i\leq N, the i-th element of A and that of B are equal.

Constraints

  • 3 \leq N \leq 2\times 10^5
  • 1\leq A_i,B_i\leq N
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_N

Output

Print Yes if there is a way for Takahashi to repeat the operation to make A and B equal, and print No otherwise.


Sample Input 1

3
1 2 1
1 1 2

Sample Output 1

Yes

Performing the operation once with (i,j,k)=(1,2,3) swaps A_1 and A_2, and swaps B_1 and B_3,
making both A and B equal to (2,1,1). Thus, you should print Yes.


Sample Input 2

3
1 2 2
1 1 2

Sample Output 2

No

There is no way to perform the operation to make A and B equal, so you should print No.


Sample Input 3

5
1 2 3 2 1
3 2 2 1 1

Sample Output 3

Yes

Sample Input 4

8
1 2 3 4 5 6 7 8
7 8 5 6 4 3 1 2

Sample Output 4

No