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 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:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    before=[[],[],[]]
    ans=[-1]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]]:
                k,l=before[d[s]].pop()
                n[d[s]]+=1
                n[l]-=1
                ans[k]=d[s]
            elif before[(d[s]+1)%3]:
                k,l=before[(d[s]+1)%3].pop()
                n[(d[s]+1)%3]+=1
                n[l]-=1
                ans[k]=(d[s]+1)%3
            else:
                print('No')
                exit()
        if n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            if n[d[s]]<n[(d[s]+1)%3]:
                ans[i]=d[s]
                n[d[s]]+=1
                n[(d[s]+1)%3]-=1
                before[(d[s]+1)%3].append((i,d[s]))
            else:
                ans[i]=(d[s]+1)%3
                n[d[s]]-=1
                n[(d[s]+1)%3]+=1
                before[d[s]].append((i,(d[s]+1)%3))
    print('Yes')
    d2=['A','B','C']
    for a in ans:
        print(d2[a])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from itertools import combinations

N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]

cnt = {'A': A, 'B': B, 'C': C}

def out(T):
    now = {'A': 0, 'B': 0, 'C': 0}
    for s in T:
        now[s] += 1

    ans = []
    for i, s in enumerate(S):
        if min(now.values()) < 0 or now[s[0]] == now[s[1]] == 0:
            return []
        if now[s[0]] > now[s[1]]:
            ans.append(s[1])
            now[s[0]] -= 1
            now[s[1]] += 1
            continue
        if now[s[0]] < now[s[1]]:
            ans.append(s[0])
            now[s[0]] += 1
            now[s[1]] -= 1
            continue
        if i == N - 1:
            ans.append(s[0])
            continue
        nx = S[i + 1]
        if nx == s:
            ans.append(s[0])
            now[s[0]] += 1
            now[s[1]] -= 1
            continue
        k = list(set(list(s)) & set(list(nx)))
        k = k[0]
        ans.append(k)
        now[k] += 1
        k = s.replace(k, '')
        now[k] -= 1

    if min(now.values()) < 0:
        return []
    return ans

for s in combinations(['', '', 'A', 'B', 'C'], r=3):
    s = ''.join(s)
    for t in s:
        if cnt[t] == 0:
            break
    else:
        ans = out(s)
        if len(ans) == 0:
            continue
        print('Yes')
        print(*ans, sep='\n')
        exit()

print('No')

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
n,a,b,c=map(int,input().split())
s=[]
for i in range(n):
    sub=input().rstrip()
    s.append(sub)
if a+b+c==0:
    print('No')
    sys.exit()
if a+b+c==1:
    com=[]
    for i in s:
        if i=='AB':
            if a==0 and b==0:
                print('No')
                sys.exit()
            else:
                if a>b:
                    com.append('B')
                else:
                    com.append('A')
                a,b=b,a
        elif i=='AC':
            if a==0 and c==0:
                print('No')
                sys.exit()
            else:
                if a>c:
                    com.append('C')
                else:
                    com.append('A')
                a,c=c,a
        elif i=='BC':
            if b==0 and c==0:
                print('No')
                sys.exit()
            else:
                if b>c:
                    com.append('C')
                else:
                    com.append('B')
                b,c=c,b
    print('Yes')
    for i in com:
        print(com)
    sys.exit()
if (s[0]=='AB' and a**2+b**2==0) or (s[0]=='AC' and a**2+c**2==0) or (s[0]=='BC' and c**2+b**2==0):
    print('No')
else:
    print('Yes')
    for i in range(n):
        check=True
        if s[i]=='AB':
            at=a+1
            bt=b-1
            ct=c
            if bt<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('A')
            else:
                a-=1
                b+=1
                print('B')
        elif s[i]=='AC':
            at=a+1
            bt=b
            ct=c-1
            if ct<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('A')
            else:
                a-=1
                c+=1
                print('C')
        elif s[i]=='BC':
            at=a
            bt=b+1
            ct=c-1
            if ct<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('B')
            else:
                b-=1
                c+=1
                print('C')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    before=[[],[],[]]
    ans=[-1]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]]:
                k=before[d[s]].pop()
                n[d[s]]+=1
                n[(d[s]-1)%3]-=1
                ans[k]=d[s]
            elif before[(d[s]+1)%3]:
                k=before[(d[s]+1)%3].pop()
                n[(d[s]+1)%3]+=1
                n[d[s]]-=1
                ans[k]=(d[s]+1)%3
            else:
                print('No')
                exit()
        if n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
            before[(d[s]+1)%3].append(i)
    print('Yes')
    d2=['A','B','C']
    for a in ans:
        print(d2[a])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
