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:
"""
import copy

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


s = s[::-1]
ans = []

tmp = [[0,0,0]]

for s1 in s:
    for i in range(len(tmp)):
        t = tmp.pop(0)
        if ans:
            ans1 = ans.pop(0)
        else:
            ans1 = ''
        if s1 == 'AB':
            if t[0] == 0 and t[1] == 0:
                tmp.append([t[0]+1,t[1],t[2]])
                tmp.append([t[0],t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'A')
            elif t[0] == 0:
                tmp.append([t[0]+1,t[1]-1,t[2]])
                ans.append(ans1+'B')
            elif t[1] == 0:
                tmp.append([t[0]-1,t[1]+1,t[2]])
                ans.append(ans1+'A')
            else:
                tmp.append([t[0]+1,t[1]-1,t[2]])
                tmp.append([t[0]-1,t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'A')
        elif s1 == 'BC':
            if t[1] == 0 and t[2] == 0:
                tmp.append([t[0],t[1],t[2]+1])
                tmp.append([t[0],t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'C')
            elif t[1] == 0:
                tmp.append([t[0],t[1]+1,t[2]-1])
                ans.append(ans1+'C')
            elif t[2] == 0:
                tmp.append([t[0],t[1]-1,t[2]+1])
                ans.append(ans1+'B')
            else:
                tmp.append([t[0],t[1]-1,t[2]+1])
                tmp.append([t[0],t[1]+1,t[2]-1])
                ans.append(ans1+'B')
                ans.append(ans1+'C')
        else:
            if t[2] == 0 and t[0] == 0:
                tmp.append([t[0]+1,t[1],t[2]])
                tmp.append([t[0],t[1],t[2]+1])
                ans.append(ans1+'C')
                ans.append(ans1+'A')

            elif t[2] == 0:
                tmp.append([t[0]-1,t[1],t[2]+1])
                ans.append(ans1+'A')

            elif t[0] == 0:
                tmp.append([t[0]+1,t[1],t[2]-1])
                ans.append(ans1+'C')

            else:
                tmp.append([t[0]+1,t[1],t[2]-1])
                tmp.append([t[0]-1,t[1],t[2]+1])
                ans.append(ans1+'C')
                ans.append(ans1+'A')

    li = [True]*len(tmp)
    for i in range(len(tmp)):
        for j in range(i+1, len(tmp)):
            for k in range(3):
                if tmp[i][k] <= tmp[j][k]:
                    li[j] = False
    tmp2 = []
    ans2 = []
    for i in range(len(tmp)):
        if li[i]:
            tmp2.append(tmp[i])
            ans2.append(ans[i])
    tmp = copy.copy(tmp2)
    ans = copy.copy(ans2)

for i in range(len(tmp)):
    if a >= tmp[i][0] and b >= tmp[i][1] and c >= tmp[i][2]:
        print('Yes')
        for j in range(n):
            print(ans[i][-j-1])
        exit()

print('No')
"""

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

        if s1 == 'AB':
            if a >= 1 and b == 0:
                a = 0
                b = 1
                ans += 'B'
            elif a == 0 and b >= 1:
                a = 1
                b = 0
                ans += 'A'
            elif a >= 1 and b >= 1:
                if s2 == 'BC':
                    b += 1
                    a -= 1
                    ans += 'B'
                else:
                    a += 1
                    b -= 1
                    ans += 'A'
            else:
                print('No')
                exit()
        elif s1 == 'BC':
            if b >= 1 and c == 0:
                b = 0
                c = 1
                ans += 'C'
            elif b == 0 and c >= 1:
                b = 1
                c = 0
                ans += 'B'
            elif b >= 1 and c >= 1:
                if s2 == 'AC':
                    c += 1
                    b -= 1
                    ans += 'C'
                else:
                    b += 1
                    c -= 1
                    ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a >= 1 and c == 0:
                a = 0
                c = 1
                ans += 'C'
            elif a == 0 and c >= 1:
                a = 1
                c = 0
                ans += 'A'
            elif a >= 1 and c >= 1:
                if s2 == 'AB':
                    a += 1
                    c -= 1
                    ans += 'A'
                else:
                    a -= 1
                    c += 1
                    ans += 'C'
            else:
                print('No')
                exit()
        s1 = s2
    print('Yes')
    for i in range(n):
        print(ans[i])

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**5)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N:return True
    if Q[f] == 'AB':
        if a>0 and dfs(f+1,a-1,b+1,c):
            ans.append('A')
            return True
        if b>0 and dfs(f+1,a+1,b-1,c):
            ans.append('B')
            return True
        
        return False
    if Q[f] == 'BC':
        if b>0 and dfs(f+1,a,b-1,c+1):
            ans.append('B')
            return True
        if c>0 and dfs(f+1,a,b+1,c-1):
            ans.append('C')
            return True
        
        return False
    if Q[f] == 'AC':
        if a>0 and dfs(f+1,a-1,b,c+1):
            ans.append('A')
            return True
        if c>0 and dfs(f+1,a+1,b,c-1):
            ans.append('C')
            return True
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)
# print(ans)
if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**5)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N-1:return True
    if Q[f] == 'AB':
        if a>0:
            dfs(f+1,a-1,b+1,c)
            ans.append('A')
        if b>0:
            dfs(f+1,a+1,b-1,c)
            ans.append('B')
        
        return False
    if Q[f] == 'BC':
        if b>0:
            dfs(f+1,a,b-1,c+1)
            ans.append('B')
        if c>0:
            dfs(f+1,a,b+1,c-1)
            ans.append('C')
        
        return False
    if Q[f] == 'AC':
        if a>0:
            dfs(f+1,a-1,b,c+1)
            ans.append('A')
        if c>0:
            dfs(f+1,a+1,b,c-1)
            ans.append('C')
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)

