-
Notifications
You must be signed in to change notification settings - Fork 0
/
E.cpp
51 lines (49 loc) · 1.08 KB
/
E.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
long long ara[500005];
int main()
{
fastio;
long long t,n,i,q,x,high,low,mid;
cin>>t;
while(t--)
{
cin>>n>>q;
for(i=0;i<n;i++)
cin>>ara[i];
sort(ara,ara+n,greater<long long>());
for(i=1;i<n;i++)
ara[i]+=ara[i-1];
while(q--)
{
cin>>x;
low=0;
high=n-1;
if(x>ara[n-1])
{
cout<<-1<<"\n";
continue;
}
if(x<=ara[0])
{
cout<<1<<"\n";
continue;
}
while(low<=high)
{
mid=(low+high)/2;
if(ara[mid]>=x && ara[mid-1]<x)
{
cout<<mid+1<<"\n";
break;
}
else if(ara[mid]<x)
low=mid+1;
else
high=mid-1;
}
}
}
return 0;
}