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 math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))
n, a, b, c = map(int, input().split())
ans = []
cnt_ab = 0
cnt_ac = 0
cnt_bc = 0
s = []
for _ in range(n):
    x = input()
    if x == 'AB':
        cnt_ab += 1
        s.append(0)
    elif x == 'AC':
        cnt_ac += 1
        s.append(1)
    else:
        cnt_bc += 1
        s.append(2)
for x in s:
    if x == 0:
        if a == 0:
            a += 1
            b -= 1
            ans.append('A')
        elif b == 0:
            a -= 1
            b += 1
            ans.append('B')
        elif cnt_ac > cnt_bc:
            b -= 1
            a += 1
            ans.append('A')
        elif cnt_bc > cnt_ac:
            a -= 1
            b += 1
            ans.append('B')
        else:
            if a > b:
                a -= 1
                b += 1
                ans.append('B')
            elif a < b:
                a += 1
                b -= 1
                ans.append('A')
        cnt_ab -= 1
    elif x == 1:
        if a == 0:
            a += 1
            c -= 1
            ans.append('A')
        elif c == 0:
            a -= 1
            c += 1
            ans.append('C')
        elif cnt_ab > cnt_bc:
            c -= 1
            a += 1
            ans.append('A')
        elif cnt_ab < cnt_bc:
            a -= 1
            c += 1
            ans.append('C')
        else:
            if a > c:
                a -= 1
                c += 1
                ans.append('C')
            elif a < c:
                a += 1
                c -= 1
                ans.append('A')
        cnt_ac -= 1
    else:
        if b == 0:
            b += 1
            c -= 1
            ans.append('B')
        elif c == 0:
            b -= 1
            c += 1
            ans.append('C')
        elif cnt_ac > cnt_ab:
            c -= 1
            b += 1
            ans.append('B')
        elif cnt_ac < cnt_ab:
            b -= 1
            c += 1
            ans.append('C')
        else:
            if b > c:
                b -= 1
                c += 1
                ans.append('C')
            elif b < c:
                b += 1
                c -= 1
                ans.append('B')
        cnt_bc -= 1
    if a < 0 or b < 0 or c < 0:
        print('No')
        sys.exit()
print('Yes')
for x in ans:
    print(x)

Submission status: Wrong Answer

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

is_possible = True
choice = []
ab = a + b
ac = a + c
bc = b + c

for i in range(n):
    if s[i] == 'AB':
        if ab == 0:
            is_possible = False
            break
        else:
            if ac > bc:
                choice.append('B')
                ac -= 1
                bc += 1
            else:
                choice.append('A')
                ac += 1
                bc -= 1
    elif s[i] == 'AC':
        if ac == 0:
            is_possible = False
            break
        else:
            if ab > bc:
                choice.append('C')
                ab -= 1
                bc += 1
            else:
                choice.append('A')
                ab += 1
                bc -= 1
    else:
        # BC
        if bc == 0:
            is_possible = False
            break
        else:
            if ab > ac:
                choice.append('C')
                ab -= 1
                ac += 1
            else:
                choice.append('B')
                ab += 1
                ac -= 1

if is_possible:
    print('Yes')
    print(*choice, sep='\n')
    # print('  ', 0, a, b, c)
    # for i in range(n):
    #     if s[i] == 'AB':
    #         if choice[i] == 'A':
    #             a += 1
    #             b -= 1
    #         else:
    #             a -= 1
    #             b += 1
    #     elif s[i] == 'AC':
    #         if choice[i] == 'A':
    #             a += 1
    #             c -= 1
    #         else:
    #             a -= 1
    #             c += 1
    #     else:
    #         # BC
    #         if choice[i] == 'B':
    #             b += 1
    #             c -= 1
    #         else:
    #             b -= 1
    #             c += 1
    #     print(s[i], choice[i], a, b, c)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
z=a+b+c
if z==0:
    print('No')
