Problem:
 Score : 700 points    Problem Statement There is a sequence X of length N , where every element is initially 0 . Let X_i denote the i -th element of X .  You are given a sequence A of length N . The i -th element of A is A_i . Determine if we can make X equal to A by repeating the operation below. If we can, find the minimum number of operations required.   Choose an integer i such that 1\leq i\leq N-1 . Replace the value of X_{i+1} with the value of X_i plus 1 .       Constraints  1 \leq N \leq 2 \times 10^5  0 \leq A_i \leq 10^9(1\leq i\leq N)  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  A_1  :  A_N       Output If we can make X equal to A by repeating the operation, print the minimum number of operations required. If we cannot, print -1 .        Sample Input 1 4
0
1
1
2      Sample Output 1 3  We can make X equal to A as follows:   Choose i=2 . X becomes (0,0,1,0) .  Choose i=1 . X becomes (0,1,1,0) .  Choose i=3 . X becomes (0,1,1,2) .        Sample Input 2 3
1
2
1      Sample Output 2 -1       Sample Input 3 9
0
1
1
0
1
2
2
1
2      Sample Output 3 8

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
if any(a - b > 1for a, b in zip(A, [-1]+A)): print(-1)
else:
    ans = 0
    for a, nxt in zip(A, A[1:]+[0]):
        if a >= nxt:
            ans += a
    print(ans)

Solution (Python):
from functools import reduce

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


ans, _ = reduce(
    lambda acc, i: (
        (
            -1 if acc[0] == -1 or A[i] < acc[1] - 1 or A[i] > i else
            acc[0] if A[i] == acc[1] - 1 else
            acc[0] + A[i]
        ),
        A[i]
    ),
    reversed(range(N)),
    (0, 0)
)

print(ans)

Solution (Python):
def is_possible(A):
    if A[0] != 0:
        return False
    for i in range(1, len(A)):
        if A[i-1] < A[i] - 1:
            return False
    return True

def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    if not is_possible(A):
        print(-1)
        return
    ans = 0
    i = N-1
    while i >= 0:
        if A[i] > 0:
            x = A[i]
            ans += x
            while i >= 0 and x > 0 and A[i] == x:
                i -= 1
                x -= 1
        else:
            i -= 1
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
def is_possible(A):
    if A[0] != 0:
        return False
    for i in range(1, len(A)):
        if A[i-1] < A[i] - 1:
            return False
    return True

def main():
    N = int(input())
    A = [int(input()) for _ in range(N)]
    if not is_possible(A):
        print(-1)
        return
    B = reversed(A)
    ans = 0
    i = N-1
    while i >= 0:
        if A[i] > 0:
            x = A[i]
            ans += x
            while i >= 0 and x > 0 and A[i] == x:
                i -= 1
                x -= 1
        else:
            i -= 1
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
a = [-1]
for i in range(n):
    a.append(int(input()))

ans = 0
cur = 0
for i, item in enumerate(a[::-1]):
    if i == 0:
        cur = item
        ans += item
        continue
    cur -= 1
    if cur < item:
        ans += item
        cur = item
    elif cur == item:
        continue
    elif cur > item:
        print(-1)
        exit()
print(ans)

Solution (Python):
a = int(input())
l = []

for i in range(a):
    t = int(input())
    l.append(t)
t =1
res = 0
for i in l[::-1]:
    if i >= t:
        res += i
    elif t - i == 1:
        t = 1
    else:
        l[0] = 1
    t = i
if l[0] ==0 :print(res)
else:print(-1)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]

if A[0] !=0:
  print(-1)
else:
  ans = 0
  for i in range(1,N):
    if A[i] > A[i-1]+1:
      ans = -1
      break
    elif A[i] == A[i-1]+1:
      ans += 1
    else:
      ans += A[i]
  print(ans)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
def check():
    globals()
    s = 0
    if a[0]!=0:
        return -1
    for i in range(n-1):
        if a[i+1]<=a[i]:
            s+=a[i]
        if a[i+1]>a[i]+1:
            return -1
    return s+a[-1]
print(check())

Solution (Python):
n,*a=map(int,open(0))
c=0
for i in range(n-1,0,-1):
  if a[i]-a[i-1]>1:
    print(-1)
    exit()
  if a[i]<=a[i-1]:
    c+=a[i]
  else:
    c+=1
