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
from collections import deque
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

n,a,b,c = map(int,readline().split())
lst1 = []
for i in range(n):
    lst1.append(readline().rstrip().decode('utf-8'))

res = [a,b,c]
l = [-1,-1,-1]
AB = deque([])
BC = deque([])
AC = deque([])
for idx,i in enumerate(lst1):
    if i == "AB":
        if res[0] == 0 and res[1] == 0:
            if l[0] != l[1]:
                if l[0] > l[1]:
                    AC.append("A")
                    res[0] = 1
                else:
                    BC.append("B")
                    res[1] = 1
        if res[0] == 0:
            res[0] = 1
            AB.append("A")
        else:
            res[0] -= 1
        if res[1] == 0:
            res[1] = 1
            AB.append("B")
        else:
            res[1] -= 1
        if res[0] == 0:
            l[0] = idx
        if res[1] == 0:
            l[1] = idx
    elif i == "BC":
        if res[1] == 0 and res[2] == 0:
            if l[1] != l[2]:
                if l[1] > l[2]:
                    AB.append("B")
                    res[1] = 1
                else:
                    AC.append("C")
                    res[2] = 1
        if res[1] == 0:
            res[1] = 1
            BC.append("B")
        else:
            res[1] -= 1
        if res[2] == 0:
            res[2] = 1
            BC.append("C")
        else:
            res[2] -= 1
        if res[1] == 0:
            l[1] = idx
        if res[2] == 0:
            l[2] = idx
    else:
        if res[0] == 0 and res[2] == 0:
            if l[0] != l[2]:
                if l[0] > l[2]:
                    AB.append("A")
                    res[0] = 1
                else:
                    BC.append("C")
                    res[2] = 1
        if res[0] == 0:
            res[0] = 1
            AC.append("A")
        else:
            res[0] -= 1
        if res[2] == 0:
            res[2] = 1
            AC.append("C")
        else:
            res[2] -= 1
        if res[0] == 0:
            l[0] = idx
        if res[2] == 0:
            l[2] = idx

res = [a,b,c]
ans = []
for i in lst1:
    if i == "AB":
    
        if AB:
            ans.append(AB.popleft())
        else:
            ans.append("A")

    elif i == "BC":
        if BC:
            ans.append(BC.popleft())
        else:
            ans.append("B")
    else:
        if AC:
            ans.append(AC.popleft())
        else:
            ans.append("A")

if AB or BC or AC:
    print("No")
    exit()
