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:
n, a, b, c = list(map(int, input().split()))
command = [input() for i in range(n)]

summ = a + b + c
if summ >= 3:
    if command[0] == 'AB' and summ == c or command[0] == 'BC' and summ == a or command[0] == 'AC' and summ == b:
        print('No')
    else:
        print('Yes')
        for com in command:
            if com == 'AB':
                if a == 0:
                    print('A')
                    a += 1
                    b -= 1
                elif b == 0:
                    print('B')
                    a -= 1
                    b += 1
                elif a == 1:
                    print('A')
                    a += 1
                    b -= 1
                else:
                    print('B')
                    a -= 1
                    b += 1
            if com == 'BC':
                if b == 0:
                    print('B')
                    b += 1
                    c -= 1
                elif c == 0:
                    print('C')
                    b -= 1
                    c += 1
                elif b == 1:
                    print('B')
                    b += 1
                    c -= 1
                else:
                    print('C')
                    b -= 1
                    c += 1
            if com == 'AC':
                if c == 0:
                    print('C')
                    c += 1
                    a -= 1
                elif a == 0:
                    print('A')
                    c -= 1
                    a += 1
                elif c == 1:
                    print('C')
                    c += 1
                    a -= 1
                else:
                    print('A')
                    c -= 1
                    a += 1


elif summ == 2:
    print('diffficult...')

elif summ == 1:
    # １つずつシミュレーション

    choice = [''] * n
    for i, com in enumerate(command):
        if com == 'AB':
            if c == 1:
                print('No')
                exit()
            if a == 0:
                choice[i] = 'A'
                a += 1
                b -= 1
            elif b == 0:
                choice[i] = 'B'
                a -= 1
                b += 1
        if com == 'BC':
            if a == 1:
                print('No')
                exit()
            if b == 0:
                choice[i] = 'B'
                b += 1
                c -= 1
            elif c == 0:
                choice[i] = 'C'
                b -= 1
                c += 1


        if com == 'AC':
            if b == 1:
                print('No')
                exit()
            if c == 0:
                choice[i] = 'C'
                c += 1
                a -= 1
            elif a == 0:
                choice[i] = 'A'
                c -= 1
                a += 1
    
    print('Yes')
    for ch in choice:
        print(ch)

Submission status: Wrong Answer

Solution code:

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

ans = []
for si in s:
  if si == 'AB':
    if a == 0 and b == 0:
      print('No')
      exit()
    elif a == 0 and b > 0:
      ans.append('A')
      a += 1
      b -= 1
    else:
      if a > b:
        ans.append('B')
        a -= 1
        b += 1
      else:
        ans.append('A')
        a += 1
        b -= 1
  elif si == 'BC':
    if b == 0 and c == 0:
      print('No')
      exit()
    elif b == 0 and c > 0:
      ans.append('B')
      b += 1
      c -= 1
    else:
      if b > c:
        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 ==0 and a > 0:
      ans.append('C')
      c += 1
      a -= 1
    else:
      if a < c:
        ans.append('A')
        c -= 1
        a += 1
      else:
        ans.append('C')
        c += 1
        a -= 1

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

Submission status: Wrong Answer

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



if [A,B,C].count(0)==2:
    print("No")
    exit()
else:
    print("Yes")
S=[0]*N
for i in range(N):
    s=input()
    S[i]=s

for i in range(N):  
    if S[i]=="AB":
        if A==1 and B==1:
            if i+1<N:
                if S[i+1]=="BC" and C==0:
                    B+=1
                    A-=1
                    print("B")
                    continue
                elif S[i+1]=="AC" and C==0:
                    A+=1
                    B-=1
                    print("A")
                    continue
            A+=1
            B-=1
            print("A")
        elif A>B:
            A-=1
            B+=1
            print("B")
        elif B>A:
            A+=1
            B-=1
            print("A")
        else:
            A+=1
            B-=1
            print("A")



    if S[i]=="AC":
        if A==1 and C==1:
            if i+1<N:
                if S[i+1]=="BC" and B==0:
                    C+=1
                    A-=1
                    print("C")
                    continue
                elif S[i+1]=="AB" and B==0:
                    A+=1
                    C-=1
                    print("A")
                    continue
            A+=1
            C-=1
            print("A")
        elif A>C:
            A-=1
            C+=1
            print("C")
        elif C>A:
            A+=1
            C-=1
            print("A")
        else:
            A+=1
            C-=1
            print("A")

    if S[i]=="BC":
        if B==1 and C==1:
            if i+1<N:
                if S[i+1]=="AC" and A==0:
                    C+=1
                    B-=1
                    print("C")
                    continue
                elif S[i+1]=="AB" and A==0:
                    B+=1
                    C-=1
                    print("B")
                    continue
            B+=1
            C-=1
            print("B")
        elif B>C:
            B-=1
            C+=1
            print("C")
        elif C>B:
            B+=1
            C-=1
            print("B")
        else:
            B+=1
            C-=1
            print("B")

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==1:
            a=-1
            b=-1
        elif (a==-1 and b==0) or (a==0 or b==-1):
            ans.append("A")
            ans.append("B")
            a=1
            b=1
            c=0
        elif a==-1 and b==-1:
            ans.append("A")
            ans.append("B")
            a=1
            b=1
        elif 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
