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:
#F
import sys
from collections import Counter as cc

N,A,B,C = list(map(int, input().split()))
abc = []
ans = []

for i in range(N):
    abc.append(input())

a = abc.count("AB")+abc.count("AC")
b = abc.count("AB")+abc.count("BC")
c = abc.count("AC")+abc.count("BC")


for i, s in enumerate(abc):
    if s == "AB":
        a -= 1
        b -= 1
        if A > B or (A == B and i != N-1 and a > b):
            A -= 1
            B += 1
            ans.append("B")
        else:
            A += 1
            B -= 1
            ans.append("A")
    elif s == "AC":
        a -= 1
        c -= 1
        if A > C or (A == C and i != N-1 and a > c):
            A -= 1
            C += 1
            ans.append("C")
        else:
            A += 1
            C -= 1
            ans.append("A")
    else:
        c -= 1
        b -= 1
        if C > B or (C == B and i != N-1 and 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")
        sys.exit()

print("Yes")
for i in range(N):
        print(ans[i])

Submission status: Wrong Answer

Solution code:
#F
import sys
from collections import Counter as cc

N,A,B,C = list(map(int, input().split()))
abc = []
ans = []

for i in range(N):
    abc.append(input())

nabc = abc[1:]

a = nabc.count("AB")+nabc.count("AC")
b = nabc.count("AB")+nabc.count("BC")
c = nabc.count("AC")+nabc.count("BC")


for i, s in enumerate(abc):
    if s == "AB":
        if A > B or (A == B and i != N-1 and a > b):
            A -= 1
            B += 1
            ans.append("B")
        else:
            A += 1
            B -= 1
            ans.append("A")
        a -= 1
        b -= 1
    elif s == "AC":
        if A > C or (A == C and i != N-1 and a > c):
            A -= 1
            C += 1
            ans.append("C")
        else:
            A += 1
            C -= 1
            ans.append("A")
        a -= 1
        c -= 1
    else:
        if C > B or (C == B and i != N-1 and c > b):
            C -= 1
            B += 1
            ans.append("B")
        else:
            C += 1
            B -= 1
            ans.append("C")
        c -= 1
        b -= 1
    if A < 0 or B < 0 or C < 0:
        print("No")
        sys.exit()

print("Yes")
for i in range(N):
        print(ans[i])

Submission status: Wrong Answer

Solution code:
#F
import sys
from collections import Counter as cc

N,A,B,C = list(map(int, input().split()))
abc = []
ans = []

for i in range(N):
    abc.append(input())

a = abc.count("AB")+abc.count("AC")
b = abc.count("AB")+abc.count("BC")
c = abc.count("AC")+abc.count("BC")


for i, s in enumerate(abc):
    if s == "AB":
        if A > B or (A == B and i != N-1 and a > b):
            A -= 1
            B += 1
            ans.append("B")
        else:
            A += 1
            B -= 1
            ans.append("A")
        a -= 1
        b -= 1
    elif s == "AC":
        if A > C or (A == C and i != N-1 and a > c):
            A -= 1
            C += 1
            ans.append("C")
        else:
            A += 1
            C -= 1
            ans.append("A")
        a -= 1
        c -= 1
    else:
        if C > B or (C == B and i != N-1 and c > b):
            C -= 1
            B += 1
            ans.append("B")
        else:
            C += 1
            B -= 1
            ans.append("C")
        c -= 1
        b -= 1
    if A < 0 or B < 0 or C < 0:
        print("No")
        sys.exit()

print("Yes")
for i in range(N):
        print(ans[i])

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()
	ans = []
	flg = False
	if 2 == A+B+C:
		flg = True

	inp = []
	for i in range(N):
		tmp = inputStr()
		inp.append(tmp)

	if flg == False:
		for i,val in enumerate(inp):
			if val == "AB":
				if A < B:
					A += 1
					B -= 1
					ans.append("A")
				else:
					A -= 1
					B += 1
					ans.append("B")
			elif val == "AC":
				if A < C:
					A += 1
					C -= 1
					ans.append("A")
				else:
					A -= 1
					C += 1
					ans.append("C")
			else:
				if B < C:
					B += 1
					C -= 1
					ans.append("B")
				else:
					B -= 1
					C += 1
					ans.append("C")

			if A < 0 or B < 0 or C < 0:
				print("No")
				sys.exit()

	else:
		for i,val in enumerate(inp):
			if val == "AB":
				if A == 1 and B == 1 and i+1 < len(inp):
					nextInp = inp[i+1]
					if nextInp == "AB":
						A += 1
						B -= 1
						ans.append("A")
					elif nextInp == "AC":
						A += 1
						B -= 1
						ans.append("A")
					else:
						A -= 1
						B += 1
						ans.append("B")
				else:
					if A < B:
						A += 1
						B -= 1
						ans.append("A")
					else:
						A -= 1
						B += 1
						ans.append("B")
			elif val == "AC":
				if A == 1 and C == 1 and i+1 < len(inp):
					nextInp = inp[i+1]
					if nextInp == "AB":
						A += 1
						C -= 1
						ans.append("A")
					elif nextInp == "AC":
						A += 1
						C -= 1
						ans.append("A")
					else:
						C += 1
						A -= 1
						ans.append("C")
				else:
					if A < C:
						A += 1
						C -= 1
						ans.append("A")
					else:
						A -= 1
						C += 1
						ans.append("C")
			else:
				if B == 1 and C == 1 and i+1 < len(inp):
					nextInp = inp[i+1]
					if nextInp == "AB":
						B += 1
						C -= 1
						ans.append("A")
					elif nextInp == "AC":
						C += 1
						B -= 1
						ans.append("C")
					else:
						C += 1
						B -= 1
						ans.append("C")
				else:
					if B < C:
						B += 1
						C -= 1
						ans.append("B")
					else:
						B -= 1
						C += 1
						ans.append("C")

			if A < 0 or B < 0 or C < 0:
				print("No")
				sys.exit()

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


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# N 個のボールを K グループに分ける場合のパターン数
def sunuke(N, K, mod=10**9+7):
	if N < K or K-1 < 0:
		return 0
	else:
		return combination(N-1, K-1, mod)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# 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 main():
    N,A,B,C=map(int,input().split())
    s=[input() for _ in range(N)]
    ans=[]
    for i in range(N):
        if s[i]=='AB':
            if A==B==1 and i+1<N:
                if s[i+1]=='AC':
                    A+=1
                    B-=1
                    ans.append('A')
                else:
                    A-=1
                    B+=1
                    ans.append('B')
            else:
                if min(A,B)==A:
                    A+=1
                    B-=1
                    ans.append('A')
                else:
                    A-=1
                    B+=1
                    ans.append('B')

        elif s[i]=='AC':
            if A==C==1 and i+1<N:
                if s[i+1]=='AB':
                    A+=1
                    C-=1
                    ans.append('A')
                else:
                    A-=1
                    C+=1
                    ans.append('C')
            else:
                if min(A,C)==A:
                    A+=1
                    C-=1
                    ans.append('A')
                else:
                    A-=1
                    C+=1
                    ans.append('C')

        else:
            if B==C==1 and i+1<N:
                if s[i+1]=='AB':
                    B+=1
                    C-=1
                    ans.append('B')
                else:
                    B-=1
                    C+=1
                    ans.append('C')
            else:
                if min(B,C)==B:
                    B+=1
                    C-=1
                    ans.append('B')
                else:
                    B-=1
                    C+=1
                    ans.append('C')
    if min(A,B,C)<0:
        print('No')
    else:
        print('Yes')
        for a in ans:
            print(a)

main()

Submission status: Wrong Answer

Solution code:
def main():
    N,A,B,C=map(int,input().split())
    s=[input() for _ in range(N)]
    ans=[]
    for i in range(N):
        if s[i]=='AB':
            if A==B==1 and i+1<N:
                if s[i+1]=='AC':
                    A+=1
                    B-=1
                    ans.append('A')
                else:
                    A-=1
                    B+=1
                    ans.append('B')
            else:
                if min(A,B)==A:
                    A+=1
                    B-=1
                    ans.append('A')
                else:
                    A-=1
                    B+=1
                    ans.append('B')

        elif s[i]=='AC':
            if A==C==1 and i+1<N:
                if s[i+1]=='BC':
                    A+=1
                    C-=1
                    ans.append('A')
                else:
                    A-=1
                    C+=1
                    ans.append('C')
            else:
                if min(A,C)==A:
                    A+=1
                    C-=1
                    ans.append('A')
                else:
                    A-=1
                    C+=1
                    ans.append('C')

        else:
            if B==C==1 and i+1<N:
                if s[i+1]=='AB':
                    B+=1
                    C-=1
                    ans.append('B')
                else:
                    B-=1
                    C+=1
                    ans.append('C')
            else:
                if min(B,C)==B:
                    B+=1
                    C-=1
                    ans.append('B')
                else:
                    B-=1
                    C+=1
                    ans.append('C')
    if min(A,B,C)<0:
        print('No')
    else:
        print('Yes')
        for a in ans:
            print(a)

main()

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
new=[]
for i in range(0,n):
    s=input()
    new.append(s)
final=[]
x,y,z=0,0,0
for i in range(0,len(new)):
    if new[i]=='AB':
        x+=1
        y+=1
    elif new[i]=='AC':
        x+=1
        z+=1
    else:
        y+=1
        z+=1

for i in range(0,n):
    if new[i]=='AB':
        if a==0 and b==0:
            print('No')
            break
        elif a<b:
            a+=1
            b-=1
            final.append('A')
        elif a>b:
            b+=1
            a-=1
            final.append('B')
        else: #imp
            if x>y:
                b-=1
                a+=1
                final.append('A')
            else:
                a-=1
                b+=1
                final.append('B')
        x-=1
        y-=1
    elif new[i]=='AC':
        if a==0 and c==0:
            print('No')
            break
        elif a<c:
            a+=1
            c-=1
            final.append('A')
        elif a>c:
            c+=1
            a-=1
            final.append('C')
        else:
            if x>z:
                a+=1
                c-=1
                final.append('A')
            else:
                a-=1
                c+=1
                final.append('C')
        x-=1
        z-=1
    else:
        if b==0 and c==0:
            print('No')
            break
        elif b<c:
            b+=1
            c-=1
            final.append('B')
        elif b>c:
            c+=1
            b-=1
            final.append('C')
        else:
            if y>z:
                b+=1
                c-=1
                final.append('B')
            else:
                b-=1
                c+=1
                final.append('C')
        y-=1
        z-=1
else:
    print('Yes')
    for i in range(0,len(final)):
        print(final[i])

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
new=[]
for i in range(0,n):
    s=input()
    new.append(s)
final=[]
x,y,z=0,0,0
for i in range(0,len(new)):
    if new[i]=='AB':
        x+=1
        y+=1
    elif new[i]=='AC':
        x+=1
        z+=1
    else:
        y+=1
        z+=1

for i in range(0,n):
    if new[i]=='AB':
        if a==0 and b==0:
            print('No')
            break
        elif a<b:
            a+=1
            b-=1
            final.append('A')
        elif a>b:
            b+=1
            a-=1
            final.append('B')
        else: #imp
            if x>y:
                b-=1
                a+=1
                final.append('A')
            else:
                a-=1
                b+=1
                final.append('B')
    elif new[i]=='AC':
        if a==0 and c==0:
            print('No')
            break
        elif a<c:
            a+=1
            c-=1
            final.append('A')
        elif a>c:
            c+=1
            a-=1
            final.append('C')
        else:
            if x>z:
                a+=1
                c-=1
                final.append('A')
            else:
                a-=1
                c+=1
                final.append('C')
    else:
        if b==0 and c==0:
            print('No')
            break
        elif b<c:
            b+=1
            c-=1
            final.append('B')
        elif b>c:
            c+=1
            b-=1
            final.append('C')
        else:
            if y>z:
                b+=1
                c-=1
                final.append('B')
            else:
                b-=1
                c+=1
                final.append('C')
else:
    print('Yes')
    for i in range(0,len(final)):
        print(final[i])

Submission status: Wrong Answer

Solution code:
def judge0(z):
  global A,B,C,f,x,y
  if s[z]=='AB':
    f=0
    x=A
    y=B
  if s[z]=='BC':
    f=1
    x=B
    y=C
  if s[z]=='AC':
    f=2
    x=A
    y=C

def ope(j):
  global A,B,C,f,r,s1,x,y
  if x==0 and y==0:
    ans='No'
  elif x>y:
    y+=1
    x-=1
    r[j]=s1[1]    
  else:
    x+=1
    y-=1
    r[j]=s1[0]
  if f==0:
    A=x
    B=y
  elif f==1:
    B=x
    C=y
  else:
    A=x
    c=y     
    
N,A,B,C=map(int, input().split())
s=[input() for i in range(N)]
r=[0]*N
ans='null'
x=0
y=0

if A+B+C==0:
  ans='No'    

elif A+B+C==1:
  for i,a in enumerate(s):
    judge0(i)
    s1=list(a)
    ope(i)
    if x<=0 and y<=0:
      ans='No'
      break
    ans='Yes'
  
else:
  judge0(0)
  if x<=0 and y<=0:
    ans='No'
  else:
    ans='Yes'
    for i,a in enumerate(s):
      judge0(i)
      s1=list(a)
      ope(i)
      #print(A,B,C)
      
print(ans)
if ans=='Yes':
  print('\n'.join(map(str,r)))

Submission status: Wrong Answer

Solution code:
def judge0(z):
  global A,B,C,f,x,y
  if s[z]=='AB':
    f=0
    x=A
    y=B
  if s[z]=='BC':
    f=1
    x=B
    y=C
  if s[z]=='AC':
    f=2
    x=A
    y=C

def ope(j):
  global f,r,s1,x,y
  if x==0 and y==0:
    ans='No'
  elif x>y:
    y+=1
    x-=1
    r[j]=s1[1]    
  else:
    x+=1
    y-=1
    r[j]=s1[0]
  if f==0:
    A=x
    B=y
  elif f==1:
    B=x
    C=y
  else:
    A=x
    c=y     
    
N,A,B,C=map(int, input().split())
s=[input() for i in range(N)]
r=[0]*N
ans='null'
x=0
y=0

if A+B+C==0:
  ans='No'    

elif A+B+C==1:
  for i,a in enumerate(s):
    judge0(i)
    s1=list(a)
    ope(i)
    if x<=0 and y<=0:
      ans='No'
      break
    ans='Yes'
  
else:
  judge0(0)
  if x<=0 and y<=0:
    ans='No'
  else:
    ans='Yes'
    for i,a in enumerate(s):
      judge0(i)
      s1=list(a)
      ope(i)
      
print(ans)
if ans=='Yes':
  print('\n'.join(map(str,r)))

Submission status: Wrong Answer

Solution code:
def judge0(z):
  global A,B,C,f,x,y
  if s[z]=='AB':
    f=0
    x=A
    y=B
  if s[z]=='BC':
    f=1
    x=B
    y=C
  if s[z]=='AC':
    f=2
    x=A
    y=C

def ope(j):
  global f,r,s1,x,y
  if x==0 and y==0:
    ans='No'
  elif x>y:
    y+=1
    x-=1
    r[j]=s1[1]    
  else:
    x+=1
    y-=1
    r[j]=s1[0]
  if f==0:
    A=x
    B=y
  elif f==1:
    B=x
    C=y
  else:
    A=x
    c=y     
    
N,A,B,C=map(int, input().split())
s=[input() for i in range(N)]
r=[0]*N
ans='null'
x=0
y=0

if A+B+C==0:
  ans='No'    

elif A+B+C==1:
  for i,a in enumerate(s):
    judge0(i)
    s1=list(a)
    ope(i)
    print(x,y)
    if x<=0 and y<=0:
      ans='No'
      break
    ans='Yes'
  
else:
  judge0(0)
  if x<=0 and y<=0:
    ans='No'
  else:
    ans='Yes'
    for i,a in enumerate(s):
      judge0(i)
      s1=list(a)
      ope(i)
      
print(ans)
if ans=='Yes':
  print('\n'.join(map(str,r)))

Submission status: Wrong Answer

Solution code:
var = [0]*3
n, var[0], var[1], var[2] = list(map(int, input().split()))
choice = [input() for i in range(n)]
selVar = []
addVar = 0
res = []
flag = True

for i in range(len(choice)):
    if choice[i] == 'AB':
        selVar = [0,1]
    elif choice[i] == 'BC':
        selVar = [1,2]
    else:
        selVar = [0,2]
    if var[0]+var[1]+var[2] == 0:
        flag = False
        break
    elif var[0]+var[1]+var[2] == 1:
        if var[selVar[0]] == 0 and var[selVar[1]] == 0:
            flag = False
            break
        elif var[selVar[0]] == 0:
            var[selVar[0]] += 1
            var[selVar[1]] -= 1
            addVar = selVar[0]
        else:
            var[selVar[0]] -= 1
            var[selVar[1]] += 1
            addVar = selVar[1]
    elif var[0]+var[1]+var[2] >= 2:
        if var[selVar[0]] == 0 and var[selVar[1]] == 0:
            flag = False
            break
        elif var[selVar[0]] == 0:
            var[selVar[0]] += 1
            var[selVar[1]] -= 1
            addVar = selVar[0]
        elif var[selVar[1]] == 0:
            var[selVar[0]] -= 1
            var[selVar[1]] += 1
            addVar = selVar[1]
        elif var[0]+var[1]+var[2] == 2:
            if i < len(choice) - 1:
                if choice[i] != choice[i+1]:
                    commonLet = set(list(choice[i])) & set(list(choice[i+1]))
                    if 'A' in commonLet:
                        var[0] += 1
                        addVar = 0
                        if selVar[0] == 0:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
                    if 'B' in commonLet:
                        var[1] += 1
                        addVar = 1
                        if selVar[0] == 1:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
                    if 'C' in commonLet:
                        var[2] += 1
                        addVar = 2
                        if selVar[0] == 2:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
            else:
                var[selVar[0]] -= 1
                var[selVar[1]] += 1
                addVar = selVar[1]
        else:
            if var[selVar[0]] < var[selVar[1]]:
                var[selVar[0]] += 1
                var[selVar[1]] -= 1
                addVar = selVar[0]
            else:
                var[selVar[0]] -= 1
                var[selVar[1]] += 1
                addVar = selVar[1]
    if addVar == 0:
        res.append('A')
    elif addVar == 1:
        res.append('B')
    else:
        res.append('C')

if not flag:
    print('No')
else:
    print('Yes')
    for re in res:
        print(re)

Submission status: Wrong Answer

Solution code:
var = [0]*3
n, var[0], var[1], var[2] = list(map(int, input().split()))
choice = [input() for i in range(n)]
selVar = []
addVar = 0
res = []
flag = True

for i in range(len(choice)):
    if i > 90000:
        print(var)
    if choice[i] == 'AB':
        selVar = [0,1]
    elif choice[i] == 'BC':
        selVar = [1,2]
    else:
        selVar = [0,2]
    if var[0]+var[1]+var[2] == 0:
        flag = False
        break
    elif var[0]+var[1]+var[2] == 1:
        if var[selVar[0]] == 0 and var[selVar[1]] == 0:
            flag = False
            break
        elif var[selVar[0]] == 0:
            var[selVar[0]] += 1
            var[selVar[1]] -= 1
            addVar = selVar[0]
        else:
            var[selVar[0]] -= 1
            var[selVar[1]] += 1
            addVar = selVar[1]
    elif var[0]+var[1]+var[2] >= 2:
        if var[selVar[0]] == 0 and var[selVar[1]] == 0:
            flag = False
            break
        elif var[selVar[0]] == 0:
            var[selVar[0]] += 1
            var[selVar[1]] -= 1
            addVar = selVar[0]
        elif var[selVar[1]] == 0:
            var[selVar[0]] -= 1
            var[selVar[1]] += 1
            addVar = selVar[1]
        elif var[0]+var[1]+var[2] == 2:
            if i < len(choice) - 1:
                if choice[i] != choice[i+1]:
                    commonLet = set(list(choice[i])) & set(list(choice[i+1]))
                    if 'A' in commonLet:
                        var[0] += 1
                        addVar = 0
                        if selVar[0] == 0:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
                    if 'B' in commonLet:
                        var[1] += 1
                        addVar = 1
                        if selVar[0] == 1:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
                    if 'C' in commonLet:
                        var[2] += 1
                        addVar = 2
                        if selVar[0] == 2:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
        else:
            if var[selVar[0]] < var[selVar[1]]:
                var[selVar[0]] += 1
                var[selVar[1]] -= 1
                addVar = selVar[0]
            else:
                var[selVar[0]] -= 1
                var[selVar[1]] += 1
                addVar = selVar[1]
    if addVar == 0:
        res.append('A')
    elif addVar == 1:
        res.append('B')
    else:
        res.append('C')

if not flag:
    print('No')
else:
    print('Yes')
    for re in res:
        print(re)

Submission status: Wrong Answer

Solution code:
var = [0]*3
n, var[0], var[1], var[2] = list(map(int, input().split()))
choice = [input() for i in range(n)]
selVar = []
addVar = 0
res = []
flag = True

for i in range(len(choice)):
    if choice[i] == 'AB':
        selVar = [0,1]
    elif choice[i] == 'BC':
        selVar = [1,2]
    else:
        selVar = [0,2]
    if var[0]+var[1]+var[2] == 0:
        flag = False
        break
    elif var[0]+var[1]+var[2] == 1:
        if var[selVar[0]] == 0 and var[selVar[1]] == 0:
            flag = False
            break
        elif var[selVar[0]] == 0:
            var[selVar[0]] += 1
            var[selVar[1]] -= 1
            addVar = selVar[0]
        else:
            var[selVar[0]] -= 1
            var[selVar[1]] += 1
            addVar = selVar[1]
    elif var[0]+var[1]+var[2] >= 2:
        if var[selVar[0]] == 0 and var[selVar[1]] == 0:
            flag = False
            break
        elif var[selVar[0]] == 0:
            var[selVar[0]] += 1
            var[selVar[1]] -= 1
            addVar = selVar[0]
        elif var[selVar[1]] == 0:
            var[selVar[0]] -= 1
            var[selVar[1]] += 1
            addVar = selVar[1]
        elif var[0]+var[1]+var[2] == 2:
            if i < len(choice) - 1:
                if choice[i] != choice[i+1]:
                    commonLet = set(list(choice[i])) & set(list(choice[i+1]))
                    if 'A' in commonLet:
                        var[0] += 1
                        addVar = 0
                        if selVar[0] == 0:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
                    if 'B' in commonLet:
                        var[1] += 1
                        addVar = 1
                        if selVar[0] == 1:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
                    if 'C' in commonLet:
                        var[2] += 1
                        addVar = 2
                        if selVar[0] == 2:
                            var[selVar[1]] -= 1
                        else:
                            var[selVar[0]] -= 1
        else:
            if var[selVar[0]] < var[selVar[1]]:
                var[selVar[0]] += 1
                var[selVar[1]] -= 1
                addVar = selVar[0]
            else:
                var[selVar[0]] -= 1
                var[selVar[1]] += 1
                addVar = selVar[1]
    if addVar == 0:
        res.append('A')
    elif addVar == 1:
        res.append('B')
    else:
        res.append('C')

if not flag:
    print('No')
else:
    print('Yes')
    for re in res:
        print(re)