Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n, a, b, c = LI()
    s = SR(n)
    ans = [None] * n
    dp = [deque() for i in range(3)]
    l = [[0] * 3 for i in range(n)]
    d = {"A": 0, "B": 1, "C": 2}
    for i in range(n):
        l[i][d[s[i][0]]] = 1
        l[i][d[s[i][1]]] = 1
    for i in range(n - 1, 0, -1):
        l[i - 1][0] += l[i][0]
        l[i - 1][1] += l[i][1]
        l[i - 1][2] += l[i][2]
    dd = [0] * 3
    ddd = defaultdict(int)
    for i in range(n):
        if s[i] == "AB":
            if a < b:
                ans[i] = "A"
                a += 1
                b -= 1
            elif a > b:
                ans[i] = "B"
                a -= 1
                b += 1
            else:
                if a == 0:
                    if len(dp[0]) - dd[0] == 0 and len(dp[1]) - dd[1] == 0:
                        print("No")
                        return
                    if len(dp[0]) - dd[0] > len(dp[1]) - dd[1]:
                        x = dp[0].popleft()
                        while ddd[x]:
                            x = dp[0].popleft()
                        ddd[x] = 1
                        dd[0] += 1
                        if s[x] == "AB":
                            dd[1] += 1
                        else:
                            dd[2] += 1
                        ans[x] = "A"
                        ans[i] = "B"
                    elif len(dp[0]) - dd[0] < len(dp[1]) - dd[1]:
                        x = dp[1].popleft()
                        while ddd[x]:
                            x = dp[1].popleft()
                        ddd[x] = 1
                        dd[1] += 1
                        if s[x] == "AB":
                            dd[0] += 1
                        else:
                            dd[2] += 1
                        ans[x] = "B"
                        ans[i] = "A"
                    else:
                        if l[i][0] < l[i][1]:
                            x = dp[0].popleft()
                            while ddd[x]:
                                x = dp[0].popleft()
                            ddd[x] = 1
                            dd[0] += 1
                            if s[x] == "AB":
                                dd[1] += 1
                            else:
                                dd[2] += 1
                            ans[x] = "A"
                            ans[i] = "B"
                        else:
                            x = dp[1].popleft()
                            while ddd[x]:
                                x = dp[1].popleft()
                            dd[1] += 1
                            if s[x] == "AB":
                                dd[0] += 1
                            else:
                                dd[2] += 1
                            ddd[x] = 1
                            ans[x] = "B"
                            ans[i] = "A"
                else:
                    dp[0].append(i)
                    dp[1].append(i)
                    a -= 1
                    b -= 1
        elif s[i] == "AC":
            if a < c:
                ans[i] = "A"
                a += 1
                c -= 1
            elif a > c:
                ans[i] = "C"
                a -= 1
                c += 1
            else:
                if a == 0:
                    if len(dp[0]) - dd[0] == 0 and len(dp[2]) - dd[2] == 0:
                        print("No")
                        return
                    if len(dp[0]) - dd[0] > len(dp[2]) - dd[2]:
                        x = dp[0].popleft()
                        while ddd[x]:
                            x = dp[0].popleft()
                        dd[0] += 1
                        if s[x] == "AB":
                            dd[1] += 1
                        else:
                            dd[2] += 1
                        ddd[x] = 1
                        ans[x] = "A"
                        ans[i] = "C"
                    elif len(dp[0]) - dd[0] < len(dp[2]) - dd[2]:
                        x = dp[2].popleft()
                        while ddd[x]:
                            x = dp[2].popleft()
                        dd[2] += 1
                        if s[x] == "AC":
                            dd[0] += 1
                        else:
                            dd[1] += 1
                        ddd[x] = 1
                        ans[x] = "C"
                        ans[i] = "A"
                    else:
                        if l[i][0] < l[i][2]:
                            x = dp[0].popleft()
                            while ddd[x]:
                                x = dp[0].popleft()
                            dd[0] += 1
                            if s[x] == "AB":
                                dd[1] += 1
                            else:
                                dd[2] += 1
                            ddd[x] = 1
                            ans[x] = "A"
                            ans[i] = "C"
                        else:
                            x = dp[2].popleft()
                            while ddd[x]:
                                x = dp[2].popleft()
                            dd[2] += 1
                            if s[x] == "AC":
                                dd[0] += 1
                            else:
                                dd[1] += 1
                            ddd[x] = 1
                            ans[x] = "C"
                            ans[i] = "A"
                else:
                    dp[0].append(i)
                    dp[2].append(i)
                    a -= 1
                    c -= 1
        else:
            if b < c:
                ans[i] = "B"
                b += 1
                c -= 1
            elif b > c:
                ans[i] = "C"
                b -= 1
                c += 1
            else:
                if b == 0:
                    if len(dp[1]) - dd[1] == 0 and len(dp[2]) - dd[2] == 0:
                        print("No")
                        return
                    if len(dp[1]) - dd[1] > len(dp[2]) - dd[2]:
                        x = dp[1].popleft()
                        while ddd[x]:
                            x = dp[1].popleft()
                        dd[1] += 1
                        if s[x] == "AB":
                            dd[0] += 1
                        else:
                            dd[2] += 1
                        ddd[x] = 1
                        ans[x] = "B"
                        ans[i] = "C"
                    elif len(dp[1]) - dd[1] < len(dp[2]) - dd[2]:
                        x = dp[2].popleft()
                        while ddd[x]:
                            x = dp[2].popleft()
                        dd[2] += 1
                        if s[x] == "AC":
                            dd[0] += 1
                        else:
                            dd[1] += 1
                        ddd[x] = 1
                        ans[x] = "C"
                        ans[i] = "B"
                    else:
                        if l[i][1] < l[i][2]:
                            x = dp[1].popleft()
                            while ddd[x]:
                                x = dp[1].popleft()
                            dd[1] += 1
                            if s[x] == "AB":
                                dd[0] += 1
                            else:
                                dd[2] += 1
                            ddd[x] = 1
                            ans[x] = "B"
                            ans[i] = "C"
                        else:
                            x = dp[2].popleft()
                            while ddd[x]:
                                x = dp[2].popleft()
                            dd[2] += 1
                            if s[x] == "AC":
                                dd[0] += 1
                            else:
                                dd[1] += 1
                            ddd[x] = 1
                            ans[x] = "C"
                            ans[i] = "B"
                else:
                    dp[1].append(i)
                    dp[2].append(i)
                    b -= 1
                    c -= 1
    ll = ["A", "B", "C"]
    print("Yes")
    for i in range(3):
        while dp[i]:
            p = dp[i].pop()
            if ans[p] == None:
                ans[p] = ll[i]
    for i in ans:
        print(i)
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
ABC = [A, B, C]
if ABC == [0, 0, 0]:
  print("No")
  quit()

