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:
import bisect,sys
import math
from collections import deque

def sRaw():
    return input().rstrip("\r")


def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29

DIV = (10**9) + 7

def mod_inv_prime(a, mod=DIV):
    return pow(a, mod-2, mod)


def mod_inv(a, b):
    r = a
    w = b
    u = 1
    v = 0
    while w != 0:
        t = r//w
        r -= t*w
        r, w = w, r
        u -= t*v
        u, v = v, u
    return (u % b+b) % b


def CONV_TBL(max, mod=DIV):
    fac, finv, inv = [0]*max, [0]*max, [0]*max
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    inv[1] = 1
    for i in range(2, max):
        fac[i] = fac[i-1]*i % mod
        inv[i] = mod - inv[mod % i] * (mod//i) % mod
        finv[i] = finv[i-1]*inv[i] % mod

    class CONV:
        def __init__(self):
            self.fac =fac
            self.finv = finv
            pass

        def ncr(self, n, k):
            if(n < k):
                return 0
            if(n < 0 or k < 0):
                return 0
            return fac[n]*(finv[k]*finv[n-k] % DIV) % DIV
        
    return CONV()


def cumsum(As):
    s = 0
    for a in As:
        s+=a
        yield s


sys.setrecursionlimit(200005)


def main():
    N,A,B,C = isRaw()
    AB = A+B
    AC = A+C
    BC = B+C
    ans=[]
    Ss = [sRaw() for n in range(N)]
    nABs = [sidx for sidx,s in enumerate(Ss) if s=="AB"]
    nACs = [sidx for sidx, s in enumerate(Ss) if s == "AC"]
    nBCs = [sidx for sidx, s in enumerate(Ss) if s == "BC"]
    nABidx = 0 if len(nABs)>0 else -1
    nACidx = 0 if len(nACs) > 0 else -1
    nBCidx = 0 if len(nBCs)>0 else -1
    for n in range(N):
        s = Ss[n]
        if s=="AB":
            nABidx = nABidx+1 if len(nABs)>nABidx+1 else -1
            if AB==0:
                print("No")
                return
            if AC >BC:
                ans.append("B")
                AC-=1
                BC+=1
            elif AC<BC:
                ans.append("A")
                AC+=1
                BC-=1
            else:
                if nBCidx == -1 or (nBCidx != -1 and nACidx != -1 and nACs[nACidx] < nBCs[nBCidx]):
                    ans.append("A")
                    AC += 1
                    BC -= 1
                else:
                    ans.append("B")
                    AC -= 1
                    BC += 1
        if s=="AC":
            nACidx = nACidx+1 if len(nACs) > nACidx+1 else -1
            if AC == 0:
                print("No")
                return
            if AB > BC:
                ans.append("C")
                AB -= 1
                BC += 1
            elif AB < BC:
                ans.append("A")
                AB += 1
                BC -= 1
            else:
                if nBCidx == -1 or (nBCidx != -1 and nABidx != -1 and nABs[nABidx] < nBCs[nBCidx]):
                    ans.append("A")
                    AB += 1
                    BC -= 1
                else:
                    ans.append("C")
                    AB -= 1
                    BC += 1
        if s == "BC":
            nBCidx = nBCidx+1 if len(nBCs) > nBCidx+1 else -1
            if BC == 0:
                print("No")
                return
            if AB > AC:
                ans.append("C")
                AB -= 1
                AC += 1
            elif AB < BC:
                ans.append("B")
                AB += 1
                AC -= 1
            else:
                if nACidx == -1 or (nACidx != -1 and nABidx != -1 and nABs[nABidx] < nACs[nACidx]):
                    ans.append("B")
                    AB += 1
                    AC -= 1
                else:
                    ans.append("C")
                    AB -= 1
                    AC += 1
    print("Yes")
    print("\n".join(ans))
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

def best_balance(x, y, L):
    if L[x] < L[y]:
        return y, x
    else:
        return x, y

ALPHABETS = ('A', 'B', 'C')

def main():
    #N, M = map(int, input().split())
    N, A, B, C = map(int, input().split())
    L = [A, B, C]
    S = []
    for i in range(N):
        S.append(input().strip())

    result = []
    tie = {}
    swap = {}
    for idx, s in enumerate(S):
        if s == 'AB':
            from_i, to_i = best_balance(0, 1, L)
        if s == 'BC':
            from_i, to_i = best_balance(1, 2, L)
        if s == 'AC':
            from_i, to_i = best_balance(0, 2, L)

        if L[from_i] == 1 and L[to_i] == 1:
            tie[from_i] = (idx, to_i)

        if L[from_i] == 0 and L[to_i] == 0:
            if from_i in tie:
                index, to_index = tie[from_i]
                swap[index] = to_index
                L[to_index] -= 1
                L[from_i] += 1
                del tie[from_i]
            elif to_i in tie:
                index, to_index = tie[to_i]
                swap[index] = to_index
                L[to_index] -= 1
                L[to_i] += 1
                del tie[to_i]
                from_i, to_i = to_i, from_i
            else:
                print("No")
                return

        L[from_i] -= 1
        L[to_i] += 1

        result.append(to_i)

    print("Yes")
    for idx, r in enumerate(result):
        if idx in swap:
            print(ALPHABETS[swap[idx]])
        else:
            print(ALPHABETS[r])


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c = list(map(int,input().split()))
an=0;d={};l=[];x=y=z=0
for i in range(n):
  s=input();l.append(s)
  x+=int(s[0]=='A')
  z+=int(s[-1]=='C')
  y+=int(s[-1]=='B')+int(s[0]=='B')
x=a-x;y=b-y;z=c-z
if (x<0)+(y<0)+(z<0)>=2:
  print("No");exit()
a=[]
for i in range(n):
  if l[i]=='AC':
    if x>=0:
      z+=1;a.append('C')
    else:
      x+=1;a.append('A')
  if l[i]=='AB':
    if x>=0:
      y+=1;a.append('B')
    else:
      x+=1;a.append('A')
  if l[i]=='BC':
    if y>=0:
      z+=1;a.append('C')
    else:
      y+=1;a.append('B')
print("Yes")
for i in a:
  print(i)

Submission status: Wrong Answer

Solution code:
def g_solve(N, A, B, C):
  seq = []
  for _ in range(N):
    s = input()
    if s == 'AB':
      if A > B:
        A -= 1
        B += 1
        seq.append('B')
      else:
        A += 1
        B -= 1
        seq.append('A')
    elif s == 'AC':
      if A > C:
        A -= 1
        C += 1
        seq.append('C')
      else:
        A += 1
        C -= 1
        seq.append('A')
    elif s == 'BC':
      if B > C:
        B -= 1
        C += 1
        seq.append('C')
      else:
        B += 1
        C -= 1
        seq.append('B')
    if A < 0 or B < 0 or C < 0:
      print('No')
      return
  print('Yes')
  print('\n'.join(seq))    


def dp_solve(N, a,b,c):
  state = {}
  state[(a, b, c)] = []
  for _ in range(N):
    s = input()
    next_state = {}
    for a, b, c in state:
      if s == 'AB':
        if b-1 >= 0:
            next_state[(a+1, b-1, c)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b+1, c)] = ['B', state[(a,b,c)]]
      if s == 'AC':
        if c-1 >= 0:
          next_state[(a+1, b, c-1)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b, c+1)] = ['C', state[(a,b,c)]]
      if s == 'BC':
        if c-1 >= 0:
          next_state[(a, b+1, c-1)] = ['B', state[(a,b,c)]]
        if b-1 >= 0:
          next_state[(a, b-1, c+1)] = ['C', state[(a,b,c)]]
    state = next_state
    if not state:
      print('No')
      return
    
  print('Yes')
  seq = list(state.values())[0]
  while seq:
    print(seq[0])
    seq = seq[1]
  
    
  
  
