Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
3 つの整数 A,B,C が与えられます。これら 3 つの整数を 2 つ以上のグループに分けて、それぞれのグループ内の数の和を等しくできるかどうか判定してください。
制約
- 1 \leq A,B,C \leq 1000
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
A B C
出力
A,B,C を和の等しい 2 つ以上のグループに分けることができるならば Yes を、できないならば No を出力せよ。
入力例 1
3 8 5
出力例 1
Yes
例えば、(3,5),(8) と 2 つのグループに分けることで、それぞれのグループ内の和をどちらも 8 にすることができます。
入力例 2
2 2 2
出力例 2
Yes
(2),(2),(2) と 3 つのグループに分けることで、それぞれのグループ内の和をどれも 2 にすることができます。
入力例 3
1 2 4
出力例 3
No
どのように 2 つ以上のグループに分けても、和を等しくすることはできません。
Score : 100 points
Problem Statement
You are given three integers A,B,C. Determine whether it is possible to divide these three integers into two or more groups so that these groups have equal sums.
Constraints
- 1 \leq A,B,C \leq 1000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
A B C
Output
If it is possible to divide A,B,C into two or more groups with equal sums, print Yes; otherwise, print No.
Sample Input 1
3 8 5
Sample Output 1
Yes
For example, by dividing into two groups (3,5) and (8), each group can have the sum 8.
Sample Input 2
2 2 2
Sample Output 2
Yes
By dividing into three groups (2),(2),(2), each group can have the sum 2.
Sample Input 3
1 2 4
Sample Output 3
No
No matter how you divide them into two or more groups, it is not possible to make the sums equal.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
面積が正である三角形 ABC があります。
三角形 ABC の三辺の長さはそれぞれ a,b,c です。
三角形 ABC が二等辺三角形であるか判定してください。
制約
- 1 \leq a,b,c \leq 10
- 三辺の長さが a,b,c であるような三角形が存在し、その面積は正である。
- a,b,c は整数
入力
入力は以下の形式で標準入力から与えられる。
a b c
出力
三角形 ABC が二等辺三角形であるならば Yes を、そうでないならば No を出力せよ。
入力例 1
4 2 4
出力例 1
Yes
a=c であるため、三角形 ABC は二等辺三角形です。
よって、Yes を出力します。
入力例 2
3 4 5
出力例 2
No
三角形 ABC の三辺の長さはすべて異なるため、二等辺三角形ではありません。
よって、No を出力します。
入力例 3
10 10 10
出力例 3
Yes
正三角形も二等辺三角形の一種であることに注意してください。
Score : 100 points
Problem Statement
There is a triangle ABC with positive area.
The lengths of the three sides of triangle ABC are a,b,c.
Determine whether triangle ABC is isosceles.
Constraints
- 1 \leq a,b,c \leq 10
- A triangle with side lengths a,b,c exists, and its area is positive.
- a,b,c are integers.
Input
The input is given from Standard Input in the following format:
a b c
Output
If triangle ABC is isosceles, output Yes; otherwise, output No.
Sample Input 1
4 2 4
Sample Output 1
Yes
Since a=c, triangle ABC is isosceles.
Thus, output Yes.
Sample Input 2
3 4 5
Sample Output 2
No
Since the three side lengths of triangle ABC are all different, it is not isosceles.
Thus, output No.
Sample Input 3
10 10 10
Sample Output 3
Yes
Note that an equilateral triangle is a kind of isosceles triangle.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
長さ N の非負整数列 A=(A_1,A_2,\dots,A_N) が与えられます。次を満たす最大の非負整数 x を求めてください。
- A に、 x 以上の要素が重複を含めて x 回以上現れる。
制約
- 1 \leq N \leq 100
- 0 \leq A_i \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N
出力
答えを出力せよ。
入力例 1
3 1 2 1
出力例 1
1
A=(1,2,1) に
- 0 以上の要素は 3 回
- 1 以上の要素は 3 回
- 2 以上の要素は 1 回
- 3 以上の要素は 0 回
現れます。条件を満たす最大の非負整数は 1 です。
入力例 2
7 1 6 2 10 2 3 2
出力例 2
3
Score : 200 points
Problem Statement
You are given a sequence of non-negative integers A=(A_1,A_2,\dots,A_N) of length N. Find the maximum non-negative integer x that satisfies the following:
- In A, elements greater than or equal to x appear at least x times (including duplicates).
Constraints
- 1 \leq N \leq 100
- 0 \leq A_i \leq 10^9
- 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 1 2 1
Sample Output 1
1
In A=(1,2,1):
- Elements greater than or equal to 0 appear 3 times.
- Elements greater than or equal to 1 appear 3 times.
- Elements greater than or equal to 2 appear 1 time.
- Elements greater than or equal to 3 appear 0 times.
The maximum non-negative integer that satisfies the condition is 1.
Sample Input 2
7 1 6 2 10 2 3 2
Sample Output 2
3
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 250 点
問題文
縦 N 行 横 N 列からなる 2 つのグリッド S,T があります。グリッドの上から i 行目、左から j 列目のマスをマス (i,j) と表します。
グリッド S,T の各マスは白または黒のいずれかに塗られています。S_{i,j} が . のとき S のマス (i,j) は白く、S_{i,j} が # のとき S のマス (i,j) は黒く塗られています。T についても同様です。
次の 2 種類の操作を好きな順序で好きな回数行うとき、グリッド S をグリッド T と一致させるために必要な操作回数の最小値を求めてください。
- グリッド S のマスを 1 つ選び、色を変更する
- グリッド S 全体を 90 度右に回転する
制約
- 1\leq N \leq 100
- N は整数
- S_{i,j},T_{i,j} は
.または#
入力
入力は以下の形式で標準入力から与えられる。
N
S_{1,1}S_{1,2}\dots S_{1,N}
\vdots
S_{N,1}S_{N,2}\dots S_{N,N}
T_{1,1}T_{1,2}\dots T_{1,N}
\vdots
T_{N,1}T_{N,2}\dots T_{N,N}
出力
答えを出力せよ。
入力例 1
4 ###. ..#. ..#. ..#. ...# ...# ###. ....
出力例 1
2
下図のようにして 2 回の操作で S を T と一致させることができます。

