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:
# coding: utf-8
# author: Stephen Zhang  time: 2020/5/3

step, A, B, C = map(int, input().split())
executable = True
steps = []

for i in range(0, step):
    usrInput = input()
    if usrInput == 'AB':
        if A > B or (A == B and A != 0):
            A -= 1
            B += 1
            steps.append("A")
        elif B > A:
            A += 1
            B -= 1
            steps.append("B")
        else:
            executable = False
            break
    elif usrInput == 'AC':
        if A > C or (A == C and A != 0):
            A -= 1
            C += 1
            steps.append("A")
        elif C > A:
            A += 1
            C -= 1
            steps.append("C")
        else:
            executable = False
            break
    else:
        if B > C or (B == C and B != 0):
            B -= 1
            C += 1
            steps.append("B")
        elif C > B:
            B += 1
            C -= 1
            steps.append("C")
        else:
            executable = False
            break

if executable:
    print("Yes")
    for each in steps:
        print(each)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
def io():
    return map(int,input().split())
def op():
    return list(map(int,input().split()))
l=op()
d={"A":1,"B":2,"C":3}
ans=[]
for i in range(l[0]):
    s=input()
    x,y=l[d[s[0]]],l[d[s[1]]]
    if x>=y:
        l[d[s[0]]]-=1
        l[d[s[1]]]+=1
        ans.append(s[1])
    else:
        l[d[s[1]]] -= 1
        l[d[s[0]]] += 1
        ans.append(s[0])
    x,y=l[d[s[0]]],l[d[s[1]]]
    if x<0 or y<0:
        print('No')
        break
else:
    print("Yes")
    for i in ans:
        print(i)

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+1: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) 
    nums[i] += 1
    nums[j] -= 1
    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:
import numpy as np
nabc=input()
[n, a, b, c] = [int(comp) for comp in nabc.split()]
ans = ''
flag = 1
for i in range(n):
    temp = input()
    if(temp == 'AB'):
        if(a > b):
            a -= 1
            b += 1
            ans += 'B'
        else:
            a += 1
            b -= 1
            ans += 'A'
    elif(temp == 'AC'):
        if(a > c):
            a -= 1
            c += 1
            ans += 'C'
        else:
            a += 1
            c -= 1
            ans += 'A'
    else:
        if(b > c):
            b -= 1
            c += 1
            ans += 'C'
        else:
            b += 1
            c -= 1
            ans += 'B'
    if(i != n -1):
        1
        ans += '\n'
    if(a < 0 or b < 0 or c < 0):
        flag = 0


if(flag):
    print('YES')
    print(ans)

else:
    print('NO')

Submission status: Wrong Answer

Solution code:
N, A, B, C = [int(i) for i in input().split()]
ret = ""
flag = False
for i in range(N):
    x = input().strip()
    if x == "AB":
        if A <= B:
            A += 1
            B -= 1
            ret += "A"
        else:
            A -= 1
            B += 1
            ret += "B"
    elif x == "AC":
        if A <= C:
            A += 1
            C -= 1
            ret += "A"
        else:
            A -= 1
            C += 1
            ret += "C"
    elif x == "BC":
        if B <= C:
            B += 1
            C -= 1
            ret += "B"
        else:
            B -= 1
            C += 1
            ret += "C"
    if A < 0 or B < 0 or C < 0:
        flag = True
if flag:
    print("No")
else:
    print("Yes")
    print("\n".join(list(ret)))

Submission status: Wrong Answer

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

OK = True
ans = []
tasks = []
A_num = 0
B_num = 0
C_num = 0
AB_num = 0
BC_num = 0
AC_num = 0
for i in range(N):
  s = input()
  tasks.append(s)
  if s == "AB":
    A_num += 1
    B_num += 1
    AB_num += 1
  elif s == "BC":
    B_num += 1
    C_num += 1
    BC_num += 1
  elif s == "AC":
    A_num += 1
    C_num += 1
    AC_num += 1

