PROBLEM LINK:
Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4
Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093
DIFFICULTY:
TBD
PREREQUISITES:
None
PROBLEM:
Chef will buy N movie tickets and M buckets of popcorn.
Each ticket costs A, each popcorn bucket costs B.
There’s also a combo of one ticket and one popcorn bucket for C.
It’s guaranteed that \max(A, B) \lt C \le A+B.
Find the minimum amount Chef needs to spend.
EXPLANATION:
Because of the condition \max(A, B) \lt C \le A+B, it’s always optimal to buy the combo when it’s possible to do so, i.e. when we need both a ticket and a bucket of popcorn.
So, let X = \min(N, M).
X denotes the largest number of combos we can buy.
The answer is hence obtained by summing the following:
- X combos, costing C each.
The total is X\cdot C. - N-X individual tickets, costing A each.
The total is (N-X)\cdot A. - M-X individual buckets, costing B each.
The total is (M-X)\cdot B.
Print their sum as the answer.
TIME COMPLEXITY:
\mathcal{O}(1) per testcase.
CODE:
Editorialist's code (PyPy3)
for _ in range(int(input())):
n, m, a, b, c = map(int, input().split())
x = min(n, m)
print(x*c + (n-x)*a + (m-x)*b)