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

Solution (Python):
import sys
input = sys.stdin.readline

n,a,b,c = map(int,input().split())
ans = []
abc = [a,b,c]

d = {'AB\n':(0,1),'AC\n':(0,2),'BC\n':(1,2)}

if(a+b+c==0):
    print('No')
    exit()

if(a+b+c >= 3)|(a+b+c==1):
    for _ in range(n):
        s = input()
        x,y = d[s]
        if(abc[x]==0)&(abc[y]==0):
            print('No')
            exit()
        if(abc[x] < abc[y]):
            x,y = y,x
        abc[x] -= 1
        abc[y] += 1
        ans.append(y)

elif(a+b+c==2):
    ss = [input() for _ in range(n)]
    if(max(abc)==2):
        
        case = True
    else:
        case = False

    for i in range(n):
        s = ss[i]
        x,y = d[s]
        if(case)|(i==n-1):
            if(abc[x]==0)&(abc[y]==0):
                print('No')
                exit()
            if(abc[x] < abc[y]):
                x,y = y,x
            abc[x] -= 1
            abc[y] += 1
            case = False
        else:
            if(abc[x]!=abc[y]):
                if(abc[x] < abc[y]):
                    x,y = y,x
                abc[x] -= 1
                abc[y] += 1
                case = False
            else:
                sn = ss[i+1]
                if(x in d[sn]):
                    x,y = y,x
                abc[x] -= 1
                abc[y] += 1
                case = True
        ans.append(y)

print('Yes')
for i in ans:
    print(chr(i+ord('A')))

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

def add(c1,c2):
    global a,b,c
    ans.append(c1)
    if c1=="A":
        a+=1
    if c1=="B":
        b+=1
    if c1=="C":
        c+=1
    if c2=="A":
        a-=1
    if c2=="B":
        b-=1
    if c2=="C":
        c-=1

for i in range(n):
    que = s[i]
    nxt="NONE"
    if i!=n-1:
        nxt = s[i+1]

    if que=="AB":
        if a==b==0:
            print("No")
            exit()
        if a==b==1:
            if nxt=="NONE":
                add("A","B")
            elif "A" in nxt:
                add("A","B")
            else:
                add("B","A")
        elif a>=b:
            add("B","A")
        else:
            add("A","B")
    elif que=="AC":
        if a==c==0:
            print("No")
            exit()
        if a==c==1:
            if nxt=="NONE":
                add("A","C")
            elif "A" in nxt:
                add("A","C")
            else:
                add("C","A")
        elif a>=c:
            add("C","A")
        else:
            add("A","C")
    else: 
        if b==c==0:
            print("No")
            exit()
        if b==c==1:
            if nxt=="NONE":
                add("B","C")
            elif "B" in nxt:
                add("B","C")
            else:
                add("C","B")
        elif b>=c:
            add("C","B")
        else:
            add("B","C")

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

Solution (Python):
n,a,b,c = map(int, input().split())
d = {}
d["A"] = a
d["B"] = b
d["C"] = c

pl = [input() for _ in range(n)]

res = []
for i in range(n):
    p = pl[i]
    if d[p[0]]==d[p[1]]==0:
        print("No")
        exit()
    elif d[p[0]]==0:
        res.append(p[0])
        d[p[0]] += 1
        d[p[1]] -= 1
    elif d[p[1]]==0:
        res.append(p[1])
        d[p[0]] -= 1
        d[p[1]] += 1
    elif i==n-1:
        res.append(p[0])
    elif p[0] in pl[i+1]:
        res.append(p[0])
        d[p[0]] += 1
        d[p[1]] -= 1
    else:
        res.append(p[1])
        d[p[0]] -= 1
        d[p[1]] += 1

    



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

Solution (Python):
import sys
sys.setrecursionlimit(1<<30)

idx = 'ABC'
N, *vals = map(int, input().split())
evs = [tuple(map(idx.index, input())) for _ in range(N)]
path = []

def f(i):
	if i == len(evs):
		print('Yes')
		print('\n'.join(path))
		raise SystemExit

	k, j = evs[i]
	for add, sub in ((k, j), (j, k)):
		if vals[sub] == 0: continue
		vals[add] += 1
		vals[sub] -= 1
		path.append(idx[add])

		f(i+1)

		vals[add] -= 1
		vals[sub] += 1
		path.pop()

f(0)

print('No')

Solution (Python):
N, A, B, C = map(int, input().split())
inputs = tuple(tuple(input()) for _ in range(N))

dic = {
    "A": A,
    "B": B,
    "C": C
}

