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:
import sys
input = sys.stdin.readline
from collections import *

N, A, B, C = map(int, input().split())
s = [input()[:-1] for _ in range(N)]
ans = []

if A+B+C<=1 or A+B+C>=3:
    for si in s:
        if si=='AB':
            if A==0 and B==0:
                print('No')
                exit()
                
            if A<B:
                A += 1
                B -= 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
        elif si=='AC':
            if A==0 and C==0:
                print('No')
                exit()
                
            if A<C:
                A += 1
                C -= 1
                ans.append('A')
            else:
                A -= 1
                C += 1
                ans.append('C')
        else:
            if B==0 and C==0:
                print('No')
                exit()
                
            if B<C:
                B += 1
                C -= 1
                ans.append('B')
            else:
                B -= 1
                C += 1
                ans.append('C')

    print('Yes')
    
    for ans_i in ans:
        print(ans_i)

    exit()
    
for i in range(N):
    if s[i]=='AB':
        if A==0 and B==0:
            print('No')
            exit()
            
        if A==0:
            A += 1
            B -= 1
            ans.append('A')
        elif B==0:
            A -= 1
            B += 1
            ans.append('B')
        else:
            if i<N-1 and s[i+1]=='AC':
                A += 1
                B -= 1
                ans.append('A')
            else:
                ans.append('B')
    elif s[i]=='AC':
        if A==0 and C==0:
            print('No')
            exit()
            
        if A==0:
            A += 1
            C -= 1
            ans.append('A')
        elif C==0:
            A -= 1
            C += 1
            ans.append('C')
        else:
            if i<N-1 and s[i+1]=='AB':
                A += 1
                C -= 1
                ans.append('A')
            else:
                ans.append('C')
    else:
        if B==0 and C==0:
            print('No')
            exit()
            
        if B==0:
            B += 1
            C -= 1
            ans.append('B')
        elif C==0:
            B -= 1
            C += 1
            ans.append('C')
        else:
            if i<N-1 and s[i+1]=='AB':
                B += 1
                C -= 1
                ans.append('B')
            else:
                ans.append('C')

print('Yes')
    
for ans_i in ans:
    print(ans_i)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
N, A, B, C = map(int, input().split())
S = [input().strip("\n").replace("A", "0").replace("B", "1").replace("C", "2") for _ in range(N)]
Ans = [None] * N

def search(i, num):
    if i == N - 1:
        if num[int(S[N-1][0])] > 0: 
            Ans[N-1] = S[N-1][0]
            return True
        elif num[int(S[N-1][1])] > 0: 
            Ans[N-1] = S[N-1][1]
            return True
        else: return False
    if min(num) < 0: return False
    L = [num[0], num[1], num[2]]
    f = S[i][0]
    s = S[i][1]
    L[int(f)] += 1
    L[int(s)] -= 1
    if search(i+1, L):
        Ans[i] = f
        return True
    L[int(f)] -= 2
    L[int(s)] += 2
    if search(i+1, L):
        Ans[i] = s
        return True
    return False

def solve():
    if search(0, [A, B, C]):
        print("Yes")
        for i, a in enumerate(Ans):
            if a == "0": print("A")
            elif a == "1": print("B")
            else: print("C")
    else: print("No")
    #print(Ans)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
N, A, B, C = map(int, input().split())
S = [input().strip("\n").replace("A", "0").replace("B", "1").replace("C", "2") for _ in range(N)]
Ans = [None] * N

def search(i, num):
    if i == N - 1:
        if num[int(S[N-1][0])] > 0: 
            Ans[N-1] = S[N-1][0]
            return True
        elif num[int(S[N-1][1])] > 0: 
            Ans[N-1] = S[N-1][1]
            return True
        else: return False
    if min(num) < 0: return False
    f = S[i][0]
    s = S[i][1]
    num[int(f)] += 1
    num[int(s)] -= 1
    if search(i+1, num):
        Ans[i] = f
        return True
    num[int(f)] -= 2
    num[int(s)] += 2
    if search(i+1, num):
        Ans[i] = s
        return True
    num[int(f)] += 1
    num[int(s)] -= 1
    return False

def solve():
    if search(0, [A, B, C]):
        print("Yes")
        for i, a in enumerate(Ans):
            if a == "0": print("A")
            elif a == "1": print("B")
            else: print("C")
    else: print("No")

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
N, A, B, C = map(int, input().split())
S = [input().strip("\n").replace("A", "0").replace("B", "1").replace("C", "2") for _ in range(N)]
Ans = [None] * N

