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:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

Q, A, B, C = map(int, readline().split())
N = A + B + C
S = read().split()

S = tuple(0 if s == b'BC' else 1 if s == b'AC' else 2 for s in S)

def solve_1(N, A, B, C, S):
    nums = [A, B, C]
    for s in S:
        if nums[s] == N:
            raise Exception
        i, j = (0, 1) if s == 2 else (0, 2) if s == 1 else (1, 2)
        if nums[i] < nums[j]:
            i, j = j, i
        yield j
        nums[j] += 1
        nums[i] -= 1

def solve_2(N, A, B, C, S):
    nums = [A, B, C]
    s = S[0]
    # 初手で死亡
    if nums[s] == N:
        raise Exception
    # あとはできる。きわどいときは 2 手読み
    S = S + (0, )
    for s, t in zip(S, S[1:]):
        i, j = (0, 1) if s == 2 else (0, 2) if s == 1 else (1, 2)
        if (nums[i], nums[j]) != (1, 1):
            if nums[i] < nums[j]:
                i, j = j, i
        elif s != t:
            i, j = t, 3 - s - t
        yield j
        nums[j] += 1
        nums[i] -= 1

def large(N, A, B, C, S):
    nums = [A, B, C]
    for s in S:
        if nums[s] == N:
            raise Exception
        i, j = (0, 1) if s == 2 else (0, 2) if s == 1 else (1, 2)
        if nums[i] < nums[j]:
            i, j = j, i
        yield j
        nums[j] += 1
        nums[i] -= 1
        print(s, nums)

N = 2
A, B, C = 1, 1, 0
S = (0, 1, 2, 1, 2, 2, 2, 1, 2, 1)

f = solve_1 if N == 1 else solve_2 if N == 2 else large

try:
    cmds = list(f(N, A, B, C, S))
    print('Yes')
    answer = ('ABC' [i] for i in cmds)
    print('\n'.join(answer))
except Exception:
    print('No')

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import bisect
import os

import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N, A, B, C = list(map(int, sys.stdin.buffer.readline().split()))
S = [sys.stdin.buffer.readline().decode().rstrip() for _ in range(N)]


def get_idx(c):
    ret = []
    for i, s in enumerate(S):
        if c == s:
            ret.append(i)
    return ret


ab_idx = get_idx('AB')
ac_idx = get_idx('AC')
bc_idx = get_idx('BC')

# 0,0 にしなければいい
a, b, c = A, B, C
ans = []
ok = True
for i, s in enumerate(S):
    pa, pb, pc = a, b, c
    if s == 'AB':
        if a == b == 0:
            ok = False
            break
        elif a > b:
            a -= 1
            b += 1
        elif a < b:
            a += 1
            b -= 1
        elif a == b == 1:
            ai = bisect.bisect_left(ac_idx, i)
            bi = bisect.bisect_left(bc_idx, i)
            if ai < bi:
                a += 1
                b -= 1
            else:
                a -= 1
                b += 1
        else:
            a -= 1
            b += 1
    if s == 'AC':
        if a == c == 0:
            ok = False
            break
        elif a > c:
            a -= 1
            c += 1
        elif a < c:
            a += 1
            c -= 1
        elif a == c == 1:
            ai = bisect.bisect_left(ab_idx, i)
            ci = bisect.bisect_left(bc_idx, i)
            if ai < ci:
                a += 1
                c -= 1
            else:
                a -= 1
                c += 1
        else:
            a -= 1
            c += 1
    if s == 'BC':
        if c == b == 0:
            ok = False
            break
        elif c > b:
            c -= 1
            b += 1
        elif c < b:
            c += 1
            b -= 1
        elif c == b == 1:
            ci = bisect.bisect_left(ac_idx, i)
            bi = bisect.bisect_left(ab_idx, i)
            if ci < bi:
                c += 1
                b -= 1
            else:
                c -= 1
                b += 1
        else:
            c -= 1
            b += 1

    if pa < a:
        ans.append('A')
    elif pb < b:
        ans.append('B')
    elif pc < c:
        ans.append('C')
    else:
        assert False
if ok:
    print('Yes')
else:
    print('No')
    exit()
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:

