Andrew and the Meatballs - AMMEAT - Wrong Answer [[Closed]]

After a lot of thinking why this particular way to solve this problem would fail
if anyone coud give me any test case for which this would fail, it would be great help.

Problem link : - AMMEAT Problem - CodeChef

cook your dish here

for tc in range(int(input())):

n,m = map(int,input().split())

li = list(map(int,input().split()))
li.sort(reverse=True)
count = 1
t_balls = 0
if(sum(li)<m):
    count=-1
else:
    for i in range(n):
        
        t_balls += li[i]
        if t_balls >=m:
            count = count + i
            break
    
    print(count)

So, I attempted the question without seeing your code first, and after it worked, I compared our approaches and they were very similar.

The only problem your code has is that your print statement should be outside of the else block. If you see, you are never printing your answer when you set ‘count’ to -1.

So, it will fail for all test cases whose answer is -1. For example:
5 25
4 3 5 2 6

I’m posting my solution below for reference:
https://www.codechef.com/viewsolution/32566993

1 Like

Absolutely works now. Thank you :slight_smile:
closed question.

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	// your code goes here
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
	    int n;
	    long long int m;
	    cin>>n>>m;
	    long long int s[n];
	    for(int i=0;i<n;++i)
	        cin>>s[i];
	    sort(s,s+n);
	     int c=0;
	    for(int i=n-1;i>=0&&m>0;--i)
	    {
	        if(m>=s[i])
	        {
	            c++;
	            m=m-s[i];
	        }
	        
	    }
	    if(m==0)
	    cout<<c<<endl;
	    else
	    cout<<"-1"<<endl;
	}
	return 0;
}

this is my code.IDK what is wrong in this.

@pooja_123a I modified your code you were not considering the case when m<s[i]
https://www.codechef.com/viewsolution/36081389