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:
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:
                if i!=N-1:
                    s2=S[i+1]
                    if s2!="AC":
                        A-=1
                        B+=1
                        ans.append("B")
                    else:
                        A+=1
                        B-=1
                        ans.append("A")
            else:
                A+=1
                B-=1
                ans.append("A")
                
        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:
                if i!=N-1:
                    s2=S[i+1]
                    if s2!="AB":
                        A-=1
                        C+=1
                        ans.append("C")
                    else:
                        A+=1
                        C-=1
                        ans.append("A")
            else:
                A+=1
                C-=1
                ans.append("A")
        
        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:
                if i!=N-1:
                    s2=S[i+1]
                    if s2!="AB":
                        A-=1
                        C+=1
                        ans.append("C")
                    else:
                        A+=1
                        C-=1
                        ans.append("A")
            else:
                B+=1
                C-=1
                ans.append("B")
                
        if A<0 or B<0 or C<0:
            flag=-1
            break
                
        
        
        
        
        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


sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, A, B, C, = map(int, input().split())
    d = {"AB": 0, "BC": 0, "AC": 0}
    cnt = {"A": A, "B": B, "C": C}

    S = [input() for _ in range(N)]
    ans = []
    for s in S:
        a, b = s[0], s[1]
        if cnt[a] > cnt[b]:
            a, b = b, a
        cnt[a] -= 1
        cnt[b] += 1
        if cnt[a] < 0 or cnt[b] < 0:
            print("No")
            break
        ans.append(b)
    else:
        print("Yes")
        for a in ans:
            print(a)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n,a,b,c = map(int, input().split())
    s = []
    for _ in range(n):
        s.append(input())
    able = 1
    ans = []
    for i in s:
        if a < 0 or b < 0 or c < 0:
            able = 0
            break
        if s == 'AB':
            if a < b:
                a += 1
                b -= 1
                ans.append('A')
            else:
                b += 1
                a -= 1
                ans.append('B')
        if s == 'AC':
            if a < c:
                a += 1
                c -= 1
                ans.append('A')
            else:
                c += 1
                a -= 1
                ans.append('C')
        else:
            if b < c:
                b += 1
                c -= 1
                ans.append('B')
            else:
                c += 1
                b -= 1
                ans.append('C')
    if able:
        print("Yes")
        print('\n'.join(ans))
    else:
        print("No")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

summ = a + b + c
if summ >= 3:
    if command[0] == 'AB' and summ == c or command[0] == 'BC' and summ == a or command[0] == 'AC' and summ == b:
        print('No')
    else:
        print('Yes')
        for com in command:
            if com == 'AB':
                if a == 0:
                    print('A')
                    a += 1
                    b -= 1
                elif b == 0:
                    print('B')
                    a -= 1
                    b += 1
                elif a == 1:
                    print('A')
                    a += 1
                    b -= 1
                else:
                    print('B')
                    a -= 1
                    b += 1
            if com == 'BC':
                if b == 0:
                    print('B')
                    b += 1
                    c -= 1
                elif c == 0:
                    print('C')
                    b -= 1
                    c += 1
                elif b == 1:
                    print('B')
                    b += 1
                    c -= 1
                else:
                    print('C')
                    b -= 1
                    c += 1
            if com == 'AC':
                if c == 0:
                    print('C')
                    c += 1
                    a -= 1
                elif a == 0:
                    print('A')
                    c -= 1
                    a += 1
                elif c == 1:
                    print('C')
                    c += 1
                    a -= 1
                else:
                    print('A')
                    c -= 1
                    a += 1


