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:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n, a, b, c = LI()
    s = SR(n)
    ans = [None] * n
    dp = [deque() for i in range(3)]
    l = [[0] * 3 for i in range(n)]
    d = {"A": 0, "B": 1, "C": 2}
    for i in range(n):
        l[i][d[s[i][0]]] = 1
        l[i][d[s[i][1]]] = 1
    for i in range(n - 1, 0, -1):
        l[i - 1][0] += l[i][0]
        l[i - 1][1] += l[i][1]
        l[i - 1][2] += l[i][2]
    dd = [0] * 3
    ddd = defaultdict(int)
    for i in range(n):
        if s[i] <= "AB":
            if a < b:
                ans[i] = "A"
                a += 1
                b -= 1
            elif a > b:
                ans[i] = "B"
                a -= 1
                b += 1
            else:
                if a <= 0:
                    if len(dp[0]) - dd[0] <= 0 or len(dp[1]) - dd[1] <= 0:
                        print("No")
                        return
                    if len(dp[0]) - dd[0] > len(dp[1]) - dd[1]:
                        x = dp[0].popleft()
                        while ddd[x]:
                            x = dp[0].popleft()
                        ddd[x] = 1
                        dd[0] += 1
                        if s[x] <= "AB":
                            dd[1] += 1
                        else:
                            dd[2] += 1
                        ans[x] = "A"
                        ans[i] = "B"
                    elif len(dp[0]) - dd[0] < len(dp[1]) - dd[1]:
                        x = dp[1].popleft()
                        while ddd[x]:
                            x = dp[1].popleft()
                        ddd[x] = 1
                        dd[1] += 1
                        if s[x] <= "AB":
                            dd[0] += 1
                        else:
                            dd[2] += 1
                        ans[x] = "B"
                        ans[i] = "A"
                    else:
                        if l[i][0] < l[i][1]:
                            x = dp[0].popleft()
                            while ddd[x]:
                                x = dp[0].popleft()
                            ddd[x] = 1
                            dd[0] += 1
                            if s[x] <= "AB":
                                dd[1] += 1
                            else:
                                dd[2] += 1
                            ans[x] = "A"
                            ans[i] = "B"
                        else:
                            x = dp[1].popleft()
                            while ddd[x]:
                                x = dp[1].popleft()
                            dd[1] += 1
                            if s[x] <= "AB":
                                dd[0] += 1
                            else:
                                dd[2] += 1
                            ddd[x] = 1
                            ans[x] = "B"
                            ans[i] = "A"
                else:
                    dp[0].append(i)
                    dp[1].append(i)
                    a -= 1
                    b -= 1
        elif s[i] <= "AC":
            if a < c:
                ans[i] = "A"
                a += 1
                c -= 1
            elif a > c:
                ans[i] = "C"
                a -= 1
                c += 1
            else:
                if a <= 0:
                    if len(dp[0]) - dd[0] <= 0 or len(dp[2]) - dd[2] <= 0:
                        print("No")
                        return
                    if len(dp[0]) - dd[0] > len(dp[2]) - dd[2]:
                        x = dp[0].popleft()
                        while ddd[x]:
                            x = dp[0].popleft()
                        dd[0] += 1
                        if s[x] <= "AB":
                            dd[1] += 1
                        else:
                            dd[2] += 1
                        ddd[x] = 1
                        ans[x] = "A"
                        ans[i] = "C"
                    elif len(dp[0]) - dd[0] < len(dp[2]) - dd[2]:
                        x = dp[2].popleft()
                        while ddd[x]:
                            x = dp[2].popleft()
                        dd[2] += 1
                        if s[x] <= "AC":
                            dd[0] += 1
                        else:
                            dd[1] += 1
                        ddd[x] = 1
                        ans[x] = "C"
                        ans[i] = "A"
                    else:
                        if l[i][0] < l[i][2]:
                            x = dp[0].popleft()
                            while ddd[x]:
                                x = dp[0].popleft()
                            dd[0] += 1
                            if s[x] <= "AB":
                                dd[1] += 1
                            else:
                                dd[2] += 1
                            ddd[x] = 1
                            ans[x] = "A"
                            ans[i] = "C"
                        else:
                            x = dp[2].popleft()
                            while ddd[x]:
                                x = dp[2].popleft()
                            dd[2] += 1
                            if s[x] <= "AC":
                                dd[0] += 1
                            else:
                                dd[1] += 1
                            ddd[x] = 1
                            ans[x] = "C"
                            ans[i] = "A"
                else:
                    dp[0].append(i)
                    dp[2].append(i)
                    a -= 1
                    c -= 1
        else:
            if b < c:
                ans[i] = "B"
                b += 1
                c -= 1
            elif b > c:
                ans[i] = "C"
                b -= 1
                c += 1
            else:
                if b <= 0:
                    if len(dp[1]) - dd[1] <= 0 or len(dp[2]) - dd[2] <= 0:
                        print("No")
                        return
                    if len(dp[1]) - dd[1] > len(dp[2]) - dd[2]:
                        x = dp[1].popleft()
                        while ddd[x]:
                            x = dp[1].popleft()
                        dd[1] += 1
                        if s[x] <= "AB":
                            dd[0] += 1
                        else:
                            dd[2] += 1
                        ddd[x] = 1
                        ans[x] = "B"
                        ans[i] = "C"
                    elif len(dp[1]) - dd[1] < len(dp[2]) - dd[2]:
                        x = dp[2].popleft()
                        while ddd[x]:
                            x = dp[2].popleft()
                        dd[2] += 1
                        if s[x] <= "AC":
                            dd[0] += 1
                        else:
                            dd[1] += 1
                        ddd[x] = 1
                        ans[x] = "C"
                        ans[i] = "B"
                    else:
                        if l[i][1] < l[i][2]:
                            x = dp[1].popleft()
                            while ddd[x]:
                                x = dp[1].popleft()
                            dd[1] += 1
                            if s[x] <= "AB":
                                dd[0] += 1
                            else:
                                dd[2] += 1
                            ddd[x] = 1
                            ans[x] = "B"
                            ans[i] = "C"
                        else:
                            x = dp[2].popleft()
                            while ddd[x]:
                                x = dp[2].popleft()
                            dd[2] += 1
                            if s[x] <= "AC":
                                dd[0] += 1
                            else:
                                dd[1] += 1
                            ddd[x] = 1
                            ans[x] = "C"
                            ans[i] = "B"
                else:
                    dp[1].append(i)
                    dp[2].append(i)
                    b -= 1
                    c -= 1
    ll = ["A", "B", "C"]
    print("Yes")
    for i in range(3):
        while dp[i]:
            p = dp[i].pop()
            if ans[p] == None:
                ans[p] = ll[i]
    for i in ans:
        print(i)
    return


