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):
def solve(writer):
    N, A, B, C = map(int, input().split())
    variables = {
        'A': A,
        'B': B,
        'C': C
    }

    queries = (list(input()) for _ in range(N))

    buffer = list()
    b = ''

    for i in range(N):
        if not buffer:
            t1, t2 = next(queries)
        else:
            b = buffer.pop()
            if b == 'pass':
                continue
            else:
                t1, t2 = list(b)

        v1 = variables[t1]
        v2 = variables[t2]

        if v1 == v2 == 0:
            return False
        elif v1 == v2 == 1:
            if i == N-1:
                writer.append(t1)
                return True

            nt1, nt2 = next(queries)
            if len({t1, t2, nt1, nt2}) == 2:
                buffer.append('pass')
                writer.append(t1)
                writer.append(t2)
                continue

            if t1 in {nt1, nt2}:
                variables[t1] += 1
                variables[t2] -= 1
                writer.append(t1)
            else:
                variables[t1] -= 1
                variables[t2] += 1
                writer.append(t2)

            buffer.append(''.join([nt1, nt2]))

        elif v1 > v2:
            variables[t1] -= 1
            variables[t2] += 1
            writer.append(t2)
        elif v2 > v1:
            variables[t1] += 1
            variables[t2] -= 1
            writer.append(t1)
        else:
            variables[t1] += 1
            variables[t2] -= 1
            writer.append(t1)

    return True


def main():
    writer = list()
    ok = solve(writer)
    if ok:
        print('Yes')
        for w in writer:
            print(w)
    else:
        print('No')


if __name__ == "__main__":
    main()

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

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

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

for ans_i in ans:
    print(ans_i)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 6)

Q, A, B, C = map(int, input().split())

def conv(s):
    return (0, 1) if s == "AB" else (1, 2) if s == "BC" else (0, 2)

S = [conv(input()) for i in range(Q)]
N = [A, B, C]
ans = []

def dfs(i):
    global ans, N
    if N[0] < 0 or N[1] < 0 or N[2] < 0:
        return
    if i == Q:
        print("Yes")
        for idx in ans:
            print("ABC"[idx])
        exit()
    x, y = S[i]
    for _ in range(2):
        N[x] += 1; N[y] -= 1
        ans.append(x)
        dfs(i + 1)
        N[x] -= 1; N[y] += 1
        ans.pop()
        x, y = y, x

dfs(0)

print("No")

Solution (Python):
import sys
sys.setrecursionlimit(1000000)

def F(S,curr,D,res):
    if D["A"]<0 or D["B"]<0 or D["C"]<0:
        return False
    if len(S)==curr:
        print("Yes")
        for c in res:
            print(c)
        
        return True
    s=S[curr]
    
    a=False
    b=False
    if D[s[0]]>=1:
        D[s[0]]-=1
        D[s[1]]+=1
        res.append(s[1])
        a=F(S,curr+1,D,res)
        res.pop(-1)
        D[s[0]]+=1
        D[s[1]]-=1
    if a:
        return a

    if D[s[1]]>=1:
            D[s[1]]-=1
            D[s[0]]+=1
            res.append(s[0])
            b=F(S,curr+1,D,res)
            res.pop()
            D[s[1]]+=1
            D[s[0]]-=1
    return a or b


N,A,B,C=map(int,input().strip().split())
D={"A":A,"B":B,"C":C}
S=[]
res=[]
for i in range(N):
    S.append(input())
if not (F(S,0,D,res)):
    print("No")

Solution (Python):
import sys

def str_to_int(ch):
	if ch =='A':
		return 0
	if ch =='B':
		return 1
	if ch =='C':
		return 2
def int_to_str(ch):
	if ch ==0:
		return 'A'
	if ch ==1:
		return 'B'
	if ch ==2:
		return 'C'

N, A, B, C = list(map(int, input().split()))
ABC =[0, 0, 0]
ABC[0] = A
ABC[1] = B
ABC[2] = C