def search(i, num):
    if i == N - 1:
        if num[int(S[N-1][0])] > 0: 
            Ans[N-1] = S[N-1][0]
            return True
        elif num[int(S[N-1][1])] > 0: 
            Ans[N-1] = S[N-1][1]
            return True
        else: return False
    f = S[i][0]
    s = S[i][1]
    num[int(f)] += 1
    num[int(s)] -= 1
    if num[int(s)] >= 0:
        if search(i+1, num):
            Ans[i] = f
            return True
    num[int(f)] -= 2
    num[int(s)] += 2
    if num[int(f)] >= 0:
        if search(i+1, num):
            Ans[i] = s
            return True
    num[int(f)] += 1
    num[int(s)] -= 1
    return False

def solve():
    if search(0, [A, B, C]):
        print("Yes")
        for i, a in enumerate(Ans):
            if a == 0: print("A")
            elif a == 1: print("B")
            else: print("C")
    else: print("No")

    return 0

if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
ansl = []
for i in range(N):
    S = input()
    if S == "AB":
        if A == 0 and B == 0:
            print("No")
            break
        else:
            if A >= B:
                A -= 1
                B += 1
                ansl.append("B")
            else:
                A += 1
                B -= 1
                ansl.append("C")
    elif S == "AC":
        if A == 0 and C == 0:
            print("No")
            break
        else:
            if A >= C:
                A -= 1
                C += 1
                ansl.append("C")
            else:
                A += 1
                C -= 1
                ansl.append("A")
    else:
        if B == 0 and C == 0:
            print("No")
            break
        else:
            if B >= C:
                B -= 1
                C += 1
                ansl.append("C")
            else:
                B += 1
                C -= 1
                ansl.append("B")
    if i == N-1:
        print("Yes")
        for x in ansl:
            print(x)

Submission status: Wrong Answer

Solution code:
import sys
def main():
    S=[0,0,0]
    N,S[0],S[1],S[2]=tuple(map(int,sys.stdin.readline().split()))
    res=[]
    for _ in [0]*N:
        if any(map(lambda x:x<0, S)):
            print('No')
            return
        s=list(sys.stdin.readline().strip())
        x,y=int(s[0],16)-int('A',16),int(s[1],16)-int('A',16)
        if S[x]==0 and S[y]==0:
            print('No')
            return
        else:
            if S[x]<S[y]:
                res.append(format(x+10,'X'))
                S[x],S[y]=S[x]+1,S[y]-1
            else:
                res.append(format(y+10,'X'))
                S[x],S[y]=S[x]-1,S[y]+1
    print('Yes')
    for r in res:print(r)
if __name__=='__main__':main()

Submission status: Wrong Answer

Solution code:
import sys
def main():
    S=[0,0,0]
    N,S[0],S[1],S[2]=tuple(map(int,sys.stdin.readline().split()))
    res=[]
    for _ in [0]*N:
        s=list(sys.stdin.readline().strip())
        x,y=int(s[0],16)-int('A',16),int(s[1],16)-int('A',16)
        if all([S[x]==0,S[y]==0]) or any(list(map(lambda x:x<0,S))):
            print('No')
            return
        else:
            if S[x]<S[y]:
                res.append(format(x+10,'X'))
                S[x],S[y]=S[x]+1,S[y]-1
            else:
                res.append(format(y+10,'X'))
                S[x],S[y]=S[x]-1,S[y]+1
    print('Yes')
    for r in res:print(r)
if __name__=='__main__':main()

Submission status: Wrong Answer

Solution code:
import sys
def main():
    S=[0,0,0]
    N,S[0],S[1],S[2]=tuple(map(int,sys.stdin.readline().split()))
    res=[]
    for _ in [0]*N:
        s=list(sys.stdin.readline().strip())
        x,y=int(s[0],16)-int('A',16),int(s[1],16)-int('A',16)
        if S[x]==0 and S[y]==0:
            print('No')
            return
        else:
            if S[x]<S[y]:
                res.append(format(x+10,'X'))
                S[x],S[y]=S[x]+1,S[y]-1
            else:
                res.append(format(y+10,'X'))
                S[x],S[y]=S[x]-1,S[y]+1
    print('Yes')
    for r in res:print(r)
if __name__=='__main__':main()

Submission status: Wrong Answer

