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


def main():
    N, A, B, C = [int(v) for v in input().split()]

    total = A + B + C

    if total == 0:
        print('No')
        exit()

    choice = []
    if total != 2:
        for _ in range(N):
            s = sys.stdin.readline().rstrip()
            if s == 'AB':
                if A == 0 and B == 0:
                    print('No')
                    exit()
                if A < B:
                    B -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    B += 1
                    choice.append('B')
            elif s == 'BC':
                if B == 0 and C == 0:
                    print('No')
                    exit()
                if B < C:
                    C -= 1
                    B += 1
                    choice.append('B')
                else:
                    B -= 1
                    C += 1
                    choice.append('C')
            elif s == 'AC':
                if A == 0 and C == 0:
                    print('No')
                    exit()
                if A < C:
                    C -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    C += 1
                    choice.append('C')
    else:
        s = sys.stdin.readline().rstrip()
        for _ in range(N - 1):
            s1 = sys.stdin.readline().rstrip()
            if s == 'AB':
                if A == 0 and B == 0:
                    print('No')
                    exit()
                if A < B:
                    B -= 1
                    A += 1
                    choice.append('A')
                elif A > B:
                    A -= 1
                    B += 1
                    choice.append('B')
                elif s1 == 'AC':
                    A += 1
                    B -= 1
                    choice.append('A')
                else:
                    A -= 1
                    B += 1
                    choice.append('B')

            elif s == 'BC':
                if B == 0 and C == 0:
                    print('No')
                    exit()
                if B < C:
                    C -= 1
                    B += 1
                    choice.append('B')
                elif B > C:
                    B -= 1
                    C += 1
                    choice.append('C')
                elif s1 == 'AC':
                    B -= 1
                    C += 1
                    choice.append('C')
                else:
                    C -= 1
                    B += 1
                    choice.append('B')

            elif s == 'AC':
                if A == 0 and C == 0:
                    print('No')
                    exit()
                if A < C:
                    C -= 1
                    A += 1
                    choice.append('A')
                elif A > C:
                    A -= 1
                    C += 1
                    choice.append('C')
                elif s1 == 'AB':
                    C -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    C += 1
                    choice.append('C')
            s = s1

        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                exit()
            if A < B:
                B -= 1
                A += 1
                choice.append('A')
            else:
                A -= 1
                B += 1
                choice.append('B')

        elif s == 'BC':
            if B == 0 and C == 0:
                print('No')
                exit()
            if B < C:
                C -= 1
                B += 1
                choice.append('B')
            else:
                B -= 1
                C += 1
                choice.append('C')

        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                exit()
            if A < C:
                C -= 1
                A += 1
                choice.append('A')
            else:
                A -= 1
                C += 1
                choice.append('C')

    print('Yes')
    for c in choice:
        print(c)


main()

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

u = ["A","B","C"]
ss = ["BC","AC","AB"]
t = [A,B,C]
trace = []
ans = "Yes"
for i in range(N):
  idx = ss.index(s[i])
  if t[(idx+1)%3] == 0 and t[(idx+2)%3] == 0:
    ans = "No"
    break
  elif t[(idx+2)%3] == 0:
    t[(idx+1)%3] -= 1
    t[(idx+2)%3] += 1
    trace.append(u[(idx+2)%3])
  elif t[(idx+1)%3] == 0:
    t[(idx+1)%3] += 1
    t[(idx+2)%3] -= 1
    trace.append(u[(idx+1)%3])
  elif i+1<N and s[i+1] == ss[(idx+1)%3]:
    t[(idx+1)%3] -= 1
    t[(idx+2)%3] += 1
    trace.append(u[(idx+2)%3])
  else:
    t[(idx+1)%3] += 1
    t[(idx+2)%3] -= 1
    trace.append(u[(idx+1)%3])
print(ans)
if ans == "Yes":
  for v in trace:
    print(v)

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

if a+b+c == 0:
  print("No")
  exit()
