Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

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

#############
# 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 A==B==1 and A+B+C==2 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 A==C==1 and A+B+C==2 and i!=n-1:
        nitem = l[i+1]
        if item == nitem:
          A += 1
          C -= 1
          a.append("A")
        else:
          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 B==C==1 and A+B+C==2 and i!=n-1:
        nitem = l[i+1]
        if item == nitem:
          B += 1
          C -= 1
          a.append("B")
        else:
          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:
n,a,b,c = map(int,input().split())

abc = [a,b,c]

def get_index(ch):
    if (ch == "A"):return 0
    elif (ch == "B"):return 1
    else:return 2

ans = ["Yes"]
S = [input() for _ in range(n)]

for i in range(n):
    s = S[i]
    s1 = get_index(s[0])
    s2 = get_index(s[1])
    
    if abc[s1] == 0 and abc[s2] == 0:
        ans[0] = "No"
    
    elif abc[s1] == 0 or abc[s2] == 0:
        if abc[s2] == 0:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(s[1])
        else:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])
    
    elif abc[s1] + abc[s2] == 2 and i != n-1 and S[i] != S[i+1]:
        next_s = S[i+1]
        next_s1 = get_index(next_s[0])
        next_s2 = get_index(next_s[1]) 
        if s1 == next_s1:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])
        else:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(s[1])
    
    else:
        if abc[s1] > 1:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(s[1])
        else:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])

for a in ans:
    print(a)
    if a == "No":
        break

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 A==B==1 and A+B+C==2 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 A==C==1 and A+B+C==2 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 B==C==1 and A+B+C==2 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 == "BC":
          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:
n,a,b,c = map(int,input().split())

O = [input() for _ in range(n)]+['DD']
def finish():
  print('No')
  exit()
ans = []
for i in range(n):
  o = O[i]
  on = O[i+1]
  if o =='AB':
    if a==b==0:
      finish()
    elif a==0:
      ans.append('A')
      a += 1
      b -= 1
    elif b==0:
      ans.append('B')
      a-= 1
      b+= 1
    elif a == 1 and c==0 and on == 'AC':
      ans.append('A')
      a += 1
      b -= 1
    elif b==1 and c==0 and on =='BC':
      ans.append('B')
      a-= 1
      b+= 1
    elif a>b:
      ans.append('B')
      a-=1
      b+=1
    else:
      ans.append('A')
      a+=1
      b-=1
  elif o =='AC':
    if a==c==0:
      finish()
    elif a==0:
      ans.append('A')
      a += 1
      c -= 1
    elif c==0:
      ans.append('C')
      a-= 1
      c+= 1
    elif a == 1 and b==0 and on == 'AB':
      ans.append('A')
      a += 1
      c -= 1
    elif b==1 and c==0 and on =='BC':
      ans.append('C')
      a-= 1
      c+= 1
    elif a>c:
      ans.append('C')
      a-=1
      c+=1
    else:
      ans.append('A')
      a+=1
      c-=1
  if o =='BC':
    if c==b==0:
      finish()
    elif c==0:
      ans.append('C')
      c += 1
      b -= 1
    elif b==0:
      ans.append('B')
      c-= 1
      b+= 1
    elif c == 1 and a==0 and on == 'AC':
      ans.append('C')
      c += 1
      b -= 1
    elif b==1 and a==0 and on =='AB':
      ans.append('B')
      c-= 1
      b+= 1
    elif c>b:
      ans.append('B')
      c-=1
      b+=1
    else:
      ans.append('C')
      c+=1
      b-=1
print('Yes')
for a in ans:
  print(a)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3

import sys
# import math
# from string import ascii_lowercase, ascii_upper_case, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from copy import deepcopy                    # to copy multi-dimentional matrix without reference
# from fractions import gcd                    # for Python 3.4 (previous contest @AtCoder)


def main():
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79...e+308
    # inf = 2 ** 64 - 1               # (for fast JIT compile in PyPy) 1.84...e+19
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    

    def parse(com):
        if com == 'AB':
            return 0, 1
        elif com == 'BC':
            return 1, 2
        else:
            return 0, 2

    def sim_1(t_copy, commands):
        s = 'ABC'
        result = []
        for com in commands:
            i, j = parse(com)
            if t_copy[i] == 0 and t_copy[j] == 0:
                return False, result
            elif t_copy[i] == 0:
                result.append(s[i])
                t_copy[i], t_copy[j] = 1, 0
            else:
                result.append(s[j])
                t_copy[i], t_copy[j] = 0, 1
        return True, result
    

    def sim_2(t_copy, commands):
        s = 'ABC'
        size = len(commands)
        result = []
        for k in range(size):
            com = commands[k]
            i, j = parse(com)
            if t_copy[i] == 0 and t_copy[j] == 0:
                return False, result
            elif t_copy[i] == 0:
                result.append(s[i])
                t_copy[i] += 1
                t_copy[j] -= 1
            elif t_copy[j] == 0:
                result.append(s[j])
                t_copy[j] += 1
                t_copy[i] -= 1
            else:
                # 片方はどうしても 0 になってしまう
                if t_copy[i] == 1 and t_copy[j] == 1:
                    if k != size - 1:
                        next_com = commands[k + 1]
                        next_i, next_j = parse(next_com)
                        if i == next_i and j != next_j and t_copy[next_i] == 1 and t_copy[next_j] == 0:
                            result.append(s[i])
                            t_copy[i] += 1
                            t_copy[j] -= 1
                            continue
                        elif i != next_i and j == next_j and t_copy[next_i] == 0 and t_copy[next_j] == 1:
                            result.append(s[j])
                            t_copy[j] += 1
                            t_copy[i] -= 1
                            continue
                    # 自由？
                    result.append(s[i])
                    t_copy[i] += 1
                    t_copy[j] -= 1
                # 0 にならないですむ                 
                elif t_copy[i] <= t_copy[j]:
                    result.append(s[i])
                    t_copy[i] += 1
                    t_copy[j] -= 1
                else:
                    result.append(s[j])
                    t_copy[j] += 1
                    t_copy[i] -= 1
        return True, result
            
    
    n, a, b, c  = mi()
    t = [a, b, c]
    commands = [input() for _ in range(n)]
    if sum(t) == 0:
        predicate, result = False, []
    elif sum(t) == 1:
        predicate, result = sim_1(t[:], commands)
    else:
        predicate, result = sim_2(t[:], commands)
    
    if predicate:
        print('Yes')
        print(*result, sep='\n')
    else:
        print('No')


    

