Polycarp plans to conduct a load testing of its new project Fakebook. He already agreed with his friends that at certain points in time they will send requests to Fakebook. The load testing will last n minutes and in the i-th minute friends will send a_{i} requests.

Polycarp plans to test Fakebook under a special kind of load. In case the information about Fakebook gets into the mass media, Polycarp hopes for a monotone increase of the load, followed by a monotone decrease of the interest to the service. Polycarp wants to test this form of load.

Your task is to determine how many requests Polycarp must add so that before some moment the load on the server strictly increases and after that moment strictly decreases. Both the increasing part and the decreasing part can be empty (i. e. absent). The decrease should immediately follow the increase. In particular, the load with two equal neigbouring values is unacceptable.

For example, if the load is described with one of the arrays [1, 2, 8, 4, 3], [1, 3, 5] or [10], then such load satisfies Polycarp (in each of the cases there is an increasing part, immediately followed with a decreasing part). If the load is described with one of the arrays [1, 2, 2, 1], [2, 1, 2] or [10, 10], then such load does not satisfy Polycarp.

Help Polycarp to make the minimum number of additional requests, so that the resulting load satisfies Polycarp. He can make any number of additional requests at any minute from 1 to n.


-----Input-----

The first line contains a single integer n (1 ≤ n ≤ 100 000) — the duration of the load testing.

The second line contains n integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ 10^9), where a_{i} is the number of requests from friends in the i-th minute of the load testing.


-----Output-----

Print the minimum number of additional requests from Polycarp that would make the load strictly increasing in the beginning and then strictly decreasing afterwards.


-----Examples-----
Input
5
1 4 3 2 5

Output
6

Input
5
1 2 2 2 1

Output
1

Input
7
10 20 40 50 70 90 30

Output
0



-----Note-----

In the first example Polycarp must make two additional requests in the third minute and four additional requests in the fourth minute. So the resulting load will look like: [1, 4, 5, 6, 5]. In total, Polycarp will make 6 additional requests.

In the second example it is enough to make one additional request in the third minute, so the answer is 1.

In the third example the load already satisfies all conditions described in the statement, so the answer is 0.

Source: codeforces | Difficulty: MEDIUM_HARD
Tags: Greedy algorithms


Solutions:
n = int(input())
arr = list(map(int, input().split()))
forward = arr[:]
cnt = arr[0] + 1
for i in range(1, n):
	forward[i] = max(cnt, arr[i])
	cnt = max(cnt, arr[i]) + 1
backward = arr[:]
cnt = arr[n - 1] + 1
for i in range(n - 2, -1, -1):
	backward[i] = max(cnt, arr[i])
	cnt = max(cnt, arr[i]) + 1
ans = 0
for i in range(n):
	if forward[i] < backward[i]:
		ans += forward[i] - arr[i]
	else:
		if abs(backward[i] - forward[i]) == 1:
			ans += 1
		ans += backward[i] - arr[i]
print(ans)


(s, l, r) = (0, 0, int(input()) - 1)
t = list(map(int, input().split()))
while 1:
	while l < r and t[l] < t[l + 1]:
		l += 1
	while l < r and t[r] < t[r - 1]:
		r -= 1
	if l == r:
		break
	if t[l] < t[r]:
		s += t[l] - t[l + 1] + 1
		t[l + 1] = t[l] + 1
	else:
		s += t[r] - t[r - 1] + 1
		t[r - 1] = t[r] + 1
print(s)


n = int(input())
p = list(map(int, input().split()))
a = [0] * n
t = s = f = 0
for i in range(n):
	if p[i] <= t:
		a[i] = t - p[i] + 1
	t = max(p[i], t + 1)
for i in range(n - 1, 0, -1):
	if p[i] <= s:
		a[i] = min(s - p[i] + 1, a[i])
	else:
		a[i] = 0
	s = max(p[i], s + 1)
for i in range(n):
	p[i] += a[i]
	f |= i > 1 and p[i] == p[i - 1]
print(sum(a[:]) + f)


n = int(input())
arr = list(map(int, input().split()))
starts = [0 for _ in range(n)]
ends = [0 for _ in range(n)]
starts[0] = arr[0]
ends[-1] = arr[-1]
for i in range(1, n):
	starts[i] = max(arr[i], starts[i - 1] + 1)
	ends[-i - 1] = max(arr[-i - 1], ends[-i] + 1)
sts = starts[:]
eds = ends[:]
for i in range(n):
	starts[i] -= arr[i]
	ends[-i - 1] -= arr[-i - 1]
for i in range(1, n):
	starts[i] += starts[i - 1]
	ends[-i - 1] += ends[-i]
bst = 10 ** 30
for i in range(n):
	score = max(sts[i], eds[i]) - arr[i]
	if i > 0:
		score += starts[i - 1]
	if i < n - 1:
		score += ends[i + 1]
	bst = min(bst, score)
print(bst)


n = int(input())
a = list(map(int, input().split()))
(lp, rp) = ([0 for i in range(n)], [0 for i in range(n)])
(lnr, rnr) = ([a[i] for i in range(n)], [a[i] for i in range(n)])
mx = a[0]
for i in range(1, n):
	if a[i] > mx:
		mx = a[i]
		lp[i] = lp[i - 1]
	else:
		mx += 1
		lp[i] = lp[i - 1] + mx - a[i]
		lnr[i] = mx
mx = a[-1]
for i in range(n - 2, -1, -1):
	if a[i] > mx:
		mx = a[i]
		rp[i] = rp[i + 1]
	else:
		mx += 1
		rp[i] = rp[i + 1] + mx - a[i]
		rnr[i] = mx
