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:
def examA():
    S = SI()
    if S=="ARC":
        ans = "ABC"
    else:
        ans = "ARC"
    print(ans)
    return

def examB():
    N, K = LI()
    snuke = set(range(1,N+1))
    for _ in range(K):
        d = I()
        A = LI()
        for a in A:
            snuke.discard(a)
    ans = len(snuke)
    print(ans)
    return

def examC():
    N, M = LI()
    H = LI()
    good = set(range(1,N+1))
    for _ in range(M):
        a, b = LI()
        if H[a-1]>=H[b-1]:
            good.discard(b)
        if H[b-1]>=H[a-1]:
            good.discard(a)
    ans = len(good)
    print(ans)
    return

def examD():
    X = I()
    #print(1000**5-999**5)
    for a in range(-1000,1000):
        for b in range(-1000,1000):
            if a**5-b**5==X:
                print(a,b)
                return
    return

def examE():
    N = I()
    A = LI()
    B = [0]*N
    D = defaultdict(int)
    for i in range(N):
        B[i] = A[i]-i
        c = A[i]+i
        D[c] += 1
    #print(B)
    #print(D)
    ans = 0
    for b in B:
        ans += D[-b]
    print(ans)
    return

def examF():
    def Run_Length_Encoding(s):
        tmp, count, res = s[0], 1, []
        for i in range(1, len(s)):
            if tmp == s[i]:
                count += 1
            else:
                res.append((count, tmp))
                tmp = s[i]
                count = 1
        res.append((count, tmp))
        return res

    N, A, B, C = LI()
    S = [SI()for _ in range(N)]
    SS = Run_Length_Encoding(S)
    #print(SS)
    n = len(SS)
    ans = [""]*(N)
    def judge(a,b):
        if a<0 or b<0:
            return False
        return True
    def dfs(i,num,A,B,C):
        if i==n:
            if A>=0 and B>=0 and C>=0:
                print("Yes")
                for v in ans:
                    print(v)
                exit()
            return
        cnt, s = SS[i]
        if s=="AB":
            if A==0 and B==0:
                return
            for j in range(cnt//2 -1):
                if A==0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "B"
                else:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "A"
            if cnt>=2:
                A += 1; B -= 1
                if judge(A, B):
                    ans[num] = "A"
                    dfs(i + 1, num + 1, A, B, C)
                A -= 2; B += 2
                if judge(A, B):
                    ans[num] = "B"
                    dfs(i + 1, num + 1, A, B, C)
                A += 1;
                B -= 2
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            A += 1; B -= 1
            if judge(A,B):
                ans[num] = "A"
                dfs(i+1,num+1,A,B,C)
            A -= 2; B += 2
            if judge(A,B):
                ans[num] = "B"
                dfs(i+1,num+1,A,B,C)
            A += 1; B -= 2
        elif s=="BC":
            if B==0 and C==0:
                return
            for j in range(cnt//2 -1):
                if B==0:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "B"
            if cnt%2==0:

                dfs(i+1,num+1,A,B,C)
                return
            B += 1; C -= 1
            if judge(B,C):
                ans[num] = "B"
                dfs(i+1,num+1,A,B,C)
            B -= 2; C += 2
            if judge(B,C):
                ans[num] = "C"
                dfs(i+1,num+1,A,B,C)
            B += 1; C -= 2
        else:
            if A==0 and C==0:
                return
            for j in range(cnt//2 -1):
                if A==0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "A"
            if cnt%2==0:
                dfs(i+1,num+1,A,B,C)
                return
            A += 1; C -= 1
            if judge(A, C):
                ans[num] = "A"
                dfs(i + 1,num+1,A,B,C)
            A -= 2; C += 2
            if judge(A, C):
                ans[num] = "C"
                dfs(i + 1,num+1,A,B,C)
            A += 1; C -= 2
        return
    dfs(0,0,A,B,C)
    print("No")
    return


def examF2():
    def Run_Length_Encoding(s):
        tmp, count, res = s[0], 1, []
        for i in range(1, len(s)):
            if tmp == s[i]:
                count += 1
            else:
                res.append((count, tmp))
                tmp = s[i]
                count = 1
        res.append((count, tmp))
        return res

    N, A, B, C = LI()
    S = [SI() for _ in range(N)]
    SS = Run_Length_Encoding(S)
    # print(SS)
    n = len(SS)
    ans = [""] * (N)

    def judge(a, b):
        if a < 0 or b < 0:
            return False
        return True

    def dfs(i, num, A, B, C):
        if i == n:
            if A >= 0 and B >= 0 and C >= 0:
                print("Yes")
                for v in ans:
                    print(v)
                exit()
            return
        cnt, s = SS[i]
        if s == "AB":
            if A == 0 and B == 0:
                return
            for j in range(cnt // 2):
                if A == 0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "B"
                else:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "A"
            if cnt % 2 == 0:
                dfs(i + 1, num + 1, A, B, C)
                return
            A += 1;
            B -= 1
            if judge(A, B):
                ans[num] = "A"
                dfs(i + 1, num + 1, A, B, C)
            A -= 2;
            B += 2
            if judge(A, B):
                ans[num] = "B"
                dfs(i + 1, num + 1, A, B, C)
            A += 1;
            B -= 1
        elif s == "BC":
            if B == 0 and C == 0:
                return
            for j in range(cnt // 2):
                if B == 0:
                    ans[num] = "B"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "B"
            if cnt % 2 == 0:
                dfs(i + 1, num + 1, A, B, C)
                return
            B += 1;
            C -= 1
            if judge(B, C):
                ans[num] = "B"
                dfs(i + 1, num + 1, A, B, C)
            B -= 2;
            C += 2
            if judge(B, C):
                ans[num] = "C"
                dfs(i + 1, num + 1, A, B, C)
            B += 1;
            C -= 1
        else:
            if A == 0 and C == 0:
                return
            for j in range(cnt // 2):
                if A == 0:
                    ans[num] = "A"
                    num += 1
                    ans[num] = "C"
                else:
                    ans[num] = "C"
                    num += 1
                    ans[num] = "A"
            if cnt % 2 == 0:
                dfs(i + 1, num + 1, A, B, C)
                return
            A += 1;
            C -= 1
            if judge(A, C):
                ans[num] = "A"
                dfs(i + 1, num + 1, A, B, C)
            A -= 2;
            C += 2
            if judge(A, C):
                ans[num] = "C"
                dfs(i + 1, num + 1, A, B, C)
            A += 1;
            C -= 1
        return

    dfs(0, 0, A, B, C)
    print("No")
    return

import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(readline())
def LI(): return list(map(int,readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 10**(-12)
alphabet = [chr(ord('a') + i) for i in range(26)]

sys.setrecursionlimit(10**7)

if __name__ == '__main__':
    examF2()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Submission status: Wrong Answer

Solution code:
n,a,b,c=[int(x) for x in input().split()]
S=[input() for i in range(n)]
ans=""
for i in range(n):
  s=S[i]
  if s=="AB":
    if a==0 and b==0:
      print("No")
      exit()
    elif a>b:
      ans+="B\n"
      a-=1
      b+=1
    elif b>a:
      ans+="A\n"
      a+=1
      b-=1
    else:
      if i+1<n and S[i+1]=="AC":
         ans+="A\n"
         a+=1
         b-=1
      else:
         ans+="B\n"
         a-=1
         b+=1
  elif s=="AC":
    if a==0 and c==0:
      print("No")
      exit()
    elif a>c:
      ans+="C\n"
      a-=1
      c+=1
    elif c>a:
      ans+="A\n"
      a+=1
      c-=1
    else:
        if i+1<n and S[i+1]=="BC":
            ans+="C\n"
            a-=1
            c+=1
        else:
            ans += "A\n"
            a+=1
            c-=1
  elif s=="BC":
    if c==0 and b==0:
      print("No")
      exit()
    elif c>b:
      ans+="B\n"
      c-=1
      b+=1
    elif b>c:
      ans+="C\n"
      c+=1
      b-=1
    else:
        if i+1<n and S[i+1]=="AB":
            ans += "B\n"
            b+=1
            a-=1
        else:
            ans+="A\n"
            a+=1
            b-=1
print("Yes")
print(ans,end="")

Submission status: Wrong Answer

Solution code:
import sys
from collections import defaultdict

read = sys.stdin.read
readline = sys.stdin.readline

N, A, B, C = map(int, readline().split())
S = list(map(str, read().split()))
v = defaultdict(int)
v['A'] = A
v['B'] = B
v['C'] = C
answer = []
for s in S:
    i = s[0]
    j = s[1]
    if v[i] > v[j]:
        answer.append(j)
        v[i] -= 1
        v[j] += 1
    else:
        answer.append(i)
        v[i] += 1
        v[j] -= 1
    if any(i < 0 for i in v.values()):
        print('No')
        exit()

print('Yes')
print('\n'.join(answer))

Submission status: Wrong Answer

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

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for s in sl:
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if yusen == "A":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if yusen == "B":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if yusen == "C":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

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

Submission status: Wrong Answer

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
ans = []
D = {'A':a, 'B':b, 'C':c}
sx = SR(n)
for i in range(n):
    s = sx[i]
    s1 = s[0]
    s2 = s[1]
    if D[s1] == D[s2] == 0:
        print('No')
        exit()
    elif D[s[0]] == D[s[1]] == 1 and i + 1 < n:
        if s1 in sx[i + 1]:
            D[s1] += 1
            D[s2] -= 1
            ans += [s1]
        else:
            D[s2] += 1
            D[s1] -= 1
            ans += [s2]
    if D[s1] == 0:
        ans += [s1]
        D[s1] += 1
        D[s2] -= 1
    elif D[s2] == 0:
        ans += [s2]
        D[s1] -= 1
        D[s2] += 1
    else:
        if D[s1] > D[s2]:
            ans += [s2]
            D[s1] -= 1
            D[s2] += 1
        else:
            ans += [s1]
            D[s1] += 1
            D[s2] -= 1

print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,a,b,c = inpm()
    aa = copy.copy(a)
    bb = copy.copy(b)
    cc = copy.copy(c)
    ans = ['p' for _ in range(n)]
    AB = deque()
    AC = deque()
    BC = deque()
    S = []
    for i in range(n):
        s = input()
        S.append(s)
        if s == 'AB':
            if a>0 or b>0:
                a -= 1
                b -= 1
                AB.append(i)
            if a <= 0 and b <= 0:
                if AC and BC:
                    j = AC.popleft()
                    ans[j] = 'A'
                    k = BC.popleft()
                    ans[k] = 'B'
                else:
                    print('No')
                    return
        if s == 'AC':
            if a>0 or c>0:
                a -= 1
                c -= 1
                AC.append(i)
            if a <= 0 and c <= 0:
                if AB and BC:
                    j = AB.popleft()
                    ans[j] = 'A'
                    k = BC.popleft()
                    ans[k] = 'C'
                else:
                    print('No')
                    return
        if s == 'BC':
            if b>0 or c>0:
                c -= 1
                b -= 1
                BC.append(i)
            if c <= 0 and b <= 0:
                if AB and AC:
                    j = AB.popleft()
                    ans[j] = 'B'
                    k = AC.popleft()
                    ans[k] = 'C'
                else:
                    print('No')
                    return
    if a >= 0 and b >= 0 and c < 0:
        if c + len(AC) + len(BC) < 0:
            print('No')
            return
        while AC:
            i = AC.popleft()
            ans[i] = 'C'
        while BC:
            i = BC.popleft()
            ans[i] = 'C'
        while AB:
            i = AB.popleft()
            ans[i] = 'A'
    if a >= 0 and b < 0 and c >= 0:
        if b + len(AB) + len(BC) < 0:
            print('No')
            return
        while AB:
            i = AB.popleft()
            ans[i] = 'B'
        while BC:
            i = BC.popleft()
            ans[i] = 'B'
        while AC:
            i = AC.popleft()
            ans[i] = 'A'
    if a < 0 and b >= 0 and c >= 0:
        if c + len(AC) + len(BC) < 0:
            print('No')
            return
        while AC:
            i = AC.popleft()
            ans[i] = 'A'
        while BC:
            i = BC.popleft()
            ans[i] = 'A'
        while AB:
            i = AB.popleft()
            ans[i] = 'C'
    for i in range(n):
        t = S[i]
        if t == 'AC':
            if ans[i] == 'A':
                aa += 1
                cc -= 1
            else:
                aa -= 1
                cc += 1
        if t == 'BC':
            if ans[i] == 'B':
                bb += 1
                cc -= 1
            else:
                bb -= 1
                cc += 1
        if t == 'AB':
            if ans[i] == 'A':
                aa += 1
                bb -= 1
            else:
                aa -= 1
                bb += 1
        if aa<0 or bb<0 or cc<0:
            print('No')
            return


    print('Yes')
    for i in range(n):
        print(ans[i])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
s = [input() for i in range(n)]+[0]
# if [a,b,c].count(0) >= 2:
#     print("No")
ans = ""

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

Submission status: Wrong Answer

Solution code:
N,A,B,C = list(map(int,input().split()))
import sys
input = sys.stdin.readline
S = tuple(input().strip() for i in range(N))
tup = [A,B,C]

def idx(ch):
  return "ABC".index(ch)

def search(ABC,path):
  res = ""
  for i,(l1,l2) in enumerate(path):
    if ABC[idx(l1)] > ABC[idx(l2)]:
      ABC[idx(l1)] -= 1
      ABC[idx(l2)] += 1
      res += l2
    elif ABC[idx(l1)] < ABC[idx(l2)] or ABC[idx(l1)] + ABC[idx(l2)] >= 2:
      ABC[idx(l1)] += 1
      ABC[idx(l2)] -= 1
      res += l1
    elif ABC[idx(l1)] + ABC[idx(l2)]== 0:
      return False,""
    else:
      # 両方のpathを探索する
      ABC_j = [i for i in ABC]
      ABC_j[idx(l1)] += 1
      ABC_j[idx(l2)] -= 1
      
      ABC_k = [i for i in ABC]
      ABC_k[idx(l1)] -= 1
      ABC_k[idx(l2)] += 1
      
      jT,jpath = search(ABC_j,path[i:])
      if jT:
        return jT,res + jpath
      
      kT,kpath = search(ABC_k,path[i:])
      if kT:
        return kT,res + kpath
      return False, ""
  else:
    return True,res
res, p = search(tup,S)
if res:
  print("Yes")
  for c in p:
    print(c)
else:
  print("No")

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
ans_list= []
chk = 0
count = 1
for i in range(N):
    s = input()
    if chk == 1:
        if s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 0:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 0:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                else:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 2:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 0:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 0:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 3:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 0:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                else:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 0:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    if s == "AB" and A == 0 and B == 0:
        print("No")
        break
    elif s == "BC" and B == 0 and C == 0:
        print("No")
        break
    elif s == "AC" and A == 0 and C == 0:
        print("No")
        break
    else:
        if s == "AB":
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 0:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B != 1 :
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B == 1:
                chk = 1
            else:
                A += 1
                B -= 1
                ans_list.append(["A"])
        elif s == "AC":
            if A == 0:
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 0:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and B != 1 :
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and B == 1:
                chk = 2
            else:
                A += 1
                C -= 1
                ans_list.append(["A"])
        else:
            if B == 0:
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 0:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C != 1 :
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 1 and B != 1:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C == 1:
                chk = 3
            else:
                B += 1
                C -= 1
                ans_list.append(["B"])
if chk == 1:
    for i in range(count):
        if i%2 == 0:
            A += 1
            B -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            B += 1
            ans_list.append(["B"])
elif chk == 2:
        if i%2 == 0:
            A += 1
            C -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            C += 1
            ans_list.append(["C"])
elif chk == 3:
        if i%2 == 0:
            B += 1
            C -= 1
            ans_list.append(["B"])
        else:
            B -= 1
            C += 1
            ans_list.append(["C"])
if len(ans_list) == N:
    print("Yes")
    for i in range(N):
        print(*ans_list[i])

Submission status: Wrong Answer

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,A,B,C=MI()
    D = { "A":A,"B":B,"C":C }
    ans = []
    for i in range(N):
        S=ST()
        if(D[S[0]]==D[S[1]]==0):
            print("No")
            return
        if(D[S[0]]<D[S[1]]):
            ans.append(S[0])
            D[S[0]]+=1
            D[S[1]]-=1
        else:
            ans.append(S[1])
            D[S[0]]-=1
            D[S[1]]+=1
    print("Yes")
    print(*ans,sep="\n")
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

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

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


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

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

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


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






# main


if nonZero >=2:
    show()


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