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
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    before=[[],[],[]]
    ans=[-1]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]] and before[(d[s]+1)%3]:
                if before[d[s]][-1][0]>before[(d[s]+1)%3][-1][0]:
                    k,l=before[d[s]].pop()
                    n[d[s]]+=2
                    n[l]-=2
                    ans[k]=d[s]
                else:
                    k,l=before[(d[s]+1)%3].pop()
                    n[(d[s]+1)%3]+=2
                    n[l]-=2
                    ans[k]=(d[s]+1)%3
            elif before[d[s]] or before[(d[s]+1)%3]:
                if before[d[s]]:
                    k,l=before[d[s]].pop()
                    n[d[s]]+=2
                    n[l]-=2
                    ans[k]=d[s]
                elif before[(d[s]+1)%3]:
                    k,l=before[(d[s]+1)%3].pop()
                    n[(d[s]+1)%3]+=2
                    n[l]-=2
                    ans[k]=(d[s]+1)%3
            else:
                print('No')
                exit()
        if n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            if n[d[s]]<n[(d[s]+1)%3]:
                ans[i]=d[s]
                n[d[s]]+=1
                n[(d[s]+1)%3]-=1
                before[(d[s]+1)%3].append((i,d[s]))
            else:
                ans[i]=(d[s]+1)%3
                n[d[s]]-=1
                n[(d[s]+1)%3]+=1
                before[d[s]].append((i,(d[s]+1)%3))
    print('Yes')
    d2=['A','B','C']
    for a in ans:
        print(d2[a])

if __name__ == '__main__':
    main()

Solution (Python):
n,a,b,c=map(int,input().split())
a=min(3,a)
b=min(3,b)
S=[input() for i in range(n)]
X=a+b+c
idx=0
log=[0]*n
plog=[(a,b)]*(n+1)

flag=False

while idx!=n:

    
    if idx<n-2 and log[idx+2]==2 and log[idx]==2:
        flag=True
        break
    if idx==0 and log[0]==2:
        flag=True
        break

    a,b=plog[idx]
    dx,dy=0,0
    if S[idx]=='AB':
        dx,dy=1,-1
    elif S[idx]=='BC':
        dx,dy=0,1
    else:
        dx,dy=1,0

    if log[idx]==0:
        a+=dx
        b+=dy
    elif log[idx]==1:
        a-=dx
        b-=dy
    else:
        log[idx]=0
        idx-=1
        log[idx]+=1
        continue
    
    if a<0 or b<0 or X-(a+b)<0:
        log[idx]+=1
        continue
    else:
        idx+=1
        plog[idx]=(a,b)

if flag:
    print('No')
else:
    print('Yes')
    for i,s in zip(log,S):
        print(s[i])

Solution (Python):
N,A,B,C=map(int,input().split())
S=[input() for _ in range(N)]
ans=''
for i in range(N):
    if S[i]=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        elif A==1 and B==0:
            A-=1
            B+=1
            ans+='B'
        elif A==0 and B==1:
            A+=1
            B-=1
            ans+='A'
        elif A==1 and B==1 and i<N-1:
            if 'A' in S[i+1]:
                A+=1
                B-=1
                ans+='A'
            else:
                A-=1
                B+=1
                ans+='B'
        elif A>B:
            A-=1
            B+=1
            ans+='B'
        else:
            A+=1
            B-=1
            ans+='A'
    elif S[i]=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        elif A==1 and C==0:
            A-=1
            C+=1
            ans+='C'
        elif A==0 and C==1:
            A+=1
            C-=1
            ans+='A'
        elif A==1 and C==1 and i<N-1:
            if 'A' in S[i+1]:
                A+=1
                C-=1
                ans+='A'
            else:
                A-=1
                C+=1
                ans+='C'
        elif A>C:
            A-=1
            C+=1
            ans+='C'
        else:
            A+=1
            C-=1
            ans+='A'
    else:
        if B==0 and C==0:
            print('No')
            exit()
        elif B==1 and C==0:
            B-=1
            C+=1
            ans+='C'
        elif B==0 and C==1:
            B+=1
            C-=1
            ans+='B'
        elif B==1 and C==1 and i<N-1:
            if 'B' in S[i+1]:
                B+=1
                C-=1
                ans+='B'
            else:
                B-=1
                C+=1
                ans+='C'
        elif B>C:
            B-=1
            C+=1
            ans+='C'
        else:
            B+=1
            C-=1
            ans+='B'
