fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. Scanner sc = new Scanner(System.in);
  13. int t = sc.nextInt();
  14. while(t-->0)
  15. {
  16. int n= sc.nextInt();
  17. int m = sc.nextInt();
  18. int A[]=new int[n];
  19. int j, res=0;
  20. for(int i=0;i<n;i++)
  21. {
  22. int p=sc.nextInt();
  23. A[i]=p;
  24. }
  25. Arrays.sort(A);
  26. for(j=n-1;j>=0;j--)
  27. {
  28. m=m-A[j];
  29. res++;
  30. if(m<=0)
  31. break;
  32. }
  33. if(m>0)
  34. res=-1;
  35. System.out.println(res);
  36. }
  37. }
  38. }
Success #stdin #stdout 0.09s 380672KB
stdin
1
4 7
1 2 3 4
stdout
2