The only difference between the two versions is that in this version, the constraints are higher.
Initially, array 𝑎 contains just the number 11. 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 𝑘 (1≤𝑘≤|𝑎|) distinct indices 𝑖1,𝑖2,…,𝑖𝑘 and insert anywhere into 𝑎 a new element with the value equal to 𝑎𝑖1+𝑎𝑖2+⋯+𝑎𝑖𝑘.
Input
The first line of the input contains an integer 𝑡 (1≤𝑡≤1000) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer 𝑛 (1≤𝑛≤2⋅105) — the number of elements the final array 𝑐 should have.
The second line of each test case contains 𝑛 space-separated integers 𝑐𝑖(1≤𝑐𝑖≤2⋅105) — 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 2⋅105.
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).
Example
input
Copy
6
1
1
1
2
5
5 1 3 2 1
5
7 1 5 2 1
3
1 1 1
5
1 1 4 2 1
output
Copy
YES NO YES NO YES YES
题意大概为每次放入数组一个数,该数为当前数组任意长度的数的和
这里无法给出严谨证明,首先数组排序,然后如果当前加入的数小于数组总和,即可加入
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <set>
#include <stack>
using namespace std;
typedef long long ll;
typedef pair<ll,ll>PII;
constexpr int N=2e5+7;
int main(){
int t;
scanf("%d",&t);
while(t--){
ll a[N],b[N],n;
memset(b,0,sizeof b);
memset(a,0,sizeof a);
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
}
sort(a+1,a+n+1);
for(int i=1;i<=n;i++){
b[i]=b[i-1]+a[i];
}
bool st= false;
if(n==1){
if(a[1]==1){
printf("YES\n");
}
else
printf("NO\n");
}
else {
if (a[1] != 1) {
printf("NO\n");
}
else {
for (int i = 2; i <= n; i++) {
if (a[i] > b[i - 1]) {
st = true;
}
}
if (st)printf("NO\n");
else printf("YES\n");
}
}
}
}