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:
#!/usr/bin/env python3

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    

    def parse(com):
        if com == 'AB':
            return 0, 1
        elif com == 'BC':
            return 1, 2
        else:
            return 0, 2

    def sim_1(t_copy, commands):
        s = 'ABC'
        result = []
        for com in commands:
            i, j = parse(com)
            if t_copy[i] == 0 and t_copy[j] == 0:
                return False, result
            elif t_copy[i] == 0:
                result.append(s[i])
                t_copy[i], t_copy[j] = 1, 0
            else:
                result.append(s[j])
                t_copy[i], t_copy[j] = 0, 1
        return True, result
    

    def sim_2(t_copy, commands):
        s = 'ABC'
        size = len(commands)
        result = []
        for k in range(size):
            com = commands[k]
            i, j = parse(com)
            if t_copy[i] == 0 and t_copy[j] == 0:
                raise RuntimeError
            elif t_copy[i] == 0:
                result.append(s[i])
                t_copy[i] += 1
                t_copy[j] -= 1
            elif t_copy[j] == 0:
                result.append(s[j])
                t_copy[j] += 1
                t_copy[i] -= 1
            else:
                # 片方はどうしても 0 になってしまう
                if t_copy[i] == 1 and t_copy[j] == 1:
                    if k != size - 1:
                        next_com = commands[k + 1]
                        next_i, next_j = parse(next_com)
                        if not (i == next_i and j == next_j):
                            next_duplicate_one = list(set([i,j]) | set([next_i, next_j]))[0]
                            tmp = [next_i, next_j]
                            tmp.remove(next_duplicate_one)
                            next_not_duplicate = tmp[0]
                            if t_copy[next_duplicate_one] == 1 and t_copy[next_not_duplicate] == 0:
                                result.append(s[next_duplicate_one])
                                t_copy[next_duplicate_one] += 1
                                tmp = [i, j]
                                tmp.remove(next_duplicate_one)
                                res = tmp[0]
                                t_copy[res] -= 1
                    # 自由？
                    result.append(s[i])
                    t_copy[i] += 1
                    t_copy[j] -= 1
                # 0 にならないですむ                 
                elif t_copy[i] <= t_copy[j]:
                    result.append(s[i])
                    t_copy[i] += 1
                    t_copy[j] -= 1
                else:
                    result.append(s[j])
                    t_copy[j] += 1
                    t_copy[i] -= 1
        return result
            
    
    n, a, b, c  = mi()
    t = [a, b, c]
    commands = [input() for _ in range(n)]
    i, j = parse(commands[0])
    if t.count(0) == 3:
        predicate = False
    elif t.count(0) == 2 and sum(t) == 1:
        predicate, result = sim_1(t[:], commands)
    elif t.count(0) == 2 and t[i] == 0 and t[j] == 0:
        predicate = False
    else:
        predicate = True
        result = sim_2(t[:], commands)
    
    if predicate:
        print('Yes')
        print(*result, sep='\n')
    else:
        print('No')


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().split())
d = {'A':a, 'B':b, 'C':c}
ans = []
def move(inc, dec):
    d[inc]+=1
    d[dec]-=1
    ans.append(inc)
q = []
for i in range(n):
    q.append(input())
for i in range(n):
    x = q[i][0]
    y = q[i][1]
    if d[x] == 0 and d[y] == 0:
        print("No")
        exit()
    if d[x] == 1 and d[y] == 1:
        if i == n-1:
            move(x, y)
        if x in q[i+1]:
            move(x, y)
        else:
            move(y, x)
    else:
        if d[x] >= d[y]:
            move(y, x)
        else:
            move(x, y)
print("Yes")
print(*ans, sep = '\n')

