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

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  
def LS2(): return list(sys.stdin.readline().rstrip())  


N,A,B,C = MI()
X = [S() for _ in range(N)]

if A+B+C == 0:
    print('No')

elif A+B+C == 1:
    ANS = []
    for i in range(N):
        if A == 1:
            if X[i] == 'BC':
                print('No')
                break
            elif X[i] == 'AB':
                ANS.append('B')
                A,B,C = 0,1,0
            else:
                ANS.append('C')
                A,B,C = 0,0,1
        elif B == 1:
            if X[i] == 'AC':
                print('No')
                break
            elif X[i] == 'AB':
                ANS.append('A')
                A,B,C = 1,0,0
            else:
                ANS.append('C')
                A,B,C = 0,0,1
        else:
            if X[i] == 'AB':
                print('No')
                break
            elif X[i] == 'BC':
                ANS.append('B')
                A,B,C = 0,1,0
            else:
                ANS.append('A')
                A,B,C = 1,0,0
    else:
        print('Yes')
        print(*ANS,sep='\n')

else:
    if X[0] == 'AB' and C == A+B+C:
        print('No')
    elif X[0] == 'BC' and A == A+B+C:
        print('No')
    elif X[0] == 'AC' and B == A+B+C:
        print('No')
    else:
        print('Yes')
        ANS = []
        if A+B+C >= 3:
            for i in range(N):
                if X[i] == 'AB':
                    if A > B:
                        A -= 1
                        B += 1
                        ANS.append('B')
                    else:
                        A += 1
                        B -= 1
                        ANS.append('A')
                elif X[i] == 'AC':
                    if A > C:
                        A -= 1
                        C += 1
                        ANS.append('C')
                    else:
                        A += 1
                        C -= 1
                        ANS.append('A')
                else:
                    if B > C:
                        B -= 1
                        C += 1
                        ANS.append('C')
                    else:
                        B += 1
                        C -= 1
                        ANS.append('B')
        else:
            for i in range(N):
                if A == B == 1 and X[i] == 'AB' and i < N-1:
                    if X[i+1] == 'AC':
                        ANS.append('A')
                        A += 1
                        B -= 1
                    else:
                        ANS.append('B')
                        A -= 1
                        B += 1
                elif A == C == 1 and X[i] == 'AC' and i < N-1:
                    if X[i+1] == 'AB':
                        ANS.append('A')
                        A += 1
                        C -= 1
                    else:
                        ANS.append('C')
                        A -= 1
                        C += 1
                elif B == C == 1 and X[i] == 'BC' and i < N-1:
                    if X[i+1] == 'AB':
                        ANS.append('B')
                        B += 1
                        C -= 1
                    else:
                        ANS.append('C')
                        B -= 1
                        C += 1
                else:
                    if X[i] == 'AB':
                        if A > B:
                            A -= 1
                            B += 1
                            ANS.append('B')
                        else:
                            A += 1
                            B -= 1
                            ANS.append('A')
                    elif X[i] == 'AC':
                        if A > C:
                            A -= 1
                            C += 1
                            ANS.append('C')
                        else:
                            A += 1
                            C -= 1
                            ANS.append('A')
                    else:
                        if B > C:
                            B -= 1
                            C += 1
                            ANS.append('C')
                        else:
                            B += 1
                            C -= 1
                            ANS.append('B')

        print(*ANS,sep='\n')

Solution (Python):
N,A,B,C = map(int,input().split())
IN = []
for i in range(N):
  temp = str(input())
  IN.append(temp)

