 # KITCHENCOST - Editorial

Author: notsoloud
Testers: iceknight1093, satyam_343
Editorialist: iceknight1093

799

None

# PROBLEM:

There are N items in a grocery store, the i-th of which has freshness A_i and cost B_i. What is the cost of buying all items with a freshness of at least X?

# EXPLANATION:

It is enough to do what is asked for:

Iterate i from 1 to N, and if A_i \geq X, add B_i to the answer.
This can be done with the help of a for loop and an if statement.

# TIME COMPLEXITY:

\mathcal{O}(N) per testcase.

# CODE:

Code (Python)
for _ in range(int(input())):
n, x = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0
for i in range(n):
if a[i] >= x: ans += b[i]
print(ans)


during the contest I faced runtime error for solution of this problem, can you explain what was wrong in my code.

#include <stdio.h>

int main(void) {
//my code goes here
int t,n,x,fresh,cost,total;
scanf("%d",&t);
while(t–)
{
total=0;
scanf("%d %d",&n,&x);
for(int i=0;i<n;i++)
{
scanf("%d",&fresh[i]);
}
for(int i=0;i<n;i++)
{
scanf("%d",&cost[i]);
}
while(n–)
{
if(fresh[n]>=x)
{
total= total + cost[n];
}
}
printf("%d\n",total);

}
return 0;


}

Hi, I think the error should be on fresh,cost, the constraints are 1≤N,X≤100.

#include
using namespace std;

int main() {
int t;
cin>>t;
while(t–){
int n,x;
cin>>n>>x;
int* a=new int [n];
int * b=new int [n];
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n;i++)
cin>>b[i];
int cnt=0;
for(int i=0;i<n;i++){
if(a[i]>=x)
{
cnt=cnt+b[i];
}
}
cout<<cnt<<endl;
}
}

#include
using namespace std;

int main() {
int t,x,a,b,n,d=0;
cin>>t;
while(t–)
{
cin>>n>>x;
int a[n];
int b[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
for( int i=0;i<n;i++)
{
cin>>b[i];
}
for( int i=0;i<n;i++)
{
if(a[i]>=x)
{
d=d+b[i];
}
}
cout<<d<<endl;
d=0;
}
return 0;
}