Buy 2 Get 1 Free Problem Code: SALE2Submit (Practice)
There is a sale going on in Chefland. For every items Chef pays for, he gets the third item for free (see sample explanations for more clarity).
It is given that the cost of item is rupees. Find the minimum money required by Chef to buy at least items.
Input Format
- First line will contain , number of test cases. Then the test cases follow.
- Each test case contains of a single line of input, two integers and .
Output Format
For each test case, output the minimum money required by Chef to buy at least items.
Constraints
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 : Chef wants items where the cost of each item is rupees. To minimise the expenditure, Chef can pay for items and get the third item for free. This way, the total money required is rupees.
Test Case : To minimise the expenditure, Chef can do the following:
- Purchase items for rupees and get the third one free. Thus, the total number of items is .
- Purchase item for rupees. Now Chef has total items.
Thus, the minimum money required to buy items is rupees.
Test Case : To minimise the expenditure, Chef can do the following:
- Purchase items for rupees and get the third one free. Thus, the total number of items is .
- Purchase items for rupees. Chef gets a third item for free. Now Chef has total items which is greater than his requirement of items.
Chef wanted to buy at least items. The minimum money required for that is rupees.
Test Case : To minimise the expenditure, Chef can do the following:
- Purchase items for rupees and get the third one free. Thus, the total number of items is .
- Purchase items for rupees and get the third one free.. Now Chef has total items.
Thus, the minimum money required to buy items is 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