Submission status: Runtime Error

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A,B,C=MI()
    S=[]
    SS=A+B+C
    for i in range(N):
        s=input()
        S.append(s)
    
    S.append("AB")
    S.append("AB")#適当に入れとけ
    
    ans=[]
    flag=1
    
    assert SS!=2
    
    if SS==0:
        flag=-1
    elif SS==2:
        for i in range(N):
            s=S[i]
            if s=="AB":
                if A<B:
                    A+=1
                    B-=1
                    ans.append("A")
                elif B>A:
                    A-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="AC":
                        A+=1
                        B-=1
                        ans.append("A")
                    else:
                        A-=1
                        B+=1
                        ans.append("B")
                        
                    
            elif s=="AC":
                if A<C:
                    A+=1
                    C-=1
                    ans.append("A")
                elif A>C:
                    A-=1
                    C+=1
                    ans.append("C")
                else:#同数
                    s2=S[i+1]
                    if s2=="AB":
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
                    
            else:#BC
                if C<B:
                    C+=1
                    B-=1
                    ans.append("C")
                elif C>B:
                    C-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="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:
                flag=-1
                break
        
    else:
        for i in range(N):
            s=S[i]
            if s=="AB":
                if A<B:
                    A+=1
                    B-=1
                    ans.append("A")
                else:
                    A-=1
                    B+=1
                    ans.append("B")
                
                        
                    
            elif s=="AC":
                if A<C:
                    A+=1
                    C-=1
                    ans.append("A")
                else:
                    A-=1
                    C+=1
                    ans.append("C")
                
                    
            else:#BC
                if C<B:
                    C+=1
                    B-=1
                    ans.append("C")
                else:
                    C-=1
                    B+=1
                    ans.append("B")
                      
            if A<0 or B<0 or C<0:
                flag=-1
                break
                
        
        
    if flag==-1:
        print("No")
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    

    def parse(com):
        if com == 'AB':
            return 0, 1
        elif com == 'BC':
            return 1, 2
        else:
            return 0, 2

    def sim_1(t_copy, commands):
        s = 'ABC'
        result = []
        for com in commands:
            i, j = parse(com)
            if t_copy[i] == 0 and t_copy[j] == 0:
                return False, result
            elif t_copy[i] == 0:
                result.append(s[i])
                t_copy[i], t_copy[j] = 1, 0
            else:
                result.append(s[j])
                t_copy[i], t_copy[j] = 0, 1
        return True, result
    

    def sim_2(t_copy, commands):
        s = 'ABC'
        size = len(commands)
        result = []
        for k in range(size):
            com = commands[k]
            i, j = parse(com)
            if t_copy[i] == 0 and t_copy[j] == 0:
                raise RuntimeError
            elif t_copy[i] == 0:
                result.append(s[i])
                t_copy[i] += 1
                t_copy[j] -= 1
            elif t_copy[j] == 0:
                result.append(s[j])
                t_copy[j] += 1
                t_copy[i] -= 1
            else:
                # 片方はどうしても 0 になってしまう
                if t_copy[i] == 1 and t_copy[j] == 1:
                    if k != size - 1:
                        next_com = commands[k + 1]
                        next_i, next_j = parse(next_com)
                        if i == next_i and j != next_j and t_copy[next_i] == 1 and t_copy[next_j] == 0:
                            result.append(s[i])
                            t_copy[i] += 1
                            t_copy[j] -= 1
                            continue
                        elif i != next_i and j == next_j and t_copy[next_i] == 0 and t_copy[next_j] == 1:
                            result.append(s[j])
                            t_copy[j] += 1
                            t_copy[i] -= 1
                            continue
                    # 自由？
                    result.append(s[i])
                    t_copy[i] += 1
                    t_copy[j] -= 1
                # 0 にならないですむ                 
                elif t_copy[i] <= t_copy[j]:
                    result.append(s[i])
                    t_copy[i] += 1
                    t_copy[j] -= 1
                else:
                    result.append(s[j])
                    t_copy[j] += 1
                    t_copy[i] -= 1
        return result
            
    
    n, a, b, c  = mi()
    t = [a, b, c]
    commands = [input() for _ in range(n)]
    i, j = parse(commands[0])
    if t.count(0) == 3:
        predicate = False
    elif t.count(0) == 2 and sum(t) == 1:
        predicate, result = sim_1(t[:], commands)
    elif t.count(0) == 2 and t[i] == 0 and t[j] == 0:
        predicate = False
    else:
        predicate = True
        result = sim_2(t[:], commands)
    
    if predicate:
        print('Yes')
        print(*result, sep='\n')
    else:
        print('No')


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N,A,B,C=map(int,input().split())
S=[input() for _ in range(N)]
ans=''
for i in range(N):
    if S[i]=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        elif A==1 and B==0:
            A-=1
            B+=1
            ans+='B'
        elif A==0 and B==1:
            A+=1
            B-=1
            ans+='A'
        elif A==1 and B==1 and i<N-1:
            if A in S[i+1]:
                A+=1
                B-=1
                ans+='A'
            else:
                A-=1
                B+=1
                ans+='B'
        elif A>B:
            A-=1
            B+=1
            ans+='B'
        else:
            A+=1
            B-=1
            ans+='A'
    elif S[i]=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        elif A==1 and C==0:
            A-=1
            C+=1
            ans+='C'
        elif A==0 and C==1:
            A+=1
            C-=1
            ans+='A'
        elif A==1 and C==1 and i<N-1:
            if A in S[i+1]:
                A+=1
                C-=1
                ans+='A'
            else:
                A-=1
                C+=1
                ans+='C'
        elif A>C:
            A-=1
            C+=1
            ans+='C'
        else:
            A+=1
            C-=1
            ans+='A'
    else:
        if B==0 and C==0:
            print('No')
            exit()
        elif B==1 and C==0:
            B-=1
            C+=1
            ans+='C'
        elif B==0 and C==1:
            B+=1
            C-=1
            ans+='B'
        elif B==1 and C==1 and i<N-1:
            if B in S[i+1]:
                B+=1
                C-=1
                ans+='B'
            else:
                B-=1
                C+=1
                ans+='C'
        elif B>C:
            B-=1
            C+=1
            ans+='C'
        else:
            B+=1
            C-=1
            ans+='B'
