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



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Accepted

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

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

        variable_up, variable_down = None, None
        # 変数の両方が 0。ゲーム続行不可能。
        if count[s0] == count[s1] == 0:
            return 'No'
        # 変数の片方が 0。そちらを増やすほかない。
        elif count[s0] == 0:
            variable_up, variable_down = s0, s1
        elif count[s1] == 0:
            variable_up, variable_down = s1, s0
        # 不適切な方法を取るとゲーム続行が不可能なので、さらに次の操作を先読みする。
        # (ただし、現時点が最後の操作なら、好きにしていい)
        elif turn + 1 == N:
            variable_up, variable_down = s0, s1
        # 次の操作に関わっている変数を増やす
        elif s0 in Selections[turn + 1]:
            variable_up, variable_down = s0, s1
        else:
            variable_up, variable_down = s1, s0

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

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

print(f_three_variables_game())

Submission status: Accepted

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


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

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


main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
ans=[]
ks = [input() for _ in range(n)]
for i in range(n):
  k = ks[0]
  if k=="AB":
    if a==0 and b==0:
      print("No")
      quit()
    else:
      if a>b:
        a -= 1
        b+=1
        ans.append("B")
      else:
        if a == 1 and b == 1 and len(ks) > 1:
          if ks[1] == "AC":
            b-=1
            a+=1
            ans.append("A")
          else:
            a -= 1
            b+=1
            ans.append("B")
        else: 
          b-=1
          a+=1
          ans.append("A")
  if k=="BC":
    if c==0 and b==0:
      print("No")
      quit()
    else:
      if c>b:
        c -= 1
        b+=1
        ans.append("B")
      else:
        if c == 1 and b == 1 and len(ks) > 1:
          if ks[1] == "AC":
            b-=1
            c+=1
            ans.append("C")
          else:
            c -= 1
            b+=1
            ans.append("B")
        else: 
          b-=1
          c+=1
          ans.append("C")
  if k=="AC":
    if a==0 and c==0:
      print("No")
      quit()
    else:
      if a>c:
        a -= 1
        c+=1
        ans.append("C")
      else:
        if a == 1 and c == 1 and len(ks) > 1:
          if ks[1] == "BC":
            a-=1
            c+=1
            ans.append("C")
          else:
            c -= 1
            a+=1
            ans.append("A")
        else: 
          c-=1
          a+=1
          ans.append("A")
  del(ks[0])
print("Yes")
for i in ans:
  print(i)

Submission status: Accepted

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

path = []
inputs = [input() for _ in range(N)]

for i in range(N):
	target = inputs[i]
	if target == 'AB':
		if A > B:
			A -= 1
			B += 1
			path.append('B')
		elif A == 1 and B == 1:
			if i != N - 1 and inputs[i + 1] == 'AC':
				A += 1
				B -= 1
				path.append('A')
			else:
				A -= 1
				B += 1
				path.append('B')
		else:
			A += 1
			B -= 1
			path.append('A')
	if target == 'AC':
		if A > C:
			A -= 1
			C += 1
			path.append('C')
		elif A == 1 and C == 1:
			if i != N - 1 and inputs[i + 1] == 'AB':
				A += 1
				C -= 1
				path.append('A')
			else:
				A -= 1
				C += 1
				path.append('C')
		else:
			A += 1
			C -= 1
			path.append('A')
	if target == 'BC':
		if B > C:
			B -= 1
			C += 1
			path.append('C')
		elif B == 1 and C == 1:
			if i != N - 1 and inputs[i + 1] == 'AB':
				B += 1
				C -= 1
				path.append('B')
			else:
				B -= 1
				C += 1
				path.append('C')
		else:
			B += 1
			C -= 1
			path.append('B')
	if A < 0 or B < 0 or C < 0:
		print('No')
		exit(0)

print('Yes')
for i in path:
	print(i)

Submission status: Accepted

Solution code:
#!/usr/bin/python3

import sys
from collections import defaultdict


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


def psum(pair):
    if pair == 'AB':
        return a + b
    elif pair == 'AC':
        return a + c
    elif pair == 'BC':
        return b + c

