CF924C.Riverside Curio

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Arkady decides to observe a river for nn consecutive days. The river's water level on each day is equal to some real value.

Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the ii -th day this value is equal to mim_{i} .

Define did_{i} as the number of marks strictly under the water level on the ii -th day. You are to find out the minimum possible sum of did_{i} over all days. There are no marks on the channel before the first day.

输入格式

The first line contains a single positive integer nn ( 1<=n<=1051<=n<=10^{5} ) — the number of days.

The second line contains nn space-separated integers m1,m2,...,mnm_{1},m_{2},...,m_{n} ( 0<=mi<i0<=m_{i}<i ) — the number of marks strictly above the water on each day.

输出格式

Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days.

输入输出样例

  • 输入#1

    6
    0 1 0 3 0 2
    

    输出#1

    6
    
  • 输入#2

    5
    0 1 2 1 2
    

    输出#2

    1
    
  • 输入#3

    5
    0 1 1 2 2
    

    输出#3

    0
    

说明/提示

In the first example, the following figure shows an optimal case.

Note that on day 33 , a new mark should be created because if not, there cannot be 33 marks above water on day 44 . The total number of marks underwater is 0+0+2+0+3+1=60+0+2+0+3+1=6 .

In the second example, the following figure shows an optimal case.

首页