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:
from collections import Counter
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines


def main():
    N,A,B,C = map(int, readline().split())
    S = [readline().strip() for _ in range(N)]
    t = Counter(S)

    ans = []
    for s in S:
        if s == "AB":
            if A > B:
                A -= 1
                B += 1
                ans.append("B")
            elif A < B:
                A += 1
                B -= 1
                ans.append("A")
            else:
                if t["AC"] >= t["BC"]:
                    A -= 1
                    B += 1
                    ans.append("B")
                else:
                    A += 1
                    B -= 1
                    ans.append("A")
        
        elif s == "BC":
            if B > C:
                B -= 1
                C += 1
                ans.append("C")
            elif B < C:
                B += 1
                C -= 1
                ans.append("B")
            else:
                if t["AB"] >= t["AC"]:
                    B -= 1
                    C += 1
                    ans.append("C")
                else:
                    B += 1
                    C -= 1
                    ans.append("B")
        
        else:
            if A > C:
                A -= 1
                C += 1
                ans.append("C")
            elif A < C:
                A += 1
                C -= 1
                ans.append("A")
            else:
                if t["AB"] >= t["BC"]:
                    A -= 1
                    C += 1
                    ans.append("C")
                else:
                    A += 1
                    C -= 1
                    ans.append("A")
        
        if A < 0 or B < 0 or C < 0:
            print("No")
            exit()
        
    print("Yes")
    print("\n".join(ans))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
idleA=10**20
idleB=10**10
idleC=1
stack=[[A,B,C]]
lst={}
dp=[{} for i in range(N)]

def main(n,a,b,c):

    if not a in dp[n]:
        dp[n][a]={}
    if not b in dp[n][a]:
        dp[n][a][b]={}
    if not c in dp[n][a][b]:
        dp[n][a][b][c]=""
    if not dp[n][a][b][c]:
        return(False)
    return(True)

for n in range(N):
    s=S[n]
    #nlst=set()
    nstack=[]
    while stack:
        #print(stack)
        a,b,c=stack.pop()
        if s=="AB":
            if a>=1 and not main(n,a-1,b+1,c):
                dp[n][a-1][b+1][c]="B"
                nstack.append([a-1,b+1,c])
            if b>=1 and not main(n,a+1,b-1,c):
                dp[n][a+1][b-1][c]="A"
                nstack.append([a+1,b-1,c])
        elif s=="BC":
            if c>=1 and not main(n,a,b+1,c-1):
                dp[n][a][b+1][c-1]="B"
                nstack.append([a,b+1,c-1])
            if b>=1 and not main(n,a,b-1,c+1):
                dp[n][a][b-1][c+1]="C"
                nstack.append([a,b-1,c+1])
        elif s=="AC":
            if a>=1 and not main(n,a-1,b,c+1):
                dp[n][a-1][b][c+1]="C"
                nstack.append([a-1,b,c+1])
            if c>=1 and not main(n,a+1,b,c-1):
                dp[n][a+1][b][c-1]="A"
                nstack.append([a+1,b,c-1])

    stack=nstack
if dp[-1]:
    print("Yes")
    lst=[]
    flag=0
    for a in dp[-1]:
        for b in dp[-1][a]:
            for c in dp[-1][a][b]:
                X=dp[-1][a][b][c]
                lst.append([X,a,b,c])
                flag=1
                break
            if flag:
                break
        if flag:
            break
    for n in range(N-1)[::-1]:
        s=S[n+1]
        X,a,b,c=lst[-1]
        if s=="AB":
            if  main(n,a-1,b+1,c):
                lst.append([dp[n][a-1][b+1][c],a-1,b+1,c])
                continue
            if  main(n,a+1,b-1,c):
                lst.append([dp[n][a+1][b-1][c],a+1,b-1,c])
                continue
        if s=="BC":
            if  main(n,a,b+1,c-1):
                lst.append([dp[n][a][b+1][c-1],a,b+1,c-1])
                continue
            if  main(n,a,b-1,c+1):
                lst.append([dp[n][a][b-1][c+1],a,b-1,c+1])
                continue
        if s=="AC":
            if  main(n,a-1,b,c+1):
                lst.append([dp[n][a-1][b][c+1],a-1,b,c+1])
                continue
            if  main(n,a+1,b,c-1):
                lst.append([dp[n][a+1][b][c-1],a+1,b,c-1])
                continue
        #print("a")
    for X,a,b,c in list(reversed(lst)):
        print(X)