Solution code:
import sys
N,A,B,C = map(int,input().split())
ANS = []
for i in range(N):
    s = input()
    if s == "AB":
        if A == 0 and B == 0:
            print("No")
            sys.exit()
        if A > B:
            B += 1
            A -= 1
            ANS.append("B")
        else:
            A += 1
            B -= 1
            ANS.append("A")
    if s == "AC":
        if A == 0 and C == 0:
            print("No")
            sys.exit()
        if A > C:
            C += 1
            A -= 1
            ANS.append("C")
        else:
            A += 1
            C -= 1
            ANS.append("A")
    if s == "BC":
        if B == 0 and C == 0:
            print("No")
            sys.exit()
        if B > C:
            C += 1
            B -= 1
            ANS.append("C")
        else:
            B += 1
            C -= 1
            ANS.append("B")
print("Yes")
for i in range(len(ANS)):
    print(ANS[i])

Submission status: Wrong Answer

Solution code:
import sys
from functools import lru_cache
from collections import defaultdict
from collections import deque
inf = float('inf')
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
n,a,b,c=reads()
lsAB=deque()
lsBC=deque()
lsAC=deque()
dic=defaultdict(int)
k=-1
lsS=[]
for i in range(n):
  s=input()
  lsS.append(s)
  if "AB" in s:
    lsAB.append(i)
    a-=1
    b-=1
  if "BC" in s:
    lsBC.append(i)
    b-=1
    c-=1
  if "AC" in s:
    lsAC.append(i)
    c-=1
    a-=1
  if a<0:
    a+=2
    try:
      hoge=lsAB.popleft()
    except:hoge=inf
    try:
      hoge2=lsAC.popleft()
    except:hoge2=inf   
    if min(hoge,hoge2)>i:
      print("No")
      exit()
    if hoge<hoge2:
        dic[hoge]=0
    else:
        dic[hoge2]=0
  if b<0:
    b+=2
    try:
      hoge=lsAB.popleft()
    except:hoge=inf
    try:
      hoge2=lsBC.popleft()
    except:hoge2=inf   
    if min(hoge,hoge2)>i:
      print("No")
      exit()
    if hoge<hoge2:
        dic[hoge]=1
    else:
        dic[hoge2]=0
  if c<0:
    c+=2
    try:
      hoge=lsBC.popleft()
    except:hoge=inf
    try:
      hoge2=lsAC.popleft()
    except:hoge2=inf   
    if min(hoge,hoge2)>i:
      print("No")
      exit()
    if hoge<hoge2:
        dic[hoge]=1
    else:
        dic[hoge2]=1
print("Yes")
for i in range(n):
  print(lsS[i][dic[i]])

Submission status: Wrong Answer

Solution code:
N,A,B,C = list(map(int, input().split()))
ANS = []
flg = True
D = []
for i in range(N):
  D.append(input())
  
for i in range(N):
  di = D[i]
  dx = D[i+1] if i < N-1 else 'XXXXX'
  
  if di == 'AB':
    if (A,B) == (0,0):
      flg = False
      break
    elif (A,B) == (1,1) and dx == 'BC':
      ANS.append('B')
      A,B = A-1, B+1
    elif (A,B) == (1,1)  and dx == 'AC':
      ANS.append('A')
      A,B = A+1, B-1
    elif A>B:
      ANS.append('B')
      A,B = A-1, B+1
    else:
      ANS.append('A')
      A,B = A+1, B-1
  elif di == 'BC':
    if (B,C) == (0,0):
      flg = False
      break
    elif (B,C) == (1,1) and dx == 'CA':
      ANS.append('C')
      B,C = B-1, C+1
    elif (B,C) == (1,1) and dx == 'AB':
      ANS.append('B')
      B,C = B+1, C-1
    elif B>C:
      ANS.append('C')
      B,C = B-1, C+1
    else:
      ANS.append('B')
      B,C = B+1, C-1
  else:
    if (C,A) == (0,0):
      flg = False
      break
    elif (C,A) == (1,1) and dx == 'AB':
      ANS.append('A')
      C,A = C-1, A+1
    elif (C,A) == (1,1) and dx == 'BC':
      ANS.append('C')
      C,A = C+1, A-1
    elif C>A:
      ANS.append('A')
      C,A = C-1, A+1
    else:
      ANS.append('C')
      C,A = C+1, A-1
      
if not flg:
  print('No')
else:
  print('Yes')
  for ans in ANS:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 
 
 
