CHEFEREN - Editorial

Author: pd_codes
Tester: yash_daga
Editorialist: iceknight1093

TBD

None

PROBLEM:

There are N episodes, numbered starting from 1.
The even-numbered episodes are A minutes long each, while the odd-numbered ones are B minutes long.

How many minutes will it take to watch all N episodes?

EXPLANATION:

Since the constraints are small, it’s possible to simply run a loop from 1 to N and add either A or B to the answer, depending on whether the current episode is odd- or even-numbered.

There’s also a solution in \mathcal{O}(1) by noting that the number of even episodes is exactly \lfloor \frac N2 \rfloor and the number of odd episodes is \lceil \frac N2 \rceil. Hence the answer is

A\left\lfloor \frac N2 \right\rfloor + B\left\lceil \frac N2 \right \rceil

where \lfloor \ \rfloor and \lceil \ \rceil denote the floor and ceiling functions, respectively.

TIME COMPLEXITY

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

CODE:

Editorialist's code (Python)
for _ in range(int(input())):
n, a, b = map(int, input().split())
print(a*(n//2) + b*(n - n//2))