fork(4) download
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. using namespace std;
  5. int main()
  6. {
  7. long long int t,m,p[7],sum;
  8. int i,count,n;
  9. cin>>t;
  10. while(t--)
  11. {
  12.  
  13. count=0;
  14. sum=0;
  15. cin>>n;
  16. cin>>m;
  17. for(i=0;i<n;i++)
  18. cin>>p[i];
  19. vector<long long int> myvector(p,p+n);
  20. sort(myvector.begin(),myvector.end(),greater<int>());
  21. for(vector<long long int>::iterator it=myvector.begin();it!=myvector.end();++it)
  22. {
  23. sum=sum+*it;
  24. count++;
  25. if(sum>=m)
  26. {
  27. break;
  28. }
  29.  
  30. }
  31. if(sum>=m)
  32. cout<<count<<"\n";
  33. else
  34. cout<<-1<<"\n";
  35. }
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 3036KB
stdin
3
4 7
1 2 3 4
4 10
1 2 3 4
4 11
1 2 3 4
stdout
2
4
-1