AtCoder Grand Contest 062
D
Walk Around Neighborhood
AtCoder
⌀ 3274
原题
← CE →
翻译中…

Problem Statement

翻译中…

Takahashi, with a notepad, is standing at the origin $(0,0)$ of a two-dimensional plane. His notepad has $N$ even numbers $D_i\ (1\leq i \leq N)$ written.

翻译中…

Takahashi will perform the following actions $N$ times on the plane.

翻译中…
  • He chooses and erases one even number written on the notepad. Let $d$ be the chosen even number. He then moves to a point exactly $d$ distance away in terms of Manhattan distance. More precisely, if Takahashi is currently at the coordinates $(x,y)$, he moves to a point $(x',y')$ such that $|x-x'|+|y-y'|=d$.
翻译中…

After performing $N$ actions, Takahashi must return to the origin $(0,0)$.

翻译中…

Determine if it is possible to perform $N$ actions in such a way. If it is possible, find the minimum value of $\displaystyle \max_{1\leq i \leq N}(|x_i|+|y_i|)$, where $(x_i,y_i)$ are the coordinates where Takahashi is located after the $i$-th action (we can prove that this value is an integer).

翻译中…

Constraints

翻译中…
  • $1 \leq N \leq 2 \times 10^5$
  • $2 \leq D_i \leq 2 \times 10^5$
  • $D_i\ (1\leq i \leq N)$ is even.
  • All input values are integers.
翻译中…

Input

翻译中…

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

翻译中…

N
D_1 D_2 \dots D_N

翻译中…

Output

翻译中…

If it is impossible to perform $N$ actions as described above, print -1. If it is possible, print the minimum value of $\displaystyle \max_{1\leq i \leq N}(|x_i|+|y_i|)$ as an integer.

翻译中…

样例

Input #1
3
2 4 6
Output #1
4
If Takahashi erases $2,6,4$ from his notepad for the first to third actions, respectively, and moves as $(0,0)\rightarrow (0,2) \rightarrow (-4,0) \rightarrow (0,0)$, then $\displaystyle \max_{1\leq i \leq N}(|x_i|+|y_i|)$ is $4$, which is the minimum.
Input #2
5
2 2 2 2 6
Output #2
3
If Takahashi erases $2,2,6,2,2$ from his notepad for the first to fifth actions, respectively, and moves as $(0,0)\rightarrow (\frac{1}{2},\frac{3}{2})\rightarrow (0,3) \rightarrow (0,-3) \rightarrow (-\frac{1}{2},-\frac{3}{2}) \rightarrow (0,0)$, then $\displaystyle \max_{1\leq i \leq N}(|x_i|+|y_i|)$ is $3$, which is the minimum. Takahashi can also move to non-grid points.
Input #3
2
2 200000
Output #3
-1
Takahashi cannot return to the origin after performing $N$ actions as described above.
题目信息
题号
agc062_d
时间限制
4 s
内存限制
1024 MB

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。