ans = [0] * N
depth = 0
while depth < N:
    key1, key2 = inputs[depth]
    if dic[key1] < dic[key2]:
        dic[key1] += 1
        dic[key2] -= 1
        ans[depth] = key1
    elif dic[key1] > dic[key2]:
        dic[key2] += 1
        dic[key1] -= 1
        ans[depth] = key2
    else:
        if not dic[key1]:
            break
        elif dic[key1] == 1 and depth + 1 < N:
            next_key1, next_key2 = inputs[depth + 1]
            common_key, *_ = {key1, key2} & {next_key1, next_key2}
            else_key, *_ = {key1, key2} - {common_key}
            dic[common_key] += 1
            dic[else_key] -= 1
            ans[depth] = common_key
        else:
            dic[key1] += 1
            dic[key2] -= 1
            ans[depth] = key1
    depth += 1
else:
    print("Yes")
    print(*ans, sep="\n")
    quit()
print("No")

Solution (Python):
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)

MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)

N,A,B,C = MI()
s = LS2(N)
abc = [A,B,C]
dic = {'A':0,'B':1,'C':2}
redic = {0:'A',1:'B',2:'C'}
f=0
select = []
for i in range(N):
    order = s[i][0]
    x = dic[order[0]]
    y = dic[order[1]]
    xnum=abc[x]
    ynum=abc[y]
    if xnum>ynum:
        select.append(y)
        abc[x] -= 1
        abc[y] += 1
    elif xnum==ynum:
        if i<N-1:
            next = s[i+1][0]
            nextx = dic[next[0]]
            nexty = dic[next[1]]
            if x==nextx or x==nexty:
                select.append(x)
                abc[x] += 1
                abc[y] -= 1
            elif y==nextx or y==nexty:
                select.append(y)
                abc[y] += 1
                abc[x] -= 1
            else:
                print(00)
        else:
            select.append(x)
            abc[x] += 1
            abc[y] -= 1
    else:
        select.append(x)
        abc[x] += 1
        abc[y] -= 1
    for i in abc:
        if i<0:
            print('No')
            f=1
            break
    if f==1:
        break
else:
    print('Yes')
    [print(redic[x]) for x in select]

Solution (Python):

n,a,b,c = list(map(int,input().split()))
abc = [a,b,c]
s = [tuple(map(lambda x:ord(x)-97,list(input().lower()))) for i in range(n)]

from copy import deepcopy

s.append(deepcopy(s[-1]))
ret = True
ret_choice = []
ra = ret_choice.append
for i,j in zip(s,s[1::]):
    
    if abc[i[0]] >=3  and abc[i[1]] >=3:
        abc[i[0]]+=1
        abc[i[1]]-=1
        ra(i[0])
    elif abc[i[0]] >=3 and abc[i[1]] < 3:
        abc[i[0]]-=1
        abc[i[1]]+=1
        ra(i[1])
    elif abc[i[0]] <3 and abc[i[1]] >= 3:
        abc[i[0]]+=1
        abc[i[1]]-=1
        ra(i[0])
    elif abc[i[0]]==0:
        abc[i[0]]+=1
        abc[i[1]]-=1
        ra(i[0])
    elif abc[i[1]]==0:
        abc[i[0]]-=1
        abc[i[1]]+=1
        ra(i[1])
    elif i[0] in j:
        abc[i[0]]+=1
        abc[i[1]]-=1
        ra(i[0])
    else:
        abc[i[0]]-=1
        abc[i[1]]+=1
        ra(i[1])
    if -1 in abc:
        ret = False
        break

if ret:
    print("Yes")
    for i in (map(lambda x:chr(x+65),ret_choice)):
        print(i)
else:
    print("No")

Solution (Python):
import sys
sys.setrecursionlimit(1<<30)

idx = 'ABC'
N, *vals = map(int, input().split())
evs = [tuple(map(idx.index, input())) for _ in range(N)]
path = []

def f(i):
	if i == len(evs):
		print('Yes')
		print('\n'.join(path))
		raise SystemExit

	k, j = evs[i]
	for add, sub in ((k, j), (j, k)):
		if vals[sub] == 0: continue
		vals[add] += 1
		vals[sub] -= 1
		path.append(idx[add])

		f(i+1)

		vals[add] -= 1
		vals[sub] += 1
		path.pop()

f(0)

print('No')

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




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



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

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

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

Solution (Python):
import sys
def input():
	return sys.stdin.readline()[:-1]

def included(x):
	if x == "AB":
		return 0, 1
	elif x == "BC":
		return 1, 2
	else:
		return 0, 2

def ans(x):
	l = ["A", "B", "C"]
	return l[x]