if a+b+c == 1:
  out = ["Yes"]

  for i in range(n):
    if nums[command[i][0]] == 0 and nums[command[i][1]] == 0:
      print("No")
      exit()
    elif nums[command[i][0]] == 0:
      out.append(command[i][0])
      nums[command[i][0]] = 1
      nums[command[i][1]] = 0
    elif nums[command[i][1]] == 0:
      out.append(command[i][1])
      nums[command[i][1]] = 1
      nums[command[i][0]] = 0
  for i in range(n+1):
    print(out[i])

if a+b+c >= 2:
  if nums[command[0][0]] == 0 and nums[command[0][1]] == 0:
    print("No")
    exit()
  print("Yes")
  for i in range(n):
    if nums[command[i][0]] == 0:
      print(command[i][0])
      nums[command[i][0]] = nums[command[i][0]] +1
      nums[command[i][1]] = nums[command[i][1]] -1
    elif nums[command[i][1]] == 0:
      print(command[i][1])
      nums[command[i][1]] = nums[command[i][1]]+1
      nums[command[i][0]] = nums[command[i][0]]-1
    elif a+b+c==2 and nums[command[i][0]] == 1 and nums[command[i][1]] == 1\
    and i < n-1 and command[i] != command[i+1]:
      if command[i][0] in command[i+1]:
        print(command[i][0])
        nums[command[i][0]] = nums[command[i][0]]+1
        nums[command[i][1]] = nums[command[i][1]]-1
      else:
        print(command[i][1])
        nums[command[i][1]] = nums[command[i][1]]+1
        nums[command[i][0]] = nums[command[i][0]]-1       
    else:
      if nums[command[i][0]] < nums[command[i][1]]:
        print(command[i][0])
        nums[command[i][0]] = nums[command[i][0]]+1
        nums[command[i][1]] = nums[command[i][1]]-1
      else:
        print(command[i][1])
        nums[command[i][1]] = nums[command[i][1]]+1
        nums[command[i][0]] = nums[command[i][0]]-1

Solution (Python):
def change(d,ans,X,Y):
    z = {'A': 0, 'B': 1, 'C': 2}
    d[z[X]]+=1
    d[z[Y]]-=1
    ans.append(X)
    return d,ans

n,a,b,c = map(int, input().split())
d = [a,b,c]
q = [input() for i in range(n)]
z = {'A': 0, 'B': 1, 'C': 2}

if sum(d)==0:
    print('No')
elif sum(d)==1:
    ans = []
    for i in range(n):
        X = q[i][0]
        Y = q[i][1]
        if d[z[X]]>0:
            d,ans = change(d,ans,Y,X)
        elif d[z[Y]]>0:
            d,ans = change(d,ans,X,Y)
        else:
            print('No')
            exit()
    print('Yes')
    print('\n'.join(ans))
else:
    ans = []
    X = q[0][0]
    Y = q[0][1]
    if d[z[X]]==d[z[Y]]==0:
        print('No')
        exit()
    for i in range(n-1):
        X = q[i][0]
        Y = q[i][1]
        if d[z[X]]==0:
            d,ans = change(d,ans,X,Y)
        elif d[z[Y]]==0:
            d,ans = change(d,ans,Y,X)
        else:
            if X in q[i+1]:
                d,ans = change(d,ans,X,Y)
            elif Y in q[i+1]:
                d,ans = change(d,ans,Y,X)
    X = q[-1][0]
    Y = q[-1][1]
    if d[z[X]]>0:
        ans.append(Y)
    else:
        ans.append(X)
    print('Yes')
    print('\n'.join(ans))

Solution (Python):


n,a,b,c = map(int,input().split())
if a + b + c == 0:
  print('No')
  exit()
