You went to the store, selling $n$ types of chocolates. There are $a_i$ chocolates of type $i$ in stock.

You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $x_i$ chocolates of type $i$ (clearly, $0 \le x_i \le a_i$), then for all $1 \le j < i$ at least one of the following must hold:  $x_j = 0$ (you bought zero chocolates of type $j$) $x_j < x_i$ (you bought less chocolates of type $j$ than of type $i$) 

For example, the array $x = [0, 0, 1, 2, 10]$ satisfies the requirement above (assuming that all $a_i \ge x_i$), while arrays $x = [0, 1, 0]$, $x = [5, 5]$ and $x = [3, 2]$ don't.

Calculate the maximum number of chocolates you can buy.


-----Input-----

The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$), denoting the number of types of chocolate.

The next line contains $n$ integers $a_i$ ($1 \le a_i \le 10^9$), denoting the number of chocolates of each type.


-----Output-----

Print the maximum number of chocolates you can buy.


-----Examples-----
Input
5
1 2 1 3 6

Output
10
Input
5
3 2 5 4 10

Output
20
Input
4
1 1 1 1

Output
1


-----Note-----

In the first example, it is optimal to buy: $0 + 0 + 1 + 3 + 6$ chocolates.

In the second example, it is optimal to buy: $1 + 2 + 3 + 4 + 10$ chocolates.

In the third example, it is optimal to buy: $0 + 0 + 0 + 1$ chocolates.

Difficulty: EASY
Tags: Implementation

Greedy algorithms
Skill types: Greedy algorithms

Starter code:

Solutions:
n = int(input())
l = list(map(int, input().split()))
l = l[::-1]
ans = 0
ls = 0
flag = 0
for i in range(n):
	if i == 0:
		continue
	if l[i] >= l[i - 1]:
		l[i] = l[i - 1] - 1
		if l[i] == 0:
			flag = i
			break
if flag:
	print(sum(l[:flag]))
else:
	print(sum(l))


t = int(input())
l1 = [int(x) for x in input().split()]
l1.reverse()
i = 0
ans = 0
temp = l1[0] + 1
while i < len(l1):
	temp = max(0, min(l1[i], temp - 1))
	ans += temp
	i += 1
print(ans)


import io, os
input = io.StringIO(os.read(0, os.fstat(0).st_size).decode()).readline
ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
n = ii()
a = li()
(ans, cur) = (0, 10 ** 9 + 1)
while a and cur:
	cur = min(cur - 1, a.pop())
	ans += cur
print(ans)


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = 1
if debug:
	test_count = inp()
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	n = inp()
	a = inlt()
	a.reverse()
	max_c = 1000000001
	ans = 0
	for x in a:
		local_max = max(0, min(x, max_c - 1))
		max_c = local_max
		ans += local_max
	print(ans)


n = int(input())
a = list(map(int, input().split()))
p = a[-1]
c = p
for i in range(n - 2, -1, -1):
	if a[i] >= p:
		c += p - 1
		p -= 1
		if p == 0:
			break
	else:
		c += a[i]
		p = a[i]
print(c)


n = int(input())
l = list(map(int, input().split()))
l.reverse()
for i in range(n - 1):
	if l[i + 1] >= l[i]:
		if l[i] - 1 < 0:
			l[i + 1] = 0
		else:
			l[i + 1] = l[i] - 1
print(sum(l))


import sys
input = sys.stdin.readline
n = int(input())
A = list(map(int, input().split()))
ANS = A[-1]
NOW = A[-1]
for i in range(n - 2, -1, -1):
	NOW = min(NOW - 1, A[i])
	if NOW < 0:
		break
	ANS += NOW
print(ANS)


n = int(input())
a = [int(x) for x in input().split()]
suma = int(0)
for i in range(n - 1, -1, -1):
	if i == n - 1:
		suma += a[i]
	elif a[i] < a[i + 1]:
		suma += a[i]
	else:
		a[i] = a[i + 1] - 1
		if a[i] < 0:
			a[i] = 0
		suma += a[i]
print(suma)


N = int(input())
A = [int(a) for a in input().split()][::-1]
b = A[0] + 1
ans = 0
for a in A:
	b = min(b - 1, a)
	if b <= 0:
		break
	ans += b
print(ans)


total = 0
sum = 0
n = int(input())
a = input().split()
b = []
for i in range(0, n):
	b.append(int(a[i]))
b.reverse()
sum = b[0]
last = b[0]
for i in range(1, n):
	if last is 0:
		break
	if b[i] < last:
		sum = sum + b[i]
		last = b[i]
	else:
		sum = sum + last - 1
		last = last - 1
print(sum)


import sys
from bisect import bisect_left, bisect_right
from collections import deque
inf = float('inf')
mod = 1000000007

def binary(number):
	result = 0
	while number:
		result = result + 1
		number = number & number - 1
	return result

def get_array():
	return list(map(int, sys.stdin.readline().split()))

def get_ints():
	return map(int, sys.stdin.readline().split())
n = int(input())
Arr = get_array()
total = Arr[n - 1]
maximum = Arr[n - 1]
for i in range(n - 2, -1, -1):
	if Arr[i] < maximum:
		total = total + Arr[i]
		maximum = Arr[i]
	else:
		total = total + max(0, maximum - 1)
		maximum = max(0, maximum - 1)
print(total)


n = int(input())
A = list(map(int, input().split()))
answer = 0
can = A[-1] + 1
for i in range(n - 1, -1, -1):
	add = min(can, A[i])
	can = max(0, add - 1)
	answer += add
print(answer)


n = int(input())
a = list(map(int, input().split()))
sum = 0
a.append(a[n - 1] + 1)
for i in range(n - 1, -1, -1):
	if a[i + 1] == 0:
		break
	if a[i] >= a[i + 1]:
		a[i] = a[i + 1] - 1
	sum += a[i]
print(sum)


n = int(input())
array = list(map(int, input().split()))
a = 1000000001
amount = 0
for i in range(len(array)):
	c = array[n - i - 1]
	if c < a:
		amount = amount + c
		a = c
	elif c >= a > 0:
		amount = amount + a - 1
		a = a - 1
print(amount)


n = int(input())
a = list(map(int, input().split()))
i = n - 2
p = a[n - 1]
s = p
while i >= 0 and p >= 1:
	if a[i] < p:
		p = a[i]
		s += p
	else:
		p -= 1
		s += p
	i -= 1
print(s)


import math
n = int(input().lstrip())
chocos = list(map(int, input().lstrip().split()))
res = 0
cur_count = float('inf')
for c in chocos[::-1]:
	cur_count = max(min(cur_count - 1, c), 0)
	res += cur_count
print(res)


n = int(input())
l = list(map(int, input().split()))
count = l[n - 1]
for i in range(n - 2, -1, -1):
	if l[i] < l[i + 1]:
		count += l[i]
	else:
		if l[i + 1] > 0:
			l[i] = l[i + 1] - 1
		else:
			l[i] = l[i + 1]
		count += l[i]
print(count)


n = int(input())
a = list(map(int, input().split()))
r = 10 ** 10
ans = 0
for i in range(n):
	r = max(min(r - 1, a[n - i - 1]), 0)
	ans += r
print(ans)


n = int(input())
ar = [int(x) for x in input().split()]
ar = ar[::-1]
ans = 0
minn = ar[0] + 1
for k in range(n):
	if ar[k] < minn:
		ans += ar[k]
		minn = ar[k]
	elif minn > 1:
		ans += minn - 1
		minn = minn - 1
print(int(ans))


rr = lambda : input().strip()
rri = lambda : int(rr())
rrm = lambda : list(map(int, rr().split()))

def solve(N, A):
	ans = 0
	prev = float('inf')
	for x in reversed(A):
		x = min(x, prev - 1)
		ans += max(x, 0)
		prev = x
	return ans
for tc in range(1):
	N = rri()
	A = rrm()
	print(solve(N, A))


n = int(input())
a = [int(_) for _ in input().split(' ')][::-1] + [0]
res = a[0]
for i in range(n - 1):
	if a[i + 1] < a[i]:
		res += a[i + 1]
	elif a[i] - 1 > 0:
		a[i + 1] = a[i] - 1
		res += a[i + 1]
	else:
		break
print(res)


import sys
input = sys.stdin.readline
I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
an = l[-1]
mi = l[-1]
for i in range(n - 2, -1, -1):
	if l[i] >= mi:
		an += max(0, mi - 1)
		mi = max(0, mi - 1)
	else:
		an += l[i]
		mi = l[i]
print(an)


from fractions import Fraction
import sys
import bisect
import heapq
from math import *
from collections import Counter as counter
from itertools import combinations as comb
from itertools import permutations as permutate
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
mod = pow(10, 9) + 7

def inp():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def graph(vertex):
	return [[] for i in range(0, vertex + 1)]

def zerolist(n):
	return [0] * n

def nextline():
	out('\n')

def testcase(t):
	for p in range(t):
		solve()

def printlist(a):
	for p in range(0, len(a)):
		out(str(a[p]) + ' ')

def lcm(a, b):
	return a * b // gcd(a, b)

def solve():
	n = int(inp())
	a = lis()
	ans = a[n - 1]
	for i in range(n - 2, -1, -1):
		if a[i] < a[i + 1]:
			ans += a[i]
		elif a[i + 1] == 1 or a[i + 1] == 0:
			break
		else:
			a[i] = a[i + 1] - 1
			ans += a[i]
	print(ans)
testcase(1)


import sys
minm = sys.maxsize
summ = 0
n = int(input())
l = list(map(int, sys.stdin.readline().split()))
for i in range(n - 1, -1, -1):
	minm = min(minm - 1, l[i])
	if minm <= 0:
		break
	if minm == l[i]:
		summ += l[i]
	else:
		summ = summ + minm
print(summ)


from collections import Counter, defaultdict, deque
import itertools
n = int(input())
a = list(map(int, input().split()))
ans = a[-1]
prev = a[-1]
for i in range(n - 2, -1, -1):
	prev = max(min(a[i], prev - 1), 0)
	ans += prev
print(ans)


input()
chocolates = [int(s) for s in input().split()]
max_choc = 1000000001
total = 0
for c in reversed(chocolates):
	take = min(max_choc, c)
	total += take
	max_choc = max(0, take - 1)
print(total)


import sys
import math
from collections import defaultdict, deque
input = sys.stdin.readline

def inar():
	return [int(el) for el in input().split()]

def main():
	n = int(input())
	arr = inar()
	ans = arr[n - 1]
	mx = arr[n - 1]
	for i in range(n - 2, -1, -1):
		temp = min(mx - 1, arr[i])
		mx = temp
		ans += temp
		if temp == 0:
			break
	print(ans)
main()


from sys import stdin, stdout
import cProfile, math
from collections import Counter, defaultdict
from bisect import bisect_left, bisect, bisect_right
import itertools
from copy import deepcopy
from fractions import Fraction
import sys, threading
import operator as op
from functools import reduce
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)
fac_warmup = False
printHeap = str()
memory_constrained = False
P = 10 ** 9 + 7
import sys

class merge_find:

	def __init__(self, n):
		self.parent = list(range(n))
		self.size = [1] * n
		self.num_sets = n
		self.lista = [[_] for _ in range(n)]

	def find(self, a):
		to_update = []
		while a != self.parent[a]:
			to_update.append(a)
			a = self.parent[a]
		for b in to_update:
			self.parent[b] = a
		return self.parent[a]

	def merge(self, a, b):
		a = self.find(a)
		b = self.find(b)
		if a == b:
			return
		if self.size[a] < self.size[b]:
			(a, b) = (b, a)
		self.num_sets -= 1
		self.parent[b] = a
		self.size[a] += self.size[b]
		self.lista[a] += self.lista[b]

	def set_size(self, a):
		return self.size[self.find(a)]

	def __len__(self):
		return self.num_sets

def display(string_to_print):
	stdout.write(str(string_to_print) + '\n')

def primeFactors(n):
	factors = dict()
	for i in range(2, math.ceil(math.sqrt(n)) + 1):
		while n % i == 0:
			if i in factors:
				factors[i] += 1
			else:
				factors[i] = 1
			n = n // i
	if n > 2:
		factors[n] = 1
	return factors

def fibonacci_modP(n, MOD):
	if n < 2:
		return 1
	return (cached_fn(fibonacci_modP, (n + 1) // 2, MOD) * cached_fn(fibonacci_modP, n // 2, MOD) + cached_fn(fibonacci_modP, (n - 1) // 2, MOD) * cached_fn(fibonacci_modP, (n - 2) // 2, MOD)) % MOD

def factorial_modP_Wilson(n, p):
	if p <= n:
		return 0
	res = p - 1
	for i in range(n + 1, p):
		res = res * cached_fn(InverseEuler, i, p) % p
	return res

def binary(n, digits=20):
	b = bin(n)[2:]
	b = '0' * (20 - len(b)) + b
	return b

def isprime(n):
	if n < 4:
		return True
	if n % 2 == 0:
		return False
	if n % 3 == 0:
		return False
	i = 5
	w = 2
	while i * i <= n:
		if n % i == 0:
			return False
		i += w
		w = 6 - w
	return True
factorial_modP = []

def warm_up_fac(MOD):
	nonlocal factorial_modP, fac_warmup
	if fac_warmup:
		return
	factorial_modP = [1 for _ in range(fac_warmup_size + 1)]
	for i in range(2, fac_warmup_size):
		factorial_modP[i] = factorial_modP[i - 1] * i % MOD
	fac_warmup = True

def InverseEuler(n, MOD):
	return pow(n, MOD - 2, MOD)

def nCr(n, r, MOD):
	nonlocal fac_warmup, factorial_modP
	if not fac_warmup:
		warm_up_fac(MOD)
		fac_warmup = True
	return factorial_modP[n] * (pow(factorial_modP[r], MOD - 2, MOD) * pow(factorial_modP[n - r], MOD - 2, MOD) % MOD) % MOD

def test_print(*args):
	if testingMode:
		print(args)

def display_list(list1, sep=' '):
	stdout.write(sep.join(map(str, list1)) + '\n')

def display_2D_list(li):
	for i in li:
		print(i)

def prefix_sum(li):
	sm = 0
	res = []
	for i in li:
		sm += i
		res.append(sm)
	return res

def get_int():
	return int(stdin.readline().strip())

def get_tuple():
	return map(int, stdin.readline().split())

def get_list():
	return list(map(int, stdin.readline().split()))
import heapq, itertools
pq = []
entry_finder = {}
REMOVED = '<removed-task>'

def add_task(task, priority=0):
	if task in entry_finder:
		remove_task(task)
	count = next(counter)
	entry = [priority, count, task]
	entry_finder[task] = entry
	heapq.heappush(pq, entry)

def remove_task(task):
	entry = entry_finder.pop(task)
	entry[-1] = REMOVED

def pop_task():
	while pq:
		(priority, count, task) = heapq.heappop(pq)
		if task is not REMOVED:
			del entry_finder[task]
			return task
	raise KeyError('pop from an empty priority queue')
memory = dict()

def clear_cache():
	nonlocal memory
	memory = dict()

def cached_fn(fn, *args):
	nonlocal memory
	if args in memory:
		return memory[args]
	else:
		result = fn(*args)
		memory[args] = result
		return result

def ncr(n, r):
	return math.factorial(n) / (math.factorial(n - r) * math.factorial(r))

def binary_serach(i, li):
	fn = lambda x: li[x] - x // i
	x = -1
	b = len(li)
	while b >= 1:
		while b + x < len(li) and fn(b + x) > 0:
			x += b
		b = b // 2
	return x
TestCases = False
testingMode = False
fac_warmup_size = 10 ** 5 + 100
optimiseForReccursion = True
from math import factorial

def main():
	n = get_int()
	li = get_list()
	li.reverse()
	k = 1000000000000000
	res = 0
	for i in li:
		k = max(min(i, k - 1), 0)
		res += k
	print(res)
if TestCases:
	for _ in range(get_int()):
		cProfile.run('main()') if testingMode else main()
else:
	(cProfile.run('main()') if testingMode else main()) if not optimiseForReccursion else threading.Thread(target=main).start()


n = int(input())
a = [int(i) for i in input().split()][::-1]
ans = [a[0]]
ctr = 1
for i in range(1, len(a)):
	if a[i] >= ans[i - 1]:
		if ans[ctr - 1] - 1 > 0:
			ans.append(ans[ctr - 1] - 1)
			ctr += 1
		else:
			break
	else:
		ans.append(a[i])
		ctr += 1
print(sum(ans))


n = int(input())
a = list(map(int, input().split()))
cur = a[-1] + 1
ss = 0
for x in reversed(a):
	cur = max(0, min(cur - 1, x))
	ss += cur
print(ss)


n = int(input())
a = list(map(int, input().split()))
ans = a[-1]
for i in range(n - 2, -1, -1):
	if a[i + 1] == 0:
		a[i] = 0
	elif a[i] < a[i + 1]:
		ans += a[i]
	elif a[i] == [i - 1]:
		a[i] = a[i] - 1
		ans += a[i]
	else:
		a[i] = a[i + 1] - 1
		ans += a[i]
print(ans)


from __future__ import division, print_function
import sys
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip
import os, sys, bisect, copy
from collections import defaultdict, Counter, deque
if os.path.exists('in.txt'):
	sys.stdin = open('in.txt', 'r')
if os.path.exists('out.txt'):
	sys.stdout = open('out.txt', 'w')

def input():
	return sys.stdin.readline()

def mapi(arg=0):
	return map(int if arg == 0 else str, input().split())
n = int(input())
a = list(mapi())
res = a[-1]
for i in range(n - 2, -1, -1):
	a[i] = max(0, min(a[i + 1] - 1, a[i]))
	res += a[i]
print(res)


n = int(input())
a = list(map(int, input().split()))
sum = a[-1]
for i in range(len(a) - 2, -1, -1):
	if a[i + 1] < 2:
		a[i] = 0
	elif a[i] >= a[i + 1]:
		a[i] = a[i + 1] - 1
	sum += a[i]
print(sum)


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


n = int(input())
a = [int(j) for j in input().split()]
for i in range(n - 1, 0, -1):
	if a[i] <= a[i - 1]:
		if a[i] > 0:
			a[i - 1] = a[i] - 1
		else:
			a[i - 1] = 0
print(sum(a))


n = int(input())
Choco = [int(i) for i in input().split()]
c = 0
x = 0
for i in range(1, n):
	if Choco[n - i] > Choco[n - i - 1]:
		c += Choco[n - i]
	elif Choco[n - i] > 0:
		c += Choco[n - i]
		Choco[n - i - 1] = Choco[n - i] - 1
		if Choco[n - i - 1] == 0:
			x = n - i - 1
			break
if Choco[0] > 0 and Choco[x] != 0:
	c += Choco[0]
print(c)


x = int(input())
y = input()
y_ = y.split()
z = [int(d) for d in y_]
t = [ele for ele in reversed(z)]
ans = z[0]
u = []
s = []
if len(t) > 1:
	for i in range(0, len(t) - 1):
		if len(u) == 0:
			u.append(t[0])
		if t[i + 1] < t[i]:
			u.append(t[i + 1])
		elif t[i + 1] >= t[i]:
			t[i + 1] = t[i] - 1
			u.append(t[i + 1])
	for num in u:
		if num >= 0:
			s.append(num)
else:
	print(t[0])
	exit()
print(sum(s))


input()
S = list(map(int, input().split()))
an = S[-1]
la = S[-1]
for i in range(len(S) - 2, -1, -1):
	ne = max(min(la - 1, S[i]), 0)
	an += ne
	la = ne
print(an)


R = lambda : list(map(int, input().split()))
narr = R()
n = narr[0]
arr = R()
i = n - 2
ans = arr[n - 1]
ans1 = arr[n - 1]
while i >= 0:
	ans = min(ans, arr[i] + n - 1 - i)
	ans1 += max(ans - (n - 1 - i), 0)
	i -= 1
print(ans1)


import sys
from collections import defaultdict
strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))
n = int(input())
arr = intInp()
arr = arr[::-1]
m = 0
tmp = arr[0]
last = tmp
for i in arr[1:]:
	if min(i, last - 1) > 0:
		last = min(i, last - 1)
		tmp += last
	else:
		break
m = max(m, tmp)
print(m)


n = int(input())
L = input().split()
for i in range(0, n):
	L[i] = int(L[i])
c = 0
x = L[i]
for j in range(n - 2, -1, -1):
	if L[j] == 0:
		break
	elif L[j + 1] == 1:
		break
	elif L[j] < L[j + 1]:
		x = x + L[j]
	else:
		x = x + L[j + 1] - 1
		L[j] = L[j + 1] - 1
if x > c:
	c = x
print(c)


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


n = int(input())
a = [int(c) for c in input().split()]
i = n - 1
s = a[i]
while a[i] != 0 and i >= 1:
	if a[i - 1] < a[i]:
		s = s + a[i - 1]
	else:
		a[i - 1] = a[i] - 1
		s = s + a[i - 1]
	i = i - 1