for i in tasks:
  if i == "AB":
    A_num -= 1
    B_num -= 1
    AB_num -= 1
    if A < B:
      A +=1
      B -=1
      ans.append("A")
    elif A > B:
      A -=1
      B +=1
      ans.append("B")
    elif A_num < B_num:
      A -=1
      B +=1
      ans.append("B")
    elif A_num > B_num:
      A +=1
      B -=1
      ans.append("A")
    elif BC_num < AC_num:
      A +=1
      B -=1
      ans.append("A")
    elif BC_num >= AC_num:  # =の時はどっちでもいいよね？
      A -=1
      B +=1
      ans.append("B")
  elif i == "BC":
    B_num -= 1
    C_num -= 1
    BC_num -= 1
    if B < C:
      B +=1
      C -=1
      ans.append("B")
    elif B > C:
      B -=1
      C +=1
      ans.append("C")
    elif B_num < C_num:
      B -=1
      C +=1
      ans.append("C")
    elif B_num > C_num:
      B +=1
      C -=1
      ans.append("B")
    elif AB_num < AC_num:
      B -=1
      C +=1
      ans.append("C")
    elif AB_num >= AC_num:
      B +=1
      C -=1
      ans.append("B")
  elif i == "AC":
    A_num -= 1
    C_num -= 1
    AC_num -= 1
    if A < C:
      A +=1
      C -=1
      ans.append("A")
    elif A > C:
      A -=1
      C +=1
      ans.append("C")
    elif A_num < C_num:
      A -=1
      C +=1
      ans.append("C")
    elif A_num > C_num:
      A +=1
      C -=1
      ans.append("A")
    elif AB_num < BC_num:
      A -=1
      C +=1
      ans.append("C")
    elif AB_num >= BC_num:
      A +=1
      C -=1
      ans.append("A")
  if A == -1 or B == -1 or C == -1:
    OK = False
    break

if OK:
  print("Yes")
  for i in ans:
    print(i)
else:
  print("No")

Submission status: Wrong Answer

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

res_list = []
flag = 9
if a == 0 and b == 0 and c == 0:
    flag = 0
elif a * b > 1 or a * c > 1 or b * c > 1:
    flag = 1
    for s in ss:
        if s == 'AB':
            if not b in [0, 1]:
                res_list.append('A')
                a, b = a+1, b-1
            else:
                res_list.append('B')
                a, b = a-1, b+1
        elif s == 'BC':
            if not c in [0, 1]:
                res_list.append('B')
                b, c = b+1, c-1
            else:
                res_list.append('C')
                b, c = b-1, c+1
        else:
            if not c in [0, 1]:
                res_list.append('A')
                a, c = a+1, c-1
            else:
                res_list.append('C')
                a, c = a-1, c+1
else:
    if a == 1 and b == 1 and c == 1:
        flag = 1
        for s in ss:
            if s == 'AB':
                if b == 0:
                    res_list.append('B')
                    a, b = a-1, b+1
                elif b == 1 and a > 1:
                    res_list.append('B')
                    a, b = a-1, b+1
                else:
                    res_list.append('A')
                    a, b = a+1, b-1
            elif s == 'BC':
                if c == 0:
                    res_list.append('C')
                    b, c = b-1, c+1
                elif c == 1 and b > 1:
                    res_list.append('C')
                    b, c = b-1, c+1
                else:
                    res_list.append('B')
                    b, c = b+1, c-1
            else:
                if c == 0:
                    res_list.append('C')
                    a, c = a-1, c+1
                elif c == 1 and a > 1:
                    res_list.append('C')
                    a, c = a-1, c+1
                else:
                    res_list.append('A')
                    a, c = a+1, c-1
    elif 1 in [a*b, b*c, a*c]:
        flag = 1
        for i in range(n):
            if ss[i] == 'AB':
                if a * b == 0:
                    if a == 0:
                        res_list.append('A')
                    else:
                        res_list.append('B')
                    a, b = b, a
                else:
                    if i == n-1:
                        res_list.append('A')
                    elif ss[i+1] == 'AC':
                        res_list.append('A')
                    elif ss[i+1] == 'BC':
                        res_list.append('B')
                    else:
                        res_list.append('A')
                    a, b = b, a
            elif ss[i] == 'BC':
                if b * c == 0:
                    if b == 0:
                        res_list.append('B')
                    else:
                        res_list.append('C')
                    b, c = c, b
                else:
                    if i == n-1:
                        res_list.append('B')
                    elif ss[i+1] == 'AC':
                        res_list.append('C')
                    elif ss[i + 1] == 'AB':
                        res_list.append('B')
                    else:
                        res_list.append('B')
                    b, c = c, b
            else:
                if a * c == 0:
                    if a == 0:
                        res_list.append('A')
                    else:
                        res_list.append('C')
                    a, c = c, a
                else:
                    if i == n-1:
                        res_list.append('A')
                    elif ss[i+1] == 'AB':
                        res_list.append('A')
                    elif ss[i + 1] == 'BC':
                        res_list.append('C')
                    else:
                        res_list.append('A')
                    a, c = c, a
    elif a+b+c == 1:
        flag = 1
        for s in ss:
            if s == 'AB' and a == 0 and b == 0:
                flag = 0
            elif s == 'BC' and b == 0 and c == 0:
                flag = 0
            elif s == 'AC' and a == 0 and c == 0:
                flag = 0
            else:
                if s == 'AB':
                    if a == 1:
                        res_list.append('A')
                    else:
                        res_list.append('B')
                    a, b = b, a
                elif s == 'BC':
                    if b == 1:
                        res_list.append('B')
                    else:
                        res_list.append('C')
                    b, c = c, b
                else:
                    if c == 1:
                        res_list.append('C')
                    else:
                        res_list.append('A')
                    a, c = c, a
    else:
        if ss[0] == 'AB' and a == 0 and b == 0:
            flag = 0
        elif ss[0] == 'AC' and a == 0 and c == 0:
            flag = 0
        elif ss[0] == 'BC' and b == 0 and c == 0:
            flag = 0
        else:
            flag = 1
            for s in ss:
                if s == 'AB':
                    if not b in [0, 1]:
                        res_list.append('A')
                        a, b = a + 1, b - 1
                    else:
                        res_list.append('B')
                        a, b = a - 1, b + 1
                elif s == 'BC':
                    if not c in [0, 1]:
                        res_list.append('B')
                        b, c = b + 1, c - 1
                    else:
                        res_list.append('C')
                        b, c = b - 1, c + 1
                else:
                    if not c in [0, 1]:
                        res_list.append('A')
                        a, c = a + 1, c - 1
                    else:
                        res_list.append('C')
                        a, c = a - 1, c + 1

