You are given a sequence X of length N, where each element is initially 0. Another sequence A of length N is provided. Determine if X can be made equal to A by a specific operation. If possible, find the minimum number of operations required. The operation involves choosing an integer i (1 ≤ i ≤ N-1) and replacing the value of X_{i+1} with the value of X_i plus 1. Constraints: 1 ≤ N ≤ 2 × 10^5, 0 ≤ A_i ≤ 10^9 (1 ≤ i ≤ N). Input is given as N followed by A_1 to A_N. If X can be made equal to A, print the minimum number of operations required; otherwise, print -1.

# Buggy version:
n=int(input())
l=[int(input()) for _ in range(n)]
ans=0
if l[0]!=0:
  print(-1)
  exit()
for i in range(n-1):
  if l[i+1]-l[i]>1:
    print(-1)
    exit()
  elif (l[i]+1==l[i+1]):
    ans+=1
  else:
	ans+=l[i+1]
print(ans)


# Fixed version:
n=int(input())
l=[int(input()) for _ in range(n)]
ans=0
if l[0]!=0:
  print(-1)
  exit()
for i in range(n-1):
  if l[i+1]-l[i]>1:
    print(-1)
    exit()
  elif (l[i]+1==l[i+1]):
    ans+=1
  else:
    ans+=l[i+1]
print(ans)

# Buggy version:
n = int(input())
a = [int(input()) for _ in range(n)]

ans, tmp, flag = 0, 0, (a[0] == 0)
for i in range(n - 1, 0, -1):
    if a[i] - a[i - 1] > 1:
        frag = False
    elif tmp != a[i]:
        ans += a[i]
    tmp = max(a[i] - 1, 0)
else:
    print(-1 if flag else ans)


# Fixed version:
n = int(input())
a = [int(input()) for _ in range(n)]

ans, tmp, flag = 0, 0, (a[0] > 0)
for i in range(n - 1, 0, -1):
    if a[i] - a[i - 1] > 1:
        print(-1)
        break
    elif tmp != a[i]:
        ans += a[i]
    tmp = max(a[i] - 1, 0)
else:
    print(-1 if flag else ans)

# Buggy version:
#解説参照
import sys
n,*a,=map(int, sys.stdin.read().split())
if a[0]:
    print(-1)
    exit()

ans=0
for i in range(1,n-1):
    if a[i+1]>a[i]+1:
        print(-1)
        exit()
    if a[i+1]-a[i]:
        ans+=1
    else:####
        ans+=a[i+1]

print(ans)

# Fixed version:
#解説参照
import sys
n,*a,=map(int, sys.stdin.read().split())
if a[0]:
    print(-1)
    exit()

ans=0
for i in range(n-1):
    if a[i+1]>a[i]+1:
        print(-1)
        exit()
    if a[i+1]-a[i]==1:
        ans+=1
    else:####
        ans+=a[i+1]

print(ans)

# Buggy version:
n = int(input())
lis = [int(input()) for i in range(n)]
if lis[0] != 0:print(-1);exit()
ans = 0
for i in range(n-1,0,-1):
    if lis[i-1] - lis[i] > 1:print(-1);exit()
    elif lis[i-1] - lis[i] == 1:ans += 1
    else:ans += lis[i]
print(ans)

# Fixed version:
n = int(input())
lis = [int(input()) for i in range(n)]
if lis[0] != 0:print(-1);exit()
ans = 0
for i in range(n-1,0,-1):
    if lis[i] - lis[i-1] > 1:print(-1);exit()
    elif lis[i] - lis[i-1] == 1:ans += 1
    else:ans += lis[i]
print(ans)

# Buggy version:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    a = IR(n)
    if a[0] != 0:
        print(-1)
    ans = 0
    tmp = 0
    for i in a:
        if tmp + 1 == i:
            tmp += 1
            continue
        if tmp >= i:
            ans += tmp
            tmp = i
        else:
            print(-1)
            return
    print(ans + tmp)
    return


#main
if __name__ == '__main__':
    solve()


# Fixed version:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    a = IR(n)
    if a[0] != 0:
        print(-1)
        return
    ans = 0
    tmp = 0
    for i in a:
        if tmp + 1 == i:
            tmp += 1
            continue
        if tmp >= i:
            ans += tmp
            tmp = i
        else:
            print(-1)
            return
    print(ans + tmp)
    return


#main
if __name__ == '__main__':
    solve()