if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
clear=1
ans=""
memo=""
for _ in range(n):
    s=input()
    if s=="AB":
        if (a==0) and (b==0):
            if memo.strip(ans[-1])=="A":
                a=0
                b=1
                c-=1
                ans=ans[:-1]+"AB"
            else:
                a=1
                b=0
                c-=1
                ans=ans[:-1]+"BA"
        elif a>b:
            a-=1
            b+=1
            ans+="B"
        else:
            a+=1
            b-=1
            ans+="A"
    elif s=="BC":
        if (b==0) and (b==0):
            if memo.strip(ans[-1])=="B":
                b=0
                c=1
                a-=1
                ans=ans[:-1]+"BC"
            else:
                b=1
                c=0
                a-=1
                ans=ans[:-1]+"CB"
        elif b>c:
            b-=1
            c+=1
            ans+="C"
        else:
            b+=1
            c-=1
            ans+="B"
    else:
        if (a==0) and (c==0):
            if memo.strip(ans[-1])=="A":
                a=0
                c=1
                b-=1
                ans=ans[:-1]+"AC"
            else:
                a=1
                c=0
                b-=1
                ans=ans[:-1]+"CA"
        elif c>a:
            c-=1
            a+=1
            ans+="A"
        else:
            a-=1
            c+=1
            ans+="C"
    if (a<0) or (b<0) or (c<0):
        clear=0
    memo=s
if clear==1:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Submission status: Runtime Error

Solution code:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""

from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def floor(a, b):
    """floor(a/b) 整数値を返す"""
    c = (a - a % b)/b
    return int(c)


