Pintu and Fruits - CHPINTU – CODECHEF MARCH LONG CHALLENGE (EASY)
Pintu and Fruits Problem
Code: CHPINTU – Codechef MArch
Long challenge (Easy)
Given m different types of fruits, their prices. We have to
find the minimum price that chef needs to pay to buy at least 1 type of fruit.
Example:
If a testcase is like this:
1
6 4
1 2 1 2 3 4
0 1 0 1 5 2
->The sum of all the buckets of type 1= 0+0=0
-> The sum of all the buckets of type 2=1+1=2
-> The sum of all the buckets of type 3=5
-> The sum of all the buckets of type 4=2
So, our answer should be 0 as it is the minimum price chef
must pay.
So, look at the code for further clarification
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the
class is public. */
class Codechef
{
public
static void main (String[] args) throws java.lang.Exception
{
Scanner
s=new Scanner(System.in);
int
t;
t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
int m=s.nextInt();
int []f=new int[n+1];
int[]p=new int[n+1];
int []min=new int[m+1];
for(int i=1;i<m+1;i++)
min[i]=-1; // as price 0 is also possible
we should assign all the prices to -1 initially
for(int i=1;i<n+1;i++)
f[i]=s.nextInt();
for(int i=1;i<n+1;i++)
p[i]=s.nextInt();
for(int i=1;i<n+1;i++)
{
int j=f[i];
if(min[j]==-1)
min[j]=p[i];
else
min[j]=min[j]+p[i];
}
for(int i=1;i<m+1;i++) //sorting the prices using
bubble sort
{
for(int j=i+1;j<m+1;j++)
{
if(min[i]>min[j])
{
int swap=min[j];
min[j]=min[i];
min[i]=swap;
}
}
}
for(int i=1;i<m+1;i++)
{
if(min[i]>=0){
System.out.println(min[i]);
break;
}
}
}
s.close();
}
}
Comments
Post a Comment