Problem p02557 (python)

Fixed submission:
from collections import Counter
N = int(input())
As = input().split()
Bs = input().split()

allct = Counter(As + Bs)
if allct.most_common(1)[0][1] > N:
    print('No')
    exit()

lastInA = {}
firstInB = {}

for i, a in enumerate(As):
    lastInA[a] = i

for i, b in enumerate(Bs):
    if b not in firstInB:
        firstInB[b] = i

offset = 0
for k, v in lastInA.items():
    if k not in firstInB:
        continue
    tmp = v - firstInB[k] + 1
    offset = offset if offset > tmp else tmp

print('Yes')
print(' '.join(Bs[-offset:] + Bs[:-offset]))

Buggy submission (verdict: Runtime Error):
from collections import Counter
from heapq import heappush, heappop
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
ctall = Counter(As + Bs)
mcvalue, mccount = ctall.most_common(1)[0]

if mccount > N:
    print('No')
    exit()

print('Yes')
ctA = Counter(As)
ctB = Counter(Bs)

q = []
for k, v in ctA.items():
    if ctB[k] > 0:
        heappush(q, [-(ctA[k]+ctB[k]), k])
onlyBs = []
for k, v in ctB.items():
    if ctA[k] == 0:
        onlyBs += [k] * v

ans = []
for a in As:
    act = ak = None
    while q:
        ct, k = heappop(q)
        if a == k:
            act, ak = ct, k
        else:
            if ctB[k] > 0:
                ctB[k] -= 1
                ans.append(k)
                if ctB[k] > 0:
                    heappush(q, [ct+1, k])
                break
    else:
        ans.append(onlyBs.pop())
    if act is not None:
        heappush(q, [act+1, ak])


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

Buggy submission (verdict: Runtime Error):
from collections import Counter
from heapq import heappush, heappop
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
ctall = Counter(As + Bs)
mcvalue, mccount = ctall.most_common(1)[0]

if mccount > N:
    print('No')
    exit()

print('Yes')
ctA = Counter(As)
ctB = Counter(Bs)

q = []
for k, v in ctA.items():
    if ctB[k] > 0:
        heappush(q, [-(ctA[k]+ctB[k]), k])
onlyBs = []
for k, v in ctB.items():
    if ctA[k] == 0:
        onlyBs += [k] * v

ans = []
for a in As:
    if q:
        ct, k = heappop(q)
        if a == k:
            while q:
                ct2, k2 = heappop(q)
                if ctB[k2] > 0:
                    ctB[k2] -= 1
                    ans.append(k2)
                    if ctB[k2] > 0:
                        heappush(q, [ct2+1, k2])
                    break
            else:
                ans.append(onlyBs.pop())
            heappush(q, [ct+1, k])
        else:
            if ctB[k] > 0:
                ctB[k] -= 1
                ans.append(k)
                if ctB[k] > 0:
                    heappush(q, [ct+1, k])
            else:
                while q:
                    ct2, k2 = heappop(q)
                    if ctB[k2] > 0:
                        ctB[k2] -= 1
                        ans.append(k2)
                        if ctB[k2] > 0:
                            heappush(q, [ct2+1, k2])
                        break
                else:
                    ans.append(onlyBs.pop())

    else:
        ans.append(onlyBs.pop())


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

Buggy submission (verdict: Runtime Error):
from collections import Counter
from heapq import heappush, heappop
N = int(input())
As = list(map(int, input().split()))
Bs = list(map(int, input().split()))
ctall = Counter(As + Bs)
mcvalue, mccount = ctall.most_common(1)[0]

if mccount > N:
    print('No')
    exit()

print('Yes')
ctA = Counter(As)
ctB = Counter(Bs)

q = []
for k, v in ctA.items():
    if ctB[k] > 0:
        heappush(q, [-(ctA[k]+ctB[k]), k])
onlyBs = []
for k, v in ctB.items():
    if ctA[k] == 0:
        onlyBs += [k] * v

ans = []
for a in As:
    if q:
        ct, k = heappop(q)
        if a == k:
            if q:
                ct2, k2 = heappop(q)
                ctB[k2] -= 1
                ans.append(k2)
                if ctB[k2] > 0:
                    heappush(q, [ct2+1, k2])
            else:
                ans.append(onlyBs.pop())
            heappush(q, [ct+1, k])
        else:
            ctB[k] -= 1
            ans.append(k)
            if ctB[k] > 0:
                heappush(q, [ct+1, k])
    else:
        ans.append(onlyBs.pop())


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