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 collections
def main():

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

    cur = collections.defaultdict(int)
    cur[(A, B, C)] = ""
    for _ in range(N):
        s = input()
        temp = collections.defaultdict(int)
        for a, b, c in cur:
            if s == "AB":
                if b-1>=0:
                    temp[(a+1, b-1, c)] = cur[(a, b, c)] + 'A'
                if a-1>=0:
                    temp[(a-1, b+1, c)] = cur[(a, b, c)] + 'B'
            elif s == "AC":
                if c-1>=0:
                    temp[(a+1, b, c-1)] = cur[(a, b, c)] + 'A'
                if a-1>=0:
                    temp[(a-1, b, c+1)] = cur[(a, b, c)] + 'C'
            else:
                if b-1>=0:
                    temp[(a, b-1, c+1)] = cur[(a, b, c)] + 'C'
                if a-1>=0:
                    temp[(a, b+1, c-1)] = cur[(a, b, c)] + 'B'
        cur = temp

    if len(cur) > 0:
        print("Yes")
        for v in cur:
            for c in cur[v]:
                print(c)
            break
    else:
        print("No")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def g_solve(N, A, B, C):
  seq = []
  for _ in range(N):
    s = input()
    if s == 'AB':
      if A > B:
        A -= 1
        B += 1
        seq.append('B')
      else:
        A += 1
        B -= 1
        seq.append('A')
    elif s == 'AC':
      if A > C:
        A -= 1
        C += 1
        seq.append('C')
      else:
        A += 1
        C -= 1
        seq.append('A')
    elif s == 'BC':
      if B > C:
        B -= 1
        C += 1
        seq.append('C')
      else:
        B += 1
        C -= 1
        seq.append('B')
    if A < 0 or B < 0 or C < 0:
      print('No')
      return
  print('Yes')
  print('\n'.join(seq))    


def dp_solve(N, a,b,c):
  state = {}
  state[(a, b, c)] = []
  for _ in range(N):
    s = input()
    next_state = {}
    for a, b, c in state:
      if s == 'AB':
        if b-1 >= 0:
            next_state[(a+1, b-1, c)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b+1, c)] = ['B', state[(a,b,c)]]
      if s == 'AC':
        if c-1 >= 0:
          next_state[(a+1, b, c-1)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b, c+1)] = ['C', state[(a,b,c)]]
      if s == 'BC':
        if c-1 >= 0:
          next_state[(a, b+1, c-1)] = ['B', state[(a,b,c)]]
        if b-1 >= 0:
          next_state[(a, b-1, c+1)] = ['C', state[(a,b,c)]]
    state = next_state
    if not state:
      print('No')
      return
    
  print('Yes')
  seq = list(state.values())[0]
  while seq:
    print(seq[0])
    seq = seq[1]
  
    
  
  
N, A, B, C = [int(x) for x in input().split(' ')]
if A + B + C < 20:
  dp_solve(N, A, B, C)
else:
  g_solve(N, A, B, C)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