print(s)


n = int(input())
x = list(map(int, input().split()))
i = n - 1
while i > 0:
	if x[i - 1] >= x[i]:
		x[i - 1] = max(x[i] - 1, 0)
	i -= 1
s = 0
for i in x:
	s += i
print(s)


from sys import stdin, stdout
for _ in range(1):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	prev = a[-1]
	ans = a[-1]
	for i in range(n - 2, -1, -1):
		if a[i] < prev or a[i] == 0:
			ans += a[i]
			prev = a[i]
		else:
			ans += prev - 1 if prev > 0 else 0
			prev = max(prev - 1, 0)
	print(ans)


n = int(input())
a = [int(i) for i in input().split()]
a.reverse()
z = a[0]
s = z
for i in range(1, n):
	if int(a[i]) < z:
		s = s + int(a[i])
		z = int(a[i])
	else:
		s = s + z - 1
		z = z - 1
	if z <= 0:
		break
print(s)


n = int(input())
a = list(map(int, input().split()))
res = 0
for i in range(n):
	if i == 0:
		res += a[-1]
		tmp = a[-1]
	else:
		tmp = min(tmp - 1, a[n - i - 1])
		res += tmp
		if tmp == 0:
			break
print(res)


n = int(input())
l = list(map(int, input().split()))
ans = 0
val = float('inf')
for i in range(1, n + 1):
	val = min(val - 1, l[-i])
	ans += max(0, val)
print(ans)


import sys
n = int(sys.stdin.readline())
a = [int(x) for x in sys.stdin.readline().strip().split(' ')]
res = cur = a[n - 1]
n -= 2
while n >= 0:
	cur = min(a[n], cur - 1)
	if cur <= 0:
		break
	res += cur
	n -= 1
print(res)


n = int(input())
m = list(map(int, input().split()))
for i in range(n - 1):
	if m[n - 1 - i] == 0:
		m[n - 2 - i] = 0
	else:
		m[n - 2 - i] = min(m[n - 1 - i] - 1, m[n - 2 - i])
print(sum(m))


n = int(input())
chocolate = list(map(int, input().split()))
count = chocolate[-1]
i = n - 2
temp = count
while i != -1:
	temp = min(chocolate[i], temp - 1)
	if temp <= 0:
		break
	count = count + temp
	if temp == 1:
		break
	i = i - 1
print(count)


import math
n = int(input())
arr = [int(x) for x in input().split()]
arr = arr[::-1]
count = arr[0]
prev = arr[0]
for i in range(1, n):
	if prev == 1:
		break
	if arr[i] < prev:
		count = count + arr[i]
		prev = arr[i]
	else:
		count = count + prev - 1
		prev = prev - 1
print(count)


n = int(input())
a = list(map(int, input().split()))
answer = 0
for i in range(1, n):
	if a[-i] != 1:
		if a[-i] > a[-i - 1]:
			answer += a[-i - 1]
		elif a[-i] < a[-i - 1]:
			a[-i - 1] = a[-i] - 1
			answer += a[-i - 1]
		else:
			a[-i - 1] -= 1
			answer += a[-i - 1]
	else:
		break
print(answer + a[-1])


n = int(input())
a = list(map(int, input().split()))
p = 0
mx = a[n - 1] + 1
for i in range(n - 1, -1, -1):
	if mx - 1 <= 0:
		break
	else:
		mx = min(a[i], mx - 1)
		p += mx
print(p)


input()
_list = list(map(int, input().split()))[::-1]
if len(_list) == 1:
	print(_list[0])
	exit(0)
(_sum, current) = [_list[0]] * 2
for i in _list[1:]:
	current = max(0, min(current - 1, i))
	_sum += current
print(_sum)


n = int(input())
(res, lst) = (0, 10 ** 10)
A = list(map(int, input().split()))
for x in reversed(A):
	lst = min(x, lst - 1)
	res += max(lst, 0)
print(res)


n = int(input())
s = list(map(int, input().split()))
m = s[-1]
sm = m
for i in s[::-1][1:]:
	if i == 0 or m == 0:
		m = 0
		sm += 0
	elif i < m:
		sm += i
		m = i
	else:
		m = m - 1
		sm += m
print(sm)


n = input()
arr = [int(j) for j in input().split()]
arr.reverse()
sum = arr[0]
temp = arr[0]
for i in range(1, len(arr)):
	if temp == 0:
		break
	if arr[i] >= temp:
		temp = temp - 1
		sum = sum + temp
	else:
		temp = arr[i]
		sum = sum + temp
print(sum)


n = int(input())
a = list(map(int, input().split()))
last = 1000000000000
ans = 0
for i in range(n - 1, -1, -1):
	new = max(min(last - 1, a[i]), 0)
	last = new
	ans += new
print(ans)


n = int(input())
a = list(map(int, input().split()))
a = [0] + a
res = a[-1]
for i in range(n - 1, 0, -1):
	if a[i] < a[i + 1]:
		res += a[i]
	else:
		a[i] = max(0, a[i + 1] - 1)
		res += a[i]
print(res)


n = int(input())
s_i = list(map(int, input().split()))
max_possible = float('inf')
answer = 0
for i in range(len(s_i) - 1, -1, -1):
	answer += min(max_possible, s_i[i])
	max_possible = min(max_possible, s_i[i]) - 1
	if max_possible < 0:
		break
print(answer)


n = int(input())
s = [int(i) for i in input().split()]
s = s[::-1]
c = 100000000000
ans = 0
for i in s:
	if i < c:
		c = i
	else:
		c = max(c - 1, 0)
	ans += c
print(ans)


n = int(input())
a = list(map(int, input().split()))
a.reverse()
ans = 0
t = a[0]
for k in a:
	t = min(k, t)
	ans = ans + t
	t = t - 1
	if t == 0:
		break
print(ans)


from __future__ import division, print_function
import bisect
import math
import itertools
import sys
from atexit import register
if sys.version_info[0] < 3:
	from io import BytesIO as stream
else:
	from io import StringIO as stream
if sys.version_info[0] < 3:

	class dict(dict):

		def items(self):
			return dict.iteritems(self)

		def keys(self):
			return dict.iterkeys(self)

		def values(self):
			return dict.itervalues(self)
	input = raw_input
	range = xrange
	filter = itertools.ifilter
	map = itertools.imap
	zip = itertools.izip

def sync_with_stdio(sync=True):
	global input, flush
	if sync:
		flush = sys.stdout.flush
	else:
		sys.stdin = stream(sys.stdin.read())
		input = lambda : sys.stdin.readline().rstrip('\r\n')
		sys.stdout = stream()
		register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))

def main():
	n = int(input())
	l1 = list(map(int, input().split()))
	t = 0
	l1 = l1[::-1]
	t += l1[0]
	x = l1[0]
	for i in range(1, n):
		t += min(l1[i], x - 1)
		x = min(l1[i], x - 1)
		if x == 0:
			break
	print(t)
sync_with_stdio(False)
main()


def mp():
	return map(int, input().split())
n = int(input())
a = list(mp())
a = a[::-1]
cnt = 0
m = 10 ** 10
for i in range(n):
	cnt += min(m, a[i])
	m = max(0, min(m - 1, a[i] - 1))
print(cnt)


input()
chocos = list(map(int, input().split()))[::-1] + [0, 0]
numbers = chocos[0]
for i in range(len(chocos) - 2):
	if chocos[i] == 0:
		break
	elif chocos[i + 1] < chocos[i]:
		numbers += chocos[i + 1]
	else:
		numbers += chocos[i] - 1
		chocos[i + 1] = chocos[i] - 1
print(numbers)


def f():
	n = int(input())
	arr = list(map(int, input().split()))
	rez = 0
	prev = arr[n - 1] + 1
	for i in range(n - 1, -1, -1):
		cur = arr[i]
		if cur < prev:
			rez += cur
			prev = cur
		else:
			cur = prev - 1
			if cur <= 0:
				break
			rez += cur
			prev = cur
	print(rez)
f()


n = int(input())
l = list(map(int, input().split()))
ans = l[-1]
x = n - 1
p = l[-1]
while x >= 1:
	if p == 0:
		break
	else:
		if l[x - 1] >= p:
			ans += p - 1
			p = p - 1
		else:
			ans += l[x - 1]
			p = l[x - 1]
		x -= 1
print(ans)


input()
a = [int(x) for x in input().split()]
(c, d) = (0, 0)
a = a[::-1]
for i in range(len(a) - 1):
	if a[i + 1] >= a[i]:
		a[i + 1] = a[i] - 1
		if a[i + 1] < 0:
			a[i + 1] = 0
			(d, m) = (1, i + 1)
	if d == 1:
		for i in range(m, len(a)):
			a[i] = 0
		break
print(sum(a))


n = int(input())
l = list(map(int, input().split()))
choco = l[len(l) - 1]
k = len(l) - 1
while k > 0:
	if l[k - 1] < l[k]:
		choco += l[k - 1]
	else:
		choco += l[k] - 1
		l[k - 1] = l[k] - 1
	k -= 1
	if l[k] == 0:
		break
print(choco)


n = int(input())
arr = list(map(int, input().split()))
arr = arr[::-1]
prevMax = arr[0]
chocs = 0
for i in range(n):
	if i == 0:
		chocs += arr[i]
	elif arr[i] < prevMax:
		chocs += arr[i]
		prevMax = arr[i]
	else:
		chocs += max(0, prevMax - 1)
		prevMax = max(0, prevMax - 1)
print(chocs)


n = int(input())
chocolate = list(map(int, input().split()))
choco = chocolate[-1]
choco_temp = choco
for i in range(n - 1):
	if choco_temp == 0:
		break
	choco_temp = min(chocolate[n - i - 2], choco_temp - 1)
	choco += choco_temp
print(choco)


n = int(input())
a = list(map(int, input().split()))
ans = 0
curr_min = 10000000000.0
for i in a[::-1]:
	if i < curr_min:
		ans += i
		curr_min = i
	elif curr_min == 0:
		ans += 0
		curr_min = 0
	else:
		ans += curr_min - 1
		curr_min -= 1
print(ans)


n = int(input())
x = input().split(' ')
res = 0
res = res + int(x[n - 1])
for i in range(n - 2, -1, -1):
	if int(x[i]) < int(x[i + 1]):
		res = res + int(x[i])
	elif int(x[i]) >= int(x[i + 1]):
		if int(x[i + 1]) - 1 >= 0:
			res = res + int(x[i + 1]) - 1
			x[i] = int(x[i + 1]) - 1
		else:
			x[i] = 0
print(res)


n = int(input())
a = list(map(int, input().split()))
a.reverse()
c = [a[0]]
for i in range(1, n):
	if a[i] >= c[len(c) - 1]:
		if c[len(c) - 1] != 0:
			c.append(c[len(c) - 1] - 1)
	else:
		c.append(a[i])
print(sum(c))


input()
L = list(map(int, input().split()))[::-1]
now = 10000000000.0
ans = 0
for x in L:
	if x < now:
		now = x
	else:
		now = now - 1
	if now == 0:
		break
	ans += now
print(ans)


n = int(input())
arr = list(map(int, input().split()))
maxi = arr[n - 1]
ans = maxi
for i in range(n - 2, -1, -1):
	y = min(maxi - 1, arr[i])
	maxi = max(y, 1)
	ans += y
print(ans)


x = int(input())
s = str(input()).split()
for i in range(x):
	s[i] = int(s[i])
s = s[::-1]
sumi = s[0]
for i in range(1, x):
	if s[i - 1] > s[i]:
		sumi += s[i]
	elif s[i - 1] - 1 > 0:
		sumi += s[i - 1] - 1
		s[i] = s[i - 1] - 1
	else:
		break
print(sumi)


import math
import bisect
import itertools
import sys
I = lambda : sys.stdin.readline()
mod = 10 ** 9 + 7

def modu(a, m):
	if a % m:
		return a % m
	return m

def mindiff(a):
	b = a[:]
	b.sort()
	m = 10000000000
	for i in range(len(b) - 1):
		if b[i + 1] - b[i] < m:
			m = b[i + 1] - b[i]
	return m

def lcm(a, b):
	return a * b // math.gcd(a, b)

def merge(a, b):
	i = 0
	j = 0
	c = 0
	ans = []
	while i < len(a) and j < len(b):
		if a[i] < b[j]:
			ans.append(a[i])
			i += 1
		else:
			ans.append(b[j])
			c += len(a) - i
			j += 1
	ans += a[i:]
	ans += b[j:]
	return (ans, c)

def mergesort(a):
	if len(a) == 1:
		return (a, 0)
	mid = len(a) // 2
	(left, left_inversion) = mergesort(a[:mid])
	(right, right_inversion) = mergesort(a[mid:])
	(m, c) = merge(left, right)
	c += left_inversion + right_inversion
	return (m, c)

def is_prime(num):
	if num == 1:
		return False
	if num == 2:
		return True
	if num == 3:
		return True
	if num % 2 == 0:
		return False
	if num % 3 == 0:
		return False
	t = 5
	a = 2
	while t <= int(math.sqrt(num)):
		if num % t == 0:
			return False
		t += a
		a = 6 - a
	return True

def ceil(a, b):
	if a % b == 0:
		return a // b
	else:
		return a // b + 1

def ncr1(n, r):
	s = 1
	for i in range(min(n - r, r)):
		s *= n - i
		s %= mod
		s *= pow(i + 1, mod - 2, mod)
		s %= mod
	return s
n = int(input())
a = list(map(int, input().split()))
ans = 0
m = 10 ** 9 + 5
for i in range(n - 1, -1, -1):
	k = min(m - 1, a[i])
	if k > 0:
		ans += k
	m = min(m - 1, a[i])
