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):
n,a,b,c=map(int,input().split())
s_=[input() for i in range(n)]
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if i!=n-1:
            if s_[i+1]=="AC" and c==0 and a<=1:
                a,b,ans=(a+1,b-1,ans+"A")
            elif s_[i+1]=="BC" and c==0 and b<=1:
                a,b,ans=(a-1,b+1,ans+"B")
            else:
                a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        else:
            a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if i!=n-1:
            if s_[i+1]=="AB" and b==0 and a<=1:
                a,c,ans=(a+1,c-1,ans+"A")
            elif s_[i+1]=="BC" and b==0 and c<=1:
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        else:
            a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if i!=n-1:
            if s_[i+1]=="AB" and a==0 and b<=1:
                b,c,ans=(b+1,c-1,ans+"B")
            elif s_[i+1]=="AC" and a==0 and c<=1:
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        else:
            b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n, a, b, c = map(int,input().split())
    D = {"AB" : (0, 1), "AC" : (0, 2), "BC" : (1, 2)}
    E = {0 : 'A', 1 : 'B', 2 : 'C'}
    queries = [D[input()] for _ in range(n)]
    s = a + b + c

    if s != 2:
        ans = []
        state = [a, b, c]
        for i, j in queries:
            if state[i] == 0 and state[j] == 0:
                print("No")
                return
            if state[i] < state[j]:
                state[i] += 1
                state[j] -= 1
                ans.append(E[i])
            else:
                state[i] -= 1
                state[j] += 1
                ans.append(E[j])
        print("Yes")
        print(*ans, sep = '\n')
        return

    
    dp = set()
    dp.add((a, b, c))
    prev = [{} for _ in range(n)]
    for k in range(n):
        ndp = set()
        i, j = queries[k]
        for state in dp:
            if state[i] > 0:
                next = list(state)
                next[i] -= 1
                next[j] += 1
                next = tuple(next)
                ndp.add(next)
                prev[k][next] = state
            if state[j] > 0:
                next = list(state)
                next[j] -= 1
                next[i] += 1
                next = tuple(next)
                ndp.add(next)
                prev[k][next] = state
        dp = ndp

    if not dp:
        print("No")
        return

    for now in dp:
        break

    ans = []
    for k in range(n - 1, -1, -1):
        pa, pb, pc = prev[k][now]
        a, b, c = now
        if a > pa:
            ans.append('A')
        elif b > pb:
            ans.append('B')
        elif c > pc:
            ans.append('C')
        now = prev[k][now]
    ans.reverse()


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

Solution (Python):
n,a,b,c=map(int,input().split())
s_=[input() for i in range(n)]
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if a==1 and b==1 and i!=n-1:
            if s_[i+1][0]=="A":
                a,b,ans=(a+1,b-1,ans+"A")
            else:
                a,b,ans=(a-1,b+1,ans+"B")
            continue
        a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if a==1 and c==1 and i!=n-1:
            if s_[i+1][1]=="C":
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a+1,c-1,ans+"A")
            continue
        a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if c==1 and b==1 and i!=n-1:
            if s_[i+1][1]=="C":
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b+1,c-1,ans+"B")
            continue
        b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

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

for i, s in enumerate(s_l): 
    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 s_l[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())
arr=[input() for _ in range(n)]
ans=[]
for i in range(n):
  if arr[i]=='AB':
    if a==1 and b==1:
      if i==n-1 or arr[i+1]=='AB' or arr[i+1]=='AC':
        a+=1
        b-=1
        ans.append('A')
      elif arr[i+1]=='BC':
        a-=1
        b+=1
        ans.append('B')
    elif a==0 and b==0:
      print('No')
      break
    else:
      if a>=b:
        a-=1
        b+=1
        ans.append('B')
      else:
        a+=1
        b-=1
        ans.append('A')
  elif arr[i]=='AC':
    if a==1 and c==1:
      if i==n-1 or arr[i+1]=='AC' or arr[i+1]=='AB':
        a+=1
        c-=1
        ans.append('A')
      elif arr[i+1]=='BC':
        a-=1
        c+=1
        ans.append('C')
    elif a==0 and c==0:
      print('No')
      break
    else:
      if a>=c:
        a-=1
        c+=1
        ans.append('C')
      else:
        a+=1
        c-=1
        ans.append('A')
  elif arr[i]=='BC':
    if b==1 and c==1:
      if i==n-1 or arr[i+1]=='BC' or arr[i+1]=='AB':
        b+=1
        c-=1
        ans.append('B')
      elif arr[i+1]=='AC':
        b-=1
        c+=1
        ans.append('C')
    elif b==0 and c==0:
      print('No')
      break
    else:
      if b>=c:
        b-=1
        c+=1
        ans.append('C')
      else:
        b+=1
        c-=1
        ans.append('B')
