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
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

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

    ans = []
    possible = True
    for i, s in enumerate(S):
        if s == "AB":
            if A + B == 0:
                print("No")
                possible = False
                break
            
            if i < n - 1 and N == 2 and A == B == 1:
                if S[i+1] == "AC":
                    A += 1
                    B -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    B += 1
                    ans.append("B")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            else:
                if A < B:
                    A += 1
                    B -= 1
                    ans.append("A")
                else:
                    A -= 1
                    B += 1
                    ans.append("B")

        elif s == "AC":
            if A + C == 0:
                print("No")
                possible = False
                break
            
            if i < n - 1 and N == 2 and A == C == 1:
                if S[i+1] == "AB":
                    A += 1
                    C -= 1
                    ans.append("A")
                elif S[i+1] == "BC":
                    A -= 1
                    C += 1
                    ans.append("C")
                else:
                    A += 1
                    C -= 1
                    ans.append("A")
            else:
                if A < C:
                    A += 1
                    C -= 1
                    ans.append("A")
                else:
                    A -= 1
                    C += 1
                    ans.append("C")

        else:
            if B + C == 0:
                print("No")
                possible = False
                break
            
            if i < n - 1 and N == 2 and B == C == 1:
                if S[i+1] == "AB":
                    B += 1
                    C -= 1
                    ans.append("B")
                elif S[i+1] == "AC":
                    B -= 1
                    C += 1
                    ans.append("C")
                else:
                    B += 1
                    C -= 1
                    ans.append("B")
            else:
                if B < C:
                    B += 1
                    C -= 1
                    ans.append("B")
                else:
                    B -= 1
                    C += 1
                    ans.append("C")

    if possible:
        print("Yes")
        print("\n".join(ans))


if __name__ == "__main__":
    main()

Solution (Python):
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		if abc[i1]+abc[i2]==0:
			ans="No"
		elif abc[i1]+abc[i2]==1:
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			else:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
if ans=="Yes":
	for i in range(n):
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")

Solution (Python):

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

value_dict = {'A': a, 'B': b, 'C': c}
result_list = []
input_list = []
flag = True

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

for next, input in enumerate(input_list, 1):
    if value_dict[input[0]] < value_dict[input[1]]:
        value_dict[input[0]] += 1
        value_dict[input[1]] -= 1
        result_list.append(input[0])
    elif value_dict[input[1]] < value_dict[input[0]]:
        value_dict[input[1]] += 1
        value_dict[input[0]] -= 1
        result_list.append(input[1])
    else:
        if value_dict[input[0]] == 0:
            flag = False
            break
        elif next == len(input_list):
            result_list.append(input[0])
        elif input[0] in input_list[next]:
            value_dict[input[0]] += 1
            value_dict[input[1]] -= 1
            result_list.append(input[0])
        else:
            value_dict[input[1]] += 1
            value_dict[input[0]] -= 1
            result_list.append(input[1])

if flag:
    print('Yes')
    for result in result_list:
        print(result)

else:
    print('No')

Solution (Python):
import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N, A, B, C = [int(x) for x in input().split()]
    S = [input().strip() for _ in range(N)]
    c = collections.Counter(S)


    ans = []
    for i in range(N):
        if S[i] == "AB":
            if A == B == 0:
                print("No")
                return
            elif A == B and i != N - 1:
                if S[i + 1] == "BC":
                    A -= 1
                    B += 1
                    ans.append("B")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
            elif A > B:
                A -= 1
                B += 1
                ans.append("B")
            else:
                A += 1
                B -= 1
                ans.append("A")
        elif S[i] == "AC":
            if A == C == 0:
                print("No")
                return
            elif A == C and i != N - 1:
                if S[i + 1] == "BC":
                    A -= 1
                    C += 1
                    ans.append("C")
                else:
                    A += 1
                    C -= 1
                    ans.append("A")
            elif A > C:
                A -= 1
                C += 1
                ans.append("C")
            else:
                A += 1
                C -= 1
                ans.append("A")
        else:
            if B == C == 0:
                print("No")
                return
            elif B == C and i != N - 1:
                if S[i + 1] == "AC":
                    B -= 1
                    C += 1
                    ans.append("C")
                else:
                    B += 1
                    C -= 1
                    ans.append("B")
            elif B > C:
                B -= 1
                C += 1
                ans.append("C")
            else:
                B += 1
                C -= 1
                ans.append("B")

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

if __name__ == '__main__':
    main()

Solution (Python):

import sys

