CHEFEREN - Editorial

PROBLEM LINK:

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

Author: pd_codes
Tester: yash_daga
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

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))