TnT/Multi_CodeNet4Repair huggingface dataset:
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n = int(input())
p = []
for _ in range(n):
    p.append(int(input()))
mx = 0
tmp = 1
for i in range(n-1):
    if p[i]+1==p[i+1]:
        tmp += 1
    else:
        mx = max(tmp, mx)
        tmp = 1
print(n-mx)

n = int(input())
p = []
for _ in range(n):
    p.append(int(input()))
li = [0]*(n+1)
for i in p:
    li[i] = li[i-1]+1
print(n-max(li))

Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n = int(input())
ppp = list(map(int, (input() for _ in range(n))))

tmp = [0] * n
for p in ppp:
    tmp[p] = tmp[p - 1] + 1

print(n - max(tmp))


n = int(input())
ppp = list(map(int, (input() for _ in range(n))))

tmp = [0] * (n + 1)
for p in ppp:
    tmp[p] = tmp[p - 1] + 1

print(n - max(tmp))


Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
N=int(input())

q=[-1]*N
for i in range(N):
    k = int(input())
    print(k)
    q[k-1]=i


max_con=1
count=0
for i in range(N-1):
    if q[i]<q[i+1]:
        count+=1
        max_con=max(max_con,count)
    else:
        count=1

print(N-max_con)

N=int(input())

q=[-1]*N
for i in range(N):
    k = int(input())
    #print(k)
    q[k-1]=i


max_con=1
count=1
for i in range(N-1):
    if q[i]<q[i+1]:
        count+=1
        max_con=max(max_con,count)
    else:
        count=1

print(N-max_con)

Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
N = int(input())
P = [int(input()) for _ in range(N)]
count = 1
flag = ""
for i in range(1,N):
    if flag == "":
        if P[i-1] < P[i]:
            flag = "<"
        else:
            flag = ">"
    else:
        if (P[i-1] < P[i] and flag == "<") or (P[i-1] > P[i] and flag == ">"):
            continue
        else:
            count += 1
            if P[i-1] < P[i]:
                flag = "<"
            else:
                flag = ">"
print(count)

N = int(input())
P = []
indexls = [0]*N
for i in range(N):
    tmp = int(input())
    P.append(tmp)
    indexls[tmp-1] = i
maxlen = 1
tmplen = 1
for i in range(1,N):
    if indexls[i] > indexls[i-1]:
        tmplen += 1
        maxlen = max(maxlen,tmplen)
    else:
        tmplen = 1
print(N-maxlen)

Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))

dp = [0]*(N+1)

for a in A:
    dp[a] += dp[a-1]+1
print(N-max(dp))


# python template for atcoder1
import sys
sys.setrecursionlimit(10**9)
input = sys.stdin.readline

N = int(input())
A = [int(input()) for _ in range(N)]
dp = [0]*(N+1)

for a in A:
    dp[a] += dp[a-1]+1
print(N-max(dp))


Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
N=int(input())
P=[int(input()) for _ in range(N)]
Q=[0 for _ in range(N)]

if N=1:
    print(1)
else:
    for i in range(N):
        Q[P[i]-1]=i

    cnt=0
    tmp=0
    for i in range(N-1):
        if Q[i]<Q[i+1]:
            cnt+=1
        else:
            if tmp<cnt+1:
                tmp=cnt+1
            cnt=0

    print(N-tmp)


N=int(input())
P=[int(input()) for _ in range(N)]
Q=[0 for _ in range(N)]

for i in range(N):
    Q[P[i]-1]=i

cnt=0
tmp=0
for i in range(N-1):
    if Q[i]<Q[i+1]:
        cnt+=1
    else:
        if tmp<cnt+1:
            tmp=cnt+1
        cnt=0

if tmp==0:
    tmp=cnt+1

print(N-tmp)


Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
N = int(input())
dp = [None for _ in range(N)]
for i in range(N):
  p = int(input())
  if (p != 1) and (dp[p - 2] == None):
    dp[p - 1] = 1
  else:
    dp[p - 1] = dp[p - 2] + 1