elif summ == 2:
    choice = [''] * n
    for i, com in enumerate(command):

        if com == 'AB':
            if c == 2:
                print('No')
                exit()
            if a == 0:
                choice[i] = 'A'
                a += 1
                b -= 1
            elif b == 0:
                choice[i] = 'B'
                a -= 1
                b += 1
            else:
                if i < n-1 and command[i+1] == 'BC':
                    choice[i] = 'B'
                    a -= 1
                    b += 1
                else:
                    choice[i] = 'A'
                    a += 1
                    b -= 1
        if com == 'BC':
            if a == 2:
                print('No')
                exit()
            if b == 0:
                choice[i] = 'B'
                b += 1
                c -= 1
            elif c == 0:
                choice[i] = 'C'
                b -= 1
                c += 1
            else:
                if i < n-1 and command[i+1] == 'AC':
                    choice[i] = 'C'
                    b -= 1
                    c += 1
                else:
                    choice[i] = 'B'
                    b += 1
                    c -= 1


        if com == 'AC':
            if b == 2:
                print('No')
                exit()
            if c == 0:
                choice[i] = 'C'
                c += 1
                a -= 1
            elif a == 0:
                choice[i] = 'A'
                c -= 1
                a += 1
            else:
                if i < n-1 and command[i+1] == 'AB':
                    choice[i] = 'A'
                    c -= 1
                    a += 1
                else:
                    choice[i] = 'C'
                    c += 1
                    a -= 1
    
    print('Yes')
    for ch in choice:
        print(ch)



elif summ == 1:
    # １つずつシミュレーション

    choice = [''] * n
    for i, com in enumerate(command):
        if com == 'AB':
            if c == 1:
                print('No')
                exit()
            if a == 0:
                choice[i] = 'A'
                a += 1
                b -= 1
            elif b == 0:
                choice[i] = 'B'
                a -= 1
                b += 1
        if com == 'BC':
            if a == 1:
                print('No')
                exit()
            if b == 0:
                choice[i] = 'B'
                b += 1
                c -= 1
            elif c == 0:
                choice[i] = 'C'
                b -= 1
                c += 1


        if com == 'AC':
            if b == 1:
                print('No')
                exit()
            if c == 0:
                choice[i] = 'C'
                c += 1
                a -= 1
            elif a == 0:
                choice[i] = 'A'
                c -= 1
                a += 1
    
    print('Yes')
    for ch in choice:
        print(ch)

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
d=[[0]*3]
X=('AB','AC','BC')
for i,s in enumerate(S):
    d.append([e for e in d[-1]])
    d[-1][X.index(s)]+=1