if flag == 0:
    print('No')
else:
    print('Yes')
    for res in res_list:
        print(res)

Submission status: Wrong Answer

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


n,a,b,c = mi()
s = []
for i in range(n):
    s.append(input())
z = a+ b+ c
flag = True
ans = []
if z != 2:
    for i in range(n):
        if s[i] == "AB":
            if a == 0 and b == 0:
                print("No")
                exit()
            if a > b:
                a -= 1
                b += 1
                ans.append("B")
            else:
                a += 1
                b -= 1
                ans.append("A")

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

else:
    for i in range(n):
        if s[i] == "AB":
            if a == 0 and b == 0:
                print("No")
                exit()
            if a > b:
                a -= 1
                b += 1
                ans.append("B")
            elif a == b:
                if i == n-1:
                    ans.append("B")
                elif s[i+1] == "BC":
                    a -= 1
                    b += 1
                    ans.append("B")
                else:
                    a += 1
                    b -= 1
                    ans.append("A")
            else:
                a += 1
                b -= 1
                ans.append("A")

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    c=[deque() for _ in range(3)]
    ans=[-1]*N
    sousa=[]
    for i in range(N):
        s=input()
        sousa.append(d[s])
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            print('No')
            exit()
        elif n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            c[d[s]].append(i)
        if len(c[d[s]])>=2:
            if n[(d[s]+2)%3]>=1:
                if n[d[s]]>=1:
                    k=c[d[s]].popleft()
                    ans[k]=(d[s]+1)%3
                    k=c[d[s]].popleft()
                    ans[k]=d[s]%3
                else:
                    k=c[d[s]].popleft()
                    ans[k]=d[s]%3
                    k=c[d[s]].popleft()
                    ans[k]=(d[s]+1)%3
        if len(c[0])>=1 and len(c[1])>=1 and len(c[2])>=1:
            for j in range(3):
                if n[j]>=1:
                    start=j
                    break
            for j in range(start,3+start):
                j%=3
                k=c[j].popleft()
                ans[k]=(j+1)%3
    n=[A,B,C]
    print('Yes')
    d2=['A','B','C']
    for x,y in zip(ans,sousa):
        if x!=-1:
            n[x]+=1
            if x==y:
                n[(x+1)%3]-=1
            else:
                n[(x-1)%3]-=1
            print(d2[x])
        else:
            if n[y]>=n[(y+1)%3]:
                n[y]-=1
                n[(y+1)%3]+=1
                print(d2[(y+1)%3])
            elif n[(y+1)%3]>=1:
                n[y]+=1
                n[(y+1)%3]-=1
                print(d2[y])
            else:
                raise Exception
                
                    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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