elif a + b + c == 1:
  ans = []
  for _ in range(n):
    q = input()
    if q == 'AB':
      if a + b == 0:
        print('No')
        exit()
      a,b = b,a
      if a:
        ans.append('A')
      else:
        ans.append('B')
    elif q == 'BC':
      if b + c == 0:
        print('No')
        exit()
      b,c = c,b
      if b:
        ans.append('B')
      else:
        ans.append('C')
    else:
      if c + a == 0:
        print('No')
        exit()
      c,a = a,c
      if a:
        ans.append('A')
      else:
        ans.append('C')
  print('Yes')
  for x in ans:
    print(x)
  exit()
p = []
for _ in range(n):
  p.append(input())
if a + b == 0 and p[0] == 'AB':
    print('No')
    exit()
if b + c == 0 and p[0] == 'BC':
    print('No')
    exit()
if a + c == 0 and p[0] == 'AC':
    print('No')
    exit()
print('Yes')
for i in range(n):
  if p[i] == 'AB':
    if a == 0:
      a += 1
      b -= 1
      print('A')
    elif b == 0:
      a -= 1
      b += 1
      print('B')
    elif i != n-1:
      if p[i+1] == 'BC':
        a -= 1
        b += 1
        print('B')
      else:
        a += 1
        b -= 1
        print('A')
    else:
      a += 1
      b -= 1
      print('A')
  elif p[i] == 'BC':
    if b == 0:
      b += 1
      c -= 1
      print('B')
    elif c == 0:
      b -= 1
      c += 1
      print('C')
    elif i != n-1:
      if p[i+1] == 'AC':
        b -= 1
        c += 1
        print('C')
      else:
        b += 1
        c -= 1
        print('B')
    else:
      b += 1
      c -= 1
      print('B')
  else:
    if a == 0:
      a += 1
      c -= 1
      print('A')
    elif c == 0:
      a -= 1
      c += 1
      print('C')
    elif i != n-1:
      if p[i+1] == 'BC':
        a -= 1
        c += 1
        print('C')
      else:
        a += 1
        c -= 1
        print('A')
    else:
      a += 1
      c -= 1
      print('A')

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

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for i in range(n):
    s = sl[i]
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if n-1 == i:
                ansl.append("A")
                a += 1
                b -= 1
            elif sl[i+1] == "AC":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if n-1 == i:
                ansl.append("B")
                b += 1
                c -= 1
            elif  sl[i+1] == "AB":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if n-1 == i:
                ansl.append("C")
                c += 1
                a -= 1
            elif sl[i+1] == "BC":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

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

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

v = [A, B, C]
id_d = {'A':0, 'B':1, 'C':2}
ans = [0] * N
for i, s in enumerate(S):
  s1, s2 = s
  id1, id2 = id_d[s1], id_d[s2]
  v1, v2 = v[id1], v[id2]
  if v1 == v2 == 0:
    if i == 0:
      print('No')
      quit()
    else:
      pre_s1, pre_s2 = S[i-1]
      pre_id1, pre_id2 = id_d[pre_s1], id_d[pre_s2]
      if pre_s1 in [s1, s2]:
        v[pre_id1] += 2
        v[pre_id2] -= 2
        ans[i-1] = pre_s1
        if v[pre_id2] < 0:
          print('No')
          quit()
      else: 
        v[pre_id2] += 2
        v[pre_id1] -= 2
        ans[i-1] = pre_s2
        if v[pre_id1] < 0:
          print('No')
          quit()
  v1, v2 = v[id1], v[id2]
  if v1 >= v2:
    ans[i] = s2
    v[id1] -= 1
    v[id2] += 1
  else:
    ans[i] = s1
    v[id1] += 1
    v[id2] -= 1

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

Solution (Python):
n, a, b, c = map(int, input().split())
d = {"A": a, "B": b, "C": c}

pl = [input() for _ in range(n)]

res = []
for i, p in enumerate(pl):
    if d[p[0]] == d[p[1]] == 0:
        print("No")
        exit()
    elif d[p[0]] == 0:
        res.append(p[0])
        d[p[0]] += 1
        d[p[1]] -= 1
    elif d[p[1]] == 0:
        res.append(p[1])
        d[p[0]] -= 1
        d[p[1]] += 1
    elif i == n - 1:
        res.append(p[0])
    elif p[0] in pl[i + 1]:
        res.append(p[0])
        d[p[0]] += 1
        d[p[1]] -= 1
    else:
        res.append(p[1])
        d[p[0]] -= 1
        d[p[1]] += 1

