A - Fizz

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

正整数 N が与えられます。

N 行出力してください。i 行目 (1\le i\le N) には、i が 3 の倍数ならば Fizz を、3 の倍数でないならば i を出力してください。

制約

  • N は 1 以上 100 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

1
2
Fizz
4

1 行目では 1 は 3 の倍数ではないので 1 を出力します。

2 行目では 2 は 3 の倍数ではないので 2 を出力します。

3 行目では 3 は 3 の倍数なので Fizz を出力します。

4 行目では 4 は 3 の倍数ではないので 4 を出力します。


入力例 2

10

出力例 2

1
2
Fizz
4
5
Fizz
7
8
Fizz
10

Score : 100 points

Problem Statement

You are given a positive integer N.

Output N lines. The i-th line (1\le i\le N) should contain Fizz if i is a multiple of 3, and i if i is not a multiple of 3.

Constraints

  • N is an integer between 1 and 100, inclusive.

Input

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

N

Output

Output the answer.


Sample Input 1

4

Sample Output 1

1
2
Fizz
4

The first line should contain 1 since 1 is not a multiple of 3.

The second line should contain 2 since 2 is not a multiple of 3.

The third line should contain Fizz since 3 is a multiple of 3.

The fourth line should contain 4 since 4 is not a multiple of 3.


Sample Input 2

10

Sample Output 2

1
2
Fizz
4
5
Fizz
7
8
Fizz
10
B - 2^n - 2*n

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

整数 N が与えられるので、 2^N-2N の値を計算して出力してください。

制約

  • N は 1 以上 11 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

1

出力例 1

0

この入力で与えられる N の値は 1 なので、 2^1 - 2 \times 1 の値である 0 を出力してください。


入力例 2

2

出力例 2

0

この入力で与えられる N の値は 2 なので、 2^2 - 2 \times 2 の値である 0 を出力してください。


入力例 3

11

出力例 3

2026

この入力で与えられる N の値は 11 なので、 2^{11} - 2 \times 11 の値である 2026 を出力してください。

Score : 100 points

Problem Statement

You are given an integer N. Compute and output the value of 2^N-2N.

Constraints

  • N is an integer between 1 and 11, inclusive.

Input

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

N

Output

Output the answer.


Sample Input 1

1

Sample Output 1

0

The value of N given in this input is 1, so output the value of 2^1 - 2 \times 1, which is 0.


Sample Input 2

2

Sample Output 2

0

The value of N given in this input is 2, so output the value of 2^2 - 2 \times 2, which is 0.


Sample Input 3

11

Sample Output 3

2026

The value of N given in this input is 11, so output the value of 2^{11} - 2 \times 11, which is 2026.

C - Langton's Takahashi

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 250 点

問題文

H 行 W 列のグリッドがあり、はじめすべてのマスが白で塗られています。グリッドの上から i 行目、左から j 列目のマスを (i, j) と表記します。

このグリッドはトーラス状であるとみなします。すなわち、各 1 \leq i \leq H に対して (i, W) の右に (i, 1) があり、各 1 \leq j \leq W に対して (H, j) の下に (1, j) があるとします。

高橋君が (1, 1) にいて上を向いています。高橋君が以下の操作を N 回繰り返した後のグリッドの各マスがどの色で塗られているか出力してください。

  • 現在いるマスが白で塗られている場合は、現在いるマスを黒に塗り替え、時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。そうでない場合は、現在いるマスを白に塗り替え、反時計回りに 90^\circ 回転し、向いている方向に 1 マス進む。

制約

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • 入力される数値はすべて整数

入力

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

H W N

出力

H 行出力せよ。i 行目には長さ W の文字列であって、(i, j) が白で塗られている場合は j 文字目が .、黒で塗られている場合は j 文字目が # であるものを出力せよ。


入力例 1

3 4 5

出力例 1

.#..
##..
....

グリッドの各マスは操作によって以下のように変化します。

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

入力例 2

2 2 1000

出力例 2

..
..

入力例 3

10 10 10

出力例 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#

Score: 250 points

Problem Statement

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

This grid is considered to be toroidal. That is, (i, 1) is to the right of (i, W) for each 1 \leq i \leq H, and (1, j) is below (H, j) for each 1 \leq j \leq W.