print(-(a[0]>0)or c)

Solution (Python):
n,*a=map(int,open(0))
c=0
for i in range(n-1,0,-1):
  if a[i]-a[i-1]>1:
    print(-1)
    exit()
  if a[i]<=a[i-1]:
    c+=a[i]
  else:
    c+=1
print(-(a[0]>0)or c)

Solution (Python):
N=int(input())
L=[]
for i in range(N):
    a=int(input())
    L.append(a)

if L[0]!=0:
    print(-1)
    exit()
for i in range(1,N):
    if L[i]>L[i-1]+1:
        print(-1)
        exit()
L.append(0)
L=L[::-1]

ans=0
for i in range(1,len(L)):
    if L[i]>=L[i-1]:
        ans+=L[i]
print(ans)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]

if a[0] != 0:
    print(-1)
    exit()

l = [-1]
for i in a:
    if i == l[-1] + 1:
        l[-1] += 1
    elif i <= l[-1]:
        l.append(i)
    else:
        print(-1)
        exit()

print(sum(l))

Solution (Python):


n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))

ans=0
pos=n
pre=0
b=a[0]
for i in range(n):
    x=a.pop()
    if x==0 and pre<=1:
        continue
    elif x>pos-1:
        ans=-1
        break
    else:
        if x==pre-1:
            pre=x
            continue
        else:
            if x>=pre:
                pre=x
                ans+=x
            else:
                ans=-1
                break
    pos-=1
if b>0:
    ans=-1
print(ans)

Solution (Python):



"""
a,b,c,k = map(int ,input().split())
s = b-a
if (k%2 == 0):
    s *= -1
print(s)
"""


n = int(input())
a = [int(input()) for i in range(n)]
if a[0] != 0:
    print(-1)
    quit()
for i in range(1,n):
    if a[i] > i:
        print(-1)
        quit()
    if a[i]-a[i-1] > 1:
        print(-1)
        quit()
ans = a[-1]
for i in range(n-1)[::-1]:
    if a[i]+1 == a[i+1]:continue
    ans += a[i]
print(ans)

Solution (Python):
N = int(input())
l =[]

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)

Solution (Python):
import sys
N = int(input()) 
A = [int(sys.stdin.readline()) for _ in range(N)]
b = 0
for i, a in enumerate(A):
    if i + b < a:
        print(-1)
        sys.exit()
    else:
        b = a - i
ans = 0
pre = -1
for a in A[::-1]:
    if pre - 1 != a:
        ans += a
    pre = a
print(ans)

Solution (Python):
import sys
N = int(input()) 
A = [int(sys.stdin.readline()) for _ in range(N)]
b = 0
for i, a in enumerate(A):
    if i + b < a:
        print(-1)
        sys.exit()
    else:
        b = a - i
ans = 0
pre = -1
for a in A[::-1]:
    if pre - 1 != a:
        ans += a
    pre = a

print(ans)

Solution (Python):
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])

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

def calc():
    count = 0
    last = 0
    if a[0] > 0:
        return -1
    for v in a:
        if v > last + 1:
            return -1
        if v == last + 1:
            count += 1
        else:
            count += v
        last = v
    return count
print(calc())

Solution (Python):
n = int(input())
a = []
for i in range(n):
    temp = int(input())
    a.append(temp)
if a[0] != 0:
    print("-1")
else:
    check = True
    for i in range(n-1):
        if a[i+1] >= a[i] + 2:
            print("-1")
            check = False
            break
    if check:
        ans = sum(a)
        for i in range(n-1):
            if a[i+1] == a[i]+1:
                ans -= a[i]
        print(ans)

Solution (Python):
N = int(input())
ans = 0
bool1 = False

a0 = int(input())
if N == 1:
    if a0 != 0:
        print(-1)
    else:
        print(0)

else:
    for i in range(N-1):
        a1 = int(input())
        if i == 0 and a0 != 0:
            bool1 = True
            break
        if a1 - a0 > 1:
            bool1 = True
            break
        elif a1 - a0 == 1:
            a0 = a1
            continue
        else:
            ans += a0
            a0 = a1

    if bool1 == True:
        print(-1)
    else:
        print(ans + a1)

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
l=-1
ans=0
for i in range(N):
  if A[i]>l+1:
    print(-1)
    exit()
  elif A[i]==l+1:
    ans+=1
  else:
    ans+=A[i]
  l=A[i]
