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

answer = int(0)
count = int(0)
c = lis[n-1]
for i in range(n-1, 0, -1):
    
    if i+1 < n and lis[i] != lis[i+1]:
        
        if lis[i] - count > 0:
            answer += lis[i] - count
            c = lis[i] - count
        else:
             answer -= lis[i]
             break
    else:  
        answer += c
    count += 1
print(answer)