n, a, b, c = map(int, input().split())
nums = [a, b, c]
s = [input() for _ in range(n)]
res = []
for i, x in enumerate(s):
	y, z = included(x)
	if nums[y] == nums[z] == 0:
		print("No")
		break
	if i == n-1:
		if nums[y] >= nums[z]:
			res.append(ans(z))
		elif nums[z] > nums[y]:
			res.append(ans(y))
	else:
		if nums[y] > nums[z]:
			res.append(ans(z))
			nums[y] -= 1
			nums[z] += 1
		elif nums[z] > nums[y]:
			res.append(ans(y))
			nums[z] -= 1
			nums[y] += 1
		elif z in included(s[i+1]):
			res.append(ans(z))
			nums[y] -= 1
			nums[z] += 1
		else:
			res.append(ans(y))
			nums[z] -= 1
			nums[y] += 1
else:
	print("Yes")
	print(*res, sep="\n")

Solution (Python):

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+c == 2

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if flg and a*b == 1 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 a*c == 1 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 b*c == 1 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]

Solution (Python):



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


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


if __name__ == "__main__":
    main()

Solution (Python):
import sys
sys.setrecursionlimit(1<<30)

idx = 'ABC'
N, *vals = map(int, input().split())
evs = [tuple(map(idx.index, input())) for _ in range(N)]

def f(i, vals, path):
	if i == len(evs):
		print('Yes')
		print('\n'.join(path))
		raise SystemExit

	k, j = evs[i]
	for add, sub in ((k, j), (j, k)):
		if vals[sub] == 0: continue
		vals[add] += 1
		vals[sub] -= 1
		path.append(idx[add])

		f(i+1, vals, path)

		vals[add] -= 1
		vals[sub] += 1
		path.pop()

f(0, vals, [])

print('No')

Solution (Python):
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:
        return ''

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

    return False


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

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

summ = a + b + c
if summ == 0:
    print('No')
    exit()

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)

Solution (Python):
import math


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

    
    
    
    

    history = []

    cnt = {
        'A': A,
        'B': B,
        'C': C,
    }

    for i in range(N):
        x, y = s[i][0], s[i][1]
        if cnt[x] > cnt[y]:
            cnt[x] -= 1
            cnt[y] += 1
            history.append(y)
        elif cnt[x] < cnt[y]:
            cnt[x] += 1
            cnt[y] -= 1
            history.append(x)
        else:
            if x in s[i + 1]:
                cnt[x] += 1
                cnt[y] -= 1
                history.append(x)
            else:
                cnt[x] -= 1
                cnt[y] += 1
                history.append(y)

        if cnt[x] < 0 or cnt[y] < 0:
            print('No')
            return

    print('Yes')
    for c in history:
        print(c)


if __name__ == '__main__':
    main()

Solution (Python):
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]














if sum(s) == 2:
    if s.count(0) == 1:
        now = s.index(0)
        i = 0
    else:
        if T[0] != s.index(2):
            now = T[0]
            i = 1
            nxt = [0,1,2]
            nxt.remove(T[0])
            nxt.remove(s.index(2))
            ans[0] = ABC[nxt[0]]
        else:
            print('No')
            exit(0)
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        if T[i] == now:
            for j in range(3):
                if j not in [now,T[i]]:ans[i] = ABC[j]
        else:
            ans[i] = ABC[now]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Solution (Python):
N, A, B, C = map(int, input().split())
opts = []
for _ in range(N):
    opt = input()
    opts.append((ord(opt[0]) - ord("A"), ord(opt[1]) - ord("A")))

choices = []
nums = [A, B, C]
for i, opt in enumerate(opts):
    if nums[opt[0]] == 0 and nums[opt[1]] == 0:
        print("No")
        break
    if nums[opt[0]] == 1 and nums[opt[1]] == 1 and i < N - 1 and opt != opts[i + 1]:
        choice = (set(opt) & set(opts[i + 1])).pop()
    elif nums[opt[0]] >= nums[opt[1]]:
        choice = opt[1]
    else:
        choice = opt[0]
    choices.append(choice)
    nums[choice] += 1
    nums[choice ^ opt[0] ^ opt[1]] -= 1
else:
    print("Yes")
    for c in choices:
        print(chr(c + ord("A")))