print(ans-1)

Solution (Python):
n = int(input())
a = [int(input()) for i in range(n)]
ans = 0
if a[0] != 0:
  print(-1)
  exit()
for i in range(1,n):
  if a[i]-1 == a[i-1]:
    pass
  else:
    if a[i]>a[i-1]+1:
      print(-1)
      exit()
    else:
      ans += a[i-1]
ans += a[n-1]
print(ans)

Solution (Python):
N = int(input())
 
prev = 0
ans = 0
able = True
for i in range(N):
	now = int(input())
	
	if now - prev > 1 or (i == 0 and now != 0):
		print(-1)
		able = False
		break
	elif now - prev == 1:
		ans += 1
	elif now:
		ans += now
	prev = now

if able:
	print(ans)

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
if A[0]!=0:
    print(-1)
else:
    flag=0
    for i in range(1,N):
        if A[i]-A[i-1]>1:
            flag=1
            break
    if flag==1:
        print(-1)
    else:
        ans=0
        for i in range(1,N):
            if A[i-1]+1==A[i]:
                ans+=1
            else:
                ans+=A[i]
        print(ans)

Solution (Python):
import sys

n, = [int(x) for x in input().strip().split(" ")]
n = int(n)

As = []
for line in sys.stdin:
    a, = line.strip().split(" ")
    As.append(int(a))


if As[0] != 0:
    print(-1)
    sys.exit()

result = 0
for i in range(n):
    k = n-1-i
    As[k]
    if As[k] > k:
        print(-1)
        sys.exit()
    if k == n-1:
        result += As[k]
    else:
        if As[k] > As[k+1]:
            result += As[k]
        if As[k] == As[k+1]:
            result += As[k]
        if As[k] + 1 < As[k+1]:
            print(-1)
            sys.exit()

print(result)

Solution (Python):
N = int( input())
A = [ int( input()) for _ in range(N)]
now = 0
ans = 0
if A[0] != 0:
    ans = -1
else:
   for i in range(1,N):
       a = A[i]
       
       
       
       
       if now + 1 == a:
           ans += 1
           now += 1
       elif now >= a:
           ans += a
           now = a
       else:
           ans = -1
           break
print(ans)

Solution (Python):
N = int( input())
A = [ int( input()) for _ in range(N)]
now = 0
ans = 0
if A[0] != 0:
    ans = -1
else:
   for i in range(1,N):
       
       
       
       if now + 1 == A[i]:
           ans += 1
           now += 1
       elif now >= A[i]:
           ans += A[i]
           now = A[i]
       else:
           ans = -1
           break
print(ans)

Solution (Python):
N = int( input())
A = [ int( input()) for _ in range(N)]
now = 0
ans = 0
if A[0] != 0:
    ans = -1
else:
   for i in range(1,N):
       a = A[i]
       if a > i:
           ans = -1
           break
       elif now + 1 == a:
           ans += 1
           now += 1
       elif now >= a:
           ans += a
           now = a
       else:
           ans = -1
           break
print(ans)

Solution (Python):
N = int( input())
A = [ int( input()) for _ in range(N)]
now = 0
ans = 0
if A[0] != 0:
    ans = -1
else:
   for i in range(1,N):
       if A[i] > i:
           ans = -1
           break
       elif now + 1 == A[i]:
           ans += 1
           now += 1
       elif now >= A[i]:
           ans += A[i]
           now = A[i]
       else:
           ans = -1
           break
print(ans)

Solution (Python):
N = int(input())
moves=0
previous=0
exists=1
a = [0 for i in range(N)]
for i in range(N):
  a[i]=int(input())
if a[0]!=0:
  exists=0
for i in [N-j-1 for j in range(N)]:
  if a[i]>=previous:
    moves+=a[i]
  elif a[i]<previous-1:
    exists=0
  previous = a[i]
if exists==0:
  print(-1)
else:
  print(moves)

Solution (Python):
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)

Solution (Python):
N = int(input())
As = [int(input()) for i in range(N)]

if As[0] != 0:
    
    print(-1)
    exit()