def main():
    n, a, b, c = wi()
    s = []
    for i in range(n):
        s.append(input())
    s.append('na')
    ans = []
    if a + b + c == 0:
        print('No')
        sys.exit()
    if a + b + c == 1:
        for i in range(n):
            if s[i] == 'AB':
                if a == 0 and b == 1:
                    a += 1
                    b -= 1
                    ans.append('A')
                elif a == 1 and b == 0:
                    a -= 1
                    b += 1
                    ans.append('B')
                elif a == 0 and b == 0:
                    print('No')
                    sys.exit()

            elif s[i] == 'BC':
                if b == 0 and c == 1:
                    b += 1
                    c -= 1
                    ans.append('B')
                elif b == 1 and c == 0:
                    b -= 1
                    c += 1
                    ans.append('C')
                elif b == 0 and c == 0:
                    print('No')
                    sys.exit()
            elif s[i] == 'AC':
                if a == 0 and c == 1:
                    a += 1
                    c -= 1
                    ans.append('A')
                elif a == 1 and c == 0:
                    a -= 1
                    c += 1
                    ans.append('C')
                elif a == 0 and c == 0:
                    print('No')
                    sys.exit()

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

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

            elif s[i] == 'AC':
                if a == 1 and c == 1:
                    if s[i+1] == 'BC':
                        a -= 1
                        c += 1
                        ans.append('C')
                    elif s[i+1] == 'AC':
                        c += 1
                        a -= 1
                        ans.append('C')
                    elif s[i+1] == 'AB':
                        a += 1
                        c -= 1
                        ans.append('A')
                    elif i == n-1:
                        b += 1
                        c -= 1
                        ans.append('B')
                elif a == 2 and c == 0:
                    a -= 1
                    c += 1
                    ans.append('C')
                elif a == 0 and c == 2:
                    a += 1
                    c -= 1
                    ans.append('A')

    else:
        for i in range(n):
            if s[i] == 'AB':
                if a == 0 and b >= 1:
                    a += 1
                    b -= 1
                    ans.append('A')
                elif b == 0 and a >= 1:
                    a -= 1
                    b += 1
                    ans.append('B')
                else:
                    a += 1
                    b -= 1
                    ans.append('A')
            elif s[i] == 'BC':
                if b == 0 and c >= 1:
                    b += 1
                    c -= 1
                    ans.append('B')
                elif c == 0 and b >= 1:
                    b -= 1
                    c += 1
                    ans.append('C')
                else:
                    b += 1
                    c -= 1
                    ans.append('B')
            elif s[i] == 'AC':
                if a == 0 and c >= 1:
                    a += 1
                    c -= 1
                    ans.append('A')
                elif c == 0 and a >= 1:
                    a -= 1
                    c += 1
                    ans.append('C')
                elif i == n-1:
                    a += 1
                    c -= 1
                    ans.append('A')
    print('Yes')
    for i in range(n):
        print(ans[i])



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline

n,a,b,c=map(int,input().split())
s_=input().split()
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if i!=n-1:
            if s_[i+1]=="AC" and c==0 and a<=1:
                a,b,ans=(a+1,b-1,ans+"A")
            elif s_[i+1]=="BC" and c==0 and b<=1:
                a,b,ans=(a-1,b+1,ans+"B")
            else:
                a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        else:
            a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if i!=n-1:
            if s_[i+1]=="AB" and b==0 and a<=1:
                a,c,ans=(a+1,c-1,ans+"A")
            elif s_[i+1]=="BC" and b==0 and c<=1:
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        else:
            a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if i!=n-1:
            if s_[i+1]=="AB" and a==0 and b<=1:
                b,c,ans=(b+1,c-1,ans+"B")
            elif s_[i+1]=="AC" and a==0 and c<=1:
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        else:
            b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())
ans=[]
slist=[]
for i in range(n):
    s=input()
    slist.append(s)
for i in range(n):
    s=slist[]
    if s=="AB":
        if a==0:
            a+=1
            b-=1
            ans.append("A")
        elif b==0:
            a-=1
            b+=1
            ans.append("B")
        elif slist[(i+1)%n]=="AB":
            if a>b:
                a-=1
                b+=1
                ans.append("B")
            else:
                a+=1
                b-=1
                ans.append("A")
        elif slist[(i+1)%n]=="AC":
            a+=1
            b-=1
            ans.append("A")
        else:
            a-=1
            b+=1
            ans.append("B")
    elif s=="AC":
        if a==0:
            a+=1
            c-=1
            ans.append("A")
        elif c==0:
            a-=1
            c+=1
            ans.append("C")
        elif slist[(i+1)%n]=="AC":
            if a>c:
                a-=1
                c+=1
                ans.append("C")
            else:
                a+=1
                c-=1
                ans.append("A")
        elif slist[(i+1)%n]=="AB":
            a+=1
            c-=1
            ans.append("A")
        else:
            a-=1
            c+=1
            ans.append("C")
    else:
        if b==0:
            b+=1
            c-=1
            ans.append("B")
        elif c==0:
            b-=1
            c+=1
            ans.append("C")
        elif slist[(i+1)%n]=="BC":
            if b>c:
                b-=1
                c+=1
                ans.append("C")
            else:
                b+=1
                c-=1
                ans.append("B")
        elif slist[(i+1)%n]=="AC":
            c+=1
            b-=1
            ans.append("C")
        else:
            c-=1
            b+=1
            ans.append("B")
    if a<0 or b<0 or c<0:
        ans.clear()
        break
if len(ans)==0:
    print("No")
else:
    print("Yes")
    for i in range(n):
        print(ans[i])

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf

import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect


class Player():

    A = 0
    B = 1
    C = 2

    def __init__(self, data, op_list):
        self.data = data
        self.op_list = op_list
        self.move_history = ["X"] * len(self.op_list)
        self.key = 0
        self.ans = "Yes" # "Yes" or "No"
        self.flg_out = False
        self.flg_foresight = False
        s = sum(data)
        if s == 2:
            self.flg_foresight = True
        elif s == 0 or s == 1:
            self.ans = "No"
            self.flg_out = True

    def has_next_op(self):
        if self.key < len(self.op_list):
            return True
        else:
            return False

    def play(self):
        for key, op in enumerate(op_list):
            if self.flg_out:
                break
            self.key = key
            self.read_op(op)

    def read_op(self, op):
        if op == "AB":
            self.do(self.A, self.B)
        if op == "AC":
            self.do(self.A, self.C)
        if op == "BC":
            self.do(self.B, self.C)

    def do(self, x, y):
        if self.data[x] < self.data[y]:
            self.move(y, x)
        elif self.data[x] > self.data[y]:
            self.move(x, y)
        else: # same
            if self.flg_foresight:
                if self.is_next_used(x):
                    self.move(y, x)
                else:
                    self.move(x, y)
            else:
                self.move(x, y) # which is ok

    def move(self, frm, to):
        self.move_history[self.key] = self.num_to_char(to)
        self.data[frm] -= 1
        self.data[to] += 1
        if self.data[frm] < 0:
            self.ans = "No"
            self.flg_out = True

    @staticmethod
    def num_to_char(z):
        chars = ['A', 'B', 'C']
        return chars[z]

    def is_next_used(self, z):
        if not self.has_next_op():
            return True # which is ok
        next_op = self.op_list[self.key + 1]
        if self.num_to_char(z) in next_op:
            return True
        else:
            return False

    def print_ans(self):
        print(self.ans)
        if self.ans == "No":
            return
        for h in self.move_history:
            print(h)




if __name__ == '__main__':
    n, a, b, c = list(map(int, input().split()))
    data = [a, b, c]
    #print('data') # debug
    #print(data) # debug
    op_list = [""] * n
    for i in range(n):
        op_list[i] = input()
    #print('op_list') # debug
    #print(op_list) # debug

    p = Player(data, op_list)
    p.play()
    p.print_ans()


    #print('\33[32m' + 'end' + '\033[0m') # debug

Submission status: Runtime Error

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


def solve(x):
    d = {"A": 0, "B": 1, "C": 2}
    ans = []
    for i in range(N):
        u, v = X[i]
        if x[d[u]] == x[d[v]] == 0:
            return

        if x[d[u]] < x[d[v]]:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)
        elif x[d[u]] > x[d[v]]:
            x[d[u]] -= 1
            x[d[v]] += 1
            ans.append(v)
        else:
            # x[d[u]] == x[d[v]] >= 1
            if i < N - 1 and X[i] != X[i + 1]:
                x, y = X[i + 1]
                if u == x or u == y:
                    x[d[u]] += 1
                    x[d[v]] -= 1
                    ans.append(u)
                else:
                    x[d[u]] -= 1
                    x[d[v]] += 1
                    ans.append(v)
            else:
                x[d[u]] += 1
                x[d[v]] -= 1
                ans.append(u)

    return ans

ans = solve([A, B, C])
if ans:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10**7)
from pprint import pprint as pp
from pprint import pformat as pf

import math
#from sortedcontainers import SortedList, SortedDict, SortedSet # no in atcoder
import bisect


class Player():

    A = 0
    B = 1
    C = 2

    def __init__(self, data, op_list):
        self.data = data
        self.op_list = op_list
        self.move_history = ["X"] * len(self.op_list)
        self.key = 0
        self.ans = "Yes" # "Yes" or "No"
        self.flg_out = False
        self.flg_foresight = False
        s = sum(data)
        if s == 2:
            self.flg_foresight = True
        elif s == 0 or s == 1:
            self.ans = "No"
            self.flg_out = True

    def has_next_op(self):
        if self.key < len(self.op_list):
            return True
        else:
            return False

    def play(self):
        for key, op in enumerate(op_list):
            if self.flg_out:
                break
            self.key = key
            self.read_op(op)

    def read_op(self, op):
        if op == "AB":
            self.do(self.A, self.B)
        if op == "AC":
            self.do(self.A, self.C)
        if op == "BC":
            self.do(self.B, self.C)

    def do(self, x, y):
        if self.data[x] < self.data[y]:
            self.move(y, x)
        elif self.data[x] > self.data[y]:
            self.move(x, y)
        else: # same
            if self.flg_foresight:
                if self.is_next_used(x):
                    self.move(y, x)
                else:
                    self.move(x, y)
            else:
                self.move(x, y) # which is ok

    def move(self, frm, to):
        self.move_history[self.key] = self.num_to_char(to)
        self.data[frm] -= 1
        self.data[to] += 1
        if self.data[frm] < 0:
            self.ans = "No"
            self.flg_out = True

    @staticmethod
    def num_to_char(z):
        chars = ['A', 'B', 'C']
        return chars[z]

    def is_next_used(self, z):
        if not self.is_next_op():
            return True # which is ok
        next_op = self.op_list[self.key + 1]
        if self.num_to_char(z) in next_op:
            return True
        else:
            return False

    def print_ans(self):
        print(self.ans)
        if self.ans == "No":
            return
        for h in self.move_history:
            print(h)




if __name__ == '__main__':
    n, a, b, c = list(map(int, input().split()))
    data = [a, b, c]
    #print('data') # debug
    #print(data) # debug
    op_list = [""] * n
    for i in range(n):
        op_list[i] = input()
    #print('op_list') # debug
    #print(op_list) # debug

    p = Player(data, op_list)
    p.play()
    p.print_ans()


    #print('\33[32m' + 'end' + '\033[0m') # debug

Submission status: Runtime Error

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


def solve(x):
    d = {"A": 0, "B": 1, "C": 2}
    ans = []
    for i in range(N):
        u, v = X[i]
        if x[d[u]] == x[d[v]] == 0:
            return

        if x[d[u]] < x[d[v]]:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)
        elif x[d[u]] > x[d[v]]:
            x[d[u]] -= 1
            x[d[v]] += 1
            ans.append(v)
        else:
            # x[d[u]] == x[d[v]] >= 1
            if i < N - 1 and X[i] != X[i + 1]:
                x, y = X[i + 1]
                if u == x:
                    x[d[u]] += 1
                    x[d[v]] -= 1
                    ans.append(u)
                else:
                    x[d[u]] -= 1
                    x[d[v]] += 1
                    ans.append(v)
            else:
                x[d[u]] += 1
                x[d[v]] -= 1
                ans.append(u)

    return ans

ans = solve([A, B, C])
if ans:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")

Submission status: Runtime Error

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


def solve(x):
    d = {"A": 0, "B": 1, "C": 2}
    ans = []
    for i in range(N):
        u, v = X[i]
        if x[d[u]] == x[d[v]] == 0:
            return

        if x[d[u]] < x[d[v]]:
            x[d[u]] += 1
            x[d[v]] -= 1
            ans.append(u)
        elif x[d[u]] > x[d[v]]:
            x[d[u]] -= 1
            x[d[v]] += 1
            ans.append(v)
        else:
            # x[d[u]] == x[d[v]] >= 1
            if i < N - 1 and X[i] != X[i + 1]:
                x, y = X[i + 1]
                if u == x:
                    x[d[u]] += 1
                    x[d[v]] += 1
                    ans.append(u)
                else:
                    x[d[u]] -= 1
                    x[d[v]] += 1
                    ans.append(v)
            else:
                x[d[u]] += 1
                x[d[v]] += 1
                ans.append(u)

    return ans

ans = solve([A, B, C])
if ans:
    print("Yes")
    print(*ans, sep="\n")
else:
    print("No")