fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. #define yes cout << "YES\n";
  6. #define no cout << "NO\n";
  7.  
  8.  
  9. void FastIO() {
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(nullptr);
  12. cout.tie(nullptr);
  13. }
  14.  
  15. void solve(){
  16. int n,q;
  17. cin >> n >> q;
  18.  
  19. vector<int>a(n);
  20.  
  21. for(auto& x : a) cin >> x;
  22.  
  23. sort(a.rbegin(), a.rend());
  24.  
  25. for(int i = 1; i < n; i++){
  26. a[i] += a[i-1];
  27. }
  28.  
  29. while(q--){
  30. int x;
  31. cin >> x;
  32.  
  33. int l = 0, r = n-1, ans = -1, mid;
  34.  
  35. while(l<=r){
  36. mid = (l+r)/2;
  37. if(a[mid] >= x){
  38. ans = mid;
  39. r = mid - 1;
  40. }
  41. else{
  42. l = mid + 1;
  43. }
  44. }
  45. if(ans == -1) cout << ans << '\n';
  46. else cout << ans+1 << '\n';
  47. }
  48. }
  49.  
  50.  
  51. signed main(){
  52. FastIO();
  53.  
  54. int t = 1;
  55. cin >> t;
  56.  
  57. while (t--){
  58. solve();
  59. }
  60. return 0;
  61. }
Success #stdin #stdout 0.01s 5320KB
stdin
3
8 7
4 3 3 1 1 4 5 9
1
10
50
14
15
22
30
4 1
1 2 3 4
3
1 2
5
4
6
stdout
1
2
-1
2
3
4
8
1
1
-1