print('Yes')
for c in ans:
    print(c)

Submission status: Runtime Error

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

O = [input() for _ in range(n)]
def finish():
  print('No')
  exit()
ans = []
for i in range(n):
  o = O[i]
  on = O[i+1]
  if o =='AB':
    if a==b==0:
      finish()
    elif a==0:
      ans.append('A')
      a += 1
      b -= 1
    elif b==0:
      ans.append('B')
      a-= 1
      b+= 1
    elif a == 1 and c==0 and on == 'AC':
      ans.append('A')
      a += 1
      b -= 1
    elif b==1 and c==0 and on =='BC':
      ans.append('B')
      a-= 1
      b+= 1
    elif a>b:
      ans.append('B')
      a-=1
      b+=1
    else:
      ans.append('A')
      a+=1
      b-=1
  elif o =='AC':
    if a==c==0:
      finish()
    elif a==0:
      ans.append('A')
      a += 1
      c -= 1
    elif c==0:
      ans.append('C')
      a-= 1
      c+= 1
    elif a == 1 and b==0 and on == 'AB':
      ans.append('A')
      a += 1
      c -= 1
    elif b==1 and c==0 and on =='BC':
      ans.append('C')
      a-= 1
      c+= 1
    elif a>c:
      ans.append('C')
      a-=1
      c+=1
    else:
      ans.append('A')
      a+=1
      c-=1
  if o =='BC':
    if c==b==0:
      finish()
    elif c==0:
      ans.append('C')
      c += 1
      b -= 1
    elif b==0:
      ans.append('B')
      c-= 1
      b+= 1
    elif c == 1 and a==0 and on == 'AC':
      ans.append('C')
      c += 1
      b -= 1
    elif b==1 and a==0 and on =='AB':
      ans.append('B')
      c-= 1
      b+= 1
    elif c>b:
      ans.append('B')
      c-=1
      b+=1
    else:
      ans.append('C')
      c+=1
      b-=1
for a in ans:
  print(a)

Submission status: Runtime Error

Solution code:
def transfer(a,b,B):
    a -= 1
    b += 1
    ans.append(B)
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
        elif A == 1 and B == 1:
            if s[i+1] == "BC":
                transfer(A,B,"B")
            else:
                transfer(B,A,"A")
        elif A >= B:
            transfer(A,B,"B")
        else:
            transfer(B,A,"A")
    if s[i] == "AC":
        if A == 0 and C == 0:
            flag = False
        elif A == 1 and C == 1:
            if s[i+1] == "BC":
                transfer(A,C,"C")
            else:
                transfer(C,A,"A")
        elif A >= C:
            transfer(A,C,"C")
        else:
            transfer(C,A,"A")
    if s[i] == "BC":
        if B == 0 and C == 0:
            flag = False
        elif B == 1 and C == 1:
            if s[i+1] == "AC":
                transfer(B,C,"C")
            else:
                transfer(C,B,"B")
        elif B >= C:
            transfer(B,C,"C")
        else:
            transfer(C,B,"B")
if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Submission status: Runtime Error

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A,B,C=MI()
    S=[]
    SS=A+B+C
    for i in range(N):
        s=input()
        S.append(s)
    
    S.append("AB")
    S.append("AB")#適当に入れとけ
    
    ans=[]
    flag=1
    
    assert SS==2

    
    if SS==0:
        flag=-1
    elif SS==2:
        for i in range(N):
            s=S[i]
            if s=="AB":
                if A<B:
                    A+=1
                    B-=1
                    ans.append("A")
                elif B>A:
                    A-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="AC":
                        A+=1
                        B-=1
                        ans.append("A")
                    else:
                        A-=1
                        B+=1
                        ans.append("B")
                        
                    
            elif s=="AC":
                if A<C:
                    A+=1
                    C-=1
                    ans.append("A")
                elif A>C:
                    A-=1
                    C+=1
                    ans.append("C")
                else:#同数
                    s2=S[i+1]
                    if s2=="AB":
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
                    
            else:#BC
                if C<B:
                    C+=1
                    B-=1
                    ans.append("C")
                elif C>B:
                    C-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="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:
                flag=-1
                break
        
    else:
        for i in range(N):
            s=S[i]
            if s=="AB":
                if A<B:
                    A+=1
                    B-=1
                    ans.append("A")
                else:
                    A-=1
                    B+=1
                    ans.append("B")
                
                        
                    
            elif s=="AC":
                if A<C:
                    A+=1
                    C-=1
                    ans.append("A")
                else:
                    A-=1
                    C+=1
                    ans.append("C")
                
                    
            else:#BC
                if C<B:
                    C+=1
                    B-=1
                    ans.append("C")
                else:
                    C-=1
                    B+=1
                    ans.append("B")
                
                
   
        
                
            if A<0 or B<0 or C<0:
                flag=-1
                break
                
        
        
    if flag==-1:
        print("No")
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()

