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 E2013 · EasyMinima & maxima

Skating Score

National Olympiad — Municipal round (2013 season)

Submission name: skating.cpp

Problem summary

Six judges give integer scores. If they are all equal, sum all six. Otherwise discard one highest and one lowest score and sum the other four.

Input

Six space-separated integer scores.

Output

The final score.

Constraints

There are six scores; the maximum judge score is 20. The PDF gives no lower bound.

Worked example

An independently prepared example for this explanation.

Input

8 2 3 9 5 6

Output

22

Solution analysis

  1. Accumulate the sum, minimum and maximum of the six scores. Equality of minimum and maximum means every judge agreed.
  2. For unanimous scores print the sum unchanged. Otherwise subtract the minimum and maximum once each, even if an extreme occurs several times.
  3. The sample sum is 33. Removing 2 and 9 leaves 22. If all scores were 8, the special rule would give 48.

Why it works

The extrema coincide exactly when all scores coincide. In the other case subtracting one occurrence of each extreme removes exactly the two required scores, so both branches implement the definition.

Complexity: O(1) time and extra space for six scores.

Watch out. Do not discard every occurrence of the extrema. The unanimous exception keeps all six scores.

Commented C++17 solution

An independently written complete program with standard headers.

#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int score;
    cin >> score;
    int total = score;
    int lowest = score;
    int highest = score;
    for (int i = 1; i < 6; i++) {
        cin >> score;
        total += score;
        lowest = min(lowest, score);
        highest = max(highest, score);
    }
    // Unanimous scores are the exception: discard nothing.
    if (lowest != highest) {
        total -= lowest + highest;
    }
    cout << total << '\n';
}

Source: Arena Olimpiici ↗