ops = []
for _ in range(N):
	l =list(map(str_to_int, list(input())))
	ops.append(l)

if sum(ABC) == 0:
	print("No")
	sys.exit(0)

ans = []
for idx, op in enumerate(ops):
	n0 = ABC[op[0]]
	n1 = ABC[op[1]]
	if n0 == 0 and n1 == 0:
		print("No")
		sys.exit(0)
	elif n1 == 0:
		inc = op[1]
		dec = op[0]
	elif n0 == 0:
		inc = op[0]
		dec = op[1]
	elif idx +1 < len(ops) and op[0] in ops[idx+1]:
		inc = op[0]
		dec = op[1]
	else:
		inc = op[1]
		dec = op[0]
	ans.append(inc)
	ABC[dec]-=1
	ABC[inc]+=1
print("Yes")
for i in map(int_to_str, ans):
	print(i)
sys.exit(0)

Solution (Python):



S, stack = [], []
N = 0


def main() -> None:
    global N
    N, A, B, C = rmi()
    for n in range(N):
        S.append(r())
    enable = dfs(stack, 0, A, B, C)
    w('Yes' if enable else 'No')
    if enable:
        wm(*stack, delim='\n')


def dfs(stack, index, a, b, c) -> bool:
    if N <= index:
        return True
    inst = S[index]
    if inst == 'AB':
        if a <= 0 and b <= 0:
            return False
        if 0 < a:
            stack.append('B')
            result = dfs(stack, index + 1, a - 1, b + 1, c)
            if result:
                return True
            else:
                stack.pop()
        if 0 < b:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b - 1, c)
            if result:
                return True
            else:
                stack.pop()
    elif inst == 'AC':
        if a <= 0 and c <= 0:
            return False
        if 0 < a:
            stack.append('C')
            result = dfs(stack, index + 1, a - 1, b, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if 0 < c:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b, c - 1)
            if result:
                return True
            else:
                stack.pop()
    else:
        if b <= 0 and c <= 0:
            return False
        if 0 < b:
            stack.append('C')
            result = dfs(stack, index + 1, a, b - 1, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if 0 < c:
            stack.append('B')
            result = dfs(stack, index + 1, a, b + 1, c - 1)
            if result:
                return True
            else:
                stack.pop()
    return False


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10**9)
    main()

Solution (Python):


import sys

sys.setrecursionlimit(10**8)


S, stack = [], []
N = 0


def main() -> None:
    global N
    N, A, B, C = rmi()
    for n in range(N):
        S.append(r())
    enable = dfs(stack, 0, A, B, C)
    w('Yes' if enable else 'No')
    if enable:
        wm(*stack, delim='\n')


