CHPINTU - Editorial

“hidden traps”

3 Likes

ohhkay I got it Thanks!!

Wow, probably the best editorial ever read!!! Just loved it.

1 Like

thank you

My solution CodeChef: Practical coding for everyone

why this is wrong ? Pls help

#include
#include
using namespace std;
int main(){
int t,n,m,i,j,sum=0,ans=INT_MAX;
int f[10000],p[10000];
cin>>t;
while(t–){
cin>>n>>m;
for(i=0;i<n;i++){
cin>>f[i];
}
for(j=0;j<n;j++){
cin>>p[j];
}
for(i=1;i<=m;i++)
{
bool found=false;
int sum=0;
for(j=0;j<n;j++)
{
if(f[j]==i){
found=true;
sum+=p[j];
}
}
if(found){
ans = min(ans,sum);
}
}
cout<<ans<<endl;
}
}

what is wrong int this solution
c++:
#include<bits/stdc++.h>
using namespace std;

int main() {

int t;
cin>>t;
vector a(60,0);
vector b(60,0);
vector c(60,0);
int m,n;
int min;
for(int i=0;i<t;i++){

cin>>n>>m;

for(int j=0;j<n;j++){
  c[j]=-1;
  cin>>a[j];
}

for(int j=0;j<n;j++){
cin>>b[j];
}

min=200000;
for(int j=0;j<n;j++){
if(c[a[j]-1]==-1){
c[a[j]-1]=b[j];
}else{
c[a[j]-1]+=b[j];
}
}
for(int j=0;j<m;j++){
if(c[j]>=0 && min>=c[j]){
min=c[j];
}
}
cout<<min<<" "<<endl;

}

return 0;
}

^^^

Thanks a lot!

@everule1 but i have checked even then answer was not accepted

Please use ` `` before and after the code to format it correctly. The code is not comprehensible.

1 Like
#include<climits>
using namespace std;
int main(){
    int t,n,m,i,j,sum=0,ans=INT_MAX;
    int f[10000],p[10000];
    cin>>t;
    while(t--){
        cin>>n>>m;
        for(i=0;i<n;i++){
            cin>>f[i];
        }
        for(j=0;j<n;j++){
            cin>>p[j];
        }
        for(i=1;i<=m;i++)
        {
            bool found=false;
            int sum=0;
            for(j=0;j<n;j++)
            {
                if(f[j]==i){
                    found=true;
                    sum+=p[j];
                }
            }
            if(found){
                ans = min(ans,sum);
            }
        }
        cout<<ans<<endl;
    }
    
}

ans is initialised only once, you need to initialise it for each test case. Generally to avoid stuff like this, you should declare all the variables inside the while loop, since you haven’t used them outside the loop. also iterator variables should be in the loop, not declared seperately. It’s not necessary, but is more readable, and simpler to debug. The scope should be the minimum possible. also use ‘\n’ instead of endl, since it’s not interactive and endl is slow.

3 Likes

What’s wrong in my solution…(in c language)

#include <stdio.h>
#include <limits.h>

int main(void) {
// your code goes here
int t;
scanf("%d", &t);
while(t–)
{
int n, m;
scanf("%d %d", &n, &m);
int type[n+1], price[n+1], hash[m+1];
for(int i=1; i<=m; i++)
hash[i]= 0;

    for(int i=1; i<=n; i++)
        scanf("%d ", &type[i]);
    for(int i=1; i<=n; i++)
        scanf("%d ", &price[i]);
   
   for(int i=1; i<= n; i++)
   {
       int temp= type[i];
       hash[temp]+= price[i];
   }
   int min= INT_MAX;
   for(int i=1; i<= m; i++)
   {
       if(hash[i]<min && hash[i]>0)
       min= hash[i];
   }
   printf("%d\n", min);
}
return 0;

}

Can’t find an error in my code. Please Help!!
#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define rep(i,a,b) for(int i=a;i<b;i++)
int32_t main()
{
IOS;
int t;
cin>>t;
while(t–)
{
int n, m;
cin>>n>>m;
int a[n][2];
int sum[m]={0};
int count[m]={0};
rep(i,0,n) cin>>a[i][0];
rep(i,0,n) cin>>a[i][1];
rep(i,0,n) sum[a[i][0]-1]+=a[i][1];
rep(i,0,n) count[a[i][0]-1]++;
vector<pair<int,int>> vect;
rep(i,0,n) vect.push_back(make_pair(sum[i],count[i]));
sort(vect.begin(),vect.end());
rep(i,0,n)
{
if(vect[i].second!=0)
{
cout<<sum[i]<<endl;
break;
}
}
}
return 0;
}

Price of a fruit can be zero also. So you will have to make an another array which stores the count of fruits. And then iterate over to get the desired ouput.

instead of cout << sum[i] do cout << vect[i].first

2 Likes

For storing the count of the fruits I made array hash[m+1]. In this array: 'i’th index contain sum of all the prices of type ‘i’.

why my code is wrong ??
https://www.codechef.com/viewsolution/30389947

in my another approach what is error in my code
https://www.codechef.com/viewsolution/30505107