print("Yes")
for p in res:
    print(p)

Solution (Python):
import sys
def input():
    return sys.stdin.readline().strip()
n, a, b, c = map(int, input().split())
s = []
for _ in range(n):
    i = input()
    if i == 'AB':
        s.append(2)
    elif i == 'AC':
        s.append(1)
    else:
        s.append(0)


if a == 0 and b == 0 and c == 0:
    print('No')
    exit()

if a == 0 and b == 0:
    if s[0] == 2:
        print('No')
        exit()
if a == 0 and c == 0:
    if s[0] == 1:
        print('No')
        exit()
if b == 0 and c == 0:
    if s[0] == 0:
        print('No')
        exit()

ans = []
if a + b + c == 1:
    for i in range(n):
        if s[i] == 0:
            if b > c:
                ans.append('C')
                b -= 1
                c += 1
            else:
                ans.append('B')
                b += 1
                c -= 1

        elif s[i] == 1:
            if a > c:
                ans.append('C')
                a -= 1
                c += 1
            else:
                ans.append('A')
                a += 1
                c -= 1

        else:
            if a > b:
                ans.append('B')
                a -= 1
                b += 1
            else:
                ans.append('A')
                a += 1
                b -= 1
        if a < 0 or b < 0 or c < 0:
            print('No')
            exit()
    
    print('Yes')
    for i in ans:
        print(i)
    exit()


print('Yes')
for i in range(n-1):
    if s[i] == 0:
        if b > c:
            print('C')
            b -= 1
            c += 1
        elif b < c:
            print('B')
            b += 1
            c -= 1
        elif s[i+1] == 2:
            print('B')
            b += 1
            c -= 1
        else:
            print('C')
            b -= 1
            c += 1

    elif s[i] == 1:
        if a > c:
            print('C')
            a -= 1
            c += 1
        elif a < c:
            print('A')
            a += 1
            c -= 1
        elif s[i+1] == 0:
            print('C')
            a -= 1
            c += 1
        else:
            print('A')
            a += 1
            c -= 1

    else:
        if a > b:
            print('B')
            a -= 1
            b += 1
        elif a < b:
            print('A')
            a += 1
            b -= 1
        elif s[i+1] == 1:
            print('A')
            a += 1
            b -= 1
        else:
            print('B')
            a -= 1
            b += 1


if s[n-1] == 0:
    if b > c:
        print('C')
        b -= 1
        c += 1
    else:
        print('B')
        b += 1
        c -= 1

elif s[n-1] == 1:
    if a > c:
        print('C')
        a -= 1
        c += 1
    else:
        print('A')
        a += 1
        c -= 1

else:
    if a > b:
        print('B')
        a -= 1
        b += 1
    else:
        print('A')
        a += 1
        b -= 1

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N,A,B,C = LI()
s = ['0']*N
for i in range(N):
    s[i] = str(input())

d = defaultdict(int)
d['A'] = A
d['B'] = B
d['C'] = C

def plus_minus(s,t):
    for c in ['A','B','C']:
        if (c in s) and (c in t):
            plus = c
    if s[0] == plus:
        minus = s[1]
    else:
        minus = s[0]
    return plus, minus 

def min_max(s):
    if d[s[0]] >= d[s[1]]:
        return s[1],s[0]
    else:
        return s[0],s[1]

if A+B+C == 0:
    print('No')
elif A+B+C == 1:
    ans = []
    for i in range(N):
        if d[s[i][0]] == d[s[i][1]] == 0:
            print('No')
            exit()
        else:
            if d[s[i][0]]:
                d[s[i][0]] -= 1
                d[s[i][1]] += 1
                ans.append(s[i][1])
            else:
                d[s[i][1]] -= 1
                d[s[i][0]] += 1
                ans.append(s[i][0])
    print('Yes')
    for a in ans:
        print(a)