def dfs(stack, index, a, b, c) -> bool:
    if N <= index:
        return True
    inst = S[index]
    if inst == 'AB':
        if a <= 0 and b <= 0:
            return False
        if 0 < a:
            stack.append('B')
            result = dfs(stack, index + 1, a - 1, b + 1, c)
            if result:
                return True
            else:
                stack.pop()
        if 0 < b:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b - 1, c)
            if result:
                return True
            else:
                stack.pop()
    elif inst == 'AC':
        if a <= 0 and c <= 0:
            return False
        if 0 < a:
            stack.append('C')
            result = dfs(stack, index + 1, a - 1, b, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if 0 < c:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b, c - 1)
            if result:
                return True
            else:
                stack.pop()
    else:
        if b <= 0 and c <= 0:
            return False
        if 0 < b:
            stack.append('C')
            result = dfs(stack, index + 1, a, b - 1, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if 0 < c:
            stack.append('B')
            result = dfs(stack, index + 1, a, b + 1, c - 1)
            if result:
                return True
            else:
                stack.pop()
    return False


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    main()

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

        if A < 0 or B < 0:
            print("No")
            exit()
    elif s == "BC":
        if B == 0:
            B += 1
            C -= 1
            ans.append("B")
        elif C == 0:
            B -= 1
            C += 1
            ans.append("C")
        else:
            if i + 1 == N or S[i + 1] == "BC":
                B += 1
                C -= 1
                ans.append("B")
            else:
                if S[i + 1] == "AB":
                    B += 1
                    C -= 1
                    ans.append("B")
                else:
                    B -= 1
                    C += 1
                    ans.append("C")

        if B < 0 or C < 0:
            print("No")
            exit()
    else:
        if C == 0:
            C += 1
            A -= 1
            ans.append("C")
        elif A == 0:
            C -= 1
            A += 1
            ans.append("A")
        else:
            if i + 1 == N or S[i + 1] == "AC":
                A += 1
                C -= 1
                ans.append("A")
            else:
                if S[i + 1] == "AB":
                    A += 1
                    C -= 1
                    ans.append("A")
                else:
                    A -= 1
                    C += 1
                    ans.append("C")

        if A < 0 or C < 0:
            print("No")
            exit()
print("Yes")
for x in ans:
    print(x)

Solution (Python):

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


V = {"A":A, "B":B, "C":C}
S.append("XX")
op = ""
for i in range(N):
    s0, s1 = S[i]
    if V[s0] + V[s1] == 0:
        break
    
    x, y = s0, s1
    if V[s1] == 0 or (V[s0] != 0 and s1 in S[i+1]):
        x, y = s1, s0

    V[x] += 1
    V[y] -= 1
    op += x


if len(op) != N:
    print("No")
else:
    print("Yes")
    [print(x) for x in op]

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)



INF = float("inf")
MOD = 10 ** 9 + 7

N, A, B, C = map(int, input().split())
num = [A, B, C]
S = []
for i in range(N):
    S.append(input().strip())
S.append('AB')

ans = []
for i in range(N):
    s1 = S[i][0]
    s2 = S[i][1]
    n1 = num["ABC".index(s1)]
    n2 = num["ABC".index(s2)]

    if n1 == n2 == 0:
        print("No")
        exit()

    s_add = ""
    s_sub = ""
    if n1 == 0:
        s_add = s1
        s_sub = s2
    elif n2 == 0:
        s_add = s2
        s_sub = s1
    elif s1 in S[i + 1]:
        s_add = s1
        s_sub = s2
    else:
        s_add = s2
        s_sub = s1

    num["ABC".index(s_add)] += 1
    num["ABC".index(s_sub)] -= 1
    ans.append(s_add)

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

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)



INF = float("inf")
MOD = 10 ** 9 + 7

N, A, B, C = map(int, input().split())
num = [A, B, C]
S = []
for i in range(N):
    S.append(input().strip())
S.append('AB')

ans = []
for i in range(N):
    s = S[i]
    s1 = s[0]
    s2 = s[1]
    n1 = num["ABC".index(s1)]
    n2 = num["ABC".index(s2)]
    if n1 == n2 == 0:
        print("No")
        exit()
    if n1 == 0:
        num["ABC".index(s1)] += 1
        num["ABC".index(s2)] -= 1
        ans.append(s1)
        continue
    if n2 == 0:
        num["ABC".index(s1)] -= 1
        num["ABC".index(s2)] += 1
        ans.append(s2)
        continue
    if s1 in S[i + 1]:
        num["ABC".index(s1)] += 1
        num["ABC".index(s2)] -= 1
        ans.append(s1)
    else:
        num["ABC".index(s1)] -= 1
        num["ABC".index(s2)] += 1
        ans.append(s2)

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

Solution (Python):
N, *ABC = map(int, input().split())
s = [input() for _ in range(N)]

yes_flag = True
ans_list = []

if sum(ABC) == 0:
    yes_flag = False

