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: Wrong Answer

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

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

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


print(ans)

Submission status: Wrong Answer

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

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

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


print(ans)

Submission status: Wrong Answer

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

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

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

        else:
            print(-1)
            exit()
    elif 1 <= i <= N-2:
        if A[i] - A[i-1] > 1:
            print(-1)
            exit()
        elif A[i] == A[i+1]:
            ans += A[i]
        elif A[i] == A[i+1] - 1:
            continue
        elif A[i+1] == 0:
            ans += A[i]
    else:
        if A[i] - A[i+1] > 1:
            print(-1)
            exit()

print(ans)

Submission status: Wrong Answer

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

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

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

        else:
            print(-1)
            exit()
    elif 1 <= i <= N-2:
        if A[i] - A[i-1] > 1:
            print(-1)
            exit()
        elif A[i] == A[i+1]:
            ans += A[i]
        elif A[i] == A[i+1] - 1:
            continue
        elif A[i] > A[i+1]:
            ans += A[i]
    else:
        if A[i] - A[i+1] > 1:
            print(-1)
            exit()

print(ans)

Submission status: Wrong Answer

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

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

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

        else:
            print(-1)
            exit()
    elif 1 <= i <= N-2:
        if A[i] - A[i-1] > 1:
            print(-1)
            exit()
        elif A[i] == A[i+1]:
            ans += A[i]
        elif A[i] == A[i+1] - 1:
            continue
        elif A[i] > A[i+1]:
            ans += A[i]
    print("aaa", ans)

print(ans)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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] <= 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: Wrong Answer

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] == 1:
        ans += 1
print(ans)

Submission status: Wrong Answer

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 abs(a[i] - a[i-1]) >= 2:
        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: Wrong Answer

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 abs(a[i] - a[i-1]) >= 2:
        print(-1)
        quit()
ans = 0
for i in range(1,n):
    if a[i] == a[i-1]:
        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: Wrong Answer

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 abs(a[i] - a[i-1]) >= 2:
        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] and a[i] != 0:
        ans += a[i]
print(ans)

Submission status: Wrong Answer

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 abs(a[i] - a[i-1]) >= 2:
        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] != 0:
        ans += a[i]
print(ans)

Submission status: Wrong Answer

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 abs(a[i] - a[i-1]) >= 2:
        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] == 1:
        ans += 1
print(ans)

Submission status: Wrong Answer

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-1):
    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: Wrong Answer

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

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

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

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
ans = 0
if A[0]:
    print(-1)
    exit()
for i in range(1, N):
    if A[i - 1] + 1 == A[i]:
        ans += 1
    else:
        ans += A[i]
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for _ in range(N)]
ans = 0
if A[0]:
    print(-1)
    exit()
for i in range(1, N):
    if A[i - 1] < A[i]:
        ans += A[i] - A[i - 1]
    else:
        ans += A[i]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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]:
                ans += a[i+1] - 1
            
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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]>=1:
      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: Wrong Answer

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 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: Wrong Answer

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=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)

Submission status: Wrong Answer

Solution code:
N=int(input())
*A,=[int(input()) for _ in [0]*N]
if A[0]!=0:print(-1);exit()
for i in range(1,N):
    if A[i]-A[i-1]>1:print(-1);exit()
ans = 0
tmp = 0
for a in A[::-1]:
    ans += max(0,a - tmp)
    if a>=tmp and tmp!= 0:tmp = tmp-1
    else:tmp = a-1
    tmp = max(0,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
*A,=[int(input()) for _ in [0]*N]
if A[0]!=0:print(-1);exit()
ans = 0
tmp = 0
for a in A[::-1]:
    ans += max(0,a - tmp)
    if a>=tmp and tmp!= 0:tmp = tmp-1
    else:tmp = a-1
    tmp = max(0,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
*A,=[int(input()) for _ in [0]*N]
if A[0]!=0:print(-1);exit()
ans = 0
tmp = 0
for a in A[::-1]:
    ans += max(0,a - tmp)
    tmp = max(0,a-tmp-1)
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
*A,=[int(input()) for _ in [0]*N]
if A[0]!=0:print(-1);exit()
B = A + [10**10]
ans = 0
for i in range(1,N):
    tmp = B[i]
    tmp2 = B[i]-B[i-1]
    if tmp2>0:tmp = min(tmp,tmp2)
    tmp2 = B[i]-(B[i+1]-1)
    if tmp2>0:tmp = min(tmp,tmp2)
    ans += tmp
print(ans)

Submission status: Wrong Answer

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:
    res += i//t + i%t
    t = i+1
if l[0] ==0 :print(res)
else:print(-1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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:
        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: Wrong Answer

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
for i in range(n):
    x=a.pop()
    if x==0:
        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
print(ans)

Submission status: Wrong Answer

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
for i in range(n):
    x=a.pop()
    if x==0:
        continue
    elif x>pos-1:
        ans=-1
        break
    else:
        if x==pre-1:
            pre=x
            continue
        else:
            pre=x
            ans+=x
    pos-=1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [-1]
b = -1
append = A.append
for i in range(N):
    a = int(input())
    if b + 1 < a:
        print(-1)
        exit()
    append(a)
    b = a
A.append(0)
ans = 0
for i in range(1,N+1):
    if A[i-1] != 0:
        if A[i-1] == A[i]-1:
            ans += 1
        elif A[i] == 0 or A[i] == 1:
            ans += 1
        else:
            ans += 2

print(ans)

Submission status: Wrong Answer

Solution code:
def makelist2(n, m):
    return [[0 for k in range(m)] for i in range(n)]


N = int(input())
A = [int(input())]
append = A.append
for i in range(1,N):
    a = int(input())
    if a > i:
        print(-1)
        exit()
    append(a)
    if A[i-1] + 1 < A[i]:
        print(-1)
        exit()

Submission status: Wrong Answer

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)
        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)

Submission status: Wrong Answer

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)
        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)

