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

	targetA = False
	targetB = False
	targetC = False
	AA = A - stat[0]
	BB = B - stat[1]
	CC = C - stat[2]
	tmp = min(AA,BB,CC)
	if AA == tmp:
		targetA = True
	if BB == tmp:
		targetB = True
	if CC == tmp:
		targetC = 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")
			elif delB == True:
				B -= 1
				A += 1
				ans.append("A")
			elif targetA == True:
				B -= 1
				A += 1
				ans.append("A")
			elif targetB == True:
				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":
			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:
				C -= 1
				A += 1
				ans.append("A")
			elif targetC == True:
				C += 1
				A -= 1
				ans.append("C")
			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":
			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:
				C -= 1
				B += 1
				ans.append("B")
			elif targetC == True:
				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:
n,a,b,c = map(int, input().split())
ans = []
for i in range(n):
    x = input()
    if (a==0):
            if (b==0):
                if (x =="AB"):
                    print("No")
                    exit()
            elif (c==0):
                if (x=="AC"):
                    print("No")
                    exit()
    if (b==0):
        if (c==0):
            if (x=="BC"):
                print("No")
                exit()    
    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("Yes")            
print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
def play():
    N, A, B, C = map(int, input().split())
    p = []
    for _ in range(N):
        s = input()
        if s == "AB":
            if A + B == 0:
                print("No")
                return 0
            elif A >= B:
                A -= 1
                B += 1
                p.append('B')
            else:
                A += 1
                B -= 1
                p.append('A')
        if s == "AC":
            if A + C == 0:
                print("No")
                return 0
            elif A >= C:
                A -= 1
                C += 1
                p.append('C')
            else:
                A += 1
                C -= 1
                p.append('A')
        if s == "BC":
            if B + C == 0:
                print("No")
                return 0
            elif B >= C:
                B -= 1
                C += 1
                p.append('C')
            else:
                B += 1
                C -= 1
                p.append('B')
    print('Yes')
    for i in range(N):
        print(p[i])
    return 0


if __name__ == '__main__':
    play()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
result = []
def calc(a, b, c, i):
    if i >= N:
        if a < 0 or b < 0 or c < 0:
            return False
        return True
    command = s[i]
    if command == "AB":
        if calc(a+1, b-1, c, i+1):
            result.append("A")
            return True
        if calc(a-1, b+1, c, i+1):
            result.append("B")
            return True
        return False
    elif command == "AC":
        if calc(a+1, b, c-1, i+1):
            result.append("A")
            return True
        if calc(a-1, b, c+1, i+1):
            result.append("C")
            return True
        return False
    elif command == "BC":
        if calc(a, b-1, c+1, i+1):
            result.append("C")
            return True
        if calc(a, b+1, c-1, i+1):
            result.append("B")
            return True
        return False
def main():
    global N, s
    N, A, B, C = map(int, input().split())
    s = [input() for _ in range(N)]
    if calc(A, B, C, 0):
        print("Yes")
        for c in result[::-1]:
            print(c)
    else:
        print("No")
main()

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
k = 0
if a == 0:
  k += 1
if b == 0:
  k += 1
if c == 0:
  k += 1

order = [0]*n
answer = [0]*n
for i in range(n):
  s = input()
  if s == "AB":
    order[i] = 1
  elif s == "BC":
    order[i] = 2
  else:
    order[i] = 3
ans = True
if k == 3:
  ans = False
