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())

sum = a + b + c
lst = []
if sum != 2:
    for _ in range(n):
        s = input()
        if s == "AB":
            if a < b:
                a += 1
                b -= 1
                lst.append("A")
            else:
                a -= 1
                b += 1
                if a < 0:
                    print("No")
                    break
                lst.append("B")
        elif s == "AC":
            if a < c:
                a += 1
                c -= 1
                lst.append("A")
            else:
                a -= 1
                c += 1
                if a < 0:
                    print("No")
                    break
                lst.append("C")
        else:
            if c < b:
                c += 1
                b -= 1
                lst.append("C")
            else:
                c -= 1
                b += 1
                if c < 0:
                    print("No")
                    break
                lst.append("B")
    else:
        print("Yes")
        for alpha in lst:
            print(alpha)
elif n == 1:
    s = input()
    if s == "AB":
        if a == b == 0:
            print("No")
        else:
            print("Yes")
            if a > b:
                print("B")
            else:
                print("A")
    elif s == "AC":
        if a == c == 0:
            print("No")
        else:
            print("Yes")
            if a > c:
                print("C")
            else:
                print("A")
    else:
        if c == b == 0:
            print("No")
        else:
            print("Yes")
            if c > b:
                print("B")
            else:
                print("C")            
                
else:
    s = input()
    flag = True
    flag2 = True
    for _ in range(n - 1):
        if s == "AB":
            if a == b == 0:
                print("No")
                flag2 = False
                break
            elif a != b:
                if a > b:
                    lst.append("B")
                    a -= 1
                    b += 1
                else:
                    lst.append("A")
                    a += 1
                    b -= 1
                s = input()
                flag = True
            else:
                s = input()
                if s == "AB":
                    lst.append("A")
                    a = 2
                    b = 0
                elif s == "AC":
                    lst.append("A")
                    a = 2
                    b = 0
                else:
                    lst.append("B")
                    b = 2
                    a = 0
                    
        elif s == "AC":
            if a == c == 0:
                print("No")
                flag2 = False
                break
            elif a != c:
                if a > c:
                    lst.append("C")
                    a -= 1
                    c += 1
                else:
                    lst.append("A")
                    a += 1
                    c -= 1
                s = input()
                flag = True
            else:
                s = input()
                if s == "AC":
                    lst.append("A")
                    a = 2
                    c = 0
                elif s == "AB":
                    lst.append("A")
                    a = 2
                    c = 0
                else:
                    lst.append("C")
                    c = 2
                    a = 0
                    
        else:
            if b == c == 0:
                print("No")
                flag2 = False
                break
            elif b != c:
                if b > c:
                    lst.append("C")
                    b -= 1
                    c += 1
                else:
                    lst.append("B")
                    b += 1
                    c -= 1
                s = input()
                flag = True
            else:
                s = input()
                if s == "BC":
                    lst.append("B")
                    b = 2
                    c = 0
                elif s == "AB":
                    lst.append("B")
                    b = 2
                    c = 0
                else:
                    lst.append("C")
                    c = 2
                    b = 0
    else:
        if flag:
            if s == "AB":
                if a == b == 0:
                    print("No")
                    flag2 = False
                if a < b:
                    lst.append("A")
                else:
                    lst.append("B")
            elif s == "AC":
                if a == c == 0:
                    print("No")
                    flag2 = False
                if a < c:
                    lst.append("A")
                else:
                    lst.append("C")
            else:
                if b == c == 0:
                    print("No")
                    flag2 = False
                if b < c:
                    lst.append("B")
                else:
                    lst.append("C")
    if flag2:
        print("Yes")
        for alpha in lst:
            print(alpha)

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

val=[a,b,c]
flag=True
ansls=list()

for i in range(n):
    s1=ord(op[i][0])-ord('A')
    s2=ord(op[i][1])-ord('A')
    if val[s1]>val[s2]:
        ansls.append(s2)
        val[s2]+=1
        val[s1]-=1
    elif val[s1]<val[s2] or i+1==n:
        ansls.append(s1)
        val[s1]+=1
        val[s2]-=1
    else:
        s1n = ord(op[i+1][0])-ord('A')
        s2n = ord(op[i+1][1])-ord('A')
        nxt = set([s1,s2]) & set([s1n,s2n])
        nxt = list(nxt)[0]
        ansls.append(nxt)
        val[nxt]+=1
        if nxt==s1:
            val[s2]-=1
        else:
            val[s1]-=1
    if min(val)<0:
        flag=False
        break

