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: Wrong Answer

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

n, a, b, c = map(int, input().split())
count = {
    'A': a,
    'B': b,
    'C': c
}
queries = []
for _ in range(n):
    query = tuple(input().replace('\n', ''))
    queries.append(query)


def solve(plus, minus):
    count[plus] += 1
    count[minus] -= 1
    ans.append(plus)


ans = []
for idx, (x, y) in enumerate(queries):
    if count[x] == count[y] == 0:
        print('No')
        exit()

    if count[x] == count[y] == 1:
        if idx == n-1:
            solve(x, y)
            break

        nex = queries[idx+1]
        if x in nex:
            solve(x, y)
        else:
            solve(y, x)

    elif count[x] >= count[y]:
        solve(x, y)

    else:
        solve(y, x)

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

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
ans = ["" for _ in range(N)]
flag = True
for i in range(N):
    query = input()
    if query == "AB":
        if A > B:
            A -= 1
            B += 1
            ans[i] = "B"
        else:
            A += 1
            B -= 1
            ans[i] = "A"
    elif query == "BC":
        if B > C:
            B -= 1
            C += 1
            ans[i] = "C"
        else:
            B += 1
            C -= 1
            ans[i] = "B"
    else:
        if A > C:
            A -= 1
            C += 1
            ans[i] = "C"
        else:
            A += 1
            C -= 1
            ans[i] = "A"
    if A >= 0 and B >= 0 and C >= 0:
        continue
    else:
        flag = False
if flag:
    print("Yes")
    for c in ans:
        print(c)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import collections
def main():

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

    ans = ""
    s = []
    for _ in range(N):
        s.append(input())

    d = dict()
    d['A'] = A
    d['B'] = B
    d['C'] = C
    ans = ""
    for i in range(N):
        if d[s[i][0]] == 0 and d[s[i][1]] == 0:
            break
        elif d[s[i][0]] == 0:
            d[s[i][0]] += 1
            d[s[i][1]] -= 1
            ans += s[i][0]
        elif d[s[i][1]] == 0:
            d[s[i][1]] += 1
            d[s[i][0]] -= 1
            ans += s[i][1]
        else:
            if d[s[i][0]] == 1 and d[s[i][1]] == 1:
                if i < N-1 and s[i][0] in s[i+1]:
                    d[s[i][0]] += 1
                    d[s[i][1]] -= 1
                    ans += s[i][0]
                else:
                    d[s[i][1]] += 1
                    d[s[i][0]] -= 1
                    ans += s[i][1]
            else:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                ans += s[i][0]
    if len(ans) == N:
        print("Yes")
        for c in ans:
            print(c)
    else:
        print("No")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

n, a, b, c = map(int, input().split())
count = {
    'A': a,
    'B': b,
    'C': c
}
queries = []
for _ in range(n):
    query = tuple(input().replace('\n', ''))
    queries.append(query)


def solve(plus, minus):
    count[plus] += 1
    count[minus] += 1
    ans.append(plus)


ans = []
for idx, (x, y) in enumerate(queries):
    if count[x] == count[y] == 0:
        print('No')
        exit()

    if count[x] == count[y] == 1:
        if idx == n-1:
            solve(x, y)
            break

        nex = queries[idx+1]
        if x in nex:
            solve(x, y)
        else:
            solve(y, x)

    elif count[x] >= count[y]:
        solve(x, y)

    else:
        solve(y, x)

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

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
Ans=[]
for i in range(N):
    S = input()
    if S[0] == 'A':
        if S[1] == 'B':
            if (A == 0) & (B == 0):
                print('No')
                exit()
            else:
                if A > B:
                    A -= 1
                    B += 1
                    Ans.append('B')
                else:
                    A += 1
                    B -= 1
                    Ans.append('A')
        else:
            if (A == 0) & (C == 0):
                print('No')
                exit()
            else:
                if A > C:
                    A -= 1
                    C += 1
                    Ans.append('C')
                else:
                    A += 1
                    C -= 1
                    Ans.append('A')
    else:
        if (B == 0) & (C == 0):
            print('No')
            exit()
        else:
            if B > C:
                B -= 1
                C += 1
                Ans.append('C')
            else:
                B += 1
                C -= 1
                Ans.append('B')
print('Yes')
for i in Ans:
    print(i)

Submission status: Wrong Answer

