Arkady decides to observe a river for n 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 i-th day this value is equal to m_{i}.

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


-----Input-----

The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days.

The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day.


-----Output-----

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


-----Examples-----
Input
6
0 1 0 3 0 2

Output
6

Input
5
0 1 2 1 2

Output
1

Input
5
0 1 1 2 2

Output
0



-----Note-----

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

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

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

Difficulty: interview

Starter code:

Solutions:
n = int(input())
m = [int(i) for i in input().split()]

dm = [0 for i in range(n)]

dm[-1] = m[-1] + 1

for i in range(n - 2, -1, -1):
    dm[i] = max(m[i] + 1, m[i+1], dm[i+1]-1)
#print(dm)
for i in range(1, n):
    dm[i] = max(dm[i], dm[i-1])
#print(dm)

print(sum([dm[i] - 1 - m[i] for i in range(n)]))


def read_data():
    n = int(input().strip())
    a = list(map(int, list(input().strip().split())))
    return n, a

def solve():
    under = [0 for i in range(n)]
    under[-1] = a[-1] + 1
    for i in range(n-2,-1,-1):
        under[i] = max(a[i]+1,a[i+1],under[i+1]-1)
    for i in range(1,n):
        under[i] = max(under[i], under[i-1])
    return sum(under[i] - 1 - a[i] for i in range(n))

n, a = read_data()
print(solve())

n = int(input())
m = input().split()
t = []
for i in range(n):
    m[i] = int(m[i])
    if i == 0:
        t.append(m[i]+1)
    else:
        t.append(max(t[i-1], m[i]+1))
s = t[n-1] - m[n-1] - 1
for i in range(n-2, -1, -1):
    if t[i] < t[i+1]-1:
        t[i] = t[i+1]-1
    s += t[i] - m[i] - 1
print(s)

n = int(input())
m = input().split(' ')
j = 0
mark = [1]
for i in range(1,len(m)) :
    tmp = max(mark[i-1],int(m[i])+1)
    mark.append(tmp)

j+=mark[len(m)-1]-int(m[len(m)-1])-1
for i in range(len(m)-2,-1,-1):
    if mark[i]<mark[i+1]-1:
        mark[i] = mark[i+1]-1
    j+=mark[i]-int(m[i])-1
print(j)


n = int(input())
m = input().split(' ')
j = 0
mark = [1]
for i in range(1,len(m)) :
    tmp = max(mark[i-1],int(m[i])+1)
    mark.append(tmp)

j+=mark[len(m)-1]-int(m[len(m)-1])-1
for i in range(len(m)-2,-1,-1):
    if mark[i]<mark[i+1]-1:
        mark[i] = mark[i+1]-1
    j+=mark[i]-int(m[i])-1
print(j)


n=int(input())
a=input().split(' ')
for i in range(len(a)):
    a[i]=int(a[i])
khat=n*[0]
ted=0
assl=0
khat[0]=1
lol=[0,0]
for i in range(1,len(khat)):
    khat[i]=max([khat[i-1],a[i]+1])
for i in range(len(khat)-2,-1,-1):
    if khat[i]<khat[i+1]-1:
        khat[i]=khat[i+1]-1
    ted=ted+(khat[i]-(a[i]+1))
ted=ted+(khat[n-1]-(a[n-1]+1))
print(ted)


n = int(input())
Ab = input().split()
Un = []
Al = [0]
r = 0
for i in range(n):
    Ab[i] = int(Ab[i])
    Al.append(max(Ab[i]+1,Al[i]))
for i in range(n,-1,-1):
    if Al[i-1] < Al[i] - 1:
        Al[i-1] = Al[i] - 1
for i in range(n):
    Un.append(Al[i+1]-Ab[i]-1)
    r += Un[-1]
print(r)


n = int(input())
Ab = input().split()
Un = []
Al = [0]
r = 0
for i in range(n):
    Ab[i] = int(Ab[i])
    Al.append(max(Ab[i]+1,Al[i]))
for i in range(n,-1,-1):
    if Al[i-1] < Al[i] - 1:
        Al[i-1] = Al[i] - 1
for i in range(n):
    Un.append(Al[i+1]-Ab[i]-1)
    r += Un[-1]
print(r)


# python3
def readline(): return list(map(int, input().split()))