def resolve():
    N, A, B, C = map(int, inp().split())
    S = [inp() for _ in range(N)]
 
    ans = []
    def dfs(now, a, b, c):
        if now == N:
            return True
        if S[now] == "AB":
            if a == 0 and b == 0:
                return False
            if 0 < a and dfs(now+1, a-1, b+1, c):
                ans.append("B")
                return True
            if 0 < b and dfs(now+1, a+1, b-1, c):
                ans.append("A")
                return True
            return False
        if S[now] == "AC":
            if a == 0 and c == 0:
                return False
            if 0 < a and dfs(now+1, a-1, b, c+1):
                ans.append("C")
                return True
            if 0 < c and dfs(now+1, a+1, b, c-1):
                ans.append("A")
                return True
            return False
        if S[now] == "BC":
            if c == 0 and b == 0:
                return False
            if 0 < b and dfs(now+1, a, b-1, c+1):
                ans.append("C")
                return True
            if 0 < c and dfs(now+1, a, b+1, c-1):
                ans.append("B")
                return True
            return False
 
    dfs(0, A, B, C)
    if len(ans) == 0:
        print("No")
        return
    else:
        print("Yes")
        for a in ans[::-1]:
            print(a)
            
if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
N,A,B,C = list(map(int, input().split()))
ANS = []
flg = True
for i in range(N):
  di = input()
  if di == 'AB':
    if (A,B) == (0,0):
      flg = False
      break
    elif A>B:
      ANS.append('B')
      A,B = A-1, B+1
    else:
      ANS.append('A')
      A,B = A+1, B-1
  elif di == 'BC':
    if (B,C) == (0,0):
      flg = False
      break
    elif B>C:
      ANS.append('C')
      B,C = B-1, C+1
    else:
      ANS.append('B')
      B,C = B+1, C-1
  else:
    if (C,A) == (0,0):
      flg = False
      break
    elif C>A:
      ANS.append('A')
      C,A = C-1, A+1
    else:
      ANS.append('C')
      C,A = C+1, A-1
      
if not flg:
  print('No')
else:
  print('Yes')
  for ans in ANS:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 


def resolve():
    N, A, B, C = map(int, inp().split())
    S = [inp() for _ in range(N)]

    ans = []
    def dfs(now, a, b, c):
        if now == N:
            return True
        if S[now] == "AB":
            if a == 0 and b == 0:
                return False
            if 0 < a and dfs(now+1, a-1, b+1, c):
                ans.append("B")
                return True
            if 0 < b and dfs(now+1, a+1, b-1, c):
                ans.append("A")
                return True
            return False
        if S[now] == "AC":
            if a == 0 and c == 0:
                return False
            if 0 < a and dfs(now+1, a-1, b, c+1):
                ans.append("C")
                return True
            if 0 < c and dfs(now+1, a+1, b, c-1):
                ans.append("A")
                return True
            return False
        if S[now] == "BC":
            if c == 0 and b == 0:
                return False
            if 0 < b and dfs(now+1, a, b-1, c+1):
                ans.append("C")
                return True
            if 0 < c and dfs(now+1, a, b+1, c-1):
                ans.append("B")
                return True
            return False

    dfs(0, A, B, C)
    if len(ans) == 0:
        print("No")
        return
    else:
        print("Yes")
        [print(a) for a in ans[::-1]]


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
def operate(S,A,B,C,N):
    OP = []
    #A,Bのストックを多めに
    for i in range(N):
        if S[i] == "AB":
            if A == 0 and B == 0:
                return -1
            else:
                if A >= B:
                    OP.append("B")
                    A,B = A-1,B+1
                else:
                    OP.append("A")
                    A,B = A+1,B-1
        if S[i] == "BC":
            if B == 0 and C == 0:
                return -1
            else:
                if C != 0:
                    OP.append("B")
                    B,C = B+1,C-1
                else:
                    OP.append("C")
                    B,C = B-1,C+1
        if S[i] == "AC":
            if A == 0 and C == 0:
                return -1
            else:
                if C != 0:
                    OP.append("A")
                    A,C = A+1,C-1
                else:
                    OP.append("C")
                    A,C = A-1,C+1
    return OP

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

    ans = operate(S,A,B,C,N)
    if ans != -1:
        print("Yes")
        for i in range(N):
            print(ans[i])
    else:
        print("No")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def operate(S,A,B,C,N):
    OP = []
    for i in range(N):
        if S[i] == "AB":
            if A == 0 and B == 0:
                return -1
            else:
                if A >= B:
                    OP.append("B")
                    A,B = A-1,B+1
                else:
                    OP.append("A")
                    A,B = A+1,B-1
        if S[i] == "BC":
            if B == 0 and C == 0:
                return -1
            else:
                if B >= C:
                    OP.append("C")
                    B,C = B-1,C+1
                else:
                    OP.append("B")
                    B,C = B+1,C-1
        if S[i] == "AC":
            if C == 0 and A == 0:
                return -1
            else:
                if C >= A:
                    OP.append("A")
                    C,A = C-1,A+1
                else:
                    OP.append("C")
                    C,A = C+1,A-1
    return OP

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

    ans = operate(S,A,B,C,N)
    if ans != -1:
        print("Yes")
        for i in range(N):
            print(ans[i])
    else:
        print("No")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