Solution code:
import collections
def main():

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

    ans = ""
    s = []
    for _ in range(N):
        s.append(input())

    d = dict()
    d['A'] = A
    d['B'] = B
    d['C'] = C
    ans = ""
    for i in range(N):
        if d[s[i][0]] == 0 and d[s[i][1]] == 0:
            print("No")
            exit
        elif d[s[i][0]] == 0:
            d[s[i][0]] += 1
            d[s[i][1]] -= 1
            ans += s[i][0]
        elif d[s[i][1]] == 0:
            d[s[i][1]] += 1
            d[s[i][0]] -= 1
            ans += s[i][1]
        else:
            if d[s[i][0]] == 1 and d[s[i][1]] == 1:
                if i < N-1 and s[i][0] in s[i+1]:
                    d[s[i][0]] += 1
                    d[s[i][1]] -= 1
                    ans += s[i][0]
                else:
                    d[s[i][1]] += 1
                    d[s[i][0]] -= 1
                    ans += s[i][1]
            else:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                ans += s[i][0]
    print("Yes")
    for c in ans:
        print(c)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
d = {}
d["A"] = a
d["B"] = b
d["C"] = c

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

res = []
for i in range(n):
    p = pl[i]
    if d[p[0]]==d[p[1]]==0:
        print("No")
        exit()
    if d[p[0]]==0:
        res.append(p[0])
        d[p[0]] += 1
        d[p[1]] -= 1
        continue
    if d[p[1]]==0:
        res.append(p[1])
        d[p[0]] -= 1
        d[p[1]] += 1
        continue
    if i==n-1:
        res.append(p[0])
        continue
    if p!=pl[i+1]:
        if p[0] in pl[i+1]:
            res.append(p[0])
            d[p[0]] += 1
            d[p[1]] -= 1
        else:
            res.append(p[1])
            d[p[0]] -= 1
            d[p[1]] += 1

    



print("Yes")
for p in res:
    print(p)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n, a, b, c = map(int,input().split())
    state = [a, b, c]
    s = sum(state)
    zeroes = state.count(0)
    D = {"AB" : (0, 1), "AC" : (0, 2), "BC" : (1, 2)}
    E = {0 : 'A', 1 : 'B', 2 : 'C'}
    S = [D[input()] for _ in range(n)]
    S.reverse()

    i, j = S[-1]
    if state[i] == 0 and state[j] == 0:
        print("No")
        return

    # s >= 3 なら必ずできる
    if s >= 3:
        print("Yes")
        while S:
            i, j = S.pop()
            if state[i] < state[j]:
                state[i] += 1
                state[j] -= 1
                print(E[i])
            else:
                state[i] -= 1
                state[j] += 1
                print(E[j])
        return

    # (0, 0, 1), (0, 1, 1), (0, 0, 2) のときは実際にやってみないとわからない
    ans = []
    while S:
        i, j = S.pop()
        if state[i] == 0 and state[j] == 0:
            print("No")
            return
        if state[i] < state[j]:
            state[i] += 1
            state[j] -= 1
            ans.append(E[i])
        else:
            state[i] -= 1
            state[j] += 1
            ans.append(E[j])
    print("Yes")
    print(*ans, sep = '\n')
resolve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

def add(c1,c2):
    global a,b,c
    ans.append(c1)
    if c1=="A":
        a+=1
    if c1=="B":
        b+=1
    if c1=="C":
        c+=1
    if c2=="A":
        a-=1
    if c2=="B":
        b-=1
    if c2=="C":
        c-=1

for i in range(n):
    que = s[i]
    nxt="NONE"
    if i!=n-1:
        nxt = s[i+1]

    if que=="AB":
        if a==b==0:
            print("No")
            exit()
        if a==b==1:
            if nxt=="NONE":
                add("A","B")
            elif "A" in nxt:
                add("A","B")
            else:
                add("B","A")
        elif a>=b:
            add("B","A")
        else:
            add("A","B")
    elif que=="AC":
        if a==c==0:
            print("No")
            exit()
        if a==c==1:
            if nxt=="NONE":
                add("A","C")
            elif "A" in nxt:
                add("A","C")
            else:
                add("C","A")
        elif a>=c:
            add("C","A")
        else:
            add("A","C")
    else: # BC
        if b==c==0:
            print("No")
            exit()
        if b==c==1:
            if nxt=="NONE":
                add("B","C")
            elif "B" in nxt:
                add("B","C")
            else:
                add("C","B")
        elif c>=b:
            add("C","B")
        else:
            add("B","C")