for i in range(N):
    if not yes_flag:
        break
    u = ord(s[i][0]) - 65
    v = ord(s[i][1]) - 65
    if ABC[u] + ABC[v] == 0:
        yes_flag = False
    elif ABC[u] + ABC[v] == 1:
        if ABC[u] == 1:
            ans_list += [s[i][1]]
            ABC[u] -= 1
            ABC[v] += 1
        else:
            ans_list += [s[i][0]]
            ABC[v] -= 1
            ABC[u] += 1
    elif ABC[u] + ABC[v] == 2:
        
        if ABC[u] * ABC[v] == 1:
            if i == N-1:
                if ABC[u] > 0:
                    ans_list += [s[i][1]]
                else:
                    ans_list += [s[i][0]]
            else:
                if s[i][1] in s[i+1]:
                    ans_list += [s[i][1]]
                    ABC[u] -= 1
                    ABC[v] += 1
                else:
                    ans_list += [s[i][0]]
                    ABC[v] -= 1
                    ABC[u] += 1
        else:
            if ABC[u] > 1:
                ans_list += [s[i][1]]
                ABC[u] -= 1
                ABC[v] += 1
            else:
                ans_list += [s[i][0]]
                ABC[v] -= 1
                ABC[u] += 1
    else:
        if ABC[u] > 1:
            ans_list += [s[i][1]]
            ABC[u] -= 1
            ABC[v] += 1
        else:
            ans_list += [s[i][0]]
            ABC[v] -= 1
            ABC[u] += 1

if yes_flag:
    print("Yes")
    print(*ans_list)
else:
    print("No")

Solution (Python):
import sys

def func(op, val):
    target = []

    for i, o in enumerate(op):
        if sum([val[k] for k in o]) == 0:
            return []
        k0 = o[0]
        k1 = o[1]
        if val[k0] == 0:
            p,m = k0,k1
        elif val[k1] == 0:
            p,m = k1,k0
        elif i < len(op)-1 and k1 in op[i+1]:
            p,m = k1,k0
        else:
            p,m = k0,k1
        val[p] += 1
        val[m] -= 1
        target.append(p)
    return target

n,a,b,c = [int(x) for x in input().split()]
operation = [sys.stdin.readline().strip() for _ in range(n)]
val = {'A':a, 'B':b, 'C':c}

target = func(operation,val)

if len(target) == 0:
    print("No")
else:
    print("Yes")
    for t in target:
        print(t)

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

if a+b+c==0:
  del ans[:]
  ans+=["No"]
  
if a+b+c==1:
  for j in range(n):
    
    if s[j]=="AB":
      if c==1:
        del ans[:]
        ans+=["No"]
        break        
      elif a==1:
        a-=1
        b+=1
        ans+=["B"]
      elif b==1:
        a+=1
        b-=1
        ans+=["A"]

    if s[j]=="AC":
      if b==1:
        del ans[:]
        ans+=["No"]
        break       
      elif a==1:
        a-=1
        c+=1
        ans+=["C"]
      elif c==1:
        a+=1
        c-=1
        ans+=["A"] 

    if s[j]=="BC":
      if a==1:
        del ans[:]
        ans+=["No"]
        break
      elif b==1:
        b-=1
        c+=1
        ans+="C"
      elif c==1:
        b+=1
        c-=1
        ans+=["B"]  

