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())
ss = [input()for i in range(n)]
ok = True
ans = []
for s in ss:
    if s == 'AB':
        if a == 0 and b == 0:
            ok = False
            break
        
        if a > b:
            b +=1
            a -= 1
            ans.append('B')
        else:
            b -= 1
            a += 1
            ans.append('A')
            
    elif s == 'AC':
        if a == 0 and c == 0:
            ok = False
            break
        
        if a > c:
            c +=1
            a -= 1
            ans.append('C')
        else:
            c -= 1
            a += 1
            ans.append('A')
            
        
    else:
        if c == 0 and b == 0:
            ok = False
            break
        
        if c > b:
            b +=1
            c -= 1
            ans.append('B')
        else:
            b -= 1
            c += 1
            ans.append('C')
if ok:
    print('Yes')
    for x in ans:
        print(x)
else:print('No')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
N,A,B,C = map(int, input().split())
list_choice = [input() for i in range(N)]
list_choice.append(list_choice[-1])

def operation(A,B,C,S,nextS):
  if S=='AB':
    if max(A,B)==0:
      return ('No','',0,0,0)
    elif A==1 and B==1:
      if nextS=='AB' or nextS=='AC':
        return ('Yes','A',A+1,B-1,C)
      else:
        return ('Yes','B',A-1,B+1,C)  
    elif A>=B:
      return ('Yes','B',A-1,B+1,C)
    else: # A<B
      return ('Yes','A',A+1,B-1,C)
  elif S=='AC':
    if max(A,C)==0:
      return ('No','',0,0,0)
    elif A==1 and C==1:
      if nextS=='AB' or nextS=='AC':
        return ('Yes','A',A+1,B,C-1)
      else:
        return ('Yes','C',A-1,B,C+1)  
    elif A>=C:
      return ('Yes','C',A-1,B,C+1)
    else: # A<C
      return ('Yes','A',A+1,B,C-1)
  else: # S=='BC'
    if max(A,C)==0:
      return ('No','',0,0,0)
    elif B==1 and C==1:
      if nextS=='AB' or nextS=='BC':
        return ('Yes','B',A,B+1,C-1)
      else:
        return ('Yes','C',A,B-1,C+1)  
    elif B>=C:
      return ('Yes','C',A,B-1,C+1)
    else: # B<C
      return ('Yes','B',A,B+1,C-1)
  
def func(A,B,C,list_choice):
  list_add = []
  for i in range(N):
    answer,add,A,B,C = operation(A,B,C,list_choice[i],list_choice[i+1])
    if answer=='No':
      return ('No', [])
    else:
      list_add.append(add)
  return ('Yes',list_add)
  
answer, list_add = func(A,B,C,list_choice)
print(answer)
if answer == 'Yes':
  for add in list_add:
    print(add)

Submission status: Wrong Answer

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

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        omake = list(set(list(before)) - set(list(s)))[0]
        for i in range(co):
            if i % 2 == 0:
                yusen.append(target)
            else:
                yusen.append(omake)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for s in sl:
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if yusen == "A":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if yusen == "B":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if yusen == "C":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
S = []
for _ in range(N):
    S.append(input())
ans = []
for s in S:
    if s == "AB":
        if A >= B:
            A -= 1
            B += 1
            ans.append("B")
        else:
            B -= 1
            A += 1
            ans.append("A")
    elif s == "AC":
        if A >= C:
            A -= 1
            C += 1
            ans.append("C")
        else:
            C -= 1
            A += 1
            ans.append("A")
    elif s == "BC":
        if B >= C:
            B -= 1
            C += 1
            ans.append("C")
        else:
            C -= 1
            B += 1
            ans.append("B")

if not all(map(lambda x: x >= 0, (A, B, C))):
    print("No")
else:
    print("Yes")
    for a in ans:
        print(a)

Submission status: Wrong Answer

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

N, A, B, C = map(int, input().split())
x = y = z = 0
ans = []
aa = bb = cc = 0
for _ in range(N):
    s = input().rstrip()
    if s == 'AB':
        if A == B == 0:
            print('No')
            exit()
        else:
            if A > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
    if s == 'BC':
        if B == C == 0:
            print('No')
            exit()
        else:
            if B > C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
    if s == 'AC':
        if A == C == 0:
            print('No')
            exit()
        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')

Submission status: Wrong Answer

Solution code:
temp=[]
x=input().split(" ")
number=int(x[0])
a=int(x[1])
b=int(x[2])
c=int(x[3])
i=0
result=""
while i < number:
    temp.append(input())
    if temp[i]=="AB":
        if a<=b:
            a+=1
            b-=1
            result+="A\n"
        else:
            a-=1
            b+=1
            result += "B\n"
    elif temp[i]=="BC":
        if b<=c:
            b+=1
            c-=1
            result += "B\n"
        else:
            b-=1
            c+=1
            result += "C\n"
    else:
        if c<=a:
            c+=1
            a-=1
            result += "C\n"
        else:
            c-=1
            a+=1
            result += "A\n"
    i = i + 1