#main
if __name__ <= '__main__':
    solve()

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]-d[j][i], ct[j]) for j in l[i]])
	# print(tmp, "tmp")

	tflag = True
	for j in l[i]:
		if (ct[j]-d[j][i], ct[j]) == 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:
n,a,b,c = map(int,input().split())
a_count,b_count,c_count = 0,0,0
S = ['' for _ in range(n)]
for i in range(n):
    s = input()
    if s == 'AB' or s=='BA':
        S[i]='AB'
        a_count +=1; b_count += 1
    elif s =='BC' or s=='CB':
        S[i]='BC'
        b_count += 1; c_count += 1
    else:
        S[i]='CA'
        a_count += 1; c_count += 1

ap = a-a_count; bp = b-b_count; cp = c-c_count
ans = []
flag = 1
for i in range(n):
    s = S[i]
    if s=='AB':
        if a==0 and b==0:
            flag = 0; break
        elif a==0: #chose B   引く方
            a+=1;b-=1;ap+=2;ans.append('A')
        elif b==0: #chose A
            b+=1;a-=1;bp+=2;ans.append('B')
        else:
            if ap<bp: #chose B
                a+=1;b-=1;ap+=2;ans.append('A')
            else: #chose A
                b+=1;a-=1;bp+=2;ans.append('B')
                
    elif s=='BC':
        if b==0 and c==0:
            flag = 0; break
        elif b==0: #chose C
            b+=1;c-=1;bp+=2;ans.append('B')
        elif c==0: #chose B
            c+=1;b-=1;cp+=2;ans.append('C')
        else:
            if bp<cp: #chose C
                b+=1;c-=1;bp+=2;ans.append('B')
            else: #chose B
                c+=1;b-=1;cp+=2;ans.append('C')
                    
                    
                    
                    
                    
    else:
        if a==0 and c==0:
            flag = 0; break
        elif a==0: #chose C
            a+=1;c-=1;ap+=2;ans.append('A')
        elif b==0: #chose A
            c+=1;a-=1;cp+=2;ans.append('C')
        else:
            if ap<=cp: #chose C
                a+=1;c-=1;ap+=2;ans.append('A')
            else: #chose A
                c+=1;a-=1;cp+=2;ans.append('C')
                    