if flag:
    print('Yes')
    for i in range(n):
        print(chr(ansls[i]+ord('A')))
else:
    print('No')

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

N, A, B, C = list(map(int, input().split()))
s = [0] * (N+1)

for i in range(N):
    s[i] = input()
s[N] = 'AB'

ans = [0] * N
for i in range(N):
    if s[i] == 'AB':
        if A > B:
            A -= 1
            B += 1
            ans[i] = 'B'
        elif A < B:
            A += 1
            B -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'BC':
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
    elif s[i] == 'AC':
        if A > C:
            A -= 1
            C += 1
            ans[i] = 'C'
        elif A < C:
            A += 1
            C -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'AB':
                A += 1
                C -= 1
                ans[i] = 'A'
            else:
                A -= 1
                C += 1
                ans[i] = 'C'
    elif s[i] == 'BC':
        if B > C:
            B -= 1
            C += 1
            ans[i] = 'C'
        elif B < C:
            B += 1
            C -= 1
            ans[i] = 'B'
        else:
            if s[i+1] == 'AB':
                B += 1
                C -= 1
                ans[i] = 'B'
            else:
                B -= 1
                C += 1
                ans[i] = 'C'

    if A < 0 or B < 0 or C < 0:
        print('No')
        sys.exit()
print('Yes')
for i in range(N):
    print(ans[i])

Solution (Python):
n, a, b, c = map(int, input().split())
S = []
T = []
r = 0
for i in range(n):
    S.append(input())
for i in range(n):
    if S[i] == "AB":
        if a < b:
            a += 1
            b -= 1
            T.append("A")
        elif a > b:
            a -= 1
            b += 1
            T.append("B")
        else:
            if i < n-1:
                if S[i+1] == "AC":
                    a += 1
                    b -= 1
                    T.append("A")
                else:
                    a -= 1
                    b += 1
                    T.append("B")
            else:
                a += 1
                b -= 1
                T.append("A")
    elif S[i] == "AC":
        if a < c:
            a += 1
            c -= 1
            T.append("A")
        elif a > c:
            a -= 1
            c += 1
            T.append("C")
        else:
            if i < n-1:
                if S[i+1] == "AB":
                    a += 1
                    c -= 1
                    T.append("A")
                else:
                    a -= 1
                    c += 1
                    T.append("C")
            else:
                a += 1
                c -= 1
                T.append("A")
    else:
        if b < c:
            b += 1
            c -= 1
            T.append("B")
        elif b > c:
            b -= 1
            c += 1
            T.append("C")
        else:
            if i < n-1:
                if S[i+1] == "AB":
                    b += 1
                    c -= 1
                    T.append("B")
                else:
                    b -= 1
                    c += 1
                    T.append("C")
            else:
                b += 1
                c -= 1
                T.append("B")
    if a < 0 or b < 0 or c < 0:
        r = 1
        break;
if r == 0:
    print("Yes")
    for i in range(n):
        print(T[i])
else:
    print("No")

Solution (Python):




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

  for move, next_move in zip(moves[:-1], moves[1:]):
    if move == 'AB':
      i, j, lc, rc = 0, 1, 'A', 'B'
    elif move == 'AC':
      i, j, lc, rc = 0, 2, 'A', 'C'
    elif move == 'BC':
      i, j, lc, rc = 1, 2, 'B', 'C'

    if next_move == 'AB':
      ni, nj = 0, 1
    elif next_move == 'AC':
      ni, nj = 0, 2
    elif next_move == 'BC':
      ni, nj = 1, 2

    if values[i] == 0 and values[j] == 0:
      cmds = None
      break
    elif values[i] < values[j]:
      cmds.append(lc)
      values[i] += 1
      values[j] -= 1
    elif values[i] > values[j]:
      cmds.append(rc)
      values[i] -= 1
      values[j] += 1
    elif ni == i or nj == i:
      cmds.append(lc)
      values[i] += 1
      values[j] -= 1
    elif ni == j or nj == j:
      cmds.append(rc)
      values[i] -= 1
      values[j] += 1
    else:
      cmds.append(lc)
      values[i] += 1
      values[j] -= 1

  if cmds is not None:
    print('Yes')
    for c in cmds:
      print(c)
  else:
    print('No')


