COLORB - Editorial

PROBLEM LINK:

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

Author: raysh07
Tester: sushil2006
Editorialist: iceknight1093

DIFFICULTY:

Cakewalk

PREREQUISITES:

None

PROBLEM:

You have R red orbs and B blue orbs.
One red and one blue orb can together be exchanged for one green orb.

In the end:

  • Each red orb increases your strength by 1.
  • Each blue orb increases your strength by 2.
  • Each green orb increases your strength by 5.

Find the maximum possible strength you can have in the end.

EXPLANATION:

It’s always optimal to exchange one red and one blue orb for one green orb when it’s possible to do so.
This is because, if we have one red orb and one blue orb with us, they will give a total strength of 1+2 = 3.
Exchanging them for a green orb will give us a strength of 5 instead, which is of course better.

So, with that in mind:

  • Suppose R \ge B.
    Then, we can obtain B green orbs, and we’ll be left with (R - B) red orbs after exchanging for green orbs.
    This gives us a final strength of 5\cdot B + (R - B).
  • Suppose R \lt B.
    Then, we can obtain R green orbs, and we’ll be left with (B - R) blue orbs after exchanging for green orbs.
    This gives us a final strength of 5\cdot R + 2\cdot(B - R).

If you prefer to not do the math, the constraints are also small enough to just simulate all the exchanges using a loop.

TIME COMPLEXITY:

\mathcal{O}(1) per testcase.

CODE:

Editorialist's code (PyPy3)
r, b = map(int, input().split())
if r >= b: print(5*b + (r - b))
else: print(5*r + 2*(b-r))