A Game with Marbles
Time limit1sMemory limit128 MB
Each move takes one marble from a bowl and, if it is not bowl 1, adds one marble to every lower-numbered bowl; count the total moves until all bowls are empty.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Math, Combinatorics
- Solved
- No attempts yet
Problem
There are bowls numbered from to . Initially, bowl contains marbles.
One move consists of removing a single marble from some bowl. When a marble is removed from bowl with , one marble is added to each of the bowls . Removing a marble from bowl adds no new marbles anywhere. The game ends once every bowl is empty.
Determine how many moves are needed to finish the game. You may assume the supply of marbles is unlimited and every bowl is large enough, so that every possible move can be performed.
Input
The input contains several test cases. Each test case begins with a line containing one integer (), the number of bowls. The next line contains integers (), where is the number of marbles in bowl at the start.
The last test case is followed by a line containing a single .
Output
For each test case, print a single line with the number of moves needed to finish the game. This number is guaranteed to fit in a signed 64-bit integer.