CHPINTU - Editorial

yes i will do formatting but what is the error in this code. why is it not being accepted?

You need to format it for me to be able to actually understand it.

thanks!!. apparently that was the mistake. it works now

done.please check now

I see two problems taking a quick look. One is ‘c’ is has a size of m and b[i] might be m, hence out of bounds. Secondly, what if there is no basket of fruit 1. Then min=c[1] will be wrong. Instead just put min=1e9. So that any fruit will cost lesser. Max price is only 2500, so anything above that will work. Also, work on your indentation. Neat code is always a plus.

Here, corrected it and commented the changes with proper indentation

#include<stdio.h>
int main(void) {
    int t,min;
    scanf("%d",&t);
    while(t--){
        int N,M,i,j;
        scanf("%d%d",&N,&M);
        
        int a[N],b[N],c[M+1];//accessing c[M] was out of bounds before
        for(i=0;i<N;i++)
            scanf("%d",&a[i]);
            
        for(i=0;i<N;i++)
            scanf("%d",&b[i]);
            
        for(i=1;i<=M;i++)
            c[i]=0;
            
        for(i=0;i<N;i++)
            c[a[i]]=c[a[i]]+b[i];
            
        min=3000;//anything greater than 2500 should work
        for(i=1;i<=M;i++){
            for(j=0;j<N;j++){
               if(a[j]==i && c[i]>=b[j] && c[i]<min)
                    min=c[i];
            }
        }
        printf("%d\n",min);
    }

}

This is what neatly indented code looks like.

1 Like

What’s the problem
Please help…

#include <bits/stdc++.h>
using namespace std;
#define For(i,n) for(i=1;i<=n;i++)
#define Int long long
int main()
{
Int n,m,t,i;
cin>>t;
while(t–)
{

     cin>>n>>m;
     Int f[n+1];
     Int p[n+1];
     Int v[m+1]={0};
     
     For(i,n) cin>>f[i];
     For(i,n) 
              {
                cin>>p[i];
                v[f[i]]+=p[i];
              }
     
     Int min=LLONG_MAX;
     For(i,m)
     if(v[i]<min && v[i]!=0)
     min=v[i];
     
     cout<<min<<'\n';
 }

return 0;

}

thanks a ton ! i got it

Please help me out I am not able to understand why my code does not work

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

You forgot the brackets after split
n,m = map(int,input().split)
It’s actually
n,m = map(int,input().split())

1 Like

Thank You.

can anyone tell me where i went wrong please:
python:

t=int(input())
z=[]
for i in range(t):
x=input()
x=x.split(" “)
n=int(x[0])
m=int(x[1])
f=input()
f=f.split(” “)
p=input()
p=p.split(” ")
for i in range(n):
f[i]=int(f[i])
p[i]=int(p[i])
for i in range(m):
count=0
for j in range(n):
if(f[j]==i+1):
count+=p[j]
z.append(count)
z.sort()
print(z[1])

and it gave correct answer though

Hi,
The below code covers all scenarios but I am getting wrong answer when I submit it.
Can anyone point out what is the mistake?

    #include<iostream>
    using namespace std;

    int main() {
    	#ifndef ONLINE_JUDGE
        freopen("P01_chpintu_input.txt", "r", stdin);
        freopen("P01_chpintu_output.txt", "w", stdout);
        #endif

        int testCount, n, m;
        int totalPrice[51];
        bool isZero[51];
        cin >> testCount;

        while(testCount){
            cin >> n >> m;

            int F[n + 1] = {0};
            int P[n + 1] = {0};
    		
            // Making the input arrays '0' for iterative test cases
            for(int i = 1; i <= m; i++){
                totalPrice[i] = 0;
                isZero[i] = false;
            }

            // Reading inputs for fruits id
            for(int i = 1; i <= n; i++){
                cin >> F[i];
            }

            // Reading inputs for price for fruit[i] id and calculate total price for each type of fruit
            for(int i = 1; i <= n; i++){
                cin >> P[i];
                totalPrice[F[i]] += P[i];

                // Zero price for a basket
                isZero[F[i]] = P[i] == 0 ? true : false;
            }

            // Minimum element
            int minValue = totalPrice[1];
            for(int i = 2; i <= m; i++)
            {
                if(totalPrice[i] == 0) {
                    if(isZero[i] == true) {
                        minValue = 0;
                        break;
                    }
                } else {
                    if(totalPrice[i] < minValue) {
                        minValue = totalPrice[i];
                    }
                }
            }
            cout << minValue << "\n";
            --testCount;
        }
        return 0;
    }`

Say, what would be the output for the test case

1
3 2
1 1 2
0 3 2

I GOT AC ON THIS*

#include <bits/stdc++.h>
using namespace std;
int main()
{
int t; cin>>t;
while(t–)
{
int flag = 0;
int n,m; cin>>n>>m;
int f[n+1],p[n+1];
int sum = 0;
int min = 99999;

    for(int i=1;i<=n;i++) cin>>f[i];
    for(int i=1;i<=n;i++) cin>>p[i];

    for(int i = 1 ; i<=m ; i++)
    {
        //cout<<i<<endl;
    for(int j= 1 ; j<=n ; j++) {
        if(i == f[j])
        {
            flag = 1;
            sum = sum + p[j];
         //   cout<<"s1 "<<sum<<endl;
       }
    }
    if(flag == 1)
    {
        if(sum<min)
       {
         //  cout<<"s"<<sum<<endl;
            min = sum;
          //  cout<<"m"<<min<<endl;
       }
        sum = 0;
        flag = 0;
    }

    }
    cout<<min<<endl;
 }

}

Program Output : 2
Expected Output (as per my understanding) : 2

I’m so sorry I misunderstood your code.
Can you try

1
2 2
2 2
1 2

the output should be

3

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