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: Runtime Error

Solution code:
def main():
    import heapq
    n, a, b, c = map(int, input().split())
    s_list = [input() for _ in [0]*n]
    h = [i for i in range(n)]
    play = [None]*n
    heapq.heapify(h)
    if a+b+c <= 5:
        print(1/0)
        return
    while a+b+c > 0:
        if a == max(a, b, c):
            color = "A"
            a -= 1
        elif b == max(a, b, c):
            color = "B"
            b -= 1
        else:
            color = "C"
            c -= 1
        color2 = color
        l = []
        cnt = 0
        while h:
            i = heapq.heappop(h)
            j = s_list[i]
            if color in j:
                if color == j[0]:
                    color = j[1]
                else:
                    color = j[0]
                play[i] = color
                cnt += 1
            else:
                l.append(i)
        for i in l:
            heapq.heappush(h, i)
        if cnt == 0:
            if color2 == "A":
                a -= a
            elif color2 == "B":
                b -= b
            else:
                c -= c
        if not h:
            print("Yes")
            for i in play:
                print(i)
            return
    print("No")


main()

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
flag = True
ans = []
s = []
for i in range(n):
    s.append(input())
for i in range(n):
    if s[i] == "AB":
        if a == 0 and b == 0:
            flag = False
        if a > b:
            a -= 1
            b += 1
            ans.append("B")
        if b < a:
            a += 1
            b -= 1
            ans.append("A")
        if a == b:
            if s[i+1] == "AC":
                a += 1
                b -= 1
                ans.append("A")
            else:
                a -= 1
                b += 1
                ans.append("B")
    if s[i] == "BC":
        if b == 0 and c == 0:
            flag = False
        if b > c:
            b -= 1
            c += 1
            ans.append("C")
        if b < c:
            b += 1
            c -= 1
            ans.append("B")
        if b == c:
            if s[i+1] == "AB":
                b += 1
                c -= 1
                ans.append("B")
            else:
                c += 1
                b -= 1
                ans.append("B")
    if s[i] == "AC":
        if a == 0 and c == 0:
            flag = False
        if a > c:
            a -= 1
            c += 1
            ans.append("C")
        if c > a:
            a += 1
            c -= 1
            ans.append("A")
        if c == a:
            if s[i+1] == "AB"
                a += 1
                c -= 1
                ans.append("A")
            else:
                c += 1
                a -= 1
                ans.append("C")
if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Submission status: Runtime Error

Solution code:
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
ans=[]
X=A+B+C
if X==1:
    #TLE
    j=0
    for i in range(10**15):
        j+=i*i*i
if X==2:
    #RE
    P=[0,1,2]
    Y=P[10]
if X==1:
    for i in range(N):
        if S[i]=="AB":
            if A>0:
                A-=1
                B+=1
                ans.append("B")
            elif B>0:
                A+=1
                B-=1
                ans.append("A")
            else:
                print("No")
                exit()
        elif S[i]=="BC":
            if B>0:
                B-=1
                C+=1
                ans.append("C")
            elif C>0:
                B+=1
                C-=1
                ans.append("B")
            else:
                print("No")
                exit()
        else:
            if C>0:
                C-=1
                A+=1
                ans.append("A")
            elif A>0:
                C+=1
                A-=1
                ans.append("C")
            else:
                print("No")
                exit()
    print("Yes")
    for i in range(N):
        print(ans[i])
    exit()
if X==2:
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                exit()
            elif A==1 and B==1:
                if i<N-1 and S[i+1]=="AC":
                    ans.append("A")
                    A+=1
                    B-=1
                else:
                    ans.append("B")
                    B+=1
                    A-=1
            elif A==0:
                ans.append("A")
                A+=1
                B-=1
            else:
                ans.append("B")
                B+=1
                A-=1
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                exit()
            elif B==1 and C==1:
                if i<N-1 and S[i+1]=="AB":
                    ans.append("B")
                    B+=1
                    C-=1
                else:
                    ans.append("C")
                    C+=1
                    B-=1
            elif B==0:
                ans.append("B")
                B+=1
                C-=1
            else:
                ans.append("C")
                C+=1
                B-=1
        else:
            if C==0 and A==0:
                print("No")
                exit()
            elif C==1 and A==1:
                if i<N-1 and S[i+1]=="BC":
                    ans.append("C")
                    C+=1
                    A-=1
                else:
                    ans.append("A")
                    A+=1
                    C-=1
            elif C==0:
                ans.append("C")
                C+=1
                A-=1
            else:
                ans.append("A")
                A+=1
                C-=1
    print("Yes")
    for i in range(N):
        print(ans[i])
    exit()