p = [0] * N
for i in range(N):
  p[i] = str(input())
  
s = [0] * N
for i in range(N):
  x = p[i]
  if x == "AB":
    if A > B:
      A, B = A - 1, B + 1
      s[i] = "B"
    elif B > A:
      A, B = A + 1, B - 1
      s[1] = "A"
    else:
      if A == 0:
        A, B = A + 1, B - 1
        s[1] = "A"  
      elif B == 0:
        A, B = A - 1, B + 1
        s[1] = "B"
      else:
        A, B = A - 1, B + 1
        s[1] = "B"
  elif x == "BC":
    if B > C:
      B, C = B - 1, C + 1
      s[i] = "C"
    elif B > A:
      B, C = B + 1, C - 1
      s[1] = "B"
  else:
    if C > A:
      C, A = C - 1, A + 1
      s[i] = "A"
    elif B > A:
      C, A = C + 1, A - 1
      s[1] = "C"    
  
print("Yes")  
for i in range(N):
  print(s[i])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


def yes():
    print("Yes")  # type: str


def no():
    print("No")  # type: str


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    ABC = [A, B, C]
    ans = []
    d = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
    # print(ABC)
    for i in range(N):
        j, k = d[s[i]]
        if ABC[j] == ABC[k] == 0:
            no()
            return
        if ABC[j] == ABC[k] == 1:
            if i != N-1 and (j not in s[i+1]):
                j, k = k, j
            ABC[j] += 1
            ABC[k] -= 1
            ans.append("ABC"[j])
        elif ABC[j] > ABC[k]:
            ABC[j] -= 1
            ABC[k] += 1
            ans.append("ABC"[k])
        else:
            ABC[j] += 1
            ABC[k] -= 1
            ans.append("ABC"[j])

    yes()
    for a in ans:
        print(a)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, A, B, C, s)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
