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

Submission status: Wrong Answer

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

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

frag = []
start = 1
next = 2
for i in range(2, N):
    if start!=-1:
        if A[i]==next:#(A[i]==0 or A[i]==1):
            next += 1
        else:
            end = i - 1
            #print(start,end,A[start:end+1])
            frag.append(A[start:end+1])
            if A[i]==0:
                start = -1
            else:
                start = i
                next = A[i] + 1
    elif start==-1 and A[i]!=0:
        if A[i]==1:
            start = i
        else:
            print(-1)
            exit()

#print(start,N-1,A[start:])
frag.append(A[start:])
#print(frag)

ans = 0
for f in frag:
    if f[0]==1:
        ans += len(f)
    else:
        ans += f[0]
print(ans)

Submission status: Wrong Answer

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

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

frag = []
start = 1
next = 2
for i in range(2, N):
    if start!=-1:
        if A[i]==next:#(A[i]==0 or A[i]==1):
            next += 1
        else:
            end = i - 1
            #print(start,end,A[start:end+1])
            frag.append(A[start:end+1])
            if A[i]==0:
                start = -1
            else:
                start = i
                next = A[i] + 1
    elif start==-1 and A[i]!=0:
        if A[i]==1:
            start = i
        else:
            print(-1)
            exit()

#print(start,N-1,A[start:])
frag.append(A[start:])
#print(frag)

ans = 0
for f in frag:
    if f[0]==1:
        ans += len(f)
    else:
        ans += f[0]
print(ans)

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 A[0]!=0:
    print(-1)
    exit()
if sum(A)==0:
    print(0)
    exit()

frag = []
start = 1
next = 2
for i in range(2, N):
    if start!=-1:
        if A[i]==next:#(A[i]==0 or A[i]==1):
            next += 1
        else:
            end = i - 1
            #print(start,end,A[start:end+1])
            frag.append(A[start:end+1])
            if A[i]==0:
                start = -1
            else:
                start = i
                next = A[i] + 1
    elif start==-1 and A[i]!=0:
        if A[i]==1:
            start = i
        else:
            print(-1)
            exit()

#print(start,N-1,A[start:])
frag.append(A[start:])
#print(frag)

ans = 0
for f in frag:
    if f[0]==1:
        ans += len(f)
    else:
        ans += f[0]
print(ans)

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 A[0]!=0:
    print(-1)
    exit()
if sum(A)==0:
    print(0)
    exit()

frag = []
start = 1
next = 2
for i in range(2, N):
    if start!=-1:
        if A[i]==next:#(A[i]==0 or A[i]==1):
            next += 1
        else:
            end = i - 1
            #print(start,end,A[start:end+1])
            frag.append(A[start:end+1])
            if A[i]==0:
                start = -1
            else:
                start = i
                next = A[i] + 1
    elif start==-1 and A[i]!=0:
        if A[i]==1:
            start = i
        else:
            print(-1)
            exit()

#print(start,N-1,A[start:])
frag.append(A[start:])
#print(frag)

ans = 0
for f in frag:
    if f[0]==1:
        ans += len(f)
    else:
        ans += f[0]
print(ans)

Submission status: Wrong Answer

Solution code:
# ~33 OK

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

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

frag = []
start = 1
next = 2
for i in range(2, N):
    if start!=-1:
        if A[i]==next:#(A[i]==0 or A[i]==1):
            next += 1
        else:
            end = i - 1
            #print(start,end,A[start:end+1])
            frag.append(A[start:end+1])
            if A[i]==0:
                start = -1
            else:
                start = i
                next = A[i] + 1
    elif start==-1 and A[i]!=0:
        if A[i]==1:
            start = i
        else:
            print(-1)
            exit()

#print(start,N-1,A[start:])
frag.append(A[start:])
#print(frag)

ans = 0
for f in frag:
    if f[0]==1:
        ans += len(f)
    else:
        ans += f[0]
print(ans)

Submission status: Wrong Answer

Solution code:
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<ai-1:
    print(-1)
    exit()
  a0=ai
  
if a0 !=0:
  print(-1)
else:
  print(ans)

Submission status: Wrong Answer

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

main()

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
x = [0 for i in range(n)]
a = []
for _ in range(n):
    a.append(int(input()))
if a[0] != 0:
    print(-1)
    sys.exit()
ans = 0
a.reverse()
for i in range(n):
    if a[i] == 1 or a[i] == 0:
        ans += a[i]
    elif a[i] == a[i+1]:
        ans += 2
    elif a[i] == a[i+1] + 1:
        ans += 1
    else:
        ans = -1
        break

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
n = int(input())
x = [0 for i in range(n)]
a = []
for _ in range(n):
    a.append(int(input()))