Takahashi is at (1, 1) and facing upwards. Print the color of each cell in the grid after Takahashi repeats the following operation N times.

  • If the current cell is painted white, repaint it black, rotate 90^\circ clockwise, and move forward one cell in the direction he is facing. Otherwise, repaint the current cell white, rotate 90^\circ counterclockwise, and move forward one cell in the direction he is facing.

Constraints

  • 1 \leq H, W \leq 100
  • 1 \leq N \leq 1000
  • All input values are integers.

Input

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

H W N

Output

Print H lines. The i-th line should contain a string of length W where the j-th character is . if the cell (i, j) is painted white, and # if it is painted black.


Sample Input 1

3 4 5

Sample Output 1

.#..
##..
....

The cells of the grid change as follows due to the operations:

....   #...   ##..   ##..   ##..   .#..
.... → .... → .... → .#.. → ##.. → ##..
....   ....   ....   ....   ....   ....

Sample Input 2

2 2 1000

Sample Output 2

..
..

Sample Input 3

10 10 10

Sample Output 3

##........
##........
..........
..........
..........
..........
..........
..........
..........
#........#
D - Counting Arrays

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

1 から N までの番号がついた N 個の数列が与えられます。
数列 i は、長さが L_i で j (1 \leq j \leq L_i) 番目の要素が a_{i,j} であるような数列です。

数列 i と 数列 j は、 L_i = L_j かつすべての k (1 \leq k \leq L_i) に対して a_{i,k} = a_{j,k} が成り立つ時に同じであるとみなします。
同じ数列は 1 種類として数えるとき、数列 1 から 数列 N の中に全部で何種類の数列がありますか?

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq 2 \times 10^5 (1 \leq i \leq N)
  • 0 \leq a_{i,j} \leq 10^{9} (1 \leq i \leq N, 1 \leq j \leq L_i)
  • すべての数列の要素の個数の和、すなわち \sum_{i=1}^N L_i は 2 \times 10^5 を超えない。
  • 入力はすべて整数である。

入力

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

N
L_1 a_{1,1} a_{1,2} \dots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \dots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \dots a_{N,L_N}

出力

数列の種類数を出力せよ。


入力例 1

4
2 1 2
2 1 1
2 2 1
2 1 2

出力例 1

3

入力例 1 で与えられている数列は以下の 4 個です。

  • 数列 1 : (1, 2)
  • 数列 2 : (1, 1)
  • 数列 3 : (2, 1)
  • 数列 4 : (1, 2)

このうち数列 1 と数列 4 は同じ数列で、それ以外は互いに異なる数列なので全部で 3 種類の数列があります。


入力例 2

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

出力例 2

4

入力例 2 で与えられている数列は以下の 5 個です。

  • 数列 1 : (1)
  • 数列 2 : (1)
  • 数列 3 : (2)
  • 数列 4 : (1, 1)
  • 数列 5 : (1, 1, 1)

入力例 3

1
1 1

出力例 3

1

Score : 200 points

Problem Statement

You are given N sequences numbered 1 to N.
Sequence i has a length of L_i and its j-th element (1 \leq j \leq L_i) is a_{i,j}.

Sequence i and Sequence j are considered the same when L_i = L_j and a_{i,k} = a_{j,k} for every k (1 \leq k \leq L_i).
How many different sequences are there among Sequence 1 through Sequence N?

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq L_i \leq 2 \times 10^5 (1 \leq i \leq N)
  • 0 \leq a_{i,j} \leq 10^{9} (1 \leq i \leq N, 1 \leq j \leq L_i)
  • The total number of elements in the sequences, \sum_{i=1}^N L_i, does not exceed 2 \times 10^5.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
L_1 a_{1,1} a_{1,2} \dots a_{1,L_1}
L_2 a_{2,1} a_{2,2} \dots a_{2,L_2}
\vdots
L_N a_{N,1} a_{N,2} \dots a_{N,L_N}

Output

Print the number of different sequences.


Sample Input 1

4
2 1 2
2 1 1
2 2 1
2 1 2

Sample Output 1

3

Sample Input 1 contains four sequences:

  • Sequence 1 : (1, 2)
  • Sequence 2 : (1, 1)
  • Sequence 3 : (2, 1)
  • Sequence 4 : (1, 2)

Except that Sequence 1 and Sequence 4 are the same, these sequences are pairwise different, so we have three different sequences.


