問題一覧 > 通常問題

No.602 The Hidden Game 2

レベル : / AI推定難易度 : 1450 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 66
作問者 : kzyKT / テスター : nmnmnmnmnmnmnm
お気に入りにしたユーザー ProblemId : 1980 / 自分の提出
問題文最終更新日: 2026-06-26 07:42:43
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2017 (順位表) の他の問題:

Problem Statement

This year too, when I opened the drawer of my father's desk, I found another game hidden there.
I decided to play it without permission again this time.

This game is played by moving a character around an infinitely large field paved with square cells.
Because the creator has a nasty personality, moving is a little troublesome.

The character can move to any cell whose Manhattan distance from the current cell is one of $d_1 \dots d_n$.
The Manhattan distance between $(x_1,y_1)$ and $(x_2,y_2)$ is expressed as $|x_1-x_2|+|y_1-y_2|$.

The character is now at the cell $(0,0)$.
I wanted to move from $(0,0)$ to $(x,y)$, but it seems my father will be home soon.
Since I really want to get to $(x,y)$, I have decided to make only $2$ more moves.

Find the minimum number of moves needed to get to $(x,y)$.
If it cannot be reached within $2$ moves, output $-1$.

Input

$n$
$d_1 \dots d_n$
$x \ y$

Line $1$ gives $n$, the number of distances that can be moved.
Line $2$ gives $d_i$ separated by spaces.
Line $3$ gives the coordinates $(x,y)$ of the destination cell separated by a space.

All inputs are given as integers.
$1 \le n \le 10^5$
$1 \le d_i \le 10^9 \ (d_i$はすべて異なる$)$
$-10^9 \le x,y \le 10^9$

Output

Output the minimum number of moves to $(x,y)$.
If it cannot be reached within $2$ moves, output $-1$.

Sample

Sample 1
Input
4
3 5 4 13
8 0
Output
2

$(3,0)→(8,0)$, $(4,0)→(8,0)$, $(13,0)→(8,0)$, etc.

Sample 2
Input
1
5
-2 -3
Output
1

Sample 3
Input
1
14
51 4
Output
-1

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。