if a[0] != 0:
    print(-1)
    sys.exit()
ans = 0
a.reverse()
for i in range(n):
    if a[i] == 1 or a[i] == 0:
        ans += a[i]
    elif a[i] == a[i+1]:
        ans += a[i]
    elif a[i] == a[i+1] + 1:
        ans += 1
    else:
        ans = -1
        break

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

def solve(a, n):
    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

main()

Submission status: Wrong Answer

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 range(1, N):
    if As[i] > i:
        ans = -1
        break

    if As[i - 1] + 1 == As[i]:
        ans += 1
    else:
        if As[i] != 0 and As[i - 1] == 0:
            ans = -1
            break
        ans += As[i]

print(ans)

Submission status: Wrong Answer

Solution code:
find = True
n = int(input())

temp1 = int(input())
if (temp1 != 0):
    find = False

sc= 0
c = 0
for i in range(n-1) :
    temp2 = temp1
    temp1 = int(input())
    if (temp1 != temp2 + 1) :
        c += sc
        c += temp1
        sc = 0
    else :
        sc += 1

c += sc

if (find == False) :
    print(-1)
else :
    print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0]*N
u = 0
for i in range(N):
	A[i] = int(input())
if A[0] == 0:
	for i in range(1,N+1):
		u = u + A[N-i]
		b = A[N-i]
		for j in range(min(N-1,A[N-i])):
			if A[N-i-j]>=b-j:
				A[N-i-j]=A[N-i-j]-b+j
			else:
				break

	print(u)
else:
	print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [0]*N
u = 0
for i in range(N):
	A[i] = int(input())
if A[0] == 0:
	for i in range(1,N+1):
		u = u + A[N-i]
		b = A[N-i]
		for j in range(min(N-1,A[N-i])):
			if A[N-i-j]>=A[N-i]-j:
				A[N-i-j]=A[N-i-j]-b+j
			else:
				break

	print(u)
else:
	print(-1)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=[]
for i in range(n):
  x=int(input())
  a.append(x)
f=0
ans=[0]
for i in range(n):
  if(a[0]==1):
    f=1
  elif(a[i]>a[i-1]+1):
    f=1
c=0
for i in range(1,n):
  if(a[i]==1):
    c=c+1
  elif(a[i]>1):
    if(i<n-1):
      if(a[i+1]==a[i]):
        c=c+2
      else:
        c=c+1
    else:
      c=c+1
if(f==1):
  print(-1)
else:
  print(c)

Submission status: Wrong Answer

Solution code:
import sys;raw_input = sys.stdin.readline
N = int(raw_input())
A = [None]*N
C = [0]*N
for i in xrange(N):
    A[i] = int(raw_input())
b = False
for i in xrange(1, N):
    if A[i] - A[i-1] > 1 :
        b = True
        break
if b or A[0] != 0:
    print -1
else:
    B = [0]*N
    ans = 0
    for i in xrange(1, N):
        val = B[i-1] + 1
        B[i] = val if val <= A[i] else 0
        C[i] = A[i] - B[i]
    for i in xrange(N-1, 0, -1):
        if B[i-1] == B[i]-1 :
            C[i-1] = min(C[i], C[i-1])
    C[0] = 0
    for i in xrange(1, N):
        if B[i] == B[i-1] + 1:
            assert C[i] >= C[i-1]
            C[i] = C[i-1] if A[i-1] + 1 >= B[i] + C[i-1] else min(C[i], A[i-1]-B[i])
            ans += (A[i] - B[i-1] - C[i] if B[i+1] == B[i] + 1 or B[i+1] <= 1 else A[i]) if i < N-1 else A[i] - B[i-1] - C[i]
        else:
            ans += A[i]
    print ans

Submission status: Wrong Answer

Solution code:
N = int(input())

l = list(range(N))

for n in range(N):
    l[n] = int(input())

# print(l)

count = 0
flag = True
for i, h in enumerate(l):
    # print(h)
    if i == 0:
        if h != 0:
            print(-1)
            flag = False
            break


    else:
        if h == l[i-1] + 1:
            count += 1
        elif h == l[i-1]:
            count += h
        else:
            count += h

if flag:
    print(count)

Submission status: Wrong Answer

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 range(1, N):
    if As[i] > i:
        print(-1)
        exit()

    if As[i - 1] + 1 == As[i]:
        ans += 1
    else:
        if As[i] > 1:
            print(-1)
            exit()
        ans += As[i]

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

def main():
    n = int(input())
    a = [int(input()) for _ in range(n)]
    print(solve(a))