print(ans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def main():
	pass

def binary(n):
	return bin(n).replace('0b', '')

def decimal(s):
	return int(s, 2)

def pow2(n):
	p = 0
	while n > 1:
		n //= 2
		p += 1
	return p

def isPrime(n):
	if n == 1:
		return False
	else:
		root = int(n ** 0.5)
		root += 1
		for i in range(2, root):
			if n % i == 0:
				return False
		return True

def lts(l):
	s = ''.join(map(str, l))
	return s

def stl(s):
	l = list(s)
	return l

def sq(a, target, arr=[]):
	s = sum(arr)
	if s == target:
		return arr
	if s >= target:
		return
	for i in range(len(a)):
		n = a[i]
		remaining = a[i + 1:]
		ans = sq(remaining, target, arr + [n])
		if ans:
			return ans

def SieveOfEratosthenes(n):
	cnt = 0
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	for p in range(2, n + 1):
		if prime[p]:
			cnt += 1
	return cnt

def nCr(n, r):
	f = math.factorial
	return f(n) // f(r) // f(n - r)
mod = int(1000000000.0) + 7

def ssinp():
	return input()

def iinp():
	return int(input())

def nninp():
	return map(int, input().split())

def llinp():
	return list(map(int, input().split()))

def p(xyz):
	print(xyz)

def p2(a, b):
	print(a, b)
import math
n = iinp()
a = llinp()
a.reverse()
ans = 0
curr = 12039810923123
for c in a:
	if curr == 0:
		break
	if c < curr:
		ans += c
		curr = c
	else:
		ans += curr - 1
		curr = curr - 1
p(ans)


n = int(input())
p = list(map(int, input().split()))
lam = p[::-1]
la = lam[0]
for x in range(1, n):
	if lam[x] < lam[x - 1]:
		la += lam[x]
	else:
		la += lam[x - 1] - 1
		lam[x] = lam[x - 1] - 1
		if lam[x] == 0:
			break
print(la)


n = int(input())
x = list(map(int, input().split()))
sum = 0
for i in range(n):
	if i == 0:
		sum += x[n - i - 1]
		continue
	else:
		if x[n - i] == 1 or x[n - i] == 0:
			x[n - i - 1] = 0
			break
		if x[n - i - 1] >= x[n - i]:
			x[n - i - 1] = x[n - i] - 1
		if x[n - i - 1] < x[n - i]:
			x[n - i - 1] = x[n - i - 1]
		sum += x[n - i - 1]
print(sum)


_ = input()
a = [int(s) for s in input().strip().split()]
x = a.copy()
for i in range(len(x) - 2, -1, -1):
	x[i] = max(0, min(x[i + 1] - 1, a[i]))
print(sum(x))


input()
x = list(map(int, input().split()))
r = 0
c = -1
for i in reversed(x):
	if c <= i and c != -1:
		c = c - 1 if c > 1 else 0
		if c > 0:
			r += c
		continue
	else:
		r += i
		c = i
print(r)


n = int(input())
l = list(map(int, input().split()))
sum = l[-1]
for i in range(n - 2, -1, -1):
	sum += min(l[i + 1] - 1, l[i])
	l[i] = min(l[i + 1] - 1, l[i])
	if min(l[i + 1] - 1, l[i]) == 0:
		break
print(sum)


n = int(input())
Ar = [int(x) for x in input().split()]
cnt = Ar[-1]
no_of_chocolates = cnt
for i in range(n - 1, 0, -1):
	if Ar[i - 1] >= no_of_chocolates:
		cnt += no_of_chocolates - 1
		no_of_chocolates -= 1
	else:
		cnt += Ar[i - 1]
		no_of_chocolates = Ar[i - 1]
	if no_of_chocolates == 1 or no_of_chocolates == 0:
		break
print(cnt)


import os
import sys
import math
import collections
import cmath

def mp():
	return map(int, input().split())

def inp():
	return input()
n = int(inp())
ar = list(mp())
sum = 0
ar1 = ar[::-1]
for i in range(n - 1):
	if ar1[i + 1] < ar1[i]:
		sum += ar1[i]
	else:
		d = ar1[i + 1] - ar1[i]
		ar1[i + 1] -= d + 1
		if ar1[i + 1] < 0:
			ar1[i + 1] = 0
		sum += ar1[i]
sum += ar1[n - 1]
print(sum)


n = int(input())
l = list(map(int, input().split()))
c = l[len(l) - 1]
for i in range(len(l) - 2, -1, -1):
	if l[i] >= l[i + 1]:
		l[i] = l[i + 1] - 1
		c = c + l[i]
	elif l[i] < l[i + 1]:
		c = c + l[i]
	if l[i] == 0:
		break
print(c)


n = int(input())
s = list(map(int, input().split()))
x = [0 for _ in range(n)]
j = 0
for i in range(-1, -n - 1, -1):
	if i == -1:
		x[i] = s[i]
	elif s[i] < x[i + 1]:
		x[i] = s[i]
	elif x[i + 1] - 1 > 0:
		x[i] = x[i + 1] - 1
	else:
		break
print(sum(x))


choco_num = int(input())
choco = list(map(int, input().split()))
a = choco[-1]
count = a
for x in range(choco_num - 2, -1, -1):
	a = min(choco[x], a - 1 if a > 1 else 0)
	count += a
print(count)


n = int(input())
chocs = [int(i) for i in input().split(' ')]
last = chocs[n - 1]
counter = last
for i in range(n - 2, -1, -1):
	if chocs[i] >= last and last != 0:
		last -= 1
	elif last != 0:
		last = chocs[i]
	counter += last
print(counter)


import sys
N = int(sys.stdin.readline())
A = [int(D) for D in sys.stdin.readline().strip().split(' ')]
total = A[-1]
prevChoc = A[-1]
for currChoc in A[-2::-1]:
	canHave = max(0, min(currChoc, prevChoc - 1))
	total += canHave
	prevChoc = canHave
print(total)


n = int(input())
chocs = list(map(int, input().split()))
res = chocs[n - 1]
prev = res
for i in range(n - 2, -1, -1):
	if chocs[i] >= prev:
		prev = prev - 1
		res += prev
	else:
		prev = chocs[i]
		res += prev
	if prev <= 0:
		break
print(res)


n = int(input())
a = list(map(int, input().split()))
summ = a[-1]
i = len(a) - 2
k = a[-1]
while i > -1:
	if a[i] < k:
		k = a[i]
		summ += k
	elif k == 0:
		break
	else:
		k -= 1
		summ += k
	i -= 1
print(summ)


import sys
length = sys.stdin.readline()
number = sys.stdin.readline()
counter = 0
prev_val = 1000000000.0 + 1
for elem in number.strip().split(' ')[::-1]:
	prev_val = min(int(elem), prev_val - 1)
	if prev_val <= 0:
		break
	counter += prev_val
print(counter)


n = int(input())
p = input()
arr = [int(j) for j in p.split()]
max1 = 10000000000
sum = 0
for i in range(n - 1, -1, -1):
	if max1 < 0:
		break
	elif arr[i] > max1:
		sum += max1
		max1 -= 1
	else:
		sum += arr[i]
		max1 = arr[i] - 1
print(sum)


n = int(input())
a = list(map(int, input().split(' ')))
if n > 1:
	for i in reversed(range(len(a) - 1)):
		if a[i] >= a[i + 1]:
			a[i] = a[i + 1] - 1
		if a[i] == 0 or i == 0:
			q = 0
			for ss in range(i, len(a)):
				q += a[ss]
			print(q)
			break
else:
	print(a[0])


n = int(input())
u = list(map(int, input().split()))
for i in range(n - 2, -1, -1):
	if u[i] >= u[i + 1] and u[i] != 0:
		u[i] = u[i + 1] - 1
		if u[i + 1] == 0:
			u[i] = 0
print(sum(u))


n = int(input())
l = list(map(int, input().split()))
for i in range(n - 1, 0, -1):
	if l[i] <= l[i - 1] and l[i] > 0 and (l[i - 1] > 0):
		l[i - 1] = l[i] - 1
	elif l[i] == 0:
		l[i - 1] = 0
print(sum(l))


n = int(input())
a = list(map(int, input().split()))
num = 0
num += a[n - 1]
temp = a[n - 1]
for i in range(n - 2, -1, -1):
	if temp == 0:
		break
	if a[i] >= temp:
		num += temp - 1
		temp = temp - 1
	else:
		num += a[i]
		temp = a[i]
print(num)


n = int(input())
a = [int(x) for x in input().rstrip().split()]
r = a[n - 1]
for i in reversed(range(0, n - 1)):
	if a[i] < a[i + 1]:
		r += a[i]
	else:
		if a[i + 1] > 0:
			r += a[i + 1] - 1
		a[i] = max(0, a[i + 1] - 1)
print(r)


n = int(input())
arr = list(map(int, input().split(' ')))
sum = arr[n - 1]
max = 0
for i in range(n - 1, -1, -1):
	if i == 0:
		break
	if arr[i] == 0:
		break
	if arr[i] > arr[i - 1]:
		sum += arr[i - 1]
	else:
		sum += arr[i] - 1
		arr[i - 1] = arr[i] - 1
print(sum)


n = int(input())
s = list(map(int, input().split()))
k = 1e+20
tot = 0
for i in s[::-1]:
	if k <= 1:
		break
	elif k <= i:
		tot += k - 1
		k -= 1
	elif k > i:
		tot += i
		k = i
print(tot)


n = int(input())
l = [int(i) for i in input().split()]
ans = l[-1]
last = l[-1]
for i in range(n - 2, -1, -1):
	if l[i] <= last - 1:
		last = l[i]
		ans += l[i]
	elif last > 1:
		last -= 1
		ans += last
print(ans)


N = int(input())
ls = list(map(int, input().split()))
cnt = 0
prev = ls[N - 1] + 1
for i in range(N - 1, -1, -1):
	cnt += max(min(ls[i], prev - 1), 0)
	prev = max(min(ls[i], prev - 1), 0)
print(cnt)


n = int(input())
l = list(map(int, input().split()))
l.reverse()
game = 0
cmin = 999999999999999999
for i in l:
	game += min(cmin - 1, i)
	cmin = min(cmin - 1, i)
	if cmin <= 0:
		break
print(game)


import math
import random
import sys
from collections import defaultdict as dd
ans = 0
n = int(input())
l = [int(i) for i in input().split()]
ans = [l[-1]]
for i in range(n - 2, -1, -1):
	ans.append(min(ans[-1] - 1, l[i]))
a = 0
for i in ans:
	a += max(0, i)
print(a)


n = input()
n = int(n)
a = [int(i) for i in input().split()]
ans = 0
curr = 99999999999999
for x in reversed(a):
	if x < curr:
		curr = x
	ans = ans + curr
	curr = max(curr - 1, 0)
print(ans)


n = int(input())
a = list(map(int, input().split()))[::-1]
prev = 9999999999999999999999
ans = 0
for i in range(n):
	prev = max(0, min(prev - 1, a[i]))
	ans += prev
print(ans)


n = int(input())
ai = list(map(int, input().split()))
ans = 0
num = ai[-1] + 1
for i in range(n - 1, -1, -1):
	num2 = max(0, min(num - 1, ai[i]))
	num = num2
	ans += num
print(ans)


n = int(input())
l = list(map(int, input().split()))
choco = []
choco.append(l[n - 1])
prev = choco
check = 0
for i in range(n - 2, -1, -1):
	if choco[n - 2 - i] < 1:
		break
	prev = min(choco[n - 2 - i] - 1, l[i])
	choco.append(prev)
print(sum(choco))


n = int(input())
l = list(map(int, input().strip().split(' ')))
sum = l[-1]
minm = l[-1]
temp = l[-1]
for i in range(n - 2, -1, -1):
	minm = min(temp - 1, l[i])
	minm = max(minm, 0)
	if minm == 0:
		break
	temp = minm
	sum += minm
print(sum)


import sys

def pro():
	return sys.stdin.readline().strip()

def rop():
	return map(int, pro().split()[::-1])

def main():
	qwerty = pro()
	o = list(rop())
	p = [o[0]]
	for (i, v) in enumerate(o[1:]):
		s = max([min([p[i] - 1, v]), 0])
		if s == 0:
			break
		p.append(s)
	print(sum(p))
main()


k = int(input())
a = input().split()
cnt = 0
cnt += int(a[k - 1])
s = [0] * k
s[k - 1] = int(a[k - 1])
for j in range(k - 2, -1, -1):
	if int(a[j]) < int(s[j + 1]):
		s[j] = int(a[j])
		cnt += s[j]
	else:
		s[j] = s[j + 1] - 1
		cnt += s[j]
	if s[j] == 0:
		break
print(cnt)


n = int(input())
a = [int(x) for x in input().split()]
a.reverse()
chocolates = a[0]
b = a[0] - 1
for i in range(1, n):
	if b == 0:
		break
	elif a[i] <= b:
		b = a[i] - 1
		chocolates += a[i]
	elif a[i] > b:
		chocolates += b
		b = b - 1
print(chocolates)


n = int(input())
a = list(map(int, input().split()))
ans = 0
flag = 0
for i in range(n - 1, 0, -1):
	if a[i - 1] >= a[i]:
		a[i - 1] = a[i] - 1
	ans += a[i]
	if a[i] <= 1:
		flag = 1
		break
if flag == 0:
	ans += a[0]
print(ans)


from sys import stdin, stdout
N = int(stdin.readline())
A = [int(x) for x in stdin.readline().split()]
X = [0 for i in range(0, N)]
X[N - 1] = A[N - 1]
res = X[N - 1]
for i in range(N - 2, -1, -1):
	X[i] = max(min(X[i + 1] - 1, A[i]), 0)
	res += X[i]
stdout.write(str(res))
stdout.write('\n')


n = int(input())
x = [int(n) for n in input().split()]
if n > 1:
	s = x[n - 1]
	while n != 1:
		if x[n - 1] <= x[n - 2] and x[n - 1] != 0:
			x[n - 2] = x[n - 1] - 1
		elif x[n - 1] == 0:
			x[n - 2] = 0
		s += x[n - 2]
		n -= 1
	print(s)
else:
	print(x[0])


n = int(input())
a = list(map(int, input().split()))
ans = 0
s = 0
s += a[n - 1]
c = a[n - 1]
if n >= 2:
	i = n - 2
	while i >= 0:
		ans = min(c - 1, a[i])
		c = ans
		s += ans
		if ans == 0:
			break
		i -= 1
print(s)


n = int(input())
a = list(map(int, input().split()))
last = a[n - 1]
ans = 0
ans += last
t = 0
for i in range(n - 2, -1, -1):
	if a[i] < last:
		ans += a[i]
		last = a[i]
	else:
		t = last - 1
		if t < 0:
			last = 0
			ans += 0
			break
		else:
			last = t
			ans += t
print(ans)


n = int(input())
arr = list(map(int, input().split()))
cnt = arr[n - 1]
mx = cnt
for i in range(n - 2, -1, -1):
	if mx - 1 == 0:
		break
	if arr[i] > mx - 1:
		cnt += mx - 1
		mx = mx - 1
	else:
		cnt += arr[i]
		mx = arr[i]
print(cnt)


n = int(input())
a = list(map(int, input().split()))
i = n - 1
out = 0
curr = a[i]
while i >= 0:
	curr = min(a[i], curr)
	out += curr
	curr -= 1
	if curr == 0:
		break
	i -= 1
print(out)


def main():
	n = int(input())
	array = list(map(int, input().split()))
	result = [array[-1]]
	for i in range(n - 2, -1, -1):
		current_number = array[i]
		if current_number >= result[-1]:
			current_number = result[-1] - 1
		result.append(current_number)
		if current_number == 0:
			break
	print(sum(result))
main()


import sys
import math
import itertools
import collections

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def wr(arr):
	return ' '.join(map(str, arr))

def revn(n):
	return str(n)[::-1]

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	r = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				r.append(i)
			else:
				r.extend([i, n // i])
	return r

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def prime(n):
	if n == 2:
		return True
	if n % 2 == 0 or n <= 1:
		return False
	sqr = int(math.sqrt(n)) + 1
	for d in range(3, sqr, 2):
		if n % d == 0:
			return False
	return True

def convn(number, base):
	newnumber = 0
	while number > 0:
		newnumber += number % base
		number //= base
	return newnumber

def cdiv(n, k):
	return n // k + (n % k != 0)
n = ii()
a = li()
ans = 0
t = 10 ** 9 + 1
i = -1
while t > 0 and i > -n - 1:
	t = min(t - 1, a[i])
	ans += t
	i -= 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
s = a[n - 1]
i = 0
k = 0
for i in range(n - 1):
	if a[n - 1 - i] > a[n - 2 - i]:
		s += a[n - 2 - i]
		k += 1
	else:
		break
for j in range(k + 1, n):
	a[n - 1 - j] = max(min(a[n - j] - 1, a[n - 1 - j]), 0)
	s += a[n - 1 - j]
print(s)


n = int(input())
l = list(map(int, input().split()))
s = l[-1]
prev = s
for i in range(n - 2, -1, -1):
	if prev == 0:
		break
	if l[i] >= prev:
		prev -= 1
		s += prev
	else:
		prev = l[i]
		s += prev
print(s)


input()
a = list(map(int, input().split()))
c = a[-1]
d = a[-1]
for i in range(1, len(a)):
	if d != 0:
		c = c + min(a[-i - 1], d - 1)
		d = min(a[-i - 1], d - 1)
	else:
		break
print(c)


import collections
n = int(input())
a = list(map(int, input().split()))
ans = a[-1]
for i in range(n - 2, -1, -1):
	curr = (a[i], max(a[i + 1] - 1, 0))[a[i] >= a[i + 1]]
	a[i] = curr
	ans += curr
print(ans)


import collections, atexit, math, sys, bisect
sys.setrecursionlimit(1000000)
isdebug = False
try:
	import pylint
	import numpy

	def dprint(*args, **kwargs):
		print(*args, file=sys.stderr)
	dprint('debug mode')
	isdebug = True
except Exception:

	def dprint(*args, **kwargs):
		pass

def red_inout():
	inId = 0
	outId = 0
	if not isdebug:
		inId = 0
		outId = 0
	if inId > 0:
		dprint('use input', inId)
		try:
			f = open('input' + str(inId) + '.txt', 'r')
			sys.stdin = f
		except Exception:
			dprint('invalid input file')
	if outId > 0:
		dprint('use output', outId)
		try:
			f = open('stdout' + str(outId) + '.txt', 'w')
			sys.stdout = f
		except Exception:
			dprint('invalid output file')
		atexit.register(lambda : sys.stdout.close())
if isdebug and len(sys.argv) == 1:
	red_inout()

def getIntList():
	return list(map(int, input().split()))

def solve():
	pass
T_ = 1
for iii_ in range(T_):
	(N,) = getIntList()
	za = getIntList()
	r = 0
	now = 10 ** 10
	for i in range(N - 1, -1, -1):
		x = min(now - 1, za[i])
		if x <= 0:
			break
		now = x
		r += x
	print(r)


from collections import deque as de
import math
from collections import Counter as cnt
from functools import reduce
from typing import MutableMapping
from itertools import groupby as gb

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

class My_stack:

	def __init__(self):
		self.data = []

	def my_push(self, x):
		return self.data.append(x)

	def my_pop(self):
		return self.data.pop()

	def my_peak(self):
		return self.data[-1]

	def my_contains(self, x):
		return self.data.count(x)

	def my_show_all(self):
		return self.data

	def isEmpty(self):
		return len(self.data) == 0
arrStack = My_stack()

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def get_prime_factors(number):
	prime_factors = []
	while number % 2 == 0:
		prime_factors.append(2)
		number = number / 2
	for i in range(3, int(math.sqrt(number)) + 1, 2):
		while number % i == 0:
			prime_factors.append(int(i))
			number = number / i
	if number > 2:
		prime_factors.append(int(number))
	return prime_factors

def get_frequency(list):
	dic = {}
	for ele in list:
		if ele in dic:
			dic[ele] += 1
		else:
			dic[ele] = 1
	return dic

def Log2(x):
	return math.log10(x) / math.log10(2)

def isPowerOfTwo(n):
	return math.ceil(Log2(n)) == math.floor(Log2(n))
n = int(input())
a = list(map(int, input().split()))
ans = a[n - 1]
curr = ans - 1
for i in range(n - 2, -1, -1):
	temp = min(curr, a[i])
	curr = temp - 1
	ans += temp
	if temp == 0 or temp == 1:
		break
print(ans)


n = int(input())
array = list(map(int, input().split()))
count = 0
m = 1000000000000000
for i in range(n - 1, -1, -1):
	if array[i] >= m:
		if m == 1:
			break
		count += m - 1
		m -= 1
	else:
		count += array[i]
		m = array[i]
print(count)


n = int(input())
number = list(map(int, input().split()))
for i in range(n - 2, -1, -1):
	if number[i] >= number[i + 1]:
		number[i] = max(number[i + 1] - 1, 0)
print(sum(number))


n = int(input())
a = [int(x) for x in input().rstrip().split()]
count = a[-1]
choc = count - 1
for i in range(2, n + 1):
	choc = min(choc, a[-i])
	if choc <= 0:
		break
	count += choc
	choc -= 1
print(count)


n = int(input())
a = list(map(int, input().split()))
a = a[::-1]
current = a[0] + 1
total = 0
for x in a:
	current = min(x, current - 1)
	if current <= 0:
		break
	total += current
print(total)


n = int(input())
x = input().split()
y = list(map(int, x))
l = len(y)
count = y[l - 1]
for i in range(l - 1):
	if y[l - i - 1] > y[l - i - 2]:
		count += y[l - i - 2]
	else:
		y[l - i - 2] = y[l - i - 1] - 1
		if y[l - i - 2] == 0:
			break
		count += y[l - i - 2]
print(count)


from collections import Counter
from math import factorial
n = int(input())
arr = [int(x) for x in input().split()]
arr.reverse()
total = 0
pos = 10 ** 10
for el in arr:
	total += min(pos, el)
	pos = min(pos - 1, el - 1)
	if pos == 0:
		break
print(total)


n = int(input())
a = [int(x) for x in input().split()]
s = a[-1]
j = n - 1
for i in range(n - 2, -1, -1):
	if a[i] >= a[j]:
		a[i] = max(a[j] - 1, 0)
	j -= 1
	s += a[i]
print(s)


n = int(input())
p = list(map(int, input().split(' ')))
i = n - 2
sum_ = p[n - 1]
while i >= 0:
	p[i] = max(min(p[i], p[i + 1] - 1), 0)
	sum_ += p[i]
	i -= 1
print(sum_)


n = int(input())
arr = list(map(int, input().split()))
i = len(arr) - 1
cur = arr[i]
res = cur
i = i - 1
while i >= 0:
	if arr[i] < cur:
		res += arr[i]
		cur = arr[i]
	else:
		cur = cur - 1
		if cur < 0:
			break
		res += cur
	i = i - 1
print(res)


n = int(input())
l = [int(i) for i in input().split()]
l.reverse()
sm = 0
last = 10 ** 20
for i in range(n):
	last = max(0, min(last - 1, l[i]))
	sm += last
print(sm)


import getpass
import sys
if getpass.getuser() != 'frohenk':
	filename = 'half'
else:
	sys.stdin = open('input.txt')
import math
import string
import re
import math
import random
from decimal import Decimal, getcontext

def ria():
	return [int(i) for i in input().split()]
n = ria()[0]
ar = [i for i in reversed(ria())]
p = ar[0]
sm = ar[0]
for i in range(1, n):
	cr = min(max(0, p - 1), ar[i])
	p = cr
	sm += cr
print(sm)


i = int(input())
data = input().split()
n = len(data)
sum = int(data[n - 1])
key = int(data[n - 1])
for j in range(n - 2, -1, -1):
	if int(data[j]) >= key:
		for k in range(j, -1, -1):
			if key > 0:
				key = key - 1
			else:
				key = 0
			sum = sum + key
			break
	else:
		sum = sum + int(data[j])
		key = int(data[j])
print(sum)


n = int(input())
arr = list(map(int, input().split()))
constraints = [None] * len(arr)
constraints[-1] = [len(constraints) - 1 - arr[-1], len(constraints) - 1]
for i in range(len(arr) - 1 - 1, -1, -1):
	if i - arr[i] > constraints[i + 1][0]:
		constraints[i] = [i - arr[i], i]
	else:
		constraints[i] = constraints[i + 1]
res = 0
for i in range(len(arr)):
	(k, k_i) = constraints[i]
	res += max(0, -k + k_i - (k_i - i))
print(res)


n = int(input())
a = list(map(int, input().split()))
a = a[::-1]
sum = 0
sum = sum + a[0]
x = a[0]
for i in range(1, len(a)):
	temp = min(x - 1, a[i])
	if temp > 0:
		sum = sum + temp
	else:
		print(sum)
		exit()
	x = temp
print(sum)


n = int(input())
a = list(map(int, input().split()))
x = [None] * n
x[n - 1] = a[n - 1]
s = 0
for i in range(n - 1, 0, -1):
	if a[i - 1] < x[i]:
		x[i - 1] = a[i - 1]
	elif a[i - 1] == x[i]:
		x[i - 1] = max(0, x[i] - 1)
	else:
		x[i - 1] = max(0, x[i] - 1)
	s += x[i]
s += x[0]
print(s)


n = int(input())
a = [int(i) for i in input().split()]
s = a[n - 1]
r = a[n - 1]
for i in range(n - 1, -1, -1):
	if i == n - 1:
		continue
	if a[i] >= r:
		if r == 0 or r == 1:
			break
		r = r - 1
		s = s + r
	else:
		r = a[i]
		s = s + r
print(s)


n = int(input())
nums = [int(x) for x in input().split()]
pre = float('inf')
ans = 0
for x in nums[::-1]:
	cur = max(0, min(pre - 1, x))
	ans += cur
	pre = cur
print(ans)


n = int(input())
a = list(map(int, input().split()))
c = a[-1]
last = a[-1]
for i in range(n - 2, -1, -1):
	if last > a[i]:
		c += a[i]
		last = a[i]
	elif last <= a[i]:
		c += last - 1
		last -= 1
	last = max(last, 1)
print(c)


n = int(input())
choc = [int(i) for i in input().split()]
current = int(choc[n - 1] + 1)
total = 0
for i in range(n):
	num = int(choc[n - i - 1])
	if num < current:
		current = num
		total += num
	elif current != 0:
		current -= 1
		total += current
print(total)


n = int(input())
a = input().split()
for i in range(n):
	a[i] = int(a[i])
for j in range(n - 1, 0, -1):
	if a[j - 1] >= a[j] and a[j] != 0:
		a[j - 1] = a[j] - 1
	elif a[j] == 0:
		a[j - 1] = 0
print(sum(a))


t = int(input())
x = [int(q) for q in input().split()]
x.reverse()
ans = 0
s = x[0]
for j in x:
	s = min(j, s)
	ans += s
	s -= 1
	if s == 0:
		break
print(ans)


n = int(input())
a = [int(i) for i in input().split(' ')]
ans = a[-1]
last = a[-1]
for i in range(len(a) - 2, -1, -1):
	last = max(0, min(a[i], last - 1))
	ans += last
print(ans)


n = int(input())
a = list(map(int, input().split()))
a = a[::-1]
res = a[0]
temp = a[0]
for i in range(1, n):
	if a[i] < temp:
		res += a[i]
		temp = a[i]
	elif temp - 1 > 0:
		res += temp - 1
		temp = temp - 1
print(res)


n = int(input())
nums = list(map(int, input().split()))
minimum = nums[n - 1]
i = n - 2
result = minimum
while i >= 0:
	if nums[i] >= minimum:
		minimum -= 1
		nums[i] = minimum
	else:
		minimum = nums[i]
	result += minimum
	if minimum == 0:
		for j in range(i):
			nums[j] = 0
		break
	i -= 1
print(result)


n = int(input())
l = list(map(int, input().split()))
l = l[::-1]
s = []
s.append(l[0])
res = 0
for i in range(1, n):
	if l[i] < s[-1]:
		res += l[i]
		s.append(l[i])
	else:
		if s[-1] == 1:
			break
		temp = s[-1] - 1
		res += temp
		s.append(temp)
print(res + l[0])


n = int(input())
a = list(map(int, input().split()))
c = a[-1]
ans = c
for i in a[::-1][1:]:
	if i < c:
		ans += i
		c = i
	else:
		if c == 0:
			break
		ans += c - 1
		c -= 1
print(ans)


a = int(input())
b = list(map(int, input().split()))
c = b[a - 1]
for i in range(a - 1, 0, -1):
	if b[i - 1] >= b[i]:
		if b[i] == 0:
			b[i - 1] = 0
		else:
			b[i - 1] = b[i] - 1
print(sum(b))


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


import os, sys, string, math

def main(a):
	if not a:
		return 0
	last = a[-1]
	count = last
	for i in range(len(a) - 2, -1, -1):
		if last == 0:
			last = 0
		else:
			last = min(a[i], last - 1)
		count += last
	return count

def parse():
	n = input()
	a = [int(i) for i in input().split(' ')]
	return a
print(main(parse()))


import sys
n = int(input())
a = list(map(int, sys.stdin.readline().split()))
rs = a[-1]
last = a[-1]
for i in range(n - 2, -1, -1):
	last -= 1
	last = min(last, a[i])
	rs += max(0, last)
print(rs)


n = int(input())
a = list(map(int, input().split()))
mn = [0] * (n + 1)
mn[n] = 10 ** 10
for i in range(n - 1, 0, -1):
	mn[i] = min(a[i], mn[i + 1])
ans = 0
prev = 10 ** 10
for i in range(n - 1, -1, -1):
	cur = max(min(a[i], prev - 1), 0)
	ans += cur
	prev = cur
print(ans)


n = int(input())
a = list(map(int, input().split()))
sum = 0
k = 0
for i in range(n - 1, -1, -1):
	if i == n - 1:
		sum += a[i]
		k = a[i]
	elif a[i] < k:
		sum += a[i]
		k = a[i]
	elif k == 1:
		pass
	else:
		sum += k - 1
		k -= 1
print(sum)


n = int(input())
li = list(map(int, input().split()))
i = n - 1
c = 0
while i >= 0:
	if i == n - 1:
		c += li[n - 1]
	else:
		if li[i] >= li[i + 1]:
			li[i] = max(0, li[i + 1] - 1)
		c += li[i]
	i -= 1
print(c)


(n, a) = (int(input()), list(reversed(list(map(int, input().split())))))
for i in range(1, n):
	a[i] = max(0, min(a[i - 1] - 1, a[i]))
print(sum(a))


n = int(input())
a = [int(x) for x in input().split()]
x = [0] * n
x[n - 1] = a[n - 1]
small = a[n - 1]
for i in range(n - 1, 0, -1):
	if small == 0:
		break
	if a[i - 1] >= small:
		x[i - 1] = small - 1
		small -= 1
	else:
		x[i - 1] = a[i - 1]
		small = x[i - 1]
print(sum(x))


n = int(input())
a = [int(num) for num in input().split()]
sum = a[n - 1]
for i in range(n - 2, -1, -1):
	if a[i] >= a[i + 1]:
		if a[i + 1] > 0:
			a[i] = a[i + 1] - 1
		else:
			a[i] = 0
	sum = sum + a[i]
print(sum)


class CodeforcesTask1139BSolution:

	def __init__(self):
		self.result = ''
		self.n = 0
		self.a = []

	def read_input(self):
		self.n = int(input())
		self.a = [int(x) for x in input().split(' ')]

	def process_task(self):
		p = 0
		mx = 10 ** 10
		for a in self.a[::-1]:
			if a < mx:
				mx = a
				p += a
			else:
				mx = max(0, mx - 1)
				p += mx
		self.result = str(p)

	def get_result(self):
		return self.result
Solution = CodeforcesTask1139BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


import math

def test(val):
	val.reverse()
	tot = 0
	curv = 1000000001
	for i in range(len(val)):
		curv = max(0, min(curv - 1, val[i]))
		tot += curv
	print(tot)
w = int(input(''))
lst = input('').split(' ')
lst = [int(i) for i in lst]
test(lst)


n = int(input())
a = list(map(int, input().split()))
res = [a.pop()]
for ai in a[::-1]:
	if ai < res[-1]:
		res.append(ai)
	else:
		res.append(max(res[-1] - 1, 0))
print(sum(res))


n = int(input())
arr = [int(p) for p in input().split()]
final = []
for i in range(len(arr) - 1, -1, -1):
	if i == len(arr) - 1:
		final.append(arr[i])
	else:
		final.append(min(arr[i], max(final[-1] - 1, 0)))
print(sum(final))


n = int(input())
a = list(map(int, input().split()))
res = 0
x = a[-1] + 1
for i in reversed(a):
	if x:
		x = min(i, x - 1)
	res += x
print(res)


shoc_types = int(input())
shocolades = input().split()
choc = 0
max_shoc = int(shocolades[-1])
for i in reversed(shocolades):
	choc += min(max_shoc, int(i))
	max_shoc = max(min(max_shoc, int(i)) - 1, 0)
print(choc)


input()
X = list(map(int, input().split()))
(SUM, Last) = (X[-1], X[-1])
for i in range(len(X) - 2, -1, -1):
	Last = X[i] if X[i] < Last else Last - 1
	SUM += Last
	if Last == 0:
		break
print(SUM)


n = int(input())
a = [int(x) for x in input().split()]
a = a[::-1]
curr = a[0]
score = curr
for i in range(1, n):
	curr = min(curr - 1, a[i])
	if curr <= 0:
		break
	score += curr
print(score)


n = int(input())
a = input().split()
ans = int(a[n - 1])
cur = int(a[n - 1])
for i in range(n - 2, -1, -1):
	if int(a[i]) < cur and cur > 1:
		cur = int(a[i])
		ans += cur
	elif int(a[i]) >= cur and cur > 1:
		cur -= 1
		ans += cur
print(ans)


n = int(input())
a = list(map(int, input().split()))
res = 0
m = 1000000001
for i in range(n - 1, -1, -1):
	m = max(0, min(m - 1, a[i]))
	res += m
print(res)


import math
n = int(input())
a = list(map(int, input().split()))
answer = a[-1]
last = a[-1]
for i in range(n - 2, -1, -1):
	if a[i] >= last:
		last = max(0, last - 1)
		answer += last
	else:
		answer += a[i]
		last = a[i]
print(answer)


n = int(input())
numbers = []
input_numbers = input().split(' ')
for i in range(n):
	numbers.append(int(input_numbers[i]))
total_num = numbers[-1]
for i in range(n - 1):
	k = -1 * (i + 2)
	if numbers[k] == numbers[k + 1]:
		numbers[k] -= 1
	elif numbers[k] > numbers[k + 1]:
		numbers[k] = numbers[k + 1] - 1
for i in range(n):
	if numbers[i] < 0:
		numbers[i] = 0
total_num = sum(numbers)
print(total_num)


n = int(input())
arr = [int(x) for x in input().split()]
xrr = [arr[n - 1]] * n
for i in range(n - 2, -1, -1):
	xrr[i] = max(0, min(xrr[i + 1] - 1, arr[i]))
print(sum(xrr))


n = int(input())
a = list(map(int, input().split()))
i = n - 1
ans = 0
mi = float('INF')
while i > -1:
	if a[i] < mi:
		ans += a[i]
		mi = a[i]
	else:
		ans += max(mi - 1, 0)
		mi = mi - 1
	i -= 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
mi = int(1000000000.0 + 1)
for i in range(len(a) - 1, -1, -1):
	if a[i] < mi:
		mi = min(a[i], mi)
	else:
		a[i] = mi - 1 if mi > 0 else 0
		mi -= 1 if mi > 0 else 0
print(sum(a))


n = int(input())
a = [int(i) for i in input().split()]
s = 0
smax = 0
prev = 0
i = n
j = a[i - 1]
if prev < j:
	curr = j
	idx = i - 1
	s = 0
	while curr > 0 and idx >= 0:
		s += curr
		idx = idx - 1
		curr = min(a[idx], curr - 1)
	if s > smax:
		smax = s
prev = j
print(smax)


from math import ceil
n = int(input())
a = list(map(int, input().split()))
choco = a[n - 1]
xj = a[n - 1]
for i in range(n - 2, -1, -1):
	if a[i] < xj:
		choco += a[i]
		xj = a[i]
	else:
		choco += max(0, xj - 1)
		xj -= 1
print(choco)


n = int(input())
arr = list(map(int, input().split(' ')))
counter = 0
last = arr[-1]
counter += arr[-1]
for i in range(len(arr) - 2, -1, -1):
	if last != 0:
		counter += min(arr[i], last - 1)
		last = min(arr[i], last - 1)
print(counter)


n = int(input())
l = list(map(int, input().strip().split()))
ct = l[-1]
mx = l[-1]
for i in range(n - 2, -1, -1):
	if mx == 1:
		break
	elif l[i] < mx:
		ct += l[i]
		mx = l[i]
	else:
		ct += mx - 1
		mx -= 1
print(ct)


c = 0
n = int(input())
a = input().split()
a = a[::-1]
for i in range(len(a)):
	a[i] = int(a[i])
for i in range(1, len(a)):
	if a[i] >= a[i - 1]:
		a[i] = a[i - 1] - 1
	if a[i] == 0:
		for j in range(i, len(a)):
			a[j] = 0
		break
	else:
		continue
for i in range(len(a)):
	c += a[i]
print(c)


n = int(input())
t = [int(x) for x in input().split()]
c = 0
for i in range(n - 1, -1, -1):
	if t[i] > 0:
		c += t[i]
	if t[i - 1] >= t[i]:
		t[i - 1] = t[i] - 1
print(c)


n = int(input())
a = list(map(int, input().split()))
ans = 0
m = 2 * 10 ** 9
for i in range(len(a) - 1, -1, -1):
	m = min(a[i], m - 1) if m > 0 else 0
	ans += m
print(ans)


a = int(input())
l = list(map(int, input().split()))[::-1]
t = l[0]
z = l[0] - 1
for i in range(1, a):
	if z <= l[i]:
		t += z
		z -= 1
		if z < 0:
			t -= z + 1
			break
	else:
		t += l[i]
		z = l[i] - 1
		if l[i] in [0, 1]:
			break
print(t)


n = int(input())
array = list(map(int, input().split()))
peak = array[-1]
res = peak
for i in range(n - 1, 0, -1):
	if array[i - 1] < peak:
		peak = array[i - 1]
	else:
		peak = peak - 1
	if peak > 0:
		res += peak
print(res)


import sys
n = int(sys.stdin.readline())
arr = list(map(int, sys.stdin.readline().split()))
ans = arr[n - 1]
now = arr[n - 1]
for i in range(n - 2, -1, -1):
	if now - 1 < 0:
		break
	ans += min(now - 1, arr[i])
	now = min(now - 1, arr[i])
print(ans)


n = int(input())
l = list(map(int, input().split()))
total = 0
temp = 2000000000
for i in reversed(range(n)):
	if l[i] < temp and temp > 0:
		temp = l[i]
		total += temp
	elif l[i] >= temp and temp > 0:
		temp = temp - 1
		total += temp
print(str(total))


def main():
	n = int(input())
	a = list(map(int, input().split(' ')))
	ans = a[-1]
	threshold = max(a[-1] - 1, 0)
	for i in range(-2, -n - 1, -1):
		ans += min(threshold, a[i])
		threshold = max(min(threshold, a[i]) - 1, 0)
	print(ans)
	return 0
exit(main())


n = int(input())
arr = list(map(int, input().split()))
ans = arr[-1]
val = max(arr[-1] - 1, 0)
for i in range(n - 2, -1, -1):
	if arr[i] > val:
		ans += val
		val = max(val - 1, 0)
	else:
		ans += arr[i]
		val = max(arr[i] - 1, 0)
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
now = 10000000000
for i in reversed(a):
	if i >= now:
		now -= 1
		if now >= 0:
			ans += now
	else:
		now = i
		if now >= 0:
			ans += now
print(ans)


n = int(input())
a = list(map(int, input().split()))
sch = 0
m = 10 ** 9 + 1
for i in range(n - 1, -1, -1):
	if a[i] >= m and m != 0:
		m = m - 1
		sch += m
	elif a[i] < m:
		sch += a[i]
		m = a[i]
print(sch)


n = int(input())
x = input()
x = x.split(' ')
x = list(map(int, x))
ans = x[-1]
prev = x[-1]
for i in range(len(x) - 2, -1, -1):
	prev -= 1
	prev = min(prev, x[i])
	if prev <= 0:
		break
	ans += prev
print(ans)


x = int(input())
s = list(map(int, input().split()))[::-1]
for n in range(1, x):
	s[n] = max(0, min(s[n - 1] - 1, s[n]))
print(sum(s))


I = lambda : list(map(int, input().split()))
(n,) = I()
l = I()
ans = l[-1]
temp = ans
for i in range(n - 2, -1, -1):
	t = l[i]
	if t < temp:
		ans += t
		temp = t
	else:
		ans += max(temp - 1, 0)
		temp = max(temp - 1, 0)
print(ans)


n = int(input())
lst = [int(x) for x in input().split()]
lim = lst[n - 1]
ans = lst[n - 1]
for i in range(n - 2, -1, -1):
	if lim < 1:
		lim = 1
	if lst[i] < lim:
		ans += lst[i]
		lim = lst[i]
	else:
		ans += lim - 1
		lim -= 1
print(ans)


def main():
	n = int(input())
	arr = input().split(' ')
	for i in range(len(arr)):
		arr[i] = int(arr[i])
	x = [0 for i in range(n + 1)]
	x[n] = 10 ** 9 + 1
	for i in range(n - 1, -1, -1):
		x[i] = max(0, min(x[i + 1] - 1, arr[i]))
	print(sum(x) - x[n])
main()


n = int(input())
p = input().rstrip().split(' ')
P = 0
X = 0
U = 0
for i in range(len(p) - 1, 0, -1):
	if int(p[i]) > int(p[i - 1]):
		P = P + int(p[i])
		X += 1
	else:
		P = P + int(p[i])
		U = 1
		X += 1
		break
if U == 0:
	P = P + int(p[0])
	print(P)
else:
	F = p[0:i]
	D = int(p[i]) - 1
	if D != 0:
		for i in range(len(F) - 1, -1, -1):
			if D == 0:
				break
			if int(F[i]) >= D:
				P += D
				D -= 1
			else:
				P += int(F[i])
				D = int(F[i]) - 1
	print(P)


n = int(input())
a = list(map(int, input().split()))
a.reverse()
k = a[0]
for i in range(1, len(a)):
	m = a[i - 1]
	if a[i] < m:
		k = k + a[i]
	elif a[i] >= m:
		if m == 0:
			a[i] = m
		else:
			a[i] = m - 1
		k = k + a[i]
print(k)


import sys
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
a = list(map(int, input().split()))
ans = 0
last = None
for i in range(n - 1, -1, -1):
	if i == n - 1:
		ans += a[i]
		last = a[i]
	elif a[i] < last:
		ans += a[i]
		last = a[i]
	else:
		ans += max(0, last - 1)
		last = max(0, last - 1)
print(ans)


import sys

class Main:

	def __init__(self):
		self.buff = None
		self.index = 0

	def next(self):
		if self.buff is None or self.index == len(self.buff):
			self.buff = sys.stdin.readline().split()
			self.index = 0
		val = self.buff[self.index]
		self.index += 1
		return val

	def next_int(self):
		return int(self.next())

	def solve(self):
		n = self.next_int()
		x = [self.next_int() for x in range(0, n)]
		r = 0
		cr = x[n - 1] + 1
		for d in x[::-1]:
			cr = max(0, min(d, cr - 1))
			r += cr
		print(r)
Main().solve()


n = int(input())
arr = list(map(int, input().split(' ')))
ans = arr[n - 1]
present = arr[n - 1]
for i in range(n - 2, -1, -1):
	present = min(present - 1, arr[i])
	if present < 0:
		ans += 0
	else:
		ans += present
print(ans)


n = int(input())
a = [int(i) for i in input().split()]
a = a[::-1]
for i in range(n - 1):
	if a[i + 1] < a[i]:
		continue
	elif a[i + 1] >= a[i]:
		a[i + 1] = a[i] - 1
		if a[i + 1] < 0:
			a[i + 1] = 0
print(sum(a))


n = int(input())
nn = list(map(int, input().rstrip().split()))
nn = nn[::-1]
summ = nn[0]
prevv = nn[0]
for i in range(1, n):
	if nn[i] >= prevv and prevv > 0:
		summ += prevv - 1
		prevv = prevv - 1
	elif nn[i] < prevv:
		prevv = nn[i]
		summ += nn[i]
	elif nn[i] >= prevv and prevv == 0:
		summ += 0
		prevv = 0
print(summ)


N = int(input())
s = [int(x) for x in input().split()]
s = s[::-1]
ans = 0
temp = 1000000007
for i in range(0, len(s)):
	if s[i] < temp and temp != 0:
		ans = ans + s[i]
		temp = s[i]
	elif temp == 0:
		continue
	elif temp != 0 and s[i] >= temp:
		ans = ans + temp - 1
		temp = temp - 1
print(ans)


a = input()
b = input()
n = [int(i) for i in b.split()]
o = [100000000000000000]
n.reverse()
for i in range(len(n)):
	if o[-1] > n[i]:
		o.append(n[i])
	elif o[i] - 1 >= 0:
		o.append(o[i] - 1)
	else:
		break
print(sum(o) - 100000000000000000)


import sys
sys.setrecursionlimit(10 ** 9)
input = sys.stdin.readline
N = int(input())
L = list(map(int, input().split()))
ans = 0
prev = float('inf')
for l in reversed(L):
	if prev <= 1:
		break
	if l < prev:
		ans += l
		prev = l
	else:
		ans += prev - 1
		prev -= 1
print(ans)


from sys import stdin
inp = lambda : stdin.readline().strip()
n = int(inp())
a = [int(x) for x in inp().split()]
prev = a[-1]
ans = a[-1]
for i in range(n - 2, -1, -1):
	prev = max(min(prev - 1, a[i]), 0)
	ans += prev
print(ans)


n = int(input())
a = list(map(int, input().split()))
summ = 0
i = n - 1
minn = a[i]
while i >= 0:
	if minn >= a[i]:
		summ += a[i]
		minn = a[i] - 1
	else:
		summ += minn
		minn = max(0, minn - 1)
	i -= 1
print(summ)


import sys
from collections import defaultdict as dd
from itertools import permutations as pp
from itertools import combinations as cc
from collections import Counter as ccd
from random import randint as rd
from bisect import bisect_left as bl
import heapq
mod = 10 ** 9 + 7

def ri(flag=0):
	if flag == 0:
		return [int(i) for i in sys.stdin.readline().split()]
	else:
		return int(sys.stdin.readline())
n = ri(1)
a = ri()
c = 0
last = 0
ans = 0
ans = [a[-1]]
for i in range(n - 2, -1, -1):
	ans.append(min(ans[-1] - 1, a[i]))
for i in ans:
	if i > 0:
		c += i
print(c)


n = int(input())
l = input().split()
li = [int(i) for i in l]
li.reverse()
x = []
for i in range(n):
	if i == 0:
		x.append(li[i])
		continue
	z = min(x[-1] - 1, li[i])
	if z == 0:
		break
	x.append(z)
print(sum(x))


def func(arr, n):
	prev = arr[n - 1]
	s = arr[n - 1]
	for i in range(n - 2, -1, -1):
		temp = min(arr[i], prev - 1)
		temp = max(0, temp)
		s += temp
		prev = temp
		if temp == 1:
			return s
	return s
n = int(input())
a = list(map(int, input().split()))
print(func(a, n))


import sys
import math
import bisect

def solve(A):
	n = len(A)
	for i in range(n - 2, -1, -1):
		if A[i + 1] == 0:
			A[i] = 0
		else:
			A[i] = min(A[i], A[i + 1] - 1)
	return sum(A)

def main():
	n = int(input())
	A = list(map(int, input().split()))
	print(solve(A))
main()


n = int(input())
a = list(map(int, input().split()))
a = a[::-1]
r = a[0]
c = r
for i in range(1, len(a)):
	if a[i] < r:
		c += a[i]
		r = a[i]
	else:
		r = r - 1
		if r > 0:
			c += r
print(c)


def solve(xs):
	last = xs[-1]
	count = xs[-1]
	for x in xs[-2::-1]:
		last = max(0, min(x, last - 1))
		count += last
	return count
n = int(input())
xs = list(map(int, input().split()))[:n]
print(solve(xs))


a = int(input())
co = [int(x) for x in input().split()]
last = 10 ** 10
res = 0
for c in reversed(co):
	last = max(min(c, last - 1), 0)
	res += last
print(res)


n = int(input())
a = list(map(int, input().split()))
pos = 10 ** 10
count = 0
for i in range(n - 1, -1, -1):
	pos = max(min(pos - 1, a[i]), 0)
	count = count + pos
print(count)


n = int(input())
a = [int(x) for x in input().split()]
pr = a[-1] + 1
s = 0
for i in range(n - 1, -1, -1):
	pr = max(0, min(pr - 1, a[i]))
	s += pr
print(s)


from sys import stdin
n = int(stdin.readline().strip())
l = list(map(int, stdin.readline().split()))
s = 0
for i in range(n - 1, -1, -1):
	if i == n - 1:
		s += l[i]
	elif l[i + 1] == 0:
		l[i] = l[i + 1]
		s += l[i]
	elif l[i] >= l[i + 1]:
		l[i] = l[i + 1] - 1
		s += l[i]
	else:
		s += l[i]
print(s)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List
sys.setrecursionlimit(99999)
(n,) = map(int, input().split())
arr = list(map(int, input().split()))
arr.append(10 ** 9 + 5)
ans = 0
for j in range(n - 1, -1, -1):
	arr[j] = max(0, min(arr[j], arr[j + 1] - 1))
	ans += arr[j]
print(ans)


n = int(input())
A = list(map(int, input().split()))
otvet = 0
posl = A[-1]
otvet += posl
for i in range(1, len(A)):
	if A[-1 - i] < posl:
		posl = A[-1 - i]
	else:
		posl = posl - 1
	otvet += posl
	if posl == 0:
		break
print(otvet)


n = int(input().strip())
a = list(map(int, input().split()))
x = a[-1]
num = a[-1]
for i in reversed(range(len(a) - 1)):
	if x <= 0:
		break
	else:
		x = min(a[i], x - 1)
		num += x
print(num)


n = int(input())
l = [int(x) for x in input().split()]
sum = l[n - 1]
for i in range(n - 2, -1, -1):
	if l[i] < l[i + 1]:
		sum += l[i]
	elif l[i] == 0:
		continue
	elif l[i + 1] == 0:
		sum += l[i + 1]
		l[i] = l[i + 1]
	else:
		sum += l[i + 1] - 1
		l[i] = l[i + 1] - 1
print(sum)


n = int(input())
lis = [int(x) for x in input().split()]
cou = lis[-1]
ans = lis[-1]
for i in range(n - 2, -1, -1):
	cou = min(lis[i], cou - 1)
	if cou <= 0:
		break
	ans += cou
print(ans)


def solve():
	N = int(input())
	A = [int(k) for k in input().split()]
	A.reverse()
	ans = A[0]
	v = A[0]
	for i in range(1, N):
		if A[i] < v:
			v = A[i]
			ans += v
		elif v > 1:
			v -= 1
			ans += v
	print(ans)
solve()


n = int(input())
l = [int(i) for i in input().split()]
c = 0
qtd = l[len(l) - 1]
if qtd == 0:
	print(qtd)
else:
	for i in range(n - 2, -1, -1):
		if l[i] == 0:
			l[i] = l[i + 1]
			continue
		if l[i + 1] > l[i]:
			qtd += l[i]
		else:
			l[i] = max(l[i + 1] - 1, 0)
			qtd += l[i]
	print(qtd)


n = int(input())
l = list(map(int, input().split()))
ans = l[n - 1]
tmp = l[n - 1]
for i in range(n - 2, -1, -1):
	tmp1 = min(tmp - 1, l[i])
	ans += tmp1
	tmp = tmp1
	if tmp == 0:
		break
print(ans)


n = int(input())
l = [*map(int, input().split())]
m = [e for e in l]
for i in range(n - 2, -1, -1):
	m[i] = max(0, min(m[i], m[i + 1] - 1))
print(sum(m))


n = int(input())
arr = list(map(int, input().split()))
ans = 0
arr.reverse()
pre = [arr[0] + 1]
for i in range(n):
	if pre[-1] == 0:
		ans += min(pre[-1], arr[i])
		pre += [min(pre[-1], arr[i])]
	else:
		ans += min(pre[-1] - 1, arr[i])
		pre += [min(pre[-1] - 1, arr[i])]
print(ans)


from math import *
from copy import *
from string import *
from random import *
from sys import stdin
from operator import *
from itertools import *
from collections import Counter
N = int(stdin.readline())
l = list(map(int, stdin.readline().rstrip().split()))
ans = l[-1]
prev = l[-1]
for i in range(N - 2, -1, -1):
	zz = min(prev - 1, l[i])
	if zz >= prev or zz <= 0:
		zz = 0
	ans += zz
	prev = zz
print(ans)


n = input()
counter = 0
hell = 1000000007
arr = list(map(int, input().split()))
arr.reverse()
for i in arr:
	num = max(min(hell, i), 0)
	counter += num
	hell = num - 1
print(counter)


n = int(input())
a = [int(o) for o in input().split()]
a = a[::-1]
ans = a[0]
pans = a[0]
for i in range(1, n):
	if a[i] < pans:
		ans += a[i]
		pans = a[i]
	else:
		pans = max(pans - 1, 0)
		ans += pans
print(ans)


from collections import defaultdict as dd
from itertools import permutations as pp
from itertools import combinations as cc
from collections import Counter as ccd
from random import randint as rd
from bisect import bisect_left as bl
from bisect import bisect_right as br
import heapq
from math import gcd
n = int(input())
l = list(map(int, input().strip().split(' ')))
ans = []
for i in range(n - 1, -1, -1):
	if i == n - 1 or l[i] < ans[-1]:
		ans.append(l[i])
	elif l[i] >= ans[-1]:
		if ans[-1] - 1 > 0:
			ans.append(ans[-1] - 1)
		else:
			ans.append(0)
print(sum(ans))


n = int(input())
a = list(map(int, input().split()))
counter = [a[-1]]
c = 0
while n > 1:
	if a[n - 2] < counter[c]:
		counter.append(a[n - 2])
	else:
		counter.append(counter[c] - 1)
	c += 1
	if counter[c] <= 1:
		break
	n -= 1
print(sum(counter))


import sys
import math
from collections import OrderedDict

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def minput():
	return map(int, input().split())

def listinput():
	return list(map(int, input().split()))
n = iinput()
l = listinput()
l = l[::-1]
s = l[0]
for i in range(1, n):
	if l[i] < l[i - 1]:
		s += l[i]
	else:
		l[i] = l[i - 1] - 1
		s += l[i - 1] - 1
		if l[i - 1] - 1 == 0:
			break
print(s)


n = int(input())
l = [*map(int, input().split())]
Help = Answer = l[n - 1]
for i in range(n - 2, -1, -1):
	if Help == 0:
		break
	if l[i] >= Help:
		Help -= 1
		Answer += Help
	else:
		Answer += l[i]
		Help = l[i]
print(Answer)


N = int(input())
vals = [int(s) for s in input().split(' ')]
ans = vals[-1]
prev = vals[-1]
for i in range(N - 2, -1, -1):
	prev = max(0, min(prev - 1, vals[i]))
	ans += prev
print(ans)


n = int(input())
lst = list(map(int, input().split()))
ans = 0
for i in range(n - 1, -1, -1):
	if i == n - 1:
		ans += lst[i]
	else:
		lst[i] = min(lst[i], lst[i + 1] - 1)
		if lst[i] < 0:
			lst[i] = 0
		ans += lst[i]
print(ans)


n = int(input())
a = list(map(int, input().split()))
k = a[-1]
a.reverse()
ans = 0
for i in a:
	ans += max(0, min(i, k))
	k = min(i, k) - 1
print(ans)


from math import ceil, sqrt, gcd, log, floor
from collections import deque

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def li():
	return list(mi())

def msi():
	return map(str, input().strip().split(' '))

def lsi():
	return list(msi())
n = ii()
s = li()
c = s[n - 1]
x = s[n - 1] - 1
for i in range(n - 2, -1, -1):
	if x == 0:
		break
	c += min(s[i], x)
	if s[i] < x:
		x = s[i] - 1
	else:
		x -= 1
print(c)


from sys import stdin
n = stdin.readline().rstrip()
n = int(n)
a = stdin.readline().rstrip()
a = [int(x) for x in a.split(' ')]
a.reverse()
num = 0
last = True
lastCur = None
for x in a:
	if last == True:
		num += x
		last = False
		prev = x
		lastCur = x
		continue
	cur = min(x, lastCur - 1)
	cur = max(cur, 0)
	num += cur
	lastCur = cur
print(num)


def get_count2(a):
	s = a[len(a) - 1]
	if s == 0:
		return 0
	for i in range(len(a) - 2, -1, -1):
		if a[i] >= a[i + 1]:
			a[i] = max(a[i + 1] - 1, 0)
		s += a[i]
		if a[i] == 0:
			break
	return s

def main():
	input()
	x = [int(i) for i in str(input()).split()]
	print(get_count2(x))
	return
main()


n = int(input())
L = list(map(int, input().split()))
s = L[n - 1]
for i in range(n - 2, -1, -1):
	x = min(L[i + 1] - 1, L[i])
	L[i] = x
	if x > 0:
		s += x
	else:
		break
print(s)


n = int(input())
nums = [int(s) for s in input().split()]
last = nums[-1]
n_choco = nums[-1]
for i in range(n - 2, -1, -1):
	if nums[i] >= last:
		last = max(0, last - 1)
		n_choco += last
	else:
		last = nums[i]
		n_choco += last
print(n_choco)


def ans(arr):
	cnt = arr[-1]
	summ = arr[-1]
	if arr[-1] == 1:
		return cnt
	for i in range(n - 2, -1, -1):
		if arr[i] < summ:
			cnt += arr[i]
			summ = arr[i]
		else:
			cnt += max(summ - 1, 0)
			summ = max(0, summ - 1)
	return cnt
n = int(input())
a = list(map(int, input().strip().split()))
print(ans(a))


types_choco = int(input())
amount_choco = list(map(int, input().split()))
getted_choco = [amount_choco[-1]]
for i in range(len(amount_choco) - 2, -1, -1):
	if min(amount_choco[i], getted_choco[-1] - 1) < 0:
		getted_choco.append(0)
	else:
		getted_choco.append(min(amount_choco[i], getted_choco[-1] - 1))
print(sum(getted_choco))


n = int(input())
c = []
k = 0
p = 1000000000000
for v in input().split()[::-1]:
	p = max(0, min(p - 1, int(v)))
	k += p
print(k)


import math
from collections import deque, defaultdict
from sys import stdin, stdout
input = stdin.readline
listin = lambda : list(map(int, input().split()))
mapin = lambda : map(int, input().split())
n = int(input())
a = listin()
for i in range(n - 2, -1, -1):
	if a[i] >= a[i + 1]:
		a[i] = max(a[i + 1] - 1, 0)
print(sum(a))


n = int(input())
arr = [int(i) for i in input().split()]
s = 0
mm = arr[n - 1] + 1
for i in range(n):
	if arr[n - i - 1] < mm:
		mm = arr[n - i - 1]
		s += mm
	elif mm == 1:
		break
	else:
		mm -= 1
		s += mm
print(s)


n = input()
a = list(map(int, input().split()))
limit = float('inf')
current = 0
for e in a[::-1]:
	stock_to_buy = max(min(e, limit - 1), 0)
	limit = stock_to_buy
	current += stock_to_buy
print(current)


def choco_cf():
	n = int(input())
	l = list(map(int, input().split()))
	l.reverse()
	s = l[0]
	for i in range(1, n):
		if l[i] >= l[i - 1]:
			l[i] = l[i - 1] - 1
		if l[i] < 1:
			break
		s += l[i]
	return s
print(choco_cf())


types_nr = int(input())
type_id___nr = [int(x) for x in input().split()]

def recursive_solve(prefix, bound):
	if len(prefix) == 0:
		return 0
	curr_added = min(prefix[-1], bound)
	return curr_added + recursive_solve(prefix[:-1], max(curr_added - 1, 0))
bound = type_id___nr[-1]
ans = 0
for choco_nr in type_id___nr[::-1]:
	ans += min(choco_nr, bound)
	bound = min(choco_nr, bound) - 1
	if bound <= 0:
		break
print(ans)


n = int(input())
l = list(map(int, input().split()))
c = 0
l = list(reversed(l))
k = l[0]
for x in l:
	if x > k:
		c += k
		k = k - 1
		if k == 0:
			break
	else:
		c += x
		k = x - 1
		if k == 0:
			break
print(c)


from sys import *
from math import *
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
b = [0] * n
b[n - 1] = a[n - 1]
m = a[n - 1]
for i in range(n - 1, -1, -1):
	if a[i] < m:
		m = a[i]
	b[i] = m
for i in range(n - 2, -1, -1):
	if b[i] >= b[i + 1]:
		b[i] = b[i + 1] - 1
	b[i] = max(0, b[i])
print(sum(b))


from __future__ import division, print_function
from sys import stdin, stdout
import itertools, os, sys, threading
from collections import deque, Counter, OrderedDict, defaultdict
import heapq
from math import ceil, floor, log, sqrt, factorial, pow, pi, gcd

def ii():
	return int(input())

def si():
	return str(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def fii():
	return int(stdin.readline())

def fsi():
	return str(stdin.readline())

def fmi():
	return map(int, stdin.readline().split())

def fli():
	return list(fmi())
abc = 'abcdefghijklmnopqrstuvwxyz'
abd = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25}
mod = 1000000007
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def getKey(item):
	return item[0]

def sort2(l):
	return sorted(l, key=getKey)

def d2(n, m, num):
	return [[num for x in range(m)] for y in range(n)]

def isPowerOfTwo(x):
	return x and (not x & x - 1)

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def ntl(n):
	return [int(i) for i in str(n)]

def powerMod(x, y, p):
	res = 1
	x %= p
	while y > 0:
		if y & 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
graph = defaultdict(list)
visited = [0] * 1000000
col = [-1] * 1000000

def dfs(v, c):
	if visited[v]:
		if col[v] != c:
			print('-1')
			exit()
		return
	col[v] = c
	visited[v] = 1
	for i in graph[v]:
		dfs(i, c ^ 1)

def bfs(d, v):
	q = []
	q.append(v)
	visited[v] = 1
	while len(q) != 0:
		x = q[0]
		q.pop(0)
		for i in d[x]:
			if visited[i] != 1:
				visited[i] = 1
				q.append(i)
		print(x)
	print(l)

def make_graph(e):
	d = {}
	for i in range(e):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
		if y not in d.keys():
			d[y] = [x]
		else:
			d[y].append(x)
	return d

def gr2(n):
	d = {}
	for i in range(n):
		(x, y) = mi()
		if x not in d.keys():
			d[x] = [y]
		else:
			d[x].append(y)
	return d

def connected_components(graph):
	seen = set()

	def dfs(v):
		vs = set([v])
		component = []
		while vs:
			v = vs.pop()
			seen.add(v)
			vs |= set(graph[v]) - seen
			component.append(v)
		return component
	ans = []
	for v in graph:
		if v not in seen:
			d = dfs(v)
			ans.append(d)
	return ans

def primeFactors(n):
	s = set()
	while n % 2 == 0:
		s.add(2)
		n = n // 2
	for i in range(3, int(sqrt(n)) + 1, 2):
		while n % i == 0:
			s.add(i)
			n = n // i
	if n > 2:
		s.add(n)
	return s
n = ii()
l = li()[::-1]
for i in range(1, n):
	if l[i] >= l[i - 1]:
		l[i] = l[i - 1] - 1
	if l[i] == 0:
		l = l[:i + 1]
		break
print(sum(l))


n = input()
b = input()
a = [int(i) for i in b.split()]
n = int(n)
t = 0
q = 10000000007
for i in range(n):
	t = t + min(q, a[n - 1 - i])
	q = max(0, min(q - 1, a[n - 1 - i] - 1))
print(t)


def chocolate_choosing(n, a):
	a = a[::-1]
	for x in range(1, n):
		if a[x] >= a[x - 1]:
			if a[x - 1] - 1 >= 0:
				a[x] = a[x - 1] - 1
			else:
				a[x] = 0
	return sum(a)
n = int(input())
a = list(map(int, input().split()))
print(chocolate_choosing(n, a))


n = int(input())
l = list(map(int, input().split()))
sum1 = l[-1]
for i in range(len(l) - 1, 0, -1):
	if l[i] == 0:
		break
	l[i - 1] = min(l[i] - 1, l[i - 1])
	sum1 = sum1 + l[i - 1]
print(sum1)


from math import inf, sqrt, floor, ceil
from collections import Counter, defaultdict, deque
from heapq import heappush as hpush, heappop as hpop, heapify as h
from operator import itemgetter
from itertools import product
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1, -1, -1):
	if i == n - 1:
		ans += a[i]
		prev = a[i]
	elif a[i] < prev:
		ans += a[i]
		prev = a[i]
	else:
		x = prev - 1
		if x:
			ans += x
			prev = x
		if x < 0:
			prev = 0
print(ans)


ii = lambda : int(input())
mi = lambda : map(int, input().split())
li = lambda : list(mi())
n = ii()
l = li()
s = l[n - 1]
temp = s - 1
for i in range(n - 2, -1, -1):
	if temp == 0:
		break
	if l[i] < temp:
		s += l[i]
		temp = l[i] - 1
	elif l[i] >= temp:
		s += temp
		temp -= 1
print(s)


n = int(input())
a = list(map(int, input().split()))
mx = a[-1]
t = a[-1]
for i in range(n - 2, -1, -1):
	if a[i] < t:
		mx += a[i]
		t = a[i]
	elif t - 1:
		mx += t - 1
		t = t - 1
	else:
		break
print(mx)


n = int(input())
l = list(map(int, input().split()))
ans = 0
mini = l[-1]
num = 0
for i in range(n - 1, -1, -1):
	if mini == 0:
		break
	if i == n - 1:
		ans += l[i]
	elif l[i] < mini:
		ans += l[i]
		mini = l[i]
	else:
		mini -= 1
		ans += mini
		mini = min(mini, l[i])
	num = max(num, ans)
if ans > num:
	num = ans
print(num)


def f(n, arr):
	now_max = arr[-1]
	sum = now_max
	for i in reversed(range(len(arr) - 1)):
		if arr[i] < now_max:
			now_max = arr[i]
			sum += arr[i]
		else:
			if now_max > 0:
				now_max -= 1
			sum += now_max
	return sum
n = int(input())
arr = list(map(int, input().split()))
print(f(n, arr))


import math
import bisect

def argsort(ls):
	return sorted(range(len(ls)), key=ls.__getitem__)

def f(p=0):
	if p == 0:
		return int(input())
	if p == 1:
		return map(int, input().split())
	elif p == 2:
		return list(map(int, input().split()))
	else:
		return sorted(list(map(int, input().split())))
n = f()
cl = f(2)
pre = -1
res = 0
for i in range(n - 1, -1, -1):
	if pre == -1:
		pre = cl[i]
		res = pre
	else:
		t = max(min(cl[i], pre - 1), 0)
		res += t
		pre = t
print(res)


n = int(input(''))
t = [int(x) for x in input('').split(' ')]
if t[-1] == 0:
	print(0)
else:
	sumi = t[-1]
	mini = t[-1]
	for i in range(1, n):
		if mini - 1 >= t[-i - 1]:
			mini = t[-i - 1]
		else:
			mini = max(mini - 1, 0)
		sumi += mini
	print(sumi)


n = int(input())
l = list(map(int, input().split()))
s = l[-1]
m = l[-1] - 1
for i in range(n - 2, -1, -1):
	if l[i] <= m and m > 0:
		s = s + l[i]
		m = l[i] - 1
	elif l[i] > m and m > 0:
		s = s + m
		m = m - 1
print(s)


n = int(input())
a = list(map(int, input().split()))
res = 0
max_now = 10 ** 9 + 10
for i in range(n - 1, -1, -1):
	if a[i] < max_now:
		res += a[i]
		max_now = a[i]
	else:
		max_now -= 1
		res += max_now
	if max_now == 0:
		break
print(res)


def ok(arr1):
	n = len(arr1)
	ans = 0
	for i in range(n - 1, 0, -1):
		if arr1[i] <= arr1[i - 1]:
			arr1[i - 1] = arr1[i] - 1 if arr1[i] - 1 >= 0 else 0
	return sum(arr1)
n = int(input())
arr1 = list(map(int, input().split()))
print(ok(arr1))


from sys import stdin, stdout
n = int(stdin.readline())
a = [int(x) for x in stdin.readline().split()]
ans = 0
pre = 1234567890
for i in a[::-1]:
	cur = max(0, min(pre - 1, i))
	ans += cur
	pre = cur
print(ans)


n = int(input())
a = list(map(int, input().split()))
co = a[-1]
tco = a[-1]
for i in range(2, n + 1):
	tco = min(tco - 1, a[-i])
	if tco:
		co += tco
	else:
		break
print(co)


n = int(input())
a = list(map(int, input().split()))
m = a[n - 1]
c = a[n - 1]
for i in range(n - 2, -1, -1):
	if a[i] < m and m >= 0:
		c += a[i]
		m = a[i]
	elif a[i] >= m and m > 0:
		c += m - 1
		m -= 1
print(c)


from collections import deque, defaultdict

def prefix_sum(a):
	res = 0
	s = []
	for i in a:
		res = res + i
		s.append(res)
	return s

def bfs(g, v, s):
	q = deque()
	q.append(s)
	while q:
		vertex = q.popleft()
		for i in g[vertex]:
			if v[i] == False:
				print(i)
				q.append(i)
				v[i] = True
	print(g, v, s)
t = int(input())
p = list(map(int, input().split()))
if t == 1:
	print(p[0])
else:
	choco = p[-1]
	mini = p[-1]
	for i in range(t - 2, -1, -1):
		if p[i] < mini:
			choco = choco + p[i]
			mini = p[i]
		elif mini - 1 > 0:
			choco = choco + (mini - 1)
			mini = mini - 1
	print(choco)


n = int(input())
l = list(map(int, input().split()))
tmp = 0
tmp = l[n - 1]
for i in range(n - 1, 0, -1):
	if l[i - 1] < l[i]:
		tmp += l[i - 1]
	elif l[i - 1] >= l[i]:
		tmp += l[i] - 1
		l[i - 1] = l[i] - 1
		if l[i - 1] == 0:
			break
print(tmp)


N = int(input())
Sum = 0
L = [int(x) for x in input().split()]
Sum += L[-1]
i = 2
x = L[-1]
while i <= N:
	Sum += min(L[-i], max(x - 1, 0))
	x = min(L[-i], max(x - 1, 0))
	i += 1
print(Sum)


n = int(input())
arr = list(map(int, input().split()))
curr = arr[-1]
ans = 0
for elem in arr[::-1]:
	ans += min(elem, curr)
	curr = min(elem, curr) - 1
	if curr <= 0:
		break
print(ans)


n = int(input())
arr = list(map(int, input().split()))
(best, ans) = (float('inf'), 0)
for i in range(n - 1, -1, -1):
	ans += min(arr[i], best)
	best = min(arr[i], best) - 1
	if best == 0:
		break
print(ans)


n = int(input().strip())
a = list(map(int, input().strip().split()))
a.reverse()
pr = 10 ** 10
ans = 0
for chocolate in a:
	if chocolate < pr:
		ans += chocolate
		pr = chocolate
	else:
		pr = pr - 1
		ans += pr
	if pr == 0:
		break
print(ans)


import sys
n = int(sys.stdin.readline())
arr = [int(i) for i in sys.stdin.readline().split()]
res = arr[-1]
remember = arr[-1]
arr.pop(-1)
if remember == 1:
	print(1)
else:
	for i in arr[::-1]:
		if remember == 1:
			break
		if i == 1:
			res += 1
			break
		if i >= remember:
			res += remember - 1
			remember -= 1
		else:
			res += i
			remember = i
	print(res)


n = int(input())
L = [int(x) for x in input().split()]
c = 0
for i in range(n - 1, -1, -1):
	if i == n - 1:
		c += L[i]
	elif L[i] >= L[i + 1] and L[i + 1] != 0:
		d = L[i] - L[i + 1] + 1
		m = L[i] - d
		c += m
		L[i] = m
	elif L[i + 1] == 0:
		L[i] = 0
		c += 0
	else:
		c += L[i]
print(c)


n = int(input())
a = [int(i) for i in input().split(' ')]
ans = a[-1]
f = a[-1]
for i in range(n - 2, -1, -1):
	if f <= 0:
		break
	if a[i] == 0:
		break
	if a[i] < f:
		f = a[i]
		ans += a[i]
	else:
		ans += f - 1
		f = f - 1
print(ans)


from sys import stdin, stdout
from collections import defaultdict, Counter, deque
from bisect import bisect, bisect_left
import math
from itertools import permutations
import queue
I = stdin.readline
n = int(I())
arr = [int(x) for x in I().split()]
ans = arr[-1]
least = arr[-1]
for i in range(n - 2, -1, -1):
	if arr[i] >= least:
		arr[i] = least - 1
	least = arr[i]
	if least == 0:
		break
	ans += arr[i]
print(ans)


n = int(input())
c = list(map(int, input().split()))
cur = 10000000000.0
tot = 0
for i in range(n - 1, -1, -1):
	cur = min(cur - 1, c[i])
	tot += max(cur, 0)
print(tot)


num = int(input())
li = input().split(' ')
for ind in range(len(li)):
	li[ind] = int(li[ind])
li.reverse()
bar = li[0] + 1
res = 0
for ele in li:
	if ele >= bar:
		ele = bar - 1
	if ele > 0:
		res += ele
	bar = ele
print(res)


(n, a) = (int(input()), list(map(int, input().split())))
while n > 1:
	if a[n - 2] >= a[n - 1]:
		a[n - 2] = a[n - 1] - 1
		if a[n - 2] == 0:
			a = a[n - 2:]
			break
	n -= 1
print(sum(a))


input()
amounts = list(map(int, input().split()))
chocolate_amount = 0
current_max = amounts[len(amounts) - 1]
for i in amounts[::-1]:
	chocolate_amount += min(current_max, i)
	current_max = min(current_max, i) - 1
	if current_max < 0:
		current_max = 0
print(chocolate_amount)


import sys
import math

def getAndParseInt(num=1):
	string = sys.stdin.readline().strip()
	if num == 1:
		return int(string)
	else:
		return [int(part) for part in string.split()]

def getAndParseString(num=1, delim=' '):
	string = sys.stdin.readline().strip()
	if num == 1:
		return string
	else:
		return [part for part in string.split(delim)]
n = getAndParseInt()
array = getAndParseInt(2)
min_so_far = array[-1]
total = 0
for cap in reversed(array):
	fill = min(cap, min_so_far)
	total += fill
	min_so_far = max(fill - 1, 0)
print(total)


intin = lambda : map(int, input().split())
Ain = lambda : list(map(int, input().split()))
n = int(input())
a = Ain()
ans = 0
cur = a[n - 1] + 1
for i in reversed(range(n)):
	cur = min(cur - 1, a[i])
	if cur <= 0:
		break
	ans += cur
print(ans)


a = int(input())
b = [int(i) for i in input().split()]
sum = 0
sum = sum + b[a - 1]
a -= 1
while a:
	if b[a] > b[a - 1]:
		sum = sum + b[a - 1]
	elif b[a] != 0:
		sum = sum + b[a] - 1
		b[a - 1] = b[a] - 1
	else:
		b[a - 1] = 0
	a -= 1
print(sum)


from bisect import bisect_left as bl
from bisect import bisect_right as br
from heapq import heappush, heappop
import math
from collections import *
from functools import reduce, cmp_to_key, lru_cache
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
M = mod = 10 ** 9 + 7

def factors(n):
	return sorted(set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0))))

