CHPINTU - Editorial

#include<bits/stdc++.h>
using namespace std;
int main(){

// m types of fruit
// n number
// fi ->  fruit types
// pi -> cost



int t;
cin>>t;
while(t--){
    
    int m , n;
    cin>>n>>m;
    int *fi=new int[n];
    int *pi=new int[n];
    int *ti =new int[m];
    
    for(int i=0;i<n;i++){
        cin>>fi[i];
    }
     for(int i=0;i<n;i++){
        cin>>pi[i];
    }
    
    for(int j=1 ;j<=m;j++){
        ti[j]=0;
    }
    
    int min=0;
    
    for(int i=0 ;i<n;i++){
        ti[fi[i]]+=pi[i];
        
    }
    
    min = ti[1];
    for(int i=2;i<=m;i++){
       // min=ti[i];
        if(min >ti[i] && ti[i]!=0){
            min=ti[i];
        }
        
        
        
    }
    
    cout<<min<<endl;
    
    delete fi;
    delete pi;
    delete ti;
    
}



return 0;

}

what`s wrong in this code

Why t[i] != 0, Price of fruit can be zero.
For these logic you need a flag array to indicate which type of fruits have occurred.

Sorry, bro, I don’t use pointers for arrays
You’ve used delete pointer and pointers for creating an array which is kind of unsettling for me :sweat_smile:
Ohh Yes, and you’ve accessed mth index of t array which is out of bound
So, it’ll give runtime error

opchpintu

This o/p i am getting on my IDE but on codechef IDE it gives me runtime error:-segmentation fault

What’s wrong in my solution? Plz help
I have also considered that price can be 0
#include
#include
#include
#include
using namespace std;
#define ll long long int
int main()
{
ll t;
cin >> t;
while (t–)
{
ll n, m;
cin >> n >> m;
ll mf[m + 1], f[n + 1], p[n + 1];
for (ll i = 0; i <= m; i++)
mf[i] = 0;

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

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

        for (ll i = 1; i <= n; i++)
            mf[f[i]] += p[i];

        set<int> st;
        for (ll i = 1; i <= n; i++)
            st.insert(f[i]);

        ll ans = mf[1];
        for (ll i = 1; i <= m; i++)
        {
            if (st.find(i) != st.end())
                ans = min(ans, mf[i]);
        }

        cout << ans << "\n";
    }
}

I am also getting Run time error but my execution time is 0.002s.Any one know what’s happening?

What’s wrong?
#include

using namespace std;

struct basket

{

int value = 0;

int flag = 0;

};

int main()

{

int t;

cin >> t;

while (t--)

{

    int n, m;

    cin >> n >> m;

    int F[n], P[n];

    struct basket min[m];

    int minB = 1000000;

    for (int i = 0; i < m; ++i)

       cout <<  min[i].value ;

    for (int i = 0; i < n; ++i)

        cin >> F[i];

    for (int i = 0; i < n; ++i)

        cin >> P[i];

    for (int i = 0; i < n; ++i)

    {

        min[F[i]].value += P[i];

        min[F[i]].flag = 1;

    }

    for (int i = 0; i < m; i++)

    {

        if (minB > min[i].value && min[i].flag == 1)

            minB = min[i].value;

    }

    cout << minB << endl;

}

return 0;

}

Hi pro coders, please help me, I am gettng 0 score, despite getting correct outputs :slightly_frowning_face: CodeChef: Practical coding for everyone

Try

2
1 1
1
3
1 1 
1
4

Then try

2
1 1
1
4
1 1
1
3
2 Likes

f[i] can be 0 so your answer will be stored at mf[0] which you’ve not accessed at all.
Probably that’s the error.

i m getting 0, but it works for all cases i can think of


using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int m,n,i,j;
        long long int x=2147483647;
        
        cin>>n>>m;
        int fruit[m];
        int present[m]; 
        
        int p[n],f[n];
        for(i=1;i<=m;i++)
            fruit[i]=0;
            
        for(i=0;i<n;i++)
            cin>>f[i];
            
        for(i=0;i<n;i++)
            cin>>p[i];
            
        for(i=0;i<n;i++)
        {
            fruit[f[i]] +=p[i];
            present[f[i]]=1;
        }
        
        for(i=1;i<=m;i++)
        {
            if(fruit[i]<x && present[i]==1)
            {
                x= fruit[i];
            }
                
        }
        cout<<x<<endl;
    }
}

variables are 0 indexed. accessing fruit[m] when you’ve declared it as size of m is out of bounds.

Got it,Thanks :grinning:

can someone pls tell me why it is showing wrong answer while submitting though it’s giving correct op with custom input. Your help is appreciated
coded in c
#include <stdio.h>

int main()
{
//printf(“Hi there\n”);
int t,m,n,i,j,s,l;
scanf("%d",&t);
for(i=0;i<t;i++)
{

scanf("%d %d",&m,&n);
int f[m],c[m],p[n];

for(j=0;j<m;j++)
{scanf("%d",&f[j]);}
for(j=0;j<m;j++)
{scanf("%d",&c[j]);}

for(s=0;s<n;s++)
p[s]=0;

for(s=0;s<n;s++)
{for(j=0;j<m;j++)
{if(s+1==f[j])
p[s]+=c[j];}
}
l=p[0];
for(s=0;s<n;s++)
{if(l>p[s]&&p[s]>0)
l=p[s];}
printf("%d\n",l);
}
return 0;
}

got it, but it works maybe its accessing the next block. although i have made the changes but its still wrong.

At least post your new code?

yes sry. n could you give a test case where it might not work


using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long int m,n,i,j;
        long long int x=2147483647;
        
        cin>>n>>m;
        int fruit[m+1];
        
        int present[m+1];
        fruit[0]=0;
        
        int p[n],f[n];
        for(i=0;i<=m;i++)
            fruit[i]=0;
            
        for(i=0;i<n;i++)
            cin>>f[i];
            
        for(i=0;i<n;i++)
            cin>>p[i];
            
        for(i=0;i<n;i++)
        {
            fruit[f[i]] +=p[i];
            present[f[i]]=1;
        }
        
        for(i=0;i<=m;i++)
        {
            if(fruit[i]<x && present[i]==1)
            {
                x= fruit[i];
            }

                
        }
        cout<<x<<endl;
      
    }
}

i have even tested for 0 priced case. what is wrong in my soln

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];
   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=c[1];
   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);

               }

}

There’s no testcase per se, but you haven’t initialised present. It holds garbage values. Also you can just use

int fruit[m+1]={0};
int present[m+1]={0};

It’s also much faster.

Please format it correctly
use ``` above and below the code to format it correctly