Problem p02692 (python)

Fixed submission:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,a,b,c = inpm()
    S = []
    ans = []
    for i in range(n):
        s = input()
        S.append(s)
    for i in range(n):
        s = S[i]
        if s == 'AB':
            if a == 0 and b == 0:
                print('No')
                return
            if a == 0:
                a += 1
                b -= 1
                ans.append('A')
            elif b == 0:
                a -= 1
                b += 1
                ans.append('B')
            elif a == 1 and b == 1 and i < n-1 and s != S[i+1]:
                if S[i+1] == 'AC':
                    a += 1
                    b -= 1
                    ans.append('A')
                else:
                    b += 1
                    a -= 1
                    ans.append('B')
            else:
                if a > b:
                    a -= 1
                    b += 1
                    ans.append('B')
                else:
                    a += 1
                    b -= 1
                    ans.append('A')
        if s == 'AC':
            if a == 0 and c == 0:
                print('No')
                return
            if a == 0:
                a += 1
                c -= 1
                ans.append('A')
            elif c == 0:
                a -= 1
                c += 1
                ans.append('C')
            elif a == 1 and c == 1 and i < n-1 and s != S[i+1]:
                if S[i+1] == 'AB':
                    a += 1
                    c -= 1
                    ans.append('A')
                else:
                    c += 1
                    a -= 1
                    ans.append('C')
            else:
                if a > c:
                    a -= 1
                    c += 1
                    ans.append('C')
                else:
                    a += 1
                    c -= 1
                    ans.append('A')
        if s == 'BC':
            if c == 0 and b == 0:
                print('No')
                return
            if b == 0:
                b += 1
                c -= 1
                ans.append('B')
            elif c == 0:
                b -= 1
                c += 1
                ans.append('C')
            elif b == 1 and c == 1 and i < n-1 and s != S[i+1]:
                if S[i+1] == 'AB':
                    b += 1
                    c -= 1
                    ans.append('B')
                else:
                    c += 1
                    b -= 1
                    ans.append('C')
            else:
                if b > c:
                    b -= 1
                    c += 1
                    ans.append('C')
                else:
                    b += 1
                    c -= 1
                    ans.append('B')
    print('Yes')
    for i in range(n):
        print(ans[i])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,a,b,c = inpm()
    s = []
    ans = []
    for i in range(n):
        S = input()
        s.append(S)
    for i in range(n):
        s = S[i]
        if s == 'AB':
            if a == 0 and b == 0:
                print('No')
                return
            if a == 0:
                a += 1
                b -= 1
                ans.append('A')
            elif b == 0:
                a -= 1
                b += 1
                ans.append('B')
            elif a == 1 and b == 1 and i < n-1 and s != S[i+1]:
                if S[i+1] == 'AC':
                    a += 1
                    b -= 1
                    ans.append('A')
                else:
                    b += 1
                    a -= 1
                    ans.append('B')
            else:
                if a > b:
                    a -= 1
                    b += 1
                    ans.append('B')
                else:
                    a += 1
                    b -= 1
                    ans.append('A')
        if s == 'AC':
            if a == 0 and c == 0:
                print('No')
                return
            if a == 0:
                a += 1
                c -= 1
                ans.append('A')
            elif c == 0:
                a -= 1
                c += 1
                ans.append('C')
            elif a == 1 and c == 1 and i < n-1 and s != S[i+1]:
                if S[i+1] == 'AB':
                    a += 1
                    c -= 1
                    ans.append('A')
                else:
                    c += 1
                    a -= 1
                    ans.append('C')
            else:
                if a > c:
                    a -= 1
                    c += 1
                    ans.append('C')
                else:
                    a += 1
                    c -= 1
                    ans.append('A')
        if s == 'BC':
            if c == 0 and b == 0:
                print('No')
                return
            if b == 0:
                b += 1
                c -= 1
                ans.append('B')
            elif c == 0:
                b -= 1
                c += 1
                ans.append('C')
            elif b == 1 and c == 1 and i < n-1 and s != S[i+1]:
                if S[i+1] == 'AB':
                    b += 1
                    c -= 1
                    ans.append('B')
                else:
                    c += 1
                    b -= 1
                    ans.append('C')
            else:
                if b > c:
                    b -= 1
                    c += 1
                    ans.append('C')
                else:
                    b += 1
                    c -= 1
                    ans.append('B')
    print('Yes')
    for i in range(n):
        print(ans[i])

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,a,b,c = inpm()
    aa = copy.copy(a)
    bb = copy.copy(b)
    cc = copy.copy(c)
    ans = ['p' for _ in range(n)]
    AB = deque()
    AC = deque()
    BC = deque()
    S = []
    for i in range(n):
        s = input()
        S.append(s)
        if s == 'AB':
            if a <= 0 and b <= 0:
                if AC and BC:
                    j = AC.popleft()
                    ans[j] = 'A'
                    k = BC.popleft()
                    ans[k] = 'B'
                else:
                    a -= 1
                    b -= 1
                    AB.append(i)
            else:
                a -= 1
                b -= 1
                AB.append(i)
        if s == 'AC':
            if a <= 0 and c <= 0:
                if AB and BC:
                    j = AB.popleft()
                    ans[j] = 'A'
                    k = BC.popleft()
                    ans[k] = 'C'
                else:
                    a -= 1
                    c -= 1
                    AC.append(i)
            else:
                a -= 1
                c -= 1
                AC.append(i)
        if s == 'BC':
            if c <= 0 and b <= 0:
                if AB and AC:
                    j = AB.popleft()
                    ans[j] = 'B'
                    k = AC.popleft()
                    ans[k] = 'C'
                else:
                    c -= 1
                    b -= 1
                    BC.append(i)
            else:
                c -= 1
                b -= 1
                BC.append(i)

    if a >= 0 and b >= 0 and c < 0:
        if c + len(AC) + len(BC) < 0:
            print('No')
            return
        while AC:
            i = AC.popleft()
            ans[i] = 'C'
        while BC:
            i = BC.popleft()
            ans[i] = 'C'
        while AB:
            i = AB.popleft()
            ans[i] = 'A'
    if a >= 0 and b < 0 and c >= 0:
        if b + len(AB) + len(BC) < 0:
            print('No')
            return
        while AB:
            i = AB.popleft()
            ans[i] = 'B'
        while BC:
            i = BC.popleft()
            ans[i] = 'B'
        while AC:
            i = AC.popleft()
            ans[i] = 'A'
    if a < 0 and b >= 0 and c >= 0:
        if c + len(AC) + len(BC) < 0:
            print('No')
            return
        while AC:
            i = AC.popleft()
            ans[i] = 'A'
        while BC:
            i = BC.popleft()
            ans[i] = 'A'
        while AB:
            i = AB.popleft()
            ans[i] = 'C'

    if a < 0 and b >= 0 and c < 0:
        while BC:
            i = BC.popleft()
            ans[i] = 'C'
            c += 1
        while AB:
            i = AB.popleft()
            ans[i] = 'A'
            a += 1
        while AC:
            while c < 0:
                if not AC:
                    break
                i = AC.popleft()
                ans[i] = 'C'
                c += 1
            while a < 0:
                if not AC:
                    break
                i = AC.popleft()
                ans[i] = 'A'
                a += 1
            i = AC.popleft()
            ans[i] = 'A'
    if a >= 0 and b < 0 and c < 0:
        while AC:
            i = AC.popleft()
            ans[i] = 'C'
            c += 1
        while AB:
            i = AB.popleft()
            ans[i] = 'B'
            b += 1
        while BC:
            while c < 0:
                if not BC:
                    break
                i = BC.popleft()
                ans[i] = 'C'
                c += 1
            while b < 0:
                if not BC:
                    break
                i = BC.popleft()
                ans[i] = 'B'
                b += 1
            i = BC.popleft()
            ans[i] = 'B'
            

    if a < 0 and b < 0 and c >= 0:
        while BC:
            i = BC.popleft()
            ans[i] = 'B'
            b += 1
        while AC:
            i = AC.popleft()
            ans[i] = 'A'
            a += 1
        while AB:
            while b < 0:
                if not AB:
                    break
                i = AB.popleft()
                ans[i] = 'B'
                b += 1
            while a < 0:
                if not AC:
                    break
                i = AC.popleft()
                ans[i] = 'A'
                a += 1
            i = AB.popleft()
            ans[i] = 'A'

    if a >= 0 and b >= 0 and c >= 0:
        while AC:
            i = AC.popleft()
            ans[i] = 'A'
            a += 1
        while AB:
            i = AB.popleft()
            ans[i] = 'A'
            a += 1
        while BC:
            i = BC.popleft()
            ans[i] = 'C'
            a += 1

    for i in range(n):
        t = S[i]
        if t == 'AC':
            if ans[i] == 'A':
                aa += 1
                cc -= 1
            else:
                aa -= 1
                cc += 1
        if t == 'BC':
            if ans[i] == 'B':
                bb += 1
                cc -= 1
            else:
                bb -= 1
                cc += 1
        if t == 'AB':
            if ans[i] == 'A':
                aa += 1
                bb -= 1
            else:
                aa -= 1
                bb += 1
        if aa<0 or bb<0 or cc<0:
            print('No')
            return


    print('Yes')
    for i in range(n):
        print(ans[i])

if __name__ == "__main__":
    main()