N, A, B, C = [int(x) for x in input().split(' ')]
if A + B + C < 6:
  dp_solve(N, A, B, C)
else:
  g_solve(N, A, B, C)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
if (a==0 and b==0 and c==0):
    print("No")
    exit()
ans = []

f = 0
for i in range(n):
    x = input()
    
    if (f==1):
        if (x == "AC"):
            b -= 2
            c += 2
            ans.pop()
            ans.append("C")
    elif (f==2):
        if (x == "BC"):
            a -= 2
            c += 2
            ans.pop()
            ans.append("C")
    elif (f==3):
        if (x == "BC"):
            a -= 2
            b += 2
            ans.pop()
            ans.append("B")
    if (a==0):
            if (b==0):
                if (x =="AB"):
                    print("No")
                    exit()
            elif (c==0):
                if (x=="AC"):
                    print("No")
                    exit()
            elif (b==1 and c==1):
                f = 1
    if (b==0):
        if (c==0):
            if (x=="BC"):
                print("No")
                exit() 
            elif (a==1 and c==1):
                f = 2
    if (a==1 and b==1 and c==0):
        f = 3
    if (x=="AB"):
        if a<=b:
            a += 1
            b -= 1
            ans.append("A")
        else:
            a -= 1
            b += 1
            ans.append("B")
    elif (x=="BC"):
        if b<=c:
            b += 1
            c -= 1
            ans.append("B")
        else:
            b -= 1
            c += 1
            ans.append("C")
    else:
        if a<=c:
            a += 1
            c -= 1
            ans.append("A")
        else:
            a -= 1
            c += 1
            ans.append("C")