ans = 0
for i in reversed(range(1, N)):
    if As[i - 1] + 1 < As[i]:
        
        ans = -1
        break
    elif As[i - 1] + 1 == As[i]:
        
        ans += 1
    else:
        
        ans += As[i]

print(ans)

Solution (Python):
n=int(input())
a=[]
for _ in range(n):
	a.append(int(input()))
a.append(0)

def ans():
	if a[0]>0:
		return -1
	else:
		ans=0
		for i in range(n):
			if a[i+1]-a[i]>1:
				return -1
			elif a[i+1]-a[i]<1:
				ans+=a[i]
		return ans

print(ans())

Solution (Python):
n = int(input())
list = [int(input()) for _ in range(n)]

def main(n, list):
    result = 0
    num = 0
    if list[0] != 0:
        return -1
    
    for i in range(n - 1, - 1, - 1):
        if list[i] >= num:
            result += list[i]
            num = list[i]
        elif list[i] == num - 1:
            num -= 1
        else:
            return -1
    return result

print(main(n, list))

Solution (Python):
N = input()
A = []
for i in xrange(N):
	A.append(input())
ans = 0
start = 0
for i in xrange(N):
	if A[i] > i - start:
		ans = -1
		break
	if A[i] > A[i-1] + 1:
		ans = -1
		break
	if i == 0:
		continue
	if A[i] == A[i - 1] + 1:
		ans += 1
		continue
	if A[i] == 0:
		start = i
		continue
	ans += A[i]
print ans

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
if a[0] != 0:
    print("-1")
else:
    x = [0] * len(a)
    d = [0] * len(a)
    dd = [0] * len(a)

    c = 0
    s = 10**6
    
    wala = False
    for i in range(len(a) - 1, 0, -1):
        
        if i <= s and a[i] == 0:
            
            continue
        if i - a[i] < 0 or i - a[i] > s:
            
            wala = True
            break
        if s + a[i] == i:
            continue
        s = min(s, i - a[i])
        c += a[i]
    if wala:
        print("-1")
    else:
        print(c)

Solution (Python):
N = int(input())
data = [int(input()) for i in range(N)]
c = 0
if data[0] > 0:
    print(-1)
    exit()
for i in range(1,N)[::-1]:
    if data[i] - data[i-1] > 1:
        print(-1)
        exit()
    if data[i] - data[i-1] == 1:
        c += 1
    if data[i] - data[i-1] < 1:
        c += data[i]
print(c)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
 
if a[0] != 0:
	print(-1)
else:
	ans = 0
	muri = 0
 
	for i in range(1, n):
		if a[i] > a[i-1] + 1:
			muri = 1
		elif a[i] < a[i-1] + 1:
			ans += a[i-1]
			if i == n - 1:
				ans += a[i]
		elif i == n - 1:
			ans += a[i]
 
	if muri:
		print(-1)
	else:
		print(ans)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]

if a[0] != 0:
	print(-1)
else:
	ans = 0
	muri = 0

	for i in range(1, n):
		if a[i] > a[i-1] + 1:
			muri = 1
		elif a[i] < a[i-1] + 1:
			ans += a[i-1]
			if i == n - 1:
				ans += a[i]
		elif i == n - 1:
			ans += a[i]

	if muri:
		print(-1)
	else:
		print(ans)

Solution (Python):
N=int(input())
A=[int(input()) for i in range(N)]
ans=0

for i in range(N):
    
    
    if i==0:
        if A[i]!=0:
            ans=-1
            break
    else:
        if A[i-1]+1 > A[i]:
            ans+=A[i-1]
        elif A[i-1]+1==A[i]:
            continue
        else:
            ans=-1
            break
else:
    ans+=A[i]
print(ans)

Solution (Python):
N = int(input())
ans = 0
prior = 0
for i in range(N):
  a = int(input())
  if i == 0 and a != 0:
    ans = -1
    break
  if a == 0:
    prior = a
  elif a == 1:
    prior = a
    ans += 1
  elif a == prior + 1:
    ans += 1
    prior = a
  elif a <= prior:
    ans += a
    prior = a
  else:
    ans = -1
    break
print(ans)

Solution (Python):
import numpy as np

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


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


def lvi(N):
    l = []
    for _ in range(N):
        l.append(ii())
    return l