def inv_mod(n):
	return pow(n, mod - 2, mod)

def li():
	return [int(i) for i in input().rstrip().split()]

def st():
	return str(input().rstrip())[2:-1]

def val():
	return int(input().rstrip())

def li2():
	return [str(i)[2:-1] for i in input().rstrip().split()]

def li3():
	return [int(i) for i in st()]
n = val()
l = li()
ans = 0
for i in range(n - 2, -1, -1):
	ans += max(0, min(l[i + 1] - 1, l[i]))
	l[i] = min(l[i], l[i + 1] - 1)
ans += l[-1]
print(ans)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	a = a[::-1]
	(ret, tmp) = (a[0], a[0])
	for i in range(1, n):
		tmp = max(0, tmp - 1)
		tmp = min(tmp, a[i])
		ret += tmp
	print(ret)
main()


n = int(input())
array = input().split(' ')
temp = int(array[n - 1])
sam = temp
for i in range(n - 2, -1, -1):
	if temp == 0:
		break
	else:
		if int(array[i]) >= temp:
			sam += temp - 1
			array[i] = temp - 1
		else:
			sam += int(array[i])
		temp = int(array[i])
print(sam)


n = int(input())
a = list(map(int, input().split()))
ans = 0
temp = float('inf')
for i in range(n - 1, -1, -1):
	if a[i] < temp:
		ans += a[i]
		temp = a[i]
	elif a[i] >= temp:
		if temp - 1 > 0:
			ans += temp - 1
			temp = temp - 1
		else:
			break