else:
    print("Yes")
    for i in ans:
        print(i)

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 turn in 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
        if state[f] < state[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
    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:
N, A, B, C = map(int, input().split())
v = [A, B, C]
s = [-1 for i in range(N)]
for i in range(N):
    t = input()
    print(t)
    if t == "AB":
        s[i] = 0
    if t == "BC":
        s[i] = 1
    if t == "AC":
        s[i] = 2
ans = [-1 for i in range(N)]
OK = True

for i in range(N):
    p = s[i]
    q = (s[i]+1)%3
    if v[p] < v[q]:
        ans[i] = p
        v[p] += 1
        v[q] -= 1
    elif v[p] > v[q]:
        ans[i] = q
        v[p] -= 1
        v[q] += 1
    elif v[p] > 0:
        if i == N-1:
            ans[i] = p
            v[p] += 1
            v[q] -= 1
        elif s[i+1] == q:
            ans[i] = q
            v[p] -= 1
            v[q] += 1
        else:
            ans[i] = p
            v[p] += 1
            v[q] -= 1
    else:
        OK = False
        break

ch = ["A", "B" ,"C"]
if OK:
    print("Yes")
    for i in range(N):
        print(ch[ans[i]])
else:
    print("No")

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
s=[input() for i in range(N)]
u=[0]*3
for i in range(N):
  if(s[i]=="AB"):
    u[0]+=1
  elif(s[i]=="BC"):
    u[1]+=1
  else:
    u[2]+=1
if((A==0 and B==0)or(B==0 and C==0)or(C==0 and A==0)):
  print("No")
else:
  for i in range(N):
    if(s[i]=="AB"):
      if(A>B):
        print("B")
      else:
        print("A")
    elif(s[i]=="BC"):
      if(B>C):
        print("C")
      else:
        print("B")
    else:
      if(C>A):
        print("A")
      else:
        print("C")

Submission status: Wrong Answer

Solution code:
import sys
N, A, B, C = map(int, input().split())
d=dict()
d["A"] = A
d["B"] = B
d["C"] = C
h = {"AB":"C","BC":"A","AC":"B"}
S = []
for _ in range(N):
    S.append(input().strip())
if A+B+C == 0:
    print("No")
elif A+B+C == 1:
    R = []
    for k in range(N):
        s = S[k]
        for i in range(2):
            if d[s[i]] == 1:
                R.append(s[1-i])
                d[s[i]] = 0
                d[s[1-i]] = 1
                break
        else:
            print("No")
            break
    else:
        print("Yes")
        for i in range(N):
            print(R[i])
else:
    R = []
    if d[S[0][0]] == 0 and d[S[0][1]] == 0:
        print("No")
        sys.exit()
    for i in range(N-1):
        s = S[i]
        if d[s[0]] == 1 and d[s[1]] == 1 and d[h[s]] == 0:
            if S[i+1] == s:
                d[s[0]] += 1
                d[s[1]] -= 1
                R.append(s[0])
            else:
                for j in range(2):
                    if s[j] in S[i+1]:
                        d[s[j]] += 1
                        d[s[j]] -= 1
                        R.append(s[j])
                        break
        elif d[s[0]] >= d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            R.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            R.append(s[0])
    for i in range(2):
        if d[S[N-1][i]] > 0:
            R.append(S[N-1][1-i])
            break
    print("Yes")
    for i in range(N):
        print(R[i])

Submission status: Wrong Answer

Solution code:
from collections import Counter

def resolve():
    n,a,b,c = map(int, input().split())
    abc = [a,b,c]
    s = [input() for i in range(n)]
    d = {'AB': [0,1,2], 'AC': [0,2,1], 'BC': [2,1,0]}
    dd = {'AB': ['AC', 'BC'], 'AC': ['AB', 'BC'], 'BC': ['AC', 'AB']}
    ans = []
    q = ['A', 'B', 'C']
    select = [0,0,0]

    for c in s:
        x = [abc[i] for i in d[c]]
        if x[0] == 0 and x[1] == 0:
            print('No')
            return
        if x[0] > x[1]:
            non = d[c][0]
            idx = d[c][1]
        else:
            non = d[c][1]
            idx = d[c][0]

        ans.append(q[idx])
        select[idx] += 1
        select[non] -= 1
        abc[idx] += 1
        abc[non] -= 1

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

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

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

ans = []
for i in range(N):
    l = s[i]
    if l == "AB":
        if A <= B:
            A += 1
            B -= 1
            ans.append('A')
        elif A >= B:
            A -= 1
            B += 1
            ans.append('B')
        else:
            if i < N - 1:
                if s[i+1] == 'AC':
                    A += 1
                    B -= 1
                    ans.append('A')
                else:
                    A -= 1
                    B += 1
                    ans.append('B')
            else:
                A -= 1
                B += 1
                ans.append('B')
    elif l == "BC":
        if B <= C:
            B += 1
            C -= 1
            ans.append('B')
        elif B >= C:
            B -= 1
            C += 1
            ans.append('C')
        else:
            if i < N - 1:
                if s[i+1] == 'AB':
                    B += 1
                    C -= 1
                    ans.append('B')
                else:
                    B -= 1
                    C += 1
                    ans.append('C')
            else:
                B -= 1
                C += 1
                ans.append('C')
    else:
        if A <= C:
            A += 1
            C -= 1
            ans.append('A')
        elif A >= C:
            A -= 1
            C += 1
            ans.append('C')
        else:
            if i < N - 1:
                if s[i+1] == 'AB':
                    A += 1
                    C -= 1
                    ans.append('A')
                else:
                    A -= 1
                    C += 1
                    ans.append('C')
            else:
                A -= 1
                C += 1
                ans.append('C')
    if A < 0 or B < 0 or C < 0:
        print('No')
        sys.exit()

print('Yes')
for i in range(N):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
def main():
    import heapq
    n, a, b, c = map(int, input().split())
    s_list = [input() for _ in [0]*n]

    def aaa(k):
        AB = 0
        AC = 0
        BC = 0
        for i in s_list[k:]:
            if i == "AB":
                if AB == 0:
                    print("B")
                    AB += 1
                else:
                    print("A")
                    AB -= 1
            elif i == "AC":
                if AC == 0:
                    print("C")
                    AC += 1
                else:
                    print("A")
                    AC -= 1
            else:
                if BC == 0:
                    print("C")
                    BC += 1
                else:
                    print("B")
                    BC -= 1

    if min(a, b, c) >= 1:
        print("Yes")
        aaa(0)
    elif a+b+c >= 3:
        l = []
        for j, i in enumerate(s_list):
            if i == "AB":
                if a == b == 0:
                    print("No")
                    return
                elif a <= b:
                    l.append("A")
                    a += 1
                    b -= 1
                else:
                    l.append("B")
                    a -= 1
                    b += 1
            elif i == "AC":
                if a == c == 0:
                    print("No")
                    return
                elif a <= c:
                    l.append("A")
                    a += 1
                    c -= 1
                else:
                    l.append("C")
                    a -= 1
                    c += 1
            else:
                if b == c == 0:
                    print("No")
                    return
                elif b <= c:
                    l.append("B")
                    b += 1
                    c -= 1
                else:
                    l.append("C")
                    b -= 1
                    c += 1
            if min(a, b, c) >= 1:
                print("Yes")
                for p in l:
                    print(p)
                aaa(j+1)
    else:
        import heapq
        h = [i for i in range(n)]
        play = [None]*n
        heapq.heapify(h)
        while a+b+c > 0:
            if a == max(a, b, c):
                color = "A"
                a -= 1
            elif b == max(a, b, c):
                color = "B"
                b -= 1
            else:
                color = "C"
                c -= 1
            color2 = color
            l = []
            cnt = 0
            while h:
                i = heapq.heappop(h)
                j = s_list[i]
                if color in j:
                    if color == j[0]:
                        color = j[1]
                    else:
                        color = j[0]
                    play[i] = color
                    cnt += 1
                else:
                    l.append(i)
            for i in l:
                heapq.heappush(h, i)
            if cnt == 0:
                if color2 == "A":
                    a -= a
                elif color2 == "B":
                    b -= b
                else:
                    c -= c
            if not h:
                print("Yes")
                for i in play:
                    print(i)
                return
        print("No")


main()

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
s=[input() for i in range(N)]
u=[0]*3
for i in range(N):
  if(s[i]=="AB"):
    u[0]+=1
  elif(s[i]=="BC"):
    u[1]+=1
  else:
    u[2]+=1
if((A==0 and B==0)or(B==0 and C==0)or(C==0 and A==0)):
  exit()
else:
  for i in range(N):
    if(s[i]=="AB"):
      if(A>B):
        print("B")
      else:
        print("A")
    elif(s[i]=="BC"):
      if(B>C):
        print("C")
      else:
        print("B")
    else:
      if(C>A):
        print("A")
      else:
        print("C")

Submission status: Wrong Answer

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')

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)