Submission status: Wrong Answer

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

counter=0#answerになる。
counter_n=l[0]#listの中の重複を数える。
l = l[::-1]
for i in range(N-1):
    if l[i]<l[i+1]:
        counter+=counter_n
        counter_n=0#初期化
    elif l[i]==l[i+1]:
        if counter_n == 0:
            counter_n = 2*l[i]
        else:
            counter_n += l[i]
        counter_n += l[i]
    elif l[i] == l[i+1]+1:
        counter += 1
counter += counter_n
print(counter)

Submission status: Wrong Answer

Solution code:
#x=[0,0,0,0]を「前の数値に1足したものを入れる」操作によりaと同じ値にする
n=int(input())
a=[]
for i in range(n):
  a.append(int(input()))
#a=[0,1,1,2]
x=[0]*n

def cal(a,x):
  if a[0]!=0:
    return -1
  write=0
  for i in range(len(a)):
    if a[i]!=0:
      if a[i]==1 or a[i-1]==a[i]-1:
        write+=1
      elif a[i-1]>a[i]-1 and a[i]>1:
        write+=2
      else: return -1
  
  return write

print(cal(a,x))

Submission status: Wrong Answer

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)

Submission status: Wrong Answer

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

def calc():
    count = 0
    value = 0
    for i in range(n - 1, -1, -1):
        if i == 0 and value > 0:
            return -1
        d = a[i] - value
        if d < 0:
            return -1
        if d > 0:
            count += a[i]
        value = max(0, a[i] - 1)
    return count
print(calc())

Submission status: Wrong Answer

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

def calc():
    count = 0
    value = 0
    for i in range(n - 1, -1, -1):
        if i == 0 and value > 0:
            return -1
        d = a[i] - value
        if d < 0:
            return -1
        count += d
        value = max(0, a[i] - 1)
    return count
print(calc())

Submission status: Wrong Answer

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
    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: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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] > 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: Wrong Answer

Solution code:
import numpy as np
import copy

N=int(input())

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

A=np.array(A)

if(A[0]!=0):
	print(-1)
else:
	num=0
	for i in range(N-1):
		if(np.abs(A[i+1]-A[i])>1):
			num=1
	if num==1:
		print(-1)
	else:
		num1=0
		index2=np.where(A!=0)
		num2=len(index2[0])
		AA=A
		for i in range(np.max(A)):
			AA=np.delete(A,0)-1
			index1=np.where(AA<0)
			AA[index1]=0
			A=np.delete(A,-1)
			B=A-AA
			index=np.where(AA==0)
			B=np.delete(B,index)
			num1=num1+np.sum(B)
			A=AA
		print(num1+num2)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))
if a[0] != 0:
    print("-1")
else:
    v = []
    for i in range(1, len(a)):
        v.append((a[i], -i))
    v.sort()
    x = [0] * len(a)
    wala = False
    for y in v:
        #print(y)
        #print(x)
        if x[-y[1]] == 0:
            continue
        elif x[-y[1] - 1] + 1 == y[0]:
            x[-y[1]] = y[0]
        else:
            wala = True
            break
    if wala:
        print("-1")
    else:
        print(len(a) - a.count(0) + 1)

Submission status: Wrong Answer

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]
		elif i == n - 1:
			ans += a[i]

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

Submission status: Wrong Answer

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 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: Wrong Answer

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 i == 0:
		continue
	if A[i] == A[i - 1] + 1:
		ans += 1
		continue
	if A[i] == 0:
		start = i
	ans += A[i]
print ans

Submission status: Wrong Answer

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 i == 0:
		start = i
		continue
	if A[i] == A[i - 1] + 1:
		ans += 1
		continue
	ans += A[i]
print ans

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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:
            ans += 1
        elif diff == 0:
            ans += A[i]
        elif diff > 1:
            return -1
        else:
            if A[i] <= 1:
                ans += A[i]
            else:
                return -1

    return ans

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

Submission status: Wrong Answer

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: Wrong Answer

Solution code:
N = int(input())
ans = 0
prior = 0
for i in range(N):
  a = int(input())
  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: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())

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



count = 0
climing = 0
for index in range(len(p) - 1):
    current_num = p[index]
    next_num = p[index + 1]
    
    if current_num > index:
        # abort
        print(-1)
        exit()

    

    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: Wrong Answer

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

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

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

Submission status: Wrong Answer

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-2):
    if A[i]>=A[i+1]:
        num += A[i]
num += A[-1]
print(num)

Submission status: Wrong Answer

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

if sum(A)==0:
    print(0)
    exit()

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

print(ans)

Submission status: Wrong Answer

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 abs(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())