if z==2:
    ans=[]
    sl=[]
    for i in range(n):
        sl.append(input())
    i=0
    while i<n:
        s=sl[i]
        if s=='AB' and a==0 and b==0:
            print('No')
            exit()
        if s=='BC' and c==0 and b==0:
            print('No')
            exit()
        if s=='AC' and a==0 and c==0:
            print('No')
            exit()
        if s=='AB':
            if a<b:
                b-=1
                a+=1
                ans.append('A')
            elif a>b:
                a-=1
                b+=1
                ans.append('B')
            else:
                i+=1
                ff=1
                while i<n and sl[i]=='AB':
                    ff+=1
                    i+=1
                if i==n:
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('A')
                        else:
                            ans.append('B')
                    break
                if sl[i]=='AC':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('A')
                        else:
                            ans.append('B')
                if sl[i]=='BC':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('B')
                        else:
                            ans.append('A')
                            
                continue
        if s=='AC':
            if c<a:
                a-=1
                c+=1
                ans.append('C')
            elif c>a:
                c-=1
                a+=1
                ans.append('A')
            else:
                i+=1
                ff=1
                while i<n and sl[i]=='AC':
                    ff+=1
                    i+=1
                if i==n:
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('A')
                        else:
                            ans.append('C')
                    break
                if sl[i]=='AB':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('A')
                        else:
                            ans.append('C')
                if sl[i]=='BC':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('C')
                        else:
                            ans.append('A')
                            
                continue
        if s=='BC':
            if c<b:
                b-=1
                c+=1
                ans.append('C')
            elif c>b:
                c-=1
                b+=1
                ans.append('B')
            else:
                i+=1
                ff=1
                while i<n and sl[i]=='BC':
                    ff+=1
                    i+=1
                if i==n:
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('C')
                        else:
                            ans.append('B')
                    break
                if sl[i]=='AC':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('C')
                        else:
                            ans.append('B')
                if sl[i]=='AB':
                    for pp in range(ff):
                        if pp%2==0:
                            ans.append('B')
                        else:
                            ans.append('C')
                            
                continue
        i+=1
    print('Yes')
    for i in ans:
        print(i)     
if z>=3 or z==1:
    ans=[]
    for i in range(n):
        s=input()
        if s=='AB' and a==0 and b==0:
            print('No')
            exit()
        if s=='BC' and c==0 and b==0:
            print('No')
            exit()
        if s=='AC' and a==0 and c==0:
            print('No')
            exit()
        if s=='AB':
            if b<a:
                a-=1
                b+=1
                ans.append('B')
            else:
                b-=1
                a+=1
                ans.append('A')
        if s=='AC':
            if c<a:
                a-=1
                c+=1
                ans.append('C')
            else:
                c-=1
                a+=1
                ans.append('A')
        if s=='BC':
            if c<b:
                b-=1
                c+=1
                ans.append('C')
            else:
                c-=1
                b+=1
                ans.append('B')
    print('Yes')
    for i in ans:
        print(i)

Submission status: Wrong Answer

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

ans=[]
for i in range(N):
    s=S[i]
    if s=="AB":
        if A>B:
            ans.append("B")
            A-=1
            B+=1
        else:
            ans.append("A")
            A+=1
            B-=1
    elif s=="BC":
        if B>C:
            ans.append("C")
            B-=1
            C+=1
        else:
            ans.append("B")
            B+=1
            C-=1
    else:
        if A>C:
            ans.append("C")
            A-=1
            C+=1
        else:
            ans.append("A")
            A+=1
            C-=1
    if min(A,B,C)<0:
        print("No")
        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())
s = [input() for _ in range(n)]
score = {"A":a, "B":b, "C":c}
if a+b == 0 or b+c == 0 or c+a==0:
    print("No")
else:
    print("Yes")
    for g in s:
        x, y = list(g)
        if score[x] < score[y]:
            score[y] -= 1
            score[x] += 1
            print(x)
        else:
            score[x] -= 1
            score[y] += 1
            print(y)

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
ans_list= []
chk = 0
for i in range(N):
    s = input()
    if chk == 1:
        if s == "AC":
            A += 1
            B -= 1
            ans_list.append(["A"])
            chk = 0
        elif s == "BC":
            A -= 1
            B += 1
            ans_list.append(["B"])
            chk = 0
        else:
            A += 1
            B -= 1
            ans_list.append(["A"])
            chk = 0
    elif chk == 2:
        if s == "AB":
            A += 1
            C -= 1
            ans_list.append(["A"])
            chk = 0
        elif s == "BC":
            A -= 1
            C += 1
            ans_list.append(["C"])
            chk = 0
        else:
            A += 1
            C -= 1
            ans_list.append(["A"])
    else:
        if s == "AB":
            B += 1
            C -= 1
            ans_list.append(["B"])
            chk = 0
        elif s == "AC":
            B -= 1
            C += 1
            ans_list.append(["C"])
            chk = 0
        else:
            B += 1
            C -= 1
            ans_list.append(["B"])
    if s == "AB" and A == 0 and B == 0:
        print("No")
        break
    elif s == "BC" and B == 0 and C == 0:
        print("No")
        break
    elif s == "AC" and A == 0 and C == 0:
        print("No")
        break
    else:
        if s == "AB":
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 0:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B != 1 :
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                B += 1
                ans_list.append(["B"])
            else:
                chk = 1
        elif s == "AC":
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 0:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B != 1 :
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                B += 1
                ans_list.append(["B"])
            else:
                chk = 2
        else:
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 0:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B != 1 :
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                B += 1
                ans_list.append(["B"])
            else:
                chk = 3