print(ans)


n = int(input())
arr = list(map(int, input().split()))
for i in range(len(arr) - 2, -1, -1):
	arr[i] = max(0, min(arr[i + 1] - 1, arr[i]))
print(sum(arr))


i = int(input()) - 1
x = list(map(int, input().split()))
while True:
	if x[i] <= x[i - 1] and i != 0:
		x[i - 1] = x[i] - 1
	if x[i - 1] == 0 or i == 0:
		break
	i -= 1
print(sum(x[i:]))


def readLine():
	a = input()
	return [int(i) for i in a.split(' ')]
n = readLine()[0]
a = readLine()
(ans, prev) = (0, a[n - 1])
for i in reversed(range(n - 1)):
	ans += prev
	prev = min(prev - 1, a[i])
	prev = max(prev, 0)
print(ans + prev)


n = int(input())
a = [int(x) for x in input().split()]
ans = a[len(a) - 1]
prev = a[len(a) - 1]
for i in range(len(a) - 1, 0, -1):
	if a[i - 1] < prev:
		ans += a[i - 1]
		prev = a[i - 1]
	elif prev > 0:
		prev = prev - 1
		ans += prev
	else:
		break
print(ans)


n = int(input())
arr = list(map(int, input().split()))
i = n - 1
curr = arr[-1]
total = curr
while i > 0:
	curr = min(arr[i - 1], curr - 1)
	total += curr
	i -= 1
	if i <= 0 or curr == 0:
		break