入力例 2
13 .#..###..##.. #.#.#..#.#.#. #.#.###..#... ###.#..#.#.#. #.#.###..##.. ............. ..#...#....#. .##..#.#..##. #.#..#.#.#.#. ####.#.#.#### ..#..#.#...#. ..#...#....#. ............. ............. .#....#...#.. .#...#.#..#.. ####.#.#.#### .#.#.###..#.# .##....#..##. .#....#...#.. ............. ..##..###.#.# .#.#.#..#.### .#.#..###.#.# .#.#.#..#.#.# ..##..###..#.
出力例 2
5
Score : 250 points
Problem Statement
There are two grids S and T, each with N rows and N columns. Let (i,j) denote the cell at the i-th row from the top and the j-th column from the left.
Each cell of grids S and T is colored either white or black. Cell (i,j) of S is white if S_{i,j} is ., and black if S_{i,j} is #. The same applies to T.
You may perform the following two types of operations any number of times in any order. Find the minimum number of operations required to make grid S identical to grid T.
- Choose one cell of grid S and change its color.
- Rotate the entire grid S 90 degrees clockwise.
Constraints
- 1 \le N \le 100
- N is an integer.
- Each of S_{i,j} and T_{i,j} is
.or#.
Input
The input is given from Standard Input in the following format:
N
S_{1,1}S_{1,2}\dots S_{1,N}
\vdots
S_{N,1}S_{N,2}\dots S_{N,N}
T_{1,1}T_{1,2}\dots T_{1,N}
\vdots
T_{N,1}T_{N,2}\dots T_{N,N}
Output
Output the minimum number of operations required.
Sample Input 1
4 ###. ..#. ..#. ..#. ...# ...# ###. ....
Sample Output 1
2
You can match S to T in two operations as shown below.