else:
    if d[s[0][0]] == d[s[0][1]] == 0:
        print('No')
    else:
        print('Yes')
        for i in range(N):
            if d[s[i][0]] == 0:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                print(s[i][0])
            elif d[s[i][1]] == 0:
                d[s[i][1]] += 1
                d[s[i][0]] -= 1
                print(s[i][1])
            elif d[s[i][0]] == d[s[i][1]] == 1:
                if i != N-1:
                    p,m = plus_minus(s[i],s[i+1])
                    d[p] += 1
                    d[m] -= 1
                    print(p)
                else:
                    d[s[i][0]] += 1
                    d[s[i][1]] -= 1
                    print(s[i][0])
            else:
                min_,max_ = min_max(s[i])
                d[min_] += 1
                d[max_] -= 1
                print(min_)

Solution (Python):
N,A,B,C = map(int,(input().split()))
s = [[] for _ in range(N+1)]
for i in range(N):
    s[i] = input()
s[N] = "NOTHING"

flag = 0
ans = []
S = A+B+C

if S >= 3:
    for i in range(N):
        if s[i] == "AB":
            if A+B <= 0:
                flag = 1
                break
            if A >= B:
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C <= 0:
                flag = 1
                break
            if A >= C:
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C <= 0:
                flag = 1
                break
            if B >= C:
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("B")
                B += 1
                C -= 1
    if flag == 1:
        print("No")
    else:
        print("Yes")
        for i in ans:
            print(i)

else:
    
    for i in range(N):
        
        if s[i] == "AB":
            if A+B <= 0:
                flag = 1
                break
            if B == 0:
                ans.append("B")
                B += 1
                A -= 1
            elif A == 0:
                ans.append("A")
                A += 1
                B -= 1
            else:
                if s[i+1] == "AC":
                    ans.append("A")
                    A += 1
                    B -= 1
                else:
                    ans.append("B")
                    B += 1
                    A -= 1

        if s[i] == "AC":
            if A+C <= 0:
                flag = 1
                break
            if C == 0:
                ans.append("C")
                C += 1
                A -= 1
            elif A == 0:
                ans.append("A")
                A += 1
                C -= 1
            else:
                if s[i+1] == "BC":
                    ans.append("C")
                    C += 1
                    A -= 1
                else:
                    ans.append("A")
                    A += 1
                    C -= 1

        if s[i] == "BC":
            if B+C <= 0:
                flag = 1
                break
            if C == 0:
                ans.append("C")
                C += 1
                B -= 1
            elif B == 0:
                ans.append("B")
                B += 1
                C -= 1
            else:
                if s[i+1] == "AC":
                    ans.append("C")
                    C += 1
                    B -= 1
                else:
                    ans.append("B")
                    B += 1
                    C -= 1

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

Solution (Python):
import sys

alpha = 'ABC'
N, A, B, C = map(int, input().split())
S = []
for s in sys.stdin.readlines():
    s = list(s)
    i, j, _ = s
    S.append((alpha.index(i), alpha.index(j)))

num = [A, B, C]


ope = []
for idx, (i, j) in enumerate(S):
    for k in range(3):
        if k not in (i, j):
            break

    if num[i] == 0 and num[j] == 0:
        ans = 'No'
        break
    
    if num[i] == 1 and num[j] == 1 and num[k] == 0 and idx < N - 1:
        if i in S[idx + 1]:
            plus, minus = i, j
        else:
            plus, minus = j, i
    else:
        if num[i] < num[j]:
            plus, minus = i, j
        else:
            plus, minus = j, i
    
    num[plus] += 1
    num[minus] -= 1
    ope.append(alpha[plus])
    

else:
    ans = 'Yes'

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

