C. Knapsack

这篇博客讨论了一个与背包问题相关的问题,其中你需要选择物品使得它们的总重量至少为背包容量的一半,但不能超过背包的总容量。文章通过一个示例解释了如何解决这个问题,并给出了相应的代码实现。当所有物品的重量都大于背包容量时,问题无法解决;反之,可以找到一种策略填充背包,满足条件。

https://codeforces.ml/contest/1447/problem/C

You have a knapsack with the capacity of WW. There are also nn items, the ii-th one has weight wiwi.

You want to put some of these items into the knapsack in such a way that their total weight CC is at least half of its size, but (obviously) does not exceed it. Formally, CC should satisfy: ⌈W2⌉≤C≤W⌈W2⌉≤C≤W.

Output the list of items you will put into the knapsack or determine that fulfilling the conditions is impossible.

If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights of items in the knapsack.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041≤t≤104). Description of the test cases follows.

The first line of each test case contains integers nn and WW (1≤n≤2000001≤n≤200000, 1≤W≤10181≤W≤1018).

The second line of each test case contains nn integers w1,w2,…,wnw1,w2,…,wn (1≤wi≤1091≤wi≤109) — weights of the items.

The sum of nn over all test cases does not exceed 200000200000.

Output

For each test case, if there is no solution, print a single integer −1−1.

If there exists a solution consisting of mm items, print mm in the first line of the output and mm integers j1j1, j2j2, ..., jmjm (1≤ji≤n1≤ji≤n, all jiji are distinct) in the second line of the output  — indices of the items you would like to pack into the knapsack.

If there are several possible lists of items satisfying the conditions, you can output any. Note that you don't have to maximize the sum of weights items in the knapsack.

Example

input

Copy

3
1 3
3
6 2
19 8 19 69 9 4
7 12
1 1 1 17 1 1 1

output

Copy

1
1
-1
6
1 2 3 5 6 7

Note

In the first test case, you can take the item of weight 33 and fill the knapsack just right.

In the second test case, all the items are larger than the knapsack's capacity. Therefore, the answer is −1−1.

In the third test case, you fill the knapsack exactly in half.

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+5;
const ll mod=1e9+7;
ll n,t,k,m,s,w,sum;
ll ans[maxn];
ll cnt=0;
struct node{
    int x;
    int id;
}a[maxn];
bool cmp(node p,node q)
{
    return p.x<q.x;
}
int main()
{
    cin>>t;
    while(t--)
    {
        cin>>n>>w;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].x;
            a[i].id=i;
        }
        cnt=0;
        sort(a+1,a+1+n,cmp);
        if(a[1].x>w)
            cout<<-1<<endl;
        else
        {
            s=0;

            for(int i=n;i>=1;i--)
            {
                if(s+a[i].x<=w)
                {
                    s+=a[i].x;
                    cnt++;
                    ans[cnt]=a[i].id;
                    if(s*2>=w)
                        break;
                }
            }
            if(s*2<w)
                cout<<-1<<endl;
            else
            {
                cout<<cnt<<endl;
                for(int i=cnt;i>=1;i--)
                {
                    cout<<ans[i]<<" ";
                }
                cout<<endl;
            }
        }
    }
    return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值