if flag ==0:
    print('No')
else:
    print('Yes')
    for i in range(n):
        print(ans[i])

Submission status: Wrong Answer

Solution code:


n, a, b, c = map(int, input().split())
if ((a == 0) + (b == 0) + (c == 0) == 1 and (a == 1) + (b == 1) + (c == 1) == 2):
    print("No")
else:
    s = "Yes"
    for _ in range(n):
        t = input()
        if (t == "AB"):
            if (a < b):
                s += "\nA"
                a += 1
                b -= 1
            else:
                s += "\nB"
                b += 1
                a -= 1
        if (t == "AC"):
            if (a < c):
                s += "\nA"
                a += 1
                c -= 1
            else:
                s += "\nC"
                c += 1
                a -= 1
        if (t == "BC"):
            if (b < c):
                s += "\nB"
                b += 1
                c -= 1
            else:
                s += "\nC"
                c += 1
                b -= 1
        if (a < 0 or b < 0 or c < 0):
            print("No")
            exit()
    print(s)

Submission status: Wrong Answer

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

A=[]
ans='Yes'
S=[]
for i in range(n):
    s=input()
    S.append(s)
for s in S:
    if s=='AC':
        if N[0]-1<=0:
            A.append('A')
            N[0]+=1
            N[2]-=1
        else:
            A.append('C')
            N[0]-=1
            N[2]+=1
    elif s=='BC':
        if N[1]-1<=0:
            A.append('B')
            N[1]+=1
            N[2]-=1
        else:
            A.append('C')
            N[1]-=1
            N[2]+=1
    else:
        if N[1]-1<=0:
            A.append('B')
            N[1]+=1
            N[0]-=1
        else:
            A.append('A')
            N[0]+=1
            N[1]-=1
    if N[0]<0 or N[1]<0 or N[2]<0:
        ans='No'
        break

if S[0]=='AB' and N[0]==0 and N[1]==0:
    ans='No'
elif S[0]=='AC' and N[0]==0 and N[2]==0:
    ans='No'
elif S[0]=='BC' and N[1]==0 and N[2]==0:
    ans='No'

if ans=='Yes':
    print(ans)
    for a in A:
        print(a)
else:
    print(ans)

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')
  ans = []
  seq = list(state.values())[0]
  while seq:
    ans.append(seq[0])
    
    seq = seq[1]
  for a in ans:
    print(a)
    
  
  
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:
def mp():return map(int,input().split())
def lmp():return list(map(int,input().split()))


n,a,b,c = mp()
ans_ar = []
q = []
cnt = [0]*3
if a != 0 and b != 0 and c != 0:
    print("Yes")
else:
    print("No")
    exit()
for i in range(n):
    s = input()
    q.append(s)
for i in range(n):
    s = q[i]
    if s == "AB":
        cnt[0] += 1
        if a == b == 0:
            print("No")
            exit()
        elif a == b == 1 and i != n-1:
            if q[i+1] == "AC":
                a += 1
                b -= 1
                ans_ar.append("A")
            else:
                a -= 1
                b += 1
                ans_ar.append("B")
        elif a >= b:
            a -= 1
            b += 1
            ans_ar.append("B")
        else:
            a += 1
            b -= 1
            ans_ar.append("A")
    if s == "AC":
        cnt[1] += 1
        if a == c == 0:
            print("No")
            exit()
        elif a == c == 1 and i != n - 1:
            if q[i + 1] == "AB":
                a += 1
                c -= 1
                ans_ar.append("A")
            else:
                a -= 1
                c += 1
                ans_ar.append("C")

        elif a >= c:
            a -= 1
            c += 1
            ans_ar.append("C")
        else:
            a += 1
            c -= 1
            ans_ar.append("A")
    if s == "BC":
        cnt[2] += 1
        if c == b == 0:
            print("No")
            exit()
        elif c == b == 1 and i != n-1:
            if q[i+1] == "AC":
                c += 1
                b -= 1
                ans_ar.append("C")
            else:
                c -= 1
                b += 1
                ans_ar.append("B")

        elif c >= b:
            c -= 1
            b += 1
            ans_ar.append("B")
        else:
            c += 1
            b -= 1
            ans_ar.append("C")