# Buggy version:
n = int(input())
A = [int(input()) for _ in range(n)]
ans = 0
prev = 0
for i in range(n):
    a = A[i]
    if prev == None and a!=0:
        ans = -1
        break
    if prev != None:
        dif = a-prev
        if dif >= 2:
            ans = -1
            break
        elif dif<=0:
            ans += prev
    prev = a
if ans!=-1:
    ans += prev
print(ans)

# Fixed version:
n = int(input())
A = [int(input()) for _ in range(n)]
ans = 0
prev = 0
if A[0]!=0:
  print(-1)
  exit()
for i in range(n):
    a = A[i]
    if prev == None and a!=0:
        ans = -1
        break
    if prev != None:
        dif = a-prev
        if dif >= 2:
            ans = -1
            break
        elif dif<=0:
            ans += prev
    prev = a
if ans!=-1:
    ans += prev
print(ans)

# Buggy version:
import sys
input = sys.stdin.readline
N = int(input())
a = []
for _ in range(N): a.append(int(input()))
if a[0] != 0:
  print(-1)
  exit(0)
for i in range(N - 1):
  if a[i + 1] > a[i] and (a[i + 1] - a[i] > 1):
    print(-1)
    exit(0)
res = 0
s = 0
x = 1
for i in range(1, N):
  if a[i] == x:
    s += 1
  else:
    if x > 0: res += (s - 1) * x + 1
    if x > a[i]:
      res += a[i] - 1
    s = 1
    x = a[i]
  #print(i, res)
if x > 0: res += (s - 1) * x + 1
print(res)

# Fixed version:
import sys
input = sys.stdin.readline
N = int(input())
a = []
for _ in range(N): a.append(int(input()))
if a[0] != 0:
  print(-1)
  exit(0)
for i in range(N - 1):
  if a[i + 1] > a[i] and (a[i + 1] - a[i] > 1):
    print(-1)
    exit(0)
res = 0
s = 0
x = 1
for i in range(1, N):
  if a[i] == x:
    s += 1
  else:
    if x > 0: res += (s - 1) * x + 1
    if x > a[i] and a[i] > 1:
      res += a[i] - 1
    s = 1
    x = a[i]
  #print(i, res)
if x > 0: res += (s - 1) * x + 1
print(res)

# Buggy version:
n, *a = map(int, open(0).read().split())
ans = 0
if a[0] != 0:
	print(-1)
	exit()
for x, y in zip(a, a[1:]):
	if x + 1 < y:
		print(-1)
		exit()
	if x >= y:
		ans += x
print(ans)


# Fixed version:
n, *a = map(int, open(0).read().split())
ans = 0
if a[0] != 0:
	print(-1)
	exit()
for x, y in zip(a, a[1:]+[0]):
	if x + 1 < y:
		print(-1)
		exit()
	if x >= y:
		ans += x
print(ans)

# Buggy version:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
cur = -1
c = 0
for i in a:
  pre,cur = cur,i
  if cur == 0:
    continue
  elif cur <= pre + 1:
    c += 1
  else:
    print(-1)
    exit()
print(c)

# Fixed version:
import sys
input = sys.stdin.readline
n = int(input())
a = [int(input()) for i in range(n)]
cur = -1
c = 0
for i in a:
  pre,cur = cur,i
  if cur == 0:
    continue
  elif cur == pre + 1:
    c += 1
  elif cur <= pre:
    c += cur
  else:
    print(-1)
    exit()
print(c)

# Buggy version:
#Sequence Growing Easy

import sys
input = sys.stdin.readline

N = int(input())
A = [int(input()) for i in range(N)]

judge = 1
for i in range(N):
    if i != N-1:
        if A[i+1] <= A[i]+1 and A[i] <= i:
            pass
        else:
            judge = 0
    else:
        if A[i] <= i:
            pass
        else:
            judge = 0

if judge == 0:
    print(-1)
else:
    n = 1
    for i in range(N-1):
        if A[(N-2)-i] == 0:
            n = n
        elif A[(N-2)-i] < A[(N-1)-i] and A[(N-2)-i] != 0:
            n = n + 1
        elif A[(N-2)-i] == A[(N-1)-i] and A[(N-2)-i] != 0:
            n = n + A[(N-2)-i]
        else:
            n = n + A[(N-1)-i]
    print(int(n))

# Fixed version:
#Sequence Growing Easy

import sys
input = sys.stdin.readline

