#P15034. [UOI 2021 II Stage] 奇迹之地
[UOI 2021 II Stage] 奇迹之地
Problem Description
Recently, Cossack Moustache heard about an interesting place called the Land of Miracles, where trees grow with money on them. He decided to plant money trees and harvest them in the second year.
When Moustache returned to the Land of Miracles for an inspection, he found that each tree had grown at least one coin, and the number of coins on the -th tree was . He felt that harvesting by hand would take too much time, so he built a machine. The machine can perform the following operation multiple times:
- Choose a positive integer .
- Find the first tree (i.e., the one with the smallest index) whose current number of coins is at least .
- Take away coins from that tree.
However, in the money tree maintenance manual, Cossack Moustache learned that after harvesting, each tree must have at least one coin left; otherwise, they will not bear fruit next year.
Now Cossack Moustache wants to know: after some number of operations, what is the maximum number of coins this machine can harvest.
Note that the chosen number may be different in different operations.
Input Format
The first line contains an integer () — the number of trees in the Land of Miracles.
The second line contains integers () — the initial number of coins on the trees.
Output Format
Output one integer — the maximum number of coins the machine can collect after a sequence of operations, while ensuring that each tree has at least one coin left.
4
1 4 2 3
3
6
1 2 2 3 4 2
0
Hint
Sample Explanation
In the second sample, it is impossible to choose a such that at least one coin can be collected, while still leaving a coin on every tree.
Scoring Rules
- (4 points): .
- (8 points): .
- (7 points): .
- (13 points): .
- (7 points): .
- (19 points): .
- (42 points): No additional constraints.
Translated by DeepSeek V3.
Translated by ChatGPT 5