q = [(0, 1) if si == 'AB' else (1, 2) if si == 'BC' else (0, 2) for si in (input() for _ in range(n))]
ans = []
ch = ['A', 'B', 'C']
d = [a, b, c]

nxt = [[-1] * 3] * n
t = [-1, -1, -1]
for i, (x, y) in reversed(list(enumerate(q))):
    nxt[i] = t
    t[x], t[y] = y, x
for (x, y), t in zip(q, nxt):
    if d[x] == d[y] == 0:
        print('No')
        break
    if d[y] == 0 or d[x] >= 2 or t[3 - x - y] == y:
            x, y = y, x
    d[x] += 1
    d[y] -= 1
    ans.append(ch[x])
else:
    print('Yes', *ans, sep='\n')

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(S[-1])
    S.append(S[-1])#適当に入れとけ
    
    ans=[]
    flag=1

    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

                
        
        
    if flag==-1:
        print("No")
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()

Submission status: Wrong Answer

Solution code:
import sys


def main():
    N, A, B, C = [int(v) for v in input().split()]

    total = A + B + C

    if total == 0:
        print('No')
        exit()

    choice = []
    if total != 2:
        for _ in range(N):
            s = sys.stdin.readline().rstrip()
            if s == 'AB':
                if A == 0 and B == 0:
                    print('No')
                    exit()
                if A < B:
                    B -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    B += 1
                    choice.append('B')
            elif s == 'BC':
                if B == 0 and C == 0:
                    print('No')
                    exit()
                if B < C:
                    C -= 1
                    B += 1
                    choice.append('B')
                else:
                    B -= 1
                    C += 1
                    choice.append('C')
            elif s == 'AC':
                if A == 0 and C == 0:
                    print('No')
                    exit()
                if A < C:
                    C -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    C += 1
                    choice.append('C')
    else:
        s = sys.stdin.readline().rstrip()
        for _ in range(N - 1):
            s1 = sys.stdin.readline().rstrip()
            if s == 'AB':
                if A == 0 and B == 0:
                    print('No')
                    exit()
                if A < B:
                    B -= 1
                    A += 1
                    choice.append('A')
                elif A > B:
                    A -= 1
                    B += 1
                    choice.append('B')
                elif s1 == 'AC':
                    A += 1
                    B -= 1
                    choice.append('A')
                else:
                    A -= 1
                    B += 1
                    choice.append('B')

            elif s == 'BC':
                if B == 0 and C == 0:
                    print('No')
                    exit()
                if B < C:
                    C -= 1
                    B += 1
                    choice.append('B')
                elif C > B:
                    B -= 1
                    C += 1
                    choice.append('C')
                elif s1 == 'AC':
                    C += 1
                    B -= 1
                    choice.append('C')
                else:
                    C -= 1
                    B += 1
                    choice.append('B')

            elif s == 'AC':
                if A == 0 and C == 0:
                    print('No')
                    exit()
                if A < C:
                    C -= 1
                    A += 1
                    choice.append('A')
                elif A > C:
                    A -= 1
                    C += 1
                    choice.append('C')
                elif s1 == 'AB':
                    A += 1
                    C -= 1
                    choice.append('A')
                else:
                    A -= 1
                    C += 1
                    choice.append('C')
            s = s1

        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                exit()
            if A < B:
                B -= 1
                A += 1
                choice.append('A')
            else:
                A -= 1
                B += 1
                choice.append('B')

        elif s == 'BC':
            if B == 0 and C == 0:
                print('No')
                exit()
            if B < C:
                C -= 1
                B += 1
                choice.append('B')
            else:
                B -= 1
                C += 1
                choice.append('C')

        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                exit()
            if A < C:
                C -= 1
                A += 1
                choice.append('A')
            else:
                A -= 1
                C += 1
                choice.append('C')

    print('Yes')
    for c in choice:
        print(c)