print("Yes")            
print(*ans, sep="\n")

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
ls=[A,B,C]
ans=[]
if ls.count(0)>2:
  print("No")
  exit()
for i in range(N):
  S=input()
  if S=="AB":
    if ls[0]==0 and ls[1]==0:
      print("No")
      exit()
    elif ls[0]>ls[1]:
      ls[0]-=1
      ls[1]+=1
      ans.append("B")
    else:
      ls[0]+=1
      ls[1]-=1
      ans.append("A")
  elif S=="AC":
    if ls[0]==0 and ls[2]==0:
      print("No")
      exit()
    elif ls[0]>ls[2]:
      ls[0]-=1
      ls[2]+=1
      ans.append("C")
    else:
      ls[0]+=1
      ls[2]-=1
      ans.append("A")
  elif S=="BC":
    if ls[1]==0 and ls[2]==0:
      print("No")
      exit()
    elif ls[1]>ls[2]:
      ls[1]-=1
      ls[2]+=1
      ans.append("C")
    else:
      ls[1]+=1
      ls[2]-=1
      ans.append("B")
print("Yes")
for i in range(N):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
list = []

for i in range(0, N):
    cl = input()
    list.append(cl)

ex_list = []
yes = True
for n in range(0, N):
    if list[n] == 'AB':
        if A == 0 and B == 0:
            yes = False
        
        if A > B:
            A -= 1
            B += 1
            ex_list.append('B')
        elif A < B:
            A += 1
            B -= 1
            ex_list.append('A')
        elif A == B:
            if n < N - 1:
                if list[n+1] == 'BC':
                    A -= 1
                    B += 1
                    ex_list.append('B')
                elif list[n+1] == 'AC':
                    A += 1
                    B -= 1
                    ex_list.append('A')
            else:
                A -= 1
                B += 1
                ex_list.append('B')

    elif list[n] == 'AC':
        if A == 0 and C == 0:
            yes = False
        if A > C:
            A -= 1
            C += 1
            ex_list.append('C')
        elif A < C:
            A += 1
            C -= 1
            ex_list.append('A')
        elif A == C:
            if n < N - 1:
                if list[n + 1] == 'AB':
                    A += 1
                    C -= 1
                    ex_list.append('A')
                elif list[n + 1] == 'BC':
                    A -= 1
                    C += 1
                    ex_list.append('C')
            else:
                A += 1
                C -= 1
                ex_list.append('A')


    elif list[n] == 'BC':
        if B == 0 and C == 0:
            yes = False
        if B > C:
            B -= 1
            C += 1
            ex_list.append('C')
        elif B < C:
            B += 1
            C -= 1
            ex_list.append('B')
        elif B == C:
            if n < N - 1:
                if list[n + 1] == 'AB':
                    B += 1
                    C -= 1
                    ex_list.append('B')
                elif list[n + 1] == 'AC':
                    B -= 1
                    C += 1
                    ex_list.append('C')
            else:
                B -= 1
                C += 1
                ex_list.append('C')

    if A < 0 or B < 0 or C < 0:
        yes = False

    if not yes:
        break

if yes:
    print('Yes')
    for element in ex_list:
        print(element)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
ab=0
ac=0
bc=0
for i in range(n):
    s=input()
    if s=="AB":
        ab+=1
    elif s=="AC":
        ac+=1
    else:
        bc+=1
ab2=ab%2
ac2=ac%2
bc2=bc%2
if ab2==1 and a+b==0:
    print("No")
elif ac2==1 and a+c==0:
    print("No")
elif bc2==1 and b+c==0:
    print("No")
elif ab2+ac2==2 and a+b+c<2:
    print("No")
elif ab2+bc2==2 and a+b+c<2:
    print("No")
elif bc2+ac2==2 and a+b+c<2:
    print("No")
elif ab2+ac2+bc2==3 and a+b+c<3:
    print("No")
else:
    print("Yes")

Submission status: Wrong Answer

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

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

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

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

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

Submission status: Wrong Answer