def lv(N):
    l = []
    for _ in range(N):
        l.append(input())
    return l


def main():
    N = ii()
    A = []
    for _ in range(N):
        a = ii()
        A.append(a)

    ans = 0
    if A[0] != 0:
        return -1

    for i in range(1, N):
        diff = A[i] - A[i-1]
        if diff == 1:
            pass
        elif diff == 0:
            ans += A[i-1]
        elif diff > 1:
            return -1
        else:
            ans += A[i-1]

    ans += A[-1]

    return ans

if __name__ == '__main__':
    print(main())

Solution (Python):
n = int(input())
A = [-1]
for _ in range(n):
    A.append(int(input()))

num = 0
count = 0
if A[1] != 0:
    print(-1)
else:
    for i in range(n,0,-1):
        if A[i] > num:
            num = A[i]
            count += num
        elif A[i] < num:
            print(-1)
            break
        num -= 1
    else:
        print(count)

Solution (Python):
n = int(input())

p = [int(input()) for _ in range(n)]



count = 0
climing = 0

if p[0] !=0:
    print(-1)
    exit()

for index in range(len(p) - 1):
    current_num = p[index]
    next_num = p[index + 1]

    if current_num == next_num:
        count += current_num
    elif current_num + 1 == next_num:
        pass
    elif current_num > next_num:
        count += current_num
    else:
        
        print(-1)
        exit()


count += p[-1]
print(count)

Solution (Python):


import math
import sys
import itertools
import numpy as np

n =int(input())
x = [int(input()) for i in range(n)] 
sousa=0
if x[0] != 0:
    print("-1")
    exit()
for i in range(1,n):
    if x[i] > x[i-1] + 1:
        print("-1")
        exit()
    elif x[i] == x[i-1] + 1:
        sousa += 1
    else:
        sousa += x[i]
print(sousa)

Solution (Python):
n = int(input())

a = []
for i in range(n):
    a.append(int(input()))

if a[0] > 0:
    print(-1)
    exit()

for i in range(1, n):
    if a[i] > i:
        print(-1)
        exit()
    if a[i] > a[i - 1] + 1:
        print(-1)
        exit()

if n == 1:
    print(0)
    exit()

result = 0

if a[n-1] != 0:
    result += a[n-1]

for i in range(2, n):
    j = n - i
    if a[j] != 0 and a[j] != a[j + 1] - 1:
            result += a[j]

print(result)

Solution (Python):
def main():

    N = int(input())
    a = int(input())
    A = [a]

    if a != 0:
        return -1

    else:
        for i in range(N-1):
            a_ = int(input())
            if a_ > a + 1:
                return -1

            else:
                a =  a_
                A.append(a)
            
        count = 0
        for i in range(N-1):
            a = A[N-1-i]
        
            if a == 0:
                continue
            elif a == A[N-2-i] + 1:
                count += 1
            else:
                count += a
                
        return count

print(main())

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**15
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
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 main():
    n = I()
    a = [I() for _ in range(n)]
    if a[0] > 0:
        return -1
    for i in range(n-1):
        if a[i] < a[i+1] - 1:
            return -1
    r = a[-1]
    for i in range(n-2,-1,-1):
        if a[i] == a[i+1] - 1:
            continue
        r += a[i]

    return r




print(main())

Solution (Python):
import sys
N = int(input())
A = [int(input()) for i in range(N)]

if A[0]!=0:
    print(-1)
    sys.exit()
for i in range(N-1):
    if A[i+1]-A[i]>1:
        print(-1)
        sys.exit()

num = 0
for i in range(N-1):
    if A[i]>=A[i+1]:
        num += A[i]
num += A[-1]
print(num)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
A2 = [index - a for index, a in enumerate(A)]


def solve():
    if A[0] != 0:
        return -1
    for i in range(N - 1):
        if A[i + 1] - A[i] > 1:
            return -1
    ans = 0
    for i in range(N - 1):
        if A2[i] != A2[i + 1]:
            ans += i - A2[i]
    else:
        i = N - 1
        ans += i - A2[i]
    return ans

print(solve())

Solution (Python):
N = int(input())

ans = 0
arr = [-1]
for i in range(N):
    A = int(input())
    if A > arr[i] + 1:
        print(-1)
        break
    if A == arr[i] + 1 and i != 0:
        ans += 1
    else:
        ans += A
    arr.append(A)
