|
| 1 | +/* |
| 2 | +You have an array a1,a2,…,an. All ai are positive integers. |
| 3 | +
|
| 4 | +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. |
| 5 | +
|
| 6 | +Can you make all ai lower or equal to d using the operation above any number of times (possibly, zero)? |
| 7 | +
|
| 8 | +Input |
| 9 | +The first line contains a single integer t (1=t=2000) — the number of test cases. |
| 10 | +
|
| 11 | +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. |
| 12 | +
|
| 13 | +The second line contains n integers a1,a2,…,an (1=ai=100) — the array a. |
| 14 | +
|
| 15 | +Output |
| 16 | +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. |
| 17 | +
|
| 18 | +You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). |
| 19 | +
|
| 20 | +Example |
| 21 | +inputCopy |
| 22 | +3 |
| 23 | +5 3 |
| 24 | +2 3 2 5 4 |
| 25 | +3 4 |
| 26 | +2 4 4 |
| 27 | +5 4 |
| 28 | +2 1 5 3 6 |
| 29 | +outputCopy |
| 30 | +NO |
| 31 | +YES |
| 32 | +YES |
| 33 | +Note |
| 34 | +In the first test case, we can prove that we can't make all ai=3. |
| 35 | +
|
| 36 | +In the second test case, all ai are already less or equal than d=4. |
| 37 | +
|
| 38 | +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]. |
| 39 | +
|
| 40 | +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. |
| 41 | +*/ |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +#include<bits/stdc++.h> |
| 49 | +using namespace std; |
| 50 | + |
| 51 | + |
| 52 | +int main(){ |
| 53 | + ios_base::sync_with_stdio(false); |
| 54 | + cin.tie(NULL); |
| 55 | + |
| 56 | + int t; |
| 57 | + cin >> t; |
| 58 | + |
| 59 | + while (t--) { |
| 60 | + int n, d; |
| 61 | + cin >> n >> d; |
| 62 | + |
| 63 | + vector <int> v(n); |
| 64 | + |
| 65 | + for (int i = 0; i < n; i++) cin >> v[i]; |
| 66 | + |
| 67 | + sort(v.begin(), v.end()); |
| 68 | + |
| 69 | + if (v[n - 1] <= d) cout << "YES" << endl; |
| 70 | + else { |
| 71 | + int tmp = v[0] + v[1]; |
| 72 | + if (tmp <= d) cout << "YES" << endl; |
| 73 | + else cout << "NO" << endl; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +} |
0 commit comments