LogiQuest

404. 3Sum Closest

MediumTwo PointersSorting

Choose three different positions whose values sum as close as possible to target. Print that sum; if two sums are equally close, print the smaller one.

Input format

Line 1: n target Line 2: n integers

Output format

The closest sum.

Example 1

Input

4 1
-1 2 1 -4

Output

2

Example 2

Input

3 1
0 0 0

Output

0

Constraints

3 ≤ n ≤ 1000

Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.

Loading the MyLoginn IDE…