Problem p03841 (python)

Fixed submission:
def examA():
    x, y = LI()
    ans = 0
    if x<0 and y<0:
        if x<=y:
            ans = y-x
        else:
            ans = x-y+2
    elif x<0 and 0<=y:
        if -x<=y:
            ans = y+x+1
        else:
            ans = min(-(x+y)+1,y-x)
    elif y<=0:
        ans = abs(y+x)+1
    else:
        if x<=y:
            ans = y-x
        else:
            ans = x-y+2
    print(ans)
    return

def examB():
    B, K = LI()
    A = LI()
    cumA = [0]*(B+1); negA = [0]*(B+1)
    for i,a in enumerate(A):
        cumA[i+1] = cumA[i]
        negA[i+1] = negA[i]
        if a>0:
            cumA[i+1] +=a
        else:
            negA[i+1] +=a
    ans = 0
    for i in range(B-K+1):
        curC = cumA[K+i]-cumA[i]; curN = negA[K+i]-negA[i]
        now1 = cumA[B] + curN
        now2 = cumA[B] - curC
#        print(now1,now2)
        ans = max(ans,now1,now2)
    print(ans)
    return

def examC():
    Ai,Ao,At,Aj,Al,As,Az = LI()
    ans = 0
    ans += Ao
    ans += (Ai//2 + Aj//2 + Al//2)*2
    if Ai>=1 and Aj>=1 and Al>=1:
        if Ai%2 + Aj%2 + Al%2==3:
            ans += 3
        elif Ai%2 + Aj%2 + Al%2==2:
            ans +=1
    print(ans)
    return

def examD():
    N = I()
    X = LI()
    A = [0]*(N**2)
    que = deque()
    ans = "Yes"
    need = [0]*(N**2)
    for i in range(N):
        need[X[i]-1] = i
    for i in range(N):
        A[X[i]-1] = (i+1)
#    print(need); print(A)
    X.sort()
    k = 0
    for i in range(N):
        cur = need[X[i]-1]
        for j in range(cur):
            while(A[k]!=0):
                k +=1
            A[k] = A[X[i]-1]
        if k>=X[i]:
            ans = "No"
            break
        res = N-1-cur
        que.append((res,i))
#    print(que);  print(A)
    loop = 0
    if ans=="No":
        print(ans)
        return
    while(que and k<N**2 and loop<10**6):
        loop +=1
        cur,i = que.popleft()
        while (k<N**2 and A[k] != 0):
            k += 1
#        print(k,A[X[i]-1])
        if k<X[i] and cur>0:
            que.append((cur,i))
            continue
        for j in range(cur):
            while(k<N**2 and A[k]!=0):
                k +=1
            A[k] = A[X[i]-1]
    if que or loop>=10**6:
        ans = "No"
    if ans=="No":
        print(ans)
        return
    else:
        print(ans)
        print(" ".join(map(str,A)))
    return
"""
3
6 7 8
"""
import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18

if __name__ == '__main__':
    examD()

Buggy submission (verdict: Runtime Error):
def examA():
    x, y = LI()
    ans = 0
    if x<0 and y<0:
        if x<=y:
            ans = y-x
        else:
            ans = x-y+2
    elif x<0 and 0<=y:
        if -x<=y:
            ans = y+x+1
        else:
            ans = min(-(x+y)+1,y-x)
    elif y<=0:
        ans = abs(y+x)+1
    else:
        if x<=y:
            ans = y-x
        else:
            ans = x-y+2
    print(ans)
    return

def examB():
    B, K = LI()
    A = LI()
    cumA = [0]*(B+1); negA = [0]*(B+1)
    for i,a in enumerate(A):
        cumA[i+1] = cumA[i]
        negA[i+1] = negA[i]
        if a>0:
            cumA[i+1] +=a
        else:
            negA[i+1] +=a
    ans = 0
    for i in range(B-K+1):
        curC = cumA[K+i]-cumA[i]; curN = negA[K+i]-negA[i]
        now1 = cumA[B] + curN
        now2 = cumA[B] - curC
#        print(now1,now2)
        ans = max(ans,now1,now2)
    print(ans)
    return

def examC():
    Ai,Ao,At,Aj,Al,As,Az = LI()
    ans = 0
    ans += Ao
    ans += (Ai//2 + Aj//2 + Al//2)*2
    if Ai>=1 and Aj>=1 and Al>=1:
        if Ai%2 + Aj%2 + Al%2==3:
            ans += 3
        elif Ai%2 + Aj%2 + Al%2==2:
            ans +=1
    print(ans)
    return

def examD():
    N = I()
    X = LI()
    A = [0]*(N**2)
    que = deque()
    ans = "Yes"
    need = [0]*(N**2)
    for i in range(N):
        need[X[i]-1] = i
    for i in range(N):
        A[X[i]-1] = (i+1)
#    print(need); print(A)
    X.sort()
    k = 0
    for i in range(N):
        cur = need[X[i]-1]
        for j in range(cur):
            while(A[k]!=0):
                k +=1
            A[k] = A[X[i]-1]
        if k>=X[i]:
            ans = "No"
            break
        res = N-1-cur
        que.append((res,i))
#    print(que);  print(A)
    loop = 0
    if ans=="No":
        print(ans)
        return
    while(que and k<N**2 and loop<10**6):
        loop +=1
        cur,i = que.popleft()
        while (k<N**2 and A[k] != 0):
            k += 1
#        print(k,A[X[i]-1])
        if k<X[i] and que[i]>0:
            que.append((cur,i))
            continue
        for j in range(cur):
            while(A[k]!=0):
                k +=1
            A[k] = A[X[i]-1]
    if que or loop>=10**6:
        ans = "No"
    if ans=="No":
        print(ans)
        return
    else:
        print(ans)
        print(" ".join(map(str,A)))
    return
"""
3
6 7 8
"""
import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18

if __name__ == '__main__':
    examD()

Buggy submission (verdict: Runtime Error):
def examA():
    x, y = LI()
    ans = 0
    if x<0 and y<0:
        if x<=y:
            ans = y-x
        else:
            ans = x-y+2
    elif x<0 and 0<=y:
        if -x<=y:
            ans = y+x+1
        else:
            ans = min(-(x+y)+1,y-x)
    elif y<=0:
        ans = abs(y+x)+1
    else:
        if x<=y:
            ans = y-x
        else:
            ans = x-y+2
    print(ans)
    return

def examB():
    B, K = LI()
    A = LI()
    cumA = [0]*(B+1); negA = [0]*(B+1)
    for i,a in enumerate(A):
        cumA[i+1] = cumA[i]
        negA[i+1] = negA[i]
        if a>0:
            cumA[i+1] +=a
        else:
            negA[i+1] +=a
    ans = 0
    for i in range(B-K+1):
        curC = cumA[K+i]-cumA[i]; curN = negA[K+i]-negA[i]
        now1 = cumA[B] + curN
        now2 = cumA[B] - curC
#        print(now1,now2)
        ans = max(ans,now1,now2)
    print(ans)
    return

def examC():
    Ai,Ao,At,Aj,Al,As,Az = LI()
    ans = 0
    ans += Ao
    ans += (Ai//2 + Aj//2 + Al//2)*2
    if Ai>=1 and Aj>=1 and Al>=1:
        if Ai%2 + Aj%2 + Al%2==3:
            ans += 3
        elif Ai%2 + Aj%2 + Al%2==2:
            ans +=1
    print(ans)
    return

def examD():
    N = I()
    X = LI()
    A = [0]*(N**2)
    que = deque()
    ans = "Yes"
    need = [0]*(N**2)
    for i in range(N):
        need[X[i]-1] = i
    for i in range(N):
        A[X[i]-1] = (i+1)
#    print(need); print(A)
    X.sort()
    k = 0
    for i in range(N):
        cur = need[X[i]-1]
        for j in range(cur):
            while(A[k]!=0):
                k +=1
            A[k] = A[X[i]-1]
        if k>=X[i]:
            ans = "No"
            break
        res = N-1-cur
        que.append((res,i))
#    print(que);  print(A)
    if ans=="No":
        print(ans)
        return
    while(que and k<N**2):
        cur,i = que.popleft()
        while (k<N**2 and A[k] != 0):
            k += 1
#        print(k,A[X[i]-1])
        if k<X[i] and que[i]>0:
            que.append((cur,i))
            continue
        for j in range(cur):
            while(A[k]!=0):
                k +=1
            A[k] = A[X[i]-1]
    if que:
        ans = "No"
    if ans=="No":
        print(ans)
        return
    else:
        print(ans)
        print(" ".join(map(str,A)))
    return
"""
3
6 7 8
"""
import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18

if __name__ == '__main__':
    examD()