Problem with Kickstart 2020 1A

The link to the question.
I cant understand why the following solution is wrong

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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int long long

int n,b,arr[100000];
int main()
{
    IOS;
    int t; cin>>t;
    int tc=0;

    while(t--)
    {
        tc++;
        //int n;
        cin>>n;
        //int b;
        cin>>b;
        //int arr[n];
        for(int i=0;i<n;i++)
            cin>>arr[i];

        sort(arr,arr+n);

        int sum=0;
        for(int i=0;i<n;i++)
        {
            //sum+=arr[i];
            if(b>=arr[i])
                b-=arr[i];

            else
            {
                cout<<"Case #"<<tc<<": "<<i<<"\n";
                break;
            }
        }

    }
}

but the this code with minor change is right

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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//#define int long long

int n,b,arr[100000];
int main()
{
    IOS;
    int t; cin>>t;
    int tc=0;

    while(t--)
    {
        tc++;
        //int n;
        cin>>n;
        //int b;
        cin>>b;
        //int arr[n];
        for(int i=0;i<n;i++)
            cin>>arr[i];

        sort(arr,arr+n);

        int sum=0;
        int ans=0;
        for(int i=0;i<n;i++)
        {
            //sum+=arr[i];
            if(b>=arr[i])
                {
                    b-=arr[i];
                    ans++;
                }
        }
        cout<<"Case #"<<tc<<": "<<ans<<"\n";


    }
}

Try this case for the incorrect code :
1
2 100
50 50

It wont give any output

1 Like

Try for test Case
n=5;
q=150;
prices are 10,20,30,40,50.
Hope it helps!! :slight_smile: :slight_smile:

1 Like