else:
  print('Yes')
  for i in range(n):
    print(ans[i])

Solution (Python):
n,a,b,c=map(int,input().split())
ans=[]
slist=[]
for i in range(n):
    s=input()
    slist.append(s)
for i in range(n):
    s=slist[i]
    if s=="AB":
        if a==0:
            a+=1
            b-=1
            ans.append("A")
        elif b==0:
            a-=1
            b+=1
            ans.append("B")
        elif slist[(i+1)%n]=="AB":
            if a>b:
                a-=1
                b+=1
                ans.append("B")
            else:
                a+=1
                b-=1
                ans.append("A")
        elif slist[(i+1)%n]=="AC":
            a+=1
            b-=1
            ans.append("A")
        else:
            a-=1
            b+=1
            ans.append("B")
    elif s=="AC":
        if a==0:
            a+=1
            c-=1
            ans.append("A")
        elif c==0:
            a-=1
            c+=1
            ans.append("C")
        elif slist[(i+1)%n]=="AC":
            if a>c:
                a-=1
                c+=1
                ans.append("C")
            else:
                a+=1
                c-=1
                ans.append("A")
        elif slist[(i+1)%n]=="AB":
            a+=1
            c-=1
            ans.append("A")
        else:
            a-=1
            c+=1
            ans.append("C")
    else:
        if b==0:
            b+=1
            c-=1
            ans.append("B")
        elif c==0:
            b-=1
            c+=1
            ans.append("C")
        elif slist[(i+1)%n]=="BC":
            if b>c:
                b-=1
                c+=1
                ans.append("C")
            else:
                b+=1
                c-=1
                ans.append("B")
        elif slist[(i+1)%n]=="AC":
            c+=1
            b-=1
            ans.append("C")
        else:
            c-=1
            b+=1
            ans.append("B")
    if a<0 or b<0 or c<0:
        ans.clear()
        break
if len(ans)==0:
    print("No")
else:
    print("Yes")
    for i in range(n):
        print(ans[i])

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


def solve(x):
    d = {"A": 0, "B": 1, "C": 2}
    ans = []
    for i in range(N):
        u, v = X[i]
        if x[d[u]] == x[d[v]] == 0:
            return

        if x[d[u]] < x[d[v]]:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)
        elif x[d[u]] > x[d[v]]:
            x[d[u]] -= 1
            x[d[v]] += 1
            ans.append(v)
        elif i < N - 1 and X[i] != X[i + 1]:
            
            if u in X[i + 1]:
                x[d[u]] += 1
                x[d[v]] -= 1
                ans.append(u)
            else:
                x[d[u]] -= 1
                x[d[v]] += 1
                ans.append(v)
        else:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)

        if x[d[u]] < 0 or x[d[v]] < 0:
            return

    return ans

ans = solve([A, B, C])
if ans:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")

Solution (Python):



import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf

import math

import bisect