N, A, B, C = list(map(int, input().split()))
s = [0] * (N+1)

for i in range(N):
    s[i] = input()
s[N] = 'AB'
cnt = 0
if A == 0:
    cnt += 1
if B == 0:
    cnt += 1
if C == 0:
    cnt += 1


if cnt == 3:
    print('No')
    sys.exit()

if not ((A == 1 and B == 1 and C == 0) or (A == 1 and B == 0 and C == 1) or (A == 0 and B == 1 and C == 1)):
    ans = [0] * N
    for i in range(N):
        if s[i] == 'AB':
            if A > B:
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
        elif s[i] == 'AC':
            if A > C:
                A -= 1
                C += 1
                ans[i] = 'C'
            else:
                A += 1
                C -= 1
                ans[i] = 'A'
        elif s[i] == 'BC':
            if B > C:
                B -= 1
                C += 1
                ans[i] = 'C'
            else:
                B += 1
                C -= 1
                ans[i] = 'B'
        if A < 0 or B < 0 or C < 0:
            print('No')
            sys.exit()
    print('Yes')
    for i in range(N):
        print(ans[i])
    sys.exit()


ans = [0] * N
for i in range(N):
    if s[i] == 'AB':
        if A > B:
            A -= 1
            B += 1
            ans[i] = 'B'
        elif A < B:
            A += 1
            B -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'BC':
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
    elif s[i] == 'AC':
        if A > C:
            A -= 1
            C += 1
            ans[i] = 'C'
        elif A < C:
            A += 1
            C -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'AB':
                A += 1
                C -= 1
                ans[i] = 'A'
            else:
                A -= 1
                C += 1
                ans[i] = 'C'
    elif s[i] == 'BC':
        if B > C:
            B -= 1
            C += 1
            ans[i] = 'C'
        elif B < C:
            B += 1
            C -= 1
            ans[i] = 'B'
        else:
            if s[i+1] == 'AB':
                B += 1
                C -= 1
                ans[i] = 'B'
            else:
                B -= 1
                C += 1
                ans[i] = 'C'

    if A < 0 or B < 0 or C < 0:
        print('No')
        sys.exit()