print('Yes')
for c in ans:
    print(c)

Solution (Python):


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

ansArr = []
ans = 'Yes'
for i in range(N):
    s = S[i]
    if s=='AB':
        if A==1 and B==1 and i<N-1:
            if S[i+1].count('A')==1:
                A += 1
                B -= 1
                ansArr.append('A')
            else:
                A -= 1
                B += 1
                ansArr.append('B')               
        elif A <= B:
            A += 1
            B -= 1
            if B < 0:
                ans = 'No'
                break
            ansArr.append('A')
        else:
            A -= 1
            B += 1
            ansArr.append('B')
    elif s =='AC':
        if A==1 and C==1 and i<N-1:
            if S[i+1].count('A')==1:
                A += 1
                C -= 1
                ansArr.append('A')
            else:
                A -= 1
                C += 1
                ansArr.append('C')   
        elif A <= C:
            A += 1
            C -= 1
            if C < 0:
                ans = 'No'
                break
            ansArr.append('A')
        else:
            A -= 1
            C += 1
            ansArr.append('C')
    else:
        if B==1 and C==1 and i<N-1:
            if S[i+1].count('B')==1:
                B += 1
                C -= 1
                ansArr.append('B')
            else:
                B -= 1
                C += 1
                ansArr.append('C')  
        elif B <= C:
            B += 1
            C -= 1
            if C < 0:
                ans = 'No'
                break
            ansArr.append('B')
        else:
            B -= 1
            C += 1
            ansArr.append('C')

print(ans)
if ans=='Yes':
    for a in ansArr:
        print(a)

Solution (Python):
import bisect,sys
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29

DIV = (10**9) + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac =fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV
        
    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s+=a
        yield s


sys.setrecursionlimit(200005)


def main():
    N,A,B,C = isRaw()
    AB = A+B
    AC = A+C
    BC = B+C
    ans=[]
    Ss = [sRaw() for n in range(N)]
    nABs = [sidx for sidx,s in enumerate(Ss) if s=="AB"]
    nACs = [sidx for sidx, s in enumerate(Ss) if s == "AC"]
    nBCs = [sidx for sidx, s in enumerate(Ss) if s == "BC"]
    nABidx = 0 if len(nABs)>0 else -1
    nACidx = 0 if len(nACs) > 0 else -1
    nBCidx = 0 if len(nBCs)>0 else -1
    for n in range(N):
        s = Ss[n]
        if s=="AB":
            nABidx = nABidx+1 if len(nABs)>nABidx+1 else -1
            if AB==0:
                print("No")
                return
            if AC >BC:
                ans.append("B")
                AC-=1
                BC+=1
            elif AC<BC:
                ans.append("A")
                AC+=1
                BC-=1
            else:
                if nBCidx == -1 or (nBCidx != -1 and nACidx != -1 and nACs[nACidx] < nBCs[nBCidx]):
                    ans.append("A")
                    AC += 1
                    BC -= 1
                else:
                    ans.append("B")
                    AC -= 1
                    BC += 1
        if s=="AC":
            nACidx = nACidx+1 if len(nACs) > nACidx+1 else -1
            if AC == 0:
                print("No")
                return
            if AB > BC:
                ans.append("C")
                AB -= 1
                BC += 1
            elif AB < BC:
                ans.append("A")
                AB += 1
                BC -= 1
            else:
                if nBCidx == -1 or (nBCidx != -1 and nABidx != -1 and nABs[nABidx] < nBCs[nBCidx]):
                    ans.append("A")
                    AB += 1
                    BC -= 1
                else:
                    ans.append("C")
                    AB -= 1
                    BC += 1
        if s == "BC":
            nBCidx = nBCidx+1 if len(nBCs) > nBCidx+1 else -1
            if BC == 0:
                print("No")
                return
            if AB > AC:
                ans.append("C")
                AB -= 1
                AC += 1
            elif AB < AC:
                ans.append("B")
                AB += 1
                AC -= 1
            else:
                if nACidx == -1 or (nACidx != -1 and nABidx != -1 and nABs[nABidx] < nACs[nACidx]):
                    ans.append("B")
                    AB += 1
                    AC -= 1
                else:
                    ans.append("C")
                    AB -= 1
                    AC += 1
    print("Yes")
    print("\n".join(ans))
