Wrong answer in JAN long challenge prob Watching CPL

Problem :WIPL Problem - CodeChef
Why my subtask 1 Subtask 1 (30 points):. is giving wrong answers.

I have written basic recursive code, so it should pass subtask 1(30 points) but it is giving WAs.Sample test cases are passing.

Can some help me figure out the bug in my code ?? I think I have done everything correctly.

#include <iostream>
using namespace std;
#include<bits/stdc++.h>
typedef long long ll;
ll fun(ll *arr,ll n,ll k,ll h1,ll h2)
{
if(n<=0 )
      {
            return 1e9;
            
      }
       if(h1>=k && h2>=k )
 {
       return n;
 }
      

 
  if(h2>=k && h1<k )
 {
       
       return  fun(arr,n-1,k,h1+arr[n-1],h2);
 }
 
 if(h1>=k && h2<k )
 {
       
       return  fun(arr,n-1,k,h1,h2+arr[n-1]);
 }

return min( fun(arr,n-1,k,h1+arr[n-1],h2) ,  fun(arr,n-1,k,h1,h2+arr[n-1]));

}


int main()
{
ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    ll t;
    cin>>t;
    while(t--)
    {
        ll n,k;cin>>n>>k;
        ll arr[n];
        for(int i=0;i<n;i++)
        {
              cin>>arr[i];
              
        }
        sort(arr,arr+n);
        ll h1=0;
        ll h2=0;
        ll x = fun(arr,n,k,h1,h2);
        
        if(x==1e9)
        {
              cout<<-1<<endl;
        }
        
        else
        cout<<n-x<<endl;
    
    

    }
}

here is what you are doing wrong.
First things first, you are doing something fundamentally wrong for the question.
you are returning a min value.
since your ans needs to be the min value, and your ans is N(constant) -x, clearly x needs to be maximised.
which is true, since your x calculates the number of boxes left, the more number of boxes left, the lesser used.
so you need to be returning the max() and not min(). Im surprised the test cases gave AC on 1(2 after a correction) even after such an error.
Another thing you did wrong was return a false(1e9) before checking for a true(h1&h2 >=k).
Imagine a case where all the boxes have to be used. eg :
5 101
1 1 1 99 100
clearly this is possible. but your code returns a false, because it checks if n <= 0(true) before it checks h1 and h2.

here is the corrected solution : CodeChef: Practical coding for everyone
Hope this helps,
Happy Coding :slight_smile:

1 Like

thank you so much, bro !!!

1 Like