print('Yes')
for i in range(N):
    print(ans[i])

Submission status: Wrong Answer

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

ok = True

ans_s = []

for i in range(n):
    s = input()

    if s == 'AB':
        if a < b:
            a += 1
            b -= 1
            ans_s.append('A')
        else:
            a -= 1
            b += 1
            ans_s.append('B')
    elif s == 'AC':
        if a < c:
            a += 1
            c -= 1
            ans_s.append('A')
        else:
            a -= 1
            c += 1
            ans_s.append('C')
    else:
        if b < c:
            b += 1
            c -= 1
            ans_s.append('B')
        else:
            b -= 1
            c += 1
            ans_s.append('C')

    if a < 0 or b < 0 or c < 0:
        ok = False
        break

if ok:
    print('Yes')
    for s_e in ans_s:
        print(s_e)
else:
    print('No')

Submission status: Wrong Answer

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

ABs = [0]
ACs = [0]
BCs = [0]

Amin = A
Amax = A
Bmin = B
Bmax = B
Cmin = C
Cmax = C

a = A
b = B
c = C

r = list()

for _ in range(N):
    s = input()
    if s == 'AB':
        if a == b == 0:
            break
        if a <= b:
            a += 1
            b -= 1
            r.append('A')
        else:
            b += 1
            a -= 1
            r.append('B')
    if s == 'AC':
        if a == c == 0:
            break
        if a <= c:
            a += 1
            c -= 1
            r.append('A')
        else:
            c += 1
            a -= 1
            r.append('C')
    if s == 'BC':
        if b == c == 0:
            break
        if b <= c:
            b += 1
            c -= 1
            r.append('B')
        else:
            c += 1
            b -= 1
            r.append('C')
else:
    print('Yes')
    for rr in r:
        print(rr)
    exit()

print('No')

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

al = [0]*(n+1)
bl = [0]*(n+1)
cl = [0]*(n+1)
pl = []
for i in range(n):
    p = input()
    pl.append(p)
    al[i+1] = al[i]
    bl[i+1] = bl[i]
    cl[i+1] = cl[i]
    if p[0]=="A" or p[1]=="A":
        al[i+1] += 1
    if p[0]=="B" or p[1]=="B":
        bl[i+1] += 1
    if p[0]=="C" or p[1]=="C":
        cl[i+1] += 1
dp = {}
dp["A"] = al
dp["B"] = bl
dp["C"] = cl

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



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