J - Moving the Piece Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

高橋君は数直線上でコマを使ったゲームをしています。最初、数直線上の座標 0 にコマが置かれており、数直線上に障害物は 1 つもありません。

これから、i = 1, 2, \dots, N の順に以下のイベントが発生します。

  1. 座標 X_i に障害物が設置される。設置された障害物は以降その場に残り続ける(すでに障害物がある座標に設置された場合も状態は変わらない)。
  2. コマの現在座標が X_i である場合、コマを以下の条件で定まる整数座標 Y へ移動させる。そうでない場合、コマは移動させない。
  • 現在障害物が置かれていない座標、すなわち Y \notin \{X_1, X_2, \dots, X_i\} を満たす座標を選ぶ。
  • そのような座標のうち、現在のコマの座標 X_i との距離 |Y - X_i| が最小となる座標を選ぶ。
  • 最小距離を d として、条件を満たす座標が X_i - d と X_i + d の 2 つ存在する場合は、より値が大きい X_i + d を選ぶ。

これにより、移動先 Y は常に一意に定まります。

すべてのイベントが終了した時点で、コマが置かれている座標を求めてください。

制約

  • 1 \leq N \leq 10^5
  • -10^9 \leq X_i \leq 10^9(1 \leq i \leq N)
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N
X_1
X_2
\vdots
X_N

出力

すべてのイベントが終了した時点で、コマが置かれている座標を 1 行で出力せよ。


入力例 1

4
0
1
5
2

出力例 1

3

入力例 2

2
10
-5

出力例 2

0

入力例 3

10
0
2
3
1
-2
-1
10
-3
-4
0

出力例 3

-5

入力例 4

25
1
2
3
4
5
-1
-2
-3
-4
0
-6
-7
-5
1000000000
-1000000000
999999999
-8
-9
-10
500
-500
-11
1
2
-12

出力例 4

-13

入力例 5

1
0

出力例 5

1

Score : 433 pts

Problem Statement

Takahashi is playing a game with a piece on a number line. Initially, the piece is placed at coordinate 0 on the number line, and there is not even 1 obstacle on the number line.

The following events occur in order for i = 1, 2, \dots, N:

  1. An obstacle is placed at coordinate X_i. The placed obstacle remains there indefinitely (if an obstacle is placed at a coordinate that already has an obstacle, the state does not change).
  2. If the current coordinate of the piece is X_i, move the piece to the integer coordinate Y determined by the following conditions. Otherwise, do not move the piece.
  • Choose a coordinate where an obstacle is not currently placed, that is, a coordinate satisfying Y \notin \{X_1, X_2, \dots, X_i\}.
  • Among such coordinates, choose one that minimizes the distance |Y - X_i| to the piece's current coordinate X_i.
  • Letting d be the minimum distance, if there are 2 coordinates satisfying the conditions, X_i - d and X_i + d, choose the larger one, X_i + d.

This always uniquely determines the destination Y.

Find the coordinate where the piece is located after all events have finished.

Constraints

  • 1 \leq N \leq 10^5
  • -10^9 \leq X_i \leq 10^9 (1 \leq i \leq N)
  • All input values are integers.

Input

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

N
X_1
X_2
\vdots
X_N

Output

Print the coordinate where the piece is located after all events have finished in 1 line.


Sample Input 1

4
0
1
5
2

Sample Output 1

3

Sample Input 2

2
10
-5

Sample Output 2

0

Sample Input 3

10
0
2
3
1
-2
-1
10
-3
-4
0

Sample Output 3

-5

Sample Input 4

25
1
2
3
4
5
-1
-2
-3
-4
0
-6
-7
-5
1000000000
-1000000000
999999999
-8
-9
-10
500
-500
-11
1
2
-12

Sample Output 4

-13

Sample Input 5

1
0

Sample Output 5

1