def select(pair, npair):
    global a, b, c
    added = None
    if pair == 'AB':
        if a < b:
            a += 1
            b -= 1
            added = 'A'
        elif a > b:
            a -= 1
            b += 1
            added = 'B'            
        else:
            # a == b
            if a + b == 2 and c == 0 and npair:
                if npair == 'AB':
                    a += 1
                    b -= 1
                    added = 'A'
                elif npair == 'AC':
                    a += 1
                    b -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    b += 1
                    added = 'B'                    
            else:
                a += 1
                b -= 1
                added = 'A'
            
    elif pair == 'AC':
        if a < c:
            a += 1
            c -= 1
            added = 'A'
        elif a > c:
            a -= 1
            c += 1
            added = 'C'            
        else:
            # a == c
            if a + c == 2 and b == 0 and npair:
                if npair == 'AB':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'AC':
                    a += 1
                    c -= 1
                    added = 'A'                    
                elif npair == 'BC':
                    a -= 1
                    c += 1
                    added = 'C'
            else:
                a += 1
                c -= 1
                added = 'A'                
        
    elif pair == 'BC':
        if b < c:
            b += 1
            c -= 1
            added = 'B'            
        elif b > c:
            b -= 1
            c += 1
            added = 'C'            
        else:
            # b == c
            if b + c == 2 and a == 0 and npair:
                if npair == 'AB':
                    b += 1
                    c -= 1
                    added = 'B'                    
                elif npair == 'AC':
                    b -= 1
                    c += 1
                    added = 'C'                    
                elif npair == 'BC':
                    b += 1
                    c -= 1
                    added = 'B'
            else:
                b += 1
                c -= 1
                added = 'B'
    return added

    
ans = []
if a + b + c == 0:
    print('No')
    exit()
else:
    for i, s in enumerate(ss):
        if psum(s) == 0:
            print('No')
            exit()
        else:
            ns = None
            if i < len(ss) - 1:
                ns = ss[i + 1]
            ans.append(select(s, ns))

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

Submission status: Accepted

Solution code:
import sys
n,a,b,c=map(int,input().split())
slist=[]
for i in range(n):
    slist.append(input())
output=[]
ynflag=True

def abc1(s):
    global a
    global b
    global c
    global output
    global ynflag

    
    if s == "AB":
        if a ==1:
            a -=1
            b +=1
            output.append("B")
            return True
        elif b == 1:
            a +=1
            b -=1
            output.append("A")
            return True
        else:
            return False
    if s == "AC":
        if a ==1:
            a -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            a +=1
            c -=1
            output.append("A")
            return True
        else:
            return False
    if s == "BC":
        if b ==1:
            b -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            b +=1
            c -=1
            output.append("B")
            return True
        else:
            return False

def abc2(i):
    global a
    global b
    global c
    global output
    global slist
    s=slist[i]
    
    if i == len(slist)-1:
        tsugi="DUMMY"
    else:
        tsugi=slist[i+1]
    
    if s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if tsugi == "BC":
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
        else:
            if a >=b:
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
    elif s == "AC":
        if a + c == 0:
            return False
        elif a == 1 and c == 1 and b == 0:
            if tsugi == "BC":
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
        else:
            if a >=c:
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
    else:
        if b + c == 0:
            return False
        elif b == 1 and c == 1 and a == 0:
            if tsugi == "AC":
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True
        else:
            if b >=c:
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True

if a == 0 and b == 0 and c == 0:
    print("No")
    sys.exit()