Sample Input 2
13 .#..###..##.. #.#.#..#.#.#. #.#.###..#... ###.#..#.#.#. #.#.###..##.. ............. ..#...#....#. .##..#.#..##. #.#..#.#.#.#. ####.#.#.#### ..#..#.#...#. ..#...#....#. ............. ............. .#....#...#.. .#...#.#..#.. ####.#.#.#### .#.#.###..#.# .##....#..##. .#....#...#.. ............. ..##..###.#.# .#.#.#..#.### .#.#..###.#.# .#.#.#..#.#.# ..##..###..#.
Sample Output 2
5
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
りんごさんは馬を縞模様に塗ってシマウマにしようとしています。
1 から N の番号がついた N 個のマスが一列に並んでいます。
最初、全てのマスは白色であり、マス i を黒く塗るためにはコストが C_i かかります。
以下の手順を一度だけ行い、いくつかのマスを黒く塗ることを考えます。
- 正整数 x を自由に選ぶ。
- 1 \leq i \leq N を満たす整数 i のうち、 (i+x) を 2W で割った余りが W より小さくなるもの全てに対し、マス i を黒く塗る。
この手順を行うためのコストの合計の最小値を求めてください。
T 個のテストケースが与えられるので、それぞれについて答えを求めてください。
制約
- 1\leq T \leq 2\times 10^5
- 1\leq N \leq 2\times 10^5
- 1\leq W \leq 2\times 10^5
- 1\leq C_i \leq 10^9
- T 個のテストケースにおける N の総和は 2\times 10^5 以下
- T 個のテストケースにおける W の総和は 2\times 10^5 以下
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。ここで \mathrm{case}_i は i 番目のテストケースを意味する。
T
\mathrm{case}_1
\vdots
\mathrm{case}_T
各テストケースは以下の形式で与えられる。
N W C_1 \dots C_N
出力
T 行出力せよ。i 行目には i 番目のテストケースに対する答えを出力せよ。
入力例 1
4 8 2 1 10 10 1 1 10 10 1 8 3 1 10 10 1 1 10 10 1 8 4 1 10 10 1 1 10 10 1 4 100 100000 100000 100000 100000
出力例 1
4 12 22 0
1 番目のテストケースにおいて、x=4 として手順を実行すると、マス 1,4,5,8 が黒く塗られ、コストの合計は 4 となります。 コストの合計を 4 未満にすることはできないため、これが最小値となります。
Score : 300 points
Problem Statement
Ringo-san is trying to paint a horse with stripes to make it a zebra.
There are N squares numbered 1 to N arranged in a line.
Initially, all squares are white, and the cost of painting square i black is C_i.
Consider performing the following procedure once to paint some squares black:
- Choose a positive integer x freely.
- Paint square i black for all integers i satisfying 1 \leq i \leq N such that the remainder when (i+x) is divided by 2W is less than W.
Find the minimum total cost to perform this procedure.
You are given T test cases; solve each of them.
Constraints
- 1\leq T \leq 2\times 10^5
- 1\leq N \leq 2\times 10^5
- 1\leq W \leq 2\times 10^5
- 1\leq C_i \leq 10^9
- The sum of N over the T test cases is at most 2\times 10^5.
- The sum of W over the T test cases is at most 2\times 10^5.
- All input values are integers.
Input
The input is given from Standard Input in the following format, where \mathrm{case}_i denotes the i-th test case.
T
\mathrm{case}_1
\vdots
\mathrm{case}_T
Each test case is given in the following format:
N W C_1 \dots C_N
Output
Output T lines. The i-th line should contain the answer for the i-th test case.
Sample Input 1
4 8 2 1 10 10 1 1 10 10 1 8 3 1 10 10 1 1 10 10 1 8 4 1 10 10 1 1 10 10 1 4 100 100000 100000 100000 100000
Sample Output 1
4 12 22 0
In the first test case, if the procedure is executed with x=4, squares 1,4,5,8 are painted black, and the total cost is 4. The total cost cannot be less than 4, so this is the minimum.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
2 次元グリッド上に 2 つの図形 S と T があります。グリッドは正方形のマスからなります。
S は N 行 N 列のグリッド内にあり、S_{i,j} が # であるようなマス全体からなります。
T も N 行 N 列のグリッド内にあり、T_{i,j} が # であるようなマス全体からなります。
S と T を 90 度回転及び平行移動の繰り返しによって一致させることができるか判定してください。
制約
- 1 \leq N \leq 200
- S,T は
#と.のみからなる - S,T は 1 つ以上
#を含む
入力
入力は以下の形式で標準入力から与えられる。
N
S_{1,1}S_{1,2}\ldots S_{1,N}
\vdots
S_{N,1}S_{N,2}\ldots S_{N,N}
T_{1,1}T_{1,2}\ldots T_{1,N}
\vdots
T_{N,1}T_{N,2}\ldots T_{N,N}
出力
S と T を90 度回転及び平行移動の繰り返しによって一致させることができるとき Yes を、そうでないとき No を出力せよ。
入力例 1
5 ..... ..#.. .###. ..... ..... ..... ..... ....# ...## ....#
出力例 1
Yes
S を左回りに 90 度回転させ、平行移動することで T に一致させることができます。
入力例 2
5 ##### ##..# #..## ##### ..... ##### #..## ##..# ##### .....
出力例 2
No
90 度回転と平行移動の繰り返しによって一致させることはできません。
入力例 3
4 #... ..#. ..#. .... #... #... ..#. ....
出力例 3
Yes
S 及び T は連結とは限りません。
入力例 4
4 #... .##. ..#. .... ##.. #... ..#. ....
出力例 4
No
回転や移動の操作は連結成分ごとにできるわけではなく、S,T 全体に対して行うことに注意してください。
Score : 300 points
Problem Statement
We have two figures S and T on a two-dimensional grid with square cells.
S lies within a grid with N rows and N columns, and consists of the cells where S_{i,j} is #.
T lies within the same grid with N rows and N columns, and consists of the cells where T_{i,j} is #.
Determine whether it is possible to exactly match S and T by 90-degree rotations and translations.
Constraints
- 1 \leq N \leq 200
- Each of S and T consists of
#and.. - Each of S and T contains at least one
#.
Input
Input is given from Standard Input in the following format:
N
S_{1,1}S_{1,2}\ldots S_{1,N}
\vdots
S_{N,1}S_{N,2}\ldots S_{N,N}
T_{1,1}T_{1,2}\ldots T_{1,N}
\vdots
T_{N,1}T_{N,2}\ldots T_{N,N}
Output
Print Yes if it is possible to exactly match S and T by 90-degree rotations and translations, and No otherwise.
Sample Input 1
5 ..... ..#.. .###. ..... ..... ..... ..... ....# ...## ....#
Sample Output 1
Yes
We can match S to T by rotating it 90-degrees counter-clockwise and translating it.
Sample Input 2
5 ##### ##..# #..## ##### ..... ##### #..## ##..# ##### .....
Sample Output 2
No
It is impossible to match them by 90-degree rotations and translations.
Sample Input 3
4 #... ..#. ..#. .... #... #... ..#. ....
Sample Output 3
Yes
Each of S and T may not be connected.
Sample Input 4
4 #... .##. ..#. .... ##.. #... ..#. ....
Sample Output 4
No
Note that it is not allowed to rotate or translate just a part of a figure; it is only allowed to rotate or translate a whole figure.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 400 点
問題文
空の数列 A があります。
クエリが Q 個与えられるので、与えられた順番に処理してください。
クエリは次の 3 種類のいずれかです。
-
1 x: A に x を追加する。 -
2 x k: A の x 以下の要素のうち、大きい方から k 番目の値を出力する。(k は \bf{5} 以下)
ただし、A に x 以下の要素が k 個以上存在しないときは-1と出力する。 -
3 x k: A の x 以上の要素のうち、小さい方から k 番目の値を出力する。(k は \bf{5} 以下)
ただし、A に x 以上の要素が k 個以上存在しないときは-1と出力する。
制約
- 1\leq Q \leq 2\times 10^5
- 1\leq x\leq 10^{18}
- 1\leq k\leq 5
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
i 番目のクエリ \text{query}_i では、まずクエリの種類 c_i (1,2,3 のいずれか) が与えられる。
c_i=1 の場合は x が追加で与えられ、c_i=2,3 の場合は x,k が追加で与えられる。
すなわち、各クエリは以下に示す 3 つの形式のいずれかである。
1 x
2 x k
3 x k
出力
c_i=2,3 を満たすクエリの個数を q として q 行出力せよ。
j(1\leq j\leq q) 行目では j 番目のそのようなクエリに対する答えを出力せよ。
入力例 1
11 1 20 1 10 1 30 1 20 3 15 1 3 15 2 3 15 3 3 15 4 2 100 5 1 1 2 100 5
出力例 1
20 20 30 -1 -1 1
\text{query}_{1,2,3,4} が終了した段階で、A=(20,10,30,20) となっています。
\text{query}_{5,6,7} について、A の 15 以上の要素は (20,30,20) です。
このうち小さい方から 1 番目の値は 20 、2 番目の値は 20 、3 番目の値は 30 です。
Score : 400 points
Problem Statement
We have an empty sequence A.
Given Q queries, process them in order.
Each query is of one of the following three types.
-
1 x: Insert x to A. -
2 x k: Among the elements of A that are less than or equal to x, print the k-th largest value. (k is no more than \bf{5})
If there are less than k elements of A that are less than or equal to x, then print-1. -
3 x k: Among the elements of A that are greater than or equal to x, print the k-th smallest value. (k is no more than \bf{5})
If there are less than k elements of A that are greater than or equal to x, then print-1.
Constraints
- 1\leq Q \leq 2\times 10^5
- 1\leq x\leq 10^{18}
- 1\leq k\leq 5
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
In the i-th query \text{query}_i, the type of query c_i (which is either 1, 2, or 3) is given first.
If c_i=1, then x is additionally given; if c_i=2, 3, then x and k are additionally given.
In other words, each query is given in one of the following three formats:
1 x
2 x k
3 x k
Output
Print q lines, where q is the number of queries such that c_i=2,3.
The j-th line (1\leq j\leq q) should contain the answer for the j-th such query.
Sample Input 1
11 1 20 1 10 1 30 1 20 3 15 1 3 15 2 3 15 3 3 15 4 2 100 5 1 1 2 100 5
Sample Output 1
20 20 30 -1 -1 1
After \text{query}_{1,2,3,4} have been processed, we have A=(20,10,30,20).
For \text{query}_{5,6,7}, the elements of A greater than or equal to 15 are (20,30,20).
The 1-st smallest value of them is 20; the 2-nd is 20; the 3-rd is 30.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 475 点
問題文
整数 N,M と各要素が 1 以上 M 以下である長さ N の整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。
この整数列 A に対して以下の操作を 10^{100} 回行います。
- 1 以上 M 以下の整数のうち A における出現回数が最も少ないものを v とする。ただし、そのような v が複数存在する場合はその中で値が最小のものとする。そして、A の末尾に v を追加する。
Q 個のクエリが与えられます。 i 番目のクエリでは整数 X_i が与えられるので、操作を 10^{100} 回行った後の A_{X_i} の値を求めてください。
制約
- 1\le N,M\le 5\times 10^5
- 1\le A_i \le M
- 1\le Q\le 2\times 10^5
- 1\le X_i \le 10^{18}
- 入力される値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \ldots A_N Q X_1 X_2 \vdots X_Q
出力
Q 行出力せよ。
i 行目には、i 番目のクエリに対する答えを出力せよ。
入力例 1
3 3 1 1 2 8 1 2 3 4 5 6 7 8
出力例 1
1 1 2 3 2 3 1 2
はじめ A=(1,1,2) です。各操作で A は以下のように変化します。
- 1 回目:A に含まれる 1,2,3 の個数はそれぞれ 2,1,0 であるため、v=3 とする。v を A の末尾に追加し、A=(1,1,2,3) となる。
- 2 回目:A に含まれる 1,2,3 の個数はそれぞれ 2,1,1 であるため、v=2 とする。v を A の末尾に追加し、A=(1,1,2,3,2) となる。
- 3 回目:A に含まれる 1,2,3 の個数はそれぞれ 2,2,1 であるため、v=3 とする。v を A の末尾に追加し、A=(1,1,2,3,2,3) となる。
- \vdots
操作を 10^{100} 回行った後の A は A=(1,1,2,3,2,3,1,2,\ldots) となります。
入力例 2
7 30 20 26 3 14 4 4 9 10 31 9 21 23 97 99 30 79 57 3
出力例 2
30 2 18 21 7 9 29 19 27 3
Score : 475 points
Problem Statement
You are given integers N, M and an integer sequence A=(A_1,A_2,\ldots,A_N) of length N where each element is between 1 and M, inclusive.
The following operation is performed 10^{100} times on this integer sequence A:
- Let v be the integer between 1 and M, inclusive, with the fewest occurrences in A. If there are multiple such v, take the smallest among them. Then, append v to the end of A.
You are given Q queries. The i-th query gives an integer X_i, so find the value of A_{X_i} after performing the operation 10^{100} times.
Constraints
- 1\le N,M\le 5\times 10^5
- 1\le A_i \le M
- 1\le Q\le 2\times 10^5
- 1\le X_i \le 10^{18}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \ldots A_N Q X_1 X_2 \vdots X_Q
Output
Output Q lines.
The i-th line should contain the answer to the i-th query.
Sample Input 1
3 3 1 1 2 8 1 2 3 4 5 6 7 8
Sample Output 1
1 1 2 3 2 3 1 2
Initially, A=(1,1,2). With each operation, A changes as follows:
- First operation: The counts of 1, 2, 3 in A are 2, 1, 0 respectively, so v=3. Append v to the end of A, giving A=(1,1,2,3).
- Second operation: The counts of 1, 2, 3 in A are 2, 1, 1 respectively, so v=2. Append v to the end of A, giving A=(1,1,2,3,2).
- Third operation: The counts of 1, 2, 3 in A are 2, 2, 1 respectively, so v=3. Append v to the end of A, giving A=(1,1,2,3,2,3).
- \vdots
After performing the operation 10^{100} times, A becomes A=(1,1,2,3,2,3,1,2,\ldots).
Sample Input 2
7 30 20 26 3 14 4 4 9 10 31 9 21 23 97 99 30 79 57 3
Sample Output 2
30 2 18 21 7 9 29 19 27 3
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