#include <stdio.h>
#include<stdlib.h>
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m;
scanf("%d %d", &n, &m);
int f[n], p[n];
for(int i=0;i<n;i++)
scanf("%d", &f[i]);
for(int i=0;i<n;i++)
scanf("%d", &p[i]);
//to check the count
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(f[i]==f[j])
{
p[i]=p[i]+p[j];
p[j]=-2;
}
}
}
int min=p[0];
for(int i=1;i<n;i++)
{
if(min>p[i])
{
if(p[i]==0)
continue;
else
min=p[i];
}
}
printf("%d\n", min);
}
return 0;
}
My code says WA. I am damn sure the problem is with nested âforâ loops. Please someone help me!
Keeping the code same and replacing the nested for loops and using same array to sum up elements the code worked perfect!
What may be the issue? I am stuck on this for 3 days. Please someone help.
def fruits(dict1):
arr=[]
for i in range(2):
arr.append(list(map(int,input().split())))
for j in range(n):
dict1[arr[0][j]]+=arr[1][j]
x=min(t for t in dict1.values() if t>0)
return(x)
t=int(input())
for _ in range(t):
n,m=map(int,input().split())
dict1={x+1:0 for x in range(m)}
print(fruits(dict1))
why its not working
indentations are taken
/please find the error in my code i have also taken care of the hidden trap/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(tâ){
int n,m;
cin>>n>>m;
int f[n],p[n],h[51]={0},i,temp=INT_MAX;
for(i=0;i<n;i++)
cin>>f[i];
for(i=0;i<n;i++){
cin>>p[i];
h[f[i]]+=p[i];
}
for(i=0;i<n;i++)
if(temp>h[f[i]])
temp=h[f[i]];
cout<<temp<<endl;
}
return 0;
}
what is wrong in this------------
tc = int(input())
while tc:
n, m = list(map(int, input().split()))
fruits = list(map(int, input().split()))
prices = list(map(int, input().split()))
presence_array = [0]*m
for i in range(1, m+1):
if i in fruits:
presence_array[i-1] = True
else:
presence_array[i-1] = False
c = presence_array.count(True)
new_array = [0]*c
for i in range(len(fruits)):
new_array[fruits[i]-1] += prices[i]
print(min(new_array))
tc -= 1
in your nested loop make the following changes to your inner loop:
for (int j=0;j<n;j++)
{
if(f[i]==f[j])
{
p[i]=p[i]+p[j];// i also recommend to use a new array instead of using p[i]
}
}
also when finding the minimum element in the price array donât skip over zero as a type of fruit may also have zero price (see the constraints carefully)
Please, can you help me, I am getting zero- socre , but i am getting the correct output.
PLEASE HELP .
t=int(input())
n_barrrier,n_type=map(int,input().split())
lis1=list(map(int,input().split()))
lis2=list(map(int,input().split()))
lis3=[]
for i in range(t):
for i in range(1,n_type+1):
sum=0
c=False
for j in range(n_barrrier):
if i==lis1[j]:
sum+=lis2[j]
lis1[j]=0
c=True
if c==True:
lis3.append(sum)
print(min(lis3))
why do i get SIGSEV here in this code and how to deal with this I am new to this platform.
int main() {
int t;
cin>>t;
while(t!=0){
int n,m;
cin>>n>>m;
int f[n],p[n];
for(int i=0;i<n;i++)
cin>>f[i];
for(int i=0;i<n;i++)
cin>>p[i];
int freq[m+1];
bool avail[m+1];
memset(avail,false,sizeof(avail));
memset(freq,-1,sizeof(freq));
for(int i=0;i<n;i++){
freq[f[i]]+=p[i];
avail[f[i]]=true;
}
int min = INT_MAX;
for(int i=0;i<=m;i++){
if(freq[i]<min && avail[i]==true){
min=freq[i];
}
}
cout<<min<<"\n";
t--;
}
return 0;
}
Your code looks perfectly correct. Have you provided custom input? Except freq is initialised with -1, it should be initialised with 0.
âhidden trapsâ
Price = 0 
Yup but nothing worked for me
Initialized freq with 0
Runtime error :- SIGTSTP
https://www.codechef.com/viewsolution/30571717
I just added the header files and the namespace, and changed freq to 0, Which I am presuming youâve done in your code.
Why just I donât know sometimes it gives sigsegv or sigtstp even i have done the changes suggested by you @everule1
Are you sure youâre providing custom input?
Online IDE by Codechef :- C++14(Gcc 6.3)
further I also compiled it on my IDE(DEV C++) it provided me the correct output .
but the IDE provided by codechef gives the run time error
but it has the condition for price = 0âŚ
It must be cause you dont have any other problems
Or initializing maybe a issue, otherwise your code must pass.