CHRBUY - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: iceknight1093
Tester: sushil2006
Editorialist: iceknight1093

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

There are W wooden chairs and P plastic chairs available.
You want to buy K (K \le W+P) of them.

Each wooden chair bought increases stylishness by 2, and each plastic chair bought increases it by 1.
Find the maximum total stylishness you can attain.

EXPLANATION:

Since there are no budget constraints, it’s clearly optimal to prioritize buying wooden chairs as much as possible.
So,

  • If K \le W, then all K chairs we buy can be wooden chairs.
    So, in this case the answer is simply 2K, since each chair bought gives a stylishness of 2.
  • If K \gt W, we’re forced to buy some plastic chairs.
    Since we’ll first buy all the W wooden chairs; we must thus buy exactly (K - W) plastic chairs.
    So, the total stylishness here equals 2\cdot W + 1\cdot (K - W) = K + W.

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (PyPy3)
for _ in range(int(input())):
    w, p, k = map(int, input().split())
    if k <= w: print(2*k)
    else: print(w + k)