for i in ans_ar:
    print(i)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

n, a, b, c = MAP()
abc  = [a, b, c]
arr = [['AB', 0, 1], ['AC', 0, 2], ['BC', 1, 2]]
s = [STR() for _ in range(n)]

cnt = [0, 0, 0]

for i in range(n):
    for j in range(3):
        if s[i] == arr[j][0]:
            cnt[arr[j][1]] += 1
            cnt[arr[j][2]] += 1

pls = [max(0, cnt[i] - abc[i]) for i in range(3)]
if sum(pls) > n:
    print('No')
    exit()

tmp = ['A', 'B', 'C']

print('Yes')
for ss in s:
    for j in range(3):
        if ss == arr[j][0]:
            if pls[arr[j][1]] >= pls[arr[j][2]]:
                print(tmp[arr[j][1]])
                pls[arr[j][1]] -= 1
            else:
                print(tmp[arr[j][2]])
                pls[arr[j][2]] -= 1
            break

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
x, y, z = 0, 0, 0
q = []
for i in range(n):
	s = input()
	q.append(s)
	if s == 'AB':
		x += 1
	elif s == 'AC':
		y += 1
	else:
		z += 1

x %= 2
y %= 2
z %= 2


k, l, m = 0, 0, 0

if (a - x >= 0 and c - z >= 0) or (a - x >= 0 and b - y >= 0) or (c - z >= 0 and b - y >= 0):
	print('Yes')
	for i in q:
		if i == 'AB':
			if k % 2 == 0:
				print('A')
			else:
				print('B')
			k += 1
		elif i == 'BC':
			if l % 2 == 0:
				print('A')
			else:
				print('C')
			l += 1
		else:
			if m % 2 == 0:
				print('B')
			else:
				print('C')
			m += 1
	exit()
print('No')

Submission status: Wrong Answer

Solution code:
import sys
n, a, b, c = map(int, input().split())
tempa =0
tempc =0
li_a = []
li_ab = []
li_bc = []
li_c = []
for i in range(n):
    s = input()
    if s == 'AB':
        if b >= 1:
            a+=1
            tempa += 1
            b-=1
            li_a.append(i)
        else:
            if a >= 1:
                a-=1
                b+=1
                li_ab.append(i)
            elif tempc >= 1:
                tempc -=1
                c -=1
                b += 1
                a+=1
                b-=1
                tempa+=1
                k = li_c.pop()
                li_bc.append(k)
            else:
                print('No')
                sys.exit()
    else:
        if b >= 1:
            c+=1
            tempc += 1
            b-=1
            li_c.append(i)
        else:
            if c >= 1:
                c-=1
                b+=1
                li_bc.append(i)
            elif tempa>=1:
                tempa -= 1
                a-=1
                b+=1
                c+=1
                b-=1
                tempc+=1
                k = li_a.pop()
                li_ab.append(k)
            else:
                print('No')
                sys.exit()
for i in range(n):
    if i in li_a:
        print('A')
    elif i in li_ab:
        print('B')
    elif i in li_bc:
        print('B')
    else:
        print('C')

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,(input().split()))
s = [[] for _ in range(N+1)]
for i in range(N):
    s[i] = input()
s[N] = "NOTHING"

flag = 0
ans = []
S = A+B+C

if S >= 3:
    for i in range(N):
        print(A,B,C)
        if s[i] == "AB":
            if A+B == 0:
                flag = 1
                break
            if A >= B:
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C == 0:
                flag = 1
                break
            if A >= C:
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C == 0:
                flag = 1
                break
            if B >= C:
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("B")
                B += 1
                C -= 1
    if flag == 1:
        print("No")
    else:
        print("Yes")
        for i in ans:
            print(i)