class Player():

    A = 0
    B = 1
    C = 2

    def __init__(self, data, op_list):
        self.data = data
        self.op_list = op_list
        self.move_history = ["X"] * len(self.op_list)
        self.key = 0
        self.ans = "Yes" 
        self.flg_out = False
        self.flg_foresight = False
        s = sum(data)
        if s == 2:
            self.flg_foresight = True
        elif s == 0:
            self.ans = "No"
            self.flg_out = True

    def has_next_op(self):
        if self.key + 1 < len(self.op_list):
            return True
        else:
            return False

    def play(self):
        for key, op in enumerate(op_list):
            
            
            if self.flg_out:
                break
            self.key = key
            self.read_op(op)

    def read_op(self, op):
        if op == "AB":
            self.do(self.A, self.B)
        if op == "AC":
            self.do(self.A, self.C)
        if op == "BC":
            self.do(self.B, self.C)

    def do(self, x, y):
        if self.data[x] < self.data[y]:
            self.move(y, x)
        elif self.data[x] > self.data[y]:
            self.move(x, y)
        else: 
            if self.flg_foresight:
                if self.is_next_used(x):
                    self.move(y, x)
                else:
                    self.move(x, y)
            else:
                self.move(x, y) 

    def move(self, frm, to):
        self.move_history[self.key] = self.num_to_char(to)
        self.data[frm] -= 1
        self.data[to] += 1
        if self.data[frm] < 0:
            self.ans = "No"
            self.flg_out = True

    @staticmethod
    def num_to_char(z):
        chars = ['A', 'B', 'C']
        return chars[z]

    def is_next_used(self, z):
        if not self.has_next_op():
            return False
        next_op = self.op_list[self.key + 1]
        if self.num_to_char(z) in next_op:
            return True
        else:
            return False

    def print_ans(self):
        print(self.ans)
        if self.ans == "No":
            return
        for h in self.move_history:
            print(h)




if __name__ == '__main__':
    n, a, b, c = list(map(int, input().split()))
    data = [a, b, c]
    
    
    op_list = [""] * n
    for i in range(n):
        op_list[i] = input()
    
    

    p = Player(data, op_list)
    p.play()
    p.print_ans()

Solution (Python):
n,a,b,c=map(int,input().split())
s_=[input() for i in range(n)]
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if a==1 and b==1 and i!=n-1:
            if s_[i+1][0]=="A":
                a,b,ans=(a+1,b-1,ans+"A")
            else:
                a,b,ans=(a-1,b+1,ans+"B")
            continue
        a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if a==1 and c==1 and i!=n-1:
            if s_[i+1][1]=="C":
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a+1,c-1,ans+"A")
            continue
        a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if c==1 and b==1 and i!=n-1:
            if s_[i+1][1]=="C":
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b+1,c-1,ans+"B")
            continue
        b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

Solution (Python):

n,a,b,c=map(int,input().split())
s=[""]*n
abc=dict()
abc["A"]=a
abc["B"]=b
abc["C"]=c

for i in range(n):
    s[i]=input()

t=[""]*n
yn=""
for i in range(n):
    if abc[s[i][0]]==abc[s[i][1]] and i<n-1:

        if s[i][1] in s[i+1]:

            t[i]=s[i][1]
            abc[s[i][0]]-=1
            abc[s[i][1]]+=1
            if abc[s[i][0]]<0:
                yn="No"
                break            
        else:

            t[i]=s[i][0]
            abc[s[i][0]]+=1
            abc[s[i][1]]-=1
            if abc[s[i][1]]<0:
                yn="No"
                break
    elif abc[s[i][0]]>=abc[s[i][1]]:
        t[i]=s[i][1]
        abc[s[i][0]]-=1
        abc[s[i][1]]+=1
        if abc[s[i][0]]<0:
            yn="No"
            break
    else:
        t[i]=s[i][0]
        abc[s[i][0]]+=1
        abc[s[i][1]]-=1
        if abc[s[i][1]]<0:
            yn="No"
            break



            
if yn=="No":
    print("No")
else:
    print("Yes")
    for i in range(n):
        print(t[i])

Solution (Python):
import sys
import bisect
inf = 10**15
mod = 10**9+7
ans = []
n,a,b,c = map(int, input().split())
abc = []


for i in range(n):
    s = input()
    abc.append(s)

for i in range(n):
    s = abc[i]
    if s == 'AB':
        if a > b:
            a-=1
            b+=1
            ans.append('B')
        elif a < b:
            a+=1
            b-=1
            ans.append('A')
        else:
            if i != n-1 and abc[i+1] == 'AC':
                a += 1
                b -= 1
                ans.append('A')
            else:
                a -= 1
                b += 1
                ans.append('B')
    elif s == 'BC':
        if b > c:
            b-=1
            c+=1
            ans.append('C')
        elif b < c:
            b+=1
            c-=1
            ans.append('B')
        else:
            if i != n-1 and abc[i + 1] == 'AB':
                b += 1
                c -= 1
                ans.append('B')
            else:
                b -= 1
                c += 1
                ans.append('C')
    else:
        if c > a:
            c-=1
            a+=1
            ans.append('A')
        elif c < a:
            c+=1
            a-=1
            ans.append('C')
        else:
            if i != n-1 and abc[i + 1] == 'BC':
                c += 1
                a -= 1
                ans.append('C')
            else:
                c -= 1
                a += 1
                ans.append('A')
    if a < 0 or b < 0 or c < 0:
        print('No')
        sys.exit()