print(total)


amount = int(input())
array = [int(s) for s in input().split()]
ans = array[-1]
curr = array[-1]
for i in range(len(array) - 2, max(-1, len(array) - 1 - array[-1]), -1):
	if curr == 0:
		break
	if array[i] >= curr - 1:
		ans += curr - 1
		curr -= 1
	else:
		ans += array[i]
		curr = array[i]
print(ans)


n = int(input())
lis = [int(x) for x in input().split()]
li = [x for x in lis]
count = 0
for i in range(n - 1, 0, -1):
	if li[i] > li[i - 1]:
		count += li[i]
	else:
		li[i - 1] = max(0, li[i] - 1)
		count += li[i]
print(count + li[0])


n = int(input())
arr = list(reversed(list(map(int, input().split()))))
curmax = arr[0]
total = arr[0]
for i in arr[1:]:
	curmax = min(curmax - 1, i)
	total += curmax
	if curmax == 0:
		break
print(total)


n = int(input())
lst = list(map(int, input().split()))
max_chocolates = lst[n - 1]
for i in range(n - 1):
	if lst[n - 2 - i] < lst[n - 1 - i]:
		max_chocolates += lst[n - 2 - i]
		if lst[n - 2 - i] == 0 or lst[n - 2 - i] == 1:
			break
	else:
		max_chocolates += lst[n - 1 - i] - 1
		lst[n - 2 - i] = lst[n - 1 - i] - 1
		if lst[n - 1 - i] == 0 or lst[n - 1 - i] == 1:
			break
print(max_chocolates)


n = int(input())
c = list(map(int, input().split()))
for i in range(n - 1, 0, -1):
	if c[i] <= c[i - 1]:
		c[i - 1] = c[i] - 1 if c[i] - 1 >= 0 else 0
print(sum(c))


class Main:

	def __init__(self):
		pass

	@staticmethod
	def return_max_bought_chocolate(number_of_chocolate_types: int, table_of_max_amount_of_chocolate_per_type: list):
		list_rev = table_of_max_amount_of_chocolate_per_type[::-1]
		for (i, choc) in enumerate(list_rev):
			if i < int(number_of_chocolate_types) - 1 and int(list_rev[i]) <= int(list_rev[i + 1]):
				list_rev[i + 1] = str(int(list_rev[i]) - 1)
		return sum([int(x) if int(x) > 0 else 0 for x in list_rev])
n = input()
s = input()
print(Main.return_max_bought_chocolate(n, s.split()))


n = int(input().strip())
arr = list(map(int, input().strip().split()))
ans = 0
limit = 999999999999
for i in range(len(arr) - 1, -1, -1):
	limit = max(0, min(arr[i], limit - 1))
	ans += limit
print(ans)


import random

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())
n = ii()
a = li()
ans = a[n - 1]
curr = a[n - 1]
for i in range(n - 2, -1, -1):
	curr = max(0, min(curr - 1, a[i]))
	ans += curr
print(ans)


n = int(input())
choc = list(reversed(list(map(int, input().split()))))
before = choc[0]
ans = before
for i in range(1, len(choc)):
	if choc[i] >= before:
		if before > 0:
			ans += before - 1
		before = before - 1
	else:
		ans += choc[i]
		before = choc[i]
print(ans)


size = int(input())
av_chocos = input()
av_chocos = list(map(int, av_chocos.split()))
least = float('inf')
total = 0
for i in reversed(av_chocos):
	least = min(i, least - 1)
	if least == 0:
		break
	total += least
print(total)


N = int(input())
Xs = [int(x) for x in input().split(' ') if len(x) > 0]
count = 0
curr_max = None
Xs.reverse()
for x in Xs:
	if curr_max == None:
		curr_max = x
		count += x
	else:
		x1 = min(x, max(0, curr_max - 1))
		count += x1
		curr_max = x1
print(count)


n = int(input())
a = list(map(int, input().split()))
a = a[::-1]
i = 0
ans = 0
mx = a[0] + 1
for x in a:
	mx = max(0, min(x, mx - 1))
	ans += mx
print(ans)


import sys
n = int(input())
arr = list(map(int, input().split()))
ans = 0
maxi = 1 << 60
while arr and maxi:
	maxi = min(arr.pop(), maxi - 1)
	ans += maxi
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
answer = 0
prev = int(1000000000.0 + 1)
for ai in reversed(a):
	current = max(0, min(prev - 1, ai))
	answer += current
	prev = current
print(answer)


n = int(input())
a = [int(x) for x in input().split()]
su = a[n - 1]
tc = a[n - 1]
for i in range(n - 2, -1, -1):
	if a[i] < tc:
		su += a[i]
		tc = a[i]
	elif tc != 0:
		su += tc - 1
		tc -= 1
	else:
		su += 0
print(su)


n = int(input())
p = list(reversed([int(x) for x in input().split()]))
res = 0
for (i, l) in enumerate(p):
	if i > 0:
		r = min(p[i - 1] - 1, p[i])
		p[i] = max(r, 0)
print(sum(p))


import sys
input = lambda : sys.stdin.readline().strip()
n = int(input().strip())
arr = list(map(int, input().strip().split()))
Max = sys.maxsize
ret = 0
for i in range(n):
	cur = arr[n - 1 - i]
	if Max == 1:
		break
	if cur < Max:
		ret += cur
		Max = cur
	else:
		ret += Max - 1
		Max = Max - 1
print(ret)


n = int(input())
a = [int(s) for s in input().split(' ')]
ans = 0
m = a[n - 1]
for i in range(n - 1, -1, -1):
	if a[i] < m or a[i] == m:
		ans += a[i]
		m = a[i] - 1
	else:
		ans += m
		m = m - 1
	if m == 0:
		break
print(ans)


n = int(input())
a = list(map(int, input().split()))
res = 0
for j in range(n - 2, -1, -1):
	if a[j] >= a[j + 1]:
		a[j] = max(0, a[j + 1] - 1)
print(sum(a))


n = int(input())
l = [int(x) for x in input().split()]
qw = 0
pass1 = 0
count1 = 0
final = 0
for i in range(n - 1, -1, -1):
	if i == n - 1:
		count1 += l[i]
		qw = l[i]
	elif l[i] >= qw:
		qw = qw - 1
		if qw <= 0:
			break
		count1 += qw
	else:
		qw = l[i]
		count1 += qw
print(count1)


__author__ = 'NIORLYS'
n = int(input())
a = list(map(int, input().split()))
if n == 1:
	print(a[0])
else:
	for i in range(n - 2, -1, -1):
		if a[i] >= a[i + 1]:
			if a[i + 1] - 1 <= 0:
				a[i] = 0
			else:
				a[i] = a[i + 1] - 1
	print(sum(a))


gi = lambda : list(map(int, input().strip().split()))
(n,) = gi()
l = gi()
ans = l[-1]
v = ans
for k in range(-2, -n - 1, -1):
	if l[k] < v:
		v = l[k]
	else:
		v -= 1
		v = max(v, 0)
	ans += v
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
el = a[n - 1]
for i in range(len(a) - 2, -1, -1):
	if a[i] < el:
		ans += a[i]
		el = a[i]
	else:
		ans += max(0, el - 1)
		el = max(0, el - 1)
print(ans + a[n - 1])


n = int(input())
a = [int(x) for x in input().split()]
a = a[::-1]
top = 1000000001
ans = 0
for i in a:
	k = i
	if k >= top:
		d = top - 1
		if d > 0:
			ans += d
			top = d
		else:
			break
	else:
		ans += k
		top = k
print(ans)


from sys import stdin
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
ans = [arr[n - 1]]
for i in range(n - 2, -1, -1):
	if arr[i] >= arr[i + 1]:
		ans.append(max(0, arr[i + 1] - 1))
		arr[i] = max(0, arr[i + 1] - 1)
	else:
		ans.append(arr[i])
print(sum(ans))


n = int(input())
arr = list(map(int, input().split(' ')))
maxi = -1
ans = 0
for num in arr[::-1]:
	if maxi == -1:
		maxi = num
		ans += num
	else:
		ans += max(min(maxi - 1, num), 0)
		maxi = max(min(maxi - 1, num), 0)
print(ans)


from sys import stdin, stdout

def main():
	stdin.readline()
	numbers = list(map(int, stdin.readline().rstrip().split()))
	solution(numbers)

def solution(numbers):
	numbers.reverse()
	current = numbers[0]
	new = 0
	for (i, c) in enumerate(numbers):
		new += min(current, c)
		current = min(current, c) - 1 if min(current, c) > 0 else 0
	print(new)
main()


n = int(input())
a = [int(s) for s in input().split()]
summ = a[-1]
cur = a[-1] - 1
for i in range(n - 2, -1, -1):
	if a[i] >= cur:
		summ += cur
		cur = cur - 1
	else:
		cur = a[i] - 1
		summ += a[i]
	if cur <= 0:
		break
print(summ)


n = int(input())
a = list(map(int, input().split()))
prev = a[n - 1]
count = prev
ans = count
for i in range(n - 2, -1, -1):
	if a[i] < prev:
		count += a[i]
		ans = max(ans, count)
		prev = a[i]
	else:
		count += prev - 1
		ans = max(ans, count)
		prev = prev - 1
	if a[i] == 1:
		count = 0
print(ans)


import sys
input = lambda : sys.stdin.readline().strip('\r\n')
n = int(input())
a = list(map(int, input().split()))
ans = a[-1]
maxi = a[-1]
check = maxi
for i in range(n - 2, -1, -1):
	maxi = min(maxi, a[i])
	if check == maxi:
		maxi -= 1
		if maxi < 0:
			maxi = 0
	check = maxi
	ans += maxi
print(ans)


input()
x = 10 ** 10
s = 0
for a in reversed(list(map(int, input().split()))):
	x = min(a, x - 1)
	if x < 1:
		break
	s += x
print(s)


import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, copy, functools
import random
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
eps = 1.0 / 10 ** 10
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI():
	return [list(map(int, l.split())) for l in sys.stdin.readlines()]

def LI_():
	return [int(x) - 1 for x in sys.stdin.readline().split()]

def LF():
	return [float(x) for x in sys.stdin.readline().split()]

def LS():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def F():
	return float(sys.stdin.readline())

def S():
	return input()

def pf(s):
	return print(s, flush=True)

def pe(s):
	return print(str(s), file=sys.stderr)

def main():
	n = I()
	a = LI()
	r = 0
	m = inf
	for c in a[::-1]:
		m = min(m - 1, c)
		r += m
		if m == 0:
			break
	return r
print(main())


n = int(input())
A = [int(i) for i in input().split()]
ans = 0
curmax = 10 ** 10
for i in range(n - 1, -1, -1):
	if curmax == 0:
		continue
	if A[i] < curmax:
		ans += A[i]
		curmax = A[i]
	else:
		ans += curmax - 1
		curmax -= 1
print(ans)


n = int(input())
a = list(map(lambda x: int(x), input().split()))
a.reverse()
sum = 0
last = 10000000000000000
for i in a:
	t = min(last - 1, i)
	if t == 0:
		break
	sum += t
	last = t
print(sum)