else:
    print("No")

Submission status: Wrong Answer

Solution code:

n,a,b,c=map(int,input().split())
s=[""]*n
iab=0
iac=0
ibc=0
for i in range(n):
    s[i]=input()
    if s[i]=="AB":
        iab+=1
    elif s[i]=="AC":
        iac+=1    
    else:
        ibc+=1

t=[""]*n
yn=""
for i in range(n):
    if s[i]=="AB":
        if a>=b:
            t[i]="B"
            a-=1
            b+=1
            if a<0:
                yn="No"
                print("No")
                break
        else:
            t[i]="A"
            a+=1
            b-=1
            if b<0:
                yn="No"
                print("No")
                break
    if s[i]=="AC":
        if a>=c:
            t[i]="C"
            a-=1
            c+=1
            if a<0:
                yn="No"
                print("No")
                break
        else:
            t[i]="A"
            a+=1
            c-=1
            if c<0:
                yn="No"
                print("No")
                break
    if s[i]=="BC":
        if c>=b:
            t[i]="B"
            c-=1
            b+=1
            if c<0:
                yn="No"
                print("No")
                break
        else:
            t[i]="C"
            c+=1
            b-=1
            if b<0:
                yn="No"
                print("No")
                break
            
if yn!="No":
    print("Yes")
    for i in range(n):
        print(t[i])

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B":b, "C": c}
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 0:
    print("No")
    exit()
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 1:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
            else:
                ans.append(s[1])
    print("Yes")
    for a in ans:
        print(a)
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 2:
    for i in range(n-1):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 2:
            if num_dic[s[0]] == 1:
                if ss[i+1].find(s[0]) > -1:
                    num_dic[s[0]] = 2
                    num_dic[s[1]] = 0
                    ans.append(s[0])
                else:
                    num_dic[s[0]] = 0
                    num_dic[s[1]] = 2
                    ans.append(s[1])
        elif num_dic[s[0]] + num_dic[s[1]] == 1:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = 0
            else:
                ans.append(s[1])
                num_dic[s[0]] = 0
                num_dic[s[1]] = 1
        elif num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = num_dic[s[1]] - 1
            else:
                ans.append(s[1])
                num_dic[s[0]] = num_dic[s[0]] - 1
                num_dic[s[1]] = num_dic[s[1]] + 1
    if num_dic[s[i+1][0]] + num_dic[s[i+1][1]] == 0:
        print("No")
        exit()
    if num_dic[s[i+1][0]] != 0:
        ans.append(s[i+1][0])
    else:
        ans.append(s[i+1][1])
    print("Yes")
    for a in ans:
        print(a)
else:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
            print("No")
            exit()
        if num_dic[s[0]] == 0:
            ans.append(s[0])
            
            num_dic[s[0]] = 1
            num_dic[s[1]] = num_dic[s[1]] - 1
        elif num_dic[s[0]] == 0:
            ans.append(s[1])
            num_dic[s[0]] = num_dic[s[0]] - 1
            num_dic[s[1]] = 1
        else:
            if num_dic[s[0]] >= num_dic[s[1]]:
                ans.append(s[1])
                num_dic[s[0]] = num_dic[s[0]] - 1
                num_dic[s[1]] = num_dic[s[1]] + 1
            else:
                ans.append(s[0])
                num_dic[s[0]] = num_dic[s[0]] + 1
                num_dic[s[1]] = num_dic[s[1]] - 1
    print("Yes")
    for a in ans:
        print(a)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

N,A,B,C = inputIL()
l = inputSLs(N)

if A+B+C == 0:
  print("No")
  exit()
  
n = len(l)


a = []