N = int(input())
A = [int(input()) for i in range(N)]

judge = 1
for i in range(N):
    if i != N-1:
        if A[i+1] <= A[i]+1 and A[i] <= i:
            pass
        else:
            judge = 0
    else:
        if A[i] <= i:
            pass
        else:
            judge = 0

if judge == 0:
    print(-1)
else:
    n = 1
    if A[N-1] == 0:
        n = 0
    for i in range(N-1):
        if A[(N-2)-i] == 0:
            n = n
        elif A[(N-2)-i] < A[(N-1)-i] and A[(N-2)-i] != 0:
            n = n + 1
        elif A[(N-2)-i] == A[(N-1)-i] and A[(N-2)-i] != 0:
            n = n + A[(N-2)-i]
        elif A[(N-2)-i] > A[(N-1)-i] and A[(N-1)-i] != 0:
            n = n + A[(N-1)-i]
        else:
            n = n + 1
    print(int(n))

# Buggy version:
n=int(input());ans=0
l=[int(input()) for i in range(n)]
if l[0]!=0:
  print(-1);exit
for i in range(n-1):
  if l[i+1]>l[i]+1:
    print(-1);exit
for i in range(1,n):
  if l[i]==l[i-1]+1:
    ans+=1
  else:
    ans+=l[i]
print(ans)

# Fixed version:
n=int(input());ans=0
l=[int(input()) for i in range(n)]
if l[0]!=0:
  print(-1);exit()
for i in range(n-1):
  if l[i+1]>l[i]+1:
    print(-1);exit()
for i in range(1,n):
  if l[i]==l[i-1]+1:
    ans+=1
  else:
    ans+=l[i]
print(ans)

# Buggy version:
N = int(input())
ls = []
for i in range(N):
  ls += [int(input())]

ans = 0
p = -1
for i in range(N):
  if i<ls[i]:
    print(-1)
    break
  if p+1==ls[i]:
    if i==N-1:
      ans += (p+1)
    p = ls[i]
    continue
  else:
    ans += p
    p = ls[i]

print(ans)

# Fixed version:
N = int(input())
ls = [-1]
for i in range(N):
  ls += [int(input())]

ans = 0
p = 0
for i in range(1,N+1):
  if ls[i-1]+1<ls[i]:
    print(-1)
    break
  if ls[i-1]+1==ls[i]:
    if i==N:
      ans += (ls[i])
    p = ls[i]
    continue
  else:
    if i==N:
      ans += ls[i]
    ans += p
    p = ls[i]
else:
  print(ans)

# Buggy version:
n = int(input())
num_list = [ int(input()) for i in range(n) ]
num_list.reverse()

ans = 0
t = 0
for i in range(n):
    t = max(t-1, 0)
    if num_list[i] != 0 and num_list[i] > t:
        ans += num_list[i]
        t = num_list[i]
    elif num_list[i] != 0 and num_list[i] < t:
        ans = 0
        break

if t != 0:
    ans = 0
print(ans)


# Fixed version:
n = int(input())
num_list = [ int(input()) for i in range(n) ]
num_list.reverse()


ans = 0
t = 0
for i in range(n):
    t = max(t-1, 0)
    if num_list[i] != 0 and num_list[i] > t:
        ans += num_list[i]
        t = num_list[i]
    elif num_list[i] < t:
        ans = -1
        break

if t != 0:
    ans = -1
print(ans)

# Buggy version:
n = int(input())
a = [int(input()) for _ in range(n)]
for i in range(1,n):
  if a[i]-a[i-1] > 1:
    print(-1)
    exit(0)
cnt = 0
cur = 0
for i in range(n-1,-1,-1):
  if cur < a[i]:
    cur = a[i]
    cnt += a[i]
  if cur:
    cur -= 1
print(cnt)

# Fixed version:
n = int(input())
a = [int(input()) for _ in range(n)]
if a[0] != 0:
  print(-1)
  exit(0)
cnt = 0
cur = 0
for i in range(n-1,-1,-1):
  if cur > a[i]:
    print(-1)
    exit(0)
  if cur < a[i]:
    cur = a[i]
    cnt += a[i]
  if cur > 0:
    cur -= 1
print(cnt)

# Buggy version:
n = int(input())
a = []
ans = 0
for _ in range(n):
    a.append(int(input()))
if a[0] != 0:
    ans = -1
