Sum Problem

3.3

3 votes
Problem

Mario loves arrays. He was playing with an array, suddenly a question arose in his mind. Given a sum S, is it possible to select any 3 elements from a given array, such that the sum of these 3 elements is greater than S. Since you are a programmer friend of Mario, please help him to solve this problem.

INPUT

First line of the input contains T denoting number of test cases. T test cases follow.
For each test case,
First line contains two space separated integers, S (given sum) and N (number of elements in array ) .
Second line contains n space separated integers (array elements).

OUTPUT

For each test case print "YES" if there exist's any 3 elements in the array, whose sum is greater than S. Otherwise print "NO", without quotes. Print answer corresponding to each test case on a new line. See sample input output for more clarity .

CONSTRAINTS

T <= 100
1 <= S <= 105
3 <= N <= 1000
0 <= value of array elements <= 105

Sample Input
3
15 4
8 6 3 4
29 3
2 5 7
10 5
2 3 4 1 0
Sample Output
YES
NO
NO
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In first test case 8+6+4 = 18 is grater than 15 so we print YES.
In second test case maximum sum of 3 numbers is 14 which is less than 29 so we print NO.
In third test case maximum sum of 3 numbers is 2+3+4 = 9 which is less than 10 so we print NO.

Editor Image

?