if A+B+C == 1:
  for item in l:
    if item == "AB":
      if A==B==0:
        print("No")
        exit()
      elif A==0:
        A += 1
        B -= 1
        a.append("A")
      else:
        A -= 1
        B += 1
        a.append("B")
    if item == "AC":
      if A==C==0:
        print("No")
        exit()
      elif A==0:
        A += 1
        C -= 1
        a.append("A")
      else:
        A -= 1
        C += 1
        a.append("C")
    if item == "BC":
      if B==C==0:
        print("No")
        exit()
      elif B==0:
        B += 1
        C -= 1
        a.append("B")
      else:
        B -= 1
        C += 1
        a.append("C")

if A+B+C == 2:
  for i in range(n):
    item = l[i]
    if item == "AB":
      if A==B==0:
        print("No")
        exit()
      elif A==0:
        A += 1
        B -= 1
        a.append("A")
      elif B==0:
        A -= 1
        B += 1
        a.append("B")
      elif C==0 and i!=n-1:
        nitem = l[i+1]
        if nitem == "AB":
          A += 1
          B -= 1
          a.append("A")
        if nitem == "AC":
          A += 1
          B -= 1
          a.append("A")
        if nitem == "BC":
          A -= 1
          B += 1
          a.append("B")
      else:
        A += 1
        B -= 1
        a.append("A")
        
    if item == "AC":
      if A==C==0:
        print("No")
        exit()
      elif A==0:
        A += 1
        C -= 1
        a.append("A")
      elif C==0:
        A -= 1
        C += 1
        a.append("C")
      elif B==0 and i!=n-1:
        nitem = l[i+1]
        if nitem == "AC":
          A += 1
          C -= 1
          a.append("A")
        if nitem == "AB":
          A += 1
          C -= 1
          a.append("A")
        if nitem == "BC":
          A -= 1
          C += 1
          a.append("C")
      else:
        A += 1
        C -= 1
        a.append("A")

        
    if item == "BC":
      if B==C==0:
        print("No")
        exit()
      elif B==0:
        B += 1
        C -= 1
        a.append("B")
      elif C==0:
        B -= 1
        C += 1
        a.append("C")
      elif A==0 and i!=n-1:
        nitem = l[i+1]
        if nitem == "BC":
          B += 1
          C -= 1
          a.append("B")
        if nitem == "AB":
          B += 1
          C -= 1
          a.append("B")
        if nitem == "AC":
          B -= 1
          C += 1
          a.append("C")
      else:
        B += 1
        C -= 1
        a.append("B")
        


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

Submission status: Wrong Answer

Solution code:

import heapq
from collections import defaultdict, deque
from math import ceil, factorial, gcd
from itertools import accumulate, permutations
import sys
import bisect


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

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
msi = lambda: map(str, input().split())
lmii = lambda: list(map(int, input().split()))
lmsi = lambda: list(map(str, input().split()))
smii = lambda: sorted(map(int, input().split()))


# ----------

N, A, B, C = mii()

v = dict()
v['A'] = A
v['B'] = B
v['C'] = C

ans = []

if A + B + C == 0:
    print('No')
    exit()

S = []
for i in range(N):
    S.append(si())

for i in range(N):
    s1, s2 = list(S[i])
    if v[s1] == v[s2] == 0:
        print('No')
        exit()
    if v[s1] > 0 and v[s2] == 0:
        v[s1] -= 1
        v[s2] += 1
        ans.append(s2)
        continue
    if v[s1] == 0 and v[s2] > 0:
        v[s1] += 1
        v[s2] -= 1
        ans.append(s1)
        continue
    if v[s1] == v[s2] == 1:
        if i != N - 1:
            if S[i] != S[i+1]:
                if s1 in S[i+1]:
                    v[s1] += 1
                    v[s2] -= 1
                    ans.append(s1)
                    continue
    v[s1] += 1
    v[s2] -= 1
    ans.append(s1)

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

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
s=[""]*n
iab=0
iac=0
ibc=0
for i in range(n):
    s[i]=input()
    if s[i]=="AB":
        iab+=1
    elif s[i]=="AC":
        iac+=1    
    else:
        ibc+=1