if a >= 0 and b >= 0 and c >= 0:
    print('Yes')
    for num in ans:
        print(num)
else:
    print('No')

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math

    
    

    n,a,b,c = map(int, input().split())
    s = [input().rstrip() for _ in range(n)]
    res = []
    for i in range(n-1):
        if s[i]=='AB':
            if s[i+1]=='AB':
                if a>=b:
                    a -= 1
                    b += 1
                    res.append('B')
                else:
                    a += 1
                    b -= 1
                    res.append('A')
            elif s[i+1]=='BC':
                if a>=b:
                    a -= 1
                    b += 1
                    res.append('B')
                else:
                    a += 1
                    b -= 1
                    res.append('A')
            else:
                if a>b:
                    a -= 1
                    b += 1
                    res.append('B')
                else:
                    a += 1
                    b -= 1
                    res.append('A')
        if s[i]=='BC':
            if s[i+1]=='BC':
                if b>=c:
                    b -= 1
                    c += 1
                    res.append('C')
                else:
                    b += 1
                    c -= 1
                    res.append('B')
            elif s[i+1]=='AC':
                if b>=c:
                    b -= 1
                    c += 1
                    res.append('C')
                else:
                    b += 1
                    c -= 1
                    res.append('B')
            else:
                if b>c:
                    b -= 1
                    c += 1
                    res.append('C')
                else:
                    b += 1
                    c -= 1
                    res.append('B')
        if s[i]=='AC':
            if s[i+1]=='AC':
                if a>=c:
                    a -= 1
                    c += 1
                    res.append('C')
                else:
                    a += 1
                    c -= 1
                    res.append('A')
            elif s[i+1]=='BC':
                if a>=c:
                    a -= 1
                    c += 1
                    res.append('C')
                else:
                    a += 1
                    c -= 1
                    res.append('A')
            else:
                if a>c:
                    a -= 1
                    c += 1
                    res.append('C')
                else:
                    a += 1
                    c -= 1
                    res.append('A')
        if a<0 or b<0 or c<0:
            print('No')
            break
    else:
        if s[-1]=='AB':
            if a>=b:
                a -= 1
                b += 1
                res.append('B')
            else:
                a += 1
                b -= 1
                res.append('A')
        if s[-1]=='AC':
            if a>=c:
                a -= 1
                c += 1
                res.append('C')
            else:
                a += 1
                c -= 1
                res.append('A')
        if s[-1]=='BC':
            if c>=b:
                c -= 1
                b += 1
                res.append('B')
            else:
                c += 1
                b -= 1
                res.append('C')
        if a>=0 and b>=0 and c>=0:
            print('Yes')
            for i in res:
                print(i)
        else:
            print('No')

if __name__ == '__main__':
    main()

Solution (Python):
n,a,b,c = map(int,input().split())
l = [[*input()] for _ in range(n)]
l2 = l[1:]+["ABC"]
d = {"A":a, "B":b, "C":c}
a2 = a + 1
b2 = b + 1
c2 = c + 1
isNo = 0
if (a>0)+(b>0)+(c>0)>= 1:
  if d[l[0][0]] == 0 and d[l[0][1]] == 0:
    isNo = 1
  elif a+b+c == 1:
    for s,s2 in zip(l,l2):
      if d[s[0]]<d[s[1]]:
        d[s[1]]+=-1
        d[s[0]]+=1
        if s[0] not in s2:
          isNo = 1
          break
      else:
        d[s[1]]+=1
        d[s[0]]+=-1
        if s[1] not in s2:
          isNo = 1
          break
  else:isNo = 0
else:isNo = 1