Sample Input 2

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

Sample Output 2

4

Sample Input 2 contains five sequences:

  • Sequence 1 : (1)
  • Sequence 2 : (1)
  • Sequence 3 : (2)
  • Sequence 4 : (1, 1)
  • Sequence 5 : (1, 1, 1)

Sample Input 3

1
1 1

Sample Output 3

1
E - Repunit Trio

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

十進法ですべての桁の数字が 1 である整数をレピュニットと呼びます。レピュニットを小さい順に並べると 1,11,111,\ldots です。

ちょうど 3 つのレピュニットの和として表せる整数のうち N 番目に小さいものを求めてください。

制約

  • N は 1 以上 333 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

5

出力例 1

113

ちょうど 3 つのレピュニットの和として表せる整数を小さい順に並べると 3,13,23,33,113,\ldots です。例えば 113 は 113=1+1+111 と表せます。

3 つのレピュニットは相異ならなくてもよいことに注意してください。


入力例 2

19

出力例 2

2333

入力例 3

333

出力例 3

112222222233

Score : 300 points

Problem Statement

A repunit is an integer whose digits are all 1 in decimal representation. The repunits in ascending order are 1, 11, 111, \ldots.

Find the N-th smallest integer that can be expressed as the sum of exactly three repunits.

Constraints

  • N is an integer between 1 and 333, inclusive.

Input

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

N

Output

Print the answer.


Sample Input 1

5

Sample Output 1

113

The integers that can be expressed as the sum of exactly three repunits are 3, 13, 23, 33, 113, \ldots in ascending order. For example, 113 can be expressed as 113 = 1 + 1 + 111.

Note that the three repunits do not have to be distinct.


Sample Input 2

19

Sample Output 2

2333

Sample Input 3

333

Sample Output 3

112222222233
F - Sensors

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

H 行 W 列のマス目の上に 0 個以上のセンサが配置されています。上から i 行目、左から j 列目のマス目を (i, j) と表記します。
センサが配置されているマス目の情報は長さ W の文字列 S_1, S_2, \ldots, S_H によって与えられ、S_i の j 文字目が # のとき、またそのときに限り (i, j) にセンサが配置されています。
このセンサは上下左右斜めに隣接しているマス目に存在する他のセンサと連動し、一つのセンサとして動作します。 ただし、マス目 (x, y) と (x', y') が上下左右斜めに隣接しているとは、\max(|x-x'|,|y-y'|) = 1 であることを指します。
また、センサ A とセンサ B が連動し、センサ A とセンサ C が連動しているとき、センサ B とセンサ C も連動することに注意してください。

連動するセンサを一つのセンサと見なしたとき、このマス目の上にあるセンサの個数を求めてください。

制約

  • 1 \leq H, W \leq 1000
  • H, W は整数
  • S_i は各文字が # または . である長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H

出力

答えを出力せよ。


入力例 1

5 6
.##...
...#..
....##
#.#...
..#...

出力例 1

3

連動しているセンサを一つのセンサと見なしたとき、

  • (1,2),(1,3),(2,4),(3,5),(3,6) にあるセンサが連動したもの
  • (4,1) にあるセンサ
  • (4,3),(5,3) にあるセンサが連動したもの

の 3 つのセンサが存在します。


入力例 2

3 3
#.#
.#.
#.#

出力例 2

1

入力例 3

4 2
..
..
..
..

出力例 3

0

入力例 4

5 47
.#..#..#####..#...#..#####..#...#...###...#####
.#.#...#.......#.#...#......##..#..#...#..#....
.##....#####....#....#####..#.#.#..#......#####
.#.#...#........#....#......#..##..#...#..#....
.#..#..#####....#....#####..#...#...###...#####

出力例 4

7

Score : 300 points

Problem Statement

There are zero or more sensors placed on a grid of H rows and W columns. Let (i, j) denote the square in the i-th row from the top and the j-th column from the left.
Whether each square contains a sensor is given by the strings S_1, S_2, \ldots, S_H, each of length W. (i, j) contains a sensor if and only if the j-th character of S_i is #.
These sensors interact with other sensors in the squares horizontally, vertically, or diagonally adjacent to them and operate as one sensor. Here, a cell (x, y) and a cell (x', y') are said to be horizontally, vertically, or diagonally adjacent if and only if \max(|x-x'|,|y-y'|) = 1.
Note that if sensor A interacts with sensor B and sensor A interacts with sensor C, then sensor B and sensor C also interact.

