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 E2026 · EasyLoops & simulation

Berries Before Hibernation

National Olympiad — Municipal round (2026 season)

Submission name: BM.cpp

Problem summary

Over N days, a bear eats M berries on day 1. On every day d from 2 onward, it eats d more berries than on the previous day. Find the total eaten over all N days.

Input

Two integers N and M.

Output

The total number of berries.

Constraints

1 ≤ N, M ≤ 100.

Worked example

An independently prepared example for this explanation.

Input

4 3

Output

28

Solution analysis

  1. Keep dailyBerries, the amount eaten on the current day, and totalBerries, the accumulated total. Initially both equal M because day 1 has already happened.
  2. For each day d = 2, 3, …, N, first increase dailyBerries by d. Only then add this new daily amount to the total.
  3. For N = 4 and M = 3, the daily amounts are 3, 5, 8, 12. Their sum is 28. This is a simulation: each iteration performs exactly the change described for that day.

Why it works

Before day d is processed, dailyBerries equals the previous day’s amount and totalBerries includes all earlier days. Adding d gives the correct new amount; adding it to the total preserves the invariant. After day N the total covers every day.

Complexity: O(N) time for N−1 updates; O(1) extra space because only counters are stored.

Watch out. Do not add 1 on day 2: the increase is the current day number. Handle N = 1 without entering the loop. Under the stated bounds int is sufficient.

Commented C++17 solution

An independently written complete program with standard headers.

#include<iostream>
using namespace std;
int main() {
    int days, firstDayBerries;
    cin >> days >> firstDayBerries;
    int dailyBerries = firstDayBerries;
    int totalBerries = dailyBerries;
    for (int day = 2; day <= days; day++) {
        // Today adds its day number to yesterday's amount.
        dailyBerries += day;
        totalBerries += dailyBerries;
    }
    cout << totalBerries << '\n';
}

Source: Arena Olimpiici ↗