def solve(a):
    peaks = find_peaks(a)
    if check_peaks(a, peaks):
        return sum(a[p] for p in peaks)
    else:
        return -1

def check_peaks(a, peaks):
    b = -1

    for i in range(len(a)):
        if a[i] != b or a[i] != b + 1:
            j = bisect.bisect_left(peaks, i)
            if j >= len(peaks) or not check_peak(a, peaks[j]):
                return False
        b = a[i]
    return True


def check_peak(a, peak):
    if a[peak] > peak:
        return False
    
    base = peak - a[peak]
    for i in range(a[peak]):
        if a[base + i] < i:
            return False
    
    return True

def find_peaks(a):
    peaks = []

    for i in range(len(a) - 1):
        if a[i] != 0 and a[i + 1] <= a[i]:
            peaks.append(i)
    
    if a[len(a) - 1] != 0:
        peaks.append(len(a) - 1)
    
    return peaks

main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
print(-1)
exit()
if A == [0, 1]:
    print(1)
    exit()
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

A = A[::-1]
M = 0

for i in range(N-1):
    if A[i] >= A[i+1]:
        ans += M
        M = A[i]
        continue
    M = max(M, A[i])

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
la = [int(input()) for _ in range(n)]


if la[0] != 0:
    print(-1)
else:
    count = 0
    for n1, n2 in zip(la[:n-1], la[1:]):
        count += (n2 - n1) if (n2 - n1) > 0 else n2
    print(count)

Submission status: Wrong Answer

Solution code:
import sys;raw_input = sys.stdin.readline
N = int(raw_input())
A = [None]*N
C = [0]*N
for i in xrange(N):
    A[i] = int(raw_input())
b = False
for i in xrange(1, N):
    if A[i] - A[i-1] > 1 :
        b = True
        break
if b or A[0] != 0:
    print -1
else:
    B = [0]*N
    ans = 0
    for i in xrange(1, N):
        val = B[i-1] + 1
        B[i] = val if val <= A[i] else 0
        C[i] = A[i] - B[i]
    for i in xrange(N-1, 0, -1):
        if B[i-1] == B[i]-1 :
            C[i-1] = min(C[i], C[i-1])
    C[0] = 0
    for i in xrange(1, N):
        if B[i] == B[i-1] + 1:
            assert C[i] >= C[i-1]
            C[i] = C[i-1]
            ans += (A[i] - B[i-1] - C[i] if B[i+1] == B[i] + 1 or B[i+1] <= 1 else A[i]) if i < N-1 else A[i] - B[i-1] - C[i]
        else:
            ans += A[i]
    print ans

Submission status: Wrong Answer

Solution code:
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] <= 1 + p[i-1]:
      k += 1
    else:
      print(-1)
      m = 1
      break

if m != 1:
  print(k)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int(input()) for i in range(N)]
if A == [0, 1]:
    print(1)
    exit()
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

A = A[::-1]
M = 0

for i in range(N-1):
    if A[i] >= A[i+1]:
        ans += M
        M = A[i]
        continue
    M = max(M, A[i])

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

N=int(input())

pre=0
answer=0
for i in range(N):
    now=int(input())
    if i==0 and now!=0:
        answer=-1
        break
    if now>pre:
        pass
    elif now==pre:
        answer+=pre
    else:
        answer+=pre
    pre=now
    if i==N-1:
        answer+=now

print(answer)

Submission status: Wrong Answer

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 range(1, N):
    if As[i] > i:
        print(-1)
        exit()

    if As[i - 1] + 1 == As[i]:
        ans += 1
    else:
        ans += As[i]

print(ans)

Submission status: Wrong Answer

Solution code:
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
    elif i == n-2:
        sums += c
    t = c
print(sums)

Submission status: Wrong Answer

Solution code:
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 < 0 or num == 1:
                ans += 1
            elif num == 0:
                ans += i
            else:
                ans = -1
                break
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
a = [int(input()) for _ in range(n)]
ans = 0
if a[0] != 0:
    ans = -1
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)

Submission status: Wrong Answer

Solution code:
n = int(input())
nums = [int(input()) for _ in range(n)]
if nums[0] >= 1:
    print(-1)
    exit()
p = nums[0]
r = 0
ans = 0

for i in range(1, n):
    if nums[i] == p:
        r += 1
        if i == n - 1:
            ans += r * 2
    elif nums[i] > p + 1:
        print(-1)
        exit()
    elif nums[i] > 0:
        ans += r * 2
        if r > 0:
            ans -= 1
        r = 0
        p = nums[i]
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

line = input()

N = int(line)
A = np.array([0]*N)
foo = 0