else:
    if a+b+c == 1:
        for s in slist:
            ynflag=abc1(s)
            if not ynflag:
                print("No")
                sys.exit()
    else:
        for i in range(n):
            ynflag=abc2(i)
            if not ynflag:
                print("No")
                sys.exit()
    print("Yes")
    for out in output:
        print(out)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    N, A, B, C = map(int, readline().split())
    s = read().decode().split()
    s.append('AB')
    ans = []
    for i in range(N):
        if s[i] == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            else:
                if A < B:
                    A += 1
                    B -= 1
                    ans.append('A')
                elif A > B:
                    A -= 1
                    B += 1
                    ans.append('B')
                else:
                    if s[i+1] == 'AB' or s[i+1] == 'AC':
                        A += 1
                        B -= 1
                        ans.append('A')
                    else:
                        A -= 1
                        B += 1
                        ans.append('B')
        if s[i] == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            else:
                if A < C:
                    A += 1
                    C -= 1
                    ans.append('A')
                elif A > C:
                    A -= 1
                    C += 1
                    ans.append('C')
                else:
                    if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                        A += 1
                        C -= 1
                        ans.append('A')
                    else:
                        A -= 1
                        C += 1
                        ans.append('C')
        if s[i] == 'BC':
            if C == 0 and B == 0:
                print('No')
                break
            else:
                if C < B:
                    C += 1
                    B -= 1
                    ans.append('C')
                elif C > B:
                    C -= 1
                    B += 1
                    ans.append('B')
                else:
                    if s[i + 1] == 'AB' or s[i + 1] == 'BC':
                        B += 1
                        C -= 1
                        ans.append('B')
                    else:
                        B -= 1
                        C += 1
                        ans.append('C')
    else:
        print('Yes')
        print('\n'.join(ans))
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
sysread = sys.stdin.readline
sys.setrecursionlimit(10**7)
def run():
    pri = []
    N, A, B, C = map(int, sysread().split())
    vals = {'A':A, 'B':B, 'C':C}
    S = [sysread() for _ in range(N)]

    pri = []
    for idx, s in enumerate(S):
        if vals[s[0]] == vals[s[1]] and vals[s[0]] == 1 and idx !=len(S)-1:
            if s[0] in S[idx+1]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])
        else:
            if vals[s[0]] <= vals[s[1]]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])

        if not check_vals(vals):
            print('No')
            return None
    print('Yes')
    for st in pri:
        print(st)

def check_vals(vals):
    for v in vals.values():
        if v < 0:
            return False
    return True

if __name__ == "__main__":
    run()

Submission status: Accepted

Solution code:
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
idleA=10**20
idleB=10**10
idleC=1
stack=[[0,A,B,C]]
lst={}
dp=[defaultdict(str) for i in range(N+1)]
def main(a,b,c):
    return(a*idleA+b*idleB+c)

while stack:
    #print(stack)
    n,a,b,c=stack.pop()
    if n==N:
        stack.append([n,a,b,c])
        break
    s=S[n]
    if s=="AB":
        if a>0:
            Q=main(a-1,b+1,c)
            if not dp[n+1][Q]:
                dp[n+1][Q]="B"
                stack.append([n+1,a-1,b+1,c])
        if b>0:
            Q=main(a+1,b-1,c)
            if not dp[n+1][Q]:
                dp[n+1][Q]="A"
                stack.append([n+1,a+1,b-1,c])
    elif s=="BC":
        if b>0:
            Q=main(a,b-1,c+1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="C"
                stack.append([n+1,a,b-1,c+1])
        if c>0:
            Q=main(a,b+1,c-1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="B"
                stack.append([n+1,a,b+1,c-1])
    elif s=="AC":
        if a>0:
            Q=main(a-1,b,c+1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="C"
                stack.append([n+1,a-1,b,c+1])
        if c>0:
            Q=main(a+1,b,c-1)
            if not dp[n+1][Q]:
                dp[n+1][Q]="A"
                stack.append([n+1,a+1,b,c-1])

if stack:
    print("Yes")
    lst=[]
    n,a,b,c=stack.pop()

    lst.append([dp[n][main(a,b,c)],a,b,c])
    #print(lst)
    for n in range(N-1)[::-1]:
        s=S[n+1]
        X,a,b,c=lst[-1]
        if s=="AB":
            if X=="A":
                Q=main(a-1,b+1,c)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a-1,b+1,c])
                    continue
            if X=="B":
                Q=main(a+1,b-1,c)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a+1,b-1,c])
                    continue
        elif s=="BC":
            if X=="B":
                Q=main(a,b-1,c+1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a,b-1,c+1])
                    continue
            if X=="C":
                Q=main(a,b+1,c-1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a,b+1,c-1])
                    continue
        elif s=="AC":
            if X=="A":
                Q=main(a-1,b,c+1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a-1,b,c+1])
                    continue
            if X=="C":
                Q=main(a+1,b,c-1)
                if dp[n+1][Q]:
                    lst.append([dp[n+1][Q],a+1,b,c-1])
                    continue
        #print("a")
    for X,a,b,c in list(reversed(lst)):
        print(X)
else:
    print("No")