import sys, os
from io import BytesIO, IOBase
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(stdin, stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def ceil(a, b):
	return (a + b - 1) // b
S1 = 'abcdefghijklmnopqrstuvwxyz'
S2 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True
n = iinp()
A = lmp()
idx = -1
mx = n * 4 + 1
ans = 0
for i in range(n - 1, 0, -1):
	if A[i] <= A[i - 1]:
		if A[i] - 1 > 0:
			A[i - 1] = A[i] - 1
		else:
			A[i - 1] = 0
print(sum(A))


n = int(input())
p = [int(x) for x in input().split()]
s = 0
s = s + p[n - 1]
for i in range(2, n + 1):
	if p[n - i + 1] == 1:
		break
	else:
		s = s + min(p[n - i], p[n - i + 1] - 1)
		p[n - i] = max(min(p[n - i], p[n - i + 1] - 1), 0)
print(s)


import re, math, decimal, bisect

def read():
	return input().strip()

def iread():
	return int(input().strip())

def viread():
	return [int(_) for _ in input().strip().split()]
n = iread()
nums = viread()
count = nums[len(nums) - 1]
last = count
for i in range(len(nums) - 2, -1, -1):
	if nums[i] < last:
		count += nums[i]
		last = nums[i]
	elif last > 1:
		count += last - 1
		last -= 1
print(count)


from sys import stdin
input = stdin.buffer.readline
n = int(input())
(*a,) = map(int, input().split())
a = a[::-1]
c = 1000000007
ans = 0
for i in a:
	c = max(0, min(c - 1, i))
	ans += c
print(ans)


n = int(input())
l = list(map(int, input().split()))
l.reverse()
f = l[0]
maxm = l[0]
for i in range(1, n):
	a = max(0, min(maxm - 1, l[i]))
	maxm = min(maxm, a)
	f += a
print(f)


n = int(input())
a = [int(i) for i in input().split()]
a.reverse()
SUM = [a[0]]
min = a[0]
for i in range(1, n):
	if a[i] < min:
		min = a[i]
		SUM.append(min)
	elif a[i] >= min and min != 1:
		min -= 1
		SUM.append(min)
print(sum(SUM))


import sys
sys.setrecursionlimit(2000)
from collections import Counter
from functools import reduce
n = [int(val) for val in sys.stdin.readline().split()][0]
a = [int(val) for val in sys.stdin.readline().split()]
prev = a[-1]
count = prev
for i in range(n - 2, -1, -1):
	prev = min(a[i], prev - 1)
	prev = max(prev, 0)
	count += prev
print(count)


from sys import setcheckinterval, stdin
setcheckinterval(1000)
from collections import defaultdict
iin = lambda : int(stdin.readline())
lin = lambda : list(map(int, stdin.readline().split()))
n = iin()
a = lin()
a = a[::-1]
ans = a[0]
for i in range(1, n):
	if a[i - 1] > a[i]:
		ans += a[i]
	else:
		if a[i - 1] - 1 <= 0:
			break
		else:
			a[i] = a[i - 1] - 1
		ans += a[i]
print(ans)


input()
nums = list(map(int, input().split(' ')))
nums = nums[::-1]
maxc = nums[0]
oldturn = nums[0]
for i in range(1, len(nums)):
	oldturn = min(nums[i], max(oldturn - 1, 0))
	maxc += oldturn
	if oldturn == 0:
		break
print(maxc)


n = int(input())
a = list(map(int, input().split()))
a = a[::-1]
tans = a[0]
exist = a[0] - 1
for i in range(1, n):
	tans += min(exist, a[i])
	exist = min(exist, a[i]) - 1
	if exist <= 0:
		break
print(tans)


n = int(input())
L = list(map(int, input().split()))
res = L[n - 1]
c = L[n - 1]
for i in reversed(range(n - 1)):
	if L[i] >= c:
		if c >= 1:
			res += c - 1
			c -= 1
	else:
		res += L[i]
		c = L[i]
print(res)


n = int(input())
types = list(map(int, input().split()))
result = types[n - 1]
for i in range(n - 1, 0, -1):
	x = min(types[i - 1], types[i] - 1)
	if x > 0:
		result += x
		types[i - 1] = x
	else:
		break
print(result)


n = int(input())
nums = list(map(int, input().split()))
res = nums[-1]
can_buy = res - 1
cur = n - 2
while cur >= 0:
	t = min(nums[cur], can_buy)
	res += t
	can_buy = t - 1
	if can_buy <= 0:
		break
	cur -= 1
print(res)


n = int(input())
l = list(map(int, input().split()))
c = prev = l[n - 1]
for i in range(n - 1, 0, -1):
	if prev != 0:
		prev = min(prev - 1, l[i - 1])
	c += prev
print(c)


from sys import stdin
from collections import deque
from math import sqrt, floor, ceil, log, log2, log10, pi, gcd, sin, cos, asin

def ii():
	return int(stdin.readline())

def fi():
	return float(stdin.readline())

def mi():
	return map(int, stdin.readline().split())

def fmi():
	return map(float, stdin.readline().split())

def li():
	return list(mi())

def lsi():
	x = list(stdin.readline())
	x.pop()
	return x

def si():
	return stdin.readline()
n = ii()
a = li()
p = 0
for i in range(n - 2, -1, -1):
	if a[i] >= a[i + 1]:
		if a[i + 1] == 0:
			p = i + 1
			break
		a[i] = a[i + 1] - 1
print(sum(a[p:]))


import time
n = int(input())
a = [int(i) for i in input().split()]
start = time.time()
now = a[-1]
ans = now
for i in range(n - 2, -1, -1):
	if a[i] < now:
		now = a[i]
	else:
		now -= 1
	if now == 0:
		break
	ans += now
print(ans)
finish = time.time()


def max_chocolates(types_stock):
	num_types = len(types_stock)
	max_stock = max(types_stock)
	max_buy = dict()
	for i in range(num_types):
		for j in range(max_stock + 1):
			if i == 0:
				if j <= types_stock[i] and j != 0:
					max_buy[i, j] = j
			elif j <= types_stock[i]:
				prev_stock = types_stock[i - 1]
				if j > 0:
					max_val = max((get_max_val(max_buy, i - 1, k) + j for k in range(min(prev_stock + 1, j))))
					if max_val != 0:
						max_buy[i, j] = max_val
	if (num_types - 1, max_stock) in max_buy:
		return max_buy[num_types - 1, max_stock]
	return 0

def get_max_val(dict, i, j):
	if (i, j) in dict:
		return dict[i, j]
	return 0

def max_chocolates_simple(types_stock):
	n = len(types_stock)
	for i in range(n - 2, -1, -1):
		if types_stock[i] >= types_stock[i + 1]:
			types_stock[i] = max(0, types_stock[i + 1] - 1)
	return sum(types_stock)

def read_input(flag=0):
	if flag > 0:
		return int(input())
	return [int(i) for i in input().split()]
n = read_input(1)
arr = read_input()
assert len(arr) == n
print(max_chocolates_simple(arr))


num = int(input())
choco = input().split(' ')
number = int(choco[num - 1])
before = number
for i in range(num - 2, -1, -1):
	if before == 1:
		break
	if int(choco[i]) < int(choco[i + 1]):
		number += min(int(choco[i]), before - 1)
		before = min(int(choco[i]), before - 1)
	else:
		number += before - 1
		before -= 1
print(number)


(n, s, re) = (int(input()), [*map(int, input().split())][::-1], 10 ** 10)
res = 0
for i in range(n):
	if s[i] <= re:
		res += s[i]
		re = s[i] - 1
	else:
		res += re
		re -= 1
	if re == 0:
		break
print(res)


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


n = int(input())
arr = input().split(' ')
arr = list(reversed(arr))
last = -1
sum = 0
for i in range(0, len(arr)):
	if last == -1:
		last = int(arr[i])
		sum += int(arr[i])
	else:
		new = int(arr[i])
		if new >= last:
			new = last - 1
		if new != 0:
			last = new
			sum += new
		else:
			break
print(sum)


n = int(input())
a = [int(x) for x in input().split()]
ans = a[n - 1]
for i in range(n - 2, -1, -1):
	a[i] = max(min(a[i], a[i + 1] - 1), 0)
	ans += a[i]
print(ans)


def main():
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 0
	curr = arr[-1]
	ans += curr
	for i in range(n - 2, -1, -1):
		if arr[i] < curr:
			curr = arr[i]
		elif arr[i] >= curr:
			curr = curr - 1
		ans += max(curr, 0)
	print(ans)
main()


n = int(input())
x = list(map(int, input().split()))[::-1]
(cnt, ans) = (x[0], x[0])
for i in range(1, n):
	if x[i] < cnt:
		cnt = x[i]
		ans += cnt
	else:
		if cnt > 0:
			cnt -= 1
		ans += cnt
print(ans)


n = int(input())
arr = list(map(int, input().split()))
num = arr[-1]
arr2 = [num]
arr.reverse()
arr.pop(0)
for k in arr:
	num = min(num - 1, k)
	if num < 0:
		arr2.append(0)
	else:
		arr2.append(num)
print(sum(arr2))


n = int(input())
st = input().split()
a = [0] * n
for i in range(n):
	a[i] = int(st[i])
m = a[n - 1]
i = n - 2
rez = m
while i != -1:
	if a[i] >= m:
		a[i] = a[i + 1] - 1
	if a[i] >= 0:
		m = a[i]
	else:
		m = 0
		a[i] = 0
	rez += a[i]
	i -= 1
print(rez)


n = int(input())
choclates = list(map(int, input().split()))
choclates.reverse()
sum = choclates[0]
new_choc = [choclates[0]]
for i in range(1, n, 1):
	if choclates[i] < new_choc[i - 1] and choclates[i] >= 0:
		sum += choclates[i]
		new_choc.append(choclates[i])
	elif new_choc[i - 1] - 1 <= choclates[i] and new_choc[i - 1] - 1 >= 0:
		sum += new_choc[i - 1] - 1
		new_choc.append(new_choc[i - 1] - 1)
	else:
		new_choc.append(0)
print(sum)


n = int(input())
cho = [int(x) for x in input().split()]
cho.reverse()
s = cho[0]
prev = cho[0]
for i in range(1, len(cho)):
	if cho[i] >= prev:
		if prev > 1:
			s += prev - 1
			prev -= 1
		else:
			break
	else:
		s += cho[i]
		prev = cho[i]
print(s)


TN = 1

def solution():
	n = int(input())
	a = list(map(int, input().split()))
	cur = a[-1]
	ans = a[-1]
	for i in reversed(range(n - 1)):
		if a[i] < cur:
			ans += a[i]
			cur = a[i]
		elif cur - 1 > 0:
			ans += cur - 1
			cur -= 1
		else:
			break
	print(ans)
while TN != 0:
	solution()
	TN -= 1


n = int(input())
p = [*map(int, input().split()), 0]
p.reverse()
sum = 0
tc = 2100000000
for i in range(1, n + 1):
	tc = min(max(tc - 1, 0), p[i])
	sum += tc
print(sum)


n = input()
a = list(map(int, input().split()[::-1]))
c = [a[0]]
for (i, v) in enumerate(a[1:]):
	b = max([min([c[i] - 1, v]), 0])
	if b == 0:
		break
	c.append(b)
print(sum(c))


n = int(str(input()).strip())
s = map(int, str(input()).strip().split()[::-1])
min_until_now = 10000000000
total = 0
for _s in s:
	if _s == 0:
		break
	if min_until_now > _s:
		total += _s
		min_until_now = _s
	elif min_until_now == _s:
		min_until_now -= 1
		total += min_until_now
	else:
		min_until_now -= 1
		total += min_until_now
	if min_until_now == 1:
		break
print(total)


t = int(input())
c = list(map(int, input().split()))[::-1]
count = c[0]
for i in range(1, len(c)):
	if c[i - 1] < 1:
		break
	if c[i] >= c[i - 1]:
		c[i] = c[i - 1] - 1
	count += c[i]
print(count)


input()
res = 0
prev = 1000000001
for cur in reversed(input().split()):
	prev = min(int(cur), max(prev, 1) - 1)
	res += prev
print(res)


import math
from collections import Counter, defaultdict
from sys import stdin, stdout
input = stdin.readline
I = lambda : int(input())
M = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
n = I()
a = LI()
s = 0
b = 10 ** 10
for i in range(n - 1, -1, -1):
	b = min(b - 1, a[i])
	b = max(0, b)
	s += b
print(s)


l = int(input())
li = list(map(int, input().split()))
c = li[-1]
m = li[-1]
for i in range(l - 2, -1, -1):
	if li[i] < m:
		c += li[i]
		m = li[i]
	elif m != 0:
		m -= 1
		c += m
print(c)


n = int(input())
l = list(map(int, input().split()))
s = 0
m = l[-1]
i = n - 1
while m > 0 and i >= 0:
	c = min(l[i], m)
	s += c
	m = c - 1
	i -= 1
print(s)


def parser():
	while 1:
		data = list(input().split(' '))
		for number in data:
			if len(number) > 0:
				yield number
input_parser = parser()

def get_word():
	global input_parser
	return next(input_parser)

def get_number():
	data = get_word()
	try:
		return int(data)
	except ValueError:
		return float(data)

def calc(pa, pb, a, b):
	p = max(pa, pb)
	n = min(a, b)
	if pa == pb and n - p > 0:
		return n - p
	elif not pa == pb and n - p >= 0:
		return 1 + n - p
	else:
		return 0
n = get_number()
ar = []
for i in range(n):
	ar.append(get_number())
for i in range(len(ar) - 2, -1, -1):
	if ar[i + 1] <= ar[i] and ar[i + 1] > 0:
		ar[i] = ar[i + 1] - 1
	if ar[i + 1] == 0:
		ar[i] = 0
sum = 0
for i in ar:
	sum += i
print(sum)


n = int(input())
a = [int(s) for s in input().split()]
i = n - 1
ma = a[i] + 1
c = 0
while (i >= 0 and a[i] > 0) and ma > 1:
	ma = min(a[i], ma - 1)
	c += ma
	i -= 1
print(c)


import sys, math, queue
sys.setrecursionlimit(1000000)
n = int(input())
a = list(map(int, input().split()))
a = [a[i] for i in range(-1, -n - 1, -1)]
ans = a[0]
last = a[0]
for i in range(1, n):
	if a[i] >= last - 1:
		ans += last - 1
		last -= 1
	elif a[i] < last:
		ans += a[i]
		last = a[i]
	if last == 0:
		break
print(ans)


input()
s = list(map(int, input().split()))[::-1]
n = 1
for _ in s[1:]:
	s[n] = max(0, min(s[n - 1] - 1, s[n]))
	n += 1
print(sum(s))


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
n = int(input())
s = list(map(int, input().split()))
prev = 9999999999999999999999999
ans = 0
for i in range(n - 1, -1, -1):
	ans += max(min(s[i], prev - 1), 0)
	prev = max(min(s[i], prev - 1), 0)
print(ans)


n = int(input())
l = list(map(int, input().split()))
l.reverse()
l.append(0)
sum = l[0]
if n > 1 and l[0] != 1:
	i = 1
	while l[i - 1] != 1 and l[i] != 0 and (i < n):
		if l[i] < l[i - 1]:
			sum += l[i]
		else:
			sum += l[i - 1] - 1
			l[i] = l[i - 1] - 1
		i += 1
	if l[0] != 1 and l[i] == 1:
		sum += 1
print(sum)


n = int(input())
a = [int(t) for t in input().split(' ')]
INF = 10 ** 10
i = n - 1
total = 0
mn = INF
while i >= 0:
	mn = max(0, min(mn - 1, a[i]))
	total += mn
	i -= 1
print(total)


n = int(input())
a = list(map(int, input().split()))[::-1]
res = 0
last = 10 ** 10
for i in range(n):
	this = max(min(a[i], last - 1), 0)
	last = this
	res += this
print(res)


import math, sys, time, random
t = int(input())
arr = list(map(int, input().split()))
arr = arr[::-1]
s = arr[0]
for i in range(1, t):
	if arr[i] >= arr[i - 1]:
		arr[i] = arr[i - 1] - 1
		if arr[i - 1] - 1 > 0:
			s += arr[i - 1] - 1
		continue
	s += arr[i]
print(s)


n = int(input())
a = list(map(int, input().split()))
a.reverse()
for i in range(len(a) - 1):
	if a[i + 1] >= a[i]:
		a[i + 1] = a[i] - 1
s = 0
for i in range(len(a)):
	if a[i] > 0:
		s += a[i]
print(s)


n = int(input())
a = [int(i) for i in input().split()]
total = 0
prev = -2
for x in range(len(a) - 1, -1, -1):
	if prev == -2:
		total += a[x]
		prev = a[x]
	else:
		if prev <= 1:
			break
		v = min(a[x], prev - 1)
		total += v
		prev = v
print(total)


n = int(input())
arr = list(map(int, input().split()))
maximum = arr[-1]
choc = arr[-1]
for i in range(len(arr) - 1, 0, -1):
	if maximum <= 0:
		break
	choc += min(maximum - 1, arr[i - 1])
	maximum = min(maximum - 1, arr[i - 1])
print(choc)


n = int(input())
a = list(map(int, input().split()))
total = 0
cur = a[-1] + 1
for x in a[::-1]:
	if cur == 0:
		continue
	cur = min(x, cur - 1)
	total += cur
print(total)


n = int(input())
l = list(map(int, input().split()))
if l[n - 1] == 0:
	print(0)
	exit(0)
sum = l[n - 1]
all = sum - 1
for i in range(n - 2, -1, -1):
	if all == 0:
		break
	if all > l[i]:
		sum += l[i]
		if l[i] == 0 or l[i] == 1:
			print(sum)
			exit(0)
		all = l[i] - 1
	else:
		sum += all
		all -= 1
		if all == 0:
			print(sum)
			exit(0)
print(sum)


n = int(input())
a = [int(x) for x in input().split()]
a.reverse()
x = 0
for i in range(len(a) - 1):
	if a[i + 1] == a[i]:
		a[i + 1] -= a[i + 1] - a[i] + 1
	elif a[i + 1] > a[i]:
		a[i + 1] = a[i] - 1
		if a[i + 1] < 0:
			a[i + 1] = 0
	x += a[i]
print(x + a[len(a) - 1])


n = int(input())
cikolata = list(map(int, input().split()))
cikolata = cikolata[::-1]
secilen = [cikolata[0]]
for i in range(n - 1):
	if cikolata[i + 1] < cikolata[i] - 1:
		secilen.append(cikolata[i + 1])
	else:
		cikolata[i + 1] = cikolata[i] - 1
		secilen.append(cikolata[i] - 1)
for i in range(n):
	if secilen[i] < 0:
		secilen[i] = 0
print(sum(secilen))


n = int(input())
a = input().split(' ')
sum = int(a[n - 1])
now = int(a[n - 1]) - 1
n -= 2
while n >= 0:
	if now <= int(a[n]) and now > 0:
		sum += now
		now -= 1
	elif now > 0:
		sum += int(a[n])
		now = int(a[n]) - 1
	else:
		break
	n -= 1
print(sum)


n = int(input())
a = [int(x) for x in input().split()]
a.reverse()
counter = 0
prev = 10 ** 10
for item in a:
	b = min(prev, item)
	counter += b
	prev = max(b - 1, 0)
print(counter)


a = int(input())
b = list(map(int, input().split()))
c = 0
d = 100000000000000000000
for j in range(a - 1, -1, -1):
	if b[j] < d:
		c += b[j]
		d = b[j]
	elif d == 1:
		break
	elif d - 1 > 0:
		c += d - 1
		d -= 1
print(c)


n = input()
inp = input().split()
ans = last = int(inp[-1])
for num in reversed(inp[:-1]):
	num = int(num)
	ch = min(num, last)
	if ch == last and ch > 0:
		ch -= 1
	ans += ch
	last = ch
print(ans)


n = int(input())
lst = list(map(int, input().split()))
(res, plus) = (lst[n - 1], lst[n - 1])
for i in range(n - 2, -1, -1):
	if plus <= lst[i]:
		plus = max(0, plus - 1)
	else:
		plus = lst[i]
	res += plus
print(res)


(n, arr, ans, c) = (int(input()), [int(i) for i in input().split()], 0, 10 ** 9 + 1)
for i in range(n - 1, -1, -1):
	c = min(c - 1, arr[i])
	c = max(c, 0)
	ans += c
print(ans)


n = int(input())
arr = list(map(int, input().split()))
arr.reverse()
summ = arr[0]
res = []
res.append(arr[0])
for i in range(1, len(arr)):
	if min(arr[i], res[len(res) - 1] - 1) >= 0:
		summ += min(arr[i], res[len(res) - 1] - 1)
		res.append(min(arr[i], res[len(res) - 1] - 1))
	else:
		summ += 0
print(summ)


n = int(input())
a = list(map(int, input().split()))
cur = a[-1] + 1
res = 0
for v in reversed(a):
	if cur:
		cur = min(v, cur - 1)
	res += cur
print(res)


n = int(input(''))
s = list(map(int, input().split()))
t = [s[i] - i for i in range(n)]
a = t[:]
for i in range(n):
	if i == 0:
		a[n - 1] = t[n - 1]
	else:
		a[n - 1 - i] = min(a[n - i], t[n - 1 - i])
ass = [max(0, a[i] + i) for i in range(n)]
print(sum(ass))


n = int(input())
a = [int(i) for i in input().split()]
ans = a[n - 1]
curr = a[n - 1]
for i in range(n - 2, -1, -1):
	if curr == 1:
		continue
	if a[i] < curr:
		ans += a[i]
		curr = a[i]
	else:
		ans += curr - 1
		curr -= 1
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
sum = 0
prev = 1000000000000
a.reverse()
for ai in a:
	summand = min(prev - 1, ai)
	if summand <= 0:
		break
	sum = sum + summand
	prev = summand
print(sum)


import io, sys, atexit, os
import math as ma
from sys import exit
from decimal import Decimal as dec
from itertools import permutations

def li():
	return list(map(int, input().split()))

def num():
	return map(int, input().split())

def nu():
	return int(input())

def find_gcd(x, y):
	while y:
		(x, y) = (y, x % y)
	return x
mm = 1000000007

def solve():
	t = 1
	for it in range(t):
		n = nu()
		a = li()
		cc = 0
		mx = -1
		for i in range(n - 1, -1, -1):
			if mx == -1:
				mx = a[i]
				cc += a[i]
			elif a[i] < mx:
				mx = a[i]
				cc += a[i]
			else:
				if mx == 0:
					continue
				mx = mx - 1
				cc += mx
		print(cc)
solve()


n = int(input())
A = list(map(int, input().split()))
cnt = 0
z = float('inf')
for i in range(n - 1, -1, -1):
	if z > A[i]:
		z = min(z, A[i])
		cnt += z
	else:
		z -= 1
		z = max(0, z)
		cnt += z
print(cnt)


def solve(n, a):
	i = len(a) - 1
	x = a[-1]
	ans = x
	while i > 0:
		i -= 1
		if a[i] < x:
			x = a[i]
		elif a[i] == 0:
			return ans
		else:
			x = x - 1
		ans += x
		if x == 0:
			return ans
	return ans
n = int(input())
a = [int(p) for p in input().split()]
print(solve(n, a))


n = int(input())
arr = [int(i) for i in input().split()]
ans = [0] * n
ans[n - 1] = arr[n - 1]
for i in range(n - 2, -1, -1):
	if ans[i + 1] == 1:
		break
	if arr[i] >= ans[i + 1]:
		ans[i] = ans[i + 1] - 1
	else:
		ans[i] = arr[i]
print(sum(ans))
import gc
gc.collect()


n = int(input())
arr = list(map(int, input().split(' ')))
minimum = min(arr)
sum = arr[len(arr) - 1]
for i in range(len(arr) - 2, -1, -1):
	if arr[i] < arr[i + 1]:
		sum = sum + arr[i]
	else:
		arr[i] = arr[i + 1] - 1
		if arr[i] > 0:
			sum = sum + arr[i]
print(sum)


n = int(input())
li = list(map(int, input().split()))
count = li[-1]
last = li[-1]
for i in range(n - 2, -1, -1):
	if li[i] < last:
		count += li[i]
		last = li[i]
	else:
		count += max(0, last - 1)
		last = max(0, last - 1)
print(count)


import sys
sys.setrecursionlimit(10 ** 5 + 1)
inf = int(10 ** 20)
max_val = inf
min_val = -inf
RW = lambda : sys.stdin.readline().strip()
RI = lambda : int(RW())
RMI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
RWI = lambda : [x for x in sys.stdin.readline().strip().split()]
lens = RI()
qnty = RMI()
maxs = max_val
sums = 0
for i in qnty[::-1]:
	maxs = min(maxs - 1, i)
	sums += max(maxs, 0)
print(sums)


n = int(input())
a = list(map(int, input().split()))
watermark = a[-1]
for i in reversed(range(n - 1)):
	if a[i] < watermark:
		watermark = a[i]
	else:
		a[i] = max(watermark - 1, 0)
		watermark = a[i]
print(sum(a))


n = int(input())
a = list(reversed(list(map(int, input().split()))))
cur = a[0] + 1
ans = 0
for i in a:
	cur = max(0, min(cur - 1, i))
	ans += cur
print(ans)


n = int(input())
arr = list(map(int, input().split()))
ans = arr[-1]
xx = arr[-1] - 1
pre = xx - 1
for i in range(n - 2, -1, -1):
	tt = min(arr[i], xx)
	tt = max(0, tt)
	ans += tt
	xx = tt - 1
	if xx <= 0:
		break
	if tt == 0:
		break
print(ans)


n = int(input())
a = list(map(int, input().split()))
a = a[::-1]
k = 0
mx = a[0] + 2
for c in a:
	mx = max(0, mx - 1)
	if c < mx:
		k += c
		mx = c
	elif mx == c:
		k += max(c, 0)
		mx = max(c, 0)
	else:
		k += mx
print(k)


n = int(input())
a = list(map(int, input().split()))
(ans, last) = (0, 10 ** 9 + 1)
for x in a[::-1]:
	last = min(x, max(last - 1, 0))
	ans += last
print(ans)


input()
a = list(map(int, input().split(' ')))
su = a[-1]
l = su
for i in reversed(a[:-1]):
	if l == 0:
		break
	next = min(i, l - 1)
	l = next
	su += next
print(su)


n = int(input())
a = [int(s) for s in input().split()]
res = 0
k = 0
if n != 1:
	for i in range(len(a) - 1, -1, -1):
		if a[i] > a[i - 1]:
			k += 1
		else:
			a[i - 1] = max(a[i] - 1, 0)
		res += a[i]
if n == 1:
	res = a[0]
print(res)


import sys
n = int(sys.stdin.readline().strip())
c = list(map(int, sys.stdin.readline().strip().split()))
ans = 0
x = max(c) + 1
for i in range(0, n):
	x = max([min([x - 1, c[n - 1 - i]]), 0])
	ans = ans + x
print(ans)


n = int(input())
s = list(map(int, input().split()))
s.reverse()
j = 10000000000000
count = 0
for i in s:
	if i < j:
		count += i
		j = i
	elif i > 0 and j > 0:
		count += j - 1
		j -= 1
print(count)


import re, math, decimal, bisect

def read():
	return input().strip()

def iread():
	return int(input().strip())

def viread():
	return [int(_) for _ in input().strip().split()]

def round(n):
	return int(n + 0.5)
n = iread()
choc = viread()
lastbought = 10 ** 9 + 1
ans = 0
for c in choc[::-1]:
	buy = max(min(lastbought - 1, c), 0)
	ans += buy
	lastbought = buy
print(ans)


import math
n = int(input())
a = list(map(int, input().split()))
a = list(reversed(a))
s = 0
m = math.inf
for i in range(len(a)):
	m = min(max(m - 1, 0), a[i])
	s += m
print(s)


from sys import stdin

def inp():
	return stdin.readline().strip()
n = int(inp())
ar = [int(x) for x in inp().split()]
ans = 0
ansAr = []
for i in range(n):
	if i == 0:
		ansAr.append(ar[n - i - 1])
	elif ar[n - i - 1] >= ansAr[len(ansAr) - 1]:
		ansAr.append(ansAr[len(ansAr) - 1] - 1)
	else:
		ansAr.append(ar[n - i - 1])
	if ansAr[len(ansAr) - 1] == 0:
		break
print(sum(ansAr))


types = int(input())
coe = list(map(int, input().split(' ')))
count = 0
for i in range(types - 1, -1, -1):
	if i == types - 1:
		count += coe[i]
	elif coe[i] == 0 or coe[i + 1] == 0:
		break
	elif coe[i] < coe[i + 1]:
		count += coe[i]
	else:
		coe[i] = coe[i + 1] - 1
		count += coe[i]
print(count)


n = int(input())
a = [int(i) for i in input().split()]
count = a[n - 1]
for i in range(n - 2, -1, -1):
	if a[i + 1] - 1 > 0:
		a[i] = min(a[i], a[i + 1] - 1)
		count += a[i]
	else:
		break
print(count)


n = int(input())
arr = list(map(int, input().split()))
tot = arr[-1]
prevHi = tot
for i in range(1, n):
	each = arr[n - i - 1]
	if each < prevHi:
		tot += each
		prevHi = each
	elif prevHi - 1 > 0:
		tot += prevHi - 1
		prevHi -= 1
print(tot)


n = int(input())
choc = list(map(int, input().split()))
res = choc[n - 1]
count = choc[n - 1]
for i in range(1, n):
	if count == 0:
		break
	a = min(choc[n - i - 1], count - 1)
	res += a
	count = a
print(res)


n = int(input())
arr = list(map(int, input().split()))
last = -1
count = 0
i = n - 1
while i >= 0:
	if last == -1:
		last = arr[i]
		count += arr[i]
	elif arr[i] < last:
		count += arr[i]
		last = arr[i]
	elif arr[i] == last:
		count += arr[i] - 1
		last = arr[i] - 1
	elif last - 1 <= 0:
		break
	else:
		last -= 1
		count += last
	i -= 1
print(count)


n = int(input())
s = list(map(int, input().split()))
sum = s[-1]
a = s[-1]
for i in range(1, n):
	a = min(a - 1, s[-i - 1])
	if a > 0:
		sum += a
	else:
		break
print(sum)


n = int(input())
l = list(map(int, input().split()))
ans = 0
curr = 1000000001
for i in range(n - 1, -1, -1):
	if l[i] < curr:
		curr = l[i]
	elif curr > 0:
		curr -= 1
	ans += curr
print(ans)


def problem_1139b():
	n = int(input())
	a = input().split(' ')
	for (i, _) in enumerate(a):
		a[i] = int(a[i])
	mi = a[n - 1] - 1
	for i in range(n - 2, -1, -1):
		if a[i] < mi:
			mi = a[i] - 1
		else:
			a[i] = mi
			mi = max(mi - 1, 0)
	answer = 0
	for i in range(n):
		answer += a[i]
	print(answer)
	return
problem_1139b()


from functools import reduce
from operator import add

def solve_it(n, chocs):
	count = 0
	sel = chocs[len(chocs) - 1]
	for i in reversed(range(len(chocs))):
		sel = min(chocs[i], sel - 1) if i + 1 < len(chocs) else chocs[i]
		sel = 0 if sel < 0 else sel
		count += sel
	return count
n = int(input())
chocs = list(map(int, input().split()))
print(solve_it(n, chocs))


def main():
	n = int(input())
	a = [int(i) for i in input().split()]
	ans = [a[n - 1]]
	for i in range(n - 2, -1, -1):
		if a[i] < ans[-1]:
			ans.append(a[i])
		elif ans[-1] == 1:
			break
		else:
			ans.append(ans[-1] - 1)
	print(sum(ans))
main()


n = int(input())
a = [int(i) for i in input().split()]
cnt = 0
cur = pow(10, 10)
for i in range(n - 1, -1, -1):
	cur = min(cur - 1, a[i])
	cur = max(cur, 0)
	cnt += cur
print(cnt)


n = int(input())
a = list(map(int, input().split()))
v = a[::-1]
c = 0
for i in range(len(v)):
	if i == 0:
		c = v[i]
		m = v[i]
	elif v[i] < m:
		c += v[i]
		m = v[i]
	else:
		c += m - 1
		if m - 1 > 0:
			m = m - 1
print(c)


m = int(input())
s = list(map(int, input().split()))
res = 0
res1 = sum(s)
for i in range(len(s) - 1, 0, -1):
	if s[i - 1] > s[i] or s[i] == s[i - 1]:
		if s[i] != 0:
			res += s[i - 1] - (s[i] - 1)
			s[i - 1] = s[i] - 1
		else:
			res += s[i - 1] - s[i]
			s[i - 1] = s[i]
print(res1 - res)


n = int(input())
a = list(map(int, input().split()))[::-1]
c = a[0]
x = [a[0]]
for i in range(1, n):
	if a[i] < x[i - 1]:
		c += a[i]
		x.append(a[i])
	else:
		c += max(x[i - 1] - 1, 0)
		x.append(max(x[i - 1] - 1, 0))
print(c)


import sys, math, itertools
from collections import Counter, deque, defaultdict
from bisect import bisect_left, bisect_right
from heapq import heappop, heappush, heapify, nlargest
from copy import deepcopy
mod = 10 ** 9 + 7
INF = float('inf')

def inp():
	return int(sys.stdin.readline())

def inpl():
	return list(map(int, sys.stdin.readline().split()))

def inpl_1():
	return list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))