Submission status: Runtime Error

Solution code:
from itertools import combinations

N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]

cnt = {'A': A, 'B': B, 'C': C}

def out(T):
    now = {'A': 0, 'B': 0, 'C': 0}
    for s in T:
        now[s] += 1

    ans = []
    for i, s in enumerate(S):
        if min(now.values()) < 0:
            return False
        if now[s[0]] > now[s[1]]:
            ans.append(s[1])
            now[s[0]] -= 1
            now[s[1]] += 1
            continue
        if now[s[0]] < now[s[1]]:
            ans.append(s[0])
            now[s[0]] += 1
            now[s[1]] -= 1
            continue
        if i == N - 1:
            ans.append(s[0])
            continue
        nx = S[i + 1]
        if nx == s:
            ans.append(s[0])
            now[s[0]] += 1
            now[s[1]] -= 1
            continue
        k = list(set(list(s)) & set(list(nx)))
        k = k[0]
        ans.append(k)
        now[k] += 1
        k = s.replace(k, '')
        now[k] -= 1

    if min(now.values()) < 0:
        return []
    return ans

for s in combinations(['', '', 'A', 'B', 'C'], r=3):
    s = ''.join(s)

    for t in s:
        if cnt[t] == 0:
            break
    else:
        ans = out(s)
        if len(ans) == 0:
            continue
        print('Yes')
        print(*ans, sep='\n')
        exit()

print('No')

Submission status: Runtime Error

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
from itertools import combinations
def run():
    pri = []
    N, A, B, C = map(int, sysread().split())
    vals = {'A':A, 'B':B, 'C':C}
    S = [sysread() for _ in range(N)]
    if A + B + C == 0:
        print('No')
        return None
    elif A + B + C == 1:
        if set([A,B,C]) != set([-1,1,1]):
            print('No')
            return None
        if -1 in (vals[S[0][0]], vals[S[0][1]]):
            if vals[S[0][0]] == -1:
                vals[S[0][0]] += 1
                vals[S[0][1]] -= 1
                pri.append(S[0][0])
            else:
                vals[S[0][1]] += 1
                vals[S[0][0]] -= 1
                print(S[0][1])
        else:
            print('No')
            return None
        for s in S[1:]:
            if vals[s[0]] == 0 and vals[s[1]] == 1:
                pri.append(s[0])
                vals[s[0]] += 1
                vals[s[1]] -= 1
            elif vals[s[0]] == 1 and vals[s[1]] == 0:
                pri.append(s[1])
                vals[s[1]] += 1
                vals[s[0]] -= 1
            else:
                print('No')
                return None
    else:
        tmp = S[0]
        done1 = False
        vals[tmp[0]] += 1
        vals[tmp[1]] -= 1
        if check_vals(vals):
            done1 = True
            pri.append(tmp[0])
        else:
            vals[tmp[0]] -= 1
            vals[tmp[1]] += 1

        if not done1:
            vals[tmp[1]] += 1
            vals[tmp[0]] -= 1
            if check_vals(vals):
                done1 = True
                pri.append(tmp[1])
        if not done1:
            print('No')
            return None

        tmp = S[1]
        done2 = False
        vals[tmp[0]] += 1
        vals[tmp[1]] -= 1
        if check_vals(vals):
            done2 = True
            pri.append(tmp[0])
        else:
            vals[tmp[0]] -= 1
            vals[tmp[1]] += 1

        if not done2:
            vals[tmp[1]] += 1
            vals[tmp[0]] -= 1
            if check_vals(vals):
                done2 = True
                pri.append(tmp[1])

        if not done2:
            print('No')
            return None


        for s in S[2:]:
            if vals[s[0]] == 0:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[1]] += 1
                vals[s[0]] -= 1
                pri.append(s[1])

    print('Yes')
    for sss in pri:
        print(sss)

def check_vals(vals):
    cnt = 0
    over = 0
    for v in vals.values():
        if v > 1:
            over += 1
        elif v == 0:
            cnt += 1
        elif v < 0:
            return False
    if cnt > 1:
        if over == 0:
            return False
        else:
            return True
    else:
        return True

if __name__ == "__main__":
    run()