cppAcademy
FROM LEARNING TO COMPETING

Beginner.
Your first challenges.

Bulgarian Informatics Group E · 2007–2026

270 independently explained problems: 54 Municipal, 54 Regional, 105 National Olympiad tasks and 57 Autumn Tournament tasks. Start with the easier tasks and work upwards. Difficulty labels are learning estimates, not official ratings; Group E also includes challenging national-level problems. Years follow Arena’s competition-season labels. Source clarifications are noted on affected tasks.
GROUP E2009 · EasyArithmetic & formulas

Room for a Song

National Olympiad — Municipal round (2009 season)

Submission name: song.cpp

Problem summary

A song lasts n minutes and m seconds. Recording one second needs 16 KB. A disk has k MB free, with 1 MB = 1024 KB. Decide whether it fits; otherwise find the missing KB.

Input

Three integers n, m, k.

Output

YES if the song fits; otherwise NO on the first line and the missing number of KB on the second.

Constraints

1 ≤ n, m, k ≤ 1,000,000,000.

Worked example

An independently prepared example for this explanation.

Input

36 5 6

Output

NO
28496

Solution analysis

  1. Convert both quantities to the same unit. The song has 60n + m seconds, so it needs 16(60n + m) KB. The available capacity is 1024k KB.
  2. Compare the required and available capacities. Equality means the song fits. If required is larger, subtraction gives exactly the shortage.
  3. For 36 5 6, the duration is 2165 seconds, requiring 34640 KB. The disk has 6144 KB, leaving a shortage of 28496 KB.

Why it works

The conversion formulas give the exact required and available storage in KB. The song fits precisely when required ≤ available; otherwise required − available is the additional storage needed.

Complexity: O(1) time and O(1) extra space.

Watch out. Use 64-bit integers before multiplication. Do not assume m < 60: the published bounds allow larger values.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
using namespace std;
int main() {
    long long minutes, seconds, megabytes;
    cin >> minutes >> seconds >> megabytes;
    // Convert both capacities to KB before comparing them.
    long long required = (60 * minutes + seconds) * 16;
    long long available = megabytes * 1024;
    if (required <= available) {
        cout << "YES\n";
    } else {
        cout << "NO\n" << required - available << '\n';
    }
}

Source: Arena Olimpiici ↗