Help me in solving AMMEAT problem

My issue

I am able to crack the sample test case, but I am shown a runtime error in the test cases, please help me out:)

My code

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		int t,n;
		long m;
		Scanner sc=new Scanner(System.in);
		t=sc.nextInt();
		while(t-->0){
		    n=sc.nextInt();
		    m=sc.nextLong();
		    int p[]= new int[n];
		    for(int i=0;i<n;i++) p[i]=sc.nextInt();
		    Arrays.sort(p);
		    long sum=0,count=0;
		    for(int i=p.length-1;i>=0;i--){
		        if(sum+p[i]<m){
		            sum+=p[i];
		            count++;
		        }
		        else if(sum+p[i]==m){
		            sum+=p[i];
		            count++;
		            break;
		        }
		    }
		    if(sum<m) count=-1;
		    System.out.println(count);
		}
	}
}

Problem Link: Andrew and the Meatballs Practice Coding Problem - CodeChef

@admiral_c
bro i don’t have good hands on java so plzz refer the following code to debug yours.

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef {
    public static void main(String[] args) throws java.lang.Exception {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            long m = sc.nextLong();
            long[] p = new long[n];
            for (int i = 0; i < n; i++) {
                p[i] = sc.nextLong();
            }
            Arrays.sort(p);
            long s = 0;
            int flag = 1;
            for (int j = n - 1; j >= 0; j--) {
                s += p[j];
                if (s >= m) {
                    flag = 0;
                    System.out.println(n - j);
                    break;
                }
            }
            if (flag == 1)
                System.out.println(-1);
        }
    }
}