You have a knapsack with the capacity of W. There are also n items, the i-th one has weight wi.
You want to put some of these items into the knapsack in such a way that their total weight C is at least half of its size, but (obviously) does not exceed it. Formally, C should satisfy: ⌈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 t (1≤t≤104). Description of the test cases follows.
The first line of each test case contains integers n and W (1≤n≤200000, 1≤W≤1018).
The second line of each test case contains n integers w1,w2,…,wn (1≤wi≤109) — weights of the items.
The sum of n over all test cases does not exceed 200000.
Output
For each test case, if there is no solution, print a single integer −1.
If there exists a solution consisting of m items, print m in the first line of the output and m integers j1, j2, …, jm (1≤ji≤n, all ji 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
inputCopy
3
1 3
3
6 2
19 8 19 69 9 4
7 12
1 1 1 17 1 1 1
outputCopy
1
1
-1
6
1 2 3 5 6 7
Note
In the first test case, you can take the item of weight 3 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.
In the third test case, you fill the knapsack exactly in half.
选择几个数字的位置,使他们的和大于⌈W2⌉,小于W,将数字排序后,按大到小选择,如果大于W继续找,一直找到加起来大于⌈W2⌉的位置为止。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <functional>
#include <vector>
#include <stack>
#include <set>
#include <bitset>
#define int long long
using namespace std;
typedef long long ll;
const int maxn=2e5+50;
const int inf=0x3f3f3f3f3f3f3f3f;
const int mod=1e9+7;
const int HASH=131;
struct node
{
int wei,id;
friend bool operator<(node a,node b)
{
return a.wei>b.wei;
}
}w[maxn];
int vis[maxn];
signed main()
{
int t;
cin>>t;
while(t--)
{
int type,weight;
cin>>type>>weight;
for(int i=1;i<=type;i++)
{
vis[i]=0;
}
for(int i=1;i<=type;i++)
{
cin>>w[i].wei;
w[i].id=i;
}
sort(w+1,w+1+type);
int ans=0;
int cnt=0;
for(int i=1;i<=type;i++)
{
if(ans+w[i].wei<=weight)
{
cnt++;
ans+=w[i].wei;
vis[i]=1;
}
}
if(weight%2==1) weight++;
if(cnt==0||ans<weight/2) cout<<-1<<endl;
else
{
cout<<cnt<<endl;
for(int i=1;i<=type;i++)
{
if(vis[i])
cout<<w[i].id<<' ';
}
cout<<endl;
}
}
return 0;
}