print(N - max(dp))

N = int(input())
dp = [None for _ in range(N)]
for i in range(N):
  p = int(input())
  if p != 1 and (dp[p - 2]) == None:
    dp[p - 1] = 1
  else:
    dp[p - 1] = dp[p - 2] + 1
print(N - max(dp))

N = int(input())
dp = [None for _ in range(N)]
for i in range(N):
  p = int(input())
  if 0 <= p - 2 and dp[p - 2] == None:
    dp[p - 1] = 1
  else:
    dp[p - 1] = dp[p - 2] + 1
print(N - max(dp))

N = int(input())
dp = [None for _ in range(N)]
for i in range(N):
  p = int(input())
  if dp[p - 2] == None or p == 1:
    dp[p - 1] = 1
  else:
    dp[p - 1] = dp[p - 2] + 1
print(N - max(dp))

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
#N, *a = map(int, open(0).read().split())
def main():
    N = int(input())
    a=[]
    for i in range(N):
        a.append(int(input()))

    b = [0 for _ in range(N+1)]
    cnt = 1
    ans = 0
    flag = 1
    i = 0
    for i in reversed(range(N)):
        if b[a[i]+1] > 0:
            b[a[i]] = b[a[i]+1] + 1
        else:
            b[a[i]] = 1
        
    return N-max(b)
print(main())


#N, *a = map(int, open(0).read().split())
def main():
    N = int(input())
    a=[]
    for i in range(N):
        a.append(int(input()))

    b = [0 for _ in range(N+2)]
    cnt = 1
    ans = 0
    flag = 1
    i = 0
    for i in reversed(range(N)):
        if b[a[i]+1] > 0:
            b[a[i]] = b[a[i]+1] + 1
        else:
            b[a[i]] = 1
        
    return N-max(b)
print(main())


Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
N = int(input())
L = [int(input()) for i in range(N)]

n = 1
for i in L:
    if i == n:
        n += 1

m = N
for i in reversed(L):
    if int(i) == m:
        m -= 1

n = N - n - 1

print(min(n, m))

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


P = [0] * (N + 1)

for p in L:
    P[p] = P[p-1] + 1


print(N-max(P))



Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n=int(input())
p=[int(input()) for _ in range(n)]
dp=[0]*(n+10)
for i in p:
  dp[p]=dp[p-1]+1
print(n-max(dp))


n=int(input())
p=[int(input()) for _ in range(n)]
dp=[0]*(n+10)
for i in p:
  dp[i]=dp[i-1]+1
print(n-max(dp))

Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n=int(input())
p=[]
for i in range (n):
	p.append(int(input()))
pos=0
num=0
n_th = 0
for i in range(n)
	if i+1 in p[pos:]:
		num += 1
		pos = p[pos:].index(i+1)+pos
	else:
		if num>n_th:
			n_th = num
		if i+1 in p:
			num = 1
			pos = p.index(i+1)
if num >n_th:
	print (n-num)
else:
	print(n-n_th)

n=int(input())
p=[]
for i in range (n):
	p.append(int(input()))
pos=0
num=0
n_th = 0
s={}
for i in p:
	if i-1 in s:
		s[i]=s[i-1]+1
	else:
		s[i]=1