ans = min(rp[0], lp[-1])
for i in range(1, n - 1):
	ca = lp[i - 1] + rp[i + 1]
	if max(lnr[i - 1], rnr[i + 1]) + 1 > a[i]:
		ca += max(lnr[i - 1], rnr[i + 1]) + 1 - a[i]
	ans = min(ans, ca)
print(ans)


n = int(input())
a = [0 for i in range(n)]
b = [0 for i in range(n)]
f = [0 for i in range(n)]
q = [0 for i in range(n)]
d = [int(s) for s in input().split()]
last = d[0]
for i in range(1, n):
	a[i] = a[i - 1]
	if d[i] <= last:
		a[i] += abs(d[i] - last) + 1
		last += 1
	else:
		last = d[i]
	f[i] = last
last = d[n - 1]
for i in range(n - 2, -1, -1):
	b[i] = b[i + 1]
	if d[i] <= last:
		b[i] += abs(d[i] - last) + 1
		last += 1
	else:
		last = d[i]
	q[i] = last
ans = float('inf')
for i in range(n - 1):
	ans = min(ans, a[i] + b[i + 1] + int(f[i] == q[i + 1]))
print(min(ans, b[0], a[n - 1]))


n = int(input())
a = list(map(int, input().strip().split()))
if n > 1:
	li = [0] * n
	ri = [0] * n
	lm = a[0]
	c = [0] * n
	b = [0] * n
	b[0] = a[0]
	for i in range(1, n):
		if lm >= a[i]:
			li[i] = li[i - 1] + (lm + 1 - a[i])
			lm = lm + 1
		else:
			li[i] = li[i - 1]
			lm = a[i]
		b[i] = lm
	lm = a[n - 1]
	c[n - 1] = a[n - 1]
	for i in range(n - 2, -1, -1):
		if lm >= a[i]:
			ri[i] = ri[i + 1] + (lm + 1 - a[i])
			lm = lm + 1
		else:
			ri[i] = ri[i + 1]
			lm = a[i]
		c[i] = lm
	ans = 1 << 64
	for i in range(n):
		if i == 0:
			ans = min(ans, ri[i], ri[i + 1])
		elif i == n - 1:
			ans = min(ans, li[i], li[i - 1])
		else:
			v1 = li[i] + ri[i + 1]
			if b[i] == c[i + 1]:
				v1 += 1
			v2 = ri[i] + li[i - 1]
			if c[i] == b[i - 1]:
				v2 += 1
			val = min(v1, v2)
			ans = min(ans, val)
	print(ans)
else:
	print(0)


n = int(input())
t = list(map(int, input().split()))
s = a = b = 0
for i in range(n):
	a = max(a, t[i] - i)
	b = max(b, t[-1 - i] - i)
x = (b - a + n) // 2 + 1
y = 0
for i in range(x):
	s += max(y - t[i], 0)
	y = max(t[i], y) + 1
y = 0
for i in range(n - x):
	s += max(y - t[-1 - i], 0)
	y = max(t[-1 - i], y) + 1
print(s)


n = int(input())

def get_cost(arr):
	cost = [0, 0]
	last = arr[0]
	for i in range(1, len(arr)):
		cost.append(cost[-1] + max(0, last + 1 - arr[i]))
		last = max(last + 1, arr[i])
	return cost
arr = list(map(int, input().split()))
(left_cost, right_cost) = (get_cost(arr), get_cost(list(reversed(arr))))
ans = min(left_cost[n], right_cost[n])
for i in range(1, n - 1):
	ans = min(ans, max(left_cost[i] + right_cost[n - i], left_cost[i + 1] + right_cost[n - i - 1]))
print(ans)


import math
n = int(input())
a = list(map(int, input().split()))
mx = 0
g = [0] * n
r = [0] * n
t1 = [0] * n
t2 = [0] * n
for i in range(n):
	g[i] = max(0, mx - a[i] + 1)
	mx = a[i] + g[i]
	t1[i] = mx
	if i > 0:
		g[i] += g[i - 1]
mx = 0
for i in range(n - 1, -1, -1):
	r[i] = max(0, mx - a[i] + 1)
	mx = a[i] + r[i]
	t2[i] = mx
	if i < n - 1:
		r[i] += r[i + 1]
ans = 10 ** 18
for i in range(n):
	sum = max(t1[i], t2[i]) - a[i]
	if i > 0:
		sum += g[i - 1]
	if i < n - 1:
		sum += r[i + 1]
	ans = min(ans, sum)
print(ans)


n = int(input())
l = [int(i) for i in input().split(' ')]
l_up = l[:]
l_down = l[::-1]
for i in range(n - 1):
	if l_up[i + 1] <= l_up[i]:
		l_up[i + 1] = l_up[i] + 1
for i in range(n - 1):
	if l_down[i + 1] <= l_down[i]:
		l_down[i + 1] = l_down[i] + 1
l_down = l_down[::-1]
index = 0
add = False
for index in range(n - 1):
	if l_up[index] < l_down[index] and l_up[index + 1] >= l_down[index + 1]:
		if l_up[index + 1] == l_down[index + 1]:
			break
		else:
			add = True
			break
	if index == n - 2:
		index = 0
if add == False:
	l_final = l_up[:index + 1] + l_down[index + 1:]
	result = sum(l_final) - sum(l)
else:
	l_final = l_up[:index + 1] + l_down[index + 1:]
	result = sum(l_final) - sum(l) + 1
print(result)