for i in range(N):
    if S[i]=="AB":
        if A==0 and B==0:
            print("No")
            exit()
        elif A+1==X:
            ans.append("B")
            A-=1
            B+=1
        elif B+1==X:
            ans.append("A")
            A+=1
            B-=1
        elif A==0:
            ans.append("A")
            A+=1
            B-=1
        elif B==0:
            ans.append("B")
            A-=1
            B+=1
        else:
            ans.append("A")
            A+=1
            B-=1
    elif S[i]=="BC":
        if B==0 and C==0:
            print("No")
            exit()
        elif B+1==X:
            ans.append("C")
            B-=1
            C+=1
        elif C+1==X:
            ans.append("B")
            B+=1
            C-=1
        elif B==0:
            ans.append("B")
            B+=1
            C-=1
        elif C==0:
            ans.append("C")
            B-=1
            C+=1
        else:
            ans.append("B")
            B+=1
            C-=1
    else:
        if C==0 and A==0:
            print("No")
            exit()
        elif C+1==X:
            ans.append("A")
            C-=1
            A+=1
        elif A+1==X:
            ans.append("C")
            C+=1
            A-=1
        elif C==0:
            ans.append("C")
            C+=1
            A-=1
        elif A==0:
            ans.append("A")
            C-=1
            A+=1
        else:
            ans.append("C")
            C+=1
            A-=1
print("Yes")
for i in range(N):
    print(ans[i])

Submission status: Runtime Error

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

for i in range(0, N):
    cl = input()
    list.append(cl)

ex_list = []
yes = True
for n in range(0, N):
    if list[n] == 'AB':
        if A > B:
            A -= 1
            B += 1
            ex_list.append('B')
        elif A < B:
            A += 1
            B -= 1
            ex_list.append('A')
        elif A == B:
            if n < N - 1:
                if list[n+1] == 'BC':
                    A -= 1
                    B += 1
                    ex_list.append('B')
                elif list[n+1] == 'AC':
                    A += 1
                    B -= 1
                    ex_list.append('A')
            else:
                A -= 1
                B += 1
                ex_list.append('B')

    elif list[n] == 'AC':
        if A > C:
            A -= 1
            C += 1
            ex_list.append('C')
        elif A < C:
            A += 1
            C -= 1
            ex_list.append('A')
        elif A == C:
            if n < N:
                if list[n + 1] == 'AB':
                    A += 1
                    C -= 1
                    ex_list.append('A')
                elif list[n + 1] == 'BC':
                    A -= 1
                    C += 1
                    ex_list.append('C')
            else:
                A += 1
                C -= 1
                ex_list.append('A')


    elif list[n] == 'BC':
        if B > C:
            B -= 1
            C += 1
            ex_list.append('C')
        elif B < C:
            B += 1
            C -= 1
            ex_list.append('B')
        elif B == C:
            if n < N:
                if list[n + 1] == 'AB':
                    B += 1
                    C -= 1
                    ex_list.append('B')
                elif list[n + 1] == 'AC':
                    B -= 1
                    C += 1
                    ex_list.append('C')
            else:
                B -= 1
                C += 1
                ex_list.append('C')

    if A < 0 or B < 0 or C < 0:
        yes = False

    if not yes:
        break

if yes:
    print('Yes')
    for element in ex_list:
        print(element)
else:
    print('No')

Submission status: Runtime Error

Solution code:
import sys
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
score = {"A":a, "B":b, "C":c}
count = (a==0) + (b==0)+ (c==0)
if count < 2:
    result = []
    for g in s:
        x, y = list(g)
        if score[x] <= score[y]:
            score[y] -= 1
            score[x] += 1
            result.append(x)
        else:
            score[x] -= 1
            score[y] += 1
            result.append(y)
    print("Yes")
    for i in result:
        print(i)