res = []
for i,turn in enumerate(S):
  if turn == "AB":
    if A == B == 0:
      print("No")
      exit()
    elif A == B == 1 and C == 0:
      if "A" in S[i+1]:
        A,B = A+1,B-1
        res.append("B")
      if "B" in S[i+1]:
        A,B = A-1,B+1
        res.append("A")
    elif A>B:
      A,B = A-1,B+1
      res.append("A")
    else:
      A,B = A+1,B-1
      res.append("B")

  if turn == "AC":
    if A == C == 0:
      print("No")
      exit()
    elif A == C == 1 and B == 0:
      if "A" in S[i+1]:
        A,C = A+1,C-1
        res.append("C")
      if "C" in S[i+1]:
        A,B = A-1,C+1
        res.append("A")
    elif A>C:
      A,C = A-1,C+1
      res.append("A")
    else:
      A,C = A+1,C-1
      res.append("C")

  if turn == "BC":
    if B == C == 0:
      print("No")
      exit()
    elif B == C == 1 and A == 0:
      if "B" in S[i+1]:
        B,C = B+1,C-1
        res.append("C")
      if "C" in S[i+1]:
        B,C = B-1,C+1
        res.append("B")
    elif B>C:
      B,C = B-1,C+1
      res.append("B")
    else:
      B,C = B+1,C-1
      res.append("C")

print("Yes")
for r in res:
  print(r)

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
column = [a, b, c]
dict_convert = {"A": 0, "B": 1, "C": 2}
reverse_convert = {0: "A", 1: "B", 2: "C"}
s = list()
for _ in range(n):
  pair = tuple(dict_convert[val] for val in tuple(input()))
  s.append(pair)
choices = list()
def generate_play():
  for i in range(n):
    if column[s[i][0]] == 0 and column[s[i][1]] == 0:
      return "No"
    elif column[s[i][0]] == 1 and column[s[i][1]] == 1 and i<n-1:
      other_index = next(x for x in range(1,4) if x not in (s[i][0], s[i][1]))
      if s[i+1] == (other_index, s[i][0]) or s[i+1] == (s[i][0], other_index):
        choice = s[i][0]
        column[s[i][1]] -= 1
        column[s[i][0]] += 1
      else:
        choice = s[i][1]
        column[s[i][1]] += 1
        column[s[i][0]] -= 1
      choices.append(choice)
    else:
      if column[s[i][0]] >= column[s[i][1]]:
        choice = s[i][1]
        column[s[i][1]] += 1
        column[s[i][0]] -= 1
      else:
        choice = s[i][0]
        column[s[i][1]] -= 1
        column[s[i][0]] += 1
      choices.append(choice)
  return "Yes"
ans = generate_play()
print(ans)
if ans == "Yes":
  [print(reverse_convert[val]) for val in choices]

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
column = [a, b, c]
dict_convert = {"A": 0, "B": 1, "C": 2}
reverse_convert = {0: "A", 1: "B", 2: "C"}
s = list()
for _ in range(n):
  pair = tuple(dict_convert[val] for val in tuple(input()))
  s.append(pair)
choices = list()
def generate_play():
  for i in range(n):
    if column[s[i][0]] == 0 and column[s[i][1]] == 0:
      return "No"
    else:
      if column[s[i][0]] >= column[s[i][1]]:
        choice = s[i][1]
      else:
        choice = s[i][0]
      choices.append(choice)
  return "Yes"
ans = generate_play()
print(ans)
if ans == "Yes":
  [print(reverse_convert[val]) for val in choices]