d2=[]
for ab,ac,bc in d[1:]:
    d2.append([max(0,0--(ab+ac-A)//2),max(0,0--(ab+bc-B)//2),max(0,0--(ac+bc-C)//2)])
d3=d2[::-1]
r=[]
x,y,z=0,0,0
f=1
for i,s in enumerate(S):
    if s=='AB':
        if x>=d3[i][0]-1 and y>=d3[i][1]:
            x+=1
            r.append('A')
        elif y>=d3[i][1]-1 and x>=d3[i][0]:
            y+=1
            r.append('B')
        else:
            f=0
    elif s=='AC':
        if x>=d3[i][0]-1 and z>=d3[i][2]:
            x+=1
            r.append('A')
        elif z>=d3[i][2]-1 and x>=d3[i][0]:
            z+=1
            r.append('C')
        else:
            f=0
    else:
        if y>=d3[i][1]-1 and z>=d3[i][2]:
            y+=1
            r.append('B')
        elif z>=d3[i][2]-1 and y>=d3[i][1]:
            z+=1
            r.append('C')
        else:
            f=0
if f:
    print('Yes')
    print(*r,sep='\n')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

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

N, A, B, C = map(int, readline().split())
S = list(map(str, read().split()))
v = defaultdict(int)
v['A'] = A
v['B'] = B
v['C'] = C
answer = []
stock = []
for idx, s in enumerate(S):
    i = s[0]
    j = s[1]
    if v[i] == v[j]:
        stock.append((i, j, idx))
        answer.append('stock')
        continue
    flag = False
    if flag:
        for key, cnt in v.items():
            if cnt < 0:
                flag = True
                break
            for i, j, idx_ in stock:
                if key == i:
                    if v[j] == 0:
                        print('No')
                        exit()
                    v[i] += 1
                    v[j] -= 1
                    stock.remove((i, j, idx_))
                    answer[idx_] = i
                    break
                elif key == j:
                    if v[i] == 0:
                        print('No')
                        exit()
                    v[j] += 1
                    v[i] -= 1
                    stock.remove((i, j, idx_))
                    answer[idx_] = j
                    break
            else:
                print('No')
                exit()
    else:
        if v[i] > v[j]:
            answer.append(j)
            v[i] -= 1
            v[j] += 1
        else:
            answer.append(i)
            v[i] += 1
            v[j] -= 1


print('Yes')
for i, j, idx in stock:
    if v[i] > v[j]:
        answer[idx] = j
        v[j] += 1
        v[i] -= 1
    else:
        answer[idx] = i
        v[i] += 1
        v[j] -= 1
print('\n'.join(answer))

Submission status: Wrong Answer

Solution code:
n,a,b,c=[int(b) for b in input().split()]
list1=[]
for i in range(n):
    string=input()
    if string=='AB':
        if a>b :
            list1.append('B')
            b+=1
            a=a-1
            
        elif a<=b :
            list1.append('A')
            a+=1
            b=b-1
               
            
    elif string=='AC':
        if a>c :
            list1.append('C')
            c+=1
            a=a-1
            
        elif a<=c :
            list1.append('A')
            a+=1
           
            c=c-1   
            
    elif string=='BC':
        if b>c :
            list1.append('C')
            c+=1
            
            b=b-1
        elif b<=c :
            list1.append('B')
            b+=1
            
            c=c-1  
            
if a>=0 and b>=0 and c>=0:
    print("YES")
    for z in list1:
        print(z)
else:
    print("NO")

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split(" "))
commands = []
data = {
    'A': A,
    'B': B,
    'C': C
}
ans = [] 

if A >=1 and B >= 1 and C >= 1 :
    print("Yes")
    for _ in range(N) :
        command = input()
        if data[command[0]] > data[command[1]] :
            data[command[0]] -= 1
            data[command[1]] += 1
            print(command[1])
        else :
            data[command[0]] += 1
            data[command[1]] -= 1
            print(command[0]) 
else :
    safe = []
    commands = []
    for _ in range(N) :
        commands.append(input())

    fail = False
    i = 0 
    ans = []
    
    while i < N : 
        if data[commands[i][0]] == 0 and data[commands[i][1]] == 0 :
            if safe == [] :
                fail = True
                break
            else : 
                data, i, _ans = safe.pop()
                ans = ans[:i+1]
                ans.append(_ans)
                
        elif data[commands[i][0]] == 1 and data[commands[i][1]] == 1 :
            _data = { 'A' :data['A'], 'B' :data['B'], 'C' :data['C'] }
            _i = i
            
            _data[commands[i][1]] += 1
            _data[commands[i][0]] -= 1
            _ans = commands[i][1] 
            safe.append( [_data, _i, _ans] )  

            data[commands[i][0]] += 1
            data[commands[i][1]] -= 1
            ans.append( commands[i][0] )
        else :
            if data[commands[i][0]] > data[commands[i][1]] :
                x = commands[i][0] 
                y = commands[i][1] 
            else :
                x = commands[i][1] 
                y = commands[i][0]

            data[x] -= 1 
            data[y] += 1
            ans.append(y)

        i += 1

    if fail :
        print("No")
    else :
        print("Yes")
        for c in ans :
            print(c)

Submission status: Wrong Answer

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

N, A, B, C = map(int, input().split())
Sss = [input().rstrip() for _ in range(N)]

convs = dict(zip(['AB','AC','BC'], [0, 1, 2]))
chs = 'ABC'
Xss = [[0,1], [0,2], [1,2]]

for S in range(8):
#    print()
    Xss = [[0,1], [0,2], [1,2]]
    for i in range(3):
        if (S>>i)&1:
            Xss[i] = Xss[i][::-1]
#    print(bin(S)[2:].zfill(3), Xss)

    anss = []
    Ks = list([A, B, C])
    for Ss in Sss:
#        print(Ks)
        no = convs[Ss]
        x, y = Xss[no]
#        print('no:', no, '/ x:', x, '/ y:', y)
        if Ks[x] == 0:
            anss.append(chs[x])
            Ks[x] += 1
            Ks[y] -= 1
        elif Ks[y] == 0:
            anss.append(chs[y])
            Ks[y] += 1
            Ks[x] -= 1
        else:
            anss.append(chs[x])
            Ks[x] += 1
            Ks[y] -= 1
        Xss[no] = Xss[no][::-1]
        if Ks[x] < 0 or Ks[y] < 0:
            break
    else:
        print('Yes')
        print('\n'.join(anss))
        break
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
x = []
for _ in range(n):
  si = str(input())
  if si == "AB":
    if a== 0 and b== 0:
      print("No")
      exit()
    if a >= 2:
      a -= 1
      b += 1
      x.append("B")
    elif b >= 2:
      b -= 1
      a += 1
      x.append("A")
    elif a >= 1:
      a -= 1
      b += 1
      x.append("B")
    elif b >= 1:
      b -= 1
      a += 1
      x.append("A")
  elif si == "AC":
    if a==0 and c == 0:
      print("No")
      exit()
    if a >= 2:
      a -= 1
      c += 1
      x.append("C")
    elif c >= 2:
      c -= 1
      a += 1
      x.append("A")
    elif a >= 1:
      a -= 1
      c += 1
      x.append("C")
    elif c >= 1:
      c -= 1
      a += 1
      x.append("A")
  elif si == "BC":
    if b == 0 and c == 0:
      print("No")
      exit()
    if b >= 2:
      b -= 1
      c += 1
      x.append("C")
    elif c >= 2:
      c -= 1
      b += 1
      x.append("B")
    elif b >= 1:
      b -= 1
      c += 1
      x.append("C")
    elif c >= 1:
      c -= 1
      b += 1
      x.append("B")
print("Yes")
for xi in x:
  print(xi)

Submission status: Wrong Answer

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

print("Yes")
for i in ans:
	print(i)

Submission status: Wrong Answer

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

def main():
    N,A,B,C = map(int,input().split())
    x,y,z = A+B,B+C,C+A
    s = [input() for _ in range(N)]
    ans = []
    
    for i,st in enumerate(s):
        if st == "AB":
            if x == 0:
                print("No")
                exit()
            if y != z:
                if y > z:
                    y -= 1;z += 1
                    ans.append("A")
                else:
                    z -= 1;y += 1
                    ans.append("B")
            else:
                if y != 1:
                    y -= 1;z += 1
                    ans.append("A")
                else:
                    if i == N-1:
                        y -= 1;z += 1
                        ans.append("A")
                    else:
                        _st = s[i+1]
                        if _st == "AB":
                            y -= 1;z += 1
                            ans.append("A")
                        elif _st == "BC":
                            y += 1;z -= 1
                            ans.append("B")
                        else:
                            y += 1;z -= 1
                            ans.append("C")
        if st == "BC":
            if y == 0:
                print("No")
                exit()
            if z != x:
                if z > x:
                    z -= 1;x += 1
                    ans.append("B")
                else:
                    x -= 1;z += 1
                    ans.append("C")
            else:
                if z != 1:
                    z -= 1;x += 1
                    ans.append("B")
                else:
                    if i == N-1:
                        z -= 1;x += 1
                        ans.append("B")
                    else:
                        _st = s[i+1]
                        if _st == "BC":
                            z -= 1;x += 1
                            ans.append("A")
                        elif _st == "AC":
                            z += 1;x -= 1
                            ans.append("C")
                        else:
                            z += 1;x -= 1
                            ans.append("B")
        if st == "AC":
            if z == 0:
                print("No")
                exit()
            if x != y:
                if x > y:
                    x -= 1;y += 1
                    ans.append("C")
                else:
                    y -= 1;x += 1
                    ans.append("A")
            else:
                if x != 1:
                    x -= 1;y += 1
                    ans.append("C")
                else:
                    if i == N-1:
                        x -= 1;y += 1
                        ans.append("C")
                    else:
                        _st = s[i+1]
                        if _st == "AC":
                            x -= 1;y += 1
                            ans.append("C")
                        elif _st == "AB":
                            x += 1;y -= 1
                            ans.append("A")
                        else:
                            y += 1;x -= 1
                            ans.append("B")
                            
    print("Yes")
    print(*ans,sep="\n")
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
l=list(map(int,input().split()))
if l.count(0)>=2:
    print("No")
else:
    print("Yes")

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 (-sum(n==0 for n in nums), min(nums))
        if min(nums) < 0:
            return (-100, 0)
        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+3, 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 min(nums) < 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:
#!python3

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


def main():
    d = {"AB": (0, 1, 2), "AC": (0, 2, 1), "BC": (1, 2, 0)}
    l = [0] * 3
    for s in S:
        l[d[s][0]] += 1
        l[d[s][1]] += 1
    
    l = [(l[0] - A + 1) // 2, (l[1] - B + 1) // 2, (l[2] - C + 1) // 2]
    dl = ["A", "B", "C"]
    ans = []
    for n in range(N):
        i, j, k = d[S[n]]
        if l[i] < l[j]:
            l[j] -= 1
            ans.append(dl[j])
        else:
            l[i] -= 1
            ans.append(dl[i])
        
    print("Yes")
    print("\n".join(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)


def input():
    return stdin.readline().strip()


N, A, B, C = map(int, input().split())
strings = [input() for _ in range(N)]


def dfs(index, hp):
    if index == N+ 1:
        return ''

    s1, s2 = strings[index-1]
    if hp[s1] > 0:
        hp2 = hp.copy()
        hp2[s1] -= 1
        hp2[s2] += 1
        ret = dfs(index + 1, hp2)
        if ret != False:
            return ret + s2
    if hp[s2] > 0:
        hp2 = hp.copy()
        hp2[s2] -= 1
        hp2[s1] += 1
        ret = dfs(index + 1, hp2)
        if ret != False:
            return ret + s1

    return False


d = {'A': A, 'B': B, 'C': C}
ans = dfs(1, d)
if ans == False:
    print('No')
else:
    for c in ans:
        print(c)

Submission status: Wrong Answer

Solution code:

N, a, b, c, *S = open(0).read().split()
N, a, b, c = map(int, (N, a, b, c))

flg = a*b*c == 0 and (a*b == 1 or a*c == 1 or b*c == 1)

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if flg and c == 0 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            elif s_ == 'BC':
                ans.append('B')
                a -= 1
                b += 1
                continue

        ans.append('B' if a > b else 'A')
        if a > b:
            a -= 1
            b += 1
        else:
            a += 1
            b -= 1

    elif s == 'AC':
        if flg and b == 0 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            elif s_ == 'BC':
                ans.append('C')
                a -= 1
                c += 1
                continue

        ans.append('C' if a > c else 'A')
        if a > c:
            a -= 1
            c += 1
        else:
            a += 1
            c -= 1

    elif s == 'BC':
        if flg and a == 0 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
            elif s_ == 'AC':
                ans.append('C')
                b -= 1
                c += 1
                continue

        ans.append('C' if b > c else 'B')
        if b > c:
            b -= 1
            c += 1
        else:
            b += 1
            c -= 1

    if a < 0 or b < 0 or c < 0:
        print('No')
        exit()


print('Yes')
[print(r) for r in ans]

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    

    
    def simulation(a, b, c):
        result = []
        for com in commands:
            if com == 'AB':
                if a == 0 and b == 0:
                    return False, result
                if a < b:
                    result.append('A')
                    a += 1
                    b -= 1
                else:
                    result.append('B')
                    b += 1
                    a -= 1
            elif com == 'BC':
                if b == 0 and c == 0:
                    return False, result
                if b < c:
                    result.append('B')
                    b += 1
                    c -= 1
                else:
                    result.append('C')
                    c += 1
                    b -= 1            
            else:
                if c == 0 and a == 0:
                    return False, result
                if c < a:
                    result.append('C')
                    c += 1
                    a -= 1
                else:
                    result.append('A')
                    a += 1
                    c -= 1
        return True, result    

    
    n, a, b, c  = mi()
    t = [a, b, c]
    commands = [input() for _ in range(n)]
    if t.count(0) == 3:
        print('No')
    elif t.count(0) == 2:
        predicate, result = simulation(a, b, c)
        if predicate:
            print(*result, sep='\n')
        else:
            print('No')
    else:
        predicate, result = simulation(a, b, c)
        if predicate:
            print(*result, sep='\n')
        else:
            print('No')

if __name__ == "__main__":
    main()