if k == 2:
  for i in range(n):
    key =  order[i]
    if key == 1:
      if a == 0 and b == 0:
        ans = False
        break
      elif a == 0:
        a += 1
        b -= 1
        answer[i] = "A"
      elif b == 0:
        answer[i] = "B"
        a -= 1
        b += 1
      elif i < n-1 and a == 1 and (order[i+1] == 1 or order[i+1] == 3):
        a += 1
        b -= 1
        answer[i] = "A"
      elif i < n-1 and b == 1 and (order[i+1] == 2 or order[i+1] == 1):
        b += 1
        a -= 1
        answer[i] = "B"
      else:
        if a < b:
          a += 1
          b -= 1
          answer[i] = "A"
        else:
          a -= 1
          b += 1
          answer[i] = "B"
    if key == 2:
      if c == 0 and b == 0:
        ans = False
        break
      elif b == 0:
        b += 1
        c -= 1
        answer[i] = "B"
      elif c == 0:
        c += 1
        b -= 1
        answer[i] = "C"
      elif i < n-1 and c == 1 and (order[i+1] == 2 or order[i+1] == 3):
        c += 1
        b -= 1
        answer[i] = "C"
      elif i < n-1 and b == 1 and (order[i+1] == 1 or order[i+1] == 3):
        b += 1
        c -= 1
        answer[i] = "B"
      else:
        if b < c:
          b += 1
          c -= 1
          answer[i] = "B"
        else:
          b -= 1
          c += 1
          answer[i] = "C"
    if key == 3:
      if a == 0 and c == 0:
        ans = False
        break
      elif a == 0:
        a += 1
        c -= 1
        answer[i] = "A"
      elif c == 0:
        c += 1
        a -= 1
        answer[i] = "C"
      elif i < n-1 and a == 1 and (order[i+1] == 1 or order[i+1] == 3):
        a += 1
        b -= 1
        answer[i] = "A"
      elif i < n-1 and b == 1 and (order[i+1] == 2 or order[i+1] == 3):
        b += 1
        a -= 1
        answer[i] = "B"
      elif i < n-1 and a == 1 and (order[i+1] == 1 or order[i+1] == 3):
        a += 1
        c -= 1
        answer[i] = "A"
      elif i < n-1 and c == 1 and (order[i+1] == 2 or order[i+1] == 3):
        c += 1
        a -= 1
        answer[i] = "C"
      else:
        if a < c:
          a += 1
          c -= 1
          answer[i] = "A"
        else:
          a -= 1
          c += 1
          answer[i] = "C"
if k <= 1:
  for i in range(n):
    if order[i] == 1:
      if a == 0:
        a += 1
        b -= 1
        answer[i] = "A"
      elif b == 0:
        b += 1
        a -= 1
        answer[i] = "B"
      else:
        if a < b:
          a += 1
          b -= 1
          answer[i] = "A"
        else:
          answer[i] = "B"
          b += 1
          a -= 1
    if order[i] == 2:
      if c == 0:
        c += 1
        b -= 1
        answer[i] = "C"
      elif b == 0:
        b += 1
        c -= 1
        answer[i] = "B"
      else:
        if c < b:
          c += 1
          b -= 1
          answer[i] = "C"
        else:
          answer[i] = "B"
          b += 1
          c -= 1  
    if order[i] == 3:
      if a == 0:
        a += 1
        c -= 1
        answer[i] = "A"
      elif c == 0:
        c += 1
        a -= 1
        answer[i] = "C"
      else:
        if a < c:
          a += 1
          c -= 1
          answer[i] = "A"
        else:
          answer[i] = "C"
          c += 1
          a -= 1
if ans:
  print("Yes")
  for v in answer:
    print(v)
else:
  print("No")

Submission status: Wrong Answer

Solution code:

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

SL = []
nonZero = 0
nonZero += A>0
nonZero += B>0
nonZero += C>0

for i in range(N):
    s = input()
    SL.append(s)


def op2(f, t):
    global A, B, C
    # print(A, B, C)
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1

def op(f, t):
    global A, B, C
    # print(A, B, C)
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1
    print(t)

def show():
    print("Yes")
    global A, B, C
    for s in SL:
        if s=="AB":
            if A > B:
                op("A", "B")
            else: op("B", "A")
        elif s=="BC":
            if B >C:
                op("B", "C")
            else: op("C", "B")
        elif s=="AC":
            if A >C:
                op("A", "C")
            else: op("C", "A")


def fight():
    global A, B, C
    pA = A
    pB = B
    pC = C
    for s in SL:
        if s=="AB":
            if A > B:
                op2("A", "B")
            else: op2("B", "A")
        elif s=="BC":
            if B >C:
                op2("B", "C")
            else: op2("C", "B")
        elif s=="AC":
            if A >C:
                op2("A", "C")
            else: op2("C", "A")
        # print("fight: ", A, B, C)
        if min(A, B, C) == -1:
            print("No")
            exit()
    else:
        A = pA
        B = pB
        C = pC
        show()






# main


if nonZero >=2:
    history = []
    for s in SL:
        if s=="AB":
            if A > B:
                op("A", "B")
            else: op("B", "A")
        elif s=="BC":
            if B >C:
                op("B", "C")
            else: op("C", "B")
        elif s=="AC":
            if A >C:
                op("A", "C")
            else: op("C", "A")


elif nonZero == 1:
    if max(A, B, C) >= 2:
        s = SL[0]
        if A!=0:
            if s == "BC":
                print("No")
                exit()
        if B!=0:
            if s == "AC":
                print("No")
                exit()
        if C!=0:
            if s == "AB":
                print("No")
                exit()
        show()
    else:
        fight()
else:
    print("No")