if len(ans_list) == N:
    print("Yes")
    for i in range(N):
        print(*ans_list[i])

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import copy

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

d = deque([(0, a, b, c, [])])
while d:
    i, a_, b_, c_, history = d.pop()

    if i == n:
        print('Yes')
        print('\n'.join(history))
        exit()

    if s[i] == 'AB':
        if a_ > 0:
            h = copy(history)
            h.append('B')
            d.append((i+1, a_ - 1, b_ + 1, c, h))
        if b_ > 0:
            h = copy(history)
            h.append('A')
            d.append((i+1, a_ + 1, b_ - 1, c, h))
    elif s[i] == 'BC':
        if b_ > 0:
            h = copy(history)
            h.append('C')
            d.append((i+1, a_, b_ - 1, c + 1, h))
        if c_ > 0:
            h = copy(history)
            h.append('B')
            d.append((i+1, a_, b_ + 1, c - 1, h))
    else:
        if c_ > 0:
            h = copy(history)
            h.append('A')
            d.append((i + 1, a_ + 1, b_, c - 1, h))
        if a_ > 0:
            h = copy(history)
            h.append('C')
            d.append((i + 1, a_ - 1, b_, c + 1, h))

print('No')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N, A, B, C = map(int, input().split())
res = []
for _ in range(N):
  s = input()[: -1]
  if s == "AB":
    A -= 1
    B -= 1
    if A < B:
      A += 2
      res.append("A")
    else:
      B += 2
      res.append("B")
  if s == "AC":
    A -= 1
    C -= 1
    if A < C:
      A += 2
      res.append("A")
    else:
      C += 2
      res.append("C")
  if s == "BC":
    B -= 1
    C -= 1
    if B < C:
      B += 2
      res.append("B")
    else:
      C += 2
      res.append("C")
  if min(A, B, C) < 0:
    print("No")
    exit(0)

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

Submission status: Wrong Answer

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

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

f = True
ans = []
for ss in s:
    if cur[ss[0]] >= cur[ss[1]]:
        cur[ss[0]] -= 1
        cur[ss[1]] += 1
        ans.append(ss[1])
    else:
        cur[ss[0]] += 1
        cur[ss[1]] -= 1
        ans.append(ss[0])

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

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

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
D = {"AB": 2, "AC": 1, "BC": 0}
N, a, b, c = map(int, input().split())
X = [a, b, c]
S = [D[input()] for _ in range(N)]
ANS = []
if max(X) == 0:
    print("No")
    exit()

if sum(X) == 1:
    t = 0 if a else 1 if b else 2
    for s in S:
        if X[s]:
            print("No")
            exit()
        t ^= s ^ 3
        ANS.append(t)
        X[s-1] ^= 1
        X[s-2] ^= 1
    print("Yes")
    print("\n".join([chr(t+65) for t in ANS]))
    exit()

s = S[0]
if X[s-1] + X[s-2] == 0:
    print("No")
    exit()

for i, s in enumerate(S):
    if X[s-1] > X[s-2]:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    elif X[s-1] < X[s-2]:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1
    elif i == N - 1 or S[i+1] == s-1:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    else:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1

print("Yes")
print("\n".join([chr(t+65) for t in ANS]))

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    remain = defaultdict(int)
    n,a,b,c = inpm()
    remain['A'] = a
    remain['B'] = b
    remain['C'] = c
    dic = defaultdict(int)
    s = []
    for _ in range(n):
        t = list(input())
        dic[t[0]] += 1
        dic[t[1]] += 1
        s.append(t)
    ans = []
    for i in range(n):
        [x,y] = s[i]
        if (dic[x] < dic[y] and remain[x] > 0) or remain[y] == 0:
            ans.append(y)
            dic[x] -= 1
            dic[y] -= 1
            remain[x] -= 1
            remain[y] += 1
            if remain[x] < 0:
                print('No')
                return
        else:
            ans.append(x)
            dic[x] -= 1
            dic[y] -= 1
            remain[x] += 1
            remain[y] -= 1
            if remain[y] < 0:
                print('No')
                return
    print('Yes')
    for i in range(n):
        print(ans[i])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