Considering the interacting sensors as one sensor, find the number of sensors on this grid.

Constraints

  • 1 \leq H, W \leq 1000
  • H and W are integers.
  • S_i is a string of length W where each character is # or ..

Input

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

H W
S_1
S_2
\vdots
S_H

Output

Print the answer.


Sample Input 1

5 6
.##...
...#..
....##
#.#...
..#...

Sample Output 1

3

When considering the interacting sensors as one sensor, the following three sensors exist:

  • The interacting sensors at (1,2),(1,3),(2,4),(3,5),(3,6)
  • The sensor at (4,1)
  • The interacting sensors at (4,3),(5,3)

Sample Input 2

3 3
#.#
.#.
#.#

Sample Output 2

1

Sample Input 3

4 2
..
..
..
..

Sample Output 3

0

Sample Input 4

5 47
.#..#..#####..#...#..#####..#...#...###...#####
.#.#...#.......#.#...#......##..#..#...#..#....
.##....#####....#....#####..#.#.#..#......#####
.#.#...#........#....#......#..##..#...#..#....
.#..#..#####....#....#####..#...#...###...#####

Sample Output 4

7
G - Concentric Circles

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

xy 平面上に以下の条件を全て満たす 2 個の円 C_1, C_2 は存在しますか? ただし C_1, C_2 は同一である可能性があります。

  • 異なる 2 点 (P_x, P_y), (Q_x, Q_y) は C_1 の円周上にある。
  • 異なる 2 点 (R_x, R_y), (S_x, S_y) は C_2 の円周上にある。
  • C_1 と C_2 は中心が一致する。

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

制約

  • 1 \leq T \leq 5 \times 10^4
  • -10^9 \leq P_x,P_y,Q_x,Q_y,R_x,R_y,S_x,S_y \leq 10^9
  • (P_x, P_y) \neq (Q_x, Q_y)
  • (R_x, R_y) \neq (S_x, S_y)
  • 入力される値は全て整数

入力

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

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

各テストケース \mathrm{case}_t は以下の形式で与えられる。

P_x P_y Q_x Q_y R_x R_y S_x S_y

出力

T 行出力せよ。t 行目には t 番目のテストケースの答えを出力せよ。
各テストケースでは、条件を全て満たす 2 個の円 C_1, C_2 が存在する場合は Yes を、存在しない場合は No を出力せよ。


入力例 1

3
2 0 1 1 -1 0 1 2
1 0 -1 0 0 1 0 -1
4 0 3 1 2 0 1 1

出力例 1

Yes
Yes
No

1 番目のテストケースを考えます。以下の図に示す通り、中心が (1,0) で半径が 1 の円を C_1、中心が (1,0) で半径が 2 の円を C_2 とすると条件を満たします。

2 番目のテストケースでは、C_1 と C_2 をともに中心が (0,0) で半径が 1 の円とすると条件を満たします。

image

Score : 425 points

Problem Statement

Do there exist two circles C_1 and C_2 on the xy-plane satisfying all of the following conditions? Here, C_1 and C_2 may coincide.

  • The two distinct points (P_x, P_y) and (Q_x, Q_y) lie on the circumference of C_1.
  • The two distinct points (R_x, R_y) and (S_x, S_y) lie on the circumference of C_2.
  • C_1 and C_2 have the same center.

You are given T test cases; solve each of them.

Constraints

  • 1 \leq T \leq 5 \times 10^4
  • -10^9 \leq P_x,P_y,Q_x,Q_y,R_x,R_y,S_x,S_y \leq 10^9
  • (P_x, P_y) \neq (Q_x, Q_y)
  • (R_x, R_y) \neq (S_x, S_y)
  • All input values are integers.

Input

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

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

Each test case \mathrm{case}_t is given in the following format:

P_x P_y Q_x Q_y R_x R_y S_x S_y

Output

Output T lines. The t-th line should contain the answer to the t-th test case.
For each test case, output Yes if there exist two circles C_1 and C_2 satisfying all of the conditions, and No otherwise.


Sample Input 1