buff_readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

def read_int_n():
    return list(map(int, buff_readline().split()))


def read_str():
    return readline().strip()


def slv(N, A, B, C, S):
    D = [A, B, C]
    ans = []

    def f(s):
        t = []
        if 'A' in s:
            t.append(0)
        if 'B' in s:
            t.append(1)
        if 'C' in s:
            t.append(2)
        return t
    for i, s in enumerate(S):
        t = f(s)
        if D[t[0]] == 0 and D[t[1]] == 0:
            print('No')
            return
        else:
            if D[t[0]] == D[t[1]] == 1 and i != N-1:
                ns = S[i+1]
                if s == ns:
                    ans.append(t[1])
                    D[t[0]] -= 1
                    D[t[1]] += 1
                else:
                    n = set(t) & set(f(ns))
                    m = set(t) - n
                    n = n.pop()
                    m = m.pop()
                    ans.append(n)
                    D[n] += 1
                    D[m] -= 1


            elif D[t[0]] > D[t[1]]:
                ans.append(t[1])
                D[t[0]] -= 1
                D[t[1]] += 1
            else:
                ans.append(t[0])
                D[t[0]] += 1
                D[t[1]] -= 1

    print('Yes')
    for a in ans:
        if a == 0:
            print('A')
        elif a == 1:
            print('B')
        else:
            print('C')



def main():
    N, A, B, C = read_int_n()
    S = [read_str() for _ in range(N)]
    (slv(N, A, B, C, S))

if __name__ == '__main__':
    main()

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

n,A,B,C = map(int,input().split())
s = [[*input()] for _ in range(n)]
a = {"A":A, "B":B, "C":C}
ans = ["Yes"]+[0]*n
def dfs(i):
    if i==n:
        print("\n".join(ans))
        exit()
    for p,m in [s[i],s[i][::-1]]:
        if a[m]:
            a[p]+=1; a[m]-=1; ans[i+1]=p
            dfs(i+1)
            
            a[p]-=1; a[m]+=1
dfs(0)
print("No")

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, a, b, c = MAP()
C = {0: a, 1: b, 2: c}
S = []
for i in range(N):
    s = input()
    c1, c2 = s
    S.append((ord(c1)-65, ord(c2)-65))

ans = []
for i in range(N-1):
    cur1, cur2 = S[i]
    if C[cur1] == C[cur2] == 0:
        No()
        exit()
    elif C[cur1] == C[cur2] == 1 and C[3-cur1-cur2] == 0:
        if cur1 in S[i+1] and 3-cur1-cur2 in S[i+1]:
            ans.append(cur1)
            C[cur1] += 1
            C[cur2] -= 1
        else:
            ans.append(cur2)
            C[cur2] += 1
            C[cur1] -= 1
    elif C[cur1] <= C[cur2]:
        ans.append(cur1)
        C[cur1] += 1
        C[cur2] -= 1
    else:
        ans.append(cur2)
        C[cur2] += 1
        C[cur1] -= 1
cur1, cur2 = S[-1]
if C[cur1] == C[cur2] == 0:
    No()
    exit()
elif C[cur1] <= C[cur2]:
    ans.append(cur1)
    C[cur1] += 1
    C[cur2] -= 1
else:
    ans.append(cur2)
    C[cur2] += 1
    C[cur1] -= 1
Yes()
for i in range(N):
    print(chr(ans[i]+65))

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, a, b, c = MAP()
C = {0: a, 1: b, 2: c}
S = []
for i in range(N):
    s = input()
    c1, c2 = s
    S.append((ord(c1)-65, ord(c2)-65))

ans = []
for i in range(N-1):
    cur1, cur2 = S[i]
    if C[cur1] == C[cur2] == 0:
        No()
        exit()
    elif C[cur1] == 0:
        ans.append(cur1)
        C[cur1] += 1
        C[cur2] -= 1
    elif C[cur2] == 0:
        ans.append(cur2)
        C[cur2] += 1
        C[cur1] -= 1
    elif C[cur1] == C[cur2] == 1 and C[3-cur1-cur2] == 0:
        if cur1 in S[i+1] and 3-cur1-cur2 in S[i+1]:
            ans.append(cur1)
            C[cur1] += 1
            C[cur2] -= 1
        else:
            ans.append(cur2)
            C[cur2] += 1
            C[cur1] -= 1
    elif C[cur1] <= C[cur2]:
        ans.append(cur1)
        C[cur1] += 1
        C[cur2] -= 1
    else:
        ans.append(cur2)
        C[cur2] += 1
        C[cur1] -= 1