x=input().split(" ")
number=int(x[0])
a=int(x[1])
b=int(x[2])
c=int(x[3])
i=1
result=""
while i<=number:
    i=i+1
    temp=input()
    if temp=="AB":
        if a<=b:
            a+=1
            b-=1
            result+="A\n"
        else:
            a-=1
            b+=1
            result += "B\n"
    elif temp=="BC":
        if b<=c:
            b+=1
            c-=1
            result += "B\n"
        else:
            b-=1
            c+=1
            result += "C\n"
    else:
        if c<=a:
            c+=1
            a-=1
            result += "C\n"
        else:
            c-=1
            a+=1
            result += "A\n"
if a<0 or b<0 or c<0:
    print("No")
else:
    print("YES")
    print(result)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(10**6)
buff_readline = sys.stdin.buffer.readline
readline = sys.stdin.readline

INF = 2**62-1


def read_int():
    return int(buff_readline())


def read_int_n():
    return list(map(int, buff_readline().split()))


def read_float():
    return float(buff_readline())


def read_float_n():
    return list(map(float, buff_readline().split()))


def read_str():
    return readline().strip()


def read_str_n():
    return readline().strip().split()

def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, A, B, C, S):
    dp = {}
    dp[(A, B, C)] = []
    for s in S:
        ndp = {}
        for k in dp:
            t = []
            if 'A' in s:
                t.append(0)
            if 'B' in s:
                t.append(1)
            if 'C' in s:
                t.append(2)

            for i, j in permutations(t, r=2):
                a = [0] * 3
                a[i] += 1
                a[j] -= 1
                if k[j] > 0:
                    l = dp[k][:]
                    l.append(i)
                    ndp[(k[0]+a[0], k[1]+a[1], k[2]+a[2])] = l
        dp = ndp
        if not dp:
            print('No')
    for v in dp.values():
        for i in v:
            if i == 0:
                print('A')
            elif i == 1:
                print('B')
            else:
                print('C')
        return


def main():
    N, A, B, C = read_int_n()
    S = [read_str() for _ in range(N)]
    (slv(N, A, B, C, S))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c=[int(x) for x in input().split()]

ans=""
for i in range(n):
  s=input()
  if s=="AB":
    if a==0 and b==0:
      print("No")
      exit()
    elif a>b:
      ans+="B\n"
      a-=1
      b+=1
    else:
      ans+="A\n"
      a+=1
      b-=1
      
  elif s=="AC":
    if a==0 and c==0:
      print("No")
      exit()
    elif a>c:
      ans+="C\n"
      a-=1
      c+=1
    else:
      ans+="A\n"
      a+=1
      c-=1
  elif s=="BC":
    if c==0 and b==0:
      print("No")
      exit()
    elif c>b:
      ans+="B\n"
      c-=1
      b+=1
    else:
      ans+="C\n"
      c+=1
      b-=1
print("Yes")
print(ans,end="")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


N, A, B, C = map(int, input().split())
Ss = [input().rstrip() for _ in range(N)]

P = sorted([A, B, C])
if P[1] == 0:
    print("No")
else:
    print("Yes")
    for i, S in enumerate(Ss):
        if S == "AC":
            if A > C:
                T = "C"
                C += 1
                A -= 1
            elif A < C:
                T = "A"
                A += 1
                C -= 1
            elif i != N-1:
                if "A" in Ss[i+1]:
                    T = "A"
                    A += 1
                    C -= 1
                else:
                    T = "C"
                    A -= 1
                    C += 1
            else:
                T = "A"
                A += 1
                C -= 1
        elif S == "AB":
            if A > B:
                T = "B"
                B += 1
                A -= 1
            elif A < B:
                T = "A"
                A += 1
                B -= 1
            elif i != N-1:
                if "A" in Ss[i+1]:
                    T = "A"
                    A += 1
                    B -= 1
                else:
                    T = "B"
                    A -= 1
                    B += 1
            else:
                T = "A"
                A += 1
                C -= 1
        else:
            if B > C:
                T = "C"
                C += 1
                B -= 1
            elif B < C:
                T = "B"
                B += 1
                C -= 1
            elif i != N-1:
                if "B" in Ss[i+1]:
                    T = "B"
                    B += 1
                    C -= 1
                else:
                    T = "C"
                    B -= 1
                    C += 1
            else:
                T = "B"
                B += 1
                C -= 1
        print(T)

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    S = [input() for _ in range(N)]
    res = []
    for s in S:
        if s == "AB":
            if A == 0 and B == 0:
                print("No")
                return 1
            if A >= B:
                A -= 1
                B += 1
                res.append("B")
            else:
                A += 1
                B -= 1
                res.append("A")
        elif s == "AC":
            if A == 0 and C == 0:
                print("No")
                return 1
            if A >= C:
                A -= 1
                C += 1
                res.append("C")
            else:
                A += 1
                C -= 1
                res.append("A")

        elif s == "BC":
            if B == 0 and C == 0:
                print("No")
                return 1
            if B >= C:
                B -= 1
                C += 1
                res.append("C")
            else:
                B += 1
                C -= 1
                res.append("B")
        else:
            print("ERROR")
    print("Yes")
    print("\n".join(map(str, res)))


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()
	hon = "Yes"
	ans = []
	for i in range(N):
		s = inputStr()
		if s == "AB":
			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 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 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:
