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: Wrong Answer

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

for i in range(n):
    if s[i] == 'AB':
        if a <= b:
            a += 1
            b -= 1
            ans.append('A')
        else:
            a -= 1
            b += 1
            ans.append('B')

    if s[i] == 'AC':
        if a <= c:
            a += 1
            c -= 1
            ans.append('A')
        else:
            a -= 1
            c += 1
            ans.append('C')

    if s[i] == 'BC':
        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:
        flg = False
        break

if flg:
    print('Yes')
    print(*ans, sep='\n')

else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
d = [a,b,c]
q = [input() for i in range(n)]
x = {'A':0,'B':1,'C':2}
y = ['A','B','C']

if d.count(0)==3:
    print('No')
elif d.count(0)==2:
    ans = []
    for i in range(n):
        p = q[i]
        if p=='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 p=='BC':
            if b==1:
                b-=1
                c+=1
                ans.append('C')
            elif c==1:
                c-=1
                b+=1
                ans.append('B')
            else:
                print('No')
                exit()
        else:
            if a==1:
                a-=1
                c+=1
                ans.append('C')
            elif c==1:
                c-=1
                a+=1
                ans.append('A')
            else:
                print('No')
                exit()
    print('Yes')
    print('\n'.join(ans))
elif d.count(0)==1:
    ans = []
    flag = 0
    for i in range(n-1):
        if flag:
            flag = 0
            continue
        s = x[q[i][0]]
        t = x[q[i][1]]
        if d[s]==0:
            d[s]+=1
            d[t]-=1
            ans.append(y[s])
        elif d[t]==0:
            d[s]-=1
            d[t]+=1
            ans.append(y[t])
        else:
            flag = 1
            if q[i]==q[i+1]:
                ans.append(y[s])
                ans.append(y[t])
            elif q[i][0] in q[i+1]:
                ans.append(y[s])
                if q[i][0] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[t]-=1
                    d[x[q[i+1][0]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[t]-=1
                    d[x[q[i+1][1]]]+=1
            else:
                ans.append(y[t])
                if q[i][1] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[s]-=1
                    d[x[q[i+1][0]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[s]-=1
                    d[x[q[i+1][1]]]+=1
    if len(ans)!=n:
        s = x[q[-1][0]]
        t = x[q[-1][1]]
        if d[s]==1:
            ans.append(y[t])
        else:
            ans.append(y[s])
    print('Yes')
    print('\n'.join(ans))
else:
    ans = []
    flag = 0
    for i in range(n-1):
        s = x[q[i][0]]
        t = x[q[i][1]]
        if flag:
            flag = 0
            continue
        if d[0]>0 and d[1]>0 and d[2]>0:
            ans.append(y[s])
            d[s]+=1
            d[t]-=1
            continue
        if d[s]==0:
            d[s]+=1
            d[t]-=1
            ans.append(y[s])
        elif d[t]==0:
            d[s]-=1
            d[t]+=1
            ans.append(y[t])
        else:
            flag = 1
            if q[i]==q[i+1]:
                ans.append(y[s])
                ans.append(y[t])
            elif q[i][0] in q[i+1]:
                ans.append(y[s])
                if q[i][0] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[t]-=1
                    d[x[q[i+1][0]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[t]-=1
                    d[x[q[i+1][1]]]+=1
            else:
                ans.append(y[t])
                if q[i][1] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[s]-=1
                    d[x[q[i+1][0]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[s]-=1
                    d[x[q[i+1][1]]]+=1
    if len(ans) != n:
        s = x[q[-1][0]]
        t = x[q[-1][1]]
        if d[s] >0 :
            ans.append(y[t])
        else:
            ans.append(y[s])
    print('Yes')
    print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    m = []
    for i in range(N):
        s = input()
        if s == "AB":
            if A >= B:
                A -= 1 
                B += 1
                m.append("B")
            else:
                A += 1 
                B -= 1
                m.append("A")
        elif s == "BC":
            if B >= C:
                B -= 1 
                C += 1
                m.append("C")
            else:
                B += 1 
                C -= 1
                m.append("B")
        else:
            if C >= A:
                C -= 1 
                A += 1
                m.append("A")
            else:
                C += 1 
                A -= 1
                m.append("C")
    if min((A, B, C)) < 0:
        print("No")
    else:
        print("Yes")
        for p in m:
            print(p)
            
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
list = []

for i in range(0, N):
    cl = input()
    list.append(cl)

ex_list = []
yes = True
for n in range(0, N):
    if list[n] == 'AB':
        if A > B:
            A -= 1
            B += 1
            ex_list.append('B')
        elif A < B:
            A += 1
            B -= 1
            ex_list.append('A')
        elif A == B:
            if n < N - 1:
                if list[n+1] == 'BC':
                    A -= 1
                    B += 1
                    ex_list.append('B')
                elif list[n+1] == 'AC':
                    A += 1
                    B -= 1
                    ex_list.append('A')
            else:
                A -= 1
                B += 1
                ex_list.append('B')

    elif list[n] == 'AC':
        if A > C:
            A -= 1
            C += 1
            ex_list.append('C')
        elif A < C:
            A += 1
            C -= 1
            ex_list.append('A')
        elif A == C:
            if n < N - 1:
                if list[n + 1] == 'AB':
                    A += 1
                    C -= 1
                    ex_list.append('A')
                elif list[n + 1] == 'BC':
                    A -= 1
                    C += 1
                    ex_list.append('C')
            else:
                A += 1
                C -= 1
                ex_list.append('A')


    elif list[n] == 'BC':
        if B > C:
            B -= 1
            C += 1
            ex_list.append('C')
        elif B < C:
            B += 1
            C -= 1
            ex_list.append('B')
        elif B == C:
            if n < N - 1:
                if list[n + 1] == 'AB':
                    B += 1
                    C -= 1
                    ex_list.append('B')
                elif list[n + 1] == 'AC':
                    B -= 1
                    C += 1
                    ex_list.append('C')
            else:
                B -= 1
                C += 1
                ex_list.append('C')

    if A < 0 or B < 0 or C < 0:
        yes = False

    if not yes:
        break

if yes:
    print('Yes')
    for element in ex_list:
        print(element)
else:
    print('No')

Submission status: Wrong Answer

Solution code:

N,A,B,C = map(int,input().split())


def score(start, queries):
    def rec(nums, d):
        if d >= len(queries):
            return min(nums)
        i,j = queries[d]
        nums[i] += 1
        nums[j] -= 1
        x = rec(nums, d+1)
        nums[i] -= 2
        nums[j] += 2
        y = rec(nums, d+1)
        nums[i] += 1
        nums[j] -= 1
        return max(x,y)
    return rec(start, 0)



Q = []
for _ in range(N):
    s = input()
    if s == 'AB':
        i,j = 0,1
    elif s == 'BC':
        i,j = 1,2
    else:
        i,j = 2,0

    Q.append((i,j))

nums = [A,B,C]
res = []
flag = True
for q,(i,j) in enumerate(Q):
    queries = Q[q:min(q+4, len(Q))]
    nums[i] += 1
    nums[j] -= 1
    x = score(nums, queries)
    nums[i] -= 2
    nums[j] += 2
    y = score(nums, queries)
    if x > y:
        res.append(i)
        nums[i] += 1
        nums[j] -= 1
    else:
        res.append(j)
        nums[i] -= 1
        nums[j] += 1
    if max(x,y) < 0:
        flag = False
        break


if not flag:
    print('No')
else:
    print('Yes')
    res = tuple(str(chr(v+ord('A'))) for v in res)
    print(*res,sep='\n')

Submission status: Wrong Answer

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

def output(a_ab, b_ab, a_ac, c_ac, b_bc, c_bc):
    for round in rounds:
        if round == "AB":
            if a_ab > 0:
                print("A")
                a_ab -= 1
            else:
                print("B")

        if round == "AC":
            if a_ac > 0:
                print("A")
                a_ac -= 1
            else:
                print("C")

        if round == "BC":
            if b_bc > 0:
                print("B")
                b_bc -= 1
            else:
                print("C")
                

ab = rounds.count("AB")
bc = rounds.count("BC")
ac = rounds.count("AC")

for a_ab in range(ab + 1):
    b_ab = ab - a_ab
    a_rest = a + a_ab - b_ab
    b_rest = b + b_ab - a_ab

    a_ac = max(0, (ac - a_rest + 1)//2)
    c_ac = ac - a_ac
    
    b_bc = max(0, (bc - b_rest + 1)//2)
    c_bc = bc - b_bc
    
    if a + a_ab - b_ab + a_ac - c_ac >= 0 and b + b_ab - a_ab + b_bc - c_bc >= 0 and c + c_bc - b_bc + c_ac - a_ac >= 0:
        print("Yes")
        output(a_ab, b_ab, a_ac, c_ac, b_bc, c_bc)
        exit()

print("No")

Submission status: Wrong Answer

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

else:
    print "No"

Submission status: Wrong Answer

Solution code:
def calc():
  n,a,b,c=map(int,input().split())
  arr=[input() for _ in range(n)]
  ans=[]
  for i in range(n):
    if arr[i]=='AB':
      if a>b:
        a-=1
        b+=1
        ans.append('B')
      elif a<b:
        a+=1
        b-=1
        ans.append('A')
      elif a==b:
        if a==0:
          print('No')
          break
        else:
          if i!=n-1:
            if arr[i+1]=='AB':
              a+=1
              b-=1
              ans.append('A')
            elif arr[i+1]=='BC':
              a-=1
              b+=1
              ans.append('B')
            elif arr[i+1]=='AC':
              a+=1
              b-=1
              ans.append('C')
          else:
            a+=1
            b-=1
            ans.append('A')
    elif arr[i]=='AC':
      if a>c:
        a-=1
        c+=1
        ans.append('C')
      elif a<c:
        a+=1
        c-=1
        ans.append('A')
      elif a==c:
        if a==0:
          print('No')
          break
        else:
          if i!=n-1:
            if arr[i+1]=='AC':
              a+=1
              c-=1
              ans.append('A')
            elif arr[i+1]=='AB':
              a+=1
              c-=1
              ans.append('A')
            elif arr[i+1]=='BC':
              a-=1
              c+=1
              ans.append('C')
          else:
            a+=1
            c-=1
            ans.append('A')
    elif arr[i]=='BC':
      if b>c:
        b-=1
        c+=1
        ans.append('C')
      elif b<c:
        b+=1
        c-=1
        ans.append('B')
      elif b==c:
        if b==0:
          print('No')
          break
        else:
          if i!=n-1:
            if arr[i+1]=='BC':
              b+=1
              c-=1
              ans.append('B')
            elif arr[i+1]=='AB':
              b+=1
              c-=1
              ans.append('B')
            elif arr[i+1]=='AC':
              b-=1
              c+=1
              ans.append('C')
          else:
            b+=1
            c-=1
            ans.append('B')
  else:
    print('Yes')
    for i in range(n):
      print(ans[i])
calc()

Submission status: Wrong Answer

Solution code:
#F
N,A,B,C = map(int,input().split())
S = [str(input()) for i in range(N)]
Ac = [0]*N
Bc = [0]*N
Cc = [0]*N

for i,s in enumerate(S):
    x,y = s
    if x == "A" or y == "A":
        Ac[i]+=1
    if x == "B" or y == "B":
        Bc[i]+=1
    if x == "C" or y == "C":
        Cc[i]+=1
        
Ac = Ac[::-1]
Bc = Bc[::-1]
Cc = Cc[::-1]
    
flag = True
ans = []
for i,s in enumerate(S):
    #print(i,s)
    if s == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        elif A == 0:
            A+=1
            B-=1
            ans.append("A")
        elif B == 0:
            A-=1
            B+=1
            ans.append("B")
        else:
            if A-Ac[i] >= B-Bc[i]:
                A-=1
                B+=1
                ans.append("B")
            else:
                A+=1
                B-=1
                ans.append("A")
    if s == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        elif A == 0:
            A+=1
            C-=1
            ans.append("A")
        elif C == 0:
            A-=1
            C+=1
            ans.append("C")
        else:
            if A-Ac[i] >= C-Cc[i]:
                C-=1
                C+=1
                ans.append("C")
            else:
                A+=1
                C-=1
                ans.append("A")
    if s == "BC":
        if C == 0 and B == 0:
            flag = False
            break
        elif C == 0:
            C+=1
            B-=1
            ans.append("C")
        elif B == 0:
            C-=1
            B+=1
            ans.append("B")
        else:
            if C-Cc[i] >= B-Bc[i]:
                C-=1
                B+=1
                ans.append("B")
            else:
                C+=1
                B-=1
                ans.append("C")
                
if flag:
    print("Yes")
    for a in ans:
        print(a)
else:
    print("No")

Submission status: Wrong Answer

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]
    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 dp[0] == deque() and dp[1] == deque():
                        print("No")
                        return
                    if len(dp[0]) >  len(dp[1]):
                        ans[dp[0].popleft()] = "A"
                        ans[i] = "B"
                    elif  len(dp[0]) <  len(dp[1]):
                        ans[dp[1].popleft()] = "B"
                        ans[i] = "A"
                    else:
                        if l[i][0] < l[i][1]:
                            ans[dp[0].popleft()] = "A"
                            ans[i] = "B"
                        else:
                            ans[dp[1].popleft()] = "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 dp[0] == deque() and dp[2] == deque():
                        print("No")
                        return
                    if len(dp[0]) > len(dp[2]):
                        ans[dp[0].popleft()] = "A"
                        ans[i] = "C"
                    elif  len(dp[0]) < len(dp[2]):
                        ans[dp[2].popleft()] = "C"
                        ans[i] = "A"
                    else:
                        if l[i][0] < l[i][2]:
                            ans[dp[0].popleft()] = "A"
                            ans[i] = "C"
                        else:
                            ans[dp[2].popleft()] = "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 a == 0:
                    if dp[1] == deque() and dp[2] == deque():
                        print("No")
                        return
                    if  len(dp[1]) > len(dp[2]):
                        ans[dp[1].popleft()] = "B"
                        ans[i] = "C"
                    elif   len(dp[1]) < len(dp[2]):
                        ans[dp[2].popleft()] = "C"
                        ans[i] = "B"
                    else:
                        if l[i][1] < l[i][2]:
                            ans[dp[1].popleft()] = "B"
                            ans[i] = "C"
                        else:
                            ans[dp[2].popleft()] = "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: Wrong Answer

Solution code:

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A,B,C=MI()
    S=[]
    SS=A+B+C
    for i in range(N):
        s=input()
        S.append(s)
    
    ans=[]
    flag=1
    for i in range(N):
        s=S[i]
        
        if s=="AB":
            if A==0:
                A+=1
                B-=1
                ans.append("A")
            elif B==0:
                A-=1
                B+=1
                ans.append("B")
            elif A==SS-1:
                A-=1
                B+=1
                ans.append("B")
            elif B==SS-1:
                A+=1
                B-=1
                ans.append("A")
            else:
                if 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
                ans.append("A")
            elif C==0:
                A-=1
                C+=1
                ans.append("C")
            elif A==SS-1:
                A-=1
                C+=1
                ans.append("C")
            elif C==SS-1:
                A+=1
                C-=1
                ans.append("A")
            else:
                if 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
                ans.append("B")
            elif C==0:
                B-=1
                C+=1
                ans.append("C")
            elif B==SS-1:
                B-=1
                C+=1
                ans.append("C")
            elif B==SS-1:
                A+=1
                B-=1
                ans.append("A")
            else:
                if C<=B:
                    C+=1
                    B-=1
                    ans.append("C")
                else:
                    C-=1
                    B+=1
                    ans.append("B")
                
        if A<0 or B<0 or C<0:
            flag=-1
            break
                
        
        
    if flag==-1:
        print("No")
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N, A, B, C = map(int, input().split())
s = [input().rstrip() for _ in range(N)]
ans = []
for i in range(N):
    if s[i] == 'AB':
        if A == B == 0:
            print('No')
            exit()
        if i < N - 1 and A == B == 1 and C == 0 and s[i+1] != 'AB':
            if s[i+1] == 'BC':
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
        else:
            if A > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
    if s[i] == 'BC':
        if B == C == 0:
            print('No')
            exit()
        if i < N - 1 and B == C == 1 and A == 0 and s[i+1] != 'BC':
            if s[i+1] == 'AB':
                B += 1
                C -= 1
                ans.append('B')
            else:
                B -= 1
                C += 1
                ans.append('C')
        else:
            if B > C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
    if s[i] == 'AC':
        if A == C == 0:
            print('No')
            exit()
        if i < N - 1 and A == C == 1 and B == 0 and s[i+1] != 'AC':
            if s[i+1] == 'AB':
                A += 1
                B -= 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
        else:
            if A > C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')
print('Yes')
print(*ans, sep='\n')