dic = {
    "A": A,
    "B": B,
    "C": C
}
ans = [0] * N
def part_solve(dic, depth):
    while depth < N:
        key1, key2 = inputs[depth]
        if dic[key1] < dic[key2]:
            dic[key1] += 1
            dic[key2] -= 1
            ans[depth] = key1
        elif dic[key1] > dic[key2]:
            dic[key2] += 1
            dic[key1] -= 1
            ans[depth] = key2
        else:
            if not dic[key1]:
                return False
            else:
                key3 = "ABC".replace(key1, "").replace(key2, "")
                return part_solve({
                    key1: dic[key1] + 1,
                    key2: dic[key2] - 1,
                    key3: dic[key3]
                }, depth) or part_solve({
                    key1: dic[key1] - 1,
                    key2: dic[key2] + 1,
                    key3: dic[key3]
                }, depth)
        depth += 1
    return True

inputs = tuple(tuple(input()) for _ in range(N))
if part_solve(dic, 0):
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")

Submission status: Runtime Error

Solution code:
import random
def answer(x):
    print("Yes")
    AB = ["B"] * x[0][0] + ["A"] * x[0][1]
    AC = ["C"] * x[1][0] + ["A"] * x[1][1]
    BC = ["C"] * x[2][0] + ["B"] * x[2][1]
    AB = random.shuffle(AB)
    AC = random.shuffle(AC)
    BC = random.shuffle(BC)
    for i in range(n):
        if s[i] == "AB":
            print(AB.pop(0))
        elif s[i] == "AC":
            print(AC.pop(0))
        else:
            print(BC.pop(0))
    exit()

n,a,b,c = map(int,input().split())
s = [str(input()) for i in range(n)]
cnt = [0 for i in range(3)]
for ss in s:
    if ss == "AB":
        a += 1
        b += 1
        cnt[0] += 1
    elif ss == "AC":
        a += 1
        c += 1
        cnt[1] += 1
    else:
        b += 1
        c += 1
        cnt[2] += 1
a,b,c = a//2,b//2,c//2
all = min(min(cnt),min(a,b,c))
a,b,c = a-all,b-all,c-all
for i in range(3):
    cnt[i] -= all
  # cnt=["AB","AC","BC"]

#"AB"=0
if cnt[0] == 0:
        ACdeA = min(cnt[1],a)
        BCdeB = min(cnt[2],b)
        ACdeC = cnt[1]-ACdeA
        BCdeC = cnt[2]-BCdeB
        if ACdeC + BCdeC <= c:
            answer([[all,0],[ACdeA,all+ACdeC],[all+BCdeB,BCdeC]])
if cnt[1] == 0:
        ABdeA = min(cnt[0],a)
        BCdeC = min(cnt[2],c)
        ABdeB = cnt[0]-ABdeA
        BCdeB = cnt[2]-BCdeC
        if ABdeB + BCdeB <= b:
            answer([[all+ABdeA,ABdeB],[0,all],[all+BCdeB,BCdeC]])
if cnt[2] == 0:
        ABdeB = min(cnt[0],b)
        ACdeC = min(cnt[1],c)
        ABdeA = cnt[0]-ABdeB
        ACdeA = cnt[1]-ACdeC
        if ABdeA + ACdeA <= a:
            answer([[all+ABdeA,ABdeB],[ACdeA,ACdeC+all],[all,0]])