cur1, cur2 = S[-1]
if C[cur1] == C[cur2] == 0:
    No()
    exit()
elif C[cur1] == 0:
    ans.append(cur1)
    C[cur1] += 1
    C[cur2] -= 1
else:
    ans.append(cur2)
    C[cur2] += 1
    C[cur1] -= 1
Yes()
for i in range(N):
    print(chr(ans[i]+65))

Solution (Python):
import sys

n, a, b, c = map(int, sys.stdin.readline().split())
s = sys.stdin.read().split() + ['$$']

def main(a, b, c):
    res = []
    for i in range(n):
        x, y = s[i], s[i+1]
        if x == 'AB':
            if a == b == 0: break
            if a > b:
                a -= 1
                b += 1
                res.append('B')
            elif a < b:
                a += 1
                b -= 1
                res.append('A')
            else:
                if y == 'AC':
                    a += 1
                    b -= 1
                    res.append('A')
                else:
                    a -= 1
                    b += 1
                    res.append('B')
        elif x == 'AC':
            if a == c == 0: break
            if a > c:
                a -= 1
                c += 1
                res.append('C')
            elif a < c:
                a += 1
                c -= 1
                res.append('A')
            else:
                if y == 'AB':
                    a += 1
                    c -= 1
                    res.append('A')
                else:
                    a -= 1
                    c += 1
                    res.append('C')  
        elif x == 'BC':
            if b == c == 0: break
            if b > c:
                b -= 1
                c += 1
                res.append('C')
            elif b < c:
                b += 1
                c -= 1
                res.append('B')
            else:
                if y == 'AB':
                    b += 1
                    c -= 1
                    res.append('B')
                else:
                    b -= 1
                    c += 1
                    res.append('C')
    else:
        print('Yes')
        print(*res, sep='\n')
        return 
    print('No')
                
if __name__ == '__main__':
    main(a, b, c)

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

S = ['']*N
for i in range(N):
  S[i] = input()
  
play = [''] * N
flag = True
for i, s in enumerate(S):
  next = ''
  if i < len(S)-1:
    next = S[i+1]
  if s == 'AB':
    if A == 0 and B == 0:
      flag = False
      break
    elif A == B and 'B' in next:
      A -= 1
      B += 1
      play[i] = 'B'
    elif A > B:
      A -= 1
      B += 1
      play[i] = 'B'
    else:
      A += 1
      B -= 1
      play[i] = 'A'
  elif s == 'AC':
    if A == 0 and C == 0:
      flag = False
      break
    elif A == C and 'C' in next:
      A -= 1
      C += 1
      play[i] = 'C'
    elif A > C:
      A -= 1
      C += 1
      play[i] = 'C'
    else:
      A += 1
      C -= 1
      play[i] = 'A'
  else:
    if B == 0 and C == 0:
      flag = False
      break
    elif B == C and 'C' in next:
      B -= 1
      C += 1
      play[i] = 'C'
    elif B > C:
      B -= 1
      C += 1
      play[i] = 'C'
    else:
      B += 1
      C -= 1
      play[i] = 'B'

if flag:
  print('Yes')
  for i in play:
    print(i)
else:
  print('No')

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

if a+b+c==0:
    for _ in range(n):
        s = input()
    print('No')
elif a+b+c==1:
    for _ in range(n):
        s = input()
        x,y = s[0],s[1]
        if abc[x]==abc[y]:
            print('No')
            break
        elif abc[x]==1:
            abc[x] -=1
            abc[y] +=1
            ans.append(y)
        else:
            abc[x] +=1
            abc[y] -=1
            ans.append(x)
elif a+b+c==2:
    s = input()
    x,y = s[0],s[1]
    if abc[x]==0 and abc[y]==0:
        print('No')
    else:
        for _ in range(n-1):
            s_ = input()
            x_,y_ = s_[0],s_[1]
            if abc[x]==abc[y]:
                if x==x_ or (x!=x_ and y!=y_ and x == 'B'):
                    abc[x] +=1
                    abc[y] -=1
                    ans.append(x)
                else:
                    abc[x] -=1
                    abc[y] +=1
                    ans.append(y)
            elif abc[x]>=1:
                abc[x] -=1
                abc[y] +=1
                ans.append(y)
            else:
                abc[x] +=1
                abc[y] -=1
                ans.append(x)
            x,y=x_,y_
        if abc[x]>=abc[y]:
            ans.append(y)
        else:
            ans.append(x)