else:
    result = []
    for i in range(n-1):
        x, y = list(s[i])
        if x in g[i+1]:
            score[x] += 1
            score[y] -= 1
            result.append(x)
        else:
            score[y] += 1
            score[x] -= 1
            result.append(y)
        if score[y] < 0 or score[x] < 0:
            print("No")
            sys.exit()
    x, y = list(s[n-1])
    if score[x] < score[y]:
        score[x] += 1
        score[y] -= 1
        result.append(x)
    else:
        score[y] += 1
        score[x] -= 1
        result.append(y)
    if score[y] < 0 or score[x] < 0:
        print("No")
        sys.exit()
    print("Yes")
    for i in result:
        print(i)

Submission status: Runtime Error

Solution code:
def examA():
    S = SI()
    if S=="ARC":
        ans = "ABC"
    else:
        ans = "ARC"
    print(ans)
    return

def examB():
    N, K = LI()
    snuke = set(range(1,N+1))
    for _ in range(K):
        d = I()
        A = LI()
        for a in A:
            snuke.discard(a)
    ans = len(snuke)
    print(ans)
    return

def examC():
    N, M = LI()
    H = LI()
    good = set(range(1,N+1))
    for _ in range(M):
        a, b = LI()
        if H[a-1]>=H[b-1]:
            good.discard(b)
        if H[b-1]>=H[a-1]:
            good.discard(a)
    ans = len(good)
    print(ans)
    return

def examD():
    X = I()
    #print(1000**5-999**5)
    for a in range(-1000,1000):
        for b in range(-1000,1000):
            if a**5-b**5==X:
                print(a,b)
                return
    return

def examE():
    N = I()
    A = LI()
    B = [0]*N
    D = defaultdict(int)
    for i in range(N):
        B[i] = A[i]-i
        c = A[i]+i
        D[c] += 1
    #print(B)
    #print(D)
    ans = 0
    for b in B:
        ans += D[-b]
    print(ans)
    return

