Help me in solving AMMEAT problem

My issue

Couldn’t get the mistake in my code

My code

#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
        long long n,m;
        cin>>n>>m;
        long long p[n];
        for(int i=0;i<n;i++){
            cin>>p[i];
        }
        sort(p,p+n,greater<int>());
        int sum=0,count=0;
        for(int i=0;i<n;i++){
    sum+=p[i];
    if(sum>=m){
        count=i+1;
        break;
    }
        } 
        if(count==0){
             cout<<-1<<endl;
        }else
            cout<<count<<endl;
        
    }
return 0;
}

Learning course: Sorting using C++
Problem Link: Andrew and the Meatballs Practice Problem in - CodeChef

@code_2004
i have corrected your code .
use long long int in all .

#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin>>t;
    while(t--){
        long long n,m;
        cin>>n>>m;
        long long p[n];
        for(int i=0;i<n;i++){
            cin>>p[i];
        }
        sort(p,p+n,greater<long long int>());
        long long int sum=0,count=0;
        for(int i=0;i<n;i++){
    sum+=p[i];
    if(sum>=m){
        count=i+1;
        break;
    }
        } 
        if(count==0){
             cout<<-1<<endl;
        }else
            cout<<count<<endl;
        
    }
return 0;
}
1 Like