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:
n,a,b,c=map(int,input().split())
ans=[]
for i in range(n):
    s=input()
    if s=="AB":
        if a==0:
            a+=1
            b-=1
        elif b==0:
            a-=1
            b+=1
        elif a>b:
            a-=1
            b+=1
            ans.append("A")
        else:
            a+=1
            b-=1
            ans.append("B")
    elif s=="AC":
        if a==0:
            a+=1
            c-=1
        elif c==0:
            a-=1
            c+=1
        elif a>c:
            a-=1
            c+=1
            ans.append("A")
        else:
            a+=1
            c-=1
            ans.append("C")
    else:
        if b==0:
            b+=1
            c-=1
        elif c==0:
            b-=1
            c+=1
        elif b>c:
            b-=1
            c+=1
            ans.append("B")
        else:
            b+=1
            c-=1
            ans.append("C")
    if a<0 or b<0 or c<0:
        ans.clear()
        break
if len(ans)==0:
    print("No")
else:
    print("Yes")
    for i in range(n):
        print(ans[i])

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())

s=[input() for _ in range(n)]

#不可能な場合
if a+b+c==0:
    print("No")
    exit()

result=[]

abc=[a,b,c]
order=[]

def fukasa(order,abc,i):
    #print(order,abc,i)
    global n,s
    if -1 in abc:
        return 0

    if i== n:
        print("Yes")
        for item in order:
            print(item)
        exit()
    else:
        if s[i]=="AB":
            for index,item in enumerate([[1,-1,0],[-1,1,0]]):
                fukasa(order+[s[i][index]],[abc[0]+item[0],abc[1]+item[1],abc[2]+item[2]],i+1)
        if s[i]=="AC":
            for index,item in enumerate([[1,0,-1],[-1,0,1]]):
                fukasa(order+[s[i][index]],[abc[0]+item[0],abc[1]+item[1],abc[2]+item[2]],i+1)
        if s[i]=="BC":
            for index,item in enumerate([[0,1,-1],[0,-1,1]]):
                fukasa(order+[s[i][index]],[abc[0]+item[0],abc[1]+item[1],abc[2]+item[2]],i+1)

fukasa([],[a,b,c],0)
print("No")

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
s = [str(input()) for i in range(n)]
lis = [a,b,c]
ans = ""
for i in range(n-1):
    if lis[ord(s[i][0]) - ord("A")] * lis[ord(s[i][1]) - ord("A")] == 0:
        print("No")
        exit()
    if s[i] == s[i+1]:
        if lis[ord(s[i][0])] == 0:
            ans += s[i][0]
            lis[ord(s[i][0])] += 1
            lis[ord(s[i][1])] -= 1
        else:
            ans += s[i][1]
            lis[ord(s[i][0])-ord("A")] -= 1
            lis[ord(s[i][1])-ord("A")] += 1
    else:
        if s[i] == "AB":
            if s[i+1] == "AC":
                if lis[1] == 0:
                    ans += "B"
                    lis[1] += 1
                    lis[0] -= 1
                else:
                    ans += "A"
                    lis[0] += 1
                    lis[1] -= 1
            else:
                if lis[0] == 0:
                    ans += "A"
                    lis[0] += 1
                    lis[1] -= 1
                else:
                    ans += "B"
                    lis[0] -= 1
                    lis[1] += 1
        elif s[i] == "AC":
            if s[i+1] == "AB":
                if lis[2] == 0:
                    ans += "C"
                    lis[2] += 1
                    lis[0] -= 1
                else:
                    ans += "A"
                    lis[0] += 1
                    lis[2] -= 1
            else:
                if lis[0] == 0:
                    ans += "A"
                    lis[0] += 1
                    lis[2] -= 1
                else:
                    ans += "C"
                    lis[0] -= 1
                    lis[2] += 1
        else:
            if s[i+1] == "AB":
                if lis[2] == 0:
                    ans += "C"
                    lis[2] += 1
                    lis[1] -= 1
                else:
                    ans += "B"
                    lis[2] -= 1
                    lis[1] += 1
            else:
                if lis[1] == 0:
                    ans += "B"
                    lis[1] += 1
                    lis[2] -= 1
                else:
                    ans += "C"
                    lis[2] += 1
                    lis[1] -= 1
if lis[ord(s[-1][0])-ord("A")] == 0:
    if lis[ord(s[-1][1])-ord("A")] == 0:
        print("No")
        exit()
    else:
        ans += s[-1][0]
else:
    ans += s[-1][1]