print(n-max(s.values()))

Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
def main():
    N=int(input())
    P=[0]*N
    p=[False]*(N+1)
    P[0]=int(input())
    for i in range(N-1):
        P[i+1=int(input())
        if P[-1]-1 in P[:-1]:
            p[P[-1]-1] = True
    ans=0
    j=0
    for i in range(N):
        if p[i]==True:
            j+=1
        else:
            ans=max(ans,j)
            j=0
    print(N-ans-1)


if __name__ == "__main__":
    main()


def main():
    N=int(input())
    p=[False]*(N+1)
    c=[False]*(N+1)
    for i in range(N):
        x=int(input())
        c[x]=True
        if c[x-1]:
            p[x-1] = True
    ans=0
    j=0
    for i in range(1,N+1):
        if p[i]:
            j+=1
        else:
            ans=max(ans,j)
            j=0
    ans=max(ans,j)
    print(N-ans-1)

if __name__ == "__main__":
    main()


Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n = int(input())
p = [int(input()) for _ in range(n)]

cnt = 0
first = p[0]
last = p[-1]
for i in range(n):
    if first > p[i]:
        cnt += 1
        first = p[i]
for i in range(n):
    if last < p[i]:
        cnt += 1
        last = p[i]

if p[0] == last:
    cnt += n - p[0] - 1
print(cnt)


n = int(input())
p = [int(input()) for _ in range(n)]
q = [0] * (n+1)

for i in range(n):
    q[p[i]] = i
maxl = 0
cnt = 0
for i in range(n):
    if q[i+1] > q[i]:
        cnt += 1
    else:
        maxl = max(maxl, cnt)
print(n - maxl)


n = int(input())
p = [int(input()) for _ in range(n)]
q = [0] * (n+1)

for i in range(n):
    q[p[i]] = i
maxl = 1
cnt = 1
for i in range(1, n):
    if q[i+1] > q[i]:
        cnt += 1
    else:
        maxl = max(maxl, cnt)
        cnt = 1
print(n - max(maxl, cnt))


Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
import math
n = int(input())
lis = [int(input()) for i in range(n)]
if lis[0] != 0:print(-1);exit()
ans = 0
num = 0
for i in range(n-1,0,-1):
    if lis[i] > 0:
        ans += lis[i]
        num += lis[i]-1
        lis[i-1] = max(0,lis[i-1]-num)
        lis[i] = 0
        num = max(0,num-1)
  #     print(lis,ans)
print(ans)

n = int(input())
lis = [int(input()) for i in range(n)]
li = [-1 for i in range(n)]
for i in range(n):
    li[lis[i]-1] = i
cnt = 0
ans = n-1
for i in range(n-1):
    if li[i] < li[i+1]:
        cnt += 1
    else:
        ans = min(ans,n-1-cnt)
        cnt = 0
ans = min(ans,n-1-cnt)
print(ans)

Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
N = int(input())
plist = []
for i in range(N):
    x = int(input())
    plist.append(x)
right = plist[-1]
print(right)
cnt = 0
cur = right
ind = -1000
for i in reversed(range(N-1)):
    if(plist[i] > right):
        cnt += 1
    elif(cur - plist[i] > 1):
        ind = i
        break
    else:
        cur = plist[i]

print(ind+cnt)

import random
print(random.randint(1,10**6))

d = {}
N = int(input())
p = [int(input()) for i in range(N)]

for i in range(N):
    if((p[i]-1) in d):
        d[p[i]] = d[p[i]-1] + 1
    else:
        d[p[i]] = 1

print(N-max(d.values()))

Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
from itertools import count
from itertools import chain

def gen_check_numbers(n):
    center = n // 2
    l2 = chain.from_iterable(zip(reversed(range(1, center + 1)), range(center + 1 , n + 1)))
    return l2

def main():
    n = int(input())
    l = [ int(input().rstrip()) for i in range(n)]

    ans = 0
    center = n // 2
    for i in gen_check_numbers(n):
        print(i)
        i1 = l.index(i)
        if i <= center:
            i2 = l.index(i + 1)
            if( i2 < i1):
                l.remove(i)
                l.insert(0,i)
                ans += 1
        else:
            i2 = l.index(i - 1)
            if( i1 < i2):
                l.remove(i)
                l.append(i)
                ans += 1
    print(ans)

if __name__ == '__main__':
    main()


from itertools import count
from itertools import chain

def gen_check_numbers(n):
    center = n // 2
    l2 = chain.from_iterable(zip(reversed(range(1, center + 1)), range(center + 1 , n + 1)))
    return l2

def main():
    n = int(input())
    l = [ int(input().rstrip()) for i in range(n)]

    ans = 0
    center = n // 2
    for i in gen_check_numbers(n):
        print(i)
        i1 = l.index(i)
        if i <= center:
            i2 = l.index(i + 1)
            if( i2 < i1):
                l.remove(i)
                l.insert(0,i)
                ans += 1
        else:
            i2 = l.index(i - 1)
            if( i1 < i2):
                l.remove(i)
                l.append(i)
                ans += 1
    print(ans, l)

if __name__ == '__main__':
    main()


n = int(input())
l = [ int(input().rstrip()) for i in range(n)]

s = {}
for i in l:
    if i - 1 in s:
        s[i] = s[i - 1] + 1
        del s[i - 1]
    else:
            s[i] = 1
print(n - max(s.values()))


Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
import sys
input = sys.stdin.readline
n = int(input())
memo = [0 for i in range(n)]
for i in range(n):
  memo[int(input())-1] = i
cand = 0
tmp = 0
for n1, n2 in range(n - 1):
  if n2 > n1:
    tmp += 1
    if tmp > cand:
      cand = tmp
  else:
    tmp = 0
print(n - cand - 1)

import sys
input = sys.stdin.readline
n = int(input())
memo = [0 for i in range(n)]
for i in range(n):
  memo[int(input())-1] = i
cand = 0
tmp = 0
for n1, n2 in zip(memo[:], memo[1:]):
  if n2 > n1:
    tmp += 1
    if tmp > cand:
      cand = tmp
  else:
    tmp = 0
print(n - cand - 1)


Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
import numpy as np



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




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



N = ii()

P = lvi(N)

'''
P = []
for _ in range(N):
    p = ii()
    P.append(p -1)

print(P)
'''

#fill( Q, Q + N, 0)
Q = [0 for i in range(N)]


#print( 'Q', Q)

for i in range(1, N):
    Q[P[i - 1]] = i


maxcnt = 1
cnt = 1

print( 'Q', Q)

for i in range(1, N):
    if Q[i] > Q[i - 1]:
        cnt += 1
    else:
        cnt = 1

    maxcnt = max( maxcnt, cnt)


print(N - maxcnt)


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


N = ii()
P = []
for _ in range(N):
    p = ii()
    P.append(p-1)

Q = [0 for i in range(N)]
for i in range(N):
    Q[P[i]] = i

maxcnt = 1
cnt = 1
for i in range(1, N):
    if Q[i] > Q[i-1]:
        cnt += 1
    else:
        cnt = 1

    maxcnt = max(maxcnt, cnt)

print(N - maxcnt)


Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
N = int(input())
src = [int(input()) for i in range(N)]

seq_f = 1
for a in src:
    if a == seq_f:
        seq_f += 1
seq_f -= 1

seq_b = N
for a in reversed(src):
    if a == seq_b:
        seq_b -= 1
seq_b += 1

ans = min(seq_b, N-seq_f-1)
print(ans)


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

D = {p:i for i,p in enumerate(P)}
pv = N
l = seq = 0
for n in range(1,N+1):
    nx = D[n]
    if pv < nx:
        seq += 1
    else:
        l = max(l,seq)
        seq = 1
    pv = nx
l = max(l, seq)

print(N - l)

Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n=int(input())
a=[int(input())for _ in[0]*n]
if n%2==0:
    m1=n//2
    m2=m1+1
    m1i=a.index(m1)
    m2i=a.index(m2)
    a1=[a[i] for i in range(m1i) if a[i]<m1]
    a2=[a[i] for i in range(m1i+1,n) if a[i]<m1]
    al=sorted(a2)+a1
    a3=[a[i] for i in range(m2i) if a[i]>m2]
    a4=[a[i] for i in range(m2i+1,n) if a[i]>m2]
    ar=a4+sorted(a3)
    r=len(a2)+len(a3)
    l=al.index(m1-1)
    r+=m1-2-l
    r+=ar.index(m2+1)
else:
    m=n//2+1
    mi=a.index(m)
    a1=[a[i] for i in range(mi) if a[i]<m]
    a2=[a[i] for i in range(mi+1,n) if a[i]<m]
    al=sorted(a2)+a1
    a3=[a[i] for i in range(mi) if a[i]>m]
    a4=[a[i] for i in range(mi+1,n) if a[i]>m]
    ar=a4+sorted(a3)
    r=len(a2)+len(a3)
    l=al.index(m-1)
    r+=m-2-l
    r+=ar.index(m+1)
print(r)

n=int(input())
p=[int(input())-1for _ in[0]*n]
q=[0]*n
for i in range(n):q[p[i]]=i
r,c=0,1
for i in range(n-1):
    c=c+1if q[i]<q[i+1]else 1
    if r<c:r=c
if r<c:r=c
print(n-r)

Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n = int(input())
p = [int(input()) for _ in range(n)]

q = [0] * (n+1)
for i in range(n):
    q[p[i]] = q[p[i]-1] + 1

print(q - max(q))


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

q = [0] * (n+1)
for i in range(n):
    q[p[i]] = q[p[i]-1] + 1

print(n - max(q))


Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n = int(input())

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

dic = {p[0]:1}
for i in range(1,n):
    print(dic)
    check = 0
    for k in list(dic):
        
        if p[i] == k + dic[k]:
            dic[k] += 1
            check = 1
            break
        elif p[i] == k - 1:
            dic[p[i]] = 1
            if dic[k] < max(dic.values()) or dic[k] == 1:
                dic.pop(k)
        else:
            dic[p[i]] = 1
            
print(n - max(dic.values()))

n = int(input())

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


q = [0 for i in range(n)]
for i in range(n):
    q[p[i] - 1] = i

result = 1
temp = 1
for i in range(1, n):
    if q[i] < q[i-1]:
        if temp > result:
            result = temp
        temp = 1
    else:
        temp += 1

if temp == n:
    result = temp

print(n - result)

Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
N = int(input())
P = [int(input()) for _ in range(N)]
vi = {v: i for i, v in enumerate(P)}
before = float('inf')
for v in range(N, -1, -1):
    if v == 0 or vi[v] > before:
        break
    else:
        before = vi[v]
before = -1
for v2 in range(1, N + 2):
    if v2 == N + 1 or vi[v2] < before:
        break
    else:
        before = vi[v2]
print(min(v, N - v2))


N = int(input())
P = [int(input()) for _ in range(N)]
vi = {v: i for i, v in enumerate(P)}
incmax = inc = 1
before = vi[1]
for v in range(2, N + 1):
    if vi[v] > before:
        inc += 1
    else:
        inc = 1
    incmax = max(incmax, inc)
    before = vi[v]
print(N - incmax)

Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n=int(input())
a=[int(input()) for i in range(n)]

al= {}
ds=[]
ans=0
for ai in a:
  ds=
  if ai - 1 in al.keys():
    al[ai]=al[ai-1]+1
    ans=max(ans,al[ai-1]+1)   
    del al[ai-1]
  else:
    al[ai]=0
    if ai + 1 in al.keys():
      del al[ai]
print(n-ans-1)

n=int(input())
a=[int(input()) for i in range(n)]
 
al= {}
ans=0
for ai in a:
  if ai - 1 in al.keys():
    al[ai]=al[ai-1]+1
    ans=max(ans,al[ai-1]+1)   
    del al[ai-1]
  else:
    al[ai]=0

print(n-ans-1)

Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
def main():
    N = int(input())
    P = [int(input()) for _ in range(N)]

    #f1(N, P)
    editorial(N, P)


def editorial(N, P):
    if N == 1:
        print(0)
        return

    tmp = 0
    ans = 0
    for i, p in enumerate(P):
        if i == 0 or P[i-1] + 1 == p:
            tmp += 1
        else:
            ans = max(ans, tmp)
            tmp = 1

    print(N - ans)


if __name__ == '__main__':
    main()


def main():
    N = int(input())
    P = [int(input()) for _ in range(N)]

    #f1(N, P)
    editorial(N, P)


def editorial(N, P):
    
    if N == 1:
        print(0)
        return

    # WA(N, P)
    a = [0] * (N+1)
    for i, p in enumerate(P):
        a[p] = i

    tmp = 1
    max_len = 1
    for i in range(1, N):
        if a[i] < a[i+1]:
            tmp += 1
            max_len = max(max_len, tmp)
        else:
            tmp = 1

    ans = N - max_len
    print(ans)


def WA(N, P):
    tmp = 0
    ans = 0
    for i, p in enumerate(P):
        if i == 0 or P[i-1] + 1 == p:
            tmp += 1
        else:
            ans = max(ans, tmp)
            tmp = 1

    print(N - ans)


if __name__ == '__main__':
    main()


Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
# -*- coding: utf-8 -*-

N = int(input())
p = [int(input()) for i in range(N)]
q = [0] * N
for i in range(N):
    q[p[i]-1] = i

cnt = 1
for i in range(N-1):
    if q[i+1] - q[i] > 0:
        cnt += 1
    else:
        max_cnt = max(max_cnt, cnt)
        cnt = 1
print(N - max_cnt)

# -*- coding: utf-8 -*-

N = int(input())
p = [int(input()) for i in range(N)]
q = [0 for i in range(N)]
for i in range(N):
    q[p[i]-1] = i

cnt = 1
max_cnt = 1
for i in range(N-1):
    if q[i+1] > q[i]:
        cnt += 1
    else:
        max_cnt = max(max_cnt, cnt)
        cnt = 1
max_cnt = max(max_cnt, cnt)
print(N - max_cnt)

Verdicts: Runtime Error

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
n = int(input())
a = [int(input()) for i in range(n)]
cnt1 = 1
cnt2 = n
for i in range(n):
  if a[i] == cnt1:
    cnt1 += 1
  if a[-i-1] == cnt2:
    cnt2 -= 1
print(n-max(cnt1,n-cnt2))

n = int(input())
a = [int(input()) for i in range(n)]
ls = [0 for i in range(n)]
for i in range(n):
  x = a[i]
  ls[x-1] = i+1
cnt = 1
ans = 0
ls.append(0)
for i in range(1,n+1):
  if ls[i]>ls[i-1]:
    cnt += 1
  else:
    ans = max(ans,cnt)
    cnt = 1
print(n-ans)

Verdicts: Wrong Answer

Accepted
Problem: You are given a sequence (P_1,P_2,...,P_N) which is a permutation of the integers from 1 through N. You would like to sort this sequence in ascending order by repeating the following operation:    * Choose an element in the sequence and move it to the beginning or the end of the sequence.  Find the minimum number of operations required. It can be proved that it is actually possible to sort the sequence using this operation.

Submission attempts (in order):
N = int(input())
a = [0]
for _ in range(N):
  a.append(int(input()))

Q = [0 for i in range(N + 1)]
for i in range(N + 1):
    Q[a[i]] = i
ans = 1
num = 0

for idx in range(2, len(a) - 1):
    if a[idx - 1] < a[idx]:
        num += 1
    else:
        ans, num = max(num, ans), 1

print(N - max(num, ans))

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

Q = [0 for i in range(N + 1)]
for i in range(N + 1):
    Q[a[i]] = i
ans = 1
num = 1

for idx in range(2, len(Q)):
    if Q[idx - 1] < Q[idx]:
        num += 1
    else:
        ans, num = max(num, ans), 1

print(N - max(num, ans))


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
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)

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)

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


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


