Subsequence Addition (Hard Version)(cf))(div4)(G)

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");
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

q619718

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值