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 python
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

def best_balance(x, y, L):
    if L[x] < L[y]:
        return y, x
    else:
        return x, y

ALPHABETS = ('A', 'B', 'C')

def main():
    #N, M = map(int, input().split())
    N, A, B, C = map(int, input().split())
    L = [A, B, C]
    S = []
    for i in range(N):
        S.append(input().strip())

    result = []
    for s in S:
        if s == 'AB':
            from_i, to_i = best_balance(0, 1, L)
        if s == 'BC':
            from_i, to_i = best_balance(1, 2, L)
        if s == 'AC':
            from_i, to_i = best_balance(0, 2, L)

        L[from_i] -= 1
        L[to_i] += 1

        if any([v < 0 for v in L]):
            print("No")
            return

        result.append(to_i)

    print("Yes")
    for r in result:
        print(ALPHABETS[r])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
d={}
count={}

d['A']=0
d['B']=0
d['C']=0

count['A']=0
count['B']=0
count['C']=0

ip=[int(x) for x in input().split()]
n=ip[0]
d['A']=ip[1]
d['B']=ip[2]
d['C']=ip[3]

flag=1
a=[]
for i in range(n):
	s=input()
	a.append(s)
	count[s[0]]+=1
	count[s[1]]+=1

ans=[]
for i in range(n):
	s=a[i]
	if d[s[0]]>d[s[1]]:
		ans.append(s[1])
		d[s[1]]+=1
		d[s[0]]-=1
	elif d[s[1]]>d[s[0]]:
		ans.append(s[0])
		d[s[1]]-=1
		d[s[0]]+=1
	else:
		if count[s[0]]>count[s[1]]:
			d[s[0]]+=1
			d[s[1]]-=1
			ans.append(s[0])
		else:
			d[s[1]]+=1
			d[s[0]]-=1
			ans.append(s[1])
	count[s[0]]-=1
	count[s[1]]-=1
	if d['A']<0 or d['B']<0 or d['C']<0:
		flag=0

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( ))
cnts = {"A":a,"B":b,"C":c}
ans=[]
import random
def rnd(ss):
    c1 = cnts[ss[0]];c2=cnts[ss[1]]

    if random.randint(1,c1+c2+1)>c1:
        cnts[ss[0]]-=1; cnts[ss[1]]+=1
        ans.append(ss[1])
    else:
        cnts[ss[0]]+=1; cnts[ss[1]]+=1
        ans.append(ss[0])
    for k in cnts:
        if cnts[k]<0:
            print("No")
            exit()
for _ in range(n):
    s= input()
    rnd(s)
print("Yes")
for ai in ans:
    print(ai)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
ans = "Yes"
ansl = []
for i in range(n):
  s = str(input())
  if(s=="AB"):
    if(a!=0):
      ansl.append("B")
      b+=1
      a-=1
    else:
      ansl.append("A")
      a+=1
      b-=1
  elif(s=="AC"):
    if(a!=0):
      ansl.append("C")
      c+=1
      a-=1
    else:
      ansl.append("A")
      a+=1
      c-=1
  else:
    if(b!=0):
      ansl.append("C")
      c+=1
      b-=1
    else:
      ansl.append("B")
      b+=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(ansl[i])
else:
  print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