t=[""]*n
yn=""
for i in range(n):
    if s[i]=="AB":
        if a>=b:
            t[i]="B"
            a-=1
            b+=1
            if a<0:
                yn="No"
                print("No")
                break
        else:
            t[i]="A"
            a+=1
            b-=1
            if b<0:
                yn="No"
                print("No")
                break
    if s[i]=="AC":
        if a>=c:
            t[i]="C"
            a-=1
            c+=1
            if a<0:
                yn="No"
                print("No")
                break
        else:
            t[i]="A"
            a+=1
            c-=1
            if c<0:
                yn="No"
                print("No")
                break
    if s[i]=="BC":
        if c<=b:
            t[i]="B"
            c-=1
            b+=1
            if c<0:
                yn="No"
                print("No")
                break
        else:
            t[i]="C"
            c+=1
            b-=1
            if b<0:
                yn="No"
                print("No")
                break
            
if yn!="No":
    print("Yes")
    for i in range(n):
        print(t[i])

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
data = {
  "A":A,
  "B":B,
  "C":C
}
select = []
for _ in range(N):
  s = input()
  if data[s[0]] == data[s[1]] ==0:
    print("No")
    exit(0)
  elif data[s[0]] >= data[s[1]]:
    data[s[0]] -= 1
    data[s[1]] += 1
    select.append(s[1])
  else:
    data[s[0]] += 1
    data[s[1]] -= 1
    select.append(s[0])
print("Yes")
for i in select:
  print(i)

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B":b, "C": c}
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 0:
    print("No")
    exit()
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 1:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
            else:
                ans.append(s[1])
    print("Yes")
    for a in ans:
        print(a)
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 2:
    for i in range(n-1):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 2:
            if num_dic[s[0]] == 1:
                if ss[i+1].find(s[0]) > -1:
                    num_dic[s[0]] = 2
                    num_dic[s[1]] = 0
                    ans.append(s[0])
                else:
                    num_dic[s[0]] = 0
                    num_dic[s[1]] = 2
                    ans.append(s[1])
        elif num_dic[s[0]] + num_dic[s[1]] == 1:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = 0
            else:
                ans.append(s[1])
                num_dic[s[0]] = 0
                num_dic[s[1]] = 1
        elif num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = 0
            else:
                ans.append(s[1])
                num_dic[s[0]] = 0
                num_dic[s[1]] = 1
    if num_dic[s[i+1][0]] + num_dic[s[i+1][1]] == 0:
        print("No")
        exit()
    if num_dic[s[i+1][0]] != 0:
        ans.append(s[i+1][0])
    else:
        ans.append(s[i+1][1])
    print("Yes")
    for a in ans:
        print(a)
else:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
            print("No")
            exit()
        if num_dic[s[0]] == 0:
            ans.append(s[0])
            
            num_dic[s[0]] = 1
            num_dic[s[1]] = num_dic[s[1]] - 1
        elif num_dic[s[0]] == 0:
            ans.append(s[1])
            num_dic[s[0]] = num_dic[s[0]] - 1
            num_dic[s[1]] = 1
        else:
            if num_dic[s[0]] >= num_dic[s[1]]:
                ans.append(s[1])
                num_dic[s[0]] = num_dic[s[0]] - 1
                num_dic[s[1]] = num_dic[s[1]] + 1
            else:
                ans.append(s[0])
                num_dic[s[0]] = num_dic[s[0]] + 1
                num_dic[s[1]] = num_dic[s[1]] - 1
    print("Yes")
    for a in ans:
        print(a)

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
data = {
  "A":A,
  "B":B,
  "C":C
}
select = []
for _ in range(N):
  s = input()
  if data[s[0]] == data[s[1]] ==0:
    print("No")
    exit(0)
  elif data[s[0]] >= data[s[1]]:
    data[s[0]] -= 1
    data[s[1]] += 1
    select.append(s[1])
  else:
    data[s[0]] += 1
    data[s[1]] -= 1
    select.append(s[0])