n = inp()
a = inpl()
a = a[::-1]
pre = INF
res = 0
for i in range(n):
	if i == 0:
		pre = a[0]
		res += pre
		continue
	pre = min(pre - 1, a[i])
	pre = max(0, pre)
	res += pre
print(res)


n = int(input())
a = list(map(int, input().split()))
t = 0
p = 1e+18
for i in range(n - 1, -1, -1):
	if a[i] < p:
		t += a[i]
		p = a[i]
	else:
		t += min(max(0, p - 1), a[i])
		p -= 1
print(t)


n = int(input())
a = [int(i) for i in input().split()]
ans = 0
ogr = 10000000000000000000
for i in a[::-1]:
	ans += min(ogr, i)
	ogr = max(min(ogr - 1, i - 1), 0)
print(ans)


n = int(input())
l = list(map(int, input().split()))
last = l[n - 1]
ans = l[n - 1]
l.reverse()
for i in l[1:]:
	last -= 1
	if last == 0:
		break
	if i < last:
		last = i
	ans += last
print(ans)


cnt = lambda s, x: s.count(x)
ii = lambda : int(input())
si = lambda : input()
f = lambda : map(int, input().split())
dgl = lambda : list(map(int, input()))
il = lambda : list(map(int, input().split()))
n = ii()
l = il()[::-1]
l1 = [l[0]]
for i in range(1, n):
	if l1[-1] > l[i]:
		l1.append(l[i])
	else:
		l1.append(max(0, min(l[i], l1[-1] - 1)))
print(sum(l1))


import math
from collections import defaultdict
import sys

def main():
	n = int(input())
	a = list(map(int, input().split()))
	res = 0
	prev = a[-1] + 1
	for i in range(n - 1, -1, -1):
		curr = max(0, min(prev - 1, a[i]))
		res += curr
		prev = curr
	print(res)
main()


n = eval(input())
s = input().split(' ')
s = list(map(int, s))
count = s[-1]
for i in range(n - 2, -1, -1):
	if s[i] < s[i + 1]:
		count += s[i]
	else:
		count += s[i + 1] - 1
		s[i] = s[i + 1] - 1
	if s[i] <= 1:
		break
print(count)


n = int(input())
arr = list(map(int, input().split()))
pre = ans = arr[n - 1]
for i in range(n - 2, -1, -1):
	if arr[i] < pre:
		ans += arr[i]
		pre = arr[i]
	else:
		ans += pre - 1
		pre -= 1
	if pre == 0:
		break
print(ans)


n = int(input())
nums = list(map(int, input().split()))
cur = 1e+18
ans = 0
for idx in range(n - 1, -1, -1):
	cur = min(cur - 1, nums[idx])
	cur = max(0, cur)
	ans += cur
print(ans)


n = int(input())
a = list(map(int, input().split()))
max = a[len(a) - 1]
sum = max
for i in range(len(a) - 2, -1, -1):
	if max <= 0:
		break
	if a[i] >= max:
		max = max - 1
		sum += max
	else:
		max = a[i]
		sum += max
print(sum)


n = int(input())
l = list(map(int, input().split()))
max = l[-1]
k1 = n - 1
temp = l[-1]
while k1 > 0:
	if l[k1 - 1] < temp:
		max += l[k1 - 1]
		temp = l[k1 - 1]
	elif temp != 0:
		max += temp - 1
		temp -= 1
	else:
		max += temp
	k1 -= 1
print(max)


kk = lambda : map(int, input().split())
ll = lambda : list(kk())
(n, ls, s, v) = (int(input()), ll(), 0, 100000000000)
for i in range(n - 1, -1, -1):
	v = max(0, min(v - 1, ls[i]))
	s += v
print(s)


def main():
	n = int(input())
	n_lst = [int(e) for e in input().split()]
	tmp = n_lst[-1] + 1
	cntr = 0
	for i in n_lst[::-1]:
		if i < tmp:
			cntr += i
			tmp = i
		elif tmp:
			cntr += tmp - 1
			tmp -= 1
	print(cntr)
main()


n = int(input())
a = [int(i) for i in input().split()]
a.reverse()
res = 0
max = a[0] + 1
for i in a:
	if i >= max:
		max -= 1
		if max < 0:
			max = 0
		res += max
	else:
		max = i
		res += i
print(res)


def chocolates(lst):
	(current, result) = (1e+18, 0)
	for i in range(len(lst) - 1, -1, -1):
		current = min(current - 1, lst[i])
		current = max(0, current)
		result += current
	return result
n = int(input())
a = [int(j) for j in input().split()]
print(chocolates(a))


from sys import stdin, stdout
from itertools import combinations
from collections import defaultdict
import math

def listIn():
	return list(map(int, stdin.readline().strip().split()))

def stringListIn():
	return [x for x in stdin.readline().split()]

def intIn():
	return int(stdin.readline())

def stringIn():
	return stdin.readline().strip()
n = intIn()
a = listIn()
idx = 0
arr = [0] * n
for i in range(n - 1, -1, -1):
	if a[i] > a[i - 1]:
		arr[i] = a[i]
	else:
		arr[i] = a[i]
		idx = i - 1
		break
for i in range(idx, -1, -1):
	arr[i] = min(arr[i + 1] - 1, a[i])
	arr[i] = max(0, arr[i])
print(sum(arr))


m = int(input())
b = input().split(' ')
b = list(map(int, b))
b.reverse()
count = b[0]
for i in range(1, m):
	if b[i] < b[i - 1]:
		count += b[i]
	else:
		b[i] = b[i - 1] - 1
		if b[i] >= 0:
			count += b[i]
		else:
			b[i] = 0
print(count)


import math
n = int(input())
list = [int(i) for i in input().split()]
(cnt, i) = (list[n - 1], n - 2)
while i >= 0:
	if list[i] == 0:
		break
	if list[i] < list[i + 1]:
		cnt += list[i]
	else:
		list[i] = list[i + 1] - 1
		cnt += list[i]
	if list[i] == 0:
		break
	i -= 1
print(cnt)


n = int(input())
l = list(map(int, input().split()))
l.reverse()
x = 0
e = l[0]
x += l[0]
for i in range(1, n):
	d = min(e - 1, l[i])
	d = max(d, 0)
	if d != 0:
		x += d
		e = d
print(x)


n = int(input())
a = list(map(int, input().split()))
counter = 0
last = 10 ** 9 + 50
for i in range(n):
	if a[n - i - 1] < last:
		counter += a[n - i - 1]
		last = a[n - i - 1]
	elif last > 0:
		counter += last - 1
		last -= 1
	elif last == 0:
		break
print(counter)


(n, a) = (int(input()), list(map(int, input().split())))
ans = a[n - 1]
a = list(reversed(a))
for i in range(1, n):
	if a[i] < a[i - 1]:
		ans += a[i]
	else:
		if a[i] > a[i - 1] - 1 and a[i - 1] >= 1:
			ans += a[i - 1] - 1
		a[i] = a[i - 1] - 1
print(ans)


from collections import Counter
n = int(input())
arr = list(map(int, input().split()))
ans = arr[-1]
var = arr[-1]
for i in range(n - 2, -1, -1):
	ans += min(max(0, var - 1), arr[i])
	var = min(var - 1, arr[i])
print(ans)


t = int(input())
a = [int(c) for c in input().split()]
s = a[t - 1]
for i in range(t - 2, -1, -1):
	z = 0 if min(a[i + 1] - 1, a[i]) < 0 else min(a[i + 1] - 1, a[i])
	s += z
	a[i] = z
print(s)


n = int(input())
a = list(map(int, input().split()))
a.reverse()
a.append(0)
s = 0
for i in range(0, n):
	if a[i] > a[i + 1]:
		s = s + a[i]
	else:
		s = s + a[i]
		a[i + 1] = a[i] - 1
		if a[i] == 0:
			break
print(s)


n = int(input())
a = list(map(int, input().split()))
b = []
for i in a:
	b.append(i)
ans = a[-1]
it = a[-1]
for i in range(len(a) - 2, -1, -1):
	if a[i] < it:
		it = a[i]
		ans += a[i]
	else:
		it = it - 1
		if it < 0:
			it = 0
		ans += it
print(ans)


n = int(input())
a = list(map(int, input().split()))
i = n - 2
ans = a[n - 1]
m = ans
while i >= 0 and m > 1:
	m = min(a[i], m - 1)
	ans += m
	i -= 1
print(ans)


n = int(input())
num_of_choclates = list(map(int, input().split()))
s = x = num_of_choclates[-1]
for i in range(2, n + 1):
	z = max(x - 1, 0)
	x = min(z, num_of_choclates[-i])
	s += x
print(s)


n = int(input())
t = list(map(int, input().split()))
v = [0] * n
v[-1] = t[-1]
for i in range(n - 1 - 1, 0 - 1, -1):
	v[i] = max(min(v[i + 1] - 1, t[i]), 0)
print(sum(v))