Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/ikki-korsatkich-eng-yaqin-yigindi

Pair with sum closest to x

Medium 1000 ms 128 MB 45%
Share

$n$ integers sorted in non-decreasing order and an integer $x$ are given. Choose two elements at different positions ($i \lt j$) so that their sum $a_{i} + a_{j}$ is as close to $x$ as possible. Print this sum. If two sums are equally close to $x$, print the smaller one.

Cells of the sorted array: the two elements whose sum is closest to the given number and their sum on the number line are highlighted

Input

The first line contains two integers: $n$ and $x$. The second line contains $n$ integers $a_{i}$ in non-decreasing order.

Output

One integer — the sum closest to $x$.

Note

$2 \le n \le 2 * 10^{5}$
$-10^{9} \le a_{i} \le 10^{9}$
$-2 * 10^{9} \le x \le 2 * 10^{9}$

Examples

Input #1
5 10
1 3 4 7 12
Output #1
10
Input #2
3 6
1 4 6
Output #2
5

Sign in to submit a solution

Signing up is free and takes a minute.