else:
    print(ans)

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
count=0

if a[0]!=0: count=-1
else:
    for i in range(n-1):
        if a[i]+1==a[i+1]:
            count+=1
        elif a[i]+1<a[i+1]:
            count=-1
            break
        else:
            count+=a[i+1]
print(count)

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]
if A[0] > 0:
    print(-1)
else:
    ans = 0
    for n in range(N-1, -1, -1):
        if A[n] - A[n-1] > 1 or A[n] > n:
            print(-1)
            break
        else:
            if A[n] - A[n-1] == 1:
                ans += 1
            else:
                ans += A[n]
    else:
        print(ans)

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

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

for i in range(N - 1):
    if A[i] < A[i + 1] and A[i + 1] - A[i] != 1:
        print(-1)
        exit()
    if A[i] > i + 1:
        print(-1)
        exit()

ans = 0
for i in range(1, N):
    if A[i-1] >= A[i]:
        ans += A[i-1]
print(ans + A[-1])

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

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

for i in range(N - 1):
    if A[i] < A[i + 1] and A[i + 1] - A[i] != 1:
        print(-1)
        exit()
    if A[i] > i + 1:
        print(-1)
        exit()

ans = 0
i = 1
old = 0
while i != N:
    if A[i] <= old:
        ans += old
    old = A[i]
    i += 1

print(ans + A[-1])

Solution (Python):
n=int(input())
a=[int(input()) for i in range(n)]
a0=0
ans=0
for ai in a[::-1]:
  
  if ai>a0-1:
    ans+=ai
  elif ai<a0-1:
    print(-1)
    exit()
  a0=ai
  
if a0 !=0:
  print(-1)
else:
  print(ans)

Solution (Python):
import bisect

def main():
    n = int(input())
    a = []
    for _ in range(n):
        a.append(int(input()))
    a.append(0)
    ans = solve(a, n)
    print(ans)

def solve(a, n):
    if not check(a, n):
        return -1

    base = 0
    s = 0
    for i in range(n):
        if a[i] > base + i:
            return -1
        
        if a[i + 1] <= a[i]:
            s += a[i]
        
        base = i - a[i]
    
    return s

def check(a, n):
    if a[0] != 0:
        return False
    
    for i in range(1, n):
        if a[i] - a[i - 1] > 1:
            return False

    return True

main()

Solution (Python):
n = int(input())
nums = [int(input()) for _ in range(n)]
if nums[0] != 0:
    print(-1)
    exit()

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

Solution (Python):
N = int(input())
A = [int(input()) for _ in range(N)]

count = 0
for i in range(N):
    if i == 0 and A[i] > 0:
        print(-1)
        exit()
    if A[i] == 0:
        continue
    else:
        if A[i] == (A[i-1] + 1):
            count += 1
        elif A[i] > A[i-1] + 1:
            print(-1)
            exit()
        else:
            count += A[i]

print(count)

Solution (Python):
n=int(input())
r=0
b=0
for i in range(n):
 t=int(input())
 if i==0!=t or b+1<t:print(-1);exit()
 elif b>=t:r+=b
 b=t
print(r+b)

Solution (Python):

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)

Solution (Python):
k = 0
m = 0
d = int(input())
p = [0]*d
for i in range(d):
  p[i] = int(input())
  if i == 0:
    if p[0] != 0:
      print(-1)
      m = 1
      break
  else:
    if p[i]  <= p[i-1]:
      k += p[i]
    elif p[i] <= 1 + p[i-1]:
      k += 1
    else:
      print(-1)
      m = 1
      break
 
if m != 1:
  print(k)

Solution (Python):
k = 0
m = 0
d = int(input())
p = [0]*d
for i in range(d):
  p[i] = int(input())
  if i == 0:
    if p[0] != 0:
      print(-1)
      m = 1
      break
  else:
    if p[i]  <= p[i-1]:
      k += p[i]
    elif p[i] <= 1 + p[i-1]:
      k += 1
    else:
      print(-1)
      m = 1
      break

if m != 1:
  print(k)

Solution (Python):
N = int(input())
A = []
for _ in range(N):
    A.append(int(input()))
if A[0] != 0:
    print(-1)