if __name__ == "__main__":
    main()

Solution (Python):
import bisect
import sys
import math
from collections import deque


def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))


INF = 1 << 29

DIV = (10**9) + 7


def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac = fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV

    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s += a
        yield s


sys.setrecursionlimit(200005)


def main():
    N, A, B, C = isRaw()
    AB = A+B
    AC = A+C
    BC = B+C
    ans = []
    Ss = [sRaw() for n in range(N)]
    
    for n in range(N):
        s = Ss[n]
        if s == "AB":
            if AB == 0:
                print("No")
                return
            if AC > BC:
                ans.append("B")
                AC -= 1
                BC += 1
            elif AC < BC:
                ans.append("A")
                AC += 1
                BC -= 1
            else:
                if n+1!=N and Ss[n+1]=="AC":
                    ans.append("A")
                    AC += 1
                    BC -= 1
                else:
                    ans.append("B")
                    AC -= 1
                    BC += 1
        if s == "AC":
            if AC == 0:
                print("No")
                return
            if AB > BC:
                ans.append("C")
                AB -= 1
                BC += 1
            elif AB < BC:
                ans.append("A")
                AB += 1
                BC -= 1
            else:
                if n+1 != N and Ss[n+1] == "AB":
                    ans.append("A")
                    AB += 1
                    BC -= 1
                else:
                    ans.append("C")
                    AB -= 1
                    BC += 1
        if s == "BC":
            if BC == 0:
                print("No")
                return
            if AB > AC:
                ans.append("C")
                AB -= 1
                AC += 1
            elif AB < AC:
                ans.append("B")
                AB += 1
                AC -= 1
            else:
                if n+1 != N and Ss[n+1] == "AB":
                    ans.append("B")
                    AB += 1
                    AC -= 1
                else:
                    ans.append("C")
                    AB -= 1
                    AC += 1
    print("Yes")
    print("\n".join(ans))


if __name__ == "__main__":
    main()

Solution (Python):
n, a, b, c = map(int, input().split())
q = [(0, 1) if si == 'AB' else (1, 2) if si == 'BC' else (0, 2) for si in (input() for _ in range(n))]
ans = []
ch = ['A', 'B', 'C']
d = [a, b, c]

nxt = [[-1] * 3] * n
t = [-1, -1, -1]
for i, (x, y) in reversed(list(enumerate(q))):
    nxt[i] = t.copy()
    t[x], t[y] = y, x
for (x, y), t in zip(q, nxt):
    if d[x] == d[y] == 0:
        print('No')
        break
    if d[x] != 0 and (d[y] == 0 or d[x] >= 2 or t[3 - x - y] == y):
            x, y = y, x
    d[x] += 1
    d[y] -= 1
    ans.append(ch[x])
else:
    print('Yes', *ans, sep='\n')

Solution (Python):
import sys

s=[]
ans=[]
 
def dfs(a,b,c,i,root):
    if i==n:
        global ans
        ans=root.copy()
        return True
    l=False
    r=False
    if s[i]=='AB':
        if a>0:
            root.append('B')
            l=dfs(a-1,b+1,c,i+1,root)
            root.pop()
        if not l and b>0:
            root.append('A')
            r=dfs(a+1,b-1,c,i+1,root)
            root.pop()
    elif s[i]=='AC':
        if a>0:
            root.append('C')
            l=dfs(a-1,b,c+1,i+1,root)
            root.pop()
        if not l and c>0:
            root.append('A')
            r=dfs(a+1,b,c-1,i+1,root)
            root.pop()
    else:
        if b>0:
            root.append('C')
            l=dfs(a,b-1,c+1,i+1,root)
            root.pop()
        if not l and c>0:
            root.append('B')
            r=dfs(a,b+1,c-1,i+1,root)
            root.pop()
    return l or r
 
sys.setrecursionlimit(10**6)
n,a,b,c=map(int,input().split())
s=['']*n
for i in range(n):
    s[i]=input()