Solution code:
def main():
  n, a, b, c = map(int, input().split())
  ab_num = 0
  ac_num = 0
  bc_num = 0
  s_li = []
  for i in range(n):
    s = input()
    s_li.append(s)
    if s == 'AB':
      ab_num += 1
    elif s == 'AC':
      ac_num += 1
    elif s == 'BC':
      bc_num += 1

  ab_num = ab_num % 2
  ac_num = ac_num % 2
  bc_num = bc_num % 2

  print(ab_num, ac_num, bc_num)

  if (a == 0 and b == 0 and ab_num > 0) or (a == 0 and c == 0 and ac_num > 0) or (b == 0 and c == 0 and bc_num > 0):
    print('No')
  else:
    print('Yes')
    # for i in s_li:
      # 

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)]
add = []
for x in S:
    if x == "AB":
        if A < B:
            A += 1
            B -= 1
            add.append("A")
        else:
            B += 1
            A -= 1
            add.append("B")
    elif x == "AC":
        if A < C:
            A += 1
            C -= 1
            add.append("A")
        else:
            C += 1
            A -= 1
            add.append("C")
    else:
        if B < C:
            B += 1
            C -= 1
            add.append("B")
        else:
            C += 1
            B -= 1
            add.append("C")


flag = True
if A >= 0 and B >= 0 and C >= 0:
    print("Yes")
    for y in add:
        print(y)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
n,a,b,c=map(int,input().split())
A=[a,b,c]
B=[]
flag=0
for i in range(n):
    S=input()
    if S=="AB":
        p,q=0,1
    elif S=="AC":
        p,q=0,2
    else:
        p,q=1,2
    if A[p]==A[q]==0:
        flag=1
        break
    if A[p]>=A[q]:
        A[p]-=1
        A[q]+=1
        B.append(q)
    else:
        A[p]+=1
        A[q]-=1
        B.append(p)
if flag:
    print("No")
    sys.exit()
print("Yes")
for i in range(n):
    if B[i]==0:
        print("A")
    elif B[i]==1:
        print("B")
    else:
        print("C")

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
s=[input() for i in range(n)]
ans=[]
for que in s:
    if que=="AB":
        if a==b==0:
            print("No")
            exit()
        if a==b==1:
            a=-1
            b=-1
        elif (a==-1 and b==0) or (a==0 or b==-1):
            ans.append("A")
            ans.append("B")
            a=1
            b=1
            c=0
        elif a==-1 and b==-1:
            ans.append("A")
            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 que=="AC":
        if a==c==0:
            print("No")
            exit()
        if a==c==1:
            a=-1
            c=-1
        elif (a==-1 and c==0) or (a==0 or c==-1):
            ans.append("A")
            ans.append("C")
            a=1
            c=1
            b=0
        elif a==-1 and c==-1:
            ans.append("A")
            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
    else: # BC
        if b==c==0:
            print("No")
            exit()

        if b==c==1:
            b=-1
            c=-1
        elif (b==-1 and c==0) or (b==0 or c==-1):
            ans.append("B")
            ans.append("C")
            b=1
            c=1
            a=0
        elif b==-1 and c==-1:
            ans.append("B")
            ans.append("C")
            b=1
            c=1
        elif c>=b:
            ans.append("B")
            c-=1
            b+=1
        else:
            ans.append("C")
            c+=1
            b-=1
if a==-1 and b==-1:
    ans.append("A")
if b==-1 and c==-1:
    ans.append("B")
if c==-1 and a==-1:
    ans.append("C")

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

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)]:
      ABC[idx(l1)] += 1
      ABC[idx(l2)] -= 1
      res += l1
    elif ABC[idx(l1)] + ABC[idx(l2)]== 0:
      return False,""
    elif ABC[idx(l1)] + ABC[idx(l2)] > 2:
      ABC[idx(l1)] += 1
      res += l1
      ABC[idx(l2)] -= 1
    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
  
if len(S) > 40:
  res, p = search(tup,S[:40])
  # simulate:
  if res:
    ## 結果がokならその先まで考慮してみる
    a,b,c = tup
    for q,ch in zip(S[:40],p):
      qset = {qq for qq in q}
      tup[idx(ch)] += 1
      tup[idx(list(qset-set(ch))[0])] -= 0
    
    res, p2 = search(tup,S[40:])
    if res:
      print("Yes")
      for c in p:
        print(c)
      for c in p2:
        print(c)
    else:
      print("No")
    
  else:
    print("No")
  