if __name__ == '__main__':
  main()

Solution (Python):
N, A, B, C = map(int, input().split())
v = [A, B, C]
s = [-1 for i in range(N)]
for i in range(N):
    t = input()
    if t == "AB":
        s[i] = 0
    if t == "BC":
        s[i] = 1
    if t == "AC":
        s[i] = 2
ans = [-1 for i in range(N)]
OK = True

for i in range(N):
    p = s[i]
    q = (s[i]+1)%3
    if v[p] < v[q]:
        ans[i] = p
        v[p] += 1
        v[q] -= 1
    elif v[p] > v[q]:
        ans[i] = q
        v[p] -= 1
        v[q] += 1
    elif v[p] > 0:
        if i == N-1:
            ans[i] = p
            v[p] += 1
            v[q] -= 1
        elif s[i+1] == q:
            ans[i] = q
            v[p] -= 1
            v[q] += 1
        else:
            ans[i] = p
            v[p] += 1
            v[q] -= 1
    else:
        OK = False
        break

ch = ["A", "B" ,"C"]
if OK:
    print("Yes")
    for i in range(N):
        print(ch[ans[i]])
else:
    print("No")

Solution (Python):
import sys
N, A, B, C = map(int, input().split())
d=dict()
d["A"] = A
d["B"] = B
d["C"] = C
h = {"AB":"C","BC":"A","AC":"B"}
S = []
for _ in range(N):
    S.append(input().strip())
if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    R = []
    for k in range(N):
        s = S[k]
        for i in range(2):
            if d[s[i]] == 1:
                R.append(s[1-i])
                d[s[i]] = 0
                d[s[1-i]] = 1
                break
        else:
            print("No")
            break
    else:
        print("Yes")
        for i in range(N):
            print(R[i])
else:
    R = []
    if d[S[0][0]] == 0 and d[S[0][1]] == 0:
        print("No")
        sys.exit()
    for i in range(N-1):
        s = S[i]
        if d[s[0]] == 1 and d[s[1]] == 1 and d[h[s]] == 0:
            if S[i+1] == s:
                d[s[0]] += 1
                d[s[1]] -= 1
                R.append(s[0])
            else:
                for j in range(2):
                    if s[j] in S[i+1]:
                        d[s[j]] += 1
                        d[s[1-j]] -= 1
                        R.append(s[j])
                        break
        elif d[s[0]] >= d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            R.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            R.append(s[0])
    for i in range(2):
        if d[S[N-1][i]] > 0:
            R.append(S[N-1][1-i])
            break
    print("Yes")
    for i in range(N):
        print(R[i])

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

N, A, B, C = list(map(int, input().split()))
s = [0] * (N+1)

for i in range(N):
    s[i] = input()
s[N] = 'AB'

sum = A + B + C
if sum != 2:
    ans = [0] * N
    for i in range(N):
        if s[i] == 'AB':
            if A > B:
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
        elif s[i] == 'AC':
            if A > C:
                A -= 1
                C += 1
                ans[i] = 'C'
            else:
                A += 1
                C -= 1
                ans[i] = 'A'
        elif s[i] == 'BC':
            if B > C:
                B -= 1
                C += 1
                ans[i] = 'C'
            else:
                B += 1
                C -= 1
                ans[i] = 'B'
        if A < 0 or B < 0 or C < 0:
            print('No')
            sys.exit()
    print('Yes')
    for i in range(N):
        print(ans[i])
    sys.exit()


ans = [0] * N
for i in range(N):
    if s[i] == 'AB':
        if A > B:
            A -= 1
            B += 1
            ans[i] = 'B'
        elif A < B:
            A += 1
            B -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'BC':
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
    elif s[i] == 'AC':
        if A > C:
            A -= 1
            C += 1
            ans[i] = 'C'
        elif A < C:
            A += 1
            C -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'AB':
                A += 1
                C -= 1
                ans[i] = 'A'
            else:
                A -= 1
                C += 1
                ans[i] = 'C'
    elif s[i] == 'BC':
        if B > C:
            B -= 1
            C += 1
            ans[i] = 'C'
        elif B < C:
            B += 1
            C -= 1
            ans[i] = 'B'
        else:
            if s[i+1] == 'AB':
                B += 1
                C -= 1
                ans[i] = 'B'
            else:
                B -= 1
                C += 1
                ans[i] = 'C'

    if A < 0 or B < 0 or C < 0:
        print('No')
        sys.exit()
