Type Here to Get Search Results !

Cost of balloons | HackerEarth Programming Problems and Solutions by CodingHumans |

1
                          
Cost of balloons

Problem

You are conducting a contest at your college. This contest consists of two problems and n participants. You know the problem that a candidate will solve during the contest.

You provide a balloon to a participant after he or she solves a problem. There are only green and purple-colored balloons available in a market. Each problem must have a balloon associated with it as a prize for solving that specific problem. You can distribute balloons to each participant by performing the following operation:

Use green-colored balloons for the first problem and purple-colored balloons for the second problem
Use purple-colored balloons for the first problem and green-colored balloons for the second problem
You are given the cost of each balloon and problems that each participant solve. Your task is to print the minimum price that you have to pay while purchasing balloons.

Input format

First line:  that denotes the number of test cases (1<=T<=10)
For each test case:
First line: Cost of green and purple-colored balloons
Second line:  that denotes the number of participants (1<=N<=10)
Next  lines: Contain the status of users. For example, if the value of the jth  integer in the ith row is 0, then it depicts that the ith participant has not solved the jth  problem. Similarly, if the value of the  integer in the  row is 1 , then it depicts that the participant has solved the  problem.

Output format

For each test case, print the minimum cost that you have to pay to purchase balloons.

Sample Input

2
9 6
10
1 1
1 1
0 1
0 0
0 1
0 0
0 1
0 1
1 1
0 0
1 9
10
0 1
0 0
0 0
0 1
1 0
0 1
0 1
0 0
0 1
0 0

Sample Output

69

14



Recommended: Please try your approach on your integrated development environment (IDE) first, before moving on to the solution.

Few words from CodingHumans : Don't Just copy paste the solution, try to analyze the problem and solve it without looking by taking the the solution as a hint or a reference . Your understanding of the solution matters.

HAVE A GOOD DAY 😁



Solution
( Java )

import java.util.*;
public class baloonsCost {

 public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  int T=sc.nextInt();
        while(T>0){ //number of test cases
  if(T>=1 && T<=10) {
   int c1=sc.nextInt();
   int c2=sc.nextInt();
      int n=sc.nextInt();
      int count1=0;
      int count2=0;
    for(int i=1;i<=n;i++) {
   int f=sc.nextInt();
   if(f==1) {
    count1++;
   }
   int s=sc.nextInt();
   if(s==1) {
    count2++;
   }
  }
   int min1=count1*c1+count2*c2;
   int min2=count1*c2+count2*c1;
   int min=(min1<min2?min1:min2);
            System.out.println(min); // returning mimimum cost
  
  }
  else
   System.out.println(-1);
    T--;//ending the while loop
 }
}
}

If you have any doubts regarding this problem or  need the solution in other programming languages then leave a comment down below .

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad