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

	#print(stat)

	hon = "Yes"
	ans = []
	for i in Q:
		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:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def f(s):
        return ord(s)-ord("A")
    n,a,b,c = LI()
    l = [a,b,c]
    ans = []
    s = [list(map(f, input())) for i in range(n)]
    for i in range(n):
        si = s[i]
        si.sort()
        if l[si[0]] == l[si[1]] == 0:
            print("No")
            return
        if l.count(1) == 2 and l.count(0) == 1 and i+1 < n:
            nsi = s[i+1]
            nsi.sort()
            if nsi == si:
                if l[si[0]] < l[si[1]]:
                    l[si[0]] += 1
                    l[si[1]] -= 1
                    ans.append(chr(si[0]+ord("A")))
                else:
                    l[si[1]] += 1
                    l[si[0]] -= 1
                    ans.append(chr(si[1]+ord("A")))
            else:
                k = [0]*3
                for i in si+nsi:
                    k[i] += 1
                    if k[i] == 2:
                        break
                l[i] += 1
                for j in si:
                    if j != i:
                        break
                l[j] -= 1
                ans.append(chr(i+ord("A")))
        elif l[si[0]] < l[si[1]]:
            l[si[0]] += 1
            l[si[1]] -= 1
            ans.append(chr(si[0]+ord("A")))
        else:
            l[si[1]] += 1
            l[si[0]] -= 1
            ans.append(chr(si[1]+ord("A")))
    print("Yes")
    for i in ans:
        print(i)
    return

#Solve
if __name__ == "__main__":
    solve()

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")
            if((a<0)or(b<0)):
                break
        else:
            b+=1
            a-=1
            l.append("B")
            if((a<0)or(b<0)):
                break
    elif(s=="AC"):
        if(a<c):
            a+=1
            c-=1
            l.append("A")
            if((a<0)or(c<0)):
                break
        else:
            c+=1
            a-=1
            l.append("C")
            if((a<0)or(c<0)):
                break
    elif(s=="BC"):
        if(b>c):
            c+=1
            b-=1
            l.append("C")
            if((c<0)or(b<0)):
                break
        else:
            b+=1
            c-=1
            l.append("B")
            if((c<0)or(b<0)):
                break
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:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

n, a, b, c = map(int, input().split())
query = []
for _ in range(n):
    s = input().rstrip()
    query.append(s)
    
ans = []
for i, s in enumerate(query):
    if s == "AB":
        if a == 0 and b == 0:
            print("No")
            exit()
        elif a == 1 and b == 1:
            if i + 1 < n - 1 and "A" in query[i + 1]:
                ans.append("A")
                a += 1; b -= 1
            else:
                ans.append("B")
                b += 1; a -= 1
        elif b >= a:
            ans.append("A")
            a += 1; b -= 1
        else:
            ans.append("B")
            b += 1; a -= 1
    elif s == "AC":
        if a == 0 and c == 0:
            print("No")
            exit()
        elif a == 1 and c == 1:
            if i + 1 < n - 1 and "A" in query[i + 1]:
                ans.append("A")
                a += 1; c -= 1
            else:
                ans.append("C")
                c += 1; a -= 1
        elif c >= a:
            ans.append("A")
            a += 1; c -= 1
        else:
            ans.append("C")
            c += 1; a -= 1
    elif s == "BC":
        if b == 0 and c == 0:
            print("No")
            exit()
        elif b == 1 and c == 1:
            if i + 1 < n - 1 and "B" in query[i + 1]:
                ans.append("B")
                b += 1; c -= 1
            else:
                ans.append("C")
                c += 1; b -= 1
        elif c >= b:
            ans.append("B")
            b += 1; c -= 1
        else:
            ans.append("C")
            c += 1; b -= 1

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

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

	#print(stat)

	hon = "Yes"
	ans = []
	for i in Q:
		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:
def solve():
    n, a, b, c = map(int, input().split())
    ans = []
    add = ans.append
    for i in range(n):
        s = input()
        if s == "AB":
            if a < b:
                a += 1
                b -= 1
                add('A')
            else:
                a -= 1
                b += 1
                add('B')
        if s == "AC":
            if a < c:
                a += 1
                c -= 1
                add('A')
            else:
                a -= 1
                c += 1
                add('C')
        if s == "BC":
            if c < b:
                c += 1
                b -= 1
                add('C')
            else:
                c -= 1
                b += 1
                add('B')
        if a*b*c < 0:
            print('No')
            return
    print('Yes')
    for i in ans:
        print(i)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
#a+b+c>1のときは必ずYes(追記:入力例3が優しい)
#a+b+c==1のときは操作が限定される
#a+b+c==0のときはNo
#a+b+c>=3のときは大きい方を-1、小さい方を+1の貪欲でいける
#a+b+c=2のときは何手か先まで読む
s=[input()for _ in range(n)]
if s[0]=="AC" and a==c==0:print("No");exit()
if s[0]=="BC" and b==c==0:print("No");exit()
if s[0]=="AB" and a==b==0:print("No");exit()
if (a+b+c)==0:print("No");exit()
if (a+b+c)==1:
  ans=[]
  for i in s:
    if i=="AB":
      if a==1:ans.append("B");a,b=0,1
      elif b==1:ans.append("A");a,b=1,0
      else:print("No");exit()
    elif i=="AC":
      if a==1:ans.append("C");a,c=0,1
      elif c==1:ans.append("A");a,c=1,0
      else:print("No");exit()
    else:
      if b==1:ans.append("C");b,c=0,1
      elif c==1:ans.append("B");b,c=1,0
      else:print("No");exit()
  print("Yes")
  for i in ans:print(i)
  exit()