print("Yes")
for a in ans:
    print(a)

Submission status: Wrong Answer

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

ans = []
def select(sel,nsel):
    ans.append(sel)
    exec(sel + ' += 1')
    exec(nsel + ' -= 1')

for i,s in enumerate(S):
    if s=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        if A==0:
            select('A','B')
        elif B==0:
            select('B','A')
        else:
            if A==B==1 and i+1 < N:
                if 'A' in S[i+1]:
                    select('A','B')
                else:
                    select('B','A')
            elif A>B:
                select('B','A')
            else:
                select('A','B')
    elif s=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        if A==0:
            select('A','C')
        elif C==0:
            select('C','A')
        else:
            if A==C==1 and i+1 < N:
                if 'A' in S[i+1]:
                    select('A','C')
                else:
                    select('C','A')
            elif A>C:
                select('C','A')
            else:
                select('A','C')
    else:
        if C==0 and B==0:
            print('No')
            exit()
        if C==0:
            select('C','B')
        elif B==0:
            select('B','C')
        else:
            if C==B==1 and i+1 < N:
                if 'C' in S[i+1]:
                    select('C','B')
                else:
                    select('B','C')
            elif C>B:
                select('B','C')
            else:
                select('C','B')
print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

def plus_minus(s,t):
    for c in ['A','B','C']:
        if (c in s) and (c in t):
            plus = c
    if s[0] == plus:
        minus = s[1]
    else:
        minus = s[0]
    return plus, minus 

N,A,B,C = LI()
s = ['0']*N
for i in range(N):
    s[i] = str(input())

d = defaultdict(int)
d['A'] = A
d['B'] = B
d['C'] = C

if A+B+C == 0:
    print('No')
elif A+B+C == 1:
    ans = []
    for i in range(N):
        if d[s[i][0]] == d[s[i][1]] == 0:
            print('No')
            exit()
        else:
            if d[s[i][0]]:
                d[s[i][0]] -= 1
                d[s[i][1]] += 1
                ans.append(s[i][1])
            else:
                d[s[i][1]] -= 1
                d[s[i][0]] += 1
                ans.append(s[i][0])
    print('Yes')
    for a in ans:
        print(a)
else:
    if d[s[0][0]] == d[s[0][1]] == 0:
        print('No')
    else:
        print('Yes')
        for i in range(N):
            if d[s[i][0]] == 0:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                print(s[i][0])
            elif d[s[i][1]] == 0:
                d[s[i][1]] += 1
                d[s[i][0]] -= 1
                print(s[i][1])
            elif d[s[i][0]] == d[s[i][1]] == 1:
                if i != N-1:
                    p,m = plus_minus(s[i],s[i+1])
                    d[p] += 1
                    d[m] -= 1
                    print(p)
                else:
                    d[s[i][0]] += 1
                    d[s[i][1]] -= 1
                    print(s[i][0])
            else:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                print(s[i][0])

Submission status: Wrong Answer

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

def add(c1,c2):
    global a,b,c
    ans.append(c1)
    if c1=="A":
        a+=1
    if c1=="B":
        b+=1
    if c1=="C":
        c+=1
    if c2=="A":
        a-=1
    if c2=="B":
        b-=1
    if c2=="C":
        c-=1

for i in range(n):
    que = s[i]
    nxt="NONE"
    if i!=n-1:
        nxt = s[i+1]

    if que=="AB":
        if a==b==0:
            print("No")
            exit()
        if a==b==1:
            if nxt=="NONE":
                add("A","B")
            elif "A" in nxt:
                add("A","B")
            else:
                add("B","A")
        elif a>=b:
            add("B","A")
        else:
            add("A","B")
    elif que=="AC":
        if a==c==0:
            print("No")
            exit()
        if a==c==1:
            if nxt=="NONE":
                add("A","C")
            elif "A" in nxt:
                add("A","C")
            else:
                add("C","A")
        elif a>=c:
            add("C","A")
        else:
            add("A","C")
    else: # BC
        if b==c==0:
            print("No")
            exit()
        if b==c==1:
            if nxt=="NONE":
                add("B","C")
            elif "A" in nxt:
                add("C","B")
            else:
                add("B","C")
        elif c>=b:
            add("B","C")
        else:
            add("C","B")