toa=0-a
tob=0-b
toc=0-c
if a<0 or b<0 or c<0:
    result_1=result.split("\n")
    if toa>0 and b>=0 and c>=0:
        i = 0
        newresult = ""
        while i < number:
            if temp[i] == "AB":
                if result_1[i]=='B' and b>0 and toa>0:
                    b-=1
                    toa-=1
                    newresult+="A\n"
                else:
                    newresult+="B\n"
            elif temp[i] == "BC":
                    newresult += result_1[i]+"\n"
            else:
                if result_1[i] == 'C' and c > 0 and toa > 0:
                    c -= 1
                    toa -= 1
                    newresult += "A\n"
                else:
                    newresult += "C\n"
            i = i + 1
    elif tob>0 and a>=0 and c>=0:
        i = 0
        newresult = ""
        while i < number:
            if temp[i] == "AB":
                if result_1[i]=='A' and a>0 and tob>0:
                    a-=1
                    tob-=1
                    newresult+="B\n"
                else:
                    newresult+="A\n"
            elif temp[i] == "AC":
                    newresult += result_1[i]+"\n"
            else:
                if result_1[i] == 'C' and c > 0 and tob > 0:
                    c -= 1
                    tob -= 1
                    newresult += "B\n"
                else:
                    newresult += "C\n"
            i = i + 1
    elif toc>0 and a>=0 and b>=0:
        i = 0
        newresult = ""
        while i < number:
            if temp[i] == "AC":
                if result_1[i]=='A' and a>0 and toc>0:
                    a-=1
                    toc-=1
                    newresult+="C\n"
                else:
                    newresult+="A\n"
            elif temp[i] == "AB":
                    newresult += result_1[i]+"\n"
            else:
                if result_1[i] == 'B' and b > 0 and toc > 0:
                    b -= 1
                    toc -= 1
                    newresult += "C\n"
                else:
                    newresult += "B\n"
            i = i + 1
    if a < 0 or b < 0 or c < 0:
        print("NO")
else:
    print("YES")
    print(result)

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
S = [input() for i in range(N)]

ans = "Yes"
select = ""
for i in range(N):
    s = S[i]
    if s=="AB":
        if A>B:A,B = A-1,B+1;select+="B"
        elif A<B:A,B = A+1,B-1;select+="A"
        else:
            if i<N-1 and S[i+1] in "A":A,B = A+1,B-1;select+="A"
            else:A,B = A-1,B+1;select+="B"
    if s=="AC":
        if A>C:A,C = A-1,C+1;select+="C"
        elif A<C:A,C = A+1,C-1;select+="A"
        else:
            if i<N-1 and S[i+1] in "A":A,C = A+1,C-1;select+="A"
            else:A,C = A-1,C+1;select+="C"
    if s=="BC":
        if C>B:C,B = C-1,B+1;select+="B"
        elif C<B:C,B = C+1,B-1;select+="C"
        else:
            if i<N-1 and S[i+1] in "C":C,B = C+1,B-1;select+="C"
            else:C,B = C-1,B+1;select+="B"
    if min(A,B,C)<0:
        ans = "No"
        break

if ans=="Yes":
    print(ans)
    for s in select:print(s)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
ans = []
D = {'A':a, 'B':b, 'C':c}
sx = SR(n)
for i in range(n):
    s = sx[i]
    s1 = s[0]
    s2 = s[1]
    if D[s1] == D[s2] == 0:
        print('No')
        exit()
    elif D[s[0]] == D[s[1]] == 1 and i + 1 < n:
        if s1 in sx[i + 1]:
            D[s1] -= 1
            D[s2] += 1
            ans += [s2]
        else:
            D[s1] += 1
            D[s1] -= 1
            ans += [s1]
    else:
        if D[s1] == 0:
            ans += [s1]
        else:
            ans += [s2]

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
s = [input() for i in range(n)]
# if [a,b,c].count(0) >= 2:
#     print("No")
ans = ""

# print("Yes")
for i in s:
    if i == "AB":
        if a<=b:
            ans+="A"
            a+=1
            b-=1
        else:
            ans+="B"
            b+=1
            a-=1
    if i == "AC":
        if a<=c:
            ans+="A"
            a+=1
            c-=1
        else:
            ans+="C"
            c+=1
            a-=1       
    if i == "BC":
        if b<=c:
            ans+="B"
            b+=1
            c-=1
        else:
            ans+="C"
            c+=1
            b-=1   
    if a<0 or b<0 or c<0:
        print("No") 
        exit()
