#P1807G2. Subsequence Addition (Hard Version)
Subsequence Addition (Hard Version)
Description
The only difference between the two versions is that in this version, the constraints are higher.
Initially, array contains just the number . You can perform several operations in order to change the array. In an operation, you can select some subsequence of and add into an element equal to the sum of all elements of the subsequence.
You are given a final array . Check if can be obtained from the initial array by performing some number (possibly 0) of operations on the initial array.
A sequence is a subsequence of a sequence if can be obtained from by the deletion of several (possibly zero, but not all) elements. In other words, select () distinct indices and insert anywhere into a new element with the value equal to .
The first line of the input contains an integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer () — the number of elements the final array should have.
The second line of each test case contains space-separated integers () — the elements of the final array that should be obtained from the initial array .
It is guaranteed that the sum of over all test cases does not exceed .
For each test case, output "YES" (without quotes) if such a sequence of operations exists, and "NO" (without quotes) otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
Input
The first line of the input contains an integer () — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer () — the number of elements the final array should have.
The second line of each test case contains space-separated integers () — the elements of the final array that should be obtained from the initial array .
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output "YES" (without quotes) if such a sequence of operations exists, and "NO" (without quotes) otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).
Note
For the first test case, the initial array is already equal to , so the answer is "YES".
For the second test case, performing any amount of operations will change to an array of size at least two which doesn't only have the element , thus obtaining the array is impossible and the answer is "NO".
For the third test case, we can perform the following operations in order to obtain the final given array :
- Initially, .
- By choosing the subsequence , and inserting in the array, changes to .
- By choosing the subsequence , and inserting in the middle of the array, changes to .
- By choosing the subsequence , and inserting after the first of the array, changes to .
- By choosing the subsequence and inserting at the beginning of the array, changes to (which is the array we needed to obtain).