else:
    s = input()
    x,y = s[0],s[1]
    if abc[x]==0 and abc[y]==0:
        print('No')
    else:
        for _ in range(n-1):
            if abc[x]>=abc[y]:
                abc[x] -=1
                abc[y] +=1
                ans.append(y)
            else:
                abc[x] +=1
                abc[y] -=1
                ans.append(x)
            s = input()
            x,y = s[0],s[1]
        if abc[x]>=abc[y]:
            ans.append(y)
        else:
            ans.append(x)
                
if len(ans)==n:
    print('Yes')
    print('\n'.join(ans))

Solution (Python):
import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, raw_input().split())
cmds = []

def getidx(c):
    return 'ABC'.index(c)

g = [ [], [], [] ]
for i in xrange(n):
    s = raw_input()
    cmds.append(s)
    g[getidx(s[0])].append(i)
    g[getidx(s[1])].append(i)

def getnext(c, idx):
    v = g[getidx(c)]
    l, r = 0, len(v) - 1
    while l <= r:
        m = (l + r) / 2
        if v[m] <= idx:
            l = m + 1
        else:
            r = m - 1
    if l >= len(v):
        return n
    return v[l]

cnt = [a, b, c]
res = []


for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1 or cnt[p1] == 0 or cnt[p2] == 0:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        if min(cnt) < 0:
            res = []
            break
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        
        assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        idx1 = getnext(cmd[0], i)
        idx2 = getnext(cmd[1], i)
        assert min(idx1, idx2) > i

        if idx1 < idx2:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
    assert min(cnt) >= 0

if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Solution (Python):
n,a,b,c = map(int,input().split())
ss=['' for _ in range(n)]
ans=['' for _ in range(n)]
wa=a+b+c
for i in range(n):
    s=input()
    ss[i]=s
i=0
if wa==0:
    print('No')
    exit()
elif wa==1:
    for s in ss:
        if s=='AB':
            if c:
                print('No')
                exit()
            elif b:
                a+=1
                b-=1
                ans[i]='A'
            elif a:
                b+=1
                a-=1
                ans[i]='B'
        elif s=='AC':
            if b:
                print('No')
                exit()
            elif a:
                c+=1
                a-=1
                ans[i]='C'
            elif c:
                a+=1
                c-=1
                ans[i]='A'
        elif s=='BC':
            if a:
                print('No')
                exit()
            elif b:
                c+=1
                b-=1
                ans[i]='C'
            elif c:
                b+=1
                c-=1
                ans[i]='B'
        i+=1
elif wa==2:
    for s in ss:
        if s=='AB':
            if a==0 and b==0:
                print('No')
                exit()
            elif a==0:
                a+=1
                b-=1
                ans[i]='A'
            elif b==0:
                b+=1
                a-=1
                ans[i]='B'
            elif a==1 and b==1:
                if i==n-1:
                    ans[i]='A'
                else:
                    if ss[i+1]=='AC' or ss[i+1]=='AB':
                        a+=1
                        b-=1
                        ans[i]='A'
                    elif ss[i+1]=='BC':
                        b+=1
                        a-=1
                        ans[i]='B'
        elif s=='AC':
            if a==0 and c==0:
                print('No')
                exit()
            elif a==0:
                a+=1
                c-=1
                ans[i]='A'
            elif c==0:
                c+=1
                a-=1
                ans[i]='C'
            elif a==1 and c==1:
                if i==n-1:
                    ans[i]='A'
                else:
                    if ss[i+1]=='AC' or ss[i+1]=='AB':
                        a+=1
                        c-=1
                        ans[i]='A'
                    elif ss[i+1]=='BC':
                        c+=1
                        a-=1
                        ans[i]='C'
        elif s=='BC':
            if b==0 and c==0:
                print('No')
                exit()
            elif b==0:
                b+=1
                c-=1
                ans[i]='B'
            elif c==0:
                c+=1
                b-=1
                ans[i]='C'
            elif b==1 and c==1:
                if i==n-1:
                    ans[i]='B'
                else:
                    if ss[i+1]=='BC' or ss[i+1]=='AB':
                        b+=1
                        c-=1
                        ans[i]='B'
                    elif ss[i+1]=='AC':
                        c+=1
                        b-=1
                        ans[i]='C'
        i+=1
