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: Accepted

Solution code:

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

s = []

for i in range(n):
    tmp_s = input()
    s.append(tmp_s)

ans = []

for i,val in enumerate(s):
    if val == 'AB':
        if a == b == 0:
            print('No')
            exit(0)
        elif a < b or (a == b and i != n-1 and 'A' in s[i+1]):
            ans.append('A')
            a += 1
            b -= 1
        else:
            ans.append('B')
            a -= 1
            b += 1

    elif val == 'BC':
        if b == c == 0:
            print('No')
            exit(0)
        elif b < c or (b == c and i != n-1 and 'B' in s[i+1]):
            ans.append('B')
            b += 1
            c -= 1
        else:
            ans.append('C')
            b -= 1
            c += 1
    else:
        if c == a == 0:
            print('No')
            exit(0)
        elif c < a or (c == a and i != n-1 and 'C' in s[i+1]):
            ans.append('C')
            c += 1
            a -= 1
        else:
            ans.append('A')
            c -= 1
            a += 1
        
        

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
n,*a = map(int,input().split())
s = [[*input()] for _ in range(n)]
a = {"A":a[0], "B":a[1], "C":a[2]}
ans = ["Yes"]+[0]*n
def dfs(i):
    if i==n:
        print("\n".join(ans))
        exit()
    for p,m in [s[i],s[i][::-1]]:
        if a[m]:
            a[p]+=1; a[m]-=1; ans[i+1]=p
            dfs(i+1)
            a[p]-=1; a[m]+=1
dfs(0)
print("No")

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split())
S=[input() for _ in range(N)]
ans=''
for i in range(N):
    if S[i]=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        elif A==1 and B==1 and i<N-1:
            if 'A' in S[i+1]:
                A+=1
                B-=1
                ans+='A'
            else:
                A-=1
                B+=1
                ans+='B'
        elif A>B:
            A-=1
            B+=1
            ans+='B'
        else:
            A+=1
            B-=1
            ans+='A'
    elif S[i]=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        elif A==1 and C==1 and i<N-1:
            if 'A' in S[i+1]:
                A+=1
                C-=1
                ans+='A'
            else:
                A-=1
                C+=1
                ans+='C'
        elif A>C:
            A-=1
            C+=1
            ans+='C'
        else:
            A+=1
            C-=1
            ans+='A'
    else:
        if B==0 and C==0:
            print('No')
            exit()
        elif B==1 and C==1 and i<N-1:
            if 'B' in S[i+1]:
                B+=1
                C-=1
                ans+='B'
            else:
                B-=1
                C+=1
                ans+='C'
        elif B>C:
            B-=1
            C+=1
            ans+='C'
        else:
            B+=1
            C-=1
            ans+='B'
print('Yes')
for c in ans:
    print(c)

Submission status: Accepted

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

Submission status: Accepted

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:
        if 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 or s1 == next_s2:
                abc[s1] += 1
                abc[s2] -= 1
                ans.append(s[0])
            else:
                abc[s1] -= 1
                abc[s2] += 1
                ans.append(s[1])
        else:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])
    
    elif abc[s1] == 1:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(s[0])

    else:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(s[1])

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

Submission status: Accepted

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:
        if 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 or s1 == next_s2:
                abc[s1] += 1
                abc[s2] -= 1
                ans.append(s[0])
            else:
                abc[s1] -= 1
                abc[s2] += 1
                ans.append(s[1])
        else:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(s[0])
    
    elif abc[s1] == 1:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(s[0])

    elif abc[s2] == 1:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(s[1])

    else:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(s[1])

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

Submission status: Accepted

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)

flag=False

while idx!=n:

    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:
        log[idx]=0
        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])

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
d = {'A':a, 'B':b, 'C':c}
ans = []
def move(inc, dec):
    d[inc]+=1
    d[dec]-=1
    ans.append(inc)
q = []
for i in range(n):
    q.append(input())
for i in range(n):
    x = q[i][0]
    y = q[i][1]
    if d[x] == 0 and d[y] == 0:
        print("No")
        exit()
    if d[x] == 1 and d[y] == 1:
        if i == n-1:
            move(x, y)
            continue
        if x in q[i+1]:
            move(x, y)
        else:
            move(y, x)
    else:
        if d[x] >= d[y]:
            move(y, x)
        else:
            move(x, y)
print("Yes")
print(*ans, sep = '\n')

Submission status: Accepted

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]] and before[(d[s]+1)%3]:
                if before[d[s]][-1][0]>before[(d[s]+1)%3][-1][0]:
                    k,l=before[d[s]].pop()
                    n[d[s]]+=2
                    n[l]-=2
                    ans[k]=d[s]
                else:
                    k,l=before[(d[s]+1)%3].pop()
                    n[(d[s]+1)%3]+=2
                    n[l]-=2
                    ans[k]=(d[s]+1)%3
            elif before[d[s]] or before[(d[s]+1)%3]:
                if before[d[s]]:
                    k,l=before[d[s]].pop()
                    n[d[s]]+=2
                    n[l]-=2
                    ans[k]=d[s]
                elif before[(d[s]+1)%3]:
                    k,l=before[(d[s]+1)%3].pop()
                    n[(d[s]+1)%3]+=2
                    n[l]-=2
                    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: Accepted

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)