Submission status: Wrong Answer

Solution code:
# coding: utf-8

N, a, b, c = map(int,input().split())
A = []
flg = True
for i in range(N):
    s = input()
    if s == 'AB':
        if a >= b:
            a -= 1
            b += 1
            A.append('B')
        else:
            a += 1
            b -= 1
            A.append('A')
        if a < 0 or b < 0:
            flg = False
            break
    elif s == 'BC':
        if b >= c:
            b -= 1
            c += 1
            A.append('C')
        else:
            b += 1
            c -= 1
            A.append('B')
        if b < 0 or c < 0:
            flg = False
            break
    else:
        if a >= c:
            a -= 1
            c += 1
            A.append('C')
        else:
            a += 1
            c -= 1
            A.append('A')
        if a < 0 or c < 0:
            flg = False
            break

if flg:
    print('Yes')
    for a in A:
        print(a)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import bisect as bi
import collections as cc
I=lambda:list(map(int,input().split()))
f=cc.defaultdict(int)
n,a,b,c=I()
for i in range(n):
    s=input()
    if 'A' in s:
        a-=1
    if 'B' in s:
        b-=1
    if 'S' in s:
        c-=1
an=0
if a<0:
    an+=1
if b<0:
    an+=1
if c<0:
    an+=1
if an>1:
    print('No')

Submission status: Wrong Answer

Solution code:

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


def score(start, queries):
    def rec(nums, d):
        if d >= len(queries):
            return min(nums)
        i,j = queries[d]
        nums[i] += 1
        nums[j] -= 1
        x = rec(nums, d+1)
        nums[i] -= 2
        nums[j] += 2
        y = rec(nums, d+1)
        nums[i] += 1
        nums[j] -= 1
        return max(x,y)
    return rec(start, 0)



Q = []
for _ in range(N):
    s = input()
    if s == 'AB':
        i,j = 0,1
    elif s == 'BC':
        i,j = 1,2
    else:
        i,j = 2,0

    Q.append((i,j))

nums = [A,B,C]
res = []
flag = True
for q,(i,j) in enumerate(Q):
    
    nums[i] += 1
    nums[j] -= 1
    x = score(nums, Q[q:min(q+3, len(Q))])
    nums[i] -= 2
    nums[j] += 2
    y = score(nums, Q[q:min(q+3, len(Q))])
    if x > y:
        res.append(i)
        nums[i] += 1
        nums[j] -= 1
    else:
        res.append(j)
        nums[i] -= 1
        nums[j] += 1
    if max(x,y) < 0:
        flag = False
        break


if not flag:
    print('No')
else:
    print('Yes')
    res = tuple(str(chr(v+ord('A'))) for v in res)
    print(*res,sep='\n')

Submission status: Wrong Answer

Solution code:

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

SL = []
nonZero = 0
nonZero += A>0
nonZero += B>0
nonZero += C>0

for i in range(N):
    s = input()
    SL.append(s)


def op2(f, t):
    global A, B, C
    # print(A, B, C)
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1

def op(f, t):
    global A, B, C
    # print(A, B, C)
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1
    print(t, A, B, C)

def show():
    print("Yes")
    global A, B, C
    for s in SL:
        if s=="AB":
            if A > B:
                op("A", "B")
            else: op("B", "A")
        elif s=="BC":
            if B >C:
                op("B", "C")
            else: op("C", "B")
        elif s=="AC":
            if A >C:
                op("A", "C")
            else: op("C", "A")


def fight():
    global A, B, C
    pA = A
    pB = B
    pC = C
    for s in SL:
        if s=="AB":
            if A > B:
                op2("A", "B")
            else: op2("B", "A")
        elif s=="BC":
            if B >C:
                op2("B", "C")
            else: op2("C", "B")
        elif s=="AC":
            if A >C:
                op2("A", "C")
            else: op2("C", "A")
        # print("fight: ", A, B, C)
        if min(A, B, C) == -1:
            print("No")
            exit()
    else:
        A = pA
        B = pB
        C = pC
        show()






# main


if nonZero >=2:
    history = []
    for s in SL:
        if s=="AB":
            if A > B:
                op("A", "B")
            else: op("B", "A")
        elif s=="BC":
            if B >C:
                op("B", "C")
            else: op("C", "B")
        elif s=="AC":
            if A >C:
                op("A", "C")
            else: op("C", "A")


elif nonZero == 1:
    if max(A, B, C) >= 2:
        s = SL[0]
        if A!=0:
            if s == "BC":
                print("No")
                exit()
        if B!=0:
            if s == "AC":
                print("No")
                exit()
        if C!=0:
            if s == "AB":
                print("No")
                exit()
        show()
    else:
        fight()