for i in range(N):
    line = input()
    ai = int(line)
    A[i] = ai
    if ai > i:
        foo = 1
        break

if foo:
    print(-1)
    
else:
    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
            
    print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
nums = [int(input()) for _ in range(n)]
if nums[0] >= 1:
    print(-1)
    exit()
p = nums[0]
r = 0
ans = 0

for i in range(1, n):
    if nums[i] == p:
        r += 1
    elif nums[i] > p + 1:
        print(-1)
        exit()
    elif nums[i] > 0:
        ans += r * 2
        if r > 0:
            ans -= 1
        r = 0
        p = nums[i]
        ans += 1

print(ans)

Submission status: Wrong Answer

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

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

ans = 0
next = 10 ** 10
for A in reversed(As):
    if A != next - 1:
        ans += A
    next = A

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
nums = [int(input()) for _ in range(n)]
if nums[0] >= 1:
    print(-1)
    exit()
p = nums[0]
r = 0
ans = 0

for i in range(1, n):
    if nums[i] == p:
        r += 1
    elif nums[i] > p + 1:
        print(-1)
        exit()
    else:
        ans += r * 2
        if r > 0:
            ans -= 1
        r = 0
        p = nums[i]
        ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
import sys;raw_input = sys.stdin.readline
N = int(raw_input())
A = [None]*N
C = [0]*N
for i in xrange(N):
    A[i] = int(raw_input())
b = False
for i in xrange(1, N):
    if A[i] - A[i-1] > 1 :
        b = True
        break
if b or A[0] != 0:
    print -1
else:
    B = [0]*N
    ans = 0
    for i in xrange(1, N):
        val = B[i-1] + 1
        B[i] = val if val <= A[i] else min(1, A[i])
        C[i] = A[i] - B[i]
    for i in xrange(N-1, 0, -1):
        if B[i-1] == B[i]-1 :
            C[i-1] = min(C[i], C[i-1])
    C[0] = 0
    for i in xrange(1, N):
        if i > 1 and B[i] == B[i-1] + 1:
            C[i] = min(C[i-1], C[i])
        if B[i] == B[i-1] + 1:
            ans += A[i] - B[i-1] - C[i-1]
        else:
            ans += A[i]
    print ans

Submission status: Wrong Answer

Solution code:
#!/bin/python

import os
import sys

#
# Complete the gradingStudents function below.
#
def solution(arr):
    if (arr[0] != 0):
        return -1
    sum = 0
    for i in range(1,len(arr)):
        if (arr[i] > 1) and (arr[i-1] == 0):
            return -1
        if (arr[i-1] >= arr[i]):
            sum += 1
        elif (arr[i-1] < arr[i]):
            if (arr[i]-arr[i-1])==1:
                sum += arr[i]-arr[i-1]
            else:
                return -1
    return sum
            

if __name__ == '__main__':
    
    n = int(raw_input())
    arr = []
    for i in range(n):
        arr.append(int(raw_input()))

    result = solution(arr)
    print str(result)

Submission status: Wrong Answer

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

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

frag = []
start = 1
next = 2
for i in range(2, N):
    if start!=-1:
        if A[i]==next:#(A[i]==0 or A[i]==1):
            next += 1
        else:
            end = i - 1
            #print(start,end,A[start:end+1])
            frag.append(A[start:end+1])
            if A[i]==0:
                start = -1
            else:
                start = i
                next = A[i] + 1
    elif start==-1 and A[i]!=0:
        if A[i]==1:
            start = i
        else:
            print(-1)
            exit()

#print(start,N-1,A[start:])
frag.append(A[start:])
#print(frag)

ans = 0
for f in frag:
    if f[0]==1:
        ans += len(f)
    else:
        ans += f[0]
print(ans)

Submission status: Wrong Answer

Solution code:
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)
else:
    print(count + 1)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n = int(input())

before = int(input())
ans = 0
if(before == 0):
  for i in range(n-1):
    now = int(input())
    if(now - before >= 2):
      ans = -1
      break
    else:
      ans += 1
    before = now

else:
  ans = -1

print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
ans=0
for i in range(n):
  a=int(input())
  if a!=0:
    ans=ans+1
print(ans)

Submission status: Wrong Answer

Solution code:
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
    ans += a
    prev = a
print(ans)

Submission status: Wrong Answer

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

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

ans = 0

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

Submission status: Wrong Answer

Solution code:
#!/bin/python

import os
import sys