main()

Submission status: Wrong Answer

Solution code:
import sys


def main():
    N, A, B, C = [int(v) for v in input().split()]

    total = A + B + C

    if total == 0:
        print('No')
        exit()

    choice = []
    if total != 2:
        for _ in range(N):
            s = sys.stdin.readline().rstrip()
            if s == 'AB':
                if A == 0 and B == 0:
                    print('No')
                    exit()
                if A < B:
                    B -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    B += 1
                    choice.append('B')
            elif s == 'BC':
                if B == 0 and C == 0:
                    print('No')
                    exit()
                if B < C:
                    C -= 1
                    B += 1
                    choice.append('B')
                else:
                    B -= 1
                    C += 1
                    choice.append('C')
            elif s == 'AC':
                if A == 0 and C == 0:
                    print('No')
                    exit()
                if A < C:
                    C -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    C += 1
                    choice.append('C')
    else:
        s = sys.stdin.readline().rstrip()
        for _ in range(N):
            s1 = sys.stdin.readline().rstrip()
            if s == 'AB':
                if A == 0 and B == 0:
                    print('No')
                    exit()
                if A < B:
                    B -= 1
                    A += 1
                    choice.append('A')
                elif A > B:
                    A -= 1
                    B += 1
                    choice.append('B')
                elif s1 == 'AC':
                    A += 1
                    B -= 1
                    choice.append('A')
                else:
                    A -= 1
                    B += 1
                    choice.append('B')

            elif s == 'BC':
                if B == 0 and C == 0:
                    print('No')
                    exit()
                if B < C:
                    C -= 1
                    B += 1
                    choice.append('B')
                elif C > B:
                    B -= 1
                    C += 1
                    choice.append('C')
                elif s1 == 'AC':
                    C += 1
                    B -= 1
                    choice.append('C')
                else:
                    C -= 1
                    B += 1
                    choice.append('B')

            elif s == 'AC':
                if A == 0 and C == 0:
                    print('No')
                    exit()
                if A < C:
                    C -= 1
                    A += 1
                    choice.append('A')
                elif A > C:
                    A -= 1
                    C += 1
                    choice.append('C')
                elif s1 == 'AB':
                    A += 1
                    C -= 1
                    choice.append('A')
                else:
                    A -= 1
                    C += 1
                    choice.append('C')
            s = s1

        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                exit()
            if A < B:
                B -= 1
                A += 1
                choice.append('A')
            else:
                A -= 1
                B += 1
                choice.append('B')

        elif s == 'BC':
            if B == 0 and C == 0:
                print('No')
                exit()
            if B < C:
                C -= 1
                B += 1
                choice.append('B')
            else:
                B -= 1
                C += 1
                choice.append('C')

        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                exit()
            if A < C:
                C -= 1
                A += 1
                choice.append('A')
            else:
                A -= 1
                C += 1
                choice.append('C')

    print('Yes')
    for c in choice:
        print(c)


main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    before=[-1,-1,-1]
    ans=[-1]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]]!=-1:
                n[d[s]]+=1
                n[(d[s]-1)%3]-=1
                ans[before[d[s]]]=d[s]
                before[d[s]]=-1
            elif before[(d[s]+1)%3]!=-1:
                n[(d[s]+1)%3]+=1
                n[d[s]]-=1
                ans[before[(d[s]+1)%3]]=(d[s]+1)%3
                before[(d[s]+1)%3]=-1
            else:
                print('No')
                exit()
        if n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
            before[(d[s]+1)%3]=i
    print('Yes')
    d2=['A','B','C']
    for a in ans:
        print(d2[a])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]

cnt = {'A': A, 'B': B, 'C': C}