ans = []
for i in range(N):
  if IN[i] == "AB":
    if A== 0 and B == 0:
      print("No")
      exit()
    elif A == 1 and B ==1:
      if i == N-1: 
        ans.append("A")
      else:
        if IN[i+1] == "AC": 
          A += 1; B -=1
          ans.append("A")
        elif IN[i+1] == "BC":
          A -=1; B += 1
          ans.append("B")
        else: 
          A += 1; B-=1
          ans.append("A")
    else:
      if A>=B:
        A-=1;B+=1
        ans.append("B")
      else:
        A+=1;B-=1
        ans.append("A")
  
  elif IN[i] == "AC":
    if A== 0 and C == 0:
      print("No")
      exit()
    elif A == 1 and C ==1:
      if i == N-1: 
        ans.append("A")
      else:
        if IN[i+1] == "AB": 
          A += 1; C -=1
          ans.append("A")
        elif IN[i+1] == "BC":
          A -=1; C += 1
          ans.append("C")
        else: 
          A += 1; C-=1
          ans.append("A")
    else:
      if A>=C:
        A-=1;C+=1
        ans.append("C")
      else:
        A+=1;C-=1
        ans.append("A")
        
  else: 
    if B == 0 and C == 0:
      print("No")
      exit()
    elif B == 1 and C ==1:
      if i == N-1: 
        ans.append("B")
      else:
        if IN[i+1] == "AB": 
          B += 1; C -=1
          ans.append("B")
        elif IN[i+1] == "AC":
          B -=1; C += 1
          ans.append("C")
        else: 
          B += 1; C-=1
          ans.append("B")
    else:
      if B>=C:
        B-=1;C+=1
        ans.append("C")
      else:
        B+=1;C-=1
        ans.append("B")
print("Yes")
print(*ans,sep="\n")

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

Solution (Python):
N, A, B, C = map(int,input().split())
ans = []
flag = True
cnt = 0
while True:
  cnt += 1
  if cnt > N:
    break
  s = input()
  if s == 'AB':
    if A == B == 0:
      flag = False
      break
    elif A == B == 1:
      if cnt == N:
        ans.append('A')
        break
      else:
        cnt += 1
        s = input()
        if s == 'AB':
          ans.append('A')
          ans.append('B')
          continue
        elif s == 'AC':
          ans.append('A')
          ans.append('C')
          B -= 1
          C += 1
          continue
        else:
          ans.append('B')
          ans.append('C')
          A -= 1
          C += 1
          continue
    elif A >= B:
      A -= 1
      B += 1
      ans.append('B')
      continue
    else:
      B -= 1
      A += 1
      ans.append('A')
      continue
  elif s == 'BC':
    if B == C == 0:
      flag = False
      break
    elif B == C == 1:
      if cnt == N:
        ans.append('B')
        break
      else:
        cnt += 1
        s = input()
        if s == 'BC':
          ans.append('B')
          ans.append('C')
          continue
        elif s == 'AB':
          ans.append('B')
          ans.append('A')
          C -= 1
          A += 1
          continue
        else:
          ans.append('C')
          ans.append('A')
          B -= 1
          A += 1
          continue
    elif B >= C:
      B -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      B += 1
      ans.append('B')
      continue 
  else:
    if C == A == 0:
      flag = False
      break
    elif C == A == 1:
      if cnt == N:
        ans.append('C')
        break
      else:
        cnt += 1
        s = input()
        if s == 'AC':
          ans.append('C')
          ans.append('A')
          continue
        elif s == 'BC':
          ans.append('C')
          ans.append('B')
          A -= 1
          B += 1
          continue
        else:
          ans.append('A')
          ans.append('B')
          C -= 1
          B += 1
          continue
    elif A >= C:
      A -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      A += 1
      ans.append('A')
      continue
if flag:
  print('Yes')
  for item in ans:
    print(item)
else:
  print('No')

Solution (Python):
N, A, B, C = map(int,input().split())
ans = []
flag = True
cnt = 0
while True:
  cnt += 1
  if cnt > N:
    break
  s = input()
  if s == 'AB':
    if A == B == 0:
      flag = False
      break
    elif A == B == 1 and C == 0:
      if cnt == N:
        ans.append('A')
        break
      else:
        cnt += 1
        s = input()
        if s == 'AB':
          ans.append('A')
          ans.append('B')
          continue
        elif s == 'AC':
          ans.append('A')
          ans.append('C')
          B -= 1
          C += 1
          continue
        else:
          ans.append('B')
          ans.append('C')
          A -= 1
          C += 1
          continue
    elif A >= B:
      A -= 1
      B += 1
      ans.append('B')
      continue
    else:
      B -= 1
      A += 1
      ans.append('A')
      continue
  elif s == 'BC':
    if B == C == 0:
      flag = False
      break
    elif B == C == 1 and A == 0:
      if cnt == N:
        ans.append('B')
        break
      else:
        cnt += 1
        s = input()
        if s == 'BC':
          ans.append('B')
          ans.append('C')
          continue
        elif s == 'AB':
          ans.append('B')
          ans.append('A')
          C -= 1
          A += 1
          continue
        else:
          ans.append('C')
          ans.append('A')
          B -= 1
          A += 1
          continue
    elif B >= C:
      B -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      B += 1
      ans.append('B')
      continue 
  else:
    if C == A == 0:
      flag = False
      break
    elif C == A == 1 and B == 0:
      if cnt == N:
        ans.append('C')
        break
      else:
        cnt += 1
        s = input()
        if s == 'AC':
          ans.append('C')
          ans.append('A')
          continue
        elif s == 'BC':
          ans.append('C')
          ans.append('B')
          A -= 1
          B += 1
          continue
        else:
          ans.append('A')
          ans.append('B')
          C -= 1
          B += 1
          continue
    elif A >= C:
      A -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      A += 1
      ans.append('A')
      continue