N,A,B,C = list(map(int, input().split()))

q = []
ans = 'Yes'
for n in range(N):
    s = input()
    
    if s == 'AB':
        if A ==0 and B==0:
            ans = 'No'
            break
        elif A > B:
            A -= 1
            B += 1
            q.append('B')
        else:
            A += 1
            B -= 1
            q.append('A')
            
    elif s == 'AC':
        if A ==0 and C==0:
            ans = 'No'
            break
        elif A > C:
            A -= 1
            C += 1
            q.append('C')
        else:
            A += 1
            C -= 1
            q.append('A')
                    
    elif s == 'BC':
        if C ==0 and B==0:
            ans = 'No'
            break
        elif C > B:
            C -= 1
            B += 1
            q.append('B')
        else:
            C += 1
            B -= 1
            q.append('C')

            
if ans == 'No':
    print(ans)
else:
    print(ans)
    for key in q:
        print(key)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
ans_list= []
if A+B+C == A or A+B+C == B or A+B+C == C:
    for i in range(N):
        s = input()
        if s == "AB" and A == 0 and B == 0:
            print("No")
            break
        elif s == "BC" and B == 0 and C == 0:
            print("No")
            break
        elif s == "AC" and A == 0 and C == 0:
            print("No")
            break
        else:
            if s == "AB":
                if A == 0:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
            elif s == "AC":
                if A == 0:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
            else:
                if B == 0:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                else:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
else:
    for i in range(N):
        s = input()
        if s == "AB":
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            else:
                A -= 1
                B += 1
                ans_list.append(["B"])
        elif s == "AC":
            if A == 0:
                A += 1
                C -= 1
                ans_list.append(["A"])
            else:
                A -= 1
                C += 1
                ans_list.append(["C"])
        else:
            if B == 0:
                B += 1
                C -= 1
                ans_list.append(["B"])
            else:
                B -= 1
                C += 1
                ans_list.append(["C"])