if a==-1 and b==-1:
    ans.append("A")
if b==-1 and c==-1:
    ans.append("B")
if c==-1 and a==-1:
    ans.append("C")

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(2 * 10**6)


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


def backtrack(S, a, b, c, tmp=''):
    if a < 0 or b < 0 or c < 0:
        return
    elif S == []:
        yield tmp

    if S[0] == 'AB':
        yield from backtrack(S[1:], a - 1, b, c, tmp + 'B')
        yield from backtrack(S[1:], a, b - 1, c, tmp + 'A')
    elif S[0] == 'BC':
        yield from backtrack(S[1:], a, b, c - 1, tmp + 'B')
        yield from backtrack(S[1:], a, b - 1, c, tmp + 'C')
    elif S[0] == 'AC':
        yield from backtrack(S[1:], a - 1, b, c, tmp + 'C')
        yield from backtrack(S[1:], a, b, c - 1, tmp + 'A')


def main():
    N, A, B, C = inpl()
    S = [input().strip() for _ in range(N)]

    for ans in backtrack(S, A, B, C):
        print("Yes")
        for a in ans:
            print(a)
        break
    else:
        print("No")

    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int, input().split( ))
cnts = {"A":a,"B":b,"C":c}
ans=[]
s = [input() for _ in range(n)]+[""]
def rnd(ss,t):
    c1 = cnts[ss[0]];c2=cnts[ss[1]]

    if cnts[ss[0]]>cnts[ss[1]]:
        cnts[ss[0]]-=1; cnts[ss[1]]+=1
        ans.append(ss[1])
    elif cnts[ss[1]]>cnts[ss[0]]:
        cnts[ss[0]]+=1; cnts[ss[1]]+=1
        ans.append(ss[0])
    else:
        if ss[0] in s[t]:
            cnts[ss[0]]+=1;cnts[ss[1]]-=1
            ans.append(ss[0])
        else:
            cnts[ss[1]]+=1;cnts[ss[0]]-=1
            ans.append(ss[1])

    for k in cnts:
        if cnts[k]<0:
            print("No")
            exit()
for i in range(n):
    rnd(s[i],i+1)
