Problem p02557 (python)

Fixed submission:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7


def run():
    N = int(sysread())
    A = list(map(int, sysread().split()))
    B = list(map(int, sysread().split()))
    list_A = defaultdict(lambda:[])
    for i in range(N):
        list_A[A[i]].append(i)

    list_B = defaultdict(lambda:[])
    for i in range(N):
        list_B[B[i]].append(i)

    cnt = [0] * (N+1)
    for v in A + B:
        cnt[v] += 1
        if cnt[v] > N:
            print('No')
            return
    q = [set() for _ in range(N+1)]
    for i in range(1, N+1):
        q[cnt[i]].add(i)
    #print(q)

    arr = [-1] * N
    for i in range(N):
        #print(list_A)
        #print(list_B)
        v = q[N-i]
        #print(i, '--------------')
        #print(q)
        if len(v) == 0:
            for alpha in list_A.keys():
                j = cnt[alpha]
                q[j].remove(alpha)
                q[j - 1].add(alpha)
                cnt[alpha] -= 1
                break

            for beta in list_B.keys():
                if alpha == beta :continue
                j = cnt[beta]
                q[j].remove(beta)
                q[j - 1].add(beta)
                cnt[beta] -= 1
                break

            arr[list_A[alpha].pop()] = beta
            list_B[beta].pop()

        elif len(v) == 1:
            alpha,  = v
            q[N-i].remove(alpha)
            mem = alpha
            #print(list_A)
            #print(list_B)
            if not list_A[alpha]:
                beta = alpha

                for alpha in list_A.keys():
                    if beta == alpha:continue
                    j = cnt[alpha]
                    q[j].remove(alpha)
                    q[j - 1].add(alpha)
                    cnt[alpha] -= 1
                    break

            else:
                for beta in list_B.keys():
                    if beta == alpha:continue
                    j = cnt[beta]
                    q[j].remove(beta)
                    q[j - 1].add(beta)
                    cnt[beta] -= 1
                    break

            arr[list_A[alpha].pop()] = beta
            list_B[beta].pop()
            q[N - i - 1].add(mem)
        else:
            alpha, beta = list(v)
            q[N-i].remove(alpha)
            q[N-i].remove(beta)
            if not list_A[alpha]:
                alpha, beta = beta, alpha
            arr[list_A[alpha].pop()] = beta
            list_B[beta].pop()
            q[N-i-1].add(alpha)
            q[N - i - 1].add(beta)

        if not len(list_A[alpha]):del list_A[alpha]
        if not len(list_B[beta]): del list_B[beta]
    print('Yes')
    print(' '.join([str(s) for s in arr]))
if __name__ == "__main__":
    run()

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7


def run():
    N = int(sysread())
    A = list(map(int, sysread().split()))
    B = list(map(int, sysread().split()))
    list_A = [[] for i in range(N+1)]
    for i in range(N):
        list_A[A[i]].append(i)

    list_B = [[] for i in range(N+1)]
    for i in range(N):
        list_B[B[i]].append(i)

    cnt = [0] * (N+1)
    for v in A + B:
        cnt[v] += 1
        if cnt[v] > N:
            print('No')
            return
    q = [[] for _ in range(N+1)]
    for i in range(1, N+1):
        q[cnt[i]].append(i)
    #print(q)

    arr = [-1] * N
    for i in range(N):
        #print(list_A)
        #print(list_B)
        v = q[N-i]
        #print(i, '--------------')
        #print(q)
        if len(v) == 0:
            for j in range(N - i - 1, 0, -1):
                if q[j]:
                    for idx, alpha in enumerate(q[j]):
                        if list_A[alpha]:
                            del q[j][idx]
                            q[j - 1].append(alpha)
                            break
            done = False
            for j in range(N - i - 1, 0, -1):
                if q[j]:
                    for idx, beta in q[j]:
                        if list_B[beta]:
                            del q[j][idx]
                            q[j - 1].append(beta)
                            done = True
                            break
                    if done:break
            arr[list_A[alpha].pop()] = beta
            list_B[beta].pop()


        elif len(v) == 1:
            alpha,  = v
            q[N-i].pop()
            mem = alpha
            #print(list_A)
            #print(list_B)
            if not list_A[alpha]:
                beta = alpha
                done= False
                for j in range(N - i - 1, 0, -1):
                    if q[j]:
                        for idx, alpha in enumerate(q[j]):
                            if list_A[alpha]:
                                del q[j][idx]
                                q[j - 1].append(alpha)
                                done = True
                                break
                        if done:break
            else:
                done = False
                for j in range(N-i-1, 0, -1):
                    if q[j]:
                        for idx, beta in enumerate(q[j]):
                            if list_B[beta]:
                                del q[j][idx]
                                q[j-1].append(beta)
                                done = True
                                break
                        if done:break
            arr[list_A[alpha].pop()] = beta
            list_B[beta].pop()
            q[N - i - 1].append(mem)
        else:
            alpha, beta = v
            q[N-i].pop()
            q[N-i].pop()
            if not list_A[alpha]:
                alpha, beta = beta, alpha
            arr[list_A[alpha].pop()] = beta
            list_B[beta].pop()
            q[N-i-1].append(alpha)
            q[N - i - 1].append(beta)
    print('Yes')
    print(' '.join([str(s) for s in arr]))
if __name__ == "__main__":
    run()