flag=dfs(a,b,c,0,[])
 
if flag:
    print("Yes")
    for t in ans:
        print(t)
else:
    print("No")

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

ans=[]
 
flag=True
 
for i in range(n):
    if s[i]=='AB':
        if a==0 and b==0:
            flag=False
            break
        if a==0 or (b!=0 and a==1 and s[i+1]=='AC'):
            ans.append('A')
            a+=1
            b-=1
        else:
            ans.append('B')
            a-=1
            b+=1
    elif s[i]=='AC':
        if a==0 and c==0:
            flag=False
            break
        if a==0 or (c!=0 and a==1 and s[i+1]=='AB'):
            ans.append('A')
            a+=1
            c-=1
        else:
            ans.append('C')
            a-=1
            c+=1
    else:
        if c==0 and b==0:
            flag=False
            break
        if c==0 or (b!=0 and c==1 and s[i+1]=='AC'):
            ans.append('C')
            c+=1
            b-=1
        else:
            ans.append('B')
            c-=1
            b+=1

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

Solution (Python):
n,a,b,c = map(int,input().split())
s = [input() for _ in range(n)]
c2v = {'A':a,'B':b,'C':c}
sum_v = sum(c2v.values())
if sum_v == 0:
    print('No')
    exit()
if sum_v == 1:
    ans = []
    for ss in s:
        if c2v[ss[0]] == 0 and c2v[ss[1]] == 0:
            print('No')
            exit()
        elif c2v[ss[0]] == 0 and c2v[ss[1]] == 1:
            ans.append(ss[0])
            c2v[ss[0]] += 1
            c2v[ss[1]] -= 1
        elif c2v[ss[0]] == 1 and c2v[ss[1]] == 0:
            ans.append(ss[1])
            c2v[ss[1]] += 1
            c2v[ss[0]] -= 1
    print('Yes')
    for ans_i in ans:
        print(ans_i)

if sum_v >= 2:
    ans = []
    for ss_i,ss in enumerate(s):
        if c2v[ss[0]] == 0 and c2v[ss[1]] == 0:
            print('No')
            exit()
        elif c2v[ss[0]] == 0 and c2v[ss[1]] >= 1:
            ans.append(ss[0])
            c2v[ss[0]] += 1
            c2v[ss[1]] -= 1
        elif c2v[ss[0]] >= 1 and c2v[ss[1]] == 0:
            ans.append(ss[1])
            c2v[ss[1]] += 1
            c2v[ss[0]] -= 1
        else: 
            if ss_i+1 == n: 
                ans.append(ss[0])
                c2v[ss[0]] += 1
                c2v[ss[1]] -= 1
                continue
            next_ss = s[ss_i+1]
            if ss[0] in next_ss:
                ans.append(ss[0])
                c2v[ss[0]] += 1
                c2v[ss[1]] -= 1
            else:
                ans.append(ss[1])
                c2v[ss[1]] += 1
                c2v[ss[0]] -= 1
    print('Yes')
    for ans_i in ans:
        print(ans_i)

Solution (Python):
from itertools import combinations

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

def sol():
    now = {'A': A, 'B': B, 'C': C}
    ret = []
    for i, s in enumerate(S):
        u, v = s[0], s[1]
        if now[u] == now[v] == 0:
            return []
        if now[u] > now[v]:
            ret.append(v)
            now[u] -= 1
            now[v] += 1
            continue
        if now[u] < now[v]:
            ret.append(u)
            now[u] += 1
            now[v] -= 1
            continue
        if i == N - 1:
            ret.append(u)
            continue

        nx = S[i + 1]
        if u in nx:
            ret.append(u)
            now[u] += 1
            now[v] -= 1
            continue
        if v in nx:
            ret.append(v)
            now[v] += 1
            now[u] -= 1
            continue

    return ret

ans = sol()
if len(ans) == 0:
    print('No')
    exit()

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

Solution (Python):
from itertools import combinations

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

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