if [A,B,C].count(0)==2:
    print("No")
    exit()
else:
    print("Yes")
S=[0]*N
for i in range(N):
    s=input()
    S[i]=s

for i in range(N):  
    if S[i]=="AB":
        if A==1 and B==1:
            if i+1<N:
                if S[i+1]=="BC" and C==0:
                    B+=1
                    A-=1
                    print("B")
                    continue
                elif S[i+1]=="AC" and C==0:
                    A+=1
                    B-=1
                    print("A")
                    continue
            A+=1
            B-=1
            print("A")
        elif A>B:
            A-=1
            B+=1
            print("B")
        elif B>A:
            A+=1
            B-=1
            print("A")
        else:
            A+=1
            B-=1
            print("A")



    if S[i]=="AC":
        if A==1 and C==1:
            if i+1<N:
                if S[i+1]=="BC" and B==0:
                    C+=1
                    A-=1
                    print(C)
                    continue
                elif S[i+1]=="AB" and B==0:
                    A+=1
                    C-=1
                    print(A)
                    continue
            A+=1
            C-=1
            print("A")
        elif A>C:
            A-=1
            C+=1
            print("C")
        elif C>A:
            A+=1
            C-=1
            print("A")
        else:
            A+=1
            C-=1
            print("A")

    if S[i]=="BC":
        if B==1 and C==1:
            if i+1<N:
                if S[i+1]=="AC" and A==0:
                    C+=1
                    B-=1
                    print("C")
                    continue
                elif S[i+1]=="AB" and A==0:
                    B+=1
                    C-=1
                    print("B")
                    continue
            B+=1
            C-=1
            print("B")
        elif B>C:
            B-=1
            C+=1
            print("C")
        elif C>B:
            B+=1
            C-=1
            print("B")
        else:
            B+=1
            C-=1
            print("B")

Submission status: Wrong Answer

Solution code:
n, *x = map(int, input().split())
k = 'ABC'
ans = []
for i in range(n):
    s = input()
    if s == 'AB':
        i = 0
        j = 1
    elif s == 'BC':
        i = 1
        j = 2
    else:
        i = 0
        j = 2
    if x[i] < x[j]:
        if x[j] == 0:
            print('No')
            break
        x[i] += 1
        x[j] -= 1
        ans.append(k[i])
    else:
        if x[i] == 0:
            print('No')
            break
        x[i] -= 1
        x[j] += 1
        ans.append(k[j])
else:
    print('Yes')
    print(*ans, sep='\n')

Submission status: Wrong Answer

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

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

freq=[0]*3
for i in range(n):
    s1=ord(op[i][0])-ord('A')
    s2=ord(op[i][1])-ord('A')
    if val[s1]>val[s2]:
        ansls.append(s2)
        val[s2]+=1
        val[s1]-=1
    else:
        ansls.append(s1)
        val[s1]+=1
        val[s2]-=1
    if min(val)<0:
        flag=False
        break

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

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
v = [A, B, C]
id_d = {'A':0, 'B':1, 'C':2}
ans = [0] * N
temp = []
temp_idx = [set(), set(), set()]
for i in range(N):
  s1, s2 = list(input())
  id1, id2 = id_d[s1], id_d[s2]
  v1, v2 = v[id1], v[id2]
  if v1 == v2 == 0:
    if temp_idx[id1]:
      idx = temp_idx[id1].pop()
      _i, _s1, _s2 = temp[idx]
      temp_idx[id_d[_s1]].discard(idx)
      temp_idx[id_d[_s2]].discard(idx)
      ans[_i] = s1
      v[id1] = 1
      v[id2] = 1
    elif temp_idx[id2]:
      idx = temp_idx[id2].pop()
      _i, _s1, _s2 = temp[idx]
      temp_idx[id_d[_s1]].discard(idx)
      temp_idx[id_d[_s2]].discard(idx)
      ans[_i] = s2
      v[id1] = 1
      v[id2] = 1
    else:
      print('No')
      quit()
  else:
    if v1 > v2:
      ans[i] = s2
      v[id_d[s1]] -= 1
      v[id_d[s2]] += 1
    elif v1 < v2:
      ans[i] = s1
      v[id_d[s2]] -= 1
      v[id_d[s1]] += 1
    else: # v1 == v2
      v[id_d[s1]] -= 1
      v[id_d[s2]] -= 1
      temp.append([i, s1, s2])