#
# Complete the gradingStudents function below.
#
def solution(arr):
    if arr[0] != 0:
        return -1
    sum = 0
    for i in range(1,len(arr)):
        if (i>1) and (arr[i] > 1) and (arr[i-1] == 0) and (arr[i-2] == 0):
            return -1
        if (arr[i-1] >= arr[i]) and (arr[i-1] != 0):
            sum += 1
        elif (arr[i-1] < arr[i]):
            sum += arr[i]-arr[i-1]
    return sum
            

if __name__ == '__main__':
    
    n = int(raw_input())
    arr = []
    for i in range(n):
        arr.append(int(raw_input()))

    result = solution(arr)
    print str(result)

Submission status: Wrong Answer

Solution code:
import sys;raw_input = sys.stdin.readline
N = int(raw_input())
A = [None]*N
for i in xrange(N):
    A[i] = int(raw_input())
b = False
for i in xrange(1, N):
    if A[i] - A[i-1] > 1 :
        b = True
        break
if b or A[0] != 0:
    print -1
else:
    B = [0]*N
    ans = 0
    for i in xrange(1, N):
        val = B[i-1] + 1
        B[i] = val if val <= A[i] else min(1, A[i])
    for i in xrange(1, N):
        if B[i] > B[i-1]: ans += A[i] - B[i-1]
        else: ans += A[i]
    print ans

Submission status: Wrong Answer

Solution code:
N = int(input())

start = int(input())
t = 0

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

print(t)

Submission status: Wrong Answer

Solution code:
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]:
        ans += L[i+1]
    
if isok:
    print(ans)
else:
    print(-1)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
#A = list(map(int, list(S)))

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

frag = []
start = 1
next = 2
for i in range(2, N):
    if start!=-1:
        if A[i]==next:#(A[i]==0 or A[i]==1):
            next += 1
        else:
            end = i - 1
            #print(start,end,A[start:end+1])
            frag.append(A[start:end+1])
            if A[i]==0:
                start = -1
            else:
                start = i
                next = A[i] + 1
    elif start==-1 and A[i]!=0:
        if A[i]==1:
            start = i
        else:
            print(-1)
            exit()

#print(start,N-1,A[start:])
frag.append(A[start:])
#print(frag)

ans = 0
for f in frag:
    if f[0]==1:
        ans += len(f)
    else:
        ans += f[0]
print(ans)

Submission status: Wrong Answer

Solution code:
#!python
# -*- coding: utf-8 -*-


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

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

    count = 0

    for i in range(N - 1, -1, -1):
        if A[i] > i:
            print(-1)
            return
        if i != N - 1:
            X[i] = max(0, X[i + 1] - 1)
        if A[i] == X[i]:
            continue

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

    print(count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
lis = []
for i in range(n):lis.append(int(input()))
if lis[0] != 0:
    print("-1")
    exit()
cou = 0
for k in range(n-1):
    if lis[n-(k+1)] > lis[n-(k+2)]:
        cou += math.ceil(lis[n-(k+1)] / (lis[n-(k+2)]+1))
        lis[n-(k+1)] = 0
    else:
        cou += lis[n-(k+1)]
        lis[n-(k+2)] -= lis[n-(k+1)]-1
        lis[n-(k+1)] = 0
print(cou)

Submission status: Wrong Answer

Solution code:
#!python
# -*- coding: utf-8 -*-


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

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

    count = 0

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

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

    print(count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/bin/python

import os
import sys

#
# Complete the gradingStudents function below.
#
def solution(arr):
    #
    # Write your code here.
    #
    if arr[0] != 0:
        return -1
    sum = 0
    for i in range(1,len(arr)):
        if (arr[i-1] >= arr[i]) or (arr[i-1] == 0):
            sum += arr[i]
        else:
            sum += arr[i]-arr[i-1]
    return sum
            

if __name__ == '__main__':
    
    n = int(raw_input())
    arr = []
    for i in range(n):
        arr.append(int(raw_input()))

    result = solution(arr)
    print str(result)

Submission status: Wrong Answer

Solution code:
import math
n = int(input())
lis = []
for i in range(n):lis.append(int(input()))
if lis[0] != 0:
    print("-1")
    exit()
cou = 0
for k in range(n-1):
    if lis[n-(k+1)] > lis[n-(k+2)]:
        cou += math.ceil(lis[n-(k+1)] / (lis[n-(k+2)]+1))
        lis[n-(k+1)] = 0
    else:
        cou += 1
        lis[n-(k+2)] -= lis[n-(k+1)]-1
        lis[n-(k+1)] = 0
print(cou)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = {}
for i in range(N):
	A[i] = int(input())
if A[0] == 0:
	u = A[N-1]
	for i in range(1,N):
		if A[N-i-1]>=A[N-i]:
			u = u + A[N-1-i]
	print(u)
else:
	print(-1)