実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
人 1 、人 2 、\ldots 、人 N と番号付けられた N 人が、この順番で時計回りに円卓に座っています。 特に、時計回りで人 N の次の位置には人 1 が座っています。
i = 1, 2, \ldots, N について、人 i の名前は S_i 、年齢は A_i です。 ここで、異なる 2 人が同じ名前や同じ年齢であることはありません。
年齢が最も小さい人を起点として、座っている位置の時計回りの順番で、N 人全員の名前を出力してください。
制約
- 2 \leq N \leq 100
- N は整数
- S_i は英小文字のみからなる長さ 1 以上 10 以下の文字列
- i \neq j \implies S_i \neq S_j
- 0 \leq A_i \leq 10^9
- A_i は整数
- i \neq j \implies A_i \neq A_j
入力
入力は以下の形式で標準入力から与えられる。
N S_1 A_1 S_2 A_2 \vdots S_N A_N
出力
N 行出力せよ。 i = 1, 2, \ldots, N について、i 行目には、年齢が最も小さい人を起点として時計回りで i 番目の位置に座っている人の名前を出力せよ。
入力例 1
5 alice 31 bob 41 carol 5 dave 92 ellen 65
出力例 1
carol dave ellen alice bob
年齢が最も小さい人は人 3 です。よって、人 3 を起点として座っている位置の時計回りの順番、すなわち、人 3 、人 4 、人 5 、人 1 、人 2 の順に名前を出力します。
入力例 2
2 takahashi 1000000000 aoki 999999999
出力例 2
aoki takahashi
Score : 100 points
Problem Statement
There are N people numbered 1, 2, \ldots, N, sitting in this clockwise order around a round table. In particular, person 1 is sitting next to person N in the clockwise direction.
For each i = 1, 2, \ldots, N, person i has a name S_i and an age A_i. Here, no two people have the same name or the same age.
Starting from the youngest person, print the names of all N people in the order of their seating positions in clockwise order.
Constraints
- 2 \leq N \leq 100
- N is an integer.
- S_i is a string of length between 1 and 10, consisting of lowercase English letters.
- i \neq j \implies S_i \neq S_j
- 0 \leq A_i \leq 10^9
- A_i is an integer.
- i \neq j \implies A_i \neq A_j
Input
The input is given from Standard Input in the following format:
N S_1 A_1 S_2 A_2 \vdots S_N A_N
Output
Print N lines. For each i = 1, 2, \ldots, N, the i-th line should contain the name of the person sitting in the i-th position clockwise from the youngest person.
Sample Input 1
5 alice 31 bob 41 carol 5 dave 92 ellen 65
Sample Output 1
carol dave ellen alice bob
The youngest person is person 3. Therefore, starting from person 3, print the names in the clockwise order of their seating positions: person 3, person 4, person 5, person 1, and person 2.
Sample Input 2
2 takahashi 1000000000 aoki 999999999
Sample Output 2
aoki takahashi
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 個のビルが横一列に並んでいて、左から i 番目のビルの高さは H_i です。
左から 1 番目のビルより高いビルが存在するか判定し、存在する場合その内最も左のビルは左から何番目か求めてください。
制約
- 1\leq N\leq 100
- 1\leq H_i \leq 100
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N H_1 H_2 \ldots H_N
出力
左から 1 番目のビルより高いビルが存在しない場合 -1 を出力せよ。
存在する場合、その内最も左のビルは左から何番目か出力せよ。
入力例 1
4 3 2 5 2
出力例 1
3
左から 1 番目のビルより高いビルは、左から 3 番目のビルです。
入力例 2
3 4 3 2
出力例 2
-1
左から 1 番目のビルより高いビルは存在しません。
入力例 3
7 10 5 10 2 10 13 15
出力例 3
6
左から 1 番目のビルより高いビルは、左から 6 番目のビルと左から 7 番目のビルです。その内最も左のビルは左から 6 番目のビルです。
Score: 100 points
Problem Statement
There are N buildings aligned in a row. The i-th building from the left has a height of H_i.
Determine if there is a building taller than the first one from the left. If such a building exists, find the position of the leftmost such building from the left.
Constraints
- 1 \leq N \leq 100
- 1 \leq H_i \leq 100
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H_1 H_2 \ldots H_N
Output
If no building is taller than the first one from the left, print -1.
If such a building exists, print the position (index) of the leftmost such building from the left.
Sample Input 1
4 3 2 5 2
Sample Output 1
3
The building taller than the first one from the left is the third one from the left.
Sample Input 2
3 4 3 2
Sample Output 2
-1
No building is taller than the first one from the left.
Sample Input 3
7 10 5 10 2 10 13 15
Sample Output 3
6
The buildings taller than the first one from the left are the sixth and seventh ones. Among them, the leftmost is the sixth one.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
正整数 A,B が与えられます。
A+B を(十進法で)計算する時、繰り上がりが生じないなら Easy 、生じるなら Hard と出力してください。
制約
- A,B は整数
- 1 \le A,B \le 10^{18}
入力
入力は以下の形式で標準入力から与えられる。
A B
出力
繰り上がりが生じないなら Easy 、生じるなら Hard と出力せよ。
入力例 1
229 390
出力例 1
Hard
229+390 を計算する際、十の位から百の位へと繰り上がりが発生します。よって、答えは Hard です。
入力例 2
123456789 9876543210
出力例 2
Easy
繰り上がりは発生しません。答えは Easy です。
また、入力が 32bit 整数に収まらないこともあります。
Score : 200 points
Problem Statement
You are given positive integers A and B.
Let us calculate A+B (in decimal). If it does not involve a carry, print Easy; if it does, print Hard.
Constraints
- A and B are integers.
- 1 \le A,B \le 10^{18}
Input
Input is given from Standard Input in the following format:
A B
Output
If the calculation does not involve a carry, print Easy; if it does, print Hard.
Sample Input 1
229 390
Sample Output 1
Hard
When calculating 229+390, we have a carry from the tens digit to the hundreds digit, so the answer is Hard.
Sample Input 2
123456789 9876543210
Sample Output 2
Easy
We do not have a carry here; the answer is Easy.
Note that the input may not fit into a 32-bit integer.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
縦 A 行、横 B 列のマスからなるタイルを縦 N 行、横 N 列に並べてできた、縦 (A\times N) 行、横 (B\times N) 列のマス目 X があります。
1\leq i,j \leq N について、上から i 行目、左から j 列目のタイルをタイル (i,j) とします。
X の各マスは以下のように塗られています。
- 各タイルは白いタイルまたは黒いタイルである。
- 白いタイルのすべてのマスは白で塗られ、黒いタイルのすべてのマスは黒で塗られている。
- タイル (1,1) は白いタイルである。
- 辺で隣接する 2 つのタイルは異なる色のタイルである。ただし、タイル (a,b) とタイル (c,d) が辺で隣接するとは、|a-c|+|b-d|=1 ( |x| を x の絶対値とする)であることを言う。
マス目 X を出力の形式に従って出力してください。
制約
- 1 \leq N,A,B \leq 10
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N A B
出力
次の条件をみたす (A\times N) 個の文字列 S_1,\ldots,S_{A\times N} を改行区切りで出力せよ。
- S_1,\ldots,S_{A\times N} はそれぞれ長さ (B\times N) の
.または#からなる文字列である。 - 各 i,j (1 \leq i \leq A\times N,1 \leq j \leq B\times N) に対し、マス目 X の上から i 行目かつ左から j 列目のマスが白で塗られているならば S_i の j 文字目は
.であり、黒く塗られているならば#である。
入力例 1
4 3 2
出力例 1
..##..## ..##..## ..##..## ##..##.. ##..##.. ##..##.. ..##..## ..##..## ..##..## ##..##.. ##..##.. ##..##..
入力例 2
5 1 5
出力例 2
.....#####.....#####..... #####.....#####.....##### .....#####.....#####..... #####.....#####.....##### .....#####.....#####.....
入力例 3
4 4 1
出力例 3
.#.# .#.# .#.# .#.# #.#. #.#. #.#. #.#. .#.# .#.# .#.# .#.# #.#. #.#. #.#. #.#.
入力例 4
1 4 4
出力例 4
.... .... .... ....
Score : 200 points
Problem Statement
Tiles are aligned in N horizontal rows and N vertical columns. Each tile has a grid with A horizontal rows and B vertical columns. On the whole, the tiles form a grid X with (A\times N) horizontal rows and (B\times N) vertical columns.
For 1\leq i,j \leq N, Tile (i,j) denotes the tile at the i-th row from the top and the j-th column from the left.
Each square of X is painted as follows.
- Each tile is either a white tile or a black tile.
- Every square in a white tile is painted white; every square in a black tile is painted black.
- Tile (1,1) is a white tile.
- Two tiles sharing a side have different colors. Here, Tile (a,b) and Tile (c,d) are said to be sharing a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).
Print the grid X in the format specified in the Output section.
Constraints
- 1 \leq N,A,B \leq 10
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A B
Output
Print (A\times N) strings S_1,\ldots,S_{A\times N} that satisfy the following condition, with newlines in between.
- Each of S_1,\ldots,S_{A\times N} is a string of length (B\times N) consisting of
.and#. - For each i and j (1 \leq i \leq A\times N,1 \leq j \leq B\times N), the j-th character of S_i is
.if the square at the i-th row from the top and j-th column from the left in grid X is painted white; the character is#if the square is painted black.
Sample Input 1
4 3 2
Sample Output 1
..##..## ..##..## ..##..## ##..##.. ##..##.. ##..##.. ..##..## ..##..## ..##..## ##..##.. ##..##.. ##..##..
Sample Input 2
5 1 5
Sample Output 2
.....#####.....#####..... #####.....#####.....##### .....#####.....#####..... #####.....#####.....##### .....#####.....#####.....
Sample Input 3
4 4 1
Sample Output 3
.#.# .#.# .#.# .#.# #.#. #.#. #.#. #.#. .#.# .#.# .#.# .#.# #.#. #.#. #.#. #.#.
Sample Input 4
1 4 4
Sample Output 4
.... .... .... ....
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。
1\leq i,j \leq N である組 (i,j) であって、A_i-A_j=X となるものが存在するかどうか判定してください。
制約
- 2 \leq N \leq 2\times 10^5
- -10^9 \leq A_i \leq 10^9
- -10^9 \leq X \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N X A_1 \ldots A_N
出力
1\leq i,j \leq N である組 (i,j) であって、A_i-A_j=X となるものが存在するとき Yes、存在しないとき No と出力せよ。
入力例 1
6 5 3 1 4 1 5 9
出力例 1
Yes
A_6-A_3=9-4=5 です。
入力例 2
6 -4 -2 -7 -1 -8 -2 -8
出力例 2
No
A_i-A_j=-4 となる組 (i,j) は存在しません。
入力例 3
2 0 141421356 17320508
出力例 3
Yes
A_1-A_1=0 です。
Score : 300 points
Problem Statement
You are given a sequence of N numbers: A=(A_1,\ldots,A_N).
Determine whether there is a pair (i,j) with 1\leq i,j \leq N such that A_i-A_j=X.
Constraints
- 2 \leq N \leq 2\times 10^5
- -10^9 \leq A_i \leq 10^9
- -10^9 \leq X \leq 10^9
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N X A_1 \ldots A_N
Output
Print Yes if there is a pair (i,j) with 1\leq i,j \leq N such that A_i-A_j=X, and No otherwise.
Sample Input 1
6 5 3 1 4 1 5 9
Sample Output 1
Yes
We have A_6-A_3=9-4=5.
Sample Input 2
6 -4 -2 -7 -1 -8 -2 -8
Sample Output 2
No
There is no pair (i,j) such that A_i-A_j=-4.
Sample Input 3
2 0 141421356 17320508
Sample Output 3
Yes
We have A_1-A_1=0.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
長さ N の非負整数列 A が与えられます。
A から K 要素を選んで順序を保ったまま抜き出した列を B としたとき、 MEX(B) としてありえる最大値を求めてください。
但し、数列 X に対して MEX(X) は以下の条件を満たす唯一の非負整数 m として定義されます。
- 0 \le i < m を満たす全ての整数 i が X に含まれる。
- m が X に含まれない。
制約
- 入力は全て整数
- 1 \le K \le N \le 3 \times 10^5
- 0 \le A_i \le 10^9
入力
入力は以下の形式で標準入力から与えられる。
N K A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
7 3 2 0 2 3 2 1 9
出力例 1
3
この入力では A=(2,0,2,3,2,1,9) であり、ここから K=3 要素を選んで抜き出して数列 B を得ます。例えば、
- 1,2,3 要素目を抜き出した時、 MEX(B)=MEX(2,0,2)=1
- 3,4,6 要素目を抜き出した時、 MEX(B)=MEX(2,3,1)=0
- 2,6,7 要素目を抜き出した時、 MEX(B)=MEX(0,1,9)=2
- 2,3,6 要素目を抜き出した時、 MEX(B)=MEX(0,2,1)=3
のようになります。
達成可能な MEX の最大値は 3 です。
Score : 300 points
Problem Statement
You are given a length-N sequence of non-negative integers.
When B is a sequence obtained by choosing K elements from A and concatenating them without changing the order, find the maximum possible value of MEX(B).
Here, for a sequence X, we define MEX(X) as the unique non-negative integer m that satisfies the following conditions:
- Every integer i such that 0 \le i < m is contained in X.
- m is not contained in X.
Constraints
- All values in the input are integers.
- 1 \le K \le N \le 3 \times 10^5
- 0 \le A_i \le 10^9
Input
The input is given from Standard Input in the following format:
N K A_1 A_2 \dots A_N
Output
Print the answer.
Sample Input 1
7 3 2 0 2 3 2 1 9
Sample Output 1
3
In this input, A=(2,0,2,3,2,1,9), and you obtain the sequence B by picking K=3 elements. For example,
- If the 1-st, 2-nd, and 3-rd elements are chosen, MEX(B)=MEX(2,0,2)=1.
- If the 3-rd, 4-th, and 6-th elements are chosen, MEX(B)=MEX(2,3,1)=0.
- If the 2-nd, 6-th, and 7-th elements are chosen, MEX(B)=MEX(0,1,9)=2.
- If the 2-nd, 3-rd, and 6-th elements are chosen, MEX(B)=MEX(0,2,1)=3.
The maximum possible MEX is 3.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
あなたの運営する Web サービスには N 人のユーザがいます。
i 番目のユーザの現在のユーザ名は S_i ですが、T_i への変更を希望しています。
ここで、S_1,\ldots,S_N は相異なり、T_1,\ldots,T_N も相異なります。
ユーザ名を変更する順序を適切に定めることで、以下の条件を全て満たすように、全てのユーザのユーザ名を希望通り変更することができるか判定してください。
- ユーザ名の変更は 1 人ずつ行う
- どのユーザもユーザ名の変更は一度だけ行う
- ユーザ名の変更を試みる時点で他のユーザが使っているユーザ名に変更することはできない
制約
- 1 \leq N \leq 10^5
- S_i,T_i は英小文字からなる 1 文字以上 8 文字以下の文字列
- S_i \neq T_i
- S_i は相異なる
- T_i は相異なる
入力
入力は以下の形式で標準入力から与えられる。
N S_1 T_1 S_2 T_2 \vdots S_N T_N
出力
条件を全て満たすように全てのユーザのユーザ名を希望通り変更することができるとき Yes、できないとき No と出力せよ。
入力例 1
2 b m m d
出力例 1
Yes
1 番目のユーザの現在のユーザ名は b であり、m への変更を希望しています。
2 番目のユーザの現在のユーザ名は m であり、d への変更を希望しています。
まず、2 番目のユーザのユーザ名を m から d に変更し、
その後 1 番目のユーザのユーザ名を b から m に変更することで、条件を満たしながら変更することができます。
最初の時点では 2 番目のユーザのユーザ名が m なので、1 番目のユーザのユーザ名を同じ m に変更することはできません。
入力例 2
3 a b b c c a
出力例 2
No
1 番目のユーザの現在のユーザ名は a であり、b への変更を希望しています。
2 番目のユーザの現在のユーザ名は b であり、c への変更を希望しています。
3 番目のユーザの現在のユーザ名は c であり、a への変更を希望しています。
条件を満たしながらユーザ名の変更を行うことはできません。
入力例 3
5 aaa bbb yyy zzz ccc ddd xxx yyy bbb ccc
出力例 3
Yes
Score : 400 points
Problem Statement
You run a web service with N users.
The i-th user with a current handle S_i wants to change it to T_i.
Here, S_1,\ldots, and S_N are pairwise distinct, and so are T_1,\ldots, and T_N.
Determine if there is an appropriate order to change their handles to fulfill all of their requests subject to the following conditions:
- you change only one user's handle at a time;
- you change each user's handle only once;
- when changing the handle, the new handle should not be used by other users at that point.
Constraints
- 1 \leq N \leq 10^5
- S_i and T_i are strings of length between 1 and 8 (inclusive) consisting of lowercase English letters.
- S_i \neq T_i
- S_i are pairwise distinct.
- T_i are pairwise distinct.
Input
The input is given from Standard Input in the following format:
N S_1 T_1 S_2 T_2 \vdots S_N T_N
Output
Print Yes if they can change their handles to fulfill all of their requests subject to the conditions; print No otherwise.
Sample Input 1
2 b m m d
Sample Output 1
Yes
The 1-st user with a current handle b wants to change it to m.
The 2-nd user with a current handle m wants to change it to d.
First, you change the 2-nd user's handle from m to d;
then you change the 1-st user's handle from b to m. This way, you can achieve the objective.
Note that you cannot change the 1-st user's handle to m at first, because it is used by the 2-nd user at that point.
Sample Input 2
3 a b b c c a
Sample Output 2
No
The 1-st user with a current handle a wants to change it to b.
The 2-nd user with a current handle b wants to change it to c.
The 3-rd user with a current handle c wants to change it to a.
We cannot change their handles subject to the conditions.
Sample Input 3
5 aaa bbb yyy zzz ccc ddd xxx yyy bbb ccc
Sample Output 3
Yes
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
0,1,2 からなる長さ N の数列 A=(A_1,A_2,\dots,A_N) と、
M, E, X からなる長さ N の文字列 S=S_1S_2\dots S_N が与えられます。
1 \leq i < j < k \leq N かつ S_iS_jS_k= MEX を満たす全ての整数の組 (i,j,k) に対する \text{mex}(A_i,A_j,A_k)
の総和を求めてください。
ここで、\text{mex}(A_i,A_j,A_k) は A_i,A_j,A_k のいずれとも一致しない最小の非負整数を意味します。
制約
- 3\leq N \leq 2\times 10^5
- N は整数
- A_i \in \lbrace 0,1,2\rbrace
- S は
M,E,Xからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N S
出力
答えを整数として出力せよ。
入力例 1
4 1 1 0 2 MEEX
出力例 1
3
S_iS_jS_k = MEX となる i,j,k\ (1 \leq i < j < k \leq N) の組は (i,j,k)=(1,2,4),(1,3,4) の 2 つです。
\text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0,\text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3 より答えは 0+3=3 です。
入力例 2
3 0 0 0 XXX
出力例 2
0
入力例 3
15 1 1 2 0 0 2 0 2 0 0 0 0 0 2 2 EXMMXXXEMEXEXMM
出力例 3
13
Score : 475 points
Problem Statement
You are given a length-N sequence A=(A_1,A_2,\dots,A_N) consisting of 0, 1, and 2,
and a length-N string S=S_1S_2\dots S_N consisting of M, E, and X.
Find the sum of
\text{mex}(A_i,A_j,A_k) over all tuples of integers (i,j,k) such that 1 \leq i < j < k \leq N and S_iS_jS_k= MEX.
Here, \text{mex}(A_i,A_j,A_k) denotes the minimum non-negative integer that equals neither A_i,A_j, nor A_k.
Constraints
- 3\leq N \leq 2\times 10^5
- N is an integer.
- A_i \in \lbrace 0,1,2\rbrace
- S is a string of length N consisting of
M,E, andX.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N S
Output
Print the answer as an integer.
Sample Input 1
4 1 1 0 2 MEEX
Sample Output 1
3
The tuples (i,j,k)\ (1 \leq i < j < k \leq N) such that S_iS_jS_k = MEX are the following two: (i,j,k)=(1,2,4),(1,3,4).
Since \text{mex}(A_1,A_2,A_4)=\text{mex}(1,1,2)=0 and \text{mex}(A_1,A_3,A_4)=\text{mex}(1,0,2)=3, the answer is 0+3=3.
Sample Input 2
3 0 0 0 XXX
Sample Output 2
0
Sample Input 3
15 1 1 2 0 0 2 0 2 0 0 0 0 0 2 2 EXMMXXXEMEXEXMM
Sample Output 3
13
実行時間制限: 3 sec / メモリ制限: 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