print('Yes')
for i in range(N):
    print(ans[i])

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

Solution (Python):
N, A, B, C = map(int, input().split())
move = []
for i in range(N):
    new = input()
    move.append(new)
    
flag = 1
path = []
for i in range(N):
    if move[i] == "AB":
        if B==0:
            if A==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "AC" and A==1 and C==0:
                    flag=0
                    break
                else:
                    B += 1
                    A -= 1
                    path.append("B")
            else:
                B += 1
                A -= 1
                path.append("B")
        elif A==0:
            if B==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "BC" and B==1 and C==0:
                    flag=0
                    break
                else:
                    A += 1
                    B -= 1
                    path.append("A")
            else:
                A += 1
                B -= 1
                path.append("A")
        else:
            if i < N-1:
                if move[i+1] == "BC" and B==1 and C==0:
                    B += 1
                    A -= 1
                    path.append("B")
                else:
                    A += 1
                    B -= 1
                    path.append("A")
            else:
                A += 1
                B -= 1
                path.append("A")
    elif move[i] == "AC":
        if C==0:
            if A==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "AB" and A==1 and B==0:
                    flag=0
                    break
                else:
                    C += 1
                    A -= 1
                    path.append("C")
            else:
                C += 1
                A -= 1
                path.append("C")
        elif A==0:
            if C==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "BC" and B==0 and C==1:
                    flag=0
                    break
                else:
                    A += 1
                    C -= 1
                    path.append("A")
            else:
                A += 1
                C -= 1
                path.append("A")
        else:
            if i < N-1:
                if move[i+1] == "BC" and B==0 and C==1:
                    C += 1
                    A -= 1
                    path.append("C")
                else:
                    A += 1
                    C -= 1
                    path.append("A")
            else:
                A += 1
                C -= 1
                path.append("A")
    else:
        if B==0:
            if C==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "AC" and A==0 and C==1:
                    flag=0
                    break
                else:
                    B += 1
                    C -= 1
                    path.append("B")
            else:
                B += 1
                C -= 1
                path.append("B")
        elif C==0:
            if B==0:
                flag=0
                break
            elif i < N-1:
                if move[i+1] == "AB" and B==1 and A==0:
                    flag=0
                    break
                else:
                    C += 1
                    B -= 1
                    path.append("C")
            else:
                C += 1
                B -= 1
                path.append("C")
        else:
            if i < N-1:
                if move[i+1] == "AB" and A==0 and B==1:
                    B += 1
                    C -= 1
                    path.append("B")
                else:
                    C += 1
                    B -= 1
                    path.append("C")
            else:
                C += 1
                B -= 1
                path.append("C")  
    if flag==0:
        break
    
                
if flag == 0:
    print("No")
else:
    print("Yes")
    for p in path:
        print(p)

Solution (Python):
N,A,B,C = map(int,input().split())
q = [input() for i in range(N)]
d = {'A':A,'B':B,'C':C}
ans = []
possible = True
for i in range(N):
    x = q[i][0]
    y = q[i][1]
    z = [s for s in 'ABC' if s!=x and s!=y][0]
    if d[x]>d[y]:
        ans.append(y)
        d[x] -= 1
        d[y] += 1
    elif d[x]<d[y]:
        ans.append(x)
        d[x] += 1
        d[y] -= 1
    else:
        if d[x]==1 and d[y]==1 and d[z]==0:
            if i==N-1 or q[i+1]==q[i]:
                ans.append(x)
                d[x] += 1
                d[y] -= 1
            elif x in q[i+1]:
                ans.append(x)
                d[x] += 1
                d[y] -= 1
            else:
                ans.append(y)
                d[y] += 1
                d[x] -= 1
        elif d[x]==0 and d[y]==0:
            possible = False
        else:
            ans.append(x)
            d[x] += 1
            d[y] -= 1