for i, s1, s2 in temp:
  if ans[i] == 0:
    ans[i] = s1

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

Submission status: Wrong Answer

Solution code:
import sys

n,a,b,c = map(int,input().split())
s_list = [input() for _ in range(n)]
print_list = []


for i,s in enumerate(s_list):
    if s == 'AB':
        if a == 0 and b == 0:  
            print('No')
            sys.exit()
        else:
            if a >= b:
                print_list.append('B')
                a -= 1
                b += 1
            else:
                print_list.append('A')
                a += 1
                b -= 1
    elif s == 'BC':
        if c == 0 and b == 0:  
            print('No')
            sys.exit()
        else:
            if c >= b:
                print_list.append('B')
                c -= 1
                b += 1
            else:
                print_list.append('C')
                c += 1
                b -= 1

    elif s == 'AC':
        if a == 0 and c == 0:  
            print('No')
            sys.exit()
        else:
            if a >= c:
                print_list.append('C')
                a -= 1
                c += 1
            else:
                print_list.append('A')
                a += 1
                c -= 1

print('Yes')
for p in print_list:
    print(p)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter
N, A, B, C = map(int, input().split())
ans = []
# c = Counter([input() for _ in range(N)])
# print(c)
for i in range(N):
    s = input()
    if s == "AB":
        if A < B:
            B -= 1
            A += 1
            ans.append("A")
        else:
            B += 1
            A -= 1
            ans.append("B")
    elif s == "AC":
        if A <= C:
            C -= 1
            A += 1
            ans.append("A")
        else:
            C += 1
            A -= 1
            ans.append("C")
    else:
        if B < C:
            C -= 1
            B += 1
            ans.append("B")
        else:
            C += 1
            B -= 1
            ans.append("C")

if 0 < A and 0 < B and 0 < C:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")

Submission status: Wrong Answer

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

ans = []
possible = True
if A+B+C!=2:
    for i in range(N):
        s = q[i]
        if s=='AB':
            if B>1:
                A += 1
                B -= 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
                if A<0:
                    possible = False
        elif s=='AC':
            if C>1:
                A += 1
                C -= 1
                ans.append('A')
            else:
                A -= 1
                C += 1
                ans.append('C')
                if A<0:
                    possible = False
        else:
            if B==0 and C==0:
                possible = False
            if B>=C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
else:
    for i in range(N):
        s = q[i]
        if s=='AB':
            if (A,B,C)==(0,0,2):
                possible = False
            elif (A,B,C)==(1,0,1):
                A -= 1
                B += 1
                ans.append('B')
            elif (A,B,C)==(0,1,1):
                A += 1
                B -= 1
                ans.append('A')
            else:
                if i==N-1 or q[i+1]=='AB':
                    A += 1
                    B -= 1
                    ans.append('A')
                else:
                    if q[i+1]=='AC':
                        A += 1
                        B -= 1
                        ans.append('A')
                    else:
                        A -= 1
                        B += 1
                        ans.append('B')
        if s=='BC':
            if (A,B,C)==(2,0,0):
                possible = False
            elif (A,B,C)==(1,0,1):
                B += 1
                C -= 1
                ans.append('B')
            elif (A,B,C)==(1,1,0):
                C += 1
                B -= 1
                ans.append('C')
            else:
                if i==N-1 or q[i+1]=='BC':
                    B += 1
                    C -= 1
                    ans.append('B')
                else:
                    if q[i+1]=='AC':
                        C += 1
                        B -= 1
                        ans.append('C')
                    else:
                        C -= 1
                        B += 1
                        ans.append('B')
        if s=='AC':
            if (A,B,C)==(0,2,0):
                possible = False
            elif (A,B,C)==(0,1,1):
                A += 1
                C -= 1
                ans.append('A')
            elif (A,B,C)==(1,1,0):
                C += 1
                A -= 1
                ans.append('C')
            else:
                if i==N-1 or q[i+1]=='AC':
                    A += 1
                    C -= 1
                    ans.append('A')
                else:
                    if q[i+1]=='AB':
                        A += 1
                        C -= 1
                        ans.append('A')
                    else:
                        C += 1
                        A -= 1
                        ans.append('C')

if possible:
    print('Yes')
    for i in range(N):
        print(ans[i])
else:
    print('No')