print("Yes")
for a in ans:
    print(a)

Submission status: Wrong Answer

Solution code:
def change(d,ans,X,Y):
    z = {'A': 0, 'B': 1, 'C': 2}
    d[z[X]]+=1
    d[z[Y]]-=1
    ans.append(X)
    return d,ans


n,a,b,c = map(int, input().split())
d = [a,b,c]
q = [input() for i in range(n)]
z = {'A': 0, 'B': 1, 'C': 2}

if d.count(0)==3:
    print('No')
elif d.count(0)==2:
    ans = []
    for i in range(n):
        X = q[i][0]
        Y = q[i][1]
        if d[z[X]]>0:
            d,ans = change(d,ans,Y,X)
        elif d[z[Y]]>0:
            d,ans = change(d,ans,X,Y)
        else:
            print('No')
            exit()
    print('Yes')
    print('\n'.join(ans))
else:
    ans = []
    flag = 0
    for i in range(n-1):
        X = q[i][0]
        Y = q[i][1]
        if d[z[X]]==0:
            d,ans = change(d,ans,X,Y)
        elif d[z[Y]]==0:
            d,ans = change(d,ans,Y,X)
        else:
            if X in q[i+1]:
                d,ans = change(d,ans,X,Y)
            if Y in q[i+1]:
                d,ans = change(d,ans,Y,X)
    if len(ans)!=n:
        X = q[-1][0]
        Y = q[-1][1]
        if d[z[X]]>0:
            ans.append(Y)
        else:
            ans.append(X)
    print('Yes')
    print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# vim: set fileencoding=utf-8

# pylint: disable=unused-import, invalid-name, missing-docstring, bad-continuation


"""Module docstring
"""

import functools
import heapq
import itertools
import logging
import math
import random
import string
import sys
from argparse import ArgumentParser
from collections import defaultdict, deque
from copy import deepcopy
from typing import Dict, List, Optional, Set, Tuple


def solve(values: List[str], a: int, b: int, c: int) -> Optional[List[str]]:
    state = [a, b, c]
    names = ["A", "B", "C"]
    result = []
    for i, turn in enumerate(values):
        indexes = []
        if "A" in turn:
            indexes.append(0)
        if "B" in turn:
            indexes.append(1)
        if "C" in turn:
            indexes.append(2)
        f, s = indexes[0], indexes[1]
        if state[f] < state[s]:
            f, s = s, f
        if state[f] == state[s] == 1 and i < len(values) - 1:
            if names[f] in values[i + 1]:
                # need to increment s
                f, s = s, f
        # f is the index of largest value
        state[f] -= 1
        if state[f] < 0:
            return None
        result.append(names[f])
        state[s] += 1
        LOG.debug((state))
    return result


def do_job():
    "Do the work"
    LOG.debug("Start working")
    # first line is number of test cases
    N, A, B, C = list(map(int, input().split()))
    values = []
    for _ in range(N):
        values.append(input().strip())
    result = solve(values, A, B, C)
    print_output(result)


def print_output(result) -> None:
    "Formats and print result"
    if result is None:
        print("No")
    else:
        print("Yes")
        for c in result:
            print(c)
    # 6 digits float precision {:.6f} (6 is the default value)
    # print("Case #{}: {:f}".format(testcase + 1, result))


def configure_log() -> None:
    "Configure the log output"
    log_formatter = logging.Formatter("%(lineno)d - " "%(message)s")
    handler = logging.StreamHandler(sys.stdout)
    handler.setFormatter(log_formatter)
    LOG.addHandler(handler)


LOG = None
# for interactive call: do not add multiple times the handler
if not LOG:
    LOG = logging.getLogger("template")
    configure_log()


def main(argv=None):
    "Program wrapper."
    if argv is None:
        argv = sys.argv[1:]
    parser = ArgumentParser()
    parser.add_argument(
        "-v",
        "--verbose",
        dest="verbose",
        action="store_true",
        default=False,
        help="run as verbose mode",
    )
    args = parser.parse_args(argv)
    if args.verbose:
        LOG.setLevel(logging.DEBUG)
    do_job()
    return 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()
    sys.exit(main())