if flag:
  print('Yes')
  for item in ans:
    print(item)
else:
  print('No')

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    def calc(x, y):
        cnt[x] += 1
        cnt[y] -= 1
        res.append(S[x])

    n, *cnt = map(int, input().split())
    S = "ABC"
    query = []
    for _ in range(n):
        s = input().rstrip()
        a = 0 if s == "AB" else 0 if s == "AC" else 1
        b = 1 if s == "AB" else 2 if s == "AC" else 2
        query.append((a, b))
    query.append((None, None))

    res = []
    for i in range(n):
        a, b = query[i]
        if cnt[a] == cnt[b] == 0:
            print("No")
            return
        elif cnt[a] == 0:
            calc(a, b)
        elif cnt[b] == 0:
            calc(b, a)
        else:
            next_a, next_b = query[i + 1]
            if a == next_a or a == next_b:
                calc(a, b)
            else:
                calc(b, a)
    print("Yes")
    print(*res, sep="\n")


if __name__ == '__main__':
    resolve()

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

for i in range(N):
	if S[i] == "AB":
		op.append((0, 1))
	elif S[i] == "BC":
		op.append((1, 2))
	else:
		op.append((2, 0))

dec = []
for i in range(N):
	j1 = op[i][0]
	j2 = op[i][1]
	if n[j1] < n[j2]:
		dec.append(c[j1])
		n[j1] += 1
		n[j2] -= 1
	elif n[j2] < n[j1]:
		dec.append(c[j2])
		n[j2] += 1
		n[j1] -= 1
	elif i < N - 1:
		if j1 in op[i + 1]:
			if j1 == op[i + 1][0]:
				j3 = op[i + 1][1]
			else:
				j3 = op[i + 1][0]
			if n[j3] < n[j1]:
				dec.append(c[j1])
				n[j1] += 1
				n[j2] -= 1
			else:
				dec.append(c[j2])
				n[j2] += 1
				n[j1] -= 1
		else:
			if j2 == op[i + 1][0]:
				j3 = op[i + 1][1]
			else:
				j3 = op[i + 1][0]
			if n[j3] < n[j2]:
				dec.append(c[j2])
				n[j2] += 1
				n[j1] -= 1
			else:
				dec.append(c[j1])
				n[j1] += 1
				n[j2] -= 1
	else:
		dec.append(c[j1])
		n[j1] += 1
		n[j2] -= 1
	if n[0] < 0 or n[1] < 0 or n[2] < 0:
		print("No")
		exit()
print("Yes")
print("\n".join(dec))

Solution (Python):
def check(x,y,z,i,ops):
    if x == 1 and y == 1 and z == 0 and i < len(ops) - 1:        
        return (ops[i], ops[i+1]) in [("AB", "AC"), ("BC", "AB"), ("AC", "AB")]
    return (x <= y)

A=0
B=1
C=2
nabc = [int(_x) for _x in input().split()]
n = nabc[0]
abc = nabc[1:]
ops = []
for i in range(n):
    ops.append(input())

result = []
for i in range(n):
    op = ops[i]
    if op == "AB":
        (s,t,u) = (A,B,C)
    elif op == "BC":
        (s,t,u) = (B,C,A)
    else:
        (s,t,u) = (A,C,B)
    if check(abc[s],abc[t],abc[u],i,ops):
        result.append(s)
        abc[s] += 1
        abc[t] -= 1
    else:
        result.append(t)
        abc[s] -= 1
        abc[t] += 1
    if abc[s]<0 or abc[t]<0:
        print("No")
        break