Solution (Python):
import collections
def main():

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

    ans = ""
    s = []
    for _ in range(N):
        s.append(input())

    d = dict()
    d['A'] = A
    d['B'] = B
    d['C'] = C
    ans = ""
    for i in range(N):
        if d[s[i][0]] == 0 and d[s[i][1]] == 0:
            break
        elif d[s[i][0]] == 0:
            d[s[i][0]] += 1
            d[s[i][1]] -= 1
            ans += s[i][0]
        elif d[s[i][1]] == 0:
            d[s[i][1]] += 1
            d[s[i][0]] -= 1
            ans += s[i][1]
        else:
            if d[s[i][0]] == 1 and d[s[i][1]] == 1 and d['A'] + d['B'] + d['C'] == 2:
                if i < N-1 and s[i] != s[i+1] and s[i][0] in s[i+1]:
                    d[s[i][0]] += 1
                    d[s[i][1]] -= 1
                    ans += s[i][0]
                else:
                    d[s[i][1]] += 1
                    d[s[i][0]] -= 1
                    ans += s[i][1]
            elif d[s[i][0]] < d[s[i][1]]:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                ans += s[i][0]
            else:
                d[s[i][1]] += 1
                d[s[i][0]] -= 1
                ans += s[i][1]

    if len(ans) == N:
        print("Yes")
        for c in ans:
            print(c)
    else:
        print("No")

if __name__ == '__main__':
    main()

Solution (Python):
n,a,b,c = map(int, input().split())
s=[input() for i in range(n)]
ans=[]
from collections import defaultdict
d = defaultdict(int)
d["A"]=a
d["B"]=b
d["C"]=c
def add(c1,c2):
    global d
    ans.append(c1)
    d[c1]+=1
    d[c2]-=1

for i in range(n):
    que = s[i]
    nxt="NONE"
    if i!=n-1:
        nxt = s[i+1]

    q1,q2=que

    if d[q1]==d[q2]==0:
        print("No")
        exit()
    if d[q1]==d[q2]==1:
        if q1 in nxt:
            add(q1,q2)
        else:
            add(q2,q1)
    elif d[q1]>=d[q2]:
        add(q2,q1)
    else:
        add(q1,q2)

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

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

s = [0] * N
for i in range(N):
  s[i] = str(input())
  
d = {"AB":(0, 1, 2), "BC": (2, 1, 0), "AC": (0, 2, 1)}  
dl = ["A", "B", "C"]  

if ABC == [0, 0, 0]:
  print("No")
  quit()

ans = [0] * N  
if A + B + C == 1:
  for n in range(N):
    i, j, k = d[s[n]]
    
    if ABC[i] == 1:
      ABC[i] = 0
      ABC[j] = 1
      ans[n] = dl[j]
    elif ABC[j] == 1:
      ABC[i] = 1
      ABC[j] = 0
      ans[n] = dl[i]
    else:
      print("No")
      quit()
  print("Yes")
  for i in range(N):
    print(ans[i])
    
else:
  for n in range(N):
    i, j, k = d[s[n]]
    
    if ABC[i] == 0:
      if ABC[j] == 0:
        print("No")
        quit()
      else:
        ABC[i] += 1
        ABC[j] -= 1
        ans[n] = dl[i]
    elif ABC[j] == 0:
      ABC[i] -= 1
      ABC[j] += 1
      ans[n] = dl[j]
    else:
      if n < N - 1:
        if (ABC[i] == 1) or (ABC[j] == 1):
          if s[n] != s[n + 1]:
            n1, n2, n3 = d[s[n + 1]]
            if (i == n1) or (i == n2):
              ABC[i] += 1
              ABC[j] -= 1
              ans[n] = dl[i]
            else:
              ABC[i] -= 1
              ABC[j] += 1
              ans[n] = dl[j]
              
          else:
            ABC[i] -= 1
            ABC[j] += 1
            ans[n] = dl[j]
        else:
          ABC[i] -= 1
          ABC[j] += 1
          ans[n] = dl[j]
      else:
        ABC[i] -= 1
        ABC[j] += 1
        ans[n] = dl[j]
        
  print("Yes")
  for i in range(N):
    print(ans[i])