3
2 0 1 1 -1 0 1 2
1 0 -1 0 0 1 0 -1
4 0 3 1 2 0 1 1

Sample Output 1

Yes
Yes
No

Consider the first test case. As shown in the figure below, if we let C_1 be the circle with center (1,0) and radius 1, and C_2 be the circle with center (1,0) and radius 2, the conditions are satisfied.

For the second test case, letting both C_1 and C_2 be the circle with center (0,0) and radius 1 satisfies the conditions.

image

H - Permute K times

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 450 点

問題文

各要素が 1 以上 N 以下である長さ N の数列 X と、長さ N の数列 A が与えられます。
A に以下の操作を K 回行った結果を出力してください。

  • B_i=A_{X_i} なる B を新たな A とする

制約

  • 入力は全て整数
  • 1 \le N \le 2 \times 10^5
  • 0 \le K \le 10^{18}
  • 1 \le X_i \le N
  • 1 \le A_i \le 2 \times 10^5

入力

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

N K
X_1 X_2 \dots X_N
A_1 A_2 \dots A_N

出力

操作後の A を A' としたとき、以下の形式で出力せよ。

A'_1 A'_2 \dots A'_N

入力例 1

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

出力例 1

7 2 3 5 1 9 3

この入力では X=(5,2,6,3,1,4,6) で、操作前の数列 A=(1,2,3,5,7,9,11) です。

  • 操作を 1 度行うと、数列は (7,2,9,3,1,5,9) となります。
  • 操作を 2 度行うと、数列は (1,2,5,9,7,3,5) となります。
  • 操作を 3 度行うと、数列は (7,2,3,5,1,9,3) となります。

入力例 2

4 0
3 4 1 2
4 3 2 1

出力例 2

4 3 2 1

操作が一度も行われない場合もあります。


入力例 3

9 1000000000000000000
3 7 8 5 9 3 7 4 2
9 9 8 2 4 4 3 5 3

出力例 3

3 3 3 3 3 3 3 3 3

Score : 450 points

Problem Statement

You are given a sequence X of length N where each element is between 1 and N, inclusive, and a sequence A of length N.
Print the result of performing the following operation K times on A.

  • Replace A with B such that B_i = A_{X_i}.

Constraints

  • All input values are integers.
  • 1 \le N \le 2 \times 10^5
  • 0 \le K \le 10^{18}
  • 1 \le X_i \le N
  • 1 \le A_i \le 2 \times 10^5

Input

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

N K
X_1 X_2 \dots X_N
A_1 A_2 \dots A_N

Output

Let A' be the sequence A after the operations. Print it in the following format:

A'_1 A'_2 \dots A'_N

Sample Input 1

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

Sample Output 1

7 2 3 5 1 9 3

In this input, X=(5,2,6,3,1,4,6) and the initial sequence is A=(1,2,3,5,7,9,11).

  • After one operation, the sequence is (7,2,9,3,1,5,9).
  • After two operations, the sequence is (1,2,5,9,7,3,5).
  • After three operations, the sequence is (7,2,3,5,1,9,3).

Sample Input 2

4 0
3 4 1 2
4 3 2 1

Sample Output 2

4 3 2 1

There may be cases where no operations are performed.


Sample Input 3

9 1000000000000000000
3 7 8 5 9 3 7 4 2
9 9 8 2 4 4 3 5 3

Sample Output 3

3 3 3 3 3 3 3 3 3
I - Hammer 2

Time Limit: 3 sec / Memory Limit: 1024 MiB

配点 : 500 点

問題文

数直線の原点に高橋君がいます。高橋君は座標 X にあるゴールに移動しようとしています。

また、数直線上に N 枚の壁と N 本のハンマーがあります。

  • 座標 Y_1,Y_2,\dots,Y_N にはそれぞれタイプ 1,2,\dots,N の壁があります。
    • 最初、高橋君は壁を超えて移動することができません。
  • 座標 Z_1,Z_2,\dots,Z_N にはそれぞれタイプ 1,2,\dots,N のハンマーがあります。
    • 高橋君はハンマーのある座標に着くとそこにあるハンマーを手に入れます。
    • タイプ i のハンマーはタイプ i の壁を破壊するための専用のもので、タイプ i のハンマーを手に入れた後でなら、タイプ i の壁を破壊して通過できるようになります。

