#P15034. [UOI 2021 II Stage] 奇迹之地

    ID: 16966 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>贪心2021UOI(乌克兰)

[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 nn 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 ii-th tree was aia_i. 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 kk.
  • Find the first tree (i.e., the one with the smallest index) whose current number of coins is at least kk.
  • Take away kk 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 kk may be different in different operations.

Input Format

The first line contains an integer nn (1≤n≤1061 \leq n \leq 10^6) — the number of trees in the Land of Miracles.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \leq a_i \leq 10^9) — 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 kk such that at least one coin can be collected, while still leaving a coin on every tree.

Scoring Rules

  • (4 points): n=2n = 2.
  • (8 points): n=3n = 3.
  • (7 points): ai≤2a_i \leq 2.
  • (13 points): ai≤3a_i \leq 3.
  • (7 points): 10≤ai10 \leq a_i.
  • (19 points): ai,n≤1 000a_i, n \leq 1\,000.
  • (42 points): No additional constraints.

Translated by DeepSeek V3.

Translated by ChatGPT 5