print("Yes")
for i in ans:
    print(i)

Submission status: Wrong Answer

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

sum = a + b + c
lst = []
if sum != 2:
    for _ in range(n):
        s = input()
        if s == "AB":
            if a < b:
                a += 1
                b -= 1
                lst.append("A")
            else:
                a -= 1
                b += 1
                if a < 0:
                    print("No")
                    break
                lst.append("B")
        elif s == "AC":
            if a < c:
                a += 1
                c -= 1
                lst.append("A")
            else:
                a -= 1
                c += 1
                if a < 0:
                    print("No")
                    break
                lst.append("C")
        else:
            if c < b:
                c += 1
                b -= 1
                lst.append("C")
            else:
                c -= 1
                b += 1
                if c < 0:
                    print("No")
                    break
                lst.append("B")
    else:
        print("Yes")
        for alpha in lst:
            print(alpha)
elif n == 1:
    s = input()
    if s == "AB":
        if a == b == 0:
            print("No")
        else:
            print("Yes")
            if a > b:
                print("B")
            else:
                print("A")
    elif s == "AC":
        if a == c == 0:
            print("No")
        else:
            print("Yes")
            if a > c:
                print("C")
            else:
                print("A")
    else:
        if c == b == 0:
            print("No")
        else:
            print("Yes")
            if c > b:
                print("B")
            else:
                print("C")            
                
else:
    s = input()
    flag = False
    flag2 = True
    for _ in range(n - 1):
        if s == "AB":
            if a == b == 0:
                print("No")
                flag2 = False
                break
            elif a != b:
                if a > b:
                    lst.append("B")
                    a -= 1
                    b += 1
                else:
                    lst.append("A")
                    a += 1
                    b -= 1
                s = input()
                flag = True
            else:
                s = input()
                flag = False
                if s == "AB":
                    lst.append("A")
                    a = 2
                    b = 0
                elif s == "AC":
                    lst.append("A")
                    a = 2
                    b = 0
                else:
                    lst.append("B")
                    b = 2
                    a = 0
                    
        elif s == "AC":
            if a == c == 0:
                print("No")
                flag2 = False
                break
            elif a != c:
                if a > c:
                    lst.append("C")
                    a -= 1
                    c += 1
                else:
                    lst.append("A")
                    a += 1
                    c -= 1
                s = input()
                flag = True
            else:
                s = input()
                flag = False
                if s == "AC":
                    lst.append("A")
                    a = 2
                    c = 0
                elif s == "AB":
                    lst.append("A")
                    a = 2
                    c = 0
                else:
                    lst.append("C")
                    c = 2
                    a = 0
                    
        else:
            if b == c == 0:
                print("No")
                flag2 = False
                break
            elif b != c:
                if b > c:
                    lst.append("C")
                    b -= 1
                    c += 1
                else:
                    lst.append("B")
                    b += 1
                    c -= 1
                s = input()
                flag = True
            else:
                s = input()
                flag = False
                if s == "BC":
                    lst.append("B")
                    b = 2
                    c = 0
                elif s == "AB":
                    lst.append("B")
                    b = 2
                    c = 0
                else:
                    lst.append("C")
                    c = 2
                    b = 0
    else:
        if flag:
            if s == "AB":
                if a == b == 0:
                    print("No")
                    flag2 = False
                if a < b:
                    lst.append("A")
                else:
                    lst.append("B")
            elif s == "AC":
                if a == c == 0:
                    print("No")
                    flag2 = False
                if a < c:
                    lst.append("A")
                else:
                    lst.append("C")
            else:
                if b == c == 0:
                    print("No")
                    flag2 = False
                if b < c:
                    lst.append("B")
                else:
                    lst.append("C")
    if flag2:
        print("Yes")
        for alpha in lst:
            print(alpha)

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int, input().split())
S = [input() for _ in range(N)]

L = []
ans = "Yes"
for i in range(N):
    s = S[i]
    if s == "AB":
        if A < B:
            A += 1
            B -= 1
            L.append("A")
        elif A > B:
            A -= 1
            B += 1
            L.append("B")
        else:
            if i < N-1 and S[i+1] == "BC":
                B += 1
                A -= 1
                L.append("B")
            else:
                A += 1
                B -= 1
                L.append("A")
    elif s == "BC":
        if B < C:
            B += 1
            C -= 1
            L.append("B")
        elif B > C:
            B -= 1
            C += 1
            L.append("C")
        else:
            if i < N-1 and S[i+1] == "AB":
                B += 1
                C -= 1
                L.append("B")
            else:
                C += 1
                B -= 1
                L.append("C")
    else:
        if A < C:
            A += 1
            C -= 1
            L.append("A")
        elif A > C:
            A -= 1
            C += 1
            L.append("C")
        else:
            if i < N-1 and S[i+1] == "BC":
                C += 1
                A -= 1
                L.append("C")
            else:
                A += 1
                C -= 1
                L.append("A")
    if A*B*C < 0:
        ans = "No"
            