ans = 0
prev = 0
for n in reversed(A):
    if prev != 0:
        
        if n == prev - 1:
            prev = n
        
        elif n >= prev:
            ans += n
            prev = n
        
        else:
            print(-1)
            exit()
    else:
        ans += n
        prev = n


print(ans)


Verdicts: Wrong Answer

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
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)


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

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

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

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


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
# AGC024C - Sequence Growing Easy
def main():
    N, *A = map(int, open(0))
    if A[0]:
        print(-1)


if __name__ == "__main__":
    main()

# AGC024C - Sequence Growing Easy
def main():
    N, *A = map(int, open(0))
    if A[0]:  # can't increment A_0
        print(-1)
        return
    A.append(0)
    ans, B = 0, A[::-1]
    for i, j in zip(B, B[1:]):  # check from the last
        if i > j + 1:  # increment from left one must be 0 or 1
            print(-1)
            return
        if j >= i:  # left one is an end of subsequence
            ans += j
    print(ans)


if __name__ == "__main__":
    main()

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
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()
d = collections.defaultdict(int)
for i in range(n):
    d[I()] = i
d[0] = n
t,maxinc = 0,0
for i in range(1,n+1):
    if d[i-1] < d[i]:
        t += 1
        maxinc = max(maxinc,t)
    else:
        t = 0