##### dict の for文 #####
# for k, v in d.items():
#     print(k, v)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N,A,B,C = inputMap()
	stat = [0,0,0]
	Q = []
	for i in range(N):
		s = inputStr()
		Q.append(s)
		if s == "AB":
			stat[0] += 1
			stat[1] += 1
		elif s == "AC":
			stat[0] += 1
			stat[2] += 1
		elif s == "BC":
			stat[1] += 1
			stat[2] += 1

	delA = False
	delB = False
	delC = False
	if stat[0] <= A:
		delA = True
	if stat[1] <= B:
		delB = True
	if stat[2] <= C:
		delC = True

	#print(stat)
	#print("{} {} {}".format(delA,delB,delC))

	hon = "Yes"
	ans = []
	for s in Q:
		#print(i)
		#print("{} {} {}".format(A,B,C))
		if s == "AB":
			stat[0] -= 1
			stat[1] -= 1
			if delA == True:
				B += 1
				A -= 1
				ans.append("B")
				continue
			if delB == True:
				B -= 1
				A += 1
				ans.append("A")
				continue
			if A < B:
				B -= 1
				A += 1
				ans.append("A")
			else:
				B += 1
				A -= 1
				ans.append("B")
			if A < 0 or B < 0:
				hon = "No"
				break
		elif s == "AC":
			stat[0] -= 1
			stat[2] -= 1
			if delA == True:
				C += 1
				A -= 1
				ans.append("C")
				continue
			if delC == True:
				C -= 1
				A += 1
				ans.append("A")
				continue
			if A < C:
				C -= 1
				A += 1
				ans.append("A")
			else:
				C += 1
				A -= 1
				ans.append("C")
			if A < 0 or C < 0:
				hon = "No"
				break
		elif s == "BC":
			stat[1] -= 1
			stat[2] -= 1
			if delB == True:
				C += 1
				B -= 1
				ans.append("C")
				continue
			if delC == True:
				C -= 1
				B += 1
				ans.append("B")
				continue
			if B < C:
				C -= 1
				B += 1
				ans.append("B")
			else:
				C += 1
				B -= 1
				ans.append("C")
			if C < 0 or B < 0:
				hon = "No"
				break
				
		if stat[0] <= A:
			delA = True
		if stat[1] <= B:
			delB = True
		if stat[2] <= C:
			delC = True

	if hon == "No":
		print(hon)
	else:
		print(hon)
		for i in ans:
			print(i)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
ans = []
for i in range(n):
    if (a==0):
            if (b==0 or c==0):
                print("No")
                exit()
    if (b==0):
        if (c==0):
            print("No")
            exit()
    x = input()    
    if (x=="AB"):
        if a<=b:
            a += 1
            b -= 1
            ans.append("A")
        else:
            a -= 1
            b += 1
            ans.append("B")
    elif (x=="BC"):
        if b<=c:
            b += 1
            c -= 1
            ans.append("B")
        else:
            b -= 1
            c += 1
            ans.append("C")
    else:
        if a<=c:
            a += 1
            c -= 1
            ans.append("A")
        else:
            a -= 1
            c += 1
            ans.append("C")
print(*ans, sep="\n")

Submission status: Wrong Answer

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

if d.count(0)==3:
    print('No')
elif d.count(0)==2:
    ans = []
    for i in range(n):
        p = q[i]
        if p=='AB':
            if a==1:
                a-=1
                b+=1
                ans.append('B')
            elif b==1:
                a+=1
                b-=1
                ans.append('A')
            else:
                print('No')
                exit()
        elif p=='BC':
            if b==1:
                b-=1
                c+=1
                ans.append('C')
            elif c==1:
                c-=1
                b+=1
                ans.append('B')
            else:
                print('No')
                exit()
        else:
            if a==1:
                a-=1
                c+=1
                ans.append('C')
            elif c==1:
                c-=1
                a+=1
                ans.append('A')
            else:
                print('No')
                exit()
    print('Yes')
    print('\n'.join(ans))
