CHPINTU - Editorial

I am getting 0 as output as the values of F[2] and P[2] are not provided in input.
As per the constraints shouldn’t there be 3 values in lines 3, 4 ?

God, I’m being stupid. I haven’t slept for 80 hours. It doesn’t work for 2 either.

Hi @everule1,

I was able to identify the issue in my solution with hiss testcase.
I was using minVal = totalPrice[1] (total price of the first fruit), as the base values before starting the comparison and thus when the 1st fruit has no baskets, the default value (0) is taken as the price of 1st fruit and thus the output is zero which is incorrect.

I update the code to use the below conditions and now the code has been accepted.

       // Minimum element
        int minValue = 1e9;
        for(int i = 1; i <= m; i++)

Solution Link : CodeChef: Practical coding for everyone

Thanks for your help.

1 Like

rashmi jain, thanks for the big help.

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

int a[50] , b[50] ,t ,m , n , i ;

void minFunction ( int a[] , int b[] , int c[] , int n , int m)
{

         for( i=0 ; i<n ; i++)
        c[a[i]-1]=c[a[i]-1] + b[i];

          sort (c , c+m);

      for( i=0 ; i<m ; i++)
        {
            if(c[i] != 0)
            {  cout<<c[i]<<endl;
                break;
                }

        }

        if(i==m)
            cout<<0;
      return ;
}



int main()
{   ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        int c[m];
        for(i=0 ; i<m ; i++)
        c[i]=0;

        for(i=0 ; i<n ; i++)
            cin>>a[i];

        for(i=0 ; i<n ; i++)
            cin>>b[i];

        minFunction ( a , b , c , n , m);
    }

    return 0;
}


what is wrong in it?

https://www.codechef.com/viewsolution/30781808
why the solution is not getting accepted?

“Hidden traps”

Probably “Hidden traps”.

I didn’t get this hidden pitfall. Why dosen’t a positive price ensure a type of fruit having atleast one basket. If the price of baskets are 0 then total price will also be 0.

It does, but that’s not the claim made here. The claim is that a fruit which has a price of 0 doesn’t mean there’s no basket with that fruit, as can be shown by the testcase

1
2 2
1 2
1 0

I get it now. Thanks!

What must be the output here?
I guess - 0?

Can you help me a bit where am I going wrong?

https://www.codechef.com/viewsolution/30798968

Try

1
2 1
1 1
1 1

The correct answer is

2

My code gives 2 too.
But it gives W.A.

Ok it seems I misunderstood
Try

1
3 2
1 2 1
1 2 3

The answer should be

2

I get it now. Also, can you tell me how did you think of such a test case? I am a beginner right now. How did you come up with such a test case? And how should I proceed thinking about test cases?

   /////////////////////////
  //  Author : k_ma2111  //
 //  Date : 02-04-2020  //
/////////////////////////

#include <bits/stdc++.h>
#define For(i , a , b) for( int(i)=(a) ; i < (b) ; i++)
#define rFor(i , a ,b) for( int(i)=(a) ; i>(b) ; i-- )
using namespace std;

int t,n,m,f[60],p[60],cost[60],minFlag;
bool check[55];


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin>>t;
    while(t--)
    {
        cin>>n>>m;
        memset(check , false , sizeof(check));
        memset(cost , 0 , sizeof(cost));
        For(i , 0 , n)
        { cin>>f[i];
          check[ f[i]-1 ] = true;
        }
        For(i , 0 , n)
        cin>>p[i];

        For(i , 0 , n)
        {
            cost[ f[i]-1 ] += p[i] ;
        }

        minFlag = cost[0];

        for(int j=0 ; (j<m && (check[j]!= false) ) ; j++)
        {
            if(cost[j] <= minFlag)
            minFlag = cost[j];

        }

        cout<<minFlag<<endl;

    }


    return 0;
}

What is wrong with this code? I have taken in account that cost of a particular basket fruit can be zero if a type of fruit has atleat one basket?

Try the test case

1
2 3
2 3
3 4

Thanks , Now got green :slight_smile:

what is wrong in my code…

#include<bits/stdc++.h>
using namespace std;
void fun()
{
int m,n,i,j,sum=0,a1=1000000;
cin>>n>>m;
int f[n],p[n];
for(i=0;i<n;i++)
cin>>f[i];
for(i=0;i<n;i++)
cin>>p[i];

    for(i=1;i<=m;i++)
    {sum=0;
        for(j=0;j<n;j++)
        {
            if(f[j]==i)
            sum+=p[j];
        }
        if(sum>=0)
        {
            if(sum<a1)
            a1=sum;
        }
      
    }
    cout<<a1<<endl;

}
int main()
{
int t;
cin>>t;

while(t--)
{
   fun();
}

}