print(n-1-maxinc)

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

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

Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
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)

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)

Verdicts: Wrong Answer

Accepted
Problem: 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.

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


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


Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
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

counter=0
counter_n=l[0]
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)
        
        


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
counter_n=l[0]
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)
        
        


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)

Verdicts: Runtime Error

Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
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)
        

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


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
line = input()

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

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

    
def IsNotPossible(A):
    for i in range(N): 
        ai = A[i]

        if ai > i:
            return 1

        for j in range(ai):
            if A[i-1-j] < ai-1-j:
                return 1

    return 0

if IsNotPossible(A):
    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)

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

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
N = int(input())
ans = -1
bef = -1
for i in range(N):
    now = int(data[input()])
    if now - bef > 1:
        ans = -1
        break
    elif now - bef == 1:
        ans += 1
        bef = now
    else:
        ans += now
        bef = now

print(str(ans))

N = int(input())
ans = -1
bef = -1
for i in range(N):
    now = int(input())
    if now - bef > 1:
        ans = -1
        break
    elif now - bef == 1:
        ans += 1
        bef = now
    else:
        ans += now
        bef = now
 
print(str(ans))

Verdicts: Runtime Error

Accepted
Problem: 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.

Submission attempts (in order):
print(-1)

import sys
input = sys.stdin.buffer.readline

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

for _ in range(N):
    a = int(input())
    if len(G[-1]) > 0 and G[-1][-1] > a:
        G.append([])
    A.append(a)
    G[-1].append(a)

def isOk():
    if any(l[-1] + 1 < r[0] for l, r in zip(G, G[1:])):
        return False
    for grp in G:
        if any(l + 1 < r for l, r in zip(grp, grp[1:])):
            return False
    return A[0] == 0

if not isOk():
    print(-1)
    exit()

ans = 0
A = [-10**18] + A[::-1]

for r, l in zip(A, A[1:]):
    if l >= r:
        ans += l
print(ans)


Verdicts: Wrong Answer

Accepted
Problem: 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.

Submission attempts (in order):
n,*a=map(int,open(0))
r=-1
for i in range(n):
    if a[i-1] >= a[i]:r+=a[i]
    elif a[i-1]+1==a[i]:r+=1
    else:r=-1;break
print(r)

n,r=int(input()),-1
a = [int(input()) for _i in range(n)]+[-1]
for i in range(n):
    if a[i-1] >= a[i]:r+=a[i]
    elif a[i-1]+1==a[i]:r+=1
    else:r=-1;break
print(r)

Verdicts: Wrong Answer

Accepted