if len(ans_list) == N:
    print("Yes")
    for i in range(N):
        print(*ans_list[i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N, A, B, C = map(int, input().split())
x = y = z = 0
ans = []
aa = bb = cc = 0
val = [A, B, C]
for _ in range(N):
    s = input().rstrip()
    if s == 'AB': x += 1
    if s == 'BC': y += 1
    if s == 'AC': z += 1
    a = (z + x - A + 1) // 2
    b = (x + y - B + 1) // 2
    c = (y + z - C + 1) // 2
    if a + b + c > x + y + z:
        print('No')
        exit()
    if a > aa:
        aa += 1
        ans.append('A')
    elif b > bb:
        bb += 1
        ans.append('B')
    elif c > cc:
        cc += 1
        ans.append('C')
    else:
        if s != 'BC':
            aa += 1
            ans.append('A')
        else:
            bb += 1
            ans.append('B')
print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
N,A,B,C = [int(i) for i in input().split()]
ans = []
for _ in range(N):
    s = input()
    if s[0] == "A" and s[1] == "B":
        if A < B:
            A += 1
            B -= 1
            ans.append("A")
        else:
            A -= 1
            B += 1
            ans.append("B")
    elif s[0] == "A" and s[1] == "C":
        if A < C:
            A += 1
            C -= 1
            ans.append("A")
        else:
            A -= 1
            C += 1
            ans.append("C")
    elif s[0] == "B" and s[1] == "C":
        if B < C:
            B += 1
            C -= 1
            ans.append("B")
        else:
            B -= 1
            C += 1
            ans.append("C")
    if min(A,B,C) < 0:
        print("No")
        break
else:
    print("Yes")
    print("\n".join(ans))

Submission status: Wrong Answer

Solution code:
from random import randint
n, a, b, c = map(int, input().split())
S = []
for i in range(n):
    s = input()
    S.append(s)
ans = []
for c0 in S:
    if c0 == 'AB':
        if a == b == 0:
            print('No')
            exit(0)
        elif a < b or (a == b and randint(0, 1)):
            ans.append('A')
            a += 1
            b -= 1
        else:
            ans.append('B')
            a -= 1
            b += 1
    elif c0 == 'BC':
        if c == b == 0:
            print('No')
            exit(0)
        elif c < b or (c == b and randint(0, 1)):
            ans.append('C')
            c += 1
            b -= 1
        else:
            ans.append('B')
            c -= 1
            b += 1
    else:
        if c == a == 0:
            print('No')
            exit(0)
        elif c < a or (c == a and randint(0, 1)):
            ans.append('C')
            c += 1
            a -= 1
        else:
            ans.append('A')
            c -= 1
            a += 1
print('Yes')
print('\n'.join(ans))
exit(0)

Submission status: Wrong Answer

Solution code:

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

import sys
sys.setrecursionlimit(500000)

s = []

for i in range(N):

    S = input()
    s.append(S)

def dfs(dep,A,B,C):

    if dep == N:
        return []

    if s[dep] == "AB":

        if A == B == 0:
            return None
        if A >= B:
            cat = dfs(dep+1,A-1,B+1,C)
            if cat != None:
                cat.append("B")
                return cat
        if A <= B:
            cat = dfs(dep+1,A+1,B-1,C)
            if cat != None:
                cat.append("A")
                return cat

    elif s[dep] == "BC":
        
        if B == C == 0:
            return None
        if B >= C:
            cat = dfs(dep+1,A,B-1,C+1)
            if cat != None:
                cat.append("C")
                return cat
        if B <= C:
            cat = dfs(dep+1,A,B+1,C-1)
            if cat != None:
                cat.append("B")
                return cat

    else:

        if A == C == 0:
            return None
        if A >= C:
            cat = dfs(dep+1,A-1,B,C+1)
            if cat != None:
                cat.append("C")
                return cat
        if A <= C:
            cat = dfs(dep+1,A+1,B+1,C-1)
            if cat != None:
                cat.append("A")
                return cat

    return None

       
rev = dfs(0,A,B,C)

if rev == None:
    print ("No")
else:
    print ("Yes")
    for i in range(N-1,-1,-1):
        print (rev[i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

N, A, B, C = map(int, input().split())
x = y = z = 0
ans = []
aa = bb = cc = 0
val = [A, B, C]
for _ in range(N):
    s = input().rstrip()
    if s == 'AB': x += 1
    if s == 'BC': y += 1
    if s == 'AC': z += 1
    a = (z + x - A + 1) // 2
    b = (x + y - B + 1) // 2
    c = (y + z - C + 1) // 2
    if a + b + c > x + y + z:
        print('No')
        exit()
    if a > aa:
        aa += 1
        ans.append('A')
    elif b > bb:
        bb += 1
        ans.append('B')
    elif c > cc:
        cc += 1
        ans.append('C')
    else:
        aa += 1
        ans.append('A')
print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
flag=True
temp=[]
for i in range(N):
    s=input()
    if(s=="AB"):
        if(A>=B):
            A-=1
            B+=1
            temp.append("B")
        else:
            A+=1
            B-=1
            temp.append("A")
    elif(s=="AC"):
        if(A>=C):
            A-=1
            C+=1
            temp.append("C")
        else:
            A+=1
            C-=1
            temp.append("A")
    else:
        if(B>=C):
            B-=1
            C+=1
            temp.append("C")
        else:
            B+=1
            C-=1
            temp.append("B")
    if(A<0 or B<0 or C<0):
        flag=False
if(flag):
    print("Yes")
    for i in range(N):
        print(temp[i])
else:
    print("No")

Submission status: Wrong Answer

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

def stoi(s):
    if s == "AB":
        return [0,1]
    if s == "AC":
        return [0,2]
    return [1,2]
itos = ["A","B","C"]
s = [stoi(input()) for _ in range(n)]

arr = [""]*n

def sim():
    for idx,select in enumerate(s):
        if state[select[0]] == 0 and state[select[1]] == 0:
            print("No")
            exit()
        if state[select[0]] == 0:
            arr[idx] = itos[select[0]]
            state[select[0]] += 1
            state[select[1]] -= 1
        elif state[select[1]] == 0:
            arr[idx] = itos[select[1]]
            state[select[1]] += 1
            state[select[0]] -= 1
        elif state[select[0]] == 1 and state[select[1]] > 1:
            arr[idx] = itos[select[0]]
            state[select[0]] += 1
            state[select[1]] -= 1
        elif state[select[1]] == 1 and state[select[0]] > 1:
            arr[idx] = itos[select[1]]
            state[select[1]] += 1
            state[select[0]] -= 1
        else:
            if idx == n-1:
                arr[idx] = itos[select[0]]
            else:
                if select[0] in s[idx+1]:
                    arr[idx] = itos[select[0]]
                    state[select[1]] += 1
                    state[select[0]] -= 1
                else:
                    arr[idx] = itos[select[1]]
                    state[select[0]] += 1
                    state[select[1]] -= 1
sim()

print("Yes")
for choice in arr:
    print(choice)

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

Q, A, B, C = map(int, readline().split())
N = A + B + C
S = read().split()

S = tuple(0 if s == b'BC' else 1 if s == b'AC' else 2 for s in S)

def solve_1(N, A, B, C, S):
    nums = [A, B, C]
    for s in S:
        if nums[s] == N:
            raise Exception
        i, j = (0, 1) if s == 2 else (0, 2) if s == 1 else (1, 2)
        if nums[i] < nums[j]:
            i, j = j, i
        yield j
        nums[j] += 1
        nums[i] -= 1


def solve_2(N, A, B, C, S):
    nums = [A, B, C]
    s = S[0]
    # 初手で死亡
    if nums[s] == N:
        raise Exception
    # あとはできる。きわどいときは 2 手読み
    S = S + (0, )
    for s, t in zip(S, S[1:]):
        i, j = (0, 1) if s == 2 else (0, 2) if s == 1 else (1, 2)
        if (nums[i], nums[j]) != (1, 1):
            if nums[i] < nums[j]:
                i, j = j, i
        if s != t:
            i, j = t, 3 - s - t
        yield j
        nums[j] += 1
        nums[i] -= 1

def large(N, A, B, C, S):
    nums = [A, B, C]
    for s in S:
        if nums[s] == N:
            raise Exception
        i, j = (0, 1) if s == 2 else (0, 2) if s == 1 else (1, 2)
        if nums[i] < nums[j]:
            i, j = j, i
        yield j
        nums[j] += 1
        nums[i] -= 1

f = solve_1 if N == 1 else solve_2 if N == 2 else large

try:
    cmds = list(f(N,A,B,C,S))
    print('Yes')
    answer = ('ABC'[i] for i in cmds)
    print('\n'.join(answer))
except Exception:
    print('No')
    exit()

Submission status: Wrong Answer

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

lst = []
for _ in range(n):
    s = input()
    if s == "AB":
        if a < b:
            a += 1
            b -= 1
            lst.append("A")
        else:
            a -= 1
            b += 1
            if a < 0:
                print("No")
                break
            lst.append("B")
    elif s == "AC":
        if a < c:
            a += 1
            c -= 1
            lst.append("A")
        else:
            a -= 1
            c += 1
            if a < 0:
                print("No")
                break
            lst.append("C")
    else:
        if c < b:
            c += 1
            b -= 1
            lst.append("C")
        else:
            c -= 1
            b += 1
            if c < 0:
                print("No")
                break
            lst.append("B")
else:
    print("Yes")
    for alpha in lst:
        print(alpha)