atcoder#AGC062D. [AGC062D] Walk Around Neighborhood

[AGC062D] Walk Around Neighborhood

Score : 11001100 points

Problem Statement

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

Takahashi will perform the following actions NN times on the plane.

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

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

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

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 2Di2×1052 \leq D_i \leq 2 \times 10^5
  • Di (1iN)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:

NN

D1D_1 D2D_2 \dots DND_N

Output

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

3
2 4 6
4

If Takahashi erases 2,6,42,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 max1iN(xi+yi)\displaystyle \max_{1\leq i \leq N}(|x_i|+|y_i|) is 44, which is the minimum.

5
2 2 2 2 6
3

If Takahashi erases 2,2,6,2,22,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 max1iN(xi+yi)\displaystyle \max_{1\leq i \leq N}(|x_i|+|y_i|) is 33, which is the minimum.

Takahashi can also move to non-grid points.

2
2 200000
-1

Takahashi cannot return to the origin after performing NN actions as described above.