A special site for solving fun programming problems and challenges, interested in computer science, programming, basics, data structure and algorithms

 


Buy 2 Get 1 Free Problem Code: SALE2SolvedSubmit (Practice)

There is a sale going on in Chefland. For every 2 items Chef pays for, he gets the third item for free (see sample explanations for more clarity).

It is given that the cost of 1 item is X rupees. Find the minimum money required by Chef to buy at least N items.

Input Format

  • First line will contain T, number of test cases. Then the test cases follow.
  • Each test case contains of a single line of input, two integers N and X.

Output Format

For each test case, output the minimum money required by Chef to buy at least N items.

Constraints

  • 1T1000
  • 1N,X1000

Subtasks

  • Subtask 1 (100 points): Original constraints.

Sample Input 1 

4
3 4
4 2
5 3
6 1

Sample Output 1 

8
6
12
4

Explanation

Test Case 1: Chef wants 3 items where the cost of each item is 4 rupees. To minimise the expenditure, Chef can pay for 2 items and get the third item for free. This way, the total money required is 42=8 rupees.

Test Case 2: To minimise the expenditure, Chef can do the following:

  • Purchase 2 items for 22=4 rupees and get the third one free. Thus, the total number of items is 3.
  • Purchase 1 item for 21=2 rupees. Now Chef has total 4 items.

Thus, the minimum money required to buy 4 items is 4+2=6 rupees.

Test Case 3: To minimise the expenditure, Chef can do the following:

  • Purchase 2 items for 32=6 rupees and get the third one free. Thus, the total number of items is 3.
  • Purchase 2 items for 32=6 rupees. Chef gets a third item for free. Now Chef has total 6 items which is greater than his requirement of 5 items.

Chef wanted to buy at least 5 items. The minimum money required for that is 6+6=12 rupees.

Test Case 4: To minimise the expenditure, Chef can do the following:

  • Purchase 2 items for 12=2 rupees and get the third one free. Thus, the total number of items is 3.
  • Purchase 2 items for 12=2 rupees and get the third one free.. Now Chef has total 6 items.

Thus, the minimum money required to buy 6 items is 2+2=4 rupees.


solution

#include<iostream>
#include<vector>
#include<cmath>
#include<algorithm>
#include<climits>
#include<string>
using namespace std;

class solution
{
public:
	void solve()
	{
		int x, cnt , n, sum, total;
		cin >> x >> n;
		cnt = x / 3;
		sum = x * n;
		total = sum - (cnt * n);
		cout << total << "\n";
	}
};
int main()
{
	solution ss;

	int t;
	cin >> t;
	while (t--)
    {
		ss.solve();
	}

	return 0;
}

No comments:

Post a Comment