else:
    for i in range(n - 1):
        if (a[i+1] - a[i]) > 1:
            ans = -1
            break
        else:
            if a[i+1] == a[i]:
                if a[i+1] > 0:
                    ans += a[i+1] - 1
            if a[i+1] > 0:
                ans += 1
            if a[i+1] < a[i]:
                ans += a[i+1] - 1
            
print(ans)

# Fixed version:
n = int(input())
a = []
ans = 0
for _ in range(n):
    a.append(int(input()))
if a[0] != 0:
    ans = -1
else:
    for i in range(n - 1):
        if (a[i+1] - a[i]) > 1:
            ans = -1
            break
        else:
            if a[i+1] == a[i]:
                if a[i+1] > 0:
                    ans += a[i+1] - 1
            if a[i+1] > 0:
                ans += 1
            if a[i+1] < a[i]:
                if a[i+1] > 1:
                    ans += a[i+1] - 1
            
print(ans)

# Buggy version:
n=int(input())
arr=[int(input()) for _ in range(n)]
l=0
flag=True
for i in range(n):
  if arr[i]==0:
    l=0
  else:
    if arr[i]>l:
      flag-False
      break
    l+=1
if flag==False:
  print(-1)
else:
  ans=arr[-1]
  tmp=arr[-1]
  for i in range(n-2,-1,-1):
    if arr[i]==tmp:
      ans+=arr[i]
    else:
      tmp=arr[i]
  print(ans)

# Fixed version:
n=int(input())
arr=[int(input()) for _ in range(n)]
l=0
flag=True
for i in range(n):
  if arr[i]==0:
    l=1
  else:
    if arr[i]>l:
      flag=False
      break
    l+=1
  if i!=0:
    if arr[i]-arr[i-1]>=2:
      flag=False
      break
if flag==False:
  print(-1)
else:
  ans=arr[-1]
  tmp=arr[-1]
  for i in range(n-2,-1,-1):
    if arr[i]<tmp:
      tmp=arr[i]
    else:
      ans+=arr[i]
      if tmp!=arr[i]:
        tmp=arr[i]
  print(ans)

# Buggy version:
N = int(input())
l =[]
#deal with error
for i in range(N):
    l.append(int(input()))
    if i == 0 and l[i]!=0:
        print(-1)
        break
    if i>=1:
        if l[i]-l[i-1]>1 :
            print(-1)
            break

ans = 0
pre = -1
for a in l[::-1]:
    if pre - 1 != a:
        ans += a
    pre = a
 
print(ans)

# Fixed version:
N = int(input())
l =[]
#deal with error
for i in range(N):
    l.append(int(input()))
    if i == 0 and l[i]!=0:
        print(-1)
        exit()
    if i>=1:
        if l[i]-l[i-1]>1 :
            print(-1)
            exit()
 
ans = 0
pre = -1
for a in l[::-1]:
    if pre - 1 != a:
        ans += a
    pre = a
print(ans)

# Buggy version:
n=int(input())
li=[0]*n
for i in range(n):
    li[i]=int(input())
fl=0
ans=0
if li[0]!=0:
    fl+=1
for i in range(n-1):
    if li[i]>=li[i+1]:
        ans+=li[i]
    if li[i+1]-li[i]>1:
        fl+=1
if fl>0:
    print(-1)
else:
    print(ans)
        

# Fixed version:
n=int(input())
li=[0]*n
for i in range(n):
    li[i]=int(input())
fl=0
ans=0
if li[0]!=0:
    fl+=1
for i in range(n-1):
    if li[i]>=li[i+1]:
        ans+=li[i]
    if li[i+1]-li[i]>1:
        fl+=1
if fl>0:
    print(-1)
else:
    print(ans+li[n-1])

# Buggy version:
n = int(input())
x = [int(input()) for _ in range(n)]
f = 0

if x[0]>0: f = 1
for i in range(n-1):
    if x[i]+1 < x[i+1]: f = 1
import sys
if f:
    print(-1)
    sys.exit()
ans = 0
for i in range(n-1):
    if x[i+1] == x[i]+1:
        ans += 1
    else:
        ans += x[i+1]
    print(ans)
print(ans)

# Fixed version:
n = int(input())
x = [int(input()) for _ in range(n)]
f = 0

if x[0]>0: f = 1
for i in range(n-1):
    if x[i]+1 < x[i+1]: f = 1
import sys
if f:
    print(-1)
    sys.exit()