class memoized:
    """Decorator that caches a function's return value each time it is called.
    If called later with the same arguments, the cached value is returned, and
    not re-evaluated.
    """

    def __init__(self, func):
        self.func = func
        self.cache = {}

    def __call__(self, *args):
        try:
            return self.cache[args]
        except KeyError:
            value = self.func(*args)
            self.cache[args] = value
            return value
        except TypeError:
            # uncachable -- for instance, passing a list as an argument.
            # Better to not cache than to blow up entirely.
            return self.func(*args)

    def __repr__(self):
        """Return the function's docstring."""
        return self.func.__doc__

    def __get__(self, obj, objtype):
        """Support instance methods."""
        return functools.partial(self.__call__, obj)

Submission status: Wrong Answer

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
    if SS<1:
        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: Wrong Answer

Solution code:
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

def main():
    N,A,B,C = map(int,input().split())
    choice = [input() for _ in range(N)]
    ab = 0
    bc = 0
    ca = 0
    for s in choice:
        if s == 'AB':
            ab += 1
        elif s == 'BC':
            bc += 1
        else:
            ca += 1
    
    ans = [[0,0] for _ in range(3)]
    if A > 0 or B > 0:
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
    else:
        if bc > 0 and C > 0:
            bc -= 1
            C -= 1
            B += 1
            ans[1][0] += 1
        elif ca > 0 and C > 0:
            ca -= 1
            C -= 1
            A += 1
            ans[2][1] += 1
        else:
            print('No')
            return
        ans[0][0] += ab//2
        ans[0][1] += ab//2
        ab %= 2
        
    if B > 0 or C > 0:
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
    else:
        if ab > 0 and A > 0:
            A -= 1
            B += 1
            ab -= 1
            ans[0][1] += 1
        elif ca > 0 and A > 0:
            A -= 1
            C += 1
            ca -= 1
            ans[2][0] += 1
        else:
            print('No')
            return
        ans[1][0] += bc//2
        ans[1][1] += bc//2
        bc %= 2
                
    if C > 0 or A > 0:
        ans[2][0] += ca//2
        ans[2][1] += ca//2
        ca %= 2
    else:
        if bc > 0 and B > 0:
            B -= 1
            C += 1
            bc -= 1
            ans[1][1] += 1
        elif ab > 0 and B > 0:
            B -= 1
            A += 1
            ab -= 1
            ans[0][0] += 1
        else:
            print('No')
            return
        ans[2][0] += bc//2
        ans[2][1] += bc//2
        bc %= 2
    
    if ab > 0 and bc > 0 and ca > 0:
        if A > 0 or B > 0 or C > 0:
            ans[0][1] += 1
            ans[1][1] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ab > 0 and bc > 0:
        if A > 0:
            ans[0][1] += 1
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
            ans[0][0] += 1
        elif B > 1:
            ans[0][0] += 1
            ans[1][1] += 1
        else:
            print('No')
            return
    elif bc > 0 and ca > 0:
        if A > 0:
            ans[2][0] += 1
            ans[1][0] += 1
        elif B > 0:
            ans[1][1] += 1
            ans[2][0] += 1
        elif C > 1:
            ans[1][0] += 1
            ans[2][1] += 1
        else:
            print('No')
            return
    elif ca > 0 and ab > 0:
        if C > 0:
            ans[2][0] += 1
            ans[0][1] += 1
        elif B > 0:
            ans[0][0] += 1
            ans[2][0] += 1
        elif A > 1:
            ans[0][1] += 1
            ans[2][0] += 1
        else:
            print('No')
            return
    
    elif ab > 0:
        if A > 0:
            ans[0][1] += 1
        elif B > 0:
            ans[0][0] += 1
        else:
            print('No')
            return
    
    elif bc > 0:
        if B > 0:
            ans[1][1] += 1
        elif C > 0:
            ans[1][0] += 1
        else:
            print('No')
            return
    
    elif ca > 0:
        if A > 0:
            ans[2][1] += 1
        elif C > 0:
            ans[2][0] += 1
        else:
            print('No')
            return
    

    print('Yes')
    for s in choice:
        if s == 'AB':
            if ans[0][0] > 0:
                print('A')
                ans[0][0] -= 1
            else:
                print('B')
        elif s == 'BC':
            if ans[1][0] > 0:
                print('B')
                ans[1][0] -= 1
            else:
                print('C')
        else:
            if ans[2][0] > 0:
                print('C')
                ans[2][0] -= 1
            else:
                print('A')

if __name__ == '__main__':
    main()