Cost of balloons

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 the 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:

  1. Use green-colored balloons for the first problem and purple-colored balloons for the second problem
  2. 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 the problems that each participant solves. Your task is to print the minimum price that you have to pay while purchasing balloons.

Example:

Input:  g = 9, p=6, n=10, v={{1,1},{1,1},{0,1},{0,0},{0,1},{0,0},{0,1},{0,1},{1,1},{0,0}}
Output: 69

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int costBalloons(int gint pint nvector<pair<intint>> v)
{
    int res1 = 0res2 = 0;
    for (int i = 0i < ni++)
    {
        int a = v[i].firstb = v[i].second;

        res1 += a * g + b * p;
        res2 += a * p + b * g;
    }
    int res = min(res1res2);
    return res;
}
int main()
{

    int g = 9p = 6;

    int n = 10;
    vector<pair<intint>> v = {{11}, {11}, {01},
 {00}, {01}, {00}, 
{01}, {01}, {11},
 {00}};

    cout << costBalloons(gpnv<< "\n";

    return 0;
}


No comments:

Post a Comment