d = {"A":a, "B":b, "C":c}
if isNo:print("No")
else:
  print("Yes")
  for s,s2 in zip(l,l2):
    if d[s[0]] > d[s[1]]:
      d[s[0]]+=-1
      d[s[1]]+=1
      print(s[1])
    elif d[s[0]] < d[s[1]]:
      d[s[1]]+=-1
      d[s[0]]+=1
      print(s[0])
    else:
      if s[0] in s2:
        d[s[1]]+=-1
        d[s[0]]+=1
        print(s[0])
      else:
        d[s[0]]+=-1
        d[s[1]]+=1
        print(s[1])

Solution (Python):
n,a,b,c = map(int,input().split())
s = [str(input()) for i in range(n)]
lis = [a,b,c]
ans = ""
for i in range(n-1):
    if lis[ord(s[i][0]) - ord("A")] == 0 and lis[ord(s[i][1]) - ord("A")] == 0:
        print("No")
        exit()
    if s[i] == s[i+1]:
        if lis[ord(s[i][0])-ord("A")] == 0:
            ans += s[i][0]
            lis[ord(s[i][0])-ord("A")] += 1
            lis[ord(s[i][1])-ord("A")] -= 1
        else:
            ans += s[i][1]
            lis[ord(s[i][0])-ord("A")] -= 1
            lis[ord(s[i][1])-ord("A")] += 1
    else:
        if s[i] == "AB":
            if s[i+1] == "AC":
                if lis[1] == 0:
                    ans += "B"
                    lis[1] += 1
                    lis[0] -= 1
                else:
                    ans += "A"
                    lis[0] += 1
                    lis[1] -= 1
            else:
                if lis[0] == 0:
                    ans += "A"
                    lis[0] += 1
                    lis[1] -= 1
                else:
                    ans += "B"
                    lis[0] -= 1
                    lis[1] += 1
        elif s[i] == "AC":
            if s[i+1] == "AB":
                if lis[2] == 0:
                    ans += "C"
                    lis[2] += 1
                    lis[0] -= 1
                else:
                    ans += "A"
                    lis[0] += 1
                    lis[2] -= 1
            else:
                if lis[0] == 0:
                    ans += "A"
                    lis[0] += 1
                    lis[2] -= 1
                else:
                    ans += "C"
                    lis[0] -= 1
                    lis[2] += 1
        else:
            if s[i+1] == "AB":
                if lis[2] == 0:
                    ans += "C"
                    lis[2] += 1
                    lis[1] -= 1
                else:
                    ans += "B"
                    lis[2] -= 1
                    lis[1] += 1
            else:
                if lis[1] == 0:
                    ans += "B"
                    lis[1] += 1
                    lis[2] -= 1
                else:
                    ans += "C"
                    lis[2] += 1
                    lis[1] -= 1
if lis[ord(s[-1][0])-ord("A")] == 0:
    if lis[ord(s[-1][1])-ord("A")] == 0:
        print("No")
        exit()
    else:
        ans += s[-1][0]
else:
    ans += s[-1][1]
print("Yes")
for i in range(n):
    print(ans[i])

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

can = 'Yes'
ans = []
val = {'A': a, 'B': b, 'C': c}
if a+b+c == 0:
    can = 'No'
elif a+b+c == 1:
    for ss in s:
        v1, v2 = ss[0], ss[1]
        if val[v1] == val[v2] == 0:
            can = 'No'
            break
        val[v1], val[v2] = val[v2], val[v1]
        ans.append(v1) if val[v1] else ans.append(v2)
else:
    for i in range(n):
        v1, v2 = s[i][0], s[i][1]
        if val[v1] == val[v2] == 0:
            can = 'No'
            break
        if a+b+c == 2 and val[v1] == val[v2] == 1 and i != n-1 and s[i] != s[i+1]:
            if v1 in s[i+1]:
                val[v1] += 1
                val[v2] -= 1
                ans.append(v1)
            else:
                val[v2] += 1
                val[v1] -= 1
                ans.append(v2)
        else:
            if val[v1] < val[v2]:
                val[v1] += 1
                val[v2] -= 1
                ans.append(v1)
            else:
                val[v2] += 1
                val[v1] -= 1
                ans.append(v2)

print(can)
if can == 'Yes':
    print('\n'.join(ans))