def sol(start):
    now = start
    for s in S:
        if not now in s:
            return 'ABC'.replace(now, '')
        now = s.replace(now, '')
    return start

ans = []
for a in 'ABC':
    if cnt[a] == 0:
        continue
    s = sol(a)
    for s in sol(a):
        if cnt[s] == 0:
            break
    else:
        if not a in s:
            s += a
        ans.append(s)

if len(ans) == 0:
    print('No')
    exit()

def out(T):
    now = {'A': 0, 'B': 0, 'C': 0}
    for s in T:
        now[s] += 1

    ans = []
    for i, s in enumerate(S):
        if now[s[0]] > now[s[1]]:
            ans.append(s[1])
            now[s[0]] -= 1
            now[s[1]] += 1
            continue
        if now[s[0]] < now[s[1]]:
            ans.append(s[0])
            now[s[0]] += 1
            now[s[1]] -= 1
            continue
        if i == N - 1:
            ans.append(s[0])
            continue
        nx = S[i + 1]
        if nx == s:
            ans.append(s[0])
            now[s[0]] += 1
            now[s[1]] -= 1
            continue
        k = list(set(list(s)) & set(list(nx)))
        k = k[0]
        ans.append(k)
        now[k] += 1
        k = s.replace(k, '')
        now[k] -= 1

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

out(ans[0])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7 # 998244353
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    n, a, b, c = map(int,input().split())
    state = [a, b, c]
    s = sum(state)
    zeroes = state.count(0)
    D = {"AB" : (0, 1), "AC" : (0, 2), "BC" : (1, 2)}
    E = {0 : 'A', 1 : 'B', 2 : 'C'}
    S = [D[input()] for _ in range(n)]

    # 最初から終わってるのは終わらせる
    i, j = S[-1]
    if state[i] == 0 and state[j] == 0:
        print("No")
        return

    # s != 2 ならやるだけ
    if s != 2:
        ans = []
        S.reverse()
        while S:
            i, j = S.pop()
            if state[i] == 0 and state[j] == 0:
                print("No")
                return
            if state[i] < state[j]:
                state[i] += 1
                state[j] -= 1
                ans.append(E[i])
            else:
                state[i] -= 1
                state[j] += 1
                ans.append(E[j])
        print("Yes")
        print(*ans, sep = '\n')
        return

    # s == 2 のときは考えなければならない
    dp = {(a, b, c) : True}
    prev = [{}] * n
    for k in range(n):
        i, j = S[k]
        ndp = {}
        for key in dp:
            if key[i] > 0:
                tmp = list(key)
                tmp[i] -= 1
                tmp[j] += 1
                tmp = tuple(tmp)
                ndp[tmp] = True
                prev[k][tmp] = key
            if key[j] > 0:
                tmp = list(key)
                tmp[i] += 1
                tmp[j] -= 1
                tmp = tuple(tmp)
                ndp[tmp] = True
                prev[k][tmp] = key
        dp = ndp

    if len(dp) == 0:
        print("No")
        return

    ans = []
    for key in dp:
        now = key
        break

    for k in range(n - 1, -1, -1):
        pa, pb, pc = prev[k][now]
        a, b, c = now
        if a > pa:
            ans.append('A')
        elif b > pb:
            ans.append('B')
        elif c > pc:
            ans.append('C')
        now = prev[k][now]

    ans.reverse()
    print("Yes")
    print(*ans, sep = '\n')
resolve()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    before=[-1,-1,-1]
    ans=[-1]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]]!=-1:
                n[d[s]]+=1
                n[(d[s]-1)%3]-=1
                ans[before[d[s]]]=d[s]
                before[d[s]]=-1
            elif before[(d[s]+1)%3]!=-1:
                n[(d[s]+1)%3]+=1
                n[d[s]]-=1
                ans[before[(d[s]+1)%3]]=(d[s]+1)%3
                before[(d[s]+1)%3]=-1
            else:
                print('No')
                exit()
        if n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            ans[i]=d[s]
            before[(d[s]+1)%3]=i
    print('Yes')
    d2=['A','B','C']
    for a in ans:
        print(d2[a])

if __name__ == '__main__':
    main()