AlgoUniversity
  • Go Back
Discussion
The Chemist :

Author

Shikhar Mehrotra

Difficulty Level : Medium

Submissions : 1164

Asked In : Barclays

Marks :20

: 16 | : 0

Barry works as a chemist in a science lab , Barry is working on new experiment. In the experiment he prepares a list of chemical elements, each element having its own chemical reaction power .He needs to select two elements and reacting the two elements together result of this reaction is subtraction of the chemical power of the second element from the first element, Barry wants the result to be as close as possible to a value Barry had decided before the experiment(in absolute difference).

The problem here is that Barry need to repeat it many times for different pairs of elements to get the closest pair to the desired result, each reaction costs time and materials, so Barry needs to select the right elements before the reaction. If there are many options, he selects the one that has summation of chemical power maximum.

Given decided result or desired result before the experiment and list of chemical reaction power, Help Barry to determine summation of chemical reaction power of the two elements Barry should select.

Input

The first line contains an integer , $$$0\le$$$desired_result$$$\le 10^{5}$$$ denoting the desired result or decided result.

The second line contains an integer $$$ 2\le n \le 10^{5}$$$ denoting number of elements in the $$$powers$$$ list.

Each line $$$l_i$$$ of $$$n$$$ subsequent lines $$$where,0<=i<n$$$ contains an integer describing $$$0\le powers_i \le 10^{5}$$$

Output

Print the summation of the chemical reaction power of two elements Barry should select.

Examples

Input
4
5
2
1
0
7
2
Output
9
Input
5
8
2
1
4
2
3
2
2
6
Output
7

Note

In the first sample $$$n=5$$$ and desired_result=4 and list of $$$power$$$ of elements is $$$power[2, 1, 0, 7, 2]$$$. the two selected powers are $$$(7,2)$$$ result=$$$7-2=5$$$

and difference between subtraction and desired result is $$$1$$$ . It can be shown that this difference can be at most 1 and maximum sum corresponding to the given minimum difference cannot exceed $$$7+2=9$$$.Hence answer is 9.

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.