print("Yes")
for i in range(n):
    print(ans[i])

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n,a,b,c=map(int,input().split())
s_=[input() for i in range(n)]
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if a>b:
            a,b=a-1,b+1
            ans+="B"
        else:
            a,b=a+1,b-1
            ans+="A"
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if a>c:
            a,c=a-1,c+1
            ans+="C"
        else:
            a,c=a+1,c-1
            ans+="A"
        if min(a,c)==-1:
            print("No")
            break
    else:
        if b>c:
            b,c=b-1,c+1
            ans+="C"
        else:
            b,c=b+1,c-1
            ans+="B"
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())
ans=[]
ks = [input() for _ in range(n)]
for i in range(n):
  k = k[0]
  if k=="AB":
    if a==0 and b==0:
      print("No")
      quit()
    else:
      if a>b:
        a -= 1
        b+=1
        ans.append("B")
      else:
        if a == 1 and b == 1 and len(ks) > 1:
          if ks[1] == "AC":
            b-=1
            a+=1
            ans.append("A")
          else:
            a -= 1
            b+=1
            ans.append("B")
      else: 
          b-=1
          a+=1
          ans.append("A")
  if k=="BC":
    if c==0 and b==0:
      print("No")
      quit()
    else:
      if c>b:
        c -= 1
        b+=1
        ans.append("B")
      else:
        if c == 1 and b == 1 and len(ks) > 1:
          if ks[1] == "AC":
            b-=1
            c+=1
            ans.append("C")
          else:
            c -= 1
            b+=1
            ans.append("B")
      else: 
          b-=1
          c+=1
          ans.append("C")
  if k=="AC":
    if a==0 and c==0:
      print("No")
      quit()
    else:
      if a>c:
        a -= 1
        c+=1
        ans.append("C")
      else:
        if a == 1 and c == 1 and len(ks) > 1:
          if ks[1] == "BC":
            a-=1
            c+=1
            ans.append("C")
          else:
            c -= 1
            a+=1
            ans.append("A")
      else: 
          c-=1
          a+=1
          ans.append("A")
  del(ks[0])
print("Yes")
for i in ans:
  print(i)

Submission status: Runtime Error

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""

from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def floor(a, b):
    """floor(a/b) 整数値を返す"""
    c = (a - a % b)/b
    return int(c)


def main():
    n, a, b, c = wi()
    s = []
    for i in range(n):
        s.append(input())
    s.append('na')
    ans = []
    if a + b + c == 0:
        print('No')
        sys.exit()
    if a + b + c == 1:
        for i in range(n):
            if s[i] == 'AB':
                if a == 0 and b == 1:
                    a += 1
                    b -= 1
                    ans.append('A')
                elif a == 1 and b == 0:
                    a -= 1
                    b += 1
                    ans.append('B')
                elif a == 0 and b == 0:
                    print('No')
                    sys.exit()

            elif s[i] == 'BC':
                if b == 0 and c == 1:
                    b += 1
                    c -= 1
                    ans.append('B')
                elif b == 1 and c == 0:
                    b -= 1
                    c += 1
                    ans.append('C')
                elif b == 0 and c == 0:
                    print('No')
                    sys.exit()
            elif s[i] == 'AC':
                if a == 0 and c == 1:
                    a += 1
                    c -= 1
                    ans.append('A')
                elif a == 1 and c == 0:
                    a -= 1
                    c += 1
                    ans.append('C')
                elif a == 0 and c == 0:
                    print('No')
                    sys.exit()

    elif a + b + c == 2:
        for i in range(n):
            if s[i] == 'AB':
                if a == 1 and b == 1:
                    if s[i+1] == 'BC':
                        b += 1
                        a -= 1
                        ans.append('B')
                    elif s[i+1] == 'AC':
                        a += 1
                        b -= 1
                        ans.append('A')
                    elif s[i+1] == 'AB':
                        a += 1
                        b -= 1
                        ans.append('A')
                    else:
                        a += 1
                        b -= 1
                        ans.append('A')
                        
                elif a == 2 and b == 0:
                    a -= 1
                    b += 1
                    ans.append('B')
                elif a == 0 and b == 2:
                    a += 1
                    b -= 1
                    ans.append('A')

            elif s[i] == 'BC':
                if b == 1 and c == 1:
                    if s[i+1] == 'BC':
                        b += 1
                        c -= 1
                        ans.append('B')
                    elif s[i+1] == 'AC':
                        c += 1
                        b -= 1
                        ans.append('C')
                    elif s[i+1] == 'AB':
                        b += 1
                        c -= 1
                        ans.append('B')
                    else:
                        b += 1
                        c -= 1
                        ans.append('B')
                elif b == 2 and c == 0:
                    b -= 1
                    c += 1
                    ans.append('C')
                elif b == 0 and c == 2:
                    b += 1
                    c -= 1
                    ans.append('B')

            elif s[i] == 'AC':
                if a == 1 and c == 1:
                    if s[i+1] == 'BC':
                        a -= 1
                        c += 1
                        ans.append('C')
                    elif s[i+1] == 'AC':
                        c += 1
                        a -= 1
                        ans.append('C')
                    elif s[i+1] == 'AB':
                        a += 1
                        c -= 1
                        ans.append('A')
                    else:
                        b += 1
                        c -= 1
                        ans.append('B')
                elif a == 2 and c == 0:
                    a -= 1
                    c += 1
                    ans.append('C')
                elif a == 0 and c == 2:
                    a += 1
                    c -= 1
                    ans.append('A')

    else:
        for i in range(n):
            if s[i] == 'AB':
                if a == 0 and b >= 1:
                    a += 1
                    b -= 1
                    ans.append('A')
                elif b == 0 and a >= 1:
                    a -= 1
                    b += 1
                    ans.append('B')
                else:
                    a += 1
                    b -= 1
                    ans.append('A')
            elif s[i] == 'BC':
                if b == 0 and c >= 1:
                    b += 1
                    c -= 1
                    ans.append('B')
                elif c == 0 and b >= 1:
                    b -= 1
                    c += 1
                    ans.append('C')
                else:
                    b += 1
                    c -= 1
                    ans.append('C')
            elif s[i] == 'AC':
                if a == 0 and c >= 1:
                    a += 1
                    c -= 1
                    ans.append('A')
                elif c == 0 and a >= 1:
                    a -= 1
                    c += 1
                    ans.append('A')
                else:
                    a += 1
                    c -= 1
                    ans.append('A')
    print('Yes')
    for i in range(n):
        print(ans[i])



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
n,a,b,c=map(int,input().split())
slist=[]
for i in range(n):
    slist.append(input())