FAST_IO = 0
if FAST_IO:
    import io, sys, atexit
    rr = iter(sys.stdin.read().splitlines()).next
    sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
    @atexit.register
    def write():
        sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
    rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())

####
from collections import Counter

def solve2(N, A, B, C, SA):
    if A==B==C==0: return
    # Wlog atleast one zero, not 3 zeroes
    x = SA[0]
    if x == 0:
        if B == C == 0: return
    if x == 1:
        if A == C == 0: return
    if x == 2:
        if A == B == 0: return
    if A+B+C >= 3:
        return solve(N, A, B, C, SA, True) # force greedy
    S = A+B+C
    if S == 1:  # playing catch
        return solve(N, A, B, C, SA, True)
    # S = 2
    seen = [None] * (N+1)
    seen[0] = {(A, B, C)}
    for i, x in enumerate(SA, 1):
        seen[i] = set()
        for a, b, c in seen[i-1]:
            if x == 0:  # BC
                na = a; nb = b + 1; nc = c - 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add((na, nb, nc))
                na = a; nb = b - 1; nc = c + 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add((na, nb, nc))
            if x == 1:  # AC
                na = a + 1; nb = b; nc = c - 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add((na, nb, nc))
                na = a - 1; nb = b; nc = c + 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add((na, nb, nc))
            if x == 2:  # AB
                na = a + 1; nb = b - 1; nc = c
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add((na, nb, nc))
                na = a - 1; nb = b + 1; nc = c
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add((na, nb, nc))
        if not seen[i]: return
    ans = []
    a,b,c = seen[-1].pop()
    for i in xrange(N-1, -1, -1):
        x = SA[i]
        if x == 0:  # BC
            na = a; nb = b + 1; nc = c - 1
            nei = na, nb, nc
            if nei in seen[i]:
                ans.append('C')
                a,b,c = nei
                continue
            na = a; nb = b - 1; nc = c + 1
            nei = na, nb, nc
            if nei in seen[i]:
                ans.append('B')
                a,b,c = nei
                continue
        if x == 1:  # AC
            na = a + 1; nb = b; nc = c - 1
            nei = na, nb, nc
            if nei in seen[i]:
                ans.append('C')
                a,b,c = nei
                continue
            na = a - 1; nb = b; nc = c + 1
            nei = na, nb, nc
            if nei in seen[i]:
                ans.append('A')
                a,b,c = nei
                continue
        if x == 1:  # AB
            na = a + 1; nb = b - 1; nc = c
            nei = na, nb, nc
            if nei in seen[i]:
                ans.append('B')
                a,b,c = nei
                continue
            na = a - 1; nb = b + 1; nc = c
            nei = na, nb, nc
            if nei in seen[i]:
                ans.append('A')
                a,b,c = nei
                continue
    return ans[::-1]
    
def solve(N, A, B, C, SA, flag=False):
    if flag is False and min(A, B, C) == 0:
        return solve2(N, A, B, C, SA)
    ans = []
    for x in SA:
        if x == 0:  # BC
            if B >= C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
        elif x == 1: #AC
            if A >= C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')
        else:  # AB
            if A >= B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
        if A<0 or B<0 or C<0: return
    return ans
    
N, A, B, C = rrm()
SA = []
for _ in xrange(N):
    s = rr()
    if s == 'AB':
        SA.append(2)
    elif s == 'BC':
        SA.append(0)
    else:
        SA.append(1)
ans = solve(N, A, B, C, SA)
if ans:
    print "Yes"
    for x in ans: print x
else:
    print "No"

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,a,b,c = inpl()
a = [a,b,c]
s = [input() for _ in range(n)]
res = []
for i,x in enumerate(s):
    if x == 'AB':
        x,y = 0,1
    elif x == 'AC':
        x,y = 0,2
    else:
        x,y = 1,2
    if a[x] >= a[y]:
        a[x] -= 1
        a[y] += 1
        res.append(y)
    else:
        a[x] += 1
        a[y] -= 1
        res.append(x)
    if a[x] < 0 or a[y] < 0:
        print('No')
        quit()
print('Yes')
for i,x in enumerate(res):
    print(chr(ord('A') + x))