if a == 0:
    ABdeB = cnt[0]
    ACdeC = cnt[1]
    b -= ABdeB
    c -= ACdeC
    if b >= 0 and c >= 0:
        BCdeB = min(b,cnt[2])
        BCdeC = cnt[2]-BCdeB
        if BCdeC <= c:
             answer([[all,ABdeB],[0,ACdeC+all],[all+BCdeB,BCdeC]])
    b += ABdeB
    c += ACdeC
if b == 0:
    ABdeA = cnt[0]
    BCdeC = cnt[2]
    a -= ABdeA
    c -= BCdeC
    if a >= 0 and c >= 0:
        ACdeA = min(a,cnt[1])
        ACdeC = cnt[1]-ACdeA
        if ACdeC <= c:
            answer([[all+ABdeA,0],[ACdeA,ACdeC+all],[all,BCdeC]])
    a += ABdeA
    c += BCdeC
if c == 0:
    ACdeA = cnt[1]
    BCdeB = cnt[2]
    a -= ACdeA
    b -= BCdeB
    if a >= 0 and b >= 0:
        ABdeA = min(a,cnt[0])
        ABdeB = cnt[0]-ABdeA
        if ABdeB <= b:
            answer([[all+ABdeA,ABdeB],[ACdeA,all],[BCdeB+all,0]])
print("No")

Submission status: Runtime Error

Solution code:
import sys,math,copy,queue,itertools,bisect,collections
from heapq import *
sys.setrecursionlimit(10**7)
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
INF = 10**18
MOD = 10**9 + 7
DD = ((1,0),(-1,0),(0,1),(0,-1))

n,a,b,c = LI()
z = [a,b,c]
s = [sys.stdin.readline().rstrip() for _ in range(n)]
ans = []
for i in range(n):
    x = s[i]
    p = -1
    if x == 'AB': j=0;k=1;l=2
    elif x == 'AC': j=0;k=2;l=1
    elif x == 'BC': j=1;k=2;l=0
    if z[j] > z[k]:
        z[j] -= 1; z[k] += 1
        p = k
    else:
        z[j] += 1; z[k] -= 1
        p = j
    if (z[j] == 0 or z[k] == 0) and z[l] == 0:
        if i < n+1:
            y = s[i+1]
            if x == 'AB': j1=0;k1=1;l1=3
            elif x == 'AC': j1=0;k1=2;l1=1
            elif x == 'BC': j1=1;k1=2;l1=0
            if z[j1] == 0 and z[k1] == 0:
                if z[j] == 0:
                    z[j] += 2; z[k] -= 2
                    p = j
                else:
                    z[j] -= 2; z[k] += 2
                    p = k
    if min(z) < 0:
        print('No')
        exit(0)
    if p==0:ans.append('A')
    elif p==1:ans.append('B')
    else: ans.append('C')
print('Yes')
print(*ans,sep='\n')