print(ans)
if ans == "Yes":
    for l in L:
        print(l)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline

N, A, B, C = map(int, readline().split())
s = list(map(str, read().split()))
S = ''.join(s).translate(str.maketrans({'A': '0', 'B': '1', 'C': '2'}))
v = [A, B, C]
answer = []
for i, j in zip(S[::2], S[1::2]):
    i = int(i)
    j = int(j)
    if v[i] >= v[j]:
        answer.append(j)
        v[i] -= 1
        v[j] += 1
    else:
        answer.append(i)
        v[i] += 1
        v[j] -= 1
    if any(i < 0 for i in v):
        print('No')
        exit()

print('Yes')
ans = []
for i in answer:
    if i == 0:
        ans.append('A')
    elif i == 1:
        ans.append('B')
    else:
        ans.append('C')
print('\n'.join(ans))

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()
    NA=0
    NB=0
    NC=0
    S=[]
    for i in range(N):
        s=input()
        S.append(s)
        NA+=s.count("A")
        NB+=s.count("B")
        NC+=s.count("C")
    
    ans=[]
    flag=1
    for i in range(N):
        s=S[i]
        
        if s=="AB":
            if A!=0:
                A-=1
                B+=1
                ans.append("B")
            else:
                A+=1
                B-=1
                ans.append("A")
        elif s=="AC":
            if A!=0:
                A-=1
                C+=1
                ans.append("C")
            else:
                A+=1
                C-=1
                ans.append("C")
        else:
            if B!=0:
                B-=1
                C+=1
                ans.append("C")
            else:
                C+=1
                B-=1
                ans.append("C")
        
        
        
        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:
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) or(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):
    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==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: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
s = [input() for i in range(n)]
# if [a,b,c].count(0) >= 2:
#     print("No")
ans = ""

# print("Yes")
for i in s:
    if i == "AB":
        if a<=b:
            ans+="A"
            a+=1
            b-=1
        else:
            ans+="B"
            b+=1
            a-=1
    if i == "AC":
        if a<=c:
            ans+="A"
            a+=1
            c-=1
        else:
            ans+="C"
            c+=1
            a-=1       
    if i == "BC":
        if b<=c:
            ans+="B"
            b+=1
            c-=1
        else:
            ans+="C"
            c+=1
            b-=1   
    if a<0 or b<0 or c<0:
        print("No") 
        exit()
for i in ans:
    print(i)

Submission status: Wrong Answer

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

N, A, B, C = map(int, readline().split())
S = [readline().strip() for i in range(N)]

AB, AC, BC = 'AB', 'AC', 'BC'

n0 = [A, B, C].count(0)

if n0 == 3:
    print('No')
elif n0 == 2 and sum([A, B, C]) == 1:
    path = []
    for com in S:
        if com == AB:
            if C == 1:
                print('No')
                exit(0)
            A, B = B, A
            path.append('A' if A == 1 else 'B')
        elif com == AC:
            if B == 1:
                print('No')
                exit(0)
            A, C = C, A
            path.append('A' if A == 1 else 'C')
        else:
            if A == 1:
                print('No')
                exit(0)
            B, C = C, B
            path.append('B' if B == 1 else 'C')
    print('Yes')
    for p in path:
        print(p)
else:
    if (S[0] == AB and A == 0 and B == 0) or \
     (S[0] == AC and A == 0 and C == 0) or (S[0] == BC and B == 0 and C == 0):
        print('No')
        exit(0)

    path = []
    F = [A, B, C]
    H = ['A', 'B', 'C']
    G = {AB: (0, 1), BC:(1,2), AC:(0, 2)}
    for i,com in enumerate(S):
        g = G[com]
        if F[g[0]] == 0:
            path.append(H[g[0]])
            F[g[0]] += 1
            F[g[1]] -= 1
        elif F[g[1]] == 0:
            path.append(H[g[1]])
            F[g[1]] += 1
            F[g[0]] -= 1
        else:
            a, b = g
            if i < len(S) - 1 and H[a] == H[b]:
                if b in G[S[i + 1]]:
                    a, b = b, a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
            else:
                if F[a] > F[b]:
                    a,b = b,a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
    print('Yes')
    for p in path:
        print(p)

Submission status: Wrong Answer

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