def out(T):
    now = {'A': A, 'B': B, 'C': C}

    ans = []
    for i, s in enumerate(S):
        u, v = s[0], s[1]
        if now[u] == now[v] == 0:
            return []
        if now[u] > now[v]:
            ans.append(v)
            now[u] -= 1
            now[v] += 1
            continue
        if now[u] < now[v]:
            ans.append(u)
            now[u] += 1
            now[v] -= 1
            continue
        if i == N - 1:
            ans.append(u)
            continue

        nx = S[i + 1]
        if u in nx:
            ans.append(u)
            now[u] += 1
            now[v] -= 1
            continue
        if v in nx:
            ans.append(v)
            now[v] += 1
            now[u] -= 1
            continue

    return ans

ans = out('')
if len(ans) == 0:
    print('No')
    exit()
print('Yes')
print(*ans, sep='\n')

Solution (Python):
N,A,B,C=map(int,input().split(' '))
S = [input() for i in range(N)]
ans = []
if A+B+C==0:
    print('No')
else:
    d = {"A":A,"B":B,"C":C}
    for n,i in enumerate(S):
        l,r=list(i)
        L,R=d[l],d[r]
        if L==R==0:
            print('No')
            exit()
        elif L==0:
            ans.append(l)
            d[l] +=1
            d[r] -=1
        elif R==0:
            d[l] -=1
            d[r] +=1
            ans.append(r)
        elif L==R==1 and n != N-1 and i != S[n+1]:
            if l in S[n+1]:
                d[l] +=1
                d[r] -=1
                ans.append(l)
            else:
                d[l] -=1
                d[r] +=1
                ans.append(r)
        else:
            if L>=R:
                d[l] -=1
                d[r] +=1
                ans.append(r)
            else:
                d[l] +=1
                d[r] -=1
                ans.append(l)
    print('Yes')
    for i in ans:
        print(i)

Solution (Python):
import sys
input=sys.stdin.readline
n,a,b,c=map(int,input().split())
s=[]
for i in range(n):
    sub=input().rstrip()
    s.append(sub)
if a+b+c==0:
    print('No')
    sys.exit()
if a+b+c==1:
    com=[]
    for i in s:
        if i=='AB':
            if a==0 and b==0:
                print('No')
                sys.exit()
            else:
                if a>b:
                    com.append('B')
                else:
                    com.append('A')
                a,b=b,a
        elif i=='AC':
            if a==0 and c==0:
                print('No')
                sys.exit()
            else:
                if a>c:
                    com.append('C')
                else:
                    com.append('A')
                a,c=c,a
        elif i=='BC':
            if b==0 and c==0:
                print('No')
                sys.exit()
            else:
                if b>c:
                    com.append('C')
                else:
                    com.append('B')
                b,c=c,b
    print('Yes')
    for i in com:
        print(i)
    sys.exit()
if (s[0]=='AB' and a**2+b**2==0) or (s[0]=='AC' and a**2+c**2==0) or (s[0]=='BC' and c**2+b**2==0):
    print('No')
else:
    print('Yes')
    for i in range(n):
        check=True
        if s[i]=='AB':
            at=a+1
            bt=b-1
            ct=c
            if bt<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('A')
            else:
                a-=1
                b+=1
                print('B')
        elif s[i]=='AC':
            at=a+1
            bt=b
            ct=c-1
            if ct<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('A')
            else:
                a-=1
                c+=1
                print('C')
        elif s[i]=='BC':
            at=a
            bt=b+1
            ct=c-1
            if ct<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('B')
            else:
                b-=1
                c+=1
                print('C')

Solution (Python):


N, A, B, C = list(map(int, input().split()))
d = {'A': A, 'B': B, 'C': C}
res = []
SN = [input() for i in range(N)]
for i in range(N):
    s = SN[i]
    s0 = s[0]
    s1 = s[1]
    
    if d[s0] == d[s1] == 0:
        print('No')
        exit()
    elif d[s0] == 0:
        d[s0] += 1
        d[s1] -= 1
        res.append(s0)
    elif d[s1] == 0:
        d[s0] -= 1
        d[s1] += 1
        res.append(s1)
    elif i == N-1:
        res.append(s0)
    elif s0 in SN[i+1]:
        d[s0] += 1
        d[s1] -= 1
        res.append(s0)
    else:
        d[s0] -= 1
        d[s1] += 1
        res.append(s1)
        
print('Yes')
for r in res:
    print(r)

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