if a+b+c==2:
  for j in range(n):
    if s[j]=="AB":
      if a==2:
        a-=1
        b+=1
        ans+=["B"]
      elif b==2:
        a+=1
        b-=1
        ans+=["A"]
      elif c==2:
        del ans[:]
        ans+=["No"]
        break     
      elif (a==1 and b==1) and (s[j+1]=="AB" or s[j+1]=="AC" or s[j+1]=="XX"):
        a+=1
        b-=1
        ans+=["A"]
      elif (a==1 and b==1) and (s[j+1]=="BC"):
        a-=1
        b+=1
        ans+=["B"]
      elif a==1 and c==1:
        a-=1
        b+=1
        ans+=["B"]
      elif b==1 and c==1:
        a+=1
        b-=1
        ans+=["A"]
    
    if s[j]=="AC":
      if a==2:
        a-=1
        c+=1
        ans+=["C"]
      elif c==2:
        a+=1
        c-=1
        ans+=["A"] 
      elif b==2:
        del ans[:]
        ans+=["No"]
        break     
      elif (a==1 and c==1) and (s[j+1]=="AB" or s[j+1]=="AC" or s[j+1]=="XX"):
        a+=1
        c-=1
        ans+=["A"]
      elif (a==1 and c==1) and (s[j+1]=="BC"):
        a-=1
        c+=1
        ans+=["C"]
      elif a==1 and b==1:
        a-=1
        c+=1
        ans+=["C"]
      elif b==1 and c==1:
        a+=1
        c-=1
        ans+=["A"]

    if s[j]=="BC":
      if c==2:
        c-=1
        b+=1
        ans+=["B"]
      elif b==2:
        c+=1
        b-=1
        ans+=["C"] 
      elif a==2:
        del ans[:]
        ans+=["No"]
        break     
      elif (c==1 and b==1) and (s[j+1]=="AB" or s[j+1]=="BC" or s[j+1]=="XX"):
        b+=1
        c-=1
        ans+=["B"]
      elif (c==1 and b==1) and (s[j+1]=="AC"):
        b-=1
        c+=1
        ans+=["C"]
      elif a==1 and c==1:
        c-=1
        b+=1
        ans+=["B"]
      elif b==1 and a==1:
        c+=1
        b-=1
        ans+=["C"]        

if a+b+c>=3:
  if s[0]=="AB" and a+b==0 or s[0]=="AC" and a+c==0 or s[0]=="BC" and b+c==0:
    del ans[:]
    ans+=["No"]
  
  else:
    for j in range(n):
      if s[j]=="AB":
        if a>b:
          a-=1
          b+=1
          ans+=["B"]
        elif a<=b:
          a+=1
          b-=1
          ans+=["A"]
        
      if s[j]=="AC":
        if a>c:
          a-=1
          c+=1
          ans+=["C"]
        elif a<=c:
          a+=1
          c-=1
          ans+=["A"]     

      if s[j]=="BC":
        if b>c:
          b-=1
          c+=1
          ans+=["C"]
        elif b<=c:
          b+=1
          c-=1
          ans+=["B"]

for an in ans:
  print(an)

Solution (Python):
import sys
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
def resolve():
    N, A, B, C = lr()
    ABC = [A, B, C]
    ans = []
    def ope(u, v):
        ABC[u] += 1
        ABC[v] -= 1
        ans.append(chr(ord('A')+u))
    q = []
    for i in range(N):
        s = sr()
        if s == 'AB':
            x, y = 0, 1
        elif s == 'AC':
            x, y = 0, 2
        else:
            x, y = 1, 2
        q.append([x, y])
    for i in range(N):
        x, y = q[i]
        if ABC[x] == 0 and ABC[y] == 0:
            print('No')
            return
        if ABC[x] == 0:
            ope(x, y)
            continue
        if ABC[y] == 0:
            ope(y, x)
            continue
        if i < N-1:
            nx, ny = q[i+1]
            if x == nx or x == ny:
                ope(x, y)
                continue
        ope(y, x)
    print('Yes')
    print(*ans, sep='\n')
resolve()

Solution (Python):
import sys


def xxx(val, op):
    target = []
    for i, o in enumerate(op):
        if sum([val[k] for k in o]) == 0:
            return []
        k0 = o[0]
        k1 = o[1]
        if val[k0] == 0:
            p, m = k0, k1
        elif val[k1] == 0:
            p, m = k1, k0
        elif i < len(op) - 1 and k1 in op[i+1]:
            p, m = k1, k0
        else:
            p, m = k0, k1
        val[p] += 1
        val[m] -= 1
        target.append(p)
    return target