else:
    print("No")

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
ans={"A":A,"B":B,"C":C}
ans1=[]
s=[]
flag=0
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:
        flag=1
if flag==1:
    print("No")
    exit()
print("Yes")
for a in ans1:
    print(a)

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n, a, b, c = map(int, input().split())
ans = deque()
for i in range(n):
    s = input()
    if s == "AB":
        if a == b == 0:
            print("No")
            sys.exit()
        else:
            if a < b:
                a += 1
                b -= 1
                ans.append('A')
            else:
                a -= 1
                b += 1
                ans.append('B')
    elif s == "BC":
        if b == c == 0:
            print("No")
            sys.exit()
        else:
            if b < c:
                b += 1
                c -= 1
                ans.append('B')
            else:
                b -= 1
                c += 1
                ans.append('C')
    else:
        if c == a == 0:
            print("No")
            sys.exit()
        else:
            if c < a:
                c += 1
                a -= 1
                ans.append('C')
            else:
                c -= 1
                a += 1
                ans.append('A')

print("Yes")
while len(ans):
    print(ans.popleft())

Submission status: Wrong Answer

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

for i in range(n):
    s = input()
    if s == "AB":
        if a >= b:
            a -= 1
            b += 1
            ans.append("B")
        else:
            a += 1
            b -= 1
            ans.append("A")
    if s == "AC":
        if a >= c:
            a -= 1
            c += 1
            ans.append("C")
        else:
            a += 1
            c -= 1
            ans.append("A")
    if s == "BC":
        if c >= b:
            c -= 1
            b += 1
            ans.append("B")
        else:
            c += 1
            b -= 1
            ans.append("C")
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()
print("Yes")
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
if s.count(0) <= 1 and sum(s) >= 3:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
    print('Yes')
    print('\n'.join(ans))
elif s.count(0) == 1:
    now = s.index(0)
    i = 0
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        for j in range(3):
            if j not in [now,T[i]]:ans[i] = ABC[j]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
from collections import Counter
s=[input() for _ in range(n)]
# 多分だめ
# 2つを枯らさなければok?
# 2つが0のときに指令が来たら失敗
# たいてい大丈夫そう
# sstr="".join(/s)
# print(sstr)

# sc=Counter(sstr)
# print(sc)

# if a+b+c<3:
#     print("No")
#     exit()
    
# print("Yes")

# シミュレーション
# def check(s):
A="A";B="B";C="C"
ans=[]

for i in s:
    # check(i)
    
    if i=="AB":
        if a<=1:
            a+=1
            b-=1
            ans+=A,
        else:
            a-=1
            b+=1
            ans+=B,
    if i=="BC":
        if b<=1:
            b+=1
            c-=1
            ans+=B,
            # print(B)
        else:
            b-=1
            c+=1
            ans+=C,
            # print(C)
    if i=="AC":
        if a<=1:
            a+=1
            c-=1
            # print(A)
            ans+=A,
        else:
            a-=1
            c+=1
            ans+=C,
            # print(C)
            
            
    if min(a,b,c)==-1:
        print("No")
        exit()
print("Yes")
for i in ans:
    print(i)

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
    if c_ac < 0:continue
    
    b_bc = max(0, (bc - b_rest + 1)//2)
    c_bc = bc - b_bc
    if c_bc < 0:continue
    
    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:
import sys
input = sys.stdin.readline

N, A, B, C = map(int, input().split())
s = [input().rstrip() for _ in range(N)]
ans = []
for i in range(N-1):
    if s[i] == 'AB':
        if A == B == 0:
            print('No')
            exit()
        if A == B == 1 and C == 0 and s[i+1] != 'AB':
            if s[i+1] == 'BC':
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
        else:
            if A > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
    if s[i] == 'BC':
        if B == C == 0:
            print('No')
            exit()
        if B == C == 1 and A == 0 and s[i+1] != 'BC':
            if s[i+1] == 'AB':
                B += 1
                C -= 1
                ans.append('B')
            else:
                B -= 1
                C += 1
                ans.append('C')
        else:
            if B > C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
    if s[i] == 'AC':
        if A == C == 0:
            print('No')
            exit()
        if A == C == 1 and B == 0 and s[i+1] != 'AC':
            if s[i+1] == 'AB':
                A += 1
                B -= 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
        else:
            if A > C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')
print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
def mp():return map(int,input().split())
def lmp():return list(map(int,input().split()))

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