CANDYSTORE - Editorial

PROBLEM LINK:

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

Author: notsoloud
Testers: tabr, yash_daga
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

Chef sold Y chocolates today.
He earns 1 rupee each for the first X of them, and then 2 rupees for every one after that.

How much money does Chef earn in total?

EXPLANATION:

Let’s count how many chocolates Chef sells for 1 rupee. Everything else will be sold for 2 rupees.

At most X can be sold for one rupee, and of course at most Y can be sold in total.
So, Chef sells \min(X, Y) chocolates for 1 rupee.
This leaves Y - \min(X, Y) chocolates remaining, which will be sold for 2 rupees each.

So, the answer is

\min(X, Y) + 2\cdot(Y - \min(X, Y))

TIME COMPLEXITY

\mathcal{O}(1) per test case.

CODE:

Editorialist's code (Python)
for _ in range(int(input())):
    x, y = map(int, input().split())
    print(y + max(0, y-x))