@author: H_Hoshigi
"""
def main():
    N, A, B, C = map(int, input().split())
    S_lsit = []
    for i in range(N):
        S_lsit.append(input())
    x, y, z = 0, 0, 0
    for S in S_lsit:
        if S == "AB":
            x += 1
        elif S == "BC":
            y += 1
        else:
            z += 1
    a_ac, b_ac, c_ac = -1, -1, -1
    for a in range(x+1):
        for b in range(y+1):
            for c in range(z+1):
                if (A -a +z -c) >= 0 \
                    and (B -b +x -a) >= 0 \
                    and (C -c +y -b) >= 0:
                    a_ac, b_ac, c_ac = a, b, c
                    break
                    break
                    break
    print([a_ac, b_ac, c_ac])
    if a_ac==-1 or b_ac==-1 or c_ac==-1:
        print("No")
    else:
        print("Yes")
        for S in S_lsit:
            if S == "AB":
                if a_ac >= 1 and A >= 1:
                    print("B")
                    A -= 1
                    B += 1
                    a_ac -= 1
                else:
                    print("A")
                    A += 1
                    B -= 1
            elif S == "BC":
                if b_ac >= 1 and B >= 1:
                    print("C")
                    B -= 1
                    C += 1
                    b_ac -= 1
                else:
                    print("B")
                    B += 1
                    C -= 1
            else:
                if c_ac >= 1 and C >= 1:
                    print("A")
                    C -= 1
                    A += 1
                    c_ac -= 1
                else:
                    print("C")
                    C += 1
                    B -= 1
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

hashing = {"AB": 6, "BC": 3, "AC": 5}
appeared = {"AB": False, "BC": False, "AC": False}

connected = hashing[ss[0]]
appeared[ss[0]] = True
for i in range(1, N):
    connected ^= hashing[ss[i]]
    if connected == 3:
        appeared["AB"] = True
    elif connected == 5:
        appeared["AC"] = True
    elif connected == 6:
        appeared["BC"] = True

flg = True

if A == 0 and B == 0 and C == 0:
    flg = False
elif A == 0 and B == 0:
    if not appeared["AB"]:
        flg = False
elif B == 0 and C == 0:
    if not appeared["BC"]:
        flg = False
elif C == 0 and A == 0:
    if not appeared["AC"]:
        flg = False
else:
    for s in ss:
        if s == "AB":
            if A > B:
                history.append('B')
                A -= 1
                B += 1
            else:
                history.append('A')
                A += 1
                B -= 1
        elif s == "AC":
            if A > C:
                history.append('C')
                A -= 1
                C += 1
            else:
                history.append('A')
                A += 1
                C -= 1
        elif s == "BC":
            if B > C:
                history.append('C')
                B -= 1
                C += 1
            else:
                history.append('B')
                B += 1
                C -= 1
        if A < 0 or B < 0 or C < 0:
            flg = False
            break
        
        
if flg:
    print("Yes")
    print(*history, sep = '\n')
else:
    print("No")

Submission status: Wrong Answer

Solution code:

N, a, b, c, *S = open(0).read().split()
N, a, b, c = map(int, (N, a, b, c))

flg = a*b*c == 0 and (a*b == 1 or a*c == 1 or b*c == 1)

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if flg and c == 0 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            elif s_ == 'BC':
                ans.append('B')
                a -= 1
                b += 1
                continue

        ans.append('B' if a > b else 'A')
        if a > b:
            a -= 1
            b += 1
        else:
            a += 1
            b -= 1

    elif s == 'AC':
        if flg and b == 0 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            elif s_ == 'BC':
                ans.append('C')
                a -= 1
                c += 1
                continue

        ans.append('C' if a > c else 'A')
        if a > c:
            a -= 1
            c += 1
        else:
            a += 1
            c -= 1

    elif s == 'BC':
        if flg and a == 0 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
            elif s_ == 'AC':
                ans.append('C')
                b -= 1
                c += 1
                continue

        ans.append('C' if b > c else 'B')
        if b > c:
            b -= 1
            c += 1
        else:
            b += 1
            c -= 1

    if a < 0 or b < 0 or c < 0:
        print('No')
        exit()

    print('---------------->', a, b, c)

print('Yes')
[print(r) for r in ans]

Submission status: Wrong Answer

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

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

convs = dict(zip(['AB','AC','BC'], [0, 1, 2]))
chs = 'ABC'
Xss = [[0,1], [0,2], [1,2]]

for S in range(8):
#    print()
    Xss = [[0,1], [0,2], [1,2]]
    for i in range(3):
        if (S>>i)&1:
            Xss[i] = Xss[i][::-1]
#    print(bin(S)[2:].zfill(3), Xss)

    anss = []
    Ks = list([A, B, C])
    for Ss in Sss:
#        print(Ks)
        no = convs[Ss]
        x, y = Xss[no]
#        print('no:', no, '/ x:', x, '/ y:', y)
        if Ks[x] == 0:
            anss.append(chs[x])
            Ks[x] += 1
            Ks[y] -= 1
        elif Ks[y] == 0:
            anss.append(chs[y])
            Ks[y] += 1
            Ks[x] -= 1
        else:
            anss.append(chs[x])
            Ks[x] += 1
            Ks[y] -= 1
            Xss[no] = Xss[no][::-1]
        if Ks[x] < 0 or Ks[y] < 0:
            break
    else:
        print('Yes')
        print('\n'.join(anss))
        break
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c = [int(j) for j in input().split()]
l = []
for i in range(n):
	s = input()
	l.append(s)
d = {"A":[0]*n, "B":[0]*n, "C":[0]*n}
# for j in l[-1]:
	# d[j].append(1)
for i in range(n-1, -1, -1):
	for j in l[i]:
		d[j][i] = 1
for i in range(n-2, -1, -1):
	for j in ["A", "B", "C"]:
		d[j][i]+=d[j][i+1]
ct = {"A":a, "B":b, "C":c}
ans = []
flag = True
# print(ct, d)
for i in range(n):
	# print(ct)
	if flag==False:
		break
	tmp = min([ct[j]**2-d[j][i] for j in l[i]])
	# print(tmp, "tmp")

	tflag = True
	for j in l[i]:
		if (ct[j]**2-d[j][i]) == tmp and tflag:
			ans.append(j)
			tflag = False
			ct[j]+=1
		else:
			# print(ct[j], j)
			ct[j]-=1
			if ct[j]<0:
				flag = False
				print("No")
				break

else:
	if flag:
		print("Yes")
		for i in ans:
			print(i)

Submission status: Wrong Answer

Solution code:
def execute(idx, order, A, B, C, ans):
    if ans[idx] != ' ': return 1
    lft, rgt = order[idx]
    le, re = eval(lft), eval(rgt)
    if le > re: lft, rgt = rgt, lft

    if le == 0 and re == 0:
        print("No")
        return -1
    elif le < re:
        exec(lft + "+=1")
        exec(rgt + "-=1")
        ans[idx] = lft
    elif lft in order[idx+1] and rgt in order[idx+1]:
        ans[idx] = lft
        ans[idx+1] = rgt
    else:
        if rgt in order[idx+1]: lft, rgt = rgt, lft
        exec(lft + "+=1")
        exec(rgt + "-=1")
        ans[idx] = lft
    return 1

def main():
    n,A,B,C = map(int,input().split())
    order = [input() for i in range(n)]
    ans = [' ']*n
    order.append("banpei")
    for idx in range(n):
        if execute(idx, order, A, B, C, ans) < 0: return
    print("Yes")
    for c in ans: print(c)

main()

Submission status: Wrong Answer

Solution code:
import sys, copy, math, heapq, bisect
from itertools import accumulate
from collections import deque, defaultdict, Counter
sys.setrecursionlimit(500000)


N,A,B,C = map(int,input().split())
ABC = [A,B,C]
S = []
SS = ('A','B','C')
d = [0,0,0]
flag = True
for i in range(N):
    s = input()
    if s=="AB":
        S.append(2)
        d[0] += 1
        d[1] += 1
    elif s=="BC":
        S.append(0)
        d[1] += 1
        d[2] += 1
    else:
        S.append(1)
        d[2] += 1
        d[0] += 1
ans = []
for i in range(N):
    x,y = (S[i]+1)%3,(S[i]+2)%3
    if ABC[x]<ABC[y]:
        ans.append(SS[x])
        ABC[y] -= 1
        ABC[x] += 1
    elif ABC[x]>ABC[y]:
        ans.append(SS[y])
        ABC[y] += 1
        ABC[x] -= 1
    else:
        if d[x]<d[y]:
            ans.append(SS[y])
            ABC[y] += 1
            ABC[x] -= 1
        else:
            ans.append(SS[x])
            ABC[y] -= 1
            ABC[x] += 1
    d[S[i]] -= 1
    if ABC[0]<0 or ABC[1]<0 or ABC[2]<0:
        flag = False
        break


if flag:
    print('Yes')
    print(*ans,sep='\n')
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
ab=[0]*n
ac=[0]*n
bc=[0]*n
AB=0
BC=0
AC=0
ip=[0]*n
for i in range(n):
	ip[i]=input()
	if ip[i]=="AB":
		AB+=1
	elif ip[i]=="BC":
		BC+=1
	else:
		AC+=1
	ab[i]=AB
	bc[i]=BC
	ac[i]=AC
res=[0]*n
ans="Yes"
for i in range(n):
	AB=ab[n-1]-ab[i]-(a+b)
	BC=bc[n-1]-bc[i]-(b+c)
	AC=ac[n-1]-ac[i]-(a+c)
	if ip[i]=="AB":
		if AC>BC:
			res[i]="A"
			a+=1
			b-=1
		else:
			res[i]="B"
			a-=1
			b+=1
	if ip[i]=="BC":
		if AB>AC:
			res[i]="B"
			b+=1
			c-=1
		else:
			res[i]="C"
			b-=1
			c+=1
	if ip[i]=="AC":
		if AB>BC:
			res[i]="A"
			a+=1
			c-=1
		else:
			res[i]="C"
			a-=1
			c+=1
	if a<0 or b<0 or c<0:
		ans="No"
		break
if ans=="Yes":
	print(ans)
	for i in range(n):
		print(res[i])
else:
	print(ans)

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N, A, B, C = LI()
s = []
for i in range(N):
    s.append(str(input()))

def solve(A, B, C, N):
    anss = []
    ans = True
    for i in range(N):
        if s[i] == "AB":
            if A == 0 and B == 0:
                ans = False
                return ans, anss
            elif A > B:
                anss.append("B")
                A -= 1
                B += 1
            else:
                anss.append("A")
                A += 1
                B -= 1
        elif s[i] == "AC":
            if A == 0 and C == 0:
                ans = False
                return ans, anss
            elif A > C:
                anss.append("C")
                A -= 1
                C += 1
            else:
                anss.append("A")
                A += 1
                C -= 1
        elif s[i] == "BC":
            if B == 0 and C == 0:
                ans = False
                return ans, anss
            elif B > C:
                anss.append("C")
                B -= 1
                C += 1
            else:
                anss.append("B")
                B += 1
                C -= 1
    return ans, anss

ans, anss = solve(A, B, C, N)
# print(anss)
if ans:
    print("Yes")
    for i in range(N):
        print(anss[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:
  print("Yes")
  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:
s = list(map(int, input().split()))
n = s[0]
a = s[1]
b = s[2]
c = s[3]

s = []
for i in range(n):
    str = input()
    s.append(str)

if a + b + c >= 2:
    if a == 0 and b == 0 and s[0] == "AB":
        print("No")
    elif a == 0 and c == 0 and s[0] == "BC":
        print("No")
    elif a == 0 and c == 0 and s[0] == "AC":
        print("No")
    else:
        print("Yes")

        for i in range(n):
            if s[i] == "AB":
                if b > 2 and a > 2:
                    b = b - 1
                    a = a + 1
                    print("A")
                elif a < 2 and b > 2:
                    b = b - 1
                    a = a + 1
                    print("A")
                elif a > 2 and b < 2:
                    b = b + 1
                    a = a - 1
                    print("B")
                elif a == 1 and b == 1:
                    if i != n - 1:
                        if s[i + 1] == "BC":
                            b = b + 1
                            a = a - 1
                            print("B")
                        else:
                            a = a + 1
                            b = b - 1
                            print("A")
                    else:
                        a = a + 1
                        b = b - 1
                        print("A")
                elif a == 0:
                    a = a + 1
                    b = b - 1
                    print("A")
                elif b == 0:
                    b = b + 1
                    a = a - 1
                    print("B")

            elif s[i] == "BC":
                if b > 2 and c > 2:
                    c = c - 1
                    b = b + 1
                    print("B")
                elif b < 2 and c > 2:
                    c = c - 1
                    b = b + 1
                    print("B")
                elif b > 2 and c < 2:
                    c = c + 1
                    b = b - 1
                    print("C")
                elif b == 1 and c == 1:
                    if i != n - 1:
                        if s[i + 1] == "AC":
                            c = c + 1
                            b = b - 1
                            print("C")
                        else:
                            b = b + 1
                            c = c - 1
                            print("B")
                    else:
                        b = b + 1
                        c = c - 1
                        print("B")
                elif b == 0:
                    b = b + 1
                    c = c - 1
                    print("B")
                elif c == 0:
                    c = c + 1
                    b = b - 1
                    print("C")

            elif s[i] == "AC":
                if c > 2 and a > 2:
                    a = a - 1
                    c = c + 1
                    print("C")
                elif c < 2 and a > 2:
                    a = a - 1
                    c = c + 1
                    print("C")
                elif c > 2 and a < 2:
                    a = a + 1
                    c = c - 1
                    print("A")
                elif c == 1 and a == 1:
                    if i != n - 1:
                        if s[i + 1] == "BC":
                            c = c + 1
                            a = a - 1
                            print("C")
                        else:
                            a = a + 1
                            c = c - 1
                            print("A")
                    else:
                        c = c + 1
                        a = a - 1
                        print("C")
                elif a == 0:
                    a = a + 1
                    c = c - 1
                    print("A")
                elif c == 0:
                    c = c + 1
                    a = a - 1
                    print("C")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)


def input():
    return stdin.readline().strip()


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


def dfs(index, hp):
    if index == N:
        return ''

    s1, s2 = strings[index-1]
    if hp[s1] > 0:
        hp2 = hp.copy()
        hp2[s1] -= 1
        hp2[s2] += 1
        ret = dfs(index + 1, hp2)
        if ret != False:
            return ret + s2
    if hp[s2] > 0:
        hp2 = hp.copy()
        hp2[s2] -= 1
        hp2[s1] += 1
        ret = dfs(index + 1, hp2)
        if ret != False:
            return ret + s1

    return False


d = {'A': A, 'B': B, 'C': C}
ans = dfs(1, d)
if ans == False:
    print('No')
else:
    for c in ans:
        print(c)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
YES = "Yes"  # type: str
NO = "No"  # type: str

def cCount(s,c):
    return 1 if s[0]==c or s[1] == c else 0

def solve(n: int, A: int, B: int, C: int, s: "List[str]"):
    # S = {}
    # S[(A,B,C)] = ""
    # for i in range(n):
    #     N = {}
    #     d = s[i]
    #     for a,b,c in S:
    #         path = S[(a,b,c)]
    #         if d == "AB":
    #             if b >= n-i:
    #                 N[(a+1,b-1,c)] = path + "A"
    #                 continue
    #             if a >= n-i:
    #                 N[(a-1,b+1,c)] = path + "B"
    #                 continue
    #             if b > 0:
    #                 # A+1,B-1
    #                 N[(min(n,a+1),b-1,c)] = path + "A"
    #             if a > 0:
    #                 N[(a-1,min(n,b+1),c)] = path + "B"
    #         elif d == "BC":
    #             if b >= n - i:
    #                 N[(a, b - 1, c + 1)] = path + "C"
    #                 continue
    #             if c >= n - i:
    #                 N[(a, b+1, c-1)] = path + "B"
    #                 continue
    #             if c > 0:
    #                 # b +1,c -1
    #                 N[(a, min(n,b + 1), c-1)] = path + "B"
    #             if b > 0:
    #                 N[(a, b -1, min(n,c+1))] = path + "C"
    #         else:
    #             if c >= n - i:
    #                 N[(a + 1, b, c-1)] = path + "A"
    #                 continue
    #             if a >= n - i:
    #                 N[(a - 1, b, c+1)] = path + "C"
    #                 continue
    #             if c > 0:
    #                 N[(min(n,a+1),b,c-1)] = path + "A"
    #             if a > 0:
    #                 N[(a-1,b,min(n,c+1))] = path + "C"
    #     if len(N) == 0:
    #         print(NO)
    #         exit()
    #     S = N
    # print(YES)
    # for a,b,c in S:
    #     p = S[(a,b,c)]
    #     for i in p:
    #         print(i)
    #     break
    S = [[0,0,0] for i in range(n)]
    prev = [0,0,0]
    for i in range(n):
        command = s[i]
        prev = S[-1-i] = [prev[0]+cCount(command,"A"),prev[1]+cCount(command,"B"),prev[2]+cCount(command,"C")]
    path = []

    for i in range(n):
        if A < 0 or B < 0 or C < 0:
            print(NO)
            exit()
        command = s[i]
        if command == "AB":
            if S[i][0] <= A:
                path.append("B")
                B+=1
                A-=1
                continue
            if S[i][1] <= B:
                path.append("A")
                A+=1
                B-=1
                continue
            if S[i][0] - A <= S[i][1] - B:
                path.append("A")
                A+=1
                B-=1
            else:
                path.append("B")
                A-=1
                B+=1
        elif command == "BC":
            if S[i][1] <= B:
                path.append("C")
                B -= 1
                C += 1
                continue
            if S[i][2] <= C:
                path.append("B")
                B += 1
                C -= 1
                continue
            if S[i][1] - B <= S[i][2] - C:
                path.append("B")
                B += 1
                C -= 1
            else:
                path.append("C")
                B -= 1
                C += 1
        else:
            if S[i][0] <= A:
                path.append("C")
                C += 1
                A -= 1
                continue
            if S[i][2] <= C:
                path.append("A")
                A += 1
                C -= 1
                continue
            if S[i][0] - A <= S[i][2] - C:
                path.append("A")
                A += 1
                C -= 1
            else:
                path.append("C")
                A -= 1
                C +=1
    if A < 0 or B < 0 or C < 0:
        print(NO)
        exit()
    print(YES)
    for i in path:
        print(i)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, A, B, C, s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)
#input = sys.stdin.readline
N,A,B,C = MI()
s = LS2(N)
abc = [A,B,C]
dic = {'A':0,'B':1,'C':2}
redic = {0:'A',1:'B',2:'C'}
f=0
select = []
for i in range(N):
    order = s[i][0]
    x = dic[order[0]]
    y = dic[order[1]]
    xnum=abc[x]
    ynum=abc[y]
    if xnum>ynum:
        select.append(y)
        abc[x] -= 1
        abc[y] += 1
    else:
        select.append(x)
        abc[x] += 1
        abc[y] -= 1
    for i in abc:
        if i<0:
            print('No')
            f=1
            break
    if f==1:
        break
else:
    print('Yes')
    [print(redic[x]) for x in select]

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N, A, B, C = LI()
s = []
for i in range(N):
    s.append(str(input()))

def solve(A, B, C, N):
    anss = []
    ans = True
    for i in range(N):
        if s[i] == "AB":
            if A == 0 and B == 0:
                ans = False
                return ans, anss
            elif A > B:
                anss.append("B")
                A -= 1
                B += 1
            else:
                anss.append("A")
                A += 1
                B -= 1
        elif s[i] == "AC":
            if A == 0 and C == 0:
                ans = False
                return ans, anss
            elif A > C:
                anss.append("C")
                A -= 1
                C += 1
            else:
                anss.append("A")
                A += 1
                C -= 1
        elif s[i] == "BC":
            if B == 0 and C == 0:
                ans = False
                return ans, anss
            elif B > C:
                anss.append("C")
                B -= 1
                C += 1
            else:
                anss.append("B")
                B += 1
                C -= 1
    return ans, anss

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