else:
    print("Yes")
    for r in result:
        if r==A: print("A")
        if r==B: print("B")
        if r==C: print("C")

Solution (Python):





















def select(x, y, ops, i, z):
    
    if not(x == 1 and y == 1 and z == 0 and i < len(ops) - 1):
        if x <= y:
            return True
        else:
            return False

    
    
    
    
    op = ops[i]
    nop = ops[i+1]

    oo = (op, nop)
    if oo in [("AB", "AC"), ("BC", "AB"), ("AC", "AB")]:
        return True

    return False

def main():
    nabc = [int(_x) for _x in input().split()]
    n = nabc[0]
    a = nabc[1]
    b = nabc[2]
    c = nabc[3]

    ops = []
    for i in range(n):
        ops.append(input())

    result = []
    for i in range(n):
        op = ops[i]
        if op == "AB":
            if select(a, b, ops, i, c):
                result.append("A")
                a+=1
                b-=1
            else:
                result.append("B")
                a-=1
                b+=1
        if op == "AC":
            if select(a, c, ops, i, b):
                result.append("A")
                a+=1
                c-=1
            else:
                result.append("C")
                a-=1
                c+=1
        if op == "BC":
            if select(b, c, ops, i, a):
                result.append("B")
                b+=1
                c-=1
            else:
                result.append("C")
                b-=1
                c+=1
        if a <0 or b<0 or c<0:
            print("No")
            return

    print("Yes")
    for r in result:
        print(r)

main()

Solution (Python):



















def select(x, y, ops, i, z):
    
    if not(x == 1 and y == 1 and i < len(ops) - 1):
        if x < y:
            return True
        else:
            return False

    
    
    
    
    op = ops[i]
    nop = ops[i+1]

    oo = (op, nop)
    if op == nop:
        return True

    if oo in [("AB", "AC"), ("BC", "AB"), ("AC", "AB")]:
        return True

    return False

def main():
    nabc = [int(_x) for _x in input().split()]
    n = nabc[0]
    a = nabc[1]
    b = nabc[2]
    c = nabc[3]

    ops = []
    for i in range(n):
        ops.append(input())

    result = []
    for i in range(n):
        op = ops[i]
        if op == "AB":
            if select(a, b, ops, i, c):
                result.append("A")
                a+=1
                b-=1
            else:
                result.append("B")
                a-=1
                b+=1
        if op == "AC":
            if select(a, c, ops, i, b):
                result.append("A")
                a+=1
                c-=1
            else:
                result.append("C")
                a-=1
                c+=1
        if op == "BC":
            if select(b, c, ops, i, a):
                result.append("B")
                b+=1
                c-=1
            else:
                result.append("C")
                b-=1
                c+=1
        if a <0 or b<0 or c<0:
            print("No")
            return

    print("Yes")
    for r in result:
        print(r)

main()

Solution (Python):
def select(x, y, ops, i, z):
    
    if not(x == 1 and y == 1 and i < len(ops) - 1):
        if x < y:
            return True
        else:
            return False

    
    
    
    
    op = ops[i]
    nop = ops[i+1]

    oo = (op, nop)
    if op == nop:
        return True

    if oo in [("AB", "AC"), ("BC", "AB"), ("AC", "AB")]:
        return True

    return False

def main():
    nabc = [int(_x) for _x in input().split()]
    n = nabc[0]
    a = nabc[1]
    b = nabc[2]
    c = nabc[3]

    ops = []
    for i in range(n):
        ops.append(input())

    result = []
    for i in range(n):
        op = ops[i]
        if op == "AB":
            if select(a, b, ops, i, c):
                result.append("A")
                a+=1
                b-=1
            else:
                result.append("B")
                a-=1
                b+=1
        if op == "AC":
            if select(a, c, ops, i, b):
                result.append("A")
                a+=1
                c-=1
            else:
                result.append("C")
                a-=1
                c+=1
        if op == "BC":
            if select(b, c, ops, i, a):
                result.append("B")
                b+=1
                c-=1
            else:
                result.append("C")
                b-=1
                c+=1                
        if a <0 or b<0 or c<0:
            print("No")
            return

    print("Yes")
    for r in result:
        print(r)

