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:
# -*- 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":
			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":
			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":
			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 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:
# -*- 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":
			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":
			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":
			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 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:
import sys
input = sys.stdin.readline

n,a,b,c = map(int,input().split())
ans = []
abc = [a,b,c]

d = {'AB\n':(0,1),'AC\n':(0,2),'BC\n':(1,2)}

if(a+b+c==0):
    print('No')
    exit()

if(a+b+c >= 3)|(a+b+c==1):
    for _ in range(n):
        s = input()
        x,y = d[s]
        if(abc[x]==0)&(abc[y]==0):
            print('No')
            exit()
        if(abc[x] < abc[y]):
            x,y = y,x
        abc[x] -= 1
        abc[y] += 1
        ans.append(y)
elif(a+b+c==2):
    ss = [input() for _ in range(n)]
    if(max(abc)==2):
        #(2,0,0)
        case = True
    else:
        case = False

    for i in range(n):
        s = ss[i]
        x,y = d[s]
        if(case)|(i==n-1):
            if(abc[x]==0)&(abc[y]==0):
                print('No')
                exit()
            if(abc[x] < abc[y]):
                x,y = y,x
            abc[x] -= 1
            abc[y] += 1
            case = False
        else:
            if(abc[x]!=abc[y]):
                if(abc[x] < abc[y]):
                    x,y = y,x
                abc[x] -= 1
                abc[y] += 1
            else:
                sn = ss[i+1]
                if(x in d[sn]):
                    x,y = y,x
                abc[x] -= 1
                abc[y] += 1
            case = True
        ans.append(y)

print('Yes')
for i in ans:
    print(chr(i+ord('A')))

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
f=1
a=[]
for s in S:
    if s=='AB':
        if A==B==0:
            f=0
        elif A<B:
            a.append('A')
            A+=1
            B-=1
        else:
            a.append('B')
            A-=1
            B+=1
    elif s=='BC':
        if B==C==0:
            f=0
        elif B<C:
            a.append('B')
            B+=1
            C-=1
        else:
            a.append('C')
            B-=1
            C+=1
    else:
        if C==A==0:
            f=0
        elif C<A:
            a.append('C')
            C+=1
            A-=1
        else:
            a.append('A')
            C-=1
            A+=1
if f:
    print('Yes')
    print(*a,sep='\n')
else:
    print('No')

Submission status: Wrong Answer

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

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":
			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":
			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":
			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 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:
def task(n, a, b, c, s):
    choices = []
    for i in range(0, n):
        if s[i] == 'AB':
            if a >= b:
                a -= 1
                b += 1
                choices.append('B')
            else:
                a += 1
                b -= 1
                choices.append('A')
        elif s[i] == 'AC':
            if a >= c:
                a -= 1
                c += 1
                choices.append('C')
            else:
                a += 1
                c -= 1
                choices.append('A')
        elif s[i] == 'BC':
            if b >= c:
                b -= 1
                c += 1
                choices.append('C')
            else:
                b += 1
                c -= 1
                choices.append('B')

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

    print('Yes')
    for choice in choices:
        print(choice)


n, a, b, c = map(int, input().split())
s = []
for i in range(0, n):
    s.append(input())

task(n, a, b, c, s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split()))
slist = []
d = {'A': a, 'B':b, 'C':c}
flag = 1
soln = []
for i in range(n):
    s = input()

    # greedy
    if d[s[0]] > d[s[1]]:
        d[s[0]] -= 1
        d[s[1]] += 1
        soln.append(s[1])

    else:
        d[s[1]] -= 1
        d[s[0]] += 1
        soln.append(s[0])

    
    if d[s[1]]<0 or d[s[0]]<0:
        print('No')
        flag = 0
        break

if flag:
    print('Yes')
    print('\n'.join(soln))

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(a<0)or(b<0)or(c<0):
    ans = "No"
    break
  if(s=="AB"):
    if(a>b):
      ansl.append("B")
      b+=1
      a-=1
    else:
      ansl.append("A")
      a+=1
      b-=1
  elif(s=="AC"):
    if(a>c):
      ansl.append("C")
      c+=1
      a-=1
    else:
      ansl.append("A")
      a+=1
      c-=1
  else:
    if(b>c):
      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:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n,a,b,c=map(int,input().split())
s=[input() for i in range(n)]
# print(s)
abc=[a,b,c]
dic={"A":0,"B":1,"C":2}

anslst=[]

for i in range(n-1):
    tmp=0
    v=dic[s[i][0]]
    w=dic[s[i][1]]
    x=dic[s[i+1][0]]
    y=dic[s[i+1][1]]
    if abc[v]==abc[w]==0:
        print("No")
        quit()
    if abc[v]==0 and abc[w]==1:
        anslst.append(s[i][0])
        abc[v]+=1
        abc[w]-=1
    elif abc[v]==1 and abc[w]==0:
        anslst.append(s[i][1])
        abc[v]-=1
        abc[w]+=1
    elif s[i]==s[i+1]:
        if abc[v]>abc[w]:
            anslst.append(s[i][1])
            abc[v]-=1
            abc[w]+=1
        else:
            anslst.append(s[i][0])
            abc[v]+=1
            abc[w]-=1
    elif v==x:
        anslst.append(s[i][0])
        abc[v]+=1
        abc[w]-=1
    elif w==y:
        anslst.append(s[i][1])
        abc[v]-=1
        abc[w]+=1
    else:
        if v==y:
            anslst.append(s[i][0])
            abc[v]+=1
            abc[w]-=1
        else:
            anslst.append(s[i][1])
            abc[v]-=1
            abc[w]+=1
    # print(abc)

if abc[dic[s[-1][0]]]==abc[dic[s[-1][1]]]==0:
    print("No")
    quit()
else:
    if abc[dic[s[-1][0]]]>abc[dic[s[-1][1]]]:
        anslst.append(s[-1][1])
    else:
        anslst.append(s[-1][0])
    
print("Yes")
for i in range(n):
    print(anslst[i])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A,B,C=MI()
    NA=0
    NB=0
    NC=0
    S=[]
    for i in range(N):
        s=input()
        S.append(s)
        NA+=s.count("A")
        NB+=s.count("B")
        NC+=s.count("C")
    
    ans=[]
    flag=1
    for i in range(N):
        s=S[i]
        if "A" in s:
            if s=="AB":
                aa=NA-A
                bb=NB-B
                if aa>=bb:
                    A+=1
                    B-=1
                    ans.append("A")
                else:
                    A-=1
                    B+=1
                    ans.append("B")
                NA-=1
                NB-=1
                    
            else:
                if s=="AC":
                    aa=NA-A
                    cc=NC-C
                    if aa>=cc:
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
                    NA-=1
                    NC-=1
                    
                    
        if "B" in s:
            if s=="BC":
                cc=NC-C
                bb=NB-B
                if cc>=bb:
                    C+=1
                    B-=1
                    ans.append("C")
                else:
                    C-=1
                    B+=1
                    ans.append("B")
                NC-=1
                NB-=1   
        if A<0 or B<0 or C<0:
            flag=-1
            break
        
    if flag==-1:
        print("No")
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()