output=[]
ynflag=True

def abc1(s):
    global a
    global b
    global c
    global output
    global ynflag

    
    if s == "AB":
        if a ==1:
            a -=1
            b +=1
            output.append("B")
            return True
        elif b == 1:
            a +=1
            b -=1
            output.append("A")
            return True
        else:
            return False
    if s == "AC":
        if a ==1:
            a -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            a +=1
            c -=1
            output.append("A")
            return True
        else:
            return False
    if s == "BC":
        if b ==1:
            b -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            b +=1
            c -=1
            output.append("B")
            return True
        else:
            return False

def abc2(i):
    global a
    global b
    global c
    global output
    global slist
    s=slist[i]
    
    if i == len(slist)-1:
        tsugi="DUMMY"
    else:
        tsugi=slist[i+1]
    
    elif s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if tsugi == "BC":
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
        else:
            if a >=b:
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
    elif s == "AC":
        if a + c == 0:
            return False
        elif a == 1 and c == 1 and b == 0:
            if tsugi == "BC":
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
        else:
            if a >=c:
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
    else:
        if b + c == 0:
            return False
        elif b == 1 and c == 1 and a == 0:
            if tsugi == "AC":
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True
        else:
            if b >=c:
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True

if a == 0 and b == 0 and c == 0:
    print("No")
    sys.exit()

else:
    if a+b+c == 1:
        for s in slist:
            ynflag=abc1(s)
            if not ynflag:
                print("No")
                sys.exit()
    else:
        for i in range(n):
            ynflag=abc2(i)
            if not ynflag:
                print("No")
                sys.exit()
    print("Yes")
    for out in output:
        print(out)

Submission status: Runtime Error

Solution code:
import sys
n,a,b,c=map(int,input().split())
slist=[]
for i in range(n):
    slist.append(input())
output=[]
ynflag=True

def abc1(s):
    global a
    global b
    global c
    global output
    global ynflag

    
    if s == "AB":
        if a ==1:
            a -=1
            b +=1
            output.append("B")
            return True
        elif b == 1:
            a +=1
            b -=1
            output.append("A")
            return True
        else:
            return False
    if s == "AC":
        if a ==1:
            a -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            a +=1
            c -=1
            output.append("A")
            return True
        else:
            return False
    if s == "BC":
        if b ==1:
            b -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            b +=1
            c -=1
            output.append("B")
            return True
        else:
            return False