if possible:
    print('Yes')
    for i in range(N):
        print(ans[i])
else:
    print('No')

Solution (Python):
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
ans = []
flg = True
def adding(x, y):
    x += 1
    y -= 1

for i in range(n):
    if s[i] == 'AB':
        if a == b:
            if i < n-1:
                if s[i+1] == 'AC':
                    a += 1
                    b -= 1
                    ans.append('A')
                elif s[i+1] == 'BC':
                    b += 1
                    a -= 1
                    ans.append('B')
                else:
                    a += 1
                    b -= 1
                    ans.append('A')
            else:
                a += 1
                b -= 1
                ans.append('A')
        elif a < b:
            a += 1
            b -= 1
            ans.append('A')
        else:
            b += 1
            a -= 1
            ans.append('B')

    if s[i] == 'AC':
        if a == c:
            if i < n - 1:
                if s[i + 1] == 'AB':
                    a += 1
                    c -= 1
                    ans.append('A')
                elif s[i + 1] == 'BC':
                    c += 1
                    a -= 1
                    ans.append('C')
                else:
                    a += 1
                    c -= 1
                    ans.append('A')
            else:
                a += 1
                c -= 1
                ans.append('A')
        elif a < c:
            a += 1
            c -= 1
            ans.append('A')
        else:
            c += 1
            a -= 1
            ans.append('C')

    if s[i] == 'BC':
        if b == c:
            if i < n - 1:
                if s[i + 1] == 'AB':
                    b += 1
                    c -= 1
                    ans.append('B')
                elif s[i + 1] == 'AC':
                    c += 1
                    b -= 1
                    ans.append('C')
                else:
                    b += 1
                    c -= 1
                    ans.append('B')
            else:
                b += 1
                c -= 1
                ans.append('B')
        elif b < c:
            b += 1
            c -= 1
            ans.append('B')
        else:
            c += 1
            b -= 1
            ans.append('C')

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

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

else:
    print('No')

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))

from collections import defaultdict, Counter
from sys import exit
import math



def main():
    n, a, b, c = getList()
    a = min(a,2)
    b = min(b, 2)
    c = min(c, 2)
    dp = [[0 for i in range(28)] for j in range(n+1)]
    dp[0][a*9 + b * 3 + c] = 1
    for i in range(n):
        q = input().strip()
        if q == "AB":
            
            for na in range(1, 3):
                for nb in range(3):
                    for nc in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][(na-1) * 9 + min(nb+1, 2) * 3 + nc] = ["AB", na * 9 + nb * 3 + nc]

            for nb in range(1, 3):
                for na in range(3):
                    for nc in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][min(na+1, 2) * 9 + (nb-1) * 3 + nc] = ["BA", na * 9 + nb * 3 + nc]

        if q == "BC":
            
            for nb in range(1, 3):
                for na in range(3):
                    for nc in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][na * 9 + (nb-1) * 3 + min(nc+1, 2)] = ["BC", na * 9 + nb * 3 + nc]

            for nc in range(1, 3):
                for na in range(3):
                    for nb in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][na * 9 + min(nb+1, 2) * 3 + nc-1] = ["CB", na * 9 + nb * 3 + nc]

        if q == "AC":
            
            for na in range(1, 3):
                for nb in range(3):
                    for nc in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][(na-1) * 9 + nb * 3 + min(nc + 1, 2)] = ["AC", na * 9 + nb * 3 + nc]

            for nc in range(1, 3):
                for na in range(3):
                    for nb in range(3):
                        if dp[i][na * 9 + nb * 3 + nc] != 0:
                            dp[i+1][min(na+1, 2) * 9 + nb * 3 + nc - 1] = ["CA", na * 9 + nb * 3 + nc]


    
    

    for i, tgt in enumerate(dp[-1]):
        if tgt != 0:
            ans = hukugen(dp, i, n)
            print("Yes")
            for an in ans:
                print(an)

            return


    print("No")

def hukugen(dp, cur, n):
    ans = []
    taiou = {"A": 9, "B": 3, "C":1}
    for i in range(n):
        moji = dp[n-i][cur][0]
        
        ans.append(moji[1])
        cur =dp[n-i][cur][1]
        

    return (list(reversed(ans)))


if __name__ == "__main__":
    main()