ans = 0
for i in range(n-1):
    if x[i+1] == x[i]+1:
        ans += 1
    else:
        ans += x[i+1]
print(ans)

# Buggy version:
N = int(input())
A = [int(input()) for _ in range(N)]
    
if A[0] != 0:
    print(-1)

else:
    roop = 0
    for i in range(1,N):
        num = A[i]
        if num == A[i-1]+1:
            roop += 1
        elif num == A[i-1]:
            roop += num
        elif num == 0:
            continue
        else:
            roop = -1
            break
    print(roop)
            

# Fixed version:

N = int(input())
A = [int(input()) for _ in range(N)]
    
if A[0] != 0:
    print(-1)

else:
    roop = 0
    for i in range(1,N):
        num = A[i]
        if num == A[i-1]+1:
            roop += 1
        elif num <= A[i-1]:
            roop += num
        else:
            roop = -1
            break
    print(roop)

# Buggy version:
n = int(input())
a = []
ans = 0
flag = True
for i in range(n):
    A = int(input())
    a.append(A)
next = 0
for i in range(n-1,-1,-1):
    if a[i] == 0:
        next = 0
        continue
    if next - a[i] == 1:
        next = a[i]
        continue
    if next - a[i] >= 2:
        flag = False
        break
    next = a[i]
    ans += a[i]
if a[0] != 0:
    flag = False
if flag:
    #print(ans)
else:
    #print(-1)
print(-1)


# Fixed version:
n = int(input())
a = []
ans = 0
flag = True
for i in range(n):
    A = int(input())
    a.append(A)
next = 0
for i in range(n-1,-1,-1):
    if next - a[i] >= 2:
        flag = False
        break
    if a[i] == 0:
        next = 0
        continue
    if next - a[i] == 1:
        next = a[i]
        continue
    next = a[i]
    ans += a[i]
if a[0] != 0:
    flag = False
if flag:
    print(ans)
else:
    print(-1)

# Buggy version:
N = int(input())

start = int(input())
t = 0

for i in range(N-1):
 k = int(input())
 if k == start:
  t += start
 elif k == start + 1:
  t += 1
  start = k
 elif k == 0:
  start = 0
 elif k > start + 1:
  t = -1
  break
 else:
  t += k
  start = k

print(t)

# Fixed version:
N = int(input())

start = int(input())
flag = 0
if start != 0:
 flag = 1
t = 0

for i in range(N-1):
 k = int(input())
 if k == start:
  t += start
 elif k == start + 1:
  t += 1
  start = k
 elif k == 0:
  start = 0
 elif k > start + 1:
  t = -1
  break
 else:
  t += k
  start = k

if flag ==1:
 t = -1
print(t)

# Buggy version:
#!python
# -*- coding: utf-8 -*-


def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    X = [0 for i in range(N)]

    if A[0] != 0:
        print(-1)
        return

    count = 0

    for i in range(N - 1, -1, -1):
        X[i] = X[i + 1] - 1
        if A[i] == X[i]:
            continue

        count += A[i]
        X[i] = A[i]

    print(count)


if __name__ == '__main__':
    main()


# Fixed version:
#!python
# -*- coding: utf-8 -*-


def main():
    N = int(input())
    A = [int(input()) for i in range(N)]
    X = [0 for i in range(N)]
    A += [0]
    X += [0]

    count = 0

    if A[0] != 0:
        print(-1)
        return

    for i in range(N - 1, -1, -1):
        if A[i - 1] + 1 < A[i]:
            print(-1)
            return

        X[i] = max(0, X[i + 1] - 1)
        if A[i] == X[i]:
            continue

        count += A[i]
        X[i] = A[i]

    print(count)


if __name__ == '__main__':
    main()

# Buggy version:
n = int(input())
a = [int(input()) for i in range(n)]
ans,flag = 0,True
for i in range(n-1):
	if a[i]+1==a[i+1]: ans+=1
	elif a[i]>=a[i+1]: ans+=a[i+1]
	else:
		flag = False
		break
if flag: print(ans)
else: print(-1)

# Fixed version:
n = int(input())
a = [int(input()) for i in range(n)]
ans,flag = 0,True
if a[0]!=0: flag = False
for i in range(n-1):
	if a[i]+1==a[i+1]: ans+=1
	elif a[i]>=a[i+1]: ans+=a[i+1]
	else:
		flag = False
		break
if flag: print(ans)
else: print(-1)