if __name__ == "__main__":
    main()

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:
  print("No")
  exit()
  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 A==B==1 and A+B+C==2 and i!=n-1:
        nitem = l[i+1]
        if item == nitem:
          A += 1
          B -= 1
          a.append("A")
        else:
          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 A==C==1 and A+B+C==2 and i!=n-1:
        nitem = l[i+1]
        if item == nitem:
          A += 1
          C -= 1
          a.append("A")
        else:
          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 B==C==1 and A+B+C==2 and i!=n-1:
        nitem = l[i+1]
        if item == nitem:
          B += 1
          C -= 1
          a.append("B")
        else:
          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:
n,a,b,c = map(int,input().split())

abc = [a,b,c]

def get_index(ch):
    if (ch == "A"):return 0
    elif (ch == "B"):return 1
    else:return 2

ans = ["Yes"]
S = [input() for _ in range(n)]

for i in range(n):
    s = S[i]
    s1 = get_index(s[0])
    s2 = get_index(s[1])
    
    if abc[s1] == 0 and abc[s2] == 0:
        ans[0] = "No"
    
    elif abc[s1] + abc[s2] == 1:
        if abc[s1] == 1:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(s[1])
        else:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])
    
    elif abc[s1] + abc[s2] == 2 and i != n-1 and S[i] != S[i+1]:
        next_s = S[i+1]
        next_s1 = get_index(next_s[0])
        next_s2 = get_index(next_s[1]) 
        if s1 == next_s1:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])
        else:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(s[1])
    
    else:
        if abc[s1] > 1:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(s[1])
        else:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])

for a in ans:
    print(a)
    if a == "No":
        break

Submission status: Wrong Answer

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

abc = [a,b,c]

def get_index(ch):
    if (ch == "A"):return 0
    elif (ch == "B"):return 1
    else:return 2

ans = ["Yes"]
S = [input() for _ in range(n)]

for i in range(n):
    s = S[i]
    s1 = get_index(s[0])
    s2 = get_index(s[1])
    
    if abc[s1] == 0 and abc[s2] == 0:
        ans[0] = "No"
    
    elif abc[s1] + abc[s2] == 1:
        if abc[s1] == 1:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(s[1])
        else:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])
    
    elif abc[s1] + abc[s2] == 2 and i != n-1 and S[i] != S[i+1]:
        next_s = S[i+1]
        next_s1 = get_index(next_s[0])
        next_s2 = get_index(next_s[1]) 
        if s1 == next_s1:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])
        else:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(s[1])
    
    else:
        if abc[s1] != 1:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(s[1])
        else:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])

for a in ans:
    print(a)
    if a == "No":
        break

Submission status: Wrong Answer

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

ans=[]
 
flag=True
 
for i in range(n):
    if s[i]=='AB':
        if a==0 and b==0:
            flag=False
            break
        if a==0 or (b!=0 and a==1 and s[i+1]=='AC'):
            ans.append('A')
            a+=1
            b-=1
        else:
            ans.append('B')
            a-=1
            b+=1
    elif s[i]=='AC':
        if a==0 and c==0:
            flag=False
            break
        if a==0 or (c!=0 and a==1 and s[i+1]=='AB'):
            ans.append('A')
            a+=1
            c-=1
        else:
            ans.append('C')
            a-=1
            c+=1
    else:
        if c==0 and b==0:
            flag=False
            break
        if c==0 or (c!=0 and c==1 and s[i+1]=='AC'):
            ans.append('C')
            c+=1
            b-=1
        else:
            ans.append('B')
            c-=1
            b+=1

if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
a=min(3,a)
b=min(3,b)
S=[input() for i in range(n)]
X=a+b+c
idx=0
log=[0]*n#0,1のいずれか,
plog=[(a,b)]*(n+1)#1から始まるので注意

flag=False

while idx!=n:

    #2回以上戻ってたらもうダメ 0回目の時点でアウトでもダメ
    if idx<n-2 and log[idx+2]==2:
        flag=True
        break
    if idx==0 and log[0]==2:
        flag=True
        break

    a,b=plog[idx]
    dx,dy=0,0
    if S[idx]=='AB':
        dx,dy=1,-1
    elif S[idx]=='BC':
        dx,dy=0,1
    else:#AC
        dx,dy=1,0

    if log[idx]==0:
        a+=dx
        b+=dy
    elif log[idx]==1:
        a-=dx
        b-=dy
    else:
        idx-=1
        log[idx]+=1
        continue
    
    if a<0 or b<0 or X-(a+b)<0:
        log[idx]+=1
        continue
    else:
        idx+=1
        plog[idx]=(a,b)

if flag:
    print('No')
else:
    print('Yes')
    for i,s in zip(log,S):
        print(s[i])