else:
    count = 0
    prev = 0
    for i in range(1, N):
        temp = A[i] - prev
        if 0 < temp:
            if 2 <= temp:
                print(-1)
                break
            if A[i] == prev + 1:
                count += 1
            else:
                count += A[i]
            prev = A[i]
        else:
            count += A[i]
            prev = A[i]
    else:
        print(count)

Solution (Python):
import sys

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

for i in range(N - 1):
    if A[i + 1] - A[i] > 1:
        print('-1')
        exit()

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

A.append(0)

count = 0
for i in range(N):
    if A[i+1] - A[i] <= 0:
        count += A[i]

print(count)

Solution (Python):
import sys,collections,random,math;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n = I()
t = I()
if t != 0:
    print(-1)
    exit()
sums = 0
for i in range(n-1):
    c = I()
    if c-t > 1 or c > i+1:
        print(-1)
        exit()
    if c-t != 1:
        sums += t
        s = 0
    if i == n-2:
        sums += c
    t = c
print(sums)

Solution (Python):
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)

Solution (Python):
n = int(input())
a = [int(input()) for _ in range(n)]
ans = 0
if a[0] != 0:
    ans = -1
elif n == 1:
    ans = 0
else:
    b = a[1:]
    for k,i in enumerate(b):
        if i <= 1:
            ans += i
        else:
            num = i-a[k]
            if num == 1:
                ans += 1
            elif num <= 0:
                ans += i
            else:
                ans = -1
                break
print(ans)

Solution (Python):
N = int(input())

ans = 0

L = []
isok = True
for i in range(N):
    n = int(input())
    if n>i: isok = False
    L.append(n)

for i in range(N-1):
    if L[i+1] <= L[i]:
        ans += L[i+1]
    elif L[i+1] == L[i]+1:
        ans += 1
    elif L[i+1] > L[i]:
        isok=False
        break
    
if isok:
    print(ans)
else:
    print(-1)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect, bisect_left

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

N = read()

A = []
for _ in range(N):
  A.append(read())

if A[0] != 0 or not all(A[i-1] >= A[i] - 1 for i in range(1, N)):
  print(-1)
  exit()

S = sorted(set(i - A[i] for i in range(N)))
print('S:', S, file=stderr)

ans = 0
for i in range(N):
  x = bisect_left(S, i) - bisect_left(S, i - A[i])
  print(i, x, file=stderr)
  ans += x
print(ans)

Solution (Python):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))

flag = True
for i in range(n - 1):
    if a[i + 1] - a[i] >= 2:
        flag = False
if a[0] != 0:
    flag = False

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

Solution (Python):
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)

Solution (Python):
def solve(n, aaa):
    if aaa[0] > 0:
        return -1
    if any(a1 - a0 > 1 for a0, a1 in zip(aaa, aaa[1:])):
        return -1
    ans = 0
    a0 = 0

    for a1 in aaa[1:]:
        if a1 - a0 != 1:
            ans += a0
        a0 = a1

    return ans + a0


n = int(input())
aaa = list(map(int, (input() for _ in range(n))))
print(solve(n, aaa))

Solution (Python):
N = int(input())
src = [int(input()) for i in range(N)]
if src[0]:
    print(-1)
    exit()

ans = prev = 0
for a in reversed(src):
    if prev > 0:
        if a == prev - 1:
            prev = a
            continue
        elif a < prev:
            print(-1)
            exit()
    ans += a
    prev = a
print(ans)

Solution (Python):
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input()


def read_str_n():
    return list(map(str, input().split()))


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        print(e - s, 'sec', file=sys.stderr)
        return ret

    return wrap


@mt
def slv(N, A):

    if A[0] != 0:
        return -1

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

    ans = A[-1]
    for i in range(N - 1):
        if A[i] >= A[i + 1]:
            ans += A[i]
    return ans


def main():
    N = read_int()
    A = [read_int() for _ in range(N)]

    print(slv(N, A))


if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
p = []
for i in range(n):
    p.append(int(input()))

count = 0
if p[0] != 0:
    print(-1)
    exit()

for i in range(1, n):
    diff = p[i] - p[i - 1]
    if diff >= 2:
        print(-1)
        exit()
    elif diff == 1:
        count += 1
    else:
        count += p[i]

print(count)