if (a+b+c)>=3:
  print("Yes")
  for i in s:
    if i=="AB":
      if a>=b:print("B");a-=1;b+=1
      else:print("A");a+=1;b-=1
    elif i=="BC":
      if b>=c:print("C");c+=1;b-=1
      else:print("B");b+=1;c-=1
    else:
      if a>=c:print("C");c+=1;a-=1
      else:print("A");a+=1;c-=1
  exit()
#a+b+c=2
#何手まで読むか
#ダメな操作の例は？
#110AB,020AC
#つまりNoになるのはその直後
#1手先まで読めばいい
print("Yes")
for i in range(n-1):
  if s[i]=="AB":
    if a==b==1:
      if s[i+1]=="AC":print("A");a+=1;b-=1
      else:print("B");b+=1;a-=1
    else:
      if a>=b:print("B");a-=1;b+=1
      else:print("A");a+=1;b-=1
  if s[i]=="BC":
    if b==c==1:
      if s[i+1]=="AB":print("B");b+=1;c-=1
      else:print("C");c+=1;b-=1
    else:
      if b>=c:print("C");c+=1;b-=1
      else:print("B");b+=1;c-=1
  if s[i]=="AC":
    if a==c==1:
      if s[i+1]=="BC":print("C");c+=1;a-=1
      else:print("A");a+=1;c-=1
    else:
      if a>=c:print("C");c+=1;a-=1
      else:print("A");a+=1;c-=1

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

	#print(stat)

	hon = "Yes"
	ans = []
	for i in Q:
		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:
import sys
input = lambda: sys.stdin.readline().rstrip()
D = {"AB": 2, "AC": 1, "BC": 0}
N, a, b, c = map(int, input().split())
X = [a, b, c]
S = [D[input()] for _ in range(N)]
ANS = []
if max(X) == 0:
    print("No")
    exit()

if sum(X) == 1:
    t = 0 if a else 1 if b else 2
    for s in S:
        if X[s]:
            print("No")
            exit()
        t ^= s ^ 3
        ANS.append(t)
        X[s-1] ^= 1
        X[s-2] ^= 1

s = S[0]
if X[s-1] + X[s-2] == 0:
    print("No")
    exit()

for i, s in enumerate(S):
    if X[s-1] > X[s-2]:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    elif X[s-1] > X[s-2]:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1
    elif i == N - 1 or S[i+1] == s-1:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    else:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1

print("Yes")
print("\n".join([chr(t+65) for t in ANS]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
cnt_A=0
cnt_B=0
cnt_C=0
ls=[]
for i in range(N):
    s=input()
    if(s=="AB"):
        cnt_A+=1
        cnt_B+=1
    elif(s=="BC"):
        cnt_B+=1
        cnt_C+=1
    else:
        cnt_A+=1
        cnt_C+=1
    ls.append(s)
flag=True
temp=[]    
for i in range(len(ls)):
    s=ls[i]
    if(s=="AB"):
        cnt_A-=1
        cnt_B-=1
        if(A==B):
            if(cnt_A>=cnt_B):
                A+=1
                B-=1
                temp.append("A")
            else:    
                A-=1
                B+=1
                temp.append("B")
        elif(A>B):
            A-=1
            B+=1
            temp.append("B")
        else:
            A+=1
            B-=1
            temp.append("A")
    elif(s=="AC"):
        cnt_A-=1
        cnt_C-=1
        if(A==C):
            if(cnt_A>=cnt_C):
                A+=1
                C-=1
                temp.append('A')
            else:
                A-=1
                C+=1
                temp.append('C')
        elif(A>C):
            A-=1
            C+=1
            temp.append("C")
        else:
            A+=1
            C-=1
            temp.append("A")
    else:
        cnt_B-=1
        cnt_C-=1
        if(B==C):
            if(cnt_B>=cnt_C):
                B+=1
                C-=1
                temp.append("B")
            else:
                B-=1
                C+=1
                temp.append("C")
        elif(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
        break
if(flag):
    print("Yes")
    for i in range(N):
        print(temp[i])
else:
    print("No")

Submission status: Wrong Answer

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

def f(st):
    if st == 'A':
        return 0
    elif st == 'B':
        return 1
    else:
        return 2

if a+b+c <= 1:
    print('No')
    sys.exit()

if (a+b+c==a and s[0]=='BC') or (a+b+c==b and s[0]=='AC') or (a+b+c==c and s[0]=='AC'):
    print('No')
    sys.exit()
    
print('Yes')

for i in range(n):
    x,y=f(s[i][0]),f(s[i][1])
    
    if num[x] == 0:
        print(s[i][0])
        num[x] += 1
        num[y] -= 1
        
    elif num[y] == 0:
        print(s[i][1])
        num[y] += 1
        num[x] -= 1
    
    elif num[x] == 1:
        print(s[i][0])
        num[x] += 1
        num[y] -= 1
        
    else:
        print(s[i][1])
        num[y] += 1
        num[x] -= 1
    
    #print(num)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
D = {"AB": 2, "AC": 1, "BC": 0}
N, a, b, c = map(int, input().split())
X = [a, b, c]
S = [D[input()] for _ in range(N)]
ANS = []
if max(X) == 0:
    print("No")
    exit()

if sum(X) == 1:
    t = 0 if a else 1 if b else 2
    for s in S:
        if X[s]:
            print("No")
            exit()
        t ^= s ^ 3
        ANS.append(t)
        X[s-1] ^= 1
        X[s-2] ^= 1

s = S[0]
if X[s-1] + X[s-2] == 0:
    print("No")
    exit()

for i, s in enumerate(S):
    if X[s-1] > X[s-2] or i == N - 1 or S[i+1] == s-1:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    else:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1

print("Yes")
print("\n".join([chr(t+65) for t in ANS]))

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)

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')