else:
  res, p = search(tup,S)
  if res:
    print("Yes")
    for c in p:
      print(c)
  else:
    print("No")

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)
    
    ans=[]
    flag=1
    for i in range(N-1):
        s=S[i]
        s2=S[i+1]
        
        if s=="AB":
            if (A,B)!=(1,1):
                if A<B:
                    A+=1
                    B-=1
                    ans.append("A")
                else:
                    A-=1
                    B+=1
                    ans.append("B")
            else:#1:1の時
                if s2=="AB":
                    A+=1
                    B-=1
                    ans.append("A")
                elif s2=="BC":
                    A-=1
                    B+=1
                    ans.append("B")
                else:
                    A+=1
                    C-=1
                    ans.append("A")
                    
        if s=="AC":
            if (A,C)!=(1,1):
                if A<C:
                    A+=1
                    C-=1
                    ans.append("A")
                else:
                    A-=1
                    C+=1
                    ans.append("C")
            else:#1:1の時
                if s2=="AB":
                    A+=1
                    C-=1
                    ans.append("A")
                elif s2=="BC":
                    A-=1
                    C+=1
                    ans.append("C")
                else:
                    A+=1
                    C-=1
                    ans.append("A")
                    
        if s=="BC":
            if (B,C)!=(1,1):
                if B<C:
                    B+=1
                    C-=1
                    ans.append("B")
                else:
                    B-=1
                    C+=1
                    ans.append("C")
            else:#1:1の時
                if s2=="AB":
                    B+=1
                    C-=1
                    ans.append("B")
                elif s2=="BC":
                    B-=1
                    C+=1
                    ans.append("C")
                else:
                    B-=1
                    C+=1
                    ans.append("C")
                
        if A<0 or B<0 or C<0:
            flag=-1
            break
        
    s=S[-1]
    if s=="AB":
        if A!=0:
            A-=1
            B+=1
            ans.append("B")
        else:
            A+=1
            B-=1
            ans.append("A")
            
    elif s=="AC":
        if A!=0:
            A-=1
            C+=1
            ans.append("C")
        else:
            A+=1
            C-=1
            ans.append("A")
            
    else:
        if B!=0:
            B-=1
            C+=1
            ans.append("C")
        else:
            B+=1
            C-=1
            ans.append("B")
            
            
            
    if A<0 or B<0 or C<0:
            flag=-1
            
                
        
        
    if flag==-1:
        print("No")
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()

Submission status: Wrong Answer

Solution code:
from itertools import product
from copy import deepcopy
N, Ax, Bx, Cx = map(int, input().split())
S = [input() for _ in range(N)]

def calc(ab, ac, bc):
    A, B, C = deepcopy(Ax), deepcopy(Bx), deepcopy(Cx)
    ans = []
    for s in S:
        if s == 'AB':
            if A - ab > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')

        if s == 'AC':
            if A - ac > C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')

        if s == 'BC':
            if B - bc > C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')

        if A < 0 or B < 0 or C < 0:
            return False
    return ans


for pattern in product([0, 1, -1], repeat=3):
    pattern = list(pattern)
    ab, ac, bc = pattern

    ans = calc(ab, ac, bc)
    if ans:
        print('Yes')
        print(*ans, sep="\n")
        exit()

print('No')

Submission status: Wrong Answer

Solution code:
def main():
    n,a,b,c=map(int,input().split())
    if a==b==0 or b==c==0 or c==a==0:
        print("No")
        exit()
    else:
        print("Yes")
        for i in range(n):
            s=input()
            #print(i)
            if s=="AB":
                if a>b:
                    a-=1
                    b+=1
                    print("B")
                else:
                    a+=1
                    b-=1
                    print("A")

            elif s=="BC":
                if c>b:
                    c-=1
                    b+=1
                    print("B")
                else:
                    c+=1
                    b-=1
                    print("C")
            
            elif s=="AC":
                if c>a:
                    c-=1
                    a+=1
                    print("A")
                else:
                    c+=1
                    a-=1
                    print("C")

if __name__=='__main__':
    main()

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
ABC = [A, B, C]
S2i = {'A':0, 'B':1, 'C':2}
ans = []
is_yes = True
for i in range(N):
    si = input()
    left, right = si[0], si[1]
    li, ri = S2i[left], S2i[right]
    if ABC[li] == ABC[ri] == 0:
        is_yes = False
        break
    elif ABC[li] < ABC[ri]:
        ans += [left]
        ABC[li] += 1
        ABC[ri] -= 1
    else:
        ans += [right]
        ABC[ri] += 1
        ABC[li] -= 1

if is_yes:
    print('Yes')
    for a in ans:
        print(a)
else:
    print('No')