Submission status: Runtime Error

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N,A,B,C = map(int,input().split())
    choice = [input() for _ in range(N)]
    ab = 0
    bc = 0
    ca = 0
    for s in choice:
        if s == 'AB':
            ab += 1
        elif s == 'BC':
            bc += 1
        else:
            ca += 1
    
    ans = [[0,0] for _ in range(3)]
    if A > 0 or B > 0:
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
    else:
        if bc > 0 and C > 0:
            bc -= 1
            C -= 1
            B += 1
            ans[1][0] += 1
        elif ca > 0 and C > 0:
            ca -= 1
            C -= 1
            A += 1
            ans[2][0] += 1
        else:
            print('No')
            return
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
        
    if B > 0 or C > 0:
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
    else:
        if ab > 0 and A > 0:
            A -= 1
            B += 1
            ab -= 1
            ans[0][1] += 1
        elif ca > 0 and A > 0:
            A -= 1
            C += 1
            ca -= 1
            ans[2][0] += 1
        else:
            print('No')
            return
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
                
    if C > 0 or A > 0:
        ans[2][0] += ca//2
        ans[2][1] += ca//2
        ca %= 2
    else:
        if cb > 0 and B > 0:
            B -= 1
            C += 1
            cb -= 1
            ans[1][1] += 1
        elif ab > 0 and B > 0:
            B -= 1
            A += 1
            ab -= 1
            ans[0][0] += 1
        else:
            print('No')
            return
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
    
    if ab > 0 and bc > 0 and ca > 0:
        if A > 0 or B > 0 or C > 0:
            ans[0][1] += 1
            ans[1][1] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ab > 0 and bc > 0:
        if A > 0:
            ans[0][1] += 1
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
            ans[0][0] += 1
        elif B > 1:
            ans[0][0] += 1
            ans[1][1] += 1
        else:
            print('No')
            return
    elif bc > 0 and ca > 0:
        if A > 0:
            ans[2][0] += 1
            ans[1][0] += 1
        elif B > 0:
            ans[1][1] += 1
            ans[2][0] += 1
        elif C > 1:
            ans[1][0] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ca > 0 and ab > 0:
        if C > 0:
            ans[2][0] += 1
            ans[0][1] += 1
        elif B > 0:
            ans[1][0] += 1
            ans[2][0] += 1
        elif A > 1:
            ans[0][1] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    
    elif ab > 0:
        if A > 0:
            ans[0][1] += 1
        elif B > 0:
            ans[0][0] += 1
        else:
            print('No')
            return
    
    elif bc > 0:
        if B > 0:
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
        else:
            print('No')
            return
    
    elif ca > 0:
        if A > 0:
            ans[2][1] += 1
        elif C > 0:
            ans[2][0] += 1
        else:
            print('No')
            return
    

    print('Yes')
    for s in choice:
        if s == 'AB':
            if ans[0][0] > 0:
                print('A')
                ans[0][0] -= 1
            elif ans[0][1] > 0:
                print('B')
        elif s == 'BC':
            if ans[1][0] > 0:
                print('B')
                ans[1][0] -= 1
            else:
                print('C')
        else:
            if ans[2][0] > 0:
                print('C')
                ans[2][0] -= 1
            else:
                print('A')

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import deque
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
ans = []
if (a==0 and  b==0 and c==1) or (a==0 and  b==1 and c==0) or (a==1 and  b==0 and c==0):
    for i in range(n):
        if s[i] == "AB":
            if a >= 1:
                a -= 1
                b += 1
                ans.append("B")
            elif b >= 1:
                a += 1
                b -= 1
                ans.append("A")
            else:
                print("No")
                exit()
        elif s[i] == "AC":
            if a >= 1:
                a -= 1
                c += 1
                ans.append("C")
            elif c >= 1:
                a += 1
                c -= 1
                ans.append("A")
            else:
                print("No")
                exit()
        else:
            if b >= 1:
                b -= 1
                c += 1
                ans.append("C")
            elif c >= 1:
                b += 1
                c -= 1
                ans.append("B")
            else:
                print("No")
                exit()
    print("Yes")
    for i in ans:
        print(i)
    exit()

if (a==0 and  b==1 and c==1) or (a==1 and  b==0 and c==1) or (a==1 and  b==1 and c==0):
    que = deque([[a, b, c, []]])
    for i in range(n):
        if not que:
            print("No")
            exit()
        len_que = len(que)
        for _ in range(len_que):
            a, b, c, ans = que.popleft()
            if s[i] == "AB":
                if a>=1 and b==0:
                    a -= 1
                    b += 1
                    ans.append("B")
                elif b>=1 and a==0:
                    a += 1
                    b -= 1
                    ans.append("A")
                elif a==0 and b==0: continue
                else:
                    que.append([a+1, b-1, c, ans.append("A")])
                    que.append([a-1, b+1, c, ans.append("B")])
                    continue
            elif s[i] == "AC":
                if a>=1 and c==0:
                    a -= 1
                    c += 1
                    ans.append("C")
                elif c>=1and a==0:
                    a += 1
                    c -= 1
                    ans.append("A")
                elif a==0 and c==0: continue
                else:
                    que.append([a+1, b, c-1, ans.append("A")])
                    que.append([a-1, b, c+1, ans.append("C")])
                    continue
            else:
                if b >= 1 and c==0:
                    b -= 1
                    c += 1
                    ans.append("C")
                elif c >= 1and b==0:
                    b += 1
                    c -= 1
                    ans.append("B")
                elif b==0 and c==0: continue  
                else:
                    que.append([a, b+1, c-1, ans.append("B")])
                    que.append([a, b-1, c+1, ans.append("C")])
                    continue
            que.append([a, b, c, ans])
    a, b, c, ans = que.popleft()
    print("Yes")
    for i in ans:
        print(i)
    exit()