main()

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7





N, A, B, C = getNM()
que = [input() for i in range(N)]
que.append(que[-1])
res = []
for i in range(N):
    q = que[i]

    if q == 'AB':
        if A > B:
            A -= 1
            B += 1
            res.append('B')
        elif A < B:
            A += 1
            B -= 1
            res.append('A')
        else:
            if que[i + 1] == 'AB':
                A -= 1
                B += 1
                res.append('B')
            elif que[i + 1] == 'AC':
                A += 1
                B -= 1
                res.append('A')
            elif que[i + 1] == 'BC':
                A -= 1
                B += 1
                res.append('B')
    elif q == 'AC':
        if A > C:
            A -= 1
            C += 1
            res.append('C')
        elif A < C:
            A += 1
            C -= 1
            res.append('A')
        else:
            if que[i + 1] == 'AC':
                A -= 1
                C += 1
                res.append('C')
            elif que[i + 1] == 'AB':
                A += 1
                C -= 1
                res.append('A')
            elif que[i + 1] == 'BC':
                A -= 1
                C += 1
                res.append('C')
    else:
        if B > C:
            B -= 1
            C += 1
            res.append('C')
        elif B < C:
            B += 1
            C -= 1
            res.append('B')
        else:
            if que[i + 1] == 'BC':
                B -= 1
                C += 1
                res.append('C')
            elif que[i + 1] == 'AB':
                B += 1
                C -= 1
                res.append('B')
            elif que[i + 1] == 'AC':
                B -= 1
                C += 1
                res.append('C')
    if A < 0 or B < 0 or C < 0:
        print('No')
        exit()
print('Yes')
for i in res:
    print(i)

Solution (Python):
def main():
    N, A, B, C = map(int, input().split())
    variables = {
        "A": A,
        "B": B,
        "C": C
    }
    ans = [None] * N
    S = [None] * N
    for i in range(N):
        S[i] = input()
    for i in range(N):
        c1, c2 = S[i]
        if variables[c1] == variables[c2] == 0:
            print("No")
            return
        elif variables[c1] == variables[c2] and i+1 < N:
            if c1 in S[i+1]:
                ans[i] = c1
                variables[c1] += 1
                variables[c2] -= 1
            else:
                ans[i] = c2
                variables[c1] -= 1
                variables[c2] += 1
        elif variables[c1] > variables[c2]:
            ans[i] = c2
            variables[c1] -= 1
            variables[c2] += 1
        else:
            ans[i] = c1
            variables[c1] += 1
            variables[c2] -= 1
    print("Yes")
    for i in range(N):
        print(ans[i])


if __name__ == "__main__":
    main()

Solution (Python):
n,a,b,c = map(int,input().split())
s = [input() for i in range(n)]
dic = {'A':a,'B':b,'C':c}

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

print(*ans,sep="\n")

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, a, b, c = map(int, readline().split())

    s = [input() for _ in range(n)]
    m = []

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

        print("Yes")
        for x in m:
            print(x)

if __name__ == '__main__':
    main()

Solution (Python):
N, A, B, C = map(int, input().split())
vars = {"A": A, "B": B, "C": C}
sum_ABC = A+B+C
S = [input() for _ in range(N)]
Ans = []
for s, s_next in zip(S, S[1:]):
    if vars[s[0]] > vars[s[1]] or (vars[s[0]]==vars[s[1]] and s[1] in s_next):
        Ans.append(s[1])
        vars[s[1]] += 1
        vars[s[0]] -= 1
    else:
        Ans.append(s[0])
        vars[s[0]] += 1
        vars[s[1]] -= 1
    if vars[s[0]] < 0 or vars[s[1]] < 0:
        print("No")
        exit()
s = S[-1]
if vars[s[0]] > vars[s[1]]:
    Ans.append(s[1])
    vars[s[1]] += 1
    vars[s[0]] -= 1
else:
    Ans.append(s[0])
    vars[s[0]] += 1
    vars[s[1]] -= 1
if vars[s[0]] < 0 or vars[s[1]] < 0:
    print("No")
    exit()
print("Yes")
print("\n".join(Ans))