def examF():
    def Run_Length_Encoding(s):
        tmp, count, res = s[0], 1, []
        for i in range(1, len(s)):
            if tmp == s[i]:
                count += 1
            else:
                res.append((count, tmp))
                tmp = s[i]
                count = 1
        res.append((count, tmp))
        return res

    N, A, B, C = LI()
    S = [SI()for _ in range(N)]
    SS = Run_Length_Encoding(S)
    #print(SS)
    n = len(SS)
    ans = [""]*(N)
    def judge(a,b):
        if a<0 or b<0:
            return False
        return True
    def dfs(i,num,A,B,C):
        if i==n:
            if A>=0 and B>=0 and C>=0:
                print("Yes")
                for v in ans:
                    print(v)
                exit()
            return
        cnt, s = SS[i]
        if s=="AB":
            if A==0 and B==0:
                return
            for j in range(cnt//2):
                if A==0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "B"
                else:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "A"
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            A += 1; B -= 1
            if judge(A,B):
                ans[num] = "A"
                dfs(i+1,num+1,A,B,C)
            A -= 2; B += 2
            if judge(A,B):
                ans[num] = "B"
                dfs(i+1,num+1,A,B,C)
            A += 1; B -= 2
        elif s=="BC":
            if B==0 and C==0:
                return
            for j in range(cnt//2):
                num += 1
                if B==0:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "B"
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            B += 1; C -= 1
            if judge(B,C):
                ans[num] = "B"
                dfs(i+1,num+1,A,B,C)
            B -= 2; C += 2
            if judge(B,C):
                ans[num] = "C"
                dfs(i+1,num+1,A,B,C)
            B += 1; C -= 2
        else:
            if A==0 and C==0:
                return
            for j in range(cnt//2):
                num += 1
                if A==0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "A"
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            A += 1; C -= 1
            if judge(A, C):
                ans[num] = "A"
                dfs(i + 1,num+1,A,B,C)
            A -= 2; C += 2
            if judge(A, C):
                ans[num] = "C"
                dfs(i + 1,num+1,A,B,C)
            A += 1; C -= 2
        return
    dfs(0,0,A,B,C)
    print("No")
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examF()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Runtime Error

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

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

ans = []
# ngMap = {}
ngMap = set()

def solve(idx, x, y, z, cur):
    text = ''.join(cur)

    if text in ngMap:
        return

    if x < 0 or y < 0 or z < 0:
        ngMap.add(text)
        return
    if idx == n:
        if x >= 0 and y >= 0 and z >= 0:
            print('Yes')
            for i in range(n):
                print(cur[i])
            exit()
        #     ans.append(cur)
        # return

    if s[idx] == 'AB':
        solve(idx + 1, x + 1, y - 1, z, cur + ['A'])
        solve(idx + 1, x - 1, y + 1, z, cur + ['B'])
    elif s[idx] == 'AC':
        solve(idx + 1, x + 1, y, z - 1, cur + ['A'])
        solve(idx + 1, x - 1, y, z + 1, cur + ['C'])
    elif s[idx] == 'BC':
        solve(idx + 1, x, y + 1, z - 1, cur + ['B'])
        solve(idx + 1, x, y - 1, z + 1, cur + ['C'])

solve(0, a, b, c, [])

# print(ans)
print('No')

Submission status: Runtime Error

Solution code:
import sys
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
score = {"A":a, "B":b, "C":c}
count = (a==0) + (b==0)+ (c==0)
if count < 2:
    result = []
    for g in s:
        x, y = list(g)
        if score[x] <= score[y]:
            score[y] -= 1
            score[x] += 1
            result.append(x)
        else:
            score[x] -= 1
            score[y] += 1
            result.append(y)
    print("Yes")
    for i in result:
        print(i)
else:
    result = []
    for i in range(n-1):
        s, y = list(g[i])
        if s in g[i+1]:
            score[s] += 1
            score[y] -= 1
            result.append(s)
        else:
            score[y] += 1
            score[s] -= 1
            result.append(y)
        if score[y] < 0 or score[s] < 0:
            print("No")
            sys.exit()
    s, y = list(g[n])
    if score[s] < score[y]:
        score[s] += 1
        score[y] -= 1
        result.append(s)
    else:
        score[y] += 1
        score[s] -= 1
        result.append(y)
    if score[y] < 0 or score[s] < 0:
        print("No")
        sys.exit()
    print("Yes")
    for i in result:
        print(i)

Submission status: Runtime Error

Solution code:

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

ns = [s[0]]
for v in s[1:]:
  if v!=ns[-1]:
    ns.append([v,1])
  else:
    ns[-1][1] += 1

ans = "No"
q = [[] for i in range(N+1)]
q[0] = [(A,B,C,[])]
for i in range(N):
  while q[i]:
    a,b,c,trace = q[i].pop(0)
    if s[i] == "AB":
      if a-1>0:
        q[i+1].append((a-1,b+1,c,trace+["B"]))
      if b-1>0:
        q[i+1].append((a+1,b-1,c,trace+["A"]))
    if s[i] == "BC":
      if b-1>0:
        q[i+1].append((a,b-1,c+1,trace+["C"]))
      if c-1>0:
        q[i+1].append((a,b+1,c-1,trace+["B"]))
    if s[i] == "AC":
      if a-1>0:
        q[i+1].append((a-1,b,c+1,trace+["C"]))
      if c-1>0:
        q[i+1].append((a+1,b,c-1,trace+["A"]))
if q[N]:
  a,b,c,trace = q[N].pop(0)
  ans = "Yes"


print(ans)
if ans =="Yes":
  for v in trace:
    print(v)

Submission status: Runtime Error

Solution code:
import sys

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

abc = {"A":a,"B":b,"C":c}

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

ans = [None]*n
i = 0
while i <n:
    x = s[i][0]
    y = s[i][1]
    if abc[x] == 0 and abc[y] ==0:
        print("No")
        sys.exit()
    elif abc[x] == 1 and abc[y] ==1 and i<n-1:
        if s[i] == s[i+1]:
            ans[i] = x
            ans[i+1] = y
            i += 1
        elif x in s[i+1]:
            ans[i] = x
            abc[x] += 1
            abc[y] -= 1
        else:
            abc[i] = y
            abc[x] -= 1
            abc[y] += 1
    else:
        if abc[x]<=abc[y]:
            ans[i] = x
            abc[x] += 1
            abc[y] -= 1
        else:
            ans[i] = y
            abc[x] -= 1
            abc[y] += 1
    i += 1
    
print("Yes")
print("\n".join(ans))

Submission status: Runtime Error

Solution code:
def play(i, a, b, c, ans):
  if i==n:
    if a>=0 and b>=0 and c>=0:
      return 1,ans
    else:
      return 0,ans
    
  if X[i] == 'AB':
    if play(i+1, a+1, b-1, c,ans)[0] == 1:
      ans.append('A')
      return 1,ans
    elif play(i+1, a-1, b+1, c, ans)[0] ==1:
      ans.append('B')
      return 1,ans
    else:
      return 0 ,ans
    
  elif X[i] == 'AC':
    if play(i+1, a+1, b, c-1,ans)[0] == 1:
      ans.append('A')
      return 1,ans
    elif play(i+1, a-1, b, c+1, ans)[0] ==1:
      ans.append('C')
      return 1,ans
    else:
      return 0 ,ans
  else:
    if play(i+1, a, b+1, c-1,ans)[0] == 1:
      ans.append('B')
      return 1,ans
    elif play(i+1, a, b-1, c+1, ans)[0] ==1:
      ans.append('C')
      return 1,ans
    else:
      return 0 ,ans

if __name__=="__main__":
  n,a,b,c = map(int,input().split())
  X = []
  ans = []
  for i in range(n):
    X.append(input())
  result = play(0,a,b,c,ans)
  if result[0] ==1:
    print('Yes')
    for i in range(len(result[1])):
      print(result[1][len(result[1])-1-i])
  else:
    print('NO')

Submission status: Runtime Error

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    S = [input() for _ in range(N)]
    res = []
    if A + B + C == 2:
        for i in range(N):
            s = S[i]
            if s == "AB":
                if A == 0 and B == 0:
                    print("No")
                    return 1
                if A > B:
                    A -= 1
                    B += 1
                    res.append("B")
                elif A < B:
                    A += 1
                    B -= 1
                    res.append("A")
                else:
                    if S == N - 1:
                        res.append("A")
                        break
                    elif S[i + 1] == "BC":
                        A -= 1
                        B += 1
                        res.append("B")
                    else:
                        A += 1
                        B -= 1
                        res.append("A")
            if s == "AC":
                if A == 0 and C == 0:
                    print("No")
                    return 1
                if A > C:
                    A -= 1
                    C += 1
                    res.append("C")
                elif A < C:
                    A += 1
                    C -= 1
                    res.append("A")
                else:
                    if S == N - 1:
                        res.append("A")
                        break
                    elif S[i + 1] == "BC":
                        A -= 1
                        C += 1
                        res.append("C")
                    else:
                        A += 1
                        C -= 1
                        res.append("A")
            if s == "BC":
                if B == 0 and C == 0:
                    print("No")
                    return 1
                if B > C:
                    B -= 1
                    C += 1
                    res.append("C")
                elif B < C:
                    B += 1
                    C -= 1
                    res.append("B")
                else:
                    if S == N - 1:
                        res.append("B")
                        break
                    elif S[i + 1] == "AC":
                        B -= 1
                        C += 1
                        res.append("C")
                    else:
                        B += 1
                        C -= 1
                        res.append("B")
        print("Yes")
        print("\n".join(map(str, res)))
        return 0
    if A == 0 and B == 0:
        if S[0] == "AB":
            print("No")
            return 1
    if A == 0 and C == 0:
        if S[0] == "AC":
            print("No")
            return 1
    if C == 0 and B == 0:
        if S[0] == "BC":
            print("No")
            return 1

    for s in S:
        if s == "AB":
            if A == 0 and B == 0:
                print("No")
                return 1
            if A >= B:
                A -= 1
                B += 1
                res.append("B")
            else:
                A += 1
                B -= 1
                res.append("A")
        elif s == "AC":
            if A == 0 and C == 0:
                print("No")
                return 1
            if A >= C:
                A -= 1
                C += 1
                res.append("C")
            else:
                A += 1
                C -= 1
                res.append("A")

        elif s == "BC":
            if B == 0 and C == 0:
                print("No")
                return 1
            if B >= C:
                B -= 1
                C += 1
                res.append("C")
            else:
                B += 1
                C -= 1
                res.append("B")
        else:
            print("ERROR")
    print("Yes")
    print("\n".join(map(str, res)))
    return 0


main()

Submission status: Runtime Error

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

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

ans = []
tower = [a,b,c]
s2num = {"AB":[0,1],"BC":[1,2],"AC":[0,2]}
num2s = {0:"A",1:"B",2:"C"}
flag =True

def check_large(l,r,t):
    if t[l]>t[r]:
        return l,r,False
    elif t[l]<t[r]:
        return r,l,False
    else:
        return l,r,True

for i in range(n):
    s = ss[i]
    l,r = s2num[s]
    big,small,f = check_large(l,r,tower)
    if f:
        if i+1 < n:
            nex = s2num[ss[i+1]]
            if big in nex:
                small,big = big,small
            else:
                continue

    if tower[big] <= 0:
        flag = False
        break
    else:
        tower[big]-=1
        tower[small]+=1
        ans.append(num2s[small])

if flag:
    print("Yes")
    for i in range(n):
        print(ans[i])
else:
    print("No")

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
S = [input() for _ in range(N)]
a = min(A,10**5)
b = min(B,10**5)
c = min(C,10**5)

res = ['0']*N

# 明らかに可能
if a+b+c >= 3:
    print('Yes')
    for i in range(N):
        if S[i]=='AC':
            if a<c:
                res[i] = 'A'
                a += 1
                c -= 1
            else:
                res[i] = 'C'
                c += 1
                a -= 1
        elif S[i]=='AB':
            if a<b:
                res[i] = 'A'
                a += 1
                b -= 1
            else:
                res[i] = 'B'
                b += 1
                a -= 1
        else:
            if b<c:
                res[i] = 'B'
                b += 1
                c -= 1
            else:
                res[i] = 'C'
                c += 1
                b -= 1
    for val in res:
        print(val)
elif a+b+c==0:
    print('No')
elif a+b+c==1:
    now = 'a'
    if b==1:
        now = 'b'
    elif c==1:
        now = 'c'
    for i in range(N):
        if S[i] == 'AC':
            if now=='b':
                print('No')
                exit()
            elif now=='a':
                res[i] = 'C'
                now = 'c'
            else:
                res[i] = 'A'
                now = 'a'
        if S[i] == 'BC':
            if now=='a':
                print('No')
                exit()
            elif now=='b':
                res[i] = 'C'
                now = 'c'
            else:
                res[i] = 'B'
                now = 'b'
        if S[i] == 'AB':
            if now=='c':
                print('No')
                exit()
            elif now=='a':
                res[i] = 'B'
                now = 'b'
            else:
                res[i] = 'A'
                now = 'a'
    for val in res:
        print(val)
else:
    assert()

Submission status: Runtime Error

Solution code:
n, A, B, C = map(int, input().split())
s = [list(input()) for _ in range(n)]

cur = dict()
cur['A'], cur['B'], cur['C'] = A, B, C

counts = dict()
counts['AB'], counts['AC'], counts['BC'] = 0, 0, 0
for ss in s:
    counts[''.join(ss)] += 1

f = True
ans = []
for i, ss in enumerate(s):
    if cur[ss[0]] == 1 and cur[ss[1]] == 1 and sorted(list(cur.values())) == [0, 1, 1] and i < len(s):
        if ss[0] in s[i + 1]:
            cur[ss[0]] += 1
            cur[ss[1]] -= 1
            ans.append(ss[0])
        elif ss[1] in s[i + 1]:
            cur[ss[1]] += 1
            cur[ss[0]] -= 1
            ans.append(ss[1])

    else:
        if cur[ss[0]] >= cur[ss[1]]:
            cur[ss[0]] -= 1
            cur[ss[1]] += 1
            ans.append(ss[1])
        else:
            cur[ss[0]] += 1
            cur[ss[1]] -= 1
            ans.append(ss[0])

    if -1 in list(cur.values()):
        f = False
        break

if f:
    print('Yes')
    for a in ans:
        print(a)
else:
    print('No')