2545. A Game with Marbles

单点时限: 2.0 sec

内存限制: 256 MB

There are n bowls, numbered from 1 to n. Initially, bowl i contains mi marbles. One game step consists of removing one marble from a bowl. When removing a marble from bowl i (i > 1), one marble is added to each of the first i-1 bowls; if a marble is removed from bowl 1, no new marble is added. The game is finished after each bowl is empty.

Your job is to determine how many game steps are needed to finish the game. You may assume that the supply of marbles is sufficient, and each bowl is large enough, so that each possible game step can be executed.

输入格式

The input contains several test cases. Each test case consists of one line containing one integer n (1 ≤ n ≤ 50), the number of bowls in the game. The following line contains n integers mi (1 ≤ i ≤ n, 0 ≤ mi ≤ 1000), where mi gives the number of marbles in bowl i at the beginning of the game.

The last test case is followed by a line containing 0.

输出格式

For each test case, print one line with the number of game steps needed to finish the game. You may assume that this number fits into a signed 64-bit integer

样例

Input
10
3 3 3 3 3 3 3 3 3 3
5
1 2 3 4 5
0
Output
3069
129

9 人解决,21 人已尝试。

11 份提交通过,共有 55 份提交。

7.3 EMB 奖励。

创建: 15 年,7 月前.

修改: 7 年,2 月前.

最后提交: 2 年,7 月前.

来源: Ulm Local 2008

题目标签