n, a, b, c = [int(x) for x in input().split()]
op = [sys.stdin.readline().strip() for _ in range(n)]
val = {"A": a, "B": b, "C": c}

target = xxx(val, op)
if len(target) == 0:
    print("No")
else:
    print("Yes")
    for t in target:
        print(t)

Solution (Python):
def abc(d, down_text, up_text):
    d[down_text] -= 1
    d[up_text] += 1


def main():
    n, a, b, c = map(int, input().split())
    ans_list = []
    dct = {"A": a, "B": b, "C": c}

    abc_list = [input() for _ in range(n)]
    abc_list.append('AB')

    for i in range(n):
        
        l_txt = abc_list[i][0]
        r_txt = abc_list[i][1]

        if dct[l_txt] < dct[r_txt]:
            abc(dct, r_txt, l_txt)
            ans_list.append(l_txt)

        elif dct[l_txt] > dct[r_txt]:
            abc(dct, l_txt, r_txt)
            ans_list.append(r_txt)

        else:
            if dct[l_txt] == dct[r_txt] == 0:
                print('No')
                break
            else:
                if l_txt in abc_list[i + 1]:
                    abc(dct, r_txt, l_txt)
                    ans_list.append(l_txt)
                elif r_txt in abc_list[i + 1]:
                    abc(dct, l_txt, r_txt)
                    ans_list.append(r_txt)
    else:
        print('Yes')
        print(*ans_list, sep='\n')


if __name__ == "__main__":
    main()

Solution (Python):
def f(s1, s2, a, b, c):
  if s1=='AB':
    if a==b==0:
      return None, None, None, 'D'
    elif a==0:
      return 1, b-1, c, 'A'
    elif b==0:
      return a-1, 1, c, 'B'
    elif a==b==1:
      if s2=='BC':
        return 0, 2, c, 'B'
      else:
        return 2, 0, c, 'A'
    else:
      if a>b:
        return a-1, b+1, c, 'B'
      else:
        return a+1, b-1, c, 'A'
  elif s1=='AC':
    if a==c==0:
      return None, None, None, 'D'
    elif a==0:
      return 1, b, c-1, 'A'
    elif c==0:
      return a-1, b, 1, 'C'
    elif a==c==1:
      if s2=='AB':
        return 2, b, 0, 'A'
      else:
        return 0, b, 2, 'C'
    else:
      if a>c:
        return a-1, b, c+1, 'C'
      else:
        return a+1, b, c-1, 'A'
  else:
    if b==c==0:
      return None, None, None, 'D'
    elif b==0:
      return a, 1, c-1, 'B'
    elif c==0:
      return a, b-1, 1, 'C'
    elif b==c==1:
      if s2=='AB':
        return a, 2, 0, 'B'
      else:
        return a, 0, 2, 'C'
    else:
      if b>c:
        return a, b-1, c+1, 'C'
      else:
        return a, b+1, c-1, 'B'

N, A, B, C=map(int, input().split())
S=[input() for _ in range(N)]
ans=['']*N
for i in range(N-1):
  A, B, C, P=f(S[i], S[i+1], A, B, C)
  if P=='D':
    print('No')
    exit()
  else:
    ans[i]=P
  
if S[-1]=='AB':
  if A==B==0:
    print('No')
    exit()
  else:
    if A<B:
      ans[-1]='A'
    else:
      ans[-1]='B'
elif S[-1]=='BC':
  if B==C==0:
    print('No')
    exit()
  else:
    if B<C:
      ans[-1]='B'
    else:
      ans[-1]='C'
else:
  if A==C==0:
    print('No')
    exit()
  else:
    if A<C:
      ans[-1]='A'
    else:
      ans[-1]='C'
      
print('Yes')
print(*ans, sep='\n')