else:
    for i in range(N):
        print(A,B,C)
        if s[i] == "AB":
            if A+B == 0:
                flag = 1
                break
            if B == 0:
                ans.append("B")
                B += 1
                A -= 1
            if A == 0:
                ans.append("A")
                A += 1
                B -= 1
            if s[i+1] == "AC":
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C == 0:
                flag = 1
                break
            if C == 0:
                ans.append("C")
                C += 1
                A -= 1
            if A == 0:
                ans.append("A")
                A += 1
                C -= 1
            if s[i+1] == "BC":
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C == 0:
                flag = 1
                break
            if C == 1:
                ans.append("C")
                C += 1
                B -= 1
            if B == 1:
                ans.append("B")
                B += 1
                C -= 1
            if s[i+1] == "AC":
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("b")
                B += 1
                C -= 1

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys, bisect, math, itertools
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))

n,a,b,c = inpl()
dic = defaultdict(int)
act = [list(input()) for i in range(n)]
ans=[]
for ind in range(n):
    i=act[ind]
    if i == "AB\n":
        if a < b:
            ans.append("A")
            a += 1
            b-=1
        elif a == b:
            if b == 0:
                print("No")
                exit()
            if ind != n - 1 and act[ind + 1] == "BC\n":
                ans.append("B")
                b += 1
                a -= 1
            else:
                ans.append("A")
                a += 1
                b -= 1
        else:
            ans.append("B")
            a-=1
            b+=1
    elif i == "BC\n":
        if b < c:
            ans.append("B")
            b += 1
            c-=1
        elif b == c:
            if b == 0:
                print("No")
                exit()
            if ind != n - 1 and act[ind + 1] == "AC\n":
                ans.append("C")
                c += 1
                b -= 1
            else:
                ans.append("B")
                b += 1
                c-=1
        else:
            ans.append("C")
            c += 1
            b-=1
    else:
        if c < a:
            ans.append("C")
            c += 1
            a -= 1
        elif a == c:
            if a == 0:
                print("No")
                exit()
            if ind != n - 1 and act[ind + 1] == "BC\n":
                ans.append("C")
                c += 1
                a -= 1
            else:
                ans.append("A")
                a += 1
                c -= 1
        else:
            if a==c and a == 0:
                print("No")
                exit()
            ans.append("A")
            a += 1
            c -= 1
print("Yes")
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
from collections import deque
n,a,b,c = map(int,input().split())
log = deque()
for i in range(n):
    s = input()
    if(s == "AB"):
        if(a > b):
            log.append("B")
            a-=1
            b+=1
        else:
            log.append("A")
            a+=1
            b-=1
    if(s == "BC"):
        if(b > c):
            log.append("C")
            b-=1
            c+=1
        else:
            log.append("B")
            b+=1
            c-=1
    if(s == "AC"):
        if(a > c):
            log.append("C")
            a-=1
            c+=1
        else:
            log.append("A")
            a+=1
            c-=1
    if(a<0 or b<0 or c<0):
        print("NO")
        break
else:
    print("YES")
    for j in log:
        print(j)

Submission status: Wrong Answer

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

idx = {'A': 0, 'B': 1, 'C': 2}

ans_str = 'Yes'
ans = []
state = 0
cnt = [A, B, C]
for i, si in enumerate(s):
    if state == 0:
        for j, sij in enumerate(si):
            if i == N - 1: continue
            #
            if cnt[idx[sij]] > 0 and sij not in s[i + 1]:
                ans_tmp = (sij, si[1 - j])
                break
            else:
                continue
        else:
            if cnt[idx[si[0]]] > cnt[idx[si[1]]]:
                ans_tmp = (si[0], si[1])
            else:
                ans_tmp = (si[1], si[0])
        #
        state = 1
    else:
        for j, sij in enumerate(si):
            if ans[i - 1][1] == sij:
                ans_tmp = (sij, si[1 - j])
                break
            else:
                continue
        else:
            for j, sij in enumerate(si):
                if i == N - 1: continue
                #
                if cnt[idx[sij]] > 0 and sij not in s[i + 1]:
                    ans_tmp = (sij, si[1 - j])
                    break
                else:
                    continue
            else:
                if cnt[idx[si[0]]] > cnt[idx[si[1]]]:
                    ans_tmp = (si[0], si[1])
                else:
                    ans_tmp = (si[1], si[0])
    #
    cnt[idx[ans_tmp[0]]] -= 1
    cnt[idx[ans_tmp[1]]] += 1
    if min(cnt) < 0:
        ans_str = 'No'
        break
    else:
        ans.append(ans_tmp)

print(ans_str)
if ans_str == 'Yes':
    for ans_tmp in ans:
        print(ans_tmp[0])