Solution (Python):
def f_three_variables_game():
    N, A, B, C = list(map(int, input().split()))
    Selections = [input() for i in range(N)]

    count = {'A': A, 'B': B, 'C': C}
    ans = []
    for turn, string in enumerate(Selections):
        s0, s1 = string

        variable_up, variable_down = None, None
        
        if count[s0] == count[s1] == 0:
            return 'No'
        
        elif count[s0] == 0:
            variable_up, variable_down = s0, s1
        elif count[s1] == 0:
            variable_up, variable_down = s1, s0
        
        
        elif turn + 1 == N:
            variable_up, variable_down = s0, s1
        
        elif s0 in Selections[turn + 1]:
            variable_up, variable_down = s0, s1
        else:
            variable_up, variable_down = s1, s0

        count[variable_up] += 1
        count[variable_down] -= 1
        ans.append(variable_up)

    return ' '.join(['Yes'] + ans)

print(f_three_variables_game())

Solution (Python):
def solve(n, a, b, c, s_ls):
    ans = []
    for i in range(n):
        s = s_ls[i]
        if s == "AB":
            if a == 0 and b == 0:
                return False, []
            elif a == 0:
                ans.append("A")
                a += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                a -= 1
                b += 1
            elif a == b == 1 and c == 0:
                if i == n - 1:
                    ans.append("A")
                    a += 1
                    b -= 1
                else:
                    nex = s_ls[i + 1]
                    if nex == "BC":
                        ans.append("B")
                        a -= 1
                        b += 1
                    else:
                        ans.append("A")
                        a += 1
                        b -= 1
            else:
                if a >= b:
                    ans.append("B")
                    a -= 1
                    b += 1
                else:
                    ans.append("A")
                    a += 1
                    b -= 1
        elif s == "BC":
            if b == 0 and c == 0:
                return False, []
            elif c == 0:
                ans.append("C")
                b -= 1
                c += 1
            elif b == 0:
                ans.append("B")
                b += 1
                c -= 1
            elif c == b == 1 and a == 0:
                if i == n - 1:
                    ans.append("C")
                    b -= 1
                    c += 1
                else:
                    nex = s_ls[i + 1]
                    if nex == "AB":
                        ans.append("B")
                        b += 1
                        c -= 1
                    else:
                        ans.append("C")
                        b -= 1
                        c += 1
            else:
                if c >= b:
                    ans.append("B")
                    b += 1
                    c -= 1
                else:
                    ans.append("C")
                    b -= 1
                    c += 1
        elif s == "AC":
            if a == 0 and c == 0:
                return False, []
            elif a == 0:
                ans.append("A")
                a += 1
                c -= 1
            elif c == 0:
                ans.append("C")
                a -= 1
                c += 1
            elif a == c == 1 and b == 0:
                if i == n - 1:
                    ans.append("A")
                    a += 1
                    c -= 1
                else:
                    nex = s_ls[i + 1]
                    if nex == "AB":
                        ans.append("A")
                        a += 1
                        c -= 1
                    else:
                        ans.append("C")
                        a -= 1
                        c += 1
            else:
                if a >= c:
                    ans.append("C")
                    a -= 1
                    c += 1
                else:
                    ans.append("A")
                    a += 1
                    c -= 1
    return True, ans


def main():
    n, a, b, c = map(int, input().split())
    s_ls = []
    for i in range(n):
        s_ls.append(input())

    res, ans = solve(n, a, b, c, s_ls)
    if not res:
        print("No")
    else:
        print("Yes")
        for x in ans:
            print(x)


main()

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

v = {
  'A': a,
  'B': b,
  'C': c
}
ss = []
for _ in range(n):
  ss.append(input())

history = [] 
flag = True
  
for i in range(n):
  
  s = ss[i]
  
  if v[s[0]] == 0 and v[s[1]] == 0:
    flag = False
    break
  elif v[s[0]] > v[s[1]]:
    history.append(s[1])
    v[s[0]] -= 1
    v[s[1]] += 1
  elif v[s[0]] < v[s[1]]:
    history.append(s[0])
    v[s[0]] += 1
    v[s[1]] -= 1
  elif i == n-1:
    history.append(s[0])
    v[s[0]] += 1
    v[s[1]] -= 1
  elif s[0] in ss[i+1]:
    history.append(s[0])
    v[s[0]] += 1
    v[s[1]] -= 1
  else:
    history.append(s[1])
    v[s[0]] -= 1
    v[s[1]] += 1
if flag:
  print("Yes")
  print('\n'.join(history))
else:
  print("No")