if a==0 and  b==0:
    if s[0] == "AB":
        print("No")
        exit()
elif a==0 and  c==0:
    if s[0] == "AC":
        print("No")
        exit()
elif b==0 and  c==0:
    if s[0] == "BC":
        print("No")
        exit()

print("Yes")
for i in range(n):
    if s[i] == "AB":
        if a >= b:
            a -= 1
            b += 1
            print("B")
        else:
            a += 1
            b -= 1
            print("A")
    elif s[i] == "AC":
        if a >= c:
            a -= 1
            c += 1
            print("C")
        else:
            a += 1
            c -= 1
            print("A")
    else:
        if b >= c:
            b -= 1
            c += 1
            print("C")
        else:
            b += 1
            c -= 1
            print("B")

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int, input().split())
op = [input() for i in range(n)]

val=[a,b,c]
flag=True
ansls=list()

for i in range(n):
    s1=ord(op[i][0])-ord('A')
    s2=ord(op[i][1])-ord('A')
    if val[s1]>val[s2]:
        ansls.append(s2)
        val[s2]+=1
        val[s1]-=1
    elif val[s1]<val[s2]:
        ansls.append(s1)
        val[s1]+=1
        val[s2]-=1
    else:
        s1n = ord(op[i+1][0])-ord('A')
        s2n = ord(op[i+1][1])-ord('A')
        nxt = set([s1,s2]) & set([s1n,s2n])
        nxt = list(nxt)[0]
        ansls.append(nxt)
        val[nxt]+=1
        if nxt==s1:
            val[s2]-=1
        else:
            val[s1]-=1
    if min(val)<0:
        flag=False
        break

if flag:
    print('Yes')
    for i in range(n):
        print(chr(ansls[i]+ord('A')))
else:
    print('No')

Submission status: Runtime Error

Solution code:
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
# if s.count(0) <= 1 and sum(s) >= 3:
#     for i,t in enumerate(T):
#         nxt = [0,1,2]
#         nxt.remove(t)
#         if s[nxt[0]] < s[nxt[1]]:
#             ans[i] = ABC[nxt[0]]
#             s[nxt[0]] += 1
#             s[nxt[1]] -= 1
#         else:
#             ans[i] = ABC[nxt[1]]
#             s[nxt[0]] -= 1
#             s[nxt[1]] += 1
#     print('Yes')
#     print('\n'.join(ans))
if s.count(0) == 1 and s.count(1) == 2:
    now = s.index(0)
    i = 0
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        s[3] = 0
        if T[i] == now:
            for j in range(3):
                if j not in [now,T[i]]:ans[i] = ABC[j]
        else:
            ans[i] = ABC[now]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


def yes():
    print("Yes")  # type: str


def no():
    print("No")  # type: str


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    ABC = [A, B, C]
    ans = []
    d = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
    # print(ABC)
    for i in range(N):
        j, k = d[s[i]]
        if ABC[j] == ABC[k] == 0:
            no()
            return
        if ABC[j] == ABC[k] == 1:
            if i != N-1:
                if j not in s[i+1]:
                    j, k = k, j
                    ABC[j] += 1
                    ABC[k] -= 1
                    ans.append("ABC"[j])
        elif ABC[j] > ABC[k]:
            ABC[j] -= 1
            ABC[k] += 1
            ans.append("ABC"[k])
        else:
            ABC[j] += 1
            ABC[k] -= 1
            ans.append("ABC"[j])

    yes()
    for a in ans:
        print(a)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, A, B, C, s)


if __name__ == '__main__':
    main()