-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1473A - Replacing Elements.cpp
78 lines (51 loc) · 1.8 KB
/
1473A - Replacing Elements.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
You have an array a1,a2,…,an. All ai are positive integers.
In one step you can choose three distinct indices i, j, and k (i?j; i?k; j?k) and assign the sum of aj and ak to ai, i. e. make ai=aj+ak.
Can you make all ai lower or equal to d using the operation above any number of times (possibly, zero)?
Input
The first line contains a single integer t (1=t=2000) — the number of test cases.
The first line of each test case contains two integers n and d (3=n=100; 1=d=100) — the number of elements in the array a and the value d.
The second line contains n integers a1,a2,…,an (1=ai=100) — the array a.
Output
For each test case, print YES, if it's possible to make all elements ai less or equal than d using the operation above. Otherwise, print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
Example
inputCopy
3
5 3
2 3 2 5 4
3 4
2 4 4
5 4
2 1 5 3 6
outputCopy
NO
YES
YES
Note
In the first test case, we can prove that we can't make all ai=3.
In the second test case, all ai are already less or equal than d=4.
In the third test case, we can, for example, choose i=5, j=1, k=2 and make a5=a1+a2=2+1=3. Array a will become [2,1,5,3,3].
After that we can make a3=a5+a2=3+1=4. Array will become [2,1,4,3,3] and all elements are less or equal than d=4.
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
while (t--) {
int n, d;
cin >> n >> d;
vector <int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
sort(v.begin(), v.end());
if (v[n - 1] <= d) cout << "YES" << endl;
else {
int tmp = v[0] + v[1];
if (tmp <= d) cout << "YES" << endl;
else cout << "NO" << endl;
}
}
}