def main():
    n = int(input())
    m = tuple(readline())

    md = [None] * n

    prev = 0
    for i in range(n):
        prev = md[i] = max(prev, m[i])

    prev = 0
    for i in range(n - 1, -1, -1):
        prev -= 1
        prev = md[i] = max(prev, md[i])

    print(sum(md) - sum(m))


main()


n = int(input())

a = list(map(int, input().split()))

ma = [1] * n

for i in range(1, n):

  ma[i] = max(ma[i - 1], a[i] + 1)

for i in range(n - 2, -1, -1):

  ma[i] = max(ma[i + 1] - 1, ma[i])  

print(sum(ma) - sum(a) - n)



# Made By Mostafa_Khaled


n = int(input())
m = list(map(int, input().split()))
starts = sorted([(i - m[i], i) for i in range(n)], reverse = True)
cur = answer = 0
for i in range(n):
    while starts[-1][1] < i:
        starts.pop()
    cur = max(cur, i - starts[-1][0])
    answer += cur - m[i]
print(answer)


n = int(input())
m = list(map(int, input().split()))

t = [1] * n

for i in range(1, n):
    t[i] = max(t[i - 1], m[i] + 1)


for i in range(n - 2, -1, -1):
    t[i] = max(t[i], t[i + 1] - 1)

summ = 0
for i in range(n):
    summ += t[i] - m[i] - 1

print(summ)

k=int(input())
o=[*map(int,input().split())]
list1=[0]*k
y=0
for j in range(k):
    y=max(y,o[j]+1)
    list1[j]=y
for z in range(k-1,0,-1):
    list1[z-1]=max(list1[z-1],list1[z]-1)
list2=zip(list1,o)
print(sum(x-f -1 for x,f in list2 ))

import sys
from collections import deque

data = sys.stdin.read().split()
data_ptr = 0

def data_next():
    nonlocal data_ptr, data
    data_ptr += 1
    return data[data_ptr - 1]

N = int(data_next())
tot_lines = 0
ans = 0
st = deque()
for i in range(N):
    n = int(data_next())
    if n >= tot_lines:
        for j in range(n - tot_lines):
            prev_repeat = st.pop()
            ans += i - prev_repeat
        tot_lines = n + 1
    else:
        ans += tot_lines - n - 1
        st.append(i)
print(ans)


import sys
n = int(sys.stdin.readline().strip())
integer_line = sys.stdin.readline().strip().split()
integers = []
for integer in integer_line:
    integers.append(int(integer))

marks = integers[:]
marks[0] = 1
for index in range(1, len(integers)):
    marks[index] = max(marks[index - 1], marks[index] + 1)
for index in range(len(integers) - 1, 0, -1):
    marks[index - 1] = max(marks[index] - 1, marks[index - 1])
for index in range(len(integers)):
    marks[index] -= integers[index] + 1
sys.stdout.write(str(sum(marks)) + "\n")


n = int(input())
l = list(map(int,input().split()))
dp = [1 for i in range(n)]
m=0
for i in range(1,n):
    dp[i] = max(dp[i-1],l[i]+1)
for i in range(n-2,-1,-1):
    dp[i] = max(dp[i],dp[i+1]-1)
ans=0
for i in range(n):
    ans+=dp[i]-l[i]-1
print(ans)

n=int(input())
mi=list(map(int,input().split()))
dp=[0]*n
dp[0]=1
ans=0
for i in range(1,n):
    dp[i]=max(dp[i-1],mi[i]+1)
for i in range(n-2,-1,-1):
    dp[i] = max(dp[i],dp[i+1]-1)
for i in range(n):
    ans+=dp[i]-mi[i]-1
print(ans)

#include <GOD>
#Pato Boride Azin Khone , Vase Hamine Delet Khune

import sys
import math


n = int(input())
arr = [int(i) for i in input().split()]
tt = [0]*n
for i in range(n):
    if(i != 0):
        tt[i]=max(tt[i-1] , arr[i]+1)
    else:
        tt[i]=arr[i]+1
ss  = 0

for i in range(n-1 , -1 , -1):
    if(i != n-1):
        tt[i]=max(tt[i+1]-1 , tt[i])
        ss+=(tt[i]-arr[i]-1)
    else:
        ss+=(tt[i]-arr[i]-1)
print(ss)