print("Yes")
for ai in ans:
    print(ai)

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]
	statAB = [0,0,0]
	Q = []
	for i in range(N):
		s = inputStr()
		Q.append(s)
		if s == "AB":
			stat[0] += 1
			stat[1] += 1
			statAB[0] += 1
		elif s == "AC":
			stat[0] += 1
			stat[2] += 1
			statAB[1] += 1
		elif s == "BC":
			stat[1] += 1
			stat[2] += 1
			statAB[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

	targetA = False
	targetB = False
	targetC = False


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

	hon = "Yes"
	ans = []
	for s in Q:
		#print(i)
		#print("{} {} {}".format(A,B,C))
		if s == "AB":
			statAB[0] -=1
			stat[0] -= 1
			stat[1] -= 1
			if delA == True:
				B += 1
				A -= 1
				ans.append("B")
			elif delB == True:
				B -= 1
				A += 1
				ans.append("A")
			elif targetA == True and B-1 >= 0:
				B -= 1
				A += 1
				ans.append("A")
			elif targetB == True and A-1 >= 0:
				B += 1
				A -= 1
				ans.append("B")
			elif statAB[0] < statAB[1]:
				B -= 1
				A += 1
				ans.append("A")
			elif statAB[0] > statAB[1]:
				B += 1
				A -= 1
				ans.append("B")
			elif 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":
			statAB[1] -=1
			stat[0] -= 1
			stat[2] -= 1
			if delA == True:
				C += 1
				A -= 1
				ans.append("C")
			elif delC == True:
				C -= 1
				A += 1
				ans.append("A")
			elif targetA == True and C-1 >= 0:
				C -= 1
				A += 1
				ans.append("A")
			elif targetC == True and A-1 >= 0:
				C += 1
				A -= 1
				ans.append("C")
			elif statAB[1] < statAB[2]:
				C += 1
				A -= 1
				ans.append("C")
			elif statAB[1] > statAB[2]:
				C -= 1
				A += 1
				ans.append("A")
			elif 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":
			statAB[2] -=1
			stat[1] -= 1
			stat[2] -= 1
			if delB == True:
				C += 1
				B -= 1
				ans.append("C")
			elif delC == True:
				C -= 1
				B += 1
				ans.append("B")
			elif targetB == True and C-1 >= 0:
				C -= 1
				B += 1
				ans.append("B")
			elif targetC == True and B-1 >= 0:
				C += 1
				B -= 1
				ans.append("C")
			elif statAB[0] < statAB[2]:
				C -= 1
				B += 1
				ans.append("B")
			elif statAB[0] > statAB[2]:
				C += 1
				B -= 1
				ans.append("C")
			elif 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:
array = list(map(int,input().split()))
a = array[1]
b = array[2]
c = array[3]
ta = a
tb = b
tc = c
array2 = []
array3 = []
flag = 0
for i in range(array[0]):
    tmp = input()
    array2.append(tmp)
    if(tmp == "AB"):
        if(a > b):
            a -= 1
            b += 1
            array3.append("B")
        else:
            a += 1
            b -= 1
            array3.append("A")
    elif(tmp == "AC"):
        if(a > c):
            a -= 1
            c += 1
            array3.append("C")
        else:
            a += 1
            c -= 1
            array3.append("A")
    else:
        if(b > c):
            b -= 1
            c += 1
            array3.append("C")
        else:
            b += 1
            c -= 1
            array3.append("B")
    if(a < 0 or b < 0 or c < 0):
        flag = 1
        break

if(flag == 1):
    a = ta
    b = tb
    c = tc
    for i in range(array[0]):
        try:
            tmp = array2[i]
        except:
            tmp = input()
        if(tmp == "AB"):
            if(a >= b):
                a -= 1
                b += 1
                array3.append("B")
            else:
                a += 1
                b -= 1
                array3.append("A")
        if(tmp == "AC"):
            if(a >= c):
                a -= 1
                c += 1
                array3.append("C")
            else:
                a += 1
                c -= 1
                array3.append("A")
        else:
            if(b >= c):
                b -= 1
                c += 1
                array3.append("C")
            else:
                b += 1
                c -= 1
                array3.append("B")
        if(a < 0 or b < 0 or c < 0):
            print("No")
            break
else:
    print("Yes")
    for i in array3:
        print(i)

Submission status: Wrong Answer

Solution code:
def answer(x):
    print("Yes")
    for i in range(n):
        if s[i] == "AB":
            if x[0][0]:
                print("B")
                x[0][0] -= 1
            else:
                print("A")
        elif s[i] == "AC":
            if x[1][0]:
                print("C")
                x[1][0] -= 1
            else:
                print("A")
        else:
            if x[2][0]:
                print("C")
                x[2][0] -= 1
            else:
                print("B")
    exit()

n,a,b,c = map(int,input().split())
s = [str(input()) for i in range(n)]
cnt = [0 for i in range(3)]
for ss in s:
    if ss == "AB":
        a += 1
        b += 1
        cnt[0] += 1
    elif ss == "AC":
        a += 1
        c += 1
        cnt[1] += 1
    else:
        b += 1
        c += 1
        cnt[2] += 1
a,b,c = a//2,b//2,c//2
all = min(min(cnt),min(a,b,c))
a,b,c = a-all,b-all,c-all
for i in range(3):
    cnt[i] -= all
  # cnt=["AB","AC","BC"]

#"AB"=0
if cnt[0] == 0:
        ACdeA = min(cnt[1],a)
        BCdeB = min(cnt[2],b)
        ACdeC = cnt[1]-ACdeA
        BCdeC = cnt[2]-BCdeB
        if ACdeC + BCdeC <= c:
            answer([[all,0],[ACdeA,all+ACdeC],[all+BCdeB,BCdeC]])
if cnt[1] == 0:
        ABdeA = min(cnt[0],a)
        BCdeC = min(cnt[2],c)
        ABdeB = cnt[0]-ABdeA
        BCdeB = cnt[2]-BCdeC
        if ABdeB + BCdeB <= b:
            answer([[all+ABdeA,ABdeB],[0,all],[all+BCdeB,BCdeC]])
if cnt[2] == 0:
        ABdeB = min(cnt[0],b)
        ACdeC = min(cnt[1],c)
        ABdeA = cnt[0]-ABdeB
        ACdeA = cnt[1]-ACdeC
        if ABdeA + ACdeA <= a:
            answer([[all+ABdeA,ABdeB],[ACdeA,ACdeC+all],[all,0]])
if a == 0:
    ABdeB = cnt[0]
    ACdeC = cnt[1]
    b -= ABdeB
    c -= ACdeC
    if b >= 0 and c >= 0:
        BCdeB = min(b,cnt[2])
        BCdeC = cnt[2]-BCdeB
        if BCdeC <= c:
             answer([[all,ABdeB],[0,ACdeC+all],[all+BCdeB,BCdeC]])
    b += ABdeB
    c += ACdeC
if b == 0:
    ABdeA = cnt[0]
    BCdeC = cnt[2]
    a -= ABdeA
    c -= BCdeC
    if a >= 0 and c >= 0:
        ACdeA = min(a,cnt[1])
        ACdeC = cnt[1]-ACdeA
        if ACdeC <= c:
            answer([[all+ABdeA,0],[ACdeA,ACdeC+all],[all,BCdeC]])
    a += ABdeA
    c += BCdeC
if c == 0:
    ACdeA = cnt[1]
    BCdeB = cnt[2]
    a -= ACdeA
    b -= BCdeB
    if a >= 0 and b >= 0:
        ABdeA = min(a,cnt[0])
        ABdeB = cnt[0]-ABdeA
        if ABdeB <= b:
            answer([[all+ABdeA,ABdeB],[ACdeA,all],[BCdeB+all,0]])
print("No")

Submission status: Wrong Answer

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



def s3(abc_dict):
    
    ans = []
    for _ in range(N):
        xy = input()
        x = xy[0] 
        y = xy[1]
        if abc_dict[x]==0 and abc_dict[y]==0:
            return False, []
        elif abc_dict[x]>=abc_dict[y]:
            abc_dict[x] -= 1
            abc_dict[y] += 1
            ans.append(y)
        else:
            abc_dict[y] -= 1
            abc_dict[x] += 1
            ans.append(x)
    return True, ans

def s2(abc_dict):

    ans = []
    flag = True
    q_list = []
    for _ in range(N):
        q_list.append(input())

    for i in range(N):
        xy = q_list[i]
        x = xy[0]
        y = xy[1]

        if (abc_dict[x]>=1 and abc_dict[y]==0):
            abc_dict[x]-=1
            abc_dict[y]+=1
            ans.append(y)
        elif (abc_dict[y]>=1 and abc_dict[x]==0):
            abc_dict[y]-=1
            abc_dict[x]+=1
            ans.append(y)
        elif  (abc_dict[x]==1 and abc_dict[y]==1):
            if i==N-1:
                ans.append(x)
            else:
                next_xy = q_list[i+1]
                if xy==next_xy: # 次も同じなのでどちらでもいい
                    abc_dict[x]+=1
                    abc_dict[y]-=1
                    ans.append(x)
                elif (x in next_xy):
                    abc_dict[x]+=1
                    abc_dict[y]-=1
                    ans.append(x)
                else: # 次にyが含まれる
                    abc_dict[y]+=1
                    abc_dict[x]-=1
                    ans.append(y)
        else:
            return False, []
    return True,ans

def s1(abc_dict):
    
    ans = []
    flag = True
    for _ in range(N):
        xy = input()
        x = xy[0] 
        y = xy[1]
        if abc_dict[x]==1:
            abc_dict[x]-=1
            abc_dict[y]+=1
            ans.append(y)
        elif abc_dict[y]==1:
            abc_dict[y]-=1
            abc_dict[x]+=1
            ans.append(x)
        else:
            flag = False
            return flag,[]
    return flag, ans
    
def s0(abc_dict):
    return False, []

abc_dict = {"A":A,"B":B,"C":C}
sumABC = A+B+C
if sumABC>=3:
    flag, ans = s3(abc_dict)
elif sumABC==2:
    flag, ans = s2(abc_dict)
elif sumABC==1:
    flag, ans = s1(abc_dict)
elif sumABC==0:
    flag, ans = False, []

if flag:
    print('Yes')
    for i in range(N):
        print(ans[i])
else:
    print("No")

Submission status: Wrong Answer

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



N, A, B, C = map(int, input().split())
s = []
for i in range(N):
    s.append(input())
a = ans(N, A, B, C, s)
if not a:
    print("No")
else:
    print("Yes")
    for i in a:
        print(i)

Submission status: Wrong Answer

Solution code:
def answer(x):
    print("Yes")
    for i in range(n):
        if s[i] == "AB":
            if x[0][0]:
                print("B")
                x[0][0] -= 1
            else:
                print("A")
        elif s[i] == "AC":
            if x[1][0]:
                print("C")
                x[1][0] -= 1
            else:
                print("A")
        else:
            if x[2][0]:
                print("C")
                x[2][0] -= 1
            else:
                print("B")
    exit()

n,a,b,c = map(int,input().split())
s = [str(input()) for i in range(n)]
cnt = [0 for i in range(3)]
for ss in s:
    if ss == "AB":
        a += 1
        b += 1
        cnt[0] += 1
    elif ss == "AC":
        a += 1
        c += 1
        cnt[1] += 1
    else:
        b += 1
        c += 1
        cnt[2] += 1
a,b,c = a//2,b//2,c//2
all = min(min(cnt),min(a,b,c))
a,b,c = a-all,b-all,c-all
for i in range(3):
    cnt[i] -= all
  # cnt=["AB","AC","BC"]
print(a,b,c)

#"AB"=0
if cnt[0] == 0:
        ACdeA = min(cnt[1],a)
        BCdeB = min(cnt[2],b)
        ACdeC = cnt[1]-ACdeA
        BCdeC = cnt[2]-BCdeB
        if ACdeC + BCdeC <= c:
            answer([[all,0],[ACdeA,all+ACdeC],[all+BCdeB,BCdeC]])
if cnt[1] == 0:
        ABdeA = min(cnt[0],a)
        BCdeC = min(cnt[2],c)
        ABdeB = cnt[0]-ABdeA
        BCdeB = cnt[2]-BCdeC
        if ABdeB + BCdeB <= b:
            answer([[all+ABdeA,ABdeB],[0,all],[all+BCdeB,BCdeC]])
if cnt[2] == 0:
        ABdeB = min(cnt[0],b)
        ACdeC = min(cnt[1],c)
        ABdeA = cnt[0]-ABdeB
        ACdeA = cnt[1]-ACdeC
        if ABdeA + ACdeA <= a:
            answer([[all+ABdeA,ABdeB],[ACdeA,ACdeC+all],[all,0]])
if a == 0:
    ABdeB = cnt[0]
    ACdeC = cnt[1]
    b -= ABdeB
    c -= ACdeC
    if b >= 0 and c >= 0:
        BCdeB = min(b,cnt[2])
        BCdeC = cnt[2]-BCdeB
        if BCdeC <= c:
             answer([[all,ABdeB],[0,ACdeC+all],[all+BCdeB,BCdeC]])
    b += ABdeB
    c += ACdeC
if b == 0:
    ABdeA = cnt[0]
    BCdeC = cnt[2]
    a -= ABdeA
    c -= BCdeC
    if a >= 0 and c >= 0:
        ACdeA = min(a,cnt[1])
        ACdeC = cnt[1]-ACdeA
        if ACdeC <= c:
            answer([[all+ABdeA,0],[ACdeA,ACdeC+all],[all,BCdeC]])
    a += ABdeA
    c += BCdeC
if c == 0:
    ACdeA = cnt[1]
    BCdeB = cnt[2]
    a -= ACdeA
    b -= BCdeB
    if a >= 0 and b >= 0:
        ABdeA = min(a,cnt[0])
        ABdeB = cnt[0]-ABdeA
        if ABdeB <= b:
            answer([[all+ABdeA,ABdeB],[ACdeA,all],[BCdeB+all,0]])
print("No")

Submission status: Wrong Answer

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

val=[a,b,c]
flag=True
ansls=list()

freq=[0]*3

for i in range(n):
    s1=ord(op[i][0])-ord('A')
    s2=ord(op[i][1])-ord('A')
    freq[s1]+=1
    freq[s2]+=1

for i in range(n):
    s1=ord(op[i][0])-ord('A')
    s2=ord(op[i][1])-ord('A')
    if val[s1]+freq[s1]>val[s2]+freq[s2]:
        ansls.append(s2)
        val[s2]+=1
        val[s1]-=1
    else:
        ansls.append(s1)
        val[s1]+=1
        val[s2]-=1
    freq[s1]-=1
    freq[s2]=-1
    if min(val)<0:
        flag=False
        break

if flag:
    print('Yes')
    for i in range(n):
        print(chr(ansls[i]+ord('A')))
else:
    print('No')

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("No")
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("No")
    flag = 0
    ans = []

Submission status: Wrong Answer

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

A=[]
ans='Yes'
for i in range(n):
    s=input()
    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 ans=='Yes':
    print(ans)
    for a in A:
        print(a)
else:
    print(ans)