for i in select:
  print(i)

Submission status: Wrong Answer

Solution code:
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A,B,C=MI()
    S=[]
    SS=A+B+C
    for i in range(N):
        s=input()
        S.append(s)
    
    S.append("AB")
    S.append("AB")#適当に入れとけ
    
    ans=[]
    flag=1
    
    if SS==0:
        flag=-1
    elif SS==2:
        for i in range(N):
            s=S[i]
            if s=="AB":
                if A<B:
                    A+=1
                    B-=1
                    ans.append("A")
                elif B>A:
                    A-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="AC":
                        A+=1
                        B-=1
                        ans.append("A")
                    else:
                        A-=1
                        B+=1
                        ans.append("B")
                        
                    
            elif s=="AC":
                if A<C:
                    A+=1
                    C-=1
                    ans.append("A")
                elif A>C:
                    A-=1
                    C+=1
                    ans.append("C")
                else:#同数
                    s2=S[i+1]
                    if s2=="AB":
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
                    
            else:#BC
                if C<B:
                    C+=1
                    B-=1
                    ans.append("C")
                elif C>B:
                    C-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="AC":
                        C+=1
                        B-=1
                        ans.append("C")
                    else:
                        C-=1
                        B+=1
                        ans.append("B")
                
   
        
                
            if A<0 or B<0 or C<0:
                flag=-1
                break
        
    else:
        for i in range(N):
            s=S[i]
            if s=="AB":
                if A<B:
                    A+=1
                    B-=1
                    ans.append("A")
                else:
                    A-=1
                    B+=1
                    ans.append("B")
                
                        
                    
            elif s=="AC":
                if A<C:
                    A+=1
                    C-=1
                    ans.append("A")
                else:
                    A-=1
                    C+=1
                    ans.append("C")
                
                    
            else:#BC
                if C<B:
                    C+=1
                    B-=1
                    ans.append("C")
                else:
                    C-=1
                    B+=1
                    ans.append("B")
                      
            if A<0 or B<0 or C<0:
                flag=-1
                break
                
        
        
    if flag==-1:
        print("No")
        assert N!=2
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B":b, "C": c}
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 0:
    print("No")
    exit()
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 1:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
            else:
                ans.append(s[1])
    print("Yes")
    for a in ans:
        print(a)
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 2:
    for i in range(n-1):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 2:
            if num_dic[s[0]] == 1:
                if ss[i+1].find(s[0]) > -1:
                    num_dic[s[0]] = 2
                    num_dic[s[1]] = 0
                    ans.append(s[0])
                else:
                    num_dic[s[0]] = 0
                    num_dic[s[1]] = 2
                    ans.append(s[1])
        elif num_dic[s[0]] + num_dic[s[1]] == 1:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = 0
            else:
                ans.append(s[1])
                num_dic[s[0]] = 0
                num_dic[s[1]] = 1
        elif num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = 0
            else:
                ans.append(s[1])
                num_dic[s[0]] = 0
                num_dic[s[1]] = 1
    if num_dic[s[i+1][0]] != 0:
        ans.append(s[i+1][0])
    else:
        ans.append(s[i+1][1])
    print("Yes")
    for a in ans:
        print(a)
else:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
            print("No")
            exit()
        if num_dic[s[0]] == 0:
            ans.append(s[0])
            
            num_dic[s[0]] = 1
            num_dic[s[1]] = num_dic[s[1]] - 1
        elif num_dic[s[0]] == 0:
            ans.append(s[1])
            num_dic[s[0]] = num_dic[s[0]] - 1
            num_dic[s[1]] = 1
        else:
            if num_dic[s[0]] >= num_dic[s[1]]:
                ans.append(s[1])
                num_dic[s[0]] = num_dic[s[0]] - 1
                num_dic[s[1]] = num_dic[s[1]] + 1
            else:
                ans.append(s[0])
                num_dic[s[0]] = num_dic[s[0]] + 1
                num_dic[s[1]] = num_dic[s[1]] - 1
    print("Yes")
    for a in ans:
        print(a)

Submission status: Wrong Answer

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