BMPD - Editorial

PROBLEM LINK:

Buy Masks

Author: Rishabh Rathi
Tester: Rishabh Rathi

DIFFICULTY:

CAKEWALK

PREREQUISITES:

None

PROBLEM:

You go to a shop to buy masks in this pandemic.
Now, there are 3 different types of packets of mask
A packet of 10 masks
A packet of 5 masks
A packet of 1 mask

You are given the costs of each packet. A packet of 10 masks costs Rs. X, a packet of 5 masks costs Rs. Y and a packet of 1 mask costs Rs. Z.

You need to find the minimum sum of rupees you need to pay to buy a total of exactly N masks from the shop.

EXPLANATION:

We can find cost of 1 mask in each packet.

Cost of 1 mask in a packet of 10 masks = X/10
Cost of 1 mask in a packet of 5 masks = Y/5
Cost of 1 mask in a packet of 1 mask = Z

After that, we will keep on buying the packets which have the least mask cost.

SOLUTIONS:

Setter's Solution - Python
t = int(input())

for _ in range(t):
	n, x, y, z = map(int, input().split())

	# cost of 1 mask in each packet
	cost1_10 = x/10
	cost1_5 = y/5
	cost1_1 = z

	total = 0

	# single packet is cheapest
	if (cost1_1<=cost1_5 and cost1_1<=cost1_10):
		total += n*z

	# packet of 5 masks is cheapest
	elif (cost1_5<=cost1_1 and cost1_5<=cost1_10):
		cnt5 = n//5
		total += cnt5*y
		n %= 5

		# for remaining, buy packet of 1 mask
		total += n*z

	# packet of 10 masks is cheapest
	else:
		cnt10 = n//10
		total += cnt10*x
		n %= 10

		# if packet of 5 masks is cheapest
		if (cost1_5<cost1_1):
			cnt5 = n//5
			total += cnt5*y
			n %= 5

		# remaining has to be packet of 1 mask
		total += n*z

	print(total)
Tester's Solution - CPP
#include <bits/stdc++.h>  
using namespace std;

typedef long long ll;

int main() {
	ios_base::sync_with_stdio(0);  
	cin.tie(0);
	ll t, n, x, y, z, total, cnt5, cnt10;
	double cost1_10, cost1_5, cost1_1;

	cin>>t;
	while(t--) {
		cin>>n;
		cin>>x;
		cin>>y;
		cin>>z;

		cost1_10 = (double)x/10;
		cost1_5 = (double)y/5;
		cost1_1 = (double)z;
		total = 0;

		if (cost1_1<=cost1_5 && cost1_1<=cost1_10) {
			total += n*z;
		}
		else if (cost1_5<=cost1_1 && cost1_5<=cost1_10) {
			cnt5 = n/5;
			total += cnt5*y;
			n %= 5;
			total += n*z;
		}
		else {
			cnt10 = n/10;
			total += cnt10*x;
			n %= 10;

			if (cost1_5<cost1_1) {
				cnt5 = n/5;
				total += cnt5*y;
				n %= 5;
			}

			total += n*z;
		}

		cout<<total<<"\n";
	}
	return 0;
}

Feel free to share your approach. In case of any doubt or anything is unclear please ask it in the comment section. Any suggestions are welcomed. :smile: