Problem p02557 (python)

Fixed submission:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
# mod = 998244353
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"
INF = 1<<32-1
# INF = 10**18

def main():
    n = int(ipt())
    base = n+1
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    na = [0]*(n+1)
    nb = [0]*(n+1)
    bl = b[-1]

    for i in range(n):
        na[a[i]] += 1
        nb[b[i]] += 1

    bs = []

    for i in range(1,n+1):
        nai = na[i]
        nbi = nb[i]
        if nai+nbi > n:
            print("No")
            exit()
        elif nai+nbi != 0:
            if bl != i:
                hpq.heappush(bs,(n-nai-nbi)*base+i)

    print("Yes")
    ans = []
    q = []
    for i,ai in enumerate(a):
        na[ai] -= 1
        while True:
            hi = hpq.heappop(bs)
            hiv = hi%base
            while True:
                hi = hpq.heappushpop(bs,(n-na[hiv]-nb[hiv])*base+hiv)
                if hiv == hi%base:
                    break
                else:
                    hiv = hi%base
            if na[hiv]+nb[hiv] < n-i and nb[bl] != 0:
                ans.append(bl)
                nb[bl] -= 1
                q.append((n-na[hiv]-nb[hiv])*base+hiv)
                break
            elif hiv == ai:
                if nb[hiv] != 0:
                    q.append((n-na[ai]-nb[ai])*base+hiv)
            elif nb[hiv] != 0:
                ans.append(hiv)
                nb[hiv] -= 1
                q.append((n-na[hiv]-nb[hiv])*base+hiv)
                break
        while q:
            hpq.heappush(bs,q.pop())

    print(" ".join(map(str,ans)))

    return None

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
# mod = 998244353
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"
INF = 1<<32-1
# INF = 10**18

def main():
    n = int(ipt())
    base = n+1
    a = [int(i) for i in ipt().split()]
    b = [int(i) for i in ipt().split()]
    na = [0]*(n+1)
    nb = [0]*(n+1)
    bl = b[-1]

    for i in range(n):
        na[a[i]] += 1
        nb[b[i]] += 1

    bs = []

    for i in range(1,n+1):
        nai = na[i]
        nbi = nb[i]
        if nai+nbi > n:
            print("No")
            exit()
        elif nai+nbi != 0:
            if bl != i:
                hpq.heappush(bs,(n-nai-nbi)*base+i)

    print("Yes")
    ans = []
    q = []
    for i,ai in enumerate(a):
        na[ai] -= 1
        while True:
            hi = hpq.heappop(bs)
            hiv = hi%base
            if na[hiv]+nb[hiv] < n-i and nb[bl] != 0:
                ans.append(bl)
                nb[bl] -= 1
                q.append((n-na[hiv]-nb[hiv])*base+hiv)
                break
            elif hiv == ai:
                if nb[hiv] != 0:
                    q.append((n-na[ai]-nb[ai])*base+hiv)
            elif nb[hiv] != 0:
                ans.append(hiv)
                nb[hiv] -= 1
                q.append((n-na[ai]-nb[ai])*base+hiv)
                break
        while q:
            hpq.heappush(bs,q.pop())

    print(" ".join(map(str,ans)))

    return None

if __name__ == '__main__':
    main()