elif d.count(0)==1:
    ans = []
    flag = 0
    for i in range(n-1):
        if flag:
            flag = 0
            continue
        s = x[q[i][0]]
        t = x[q[i][1]]
        if d[s]==0:
            d[s]+=1
            d[t]-=1
            ans.append(y[s])
        elif d[t]==0:
            d[s]-=1
            d[t]+=1
            ans.append(y[t])
        else:
            flag = 1
            if q[i]==q[i+1]:
                ans.append(y[s])
                ans.append(y[t])
            elif q[i][0] in q[i+1]:
                ans.append(y[s])
                if q[i][0] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[t]-=1
                    d[x[q[i+1][1]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[t]-=1
                    d[x[q[i+1][0]]]+=1
            else:
                ans.append(y[t])
                if q[i][1] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[s]-=1
                    d[x[q[i+1][1]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[s]-=1
                    d[x[q[i+1][0]]]+=1
    if len(ans)!=n:
        s = x[q[-1][0]]
        t = x[q[-1][1]]
        if d[s]==1:
            ans.append(y[t])
        else:
            ans.append(y[s])
    print('Yes')
    print('\n'.join(ans))
else:
    ans = []
    flag = 0
    for i in range(n-1):
        s = x[q[i][0]]
        t = x[q[i][1]]
        if flag:
            flag = 0
            continue
        if d[0]>0 and d[1]>0 and d[2]>0:
            ans.append(y[s])
            d[s]+=1
            d[t]-=1
            continue
        if d[s] == 0:
            d[s] += 1
            d[t] -= 1
            ans.append(y[s])
        elif d[t] == 0:
            d[s] -= 1
            d[t] += 1
            ans.append(y[t])
        else:
            flag = 1
            if q[i] == q[i + 1]:
                ans.append(y[s])
                ans.append(y[t])
            elif q[i][0] in q[i + 1]:
                ans.append(y[s])
                if q[i][0] != q[i + 1][0]:
                    ans.append(q[i + 1][0])
                    d[t] -= 1
                    d[x[q[i + 1][1]]] += 1
                else:
                    ans.append(q[i + 1][1])
                    d[t] -= 1
                    d[x[q[i + 1][0]]] += 1
            else:
                ans.append(y[t])
                if q[i][1] != q[i + 1][0]:
                    ans.append(q[i + 1][0])
                    d[s] -= 1
                    d[x[q[i + 1][1]]] += 1
                else:
                    ans.append(q[i + 1][1])
                    d[s] -= 1
                    d[x[q[i + 1][0]]] += 1
    if len(ans) != n:
        s = x[q[-1][0]]
        t = x[q[-1][1]]
        if d[s] >0 :
            ans.append(y[t])
        else:
            ans.append(y[s])
    print('Yes')
    print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
ans = []
D = {'A':a, 'B':b, 'C':c}
sx = SR(n)
for i in range(n):
    s = sx[i]
    s1 = s[0]
    s2 = s[1]
    if D[s1] == D[s2] == 0:
        print('No')
        exit()
    elif D[s[0]] == D[s[1]] == 1 and i + 1 < n:
        if s1 in sx[i + 1]:
            D[s1] -= 1
            D[s2] += 1
            ans += [s2]
        else:
            D[s1] += 1
            D[s1] -= 1
            ans += [s1]
    else:
        if D[s1] == 0:
            ans += [s1]
            D[s1] += 1
            D[s2] -= 1
        else:
            ans += [s2]
            D[s1] -= 1
            D[s2] += 1

print('Yes')
print(*ans, sep='\n')

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]
    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
        l = []
        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
            else:
                l.append(i)
        for i in l:
            heapq.heappush(h, i)
        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())
ans={"A":A,"B":B,"C":C}
ans1=[]
s=[]
for _ in range(N):
    s=input()
    if ans[s[0]]>=ans[s[1]]:
        ans[s[0]]-=1
        ans[s[1]]+=1
        ans1.append(s[1])
    else:
        ans[s[0]]+=1
        ans[s[1]]-=1
        ans1.append(s[0])
    if ans[s[0]] <=-1 or ans[s[1]]<= -1:
        print("No")
        exit()
print("Yes")
for a in ans1:
    print(a)

Submission status: Wrong Answer

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

def output(a_ab, b_ab, a_ac, c_ac, b_bc, c_bc):
    for round in rounds:
        if round == "AB":
            if a_ab > 0:
                print("A")
                a_ab -= 1
            else:
                print("B")

        if round == "AC":
            if a_ac > 0:
                print("A")
                a_ac -= 1
            else:
                print("C")

        if round == "BC":
            if b_bc > 0:
                print("B")
                b_bc -= 1
            else:
                print("C")
                

ab = rounds.count("AB")
bc = rounds.count("BC")
ac = rounds.count("AC")

for a_ab in range(ab + 1):
    b_ab = ab - a_ab
    a_rest = a + a_ab - b_ab
    b_rest = b + b_ab - a_ab

    a_ac = max(0, (ac - a_rest + 1)//2)
    c_ac = ac - a_ac

    b_bc = max(0, (bc - b_rest + 1)//2)
    c_bc = bc-b_bc

    
    if c + c_bc - b_bc + c_ac - a_ac >= 0:
        print("Yes")
        output(a_ab, b_ab, a_ac, c_ac, b_bc, c_bc)
        exit()

print("No")

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#import sys
#input = sys.stdin.readline
def one(D, S):
    d = {"A":0, "B":1, "C":2}
    ANS = []
    for a, b in S:
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        else:
            D[x] -= 1
            D[y] += 1
            ANS.append(b)
    print("\n".join(ANS))
            
def two(D,S,N):
    d = {"A":0, "B":1, "C":2}
    e = {0:"A", 1:"B",2:"C"}
    ANS = []
    for i in range(N):
        a, b = S[i]
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        elif D[y] == 0: 
            D[x] -= 1
            D[y] += 1
            ANS.append(b)
        else: #(1,1)
            if i+1 > N-1:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
                continue
            if S[i] == S[i+1]:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
                continue
            if not a in S[i+1]:
                x,y = y, x
                a, b = b,a
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
                
    print("\n".join(ANS))
    
def main():
    N, A, B, C = map( int, input().split())
    S = [ tuple( input()) for _ in range(N)]
    d = {"A":0, "B":1, "C":2}
    D = [A,B,C]
    ANS = []
    if sum(D) == 1:
        one(D, S)
        return
    if sum(D) == 2:
        two(D,S,N)
        return
    for a, b in S:
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        elif D[x] == 1:
            if D[y] == 0:
                D[x] -= 1
                D[y] += 1
                ANS.append(b)
            else:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
        else:
            D[x] -= 1
            D[y] += 1
            ANS.append(b)

    print("Yes")
    print("\n".join(ANS))
    
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import defaultdict

def getlist():
	return list(map(int, input().split()))

#処理内容
def main():
	N, A, B, C = getlist()
	ans = []
	Q = []
	cnt = 0
	for i in range(N):
		s = input()
		Q.append(s)

	#s1シミュレーﾄ
	if A + B + C == 1:
		for i in range(N):
			if Q[i] == "AB":
				if A == 1:
					ans.append("B")
					A -= 1; B += 1
				elif B == 1:
					ans.append("A")
					B -= 1; A += 1
				else:
					print("No")
					return
			elif Q[i] == "AC":
				if A == 1:
					ans.append("C")
					A -= 1; C += 1
				elif C == 1:
					ans.append("A")
					C -= 1; A += 1
				else:
					print("No")
					return
			else:
				if A == B:
					ans.append("C")
					B -= 1; C += 1
				elif C == 1:
					ans.append("B")
					C -= 1; B += 1
				else:
					print("No")
					return
		print("Yes")
		for i in range(N):
			print(ans[i])
		return

	#初期化事故
	if Q[0] == "AB" and A == 0 and B == 0:
		print("No")
		return
	if Q[0] == "AC" and A == 0 and C == 0:
		print("No")
		return
	if Q[0] == "BC" and B == 0 and C == 0:
		print("No")
		return

	for i in range(N - 1):
		if Q[i] == "AB":
			if A == 1 and B == 0:
				ans.append("B")
				A -= 1; B += 1
			elif A == 0 and B == 1:
				ans.append("A")
				A += 1; B -= 1

			elif A >= 2:
				ans.append("B")
				A -= 1; B += 1
			elif B >= 2:
				ans.append("A")
				A += 1; B -= 1
			else:
				if Q[i + 1] == "AC":
					ans.append("A")
					A += 1; B -= 1
				else:
					ans.append("B")
					A -= 1; B += 1

		elif Q[i] == "AC":
			if A == 1 and C == 0:
				ans.append("C")
				A -= 1; C += 1
			elif A == 0 and C == 1:
				ans.append("A")
				A += 1; C -= 1

			elif A >= 2:
				ans.append("C")
				A -= 1; C += 1
			elif C >= 2:
				ans.append("A")
				A += 1; C -= 1
			else:
				if Q[i + 1] == "AB":
					ans.append("A")
					A += 1; C -= 1
				else:
					ans.append("C")
					A -= 1; C += 1


		else:
			if B == 1 and C == 0:
				ans.append("C")
				B -= 1; C += 1
			elif B == 0 and C == 1:
				ans.append("B")
				B += 1; C -= 1

			elif B >= 2:
				ans.append("C")
				B -= 1; C += 1
			elif C >= 2:
				ans.append("B")
				B += 1; C -= 1
			else:
				if Q[i + 1] == "AB":
					ans.append("B")
					B += 1; C -= 1
				else:
					ans.append("C")
					B -= 1; C += 1

	#最後
	if Q[N - 1] == "AB":
		if A >= 1:
			ans.append("B")
		else:
			ans.append("A")
	elif Q[N - 1] == "AC":
		if A >= 1:
			ans.append("C")
		else:
			ans.append("A")
	else:
		if B >= 1:
			ans.append("C")
		else:
			ans.append("B")

	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 and C == 0:
    print("NG")
elif A == 0 and B == 0 and C == 1 \
    or A == 0 and B == 1 and C == 0 \
    or A == 1 and B == 0 and C == 0:
    flag = True
    ans = []
    for i in range(N):
        if s[i] == "AB":
            if A == 0 and B == 0:
                flag = False
                break
            elif A < B:
                A += 1
                B -= 1
                ans.append("A")
            else:
                A -= 1
                B += 1
                ans.append("B")
        elif s[i] == "BC":
            if B == 0 and C == 0:
                flag = False
                break
            elif B < C:
                B += 1
                C -= 1
                ans.append("B")
            else:
                B -= 1
                C += 1
                ans.append("C")
        else:
            if C == 0 and A == 0:
                flag = False
                break
            elif C < A:
                C += 1
                A -= 1
                ans.append("C")
            else:
                C -= 1
                A += 1
                ans.append("A")
    if flag:
        print("Yes")
        print(*ans)
    else:
        print("No")
elif A == 0 and B == 0 and C > 1 and s[0] == "AB" \
        or A == 0 and B > 1 and C == 0 and s[0] == "AC" \
        or A > 1 and B == 0 and C == 0 and s[0] == "BC":
        print("No")
else:
    print("Yes")
    flag = 0
    ans = []
    for i in range(N):
        if flag > 0:
            if flag == 1:
                if s[i] == "AB":
                    continue
                elif s[i] == "BC":
                    A -= 1
                    C += 1
                    ans.append("B")
                    ans.append("C")
                else:
                    B -= 1
                    C += 1
                    ans.append("A")
                    ans.append("C")
            elif flag == 2:
                if s[i] == "BC":
                    continue
                elif s[i] == "AC":
                    B -= 1
                    A += 1
                    ans.append("C")
                    ans.append("A")
                else:
                    C -= 1
                    A += 1
                    ans.append("B")
                    ans.append("A")
            elif flag == 3:
                if s[i] == "AC":
                    continue
                elif s[i] == "AB":
                    C -= 1
                    B += 1
                    ans.append("A")
                    ans.append("B")
                else:
                    A -= 1
                    B += 1
                    ans.append("C")
                    ans.append("B")
            flag = 0
        else:
            if s[i] == "AB":
                if A < B:
                    A += 1
                    B -= 1
                    ans.append("A")
                elif A == 1 and B ==1:
                    flag = 1
                else:
                    A -= 1
                    B += 1
                    ans.append("B")
            elif s[i] == "BC":
                if B < C:
                    B += 1
                    C -= 1
                    ans.append("B")
                elif B == 1 and C ==1:
                    flag = 2
                else:
                    B -= 1
                    C += 1
                    ans.append("C")
            else:
                if C < A:
                    C += 1
                    A -= 1
                    ans.append("C")
                elif C == 1 and A ==1:
                    flag = 3
                else:
                    C -= 1
                    A += 1
                    ans.append("A")
    print(*ans)