def abc2(i):
    global a
    global b
    global c
    global output
    global slist
    s=slist[i]
    
    if i == len(slist)-1:
        if s =="AB":
            if a > b:
                output.append("B")
                return True
            else:
                output.append("A")
                return True
        elif s =="AC":
            if a > c:
                output.append("C")
                return True
            else:
                output.append("A")
                return True
        else:
            if b > c:
                output.append("C")
                return True
            else:
                output.append("B")
                return True
    
    elif s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if s[i+1] == "BC":
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
        else:
            if a >=b:
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
    elif s == "AC":
        if a + c == 0:
            return False
        elif a == 1 and c == 1 and b == 0:
            if s[i+1] == "BC":
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
        else:
            if a >=c:
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
    else:
        if b + c == 0:
            return False
        elif b == 1 and c == 1 and a == 0:
            if s[i+1] == "AC":
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True
        else:
            if b >=c:
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True

if a == 0 and b == 0 and c == 0:
    print("No")
    sys.exit()

else:
    if a+b+c == 1:
        for s in slist:
            ynflag=abc1(s)
            if not ynflag:
                print("No")
                sys.exit()
    else:
        for i in range(n):
            ynflag=abc2(i)
            if not ynflag:
                print("No")
                sys.exit()
    print("Yes")
    for out in output:
        print(out)

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
from itertools import combinations
def run():
    pri = []
    N, A, B, C = map(int, sysread().split())
    vals = {'A':A, 'B':B, 'C':C}
    S = [sysread() for _ in range(N)]

    pri = []
    for idx, s in enumerate(S):
        if vals[s[0]] == vals[s[1]] and vals[s[0]] == 1:
            if s[0] in S[idx+1]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])
        else:
            if vals[s[0]] <= vals[s[1]]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])

        if not check_vals(vals):
            print('No')
            return None
    print('Yes')
    for st in pri:
        print(st)

def check_vals(vals):
    for v in vals.values():
        if v < 0:
            return False
    return True

if __name__ == "__main__":
    run()

Submission status: Runtime Error

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

a = min(a, 1)
b = min(b, 1)
c = min(c, 1)

ans = []

d = {"A": a, "B": b, "C": c}


def dfs(i):
    if min(d.values()) < 0:
        return

    if i == n:
        print("Yes")
        print(*ans, sep="\n")
        exit()

    k1, k2 = s[i]

    d[k1] += 1
    d[k2] -= 1
    ans.append(k1)
    dfs(i+1)
    d[k1] -= 1
    d[k2] += 1
    ans.pop()

    d[k2] += 1
    d[k1] -= 1
    ans.append(k2)
    dfs(i+1)
    d[k2] -= 1
    d[k1] += 1
    ans.pop()


dfs(0)

print("No")

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
cc = []
for i in range(n):
  ff = input().strip()
  cc.append((ff))
print(cc,cc[0][0],cc[1],cc[1][0],cc[1][1])  
chk = True
ans = []
for num,q in enumerate(cc):
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if a == 1 and b == 1:
      if cc[min(n-1,num+1)][0] == 'A':
        a += 1
        b -= 1
        ans.append('A')
      else:
        a -= 1
        b += 1
        ans.append('B')       
    elif m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if c == 1 and a == 1:
      if cc[min(n-1,num+1)][0] == 'A':
        a += 1
        c -= 1
        ans.append('A')
      else:
        a -= 1
        c += 1
        ans.append('C')       
    if m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if c == 1 and b == 1:
      if cc[min(n-1,num+1)][1] == 'C':
        c += 1
        b -= 1
        ans.append('C')
      else:
        c -= 1
        b += 1
        ans.append('B')       
    if m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
cc = []
for i in range(n):
  ff = input().strip()
  cc.append((ff))
  
chk = True
ans = []
for num,q in enumerate(cc):
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if a == b and b == 1:
      if q[num+1][0] == 'A':
        a += 1
        b -= 1
        ans.append('A')
      else:
        a -= 1
        b += 1
        ans.append('B')       
    elif m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if a == c and a == 1:
      if q[num+1][0] == 'A':
        a += 1
        b -= 1
        ans.append('A')
      else:
        a -= 1
        c += 1
        ans.append('C')       
    if m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if c == b and b == 1:
      if q[num+1][1] == 'C':
        c += 1
        b -= 1
        ans.append('C')
      else:
        c -= 1
        b += 1
        ans.append('B')       
    if m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')