高橋君がゴールに到達することが可能か判定し、可能であれば移動距離の最小値を求めてください。

制約

  • 入力は全て整数
  • 1 \le N \le 1500
  • 1 \le |X|,|Y_i|,|Z_i| \le 10^9
  • 合計 2 \times N + 1 個の座標 X,Y_i,Z_i は相異なる

入力

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

N X
Y_1 Y_2 \dots Y_N
Z_1 Z_2 \dots Z_N

出力

高橋君がゴールに到達することが可能であれば移動距離の最小値を整数として出力せよ。
不可能であれば -1 と出力せよ。


入力例 1

3 10
-2 8 -5
5 -10 3

出力例 1

40

以下の手順により、移動距離 40 で高橋くんがゴールに到達でき、これが移動距離の最小です。

  • 座標 0 から高橋君が行動を開始する。
  • 座標 3 に行く。タイプ 3 のハンマーを手に入れる。
  • 座標 5 に行く。タイプ 1 のハンマーを手に入れる。
  • 座標 -2 に行く。タイプ 1 の壁を破壊する。
  • 座標 -5 に行く。タイプ 3 の壁を破壊する。
  • 座標 -10 に行く。タイプ 2 のハンマーを手に入れる。
  • 座標 8 に行く。タイプ 2 の壁を破壊する。
  • 座標 10 に行く。ここがゴールである。

入力例 2

5 -1
10 -20 30 -40 50
-10 20 -30 40 -50

出力例 2

1

ゴールに移動するために、ハンマーを手に入れる必要も壁を破壊する必要もない場合もあります。


入力例 3

1 100
30
60

出力例 3

-1

高橋君がタイプ 1 のハンマーを手に入れることは不可能であり、ゴールに辿り着くこともできません。


入力例 4

4 865942261
703164879 -531670946 -874856231 -700164975
-941120316 599462305 -649785130 665402307

出力例 4

4078987507

Score : 500 points

Problem Statement

Takahashi is at the origin of a number line. Takahashi wants to reach the goal at coordinate X.

Also, there are N walls and N hammers on the number line.

  • At coordinates Y_1,Y_2,\dots,Y_N are walls of types 1,2,\dots,N, respectively.
    • Initially, Takahashi cannot get over the walls.
  • At coordinates Z_1,Z_2,\dots,Z_N are hammers of types 1,2,\dots,N, respectively.
    • When he arrives at a coordinate with a hammer, he obtains the hammer.
    • The hammer of type i is dedicated to destroying the wall of type i. After he obtains the hammer of type i, he can destroy the wall of type i and get over it.

Determine if he can reach the goal. If he can, find the minimum distance he travels.

Constraints

  • All values in the input are integers.
  • 1 \le N \le 1500
  • 1 \le |X|,|Y_i|,|Z_i| \le 10^9
  • The (2 \times N + 1) coordinates X,Y_i and Z_i are distinct.

Input

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

N X
Y_1 Y_2 \dots Y_N
Z_1 Z_2 \dots Z_N

Output

If Takahashi can reach the goal, print the minimum possible distance he travels as an integer.
Otherwise, print -1.


Sample Input 1

3 10
-2 8 -5
5 -10 3

Sample Output 1

40

Takahashi can reach the goal by traveling a distance of 40 as follows, which is the minimum possible:

  • He starts at coordinate 0.
  • He moves to coordinate 3 to obtain the hammer of type 3.
  • He moves to coordinate 5 to obtain the hammer of type 1.
  • He moves to coordinate -2 to destroy the wall of type 1.
  • He moves to coordinate -5 to destroy the wall of type 3.
  • He moves to coordinate -10 to obtain the hammer of type 2.
  • He moves to coordinate 8 to destroy the wall of type 2.
  • He moves to coordinate 10, which is the goal.

Sample Input 2

5 -1
10 -20 30 -40 50
-10 20 -30 40 -50

Sample Output 2

1

It may not be required that he obtains a hammer or destroys a wall to reach the goal.


Sample Input 3

1 100
30
60

Sample Output 3

-1

Takahashi cannot obtain the hammer of type 1, and neither can he reach the goal.


Sample Input 4

4 865942261
703164879 -531670946 -874856231 -700164975
-941120316 599462305 -649785130 665402307

Sample Output 4

4078987507