elif wa>2:
    for i in range(n):
        s=ss[i]
        if s=='AB':
            if a==0 and b==0:
                print('No')
                exit()
            elif a==0:
                a+=1
                b-=1
                ans[i]='A'
            elif b==0:
                b+=1
                a-=1
                ans[i]='B'
            else:
                if i==n-1:
                    ans[i]='A'
                    a+=1
                    b-=1
                else:
                    if b>=a:
                        a+=1
                        b-=1
                        ans[i]='A'
                    else:
                        b+=1
                        a-=1
                        ans[i]='B'
        elif s=='AC':
            if a==0 and c==0:
                print('No')
                exit()
            elif a==0:
                a+=1
                c-=1
                ans[i]='A'
            elif c==0:
                c+=1
                a-=1
                ans[i]='C'
            else:
                if i==n-1:
                    ans[i]='A'
                    a+=1
                    c-=1
                else:
                    if c>=a:
                        a+=1
                        c-=1
                        ans[i]='A'
                    else:
                        c+=1
                        a-=1
                        ans[i]='C'
        elif s=='BC':
            if b==0 and c==0:
                print('No')
                exit()
            elif b==0:
                b+=1
                c-=1
                ans[i]='B'
            elif c==0:
                c+=1
                b-=1
                ans[i]='C'
            else:
                if i==n-1:
                    ans[i]='B'
                    b+=1
                    c-=1
                else:
                    if c>=b:
                        b+=1
                        c-=1
                        ans[i]='B'
                    else:
                        c+=1
                        b-=1
                        ans[i]='C'
        
print('Yes')
for x in ans:
    print(x)

Solution (Python):
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		if abc[i1]+abc[i2]==0:
			ans="No"
		elif abc[i1]+abc[i2]==1:
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			else:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				if abc[i1]>=abc[i2]:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
print(ans)
if ans=="Yes":
	for i in range(n):
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")

Solution (Python):
X = [0, 0, 0]
N, X[0], X[1], X[2] = map(int, input().split())
Q = []
for _ in range(N):
    s = input()
    if s == 'AB':
        Q.append([0, 1])
    elif s == 'AC':
        Q.append([0, 2])
    else:
        Q.append([1, 2])
ans = True
ret = ''
for i in range(N):
    a = Q[i][0]
    b = Q[i][1]
    if X[a] == X[b] == 0:
        ans = False
        break
    elif X[a] < X[b]:
        ret += chr(ord(str(a)) + 17)
        X[a] += 1
        X[b] -= 1
    elif X[a] > X[b] or i == N - 1:
        ret += chr(ord(str(b)) + 17)
        X[a] -= 1
        X[b] += 1
    else:
        s = Q[i + 1][0]
        t = Q[i + 1][1]
        if a == s or a == t:
            ret += chr(ord(str(a)) + 17)
            X[a] += 1
            X[b] -= 1
        else:
            ret += chr(ord(str(b)) + 17)
            X[a] -= 1
            X[b] += 1
    if X[a] < 0 or X[b] < 0:
        ans = False
        break
if ans:
    print('Yes')
    print('\n'.join(ret))
else:
    print('No')

Solution (Python):


N,A,B,C=input().split()
D=dict()
D["A"]=int(A)
D["B"]=int(B)
D["C"]=int(C)

S=[]
for i in range(int(N)):
    S.append(input())

ans="Yes"
F=[]
for i,s in enumerate(S):
    if D[s[0]]==0 and D[s[1]]==0:
        ans="No"
        break
    if D[s[0]]==1 and D[s[1]]==1:
        if D["A"]+D["B"]+D["C"]==2:
            if i!=int(N)-1:
                if S[i+1] != s:
                    s0=S[i+1][0]
                    s1=S[i+1][1]
                    if s[0]==s0 or s[0]==s1:
                        F.append(s[0])
                    if s[1]==s0 or s[1]==s1:
                        F.append(s[1])
    if len(F)!=i+1:
        if D[s[0]]<D[s[1]]:
            F.append(s[0])
        if D[s[0]]>D[s[1]]:
            F.append(s[1])
        if D[s[0]]==D[s[1]]:
            F.append(s[0])

    D[F[-1]]+=1
    if s[0]==F[-1]: D[s[1]]-=1
    if s[1]==F[-1]: D[s[0]]-=1
    
    

print(ans)
if ans=="Yes":
    for f in F:
        print(f)

Solution (Python):
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B": b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
    elif num_dic[s[0]] < num_dic[s[1]]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    elif i == n -1:
        ans.append(s[0])
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
    elif s[0] in ss[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
print("Yes")
for a in ans:
    print(a)