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
- Keep dailyBerries, the amount eaten on the current day, and totalBerries, the accumulated total. Initially both equal M because day 1 has already happened.
- For each day d = 2, 3, …, N, first increase dailyBerries by d. Only then add this new daily amount to the total.
- 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.
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';
}