flag=False

while idx!=n:

    #3回以上戻ってたらもうダメ 0回目の時点でアウトでもダメ
    if idx<n-2 and log[idx+2]==2 and log[idx]==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:
        log[idx]=0
        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])

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split())
S=[input() for _ in range(N)]
ans=''
for i in range(N):
    if S[i]=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        elif A==1 and B==0:
            A-=1
            B+=1
            ans+='B'
        elif A==0 and B==1:
            A+=1
            B-=1
            ans+='A'
        elif A==1 and B==1 and i<N-1:
            if 'A' in S[i+1]:
                A+=1
                B-=1
                ans+='A'
            else:
                A-=1
                B+=1
                ans+='B'
        elif A>B:
            A-=1
            B+=1
            ans+='B'
        else:
            A+=1
            B-=1
            ans+='A'
    elif S[i]=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        elif A==1 and C==0:
            A-=1
            C+=1
            ans+='C'
        elif A==0 and C==1:
            A+=1
            C-=1
            ans+='A'
        elif A==1 and C==1 and i<N-1:
            if 'A' in S[i+1]:
                A+=1
                C-=1
                ans+='A'
            else:
                A-=1
                C+=1
                ans+='C'
        elif A>C:
            A-=1
            C+=1
            ans+='C'
        else:
            A+=1
            C-=1
            ans+='A'
    else:
        if B==0 and C==0:
            print('No')
            exit()
        elif B==1 and C==0:
            B-=1
            C+=1
            ans+='C'
        elif B==0 and C==1:
            B+=1
            C-=1
            ans+='B'
        elif B==1 and C==1 and i<N-1:
            if 'B' in S[i+1]:
                B+=1
                C-=1
                ans+='B'
            else:
                B-=1
                C+=1
                ans+='C'
        elif B>C:
            B-=1
            C+=1
            ans+='C'
        else:
            B+=1
            C-=1
            ans+='B'
print('Yes')
for c in ans:
    print(c)

Submission status: Accepted

Solution code:
# F - Three Variables Game

N, A, B, C = map(int, input().split())
S = [''] * N
for i in range(N):
    S[i] = input()

ansArr = []
ans = 'Yes'
for i in range(N):
    s = S[i]
    if s=='AB':
        if A==1 and B==1 and i<N-1:
            if S[i+1].count('A')==1:
                A += 1
                B -= 1
                ansArr.append('A')
            else:
                A -= 1
                B += 1
                ansArr.append('B')               
        elif A <= B:
            A += 1
            B -= 1
            if B < 0:
                ans = 'No'
                break
            ansArr.append('A')
        else:
            A -= 1
            B += 1
            ansArr.append('B')
    elif s =='AC':
        if A==1 and C==1 and i<N-1:
            if S[i+1].count('A')==1:
                A += 1
                C -= 1
                ansArr.append('A')
            else:
                A -= 1
                C += 1
                ansArr.append('C')   
        elif A <= C:
            A += 1
            C -= 1
            if C < 0:
                ans = 'No'
                break
            ansArr.append('A')
        else:
            A -= 1
            C += 1
            ansArr.append('C')
    else:
        if B==1 and C==1 and i<N-1:
            if S[i+1].count('B')==1:
                B += 1
                C -= 1
                ansArr.append('B')
            else:
                B -= 1
                C += 1
                ansArr.append('C')  
        elif B <= C:
            B += 1
            C -= 1
            if C < 0:
                ans = 'No'
                break
            ansArr.append('B')
        else:
            B -= 1
            C += 1
            ansArr.append('C')

print(ans)
if ans=='Yes':
    for a in ansArr:
        print(a)

Submission status: Accepted

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 < AC:
                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: Accepted

Solution code:
import bisect
import 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)]
    
    for n in range(N):
        s = Ss[n]
        if s == "AB":
            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 n+1!=N and Ss[n+1]=="AC":
                    ans.append("A")
                    AC += 1
                    BC -= 1
                else:
                    ans.append("B")
                    AC -= 1
                    BC += 1
        if s == "AC":
            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 n+1 != N and Ss[n+1] == "AB":
                    ans.append("A")
                    AB += 1
                    BC -= 1
                else:
                    ans.append("C")
                    AB -= 1
                    BC += 1
        if s == "BC":
            if BC == 0:
                print("No")
                return
            if AB > AC:
                ans.append("C")
                AB -= 1
                AC += 1
            elif AB < AC:
                ans.append("B")
                AB += 1
                AC -= 1
            else:
                if n+1 != N and Ss[n+1] == "AB":
                    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: Accepted

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.copy()
    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[x] != 0 and (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')