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



Sample input:
4
0
1
1
2


Sample output:
3

Submission status: Accepted

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

ans = 0
for i in range(N-1, -1, -1):
    if A[i] > i or A[i+1]-A[i] >= 2:
        print(-1)
        exit()
    elif A[i+1]-A[i] == 1:
        continue
    else:
        ans += A[i]
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=[int(input()) for _ in range(N)]
tab = [0]*N
prev=10**10
for i,a in enumerate(A):
    dst = i-a
    if dst < 0 or dst >=N or (a>prev and a-prev>1):
        print(-1)
        exit()
    tab[dst] = max(tab[dst], a)
    prev = a
print(sum(tab))

Submission status: Accepted

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

ok = True
pre = -1
for i, a in enumerate(A):
    if a > pre + 1:
        ok = False
        break
    pre = a

if not ok:
    print(-1)
else:
    c = 0
    for i in range(N-1):
        if A[i+1] != A[i] + 1:
            c += A[i+1]
        else:
            c += 1
    print(c)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(input()) for i in range(n)]

if a[0] != 0:
    print(-1)
    exit()
inc_cnt = 0
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]:
        inc_cnt = a[i+1]
    else:
        ans += inc_cnt
        inc_cnt = a[i+1]
ans += inc_cnt
print(ans)

Submission status: Accepted

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

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

ans = 0
tmp = 0
for num in reversed(A):
    if tmp > 0:
        if num == tmp-1:
            tmp = num
        elif num >= tmp:
            ans += num
            tmp = num
        elif num < tmp-1:
            print(-1)
            exit()

    else:
        ans += num
        tmp = num
        

print(ans)

Submission status: Accepted

Solution code:


def solve(N, A):
    c = 0
    z = 0
    if A[0] != 0:
        return -1
    for i in range(1, N):
        if A[i] == 0:
            z = 0
            c+=A[i-1]
        elif A[i-1] < A[i] - 1:
            return -1
        elif A[i] <= i - z:
            if A[i] != A[i-1]+1:
                c += A[i-1]
        else:
            return -1
        #print("A[%d]=%d, c=%d" % (i, A[i], c))
    c += A[-1]
    return c

def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = [0]*N
    for i in range(N):
        A[i] = int(input())
    print(solve(N, A))

main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC24 C

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#AGC24 C

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = [I() for _ in range(n)]
if a[0] != 0:
    print(-1)
    quit()
for i in range(1,n):
    if a[i] - a[i-1] > 1:
        print(-1)
        quit()
ans = 0
for i in range(1,n):
    if a[i] == a[i-1] and a[i] > 0:
        ans += a[i]
    elif a[i] == a[i-1] + 1:
        ans += 1
    elif a[i] < a[i-1]:
        ans += a[i]
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
A = [int(input()) for _ in range(N)]
import sys
ans = 0
if A[0] != 0:
    print(-1)
    sys.exit()
for i in range(1,N):
    if A[i] - A[i-1] == 1:
        ans += 1
    elif A[i] <= A[i-1]:
        ans += A[i]
    else:
        print(-1)
        sys.exit()
    
print(ans)

Submission status: Accepted

Solution code:
import sys
n=int(input())
l=[0]*(n+1)
for i in range(n):
    l[i]=int(input())
    if l[i]>i:print(-1);sys.exit()
for i in range(n):
    if l[i]-l[i-1]>=2:print(-1);sys.exit()
ans=0
for i in range(n+1):
    if l[i]<=l[i-1]:
        ans+=l[i-1]
print(ans)

Submission status: Accepted

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

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

ans = 0
prev = 0

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

print(ans)

Submission status: Accepted

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

ans = 0
for i in range(1, N):
    if a[i - 1] + 1 < a[i]:
        print(-1)
        exit()
    elif a[i - 1] + 1 == a[i]:
        ans += 1
    else:
        ans += a[i]

print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

import numpy as np

N = int(input())
A = np.array([int(input()) for x in range(N)] + [0], dtype=np.int64)

x = (A[:-1][A[:-1] >= A[1:]]).sum()

if (A > np.arange(len(A))).any():
    x = -1
if (A[1:] >= A[:-1] + 2).any():
    x = -1

print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def input():
    return sys.stdin.readline()[:-1]
n=int(input())
a=[int(input()) for i in range(n)]
if a[0]!=0:
    print(-1)
    quit()
ans=0
for i in range(n-1):
    if a[i+1]-a[i]>1:
        print(-1)
        quit()
    elif a[i+1]-a[i]==1:
        ans+=1
    else:
        ans+=a[i+1]
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
N=int(input())
L=[]
for i in range(N):
    a=int(input())
    L.append(a)
#print(L)
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]
#print(L)
ans=0
for i in range(1,len(L)):
    if L[i]>=L[i-1]:
        ans+=L[i]
print(ans)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n=int(input())
a=[]
for i in range(n):
    a.append(int(input()))
#a=a[::-1]
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)

Submission status: Accepted

Solution code:
#!usr/bin/env python3

# A
"""
a,b,c,k = map(int ,input().split())
s = b-a
if (k%2 == 0):
    s *= -1
print(s)
"""
# B
# C
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)


# D

# E

# F

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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))
#print(As)

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)
print(result)

Submission status: Accepted

Solution code:
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
       #el
       if now + 1 == a:
           ans += 1
           now += 1
       elif now >= a:
           ans += a
           now = a
       else:
           ans = -1
           break
print(ans)

Submission status: Accepted

Solution code:
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
       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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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]:
        # X[i-1]→X[i]の、1回の操作で可能
        ans += 1
    else:
        # X[i-As[i]]から順に、As[i]回の操作が必要
        ans += As[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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

Submission status: Accepted

Solution code:
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
    #0 1 1 0 1 2 2 2 3
    wala = False
    for i in range(len(a) - 1, 0, -1):
        #print("-")
        if i <= s and a[i] == 0:
            #print("X")
            continue
        if i - a[i] < 0 or i - a[i] > s:
            #print("Y")
            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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

for i in range(N):
    #print(str(i-1)+"  ",end="")
    #print(ans)
    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:#A[i-1]<A[i]:
            ans=-1
            break
else:
    ans+=A[i]
print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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:
        # abort
        print(-1)
        exit()

# 終端処理
count += p[-1]
print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# 
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)