Solution (Python):
import sys
def main():
    input = sys.stdin.readline
    N,A,B,C=map(int, input().split())
    S=[input().rstrip() for _ in range(N)]

    MX=2
    dp=[[[[None]*(MX+1) for _ in range(MX+1)] for __ in range(MX+1)] for ___ in range(N+4)]
    dp[0][min(MX,A)][min(MX,B)][min(MX,C)]=1
    for i,s in enumerate(S):
        ni=i+1
        for a in range(MX+1):
            for b in range(MX+1):
                for c in range(MX+1):
                    for j in range(2):
                        na,nb,nc=a,b,c
                        sel=None
                        if s=='AB':
                            if j==0:
                                if b==0: continue
                                na,nb=min(MX,na+1),nb-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nb=na-1,min(MX,nb+1)
                                sel='B'
                        elif s=='AC':
                            if j==0:
                                if c==0: continue
                                na,nc=min(MX,na+1),nc-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nc=na-1,min(MX,nc+1)
                                sel='C'
                        elif s=='BC':
                            if j==0:
                                if c==0: continue
                                nb,nc=min(MX,nb+1),nc-1
                                sel='B'
                            else:
                                if b==0: continue
                                nb,nc=nb-1,min(MX,nc+1)
                                sel='C'
                        if dp[i][a][b][c]: dp[ni][na][nb][nc]=(a,b,c,sel)

    flag=False
    ga,gb,gc=0,0,0
    for a in range(MX+1):
        for b in range(MX+1):
            for c in range(MX+1):
                if dp[N][a][b][c]:
                    flag=True
                    ga,gb,gc=a,b,c
    if flag==False:
        print('No')
        return
    print('Yes')
    ans=[]
    for i in range(N,0,-1):
        ga,gb,gc,sel=dp[i][ga][gb][gc]
        ans.append(sel)
    for a in reversed(ans):
        print(a)

if __name__ == '__main__':
    main()

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

ok = True

s_l = []

ans_s = []

for i in range(n):
    s_l.append(input())

for i in range(n):
    s = s_l[i]
    if i < n-1:
        nex = s_l[i+1]
    else:
        nex = 'NONO'

    if s == 'AB':
        if a < b:
            a += 1
            b -= 1
            ans_s.append('A')
        elif a > b:
            a -= 1
            b += 1
            ans_s.append('B')
        elif nex == 'BC':
            a -= 1
            b += 1
            ans_s.append('B')
        else:
            a += 1
            b -= 1
            ans_s.append('A')
    elif s == 'AC':
        if a < c:
            a += 1
            c -= 1
            ans_s.append('A')
        elif a > c:
            a -= 1
            c += 1
            ans_s.append('C')
        elif nex == 'BC':
            a -= 1
            c += 1
            ans_s.append('C')
        else:
            a += 1
            c -= 1
            ans_s.append('A')
    else:
        if b < c:
            b += 1
            c -= 1
            ans_s.append('B')
        elif b > c:
            b -= 1
            c += 1
            ans_s.append('C')
        elif nex == 'AB':
            c -= 1
            b += 1
            ans_s.append('B')
        else:
            c += 1
            b -= 1
            ans_s.append('C')

    if a < 0 or b < 0 or c < 0:
        ok = False
        break

if ok:
    print('Yes')
    for s_e in ans_s:
        print(s_e)
else:
    print('No')

Solution (Python):
def mp():return map(int,input().split())
def lmp():return list(map(int,input().split()))


n,a,b,c = mp()
ans_ar = []
q = []
for i in range(n):
    s = input()
    q.append(s)
for i in range(n):
    s = q[i]
    if s == "AB":
        if a == b == 0:
            print("No")
            exit()
        elif a == b == 1 and i != n-1:
            if q[i+1] == "AC":
                a += 1
                b -= 1
                ans_ar.append("A")
            else:
                a -= 1
                b += 1
                ans_ar.append("B")
        elif a >= b:
            a -= 1
            b += 1
            ans_ar.append("B")
        else:
            a += 1
            b -= 1
            ans_ar.append("A")
    if s == "AC":
        if a == c == 0:
            print("No")
            exit()
        elif a == c == 1 and i != n - 1:
            if q[i + 1] == "AB":
                a += 1
                c -= 1
                ans_ar.append("A")
            else:
                a -= 1
                c += 1
                ans_ar.append("C")

        elif a >= c:
            a -= 1
            c += 1
            ans_ar.append("C")
        else:
            a += 1
            c -= 1
            ans_ar.append("A")
    if s == "BC":
        if c == b == 0:
            print("No")
            exit()
        elif c == b == 1 and i != n-1:
            if q[i+1] == "AC":
                c += 1
                b -= 1
                ans_ar.append("C")
            else:
                c -= 1
                b += 1
                ans_ar.append("B")

        elif c >= b:
            c -= 1
            b += 1
            ans_ar.append("B")
        else:
            c += 1
            b -= 1
            ans_ar.append("C")
print("Yes")
for i in ans_ar:
    print(i)