Solution (Python):

itos = ["A", "B", "C"]

def swap(ABC, f, t):
    ABC[f] -= 1
    ABC[t] += 1
    return itos[t]


def main():
    N, A, B, C = map(int, input().split())
    S = [input() for i in range(N)]
    ABC = [A, B, C]
    StoP = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
    itos = ["A", "B", "C"]
    L = []
    for i in range(N):
        x, y = StoP[S[i]]

        if ABC[x] == 0 and ABC[y] == 0:
            print("No")
            return
        if ABC[x] == 1 and ABC[y] == 1 and i < N - 1 and S[i] != S[i+1]:
            x2, y2 = StoP[S[i+1]]
            if x in [x2, y2]:
                L.append(swap(ABC, y, x))
            else:
                L.append(swap(ABC, x, y))
        elif ABC[x] < ABC[y]:
            L.append(swap(ABC, y, x))
        else:
            L.append(swap(ABC, x, y))

    print("Yes")
    print(*L, sep='\n')
if __name__ == '__main__':
    main()

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

n, a, b, c = map(int, input().split())
count = {
    'A': a,
    'B': b,
    'C': c
}
queries = []
for _ in range(n):
    query = tuple(input().replace('\n', ''))
    queries.append(query)


def solve(plus, minus):
    count[plus] += 1
    count[minus] -= 1
    ans.append(plus)


ans = []
for idx, (x, y) in enumerate(queries):
    if count[x] == count[y] == 0:
        print('No')
        exit()

    if count[x] == count[y] == 1:
        if idx == n-1:
            solve(x, y)
            break

        nex = queries[idx+1]
        if x in nex:
            solve(x, y)
        else:
            solve(y, x)

    elif count[x] >= count[y]:
        solve(y, x)

    else:
        solve(x, y)

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

Solution (Python):
def f(A, i, j, k, nex,res):
    if A[i] == A[j] == 0:
        print("No")
        exit()
    elif A[i] == A[j] == 1 and A[k] == 0:
        if i in nex:
            A[i] += 1
            A[j] -= 1
            return "ABC"[i]
        else:
            A[i] -= 1
            A[j] += 1
            return "ABC"[j]
    else:
        A[i] += 1
        A[j] -= 1
        return "ABC"[i]
    

def main():
    N, A, B, C = [int(_n) for _n in input().split()]
    S = []
    for i in range(N):
        s = input()
        S.append(s)
    if [A,B,C].count(0) == 3:
        print("No")
        return
    res = ""
    arr = [A,B,C]
    S.append("")
    for i,s in enumerate(S[:-1]):
        n = []
        if S[i+1] == "AB":
            n = [0,1]
        if S[i+1] == "BC":
            n = [2,1]
        if S[i+1] == "AC":
            n = [0,2]
        if s == "AB":
            if arr[0] > arr[1]:
                res += f(arr, 1, 0, 2, n,res)
            else:
                res += f(arr,0,1,2,n,res)
        elif s == "BC":
            if arr[1] > arr[2]:
                res += f(arr, 2, 1, 0, n,res)
            else:
                res += f(arr,1,2,0,n,res)
        elif s == "AC":
            if arr[0] > arr[2]:
                res += f(arr, 2,0, 1, n,res)
            else:
                res += f(arr,0,2,1,n,res)
    print("Yes")
    for s in res:
        print(s)
main()

Solution (Python):
import sys
sys.setrecursionlimit(1<<30)
d = 'ABC'
N, *v = map(int, input().split())
e = [tuple(map(d.index, input())) for _ in range(N)]
p = []
def f(i):
	if i == len(e):
		print('Yes')
		print('\n'.join(p))
		raise SystemExit
	k, j = e[i]
	for a, s in ((k, j), (j, k)):
		if v[s] == 0: continue
		v[a] += 1
		v[s] -= 1
		p.append(d[a])
		f(i+1)
		v[a] -= 1
		v[s] += 1
		p.pop()
f(0)
print('No')