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:
N, A, B, C = map(int, input().split())
S = [input() for i in range(N)]
add = []

flag = True

for x in range(len(S)):
    if S[x] == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        if x + 1 < len(S):
            if S[x + 1] == "AC":
                A += 1
                B -= 1
                add.append("A")
            elif S[x + 1] == "BC":
                B += 1
                A -= 1
                add.append("B")
        elif A > B:
            B += 1
            A -= 1
            add.append("B")
        else:
            A += 1
            B -= 1
            add.append("A")
    elif S[x] == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        if x + 1 < len(S):
            if S[x + 1] == "BC":
                C += 1
                A -= 1
                add.append("C")
            elif S[x + 1] == "AB":
                A += 1
                C -= 1
                add.append("A")
        elif A > C:
            C += 1
            A -= 1
            add.append("C")
        else:
            A += 1
            C -= 1
            add.append("A")
    else:
        if B == 0 and C == 0:
            flag = False
            break
        if x + 1 < len(S):
            if S[x + 1] == "AB":
                B += 1
                C -= 1
                add.append("B")
            elif S[x + 1] == "AC":
                C += 1
                B -= 1
                add.append("C")
        elif B > C:
            C += 1
            B -= 1
            add.append("C")
        else:
            B += 1
            C -= 1
            add.append("B")


if flag is True and 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:
n,a,b,c=map(int,input().split())
s=[input() for _ in range(n)]
if a+b+c==0:
    print("No")
elif a+b+c==1:
    choice=[]
    for si in s:
        if si=="AB":
            if a==0 and b==0:
                print("No")
                exit()
            elif a==1:
                a-=1
                b+=1
                choice.append("B")
            else:
                a+=1
                b-=1
                choice.append("A")
        elif si=="AC":
            if a==0 and c==0:
                print("No")
                exit()
            elif a==1:
                a-=1
                c+=1
                choice.append("C")
            else:
                a+=1
                c-=1
                choice.append("A")
        else:
            if b==0 and c==0:
                print("No")
                exit()
            elif b==1:
                b-=1
                c+=1
                choice.append("C")
            else:
                b+=1
                c-=1
                choice.append("B")
    print("Yes")
    for ch in choice:
        print(ch)
else:
    choice=[]
    if s[0]=="AB":
        if a==0 and b==0:
            print("No")
            exit()
    elif s[0]=="AC":
        if a==0 and c==0:
            print("No")
            exit()
    else:
        if b==0 and c==0:
            print("No")
            exit()
    for i in range(n):
        if a+b+c>=3:
            if s[i]=="AB":
                if a>=b:
                    a-=1
                    b+=1
                    choice.append("B")
                else:
                    a+=1
                    b-=1
                    choice.append("A")
            elif s[i]=="AC":
                if a>=c:
                    a-=1
                    c+=1
                    choice.append("C")
                else:
                    a+=1
                    c-=1
                    choice.append("A")
            else:
                if b>=c:
                    b-=1
                    c+=1
                    choice.append("C")
                else:
                    b+=1
                    c-=1
                    choice.append("B")
        else:
            if i<n-1:
                if s[i]=="AB":
                    if a==b:
                        if s[i+1]=="AB" or s[i+1]=="BC":
                            a-=1
                            b+=1
                            choice.append("B")
                        else:
                            a+=1
                            b-=1
                            choice.append("A")
                    elif b>a:
                        a+=1
                        b-=1
                        choice.append("A")
                    else:
                        a=-1
                        b+=1
                        choice.append("B")
                elif s[i]=="AC":
                    if a==c:
                        if s[i+1]=="AC" or s[i+1]=="BC":
                            a-=1
                            c+=1
                            choice.append("C")
                        else:
                            a+=1
                            c-=1
                            choice.append("A")
                    elif c>a:
                        a+=1
                        c-=1
                        choice.append("A")
                    else:
                        a-=1
                        c+=1
                        choice.append("C")
                else:
                    if b==c:
                        if s[i+1]=="BC" or s[i+1]=="AC":
                            b-=1
                            c+=1
                            choice.append("C")
                        else:
                            b+=1
                            c-=1
                            choice.append("B")
                    elif b>c:
                        b-=1
                        c+=1
                        choice.append("C")
                    else:
                        b+=1
                        c-=1
                        choice.append("B")
            else:
                if s[i]=="AB":
                    if a>=b:
                        a-=1
                        b+=1
                        choice.append("B")
                    else:
                        a+=1
                        b-=1
                        choice.append("A")
                elif s[i]=="AC":
                    if a>=c:
                        a-=1
                        c+=1
                        choice.append("C")
                    else:
                        a+=1
                        c-=1
                        choice.append("A")
                else:
                    if b>=c:
                        b-=1
                        c+=1
                        choice.append("C")
                    else:
                        b+=1
                        c-=1
                        choice.append("B")
    print("Yes")
    for ch in choice:
        print(ch)

Submission status: Wrong Answer

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

flag = True

for x in range(len(S)):
    if S[x] == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        if x + 2 == len(S):
            if S[x + 1] == "AC":
                A += 1
                B -= 1
                add.append("A")
            elif S[x + 1] == "BC":
                B += 1
                A -= 1
                add.append("B")
        elif A > B:
            B += 1
            A -= 1
            add.append("B")
        else:
            A += 1
            B -= 1
            add.append("A")
    elif S[x] == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        if x + 2 == len(S):
            if S[x + 1] == "BC":
                C += 1
                A -= 1
                add.append("C")
            elif S[x + 1] == "AB":
                A += 1
                C -= 1
                add.append("A")
        elif A > C:
            C += 1
            A -= 1
            add.append("C")
        else:
            A += 1
            C -= 1
            add.append("A")
    else:
        if B == 0 and C == 0:
            flag = False
            break
        if x + 2 == len(S):
            if S[x + 1] == "AB":
                B += 1
                C -= 1
                add.append("B")
            elif S[x + 1] == "AC":
                C += 1
                B -= 1
                add.append("C")
        elif B > C:
            C += 1
            B -= 1
            add.append("C")
        else:
            B += 1
            C -= 1
            add.append("B")


if flag is True and 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:
N, A, B, C = map(int, input().split())
S = [input() for i in range(N)]
add = []

flag = True

for x in range(len(S)):
    if S[x] == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        if x + 2 == len(S):
            if S[x + 1] == "AC":
                A += 1
                B -= 1
                add.append("A")
            elif S[x + 1] == "BC":
                B += 1
                A -= 1
                add.append("B")
        elif A > B:
            B += 1
            A -= 1
            add.append("B")
        else:
            A += 1
            B -= 1
            add.append("A")
    elif x == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        if x + 2 == len(S):
            if S[x + 1] == "BC":
                C += 1
                A -= 1
                add.append("C")
            elif S[x + 1] == "AB":
                A += 1
                C -= 1
                add.append("A")
        elif A > C:
            C += 1
            A -= 1
            add.append("C")
        else:
            A += 1
            C -= 1
            add.append("A")
    else:
        if B == 0 and C == 0:
            flag = False
            break
        if x + 2 == len(S):
            if S[x + 1] == "AB":
                B += 1
                C -= 1
                add.append("B")
            elif S[x + 1] == "AC":
                C += 1
                B -= 1
                add.append("C")
        elif B > C:
            C += 1
            B -= 1
            add.append("C")
        else:
            B += 1
            C -= 1
            add.append("B")


if flag is True and 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:









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

#print(arr)
arr1=[]
for i in range(N):
    if arr[i]=="AB":
        if A>B:
            A-=1
            B+=1
            arr1.append("B")
        elif B>A:
            A+=1
            B-=1    
            arr1.append("A")
        else:    
            if i!=N-1 and arr[i+1]=="AC":
                arr1.append("A")
                A+=1
                B-=1
            elif i!=N-1 and arr[i+1]=="BC":
                B+=1
                A-=1
                arr1.append("B")       
            else:
                arr1.append("B")
                A-=1
                B+=1    

    elif arr[i]=="AC":
        if A>C:
            A-=1
            C+=1
            arr1.append("C")

        elif A<C:
            A+=1
            C-=1    
            arr1.append("A")
        else:
            if i!=N-1 and arr[i+1]=="AB":
                arr1.append("A")
                A+=1
                C-=1
            elif i!=N-1 and arr[i+1]=="CB":
                C+=1
                A-=1
                arr1.append("C")       
            else:
                arr1.append("C")
                A-=1
                C+=1
                    

    else:    
        if B>C:
            B-=1
            C+=1
            arr1.append("C")

        elif C>B:
            B+=1
            C-=1    
            arr1.append("B")
        else:
            if i!=N-1 and arr[i+1]=="BA":
                arr1.append("B")
                B+=1
                C-=1
            elif i!=N-1 and arr[i+1]=="CA":
                C+=1
                B-=1
                arr1.append("C")       
            else:
                arr1.append("C")
                B-=1
                C+=1    

if A>=0 and B>=0 and C>=0:
    print("Yes")
    for i in arr1:
        print(i)
else:
    print("No")

Submission status: Wrong Answer

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

YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    L = ["A" for _ in range(N)]
    for i in range(N):
        if s[i] == "AB":
            if A == 0 and B == 0:
                print(NO)
                return
            else:
                if A >= B:
                    A -= 1
                    B += 1
                    L[i] = "B"
                else:
                    A += 1
                    B -= 1
                    L[i] = "A"
        elif s[i] == "BC":
            if B == 0 and C == 0:
                print(NO)
                return
            else:
                if B >= C:
                    B -= 1
                    C += 1
                    L[i] = "C"
                else:
                    B += 1
                    C -= 1
                    L[i] = "B"
        else:   # AC
            if A == 0 and C == 0:
                print(NO)
                return
            else:
                if A >= C:
                    A -= 1
                    C += 1
                    L[i] = "C"
                else:
                    A += 1
                    C -= 1
                    L[i] = "A"
    print(YES)
    for l in L:
        print(l)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, A, B, C, s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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


def addAns(x, y, s):
    x += 1
    y -= 1
    ans.append(s)


if A + B + C == 0:
    print("No")
    exit()
elif A + B + C == 1:
    for _ in range(N):
        s = input()
        if s == "AB" and A + B == 1:
            if A == 1:
                addAns(B, A, "B")
            else:
                addAns(A, B, "A")
        elif s == "BC" and B + C == 1:
            if B == 1:
                B -= 1
                C += 1
                ans.append("C")
            else:
                B += 1
                C -= 1
                ans.append("B")
        elif s == "AC" and A + C == 1:
            if A == 1:
                A -= 1
                C += 1
                ans.append("C")
            else:
                A += 1
                C -= 1
                ans.append("A")
        else:
            print("No")
            exit()
else:
    fir = ""
    sec = input()
    for _ in range(N - 1):
        fir = sec
        sec = input()
        if fir == "AB" and A + B == 1:
            if A == 1:
                A -= 1
                B += 1
                ans.append("B")
            else:
                A += 1
                B -= 1
                ans.append("A")
        elif fir == "BC" and B + C == 1:
            if B == 1:
                B -= 1
                C += 1
                ans.append("C")
            else:
                B += 1
                C -= 1
                ans.append("B")
        elif fir == "AC" and A + C == 1:
            if A == 1:
                A -= 1
                C += 1
                ans.append("C")
            else:
                A += 1
                C -= 1
                ans.append("A")
        elif fir == "AB" and A >= 2:
            A -= 1
            B += 1
            ans.append("B")
        elif fir == "AB" and B >= 2:
            addAns(A, B, "A")
        elif fir == "BC" and B >= 2:
            addAns(C, B, "C")
        elif fir == "BC" and C >= 2:
            addAns(B, C, "B")
        elif fir == "AC" and A >= 2:
            addAns(C, A, "C")
        elif fir == "AC" and C >= 2:
            addAns(A, C, "A")
        elif fir == "AB" and A == 1 and B == 1:
            if sec == "AB":
                addAns(A, B, "A")
            elif sec == "BC":
                addAns(B, A, "B")
            elif sec == "AC":
                addAns(A, B, "A")
        elif fir == "BC" and B == 1 and C == 1:
            if sec == "BC":
                addAns(B, C, "B")
            elif sec == "AB":
                addAns(B, C, "B")
            elif sec == "AC":
                addAns(C, B, "C")
        elif fir == "AC" and A == 1 and C == 1:
            if sec == "AC":
                addAns(A, C, "A")
            elif sec == "AB":
                addAns(A, C, "A")
            elif sec == "BC":
                addAns(C, A, "C")
        else:
            print("No")
            exit()
    if sec == "AB":
        if A >= 1:
            addAns(B, A, "B")
        elif B >= 1:
            addAns(A, B, "A")
        else:
            print("No")
            exit()
    elif sec == "BC":
        if B >= 1:
            addAns(C, B, "C")
        elif C >= 1:
            addAns(B, C, "B")
        else:
            print("No")
            exit()
    elif sec == "AC":
        if A >= 1:
            addAns(C, A, "C")
        elif C >= 1:
            addAns(A, C, "A")
        else:
            print("No")
            exit()

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

Submission status: Wrong Answer

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

ans = []

for _ in range(n):
    s = input()
    if 'C' not in s:
        if a < b:
            a += 1
            b -= 1
            ans.append('A')
        else:
            a -= 1
            b += 1
            ans.append('B')
    elif 'B' not in s:
        if a < c:
            a += 1
            c -= 1
            ans.append('A')
        else:
            a -= 1
            c += 1
            ans.append('C')
    elif 'A' not in s:
        if b < c:
            b += 1
            c -= 1
            ans.append('B')
        else:
            b -= 1
            c += 1
            ans.append('C')

    if -1 in (a, b, c):
        print('No')
        break
else:
    print('Yes')
    for x in ans:
        print(x)

Submission status: Wrong Answer

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

ans = []

for _ in range(n):
    s = input()
    if 'C' not in s:
        if a < b:
            a += 1
            b -= 1
            ans.append('A')
        else:
            a -= 1
            b += 1
            ans.append('B')
    elif 'B' not in s:
        if a < c:
            a += 1
            c -= 1
            ans.append('A')
        else:
            a -= 1
            c += 1
            ans.append('C')
    elif 'A' not in s:
        if b < c:
            b += 1
            c -= 1
            ans.append('B')
        else:
            b -= 1
            c += 1
            ans.append('C')

if all(0 <= x for x in (a, b, c)):
    print('Yes')
    for x in ans:
        print(x)
else:
    print('No')

Submission status: Wrong Answer

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

strs=[]
for _ in range(N):
    s=input()
    if s=='AB':
        strs.append((0,1))
    elif s=='AC':
        strs.append((0,2))
    else: #BC
        strs.append((1,2))

l=[A,B,C]
ans=[]
ds={0:'A',1:'B',2:'C'}

if sum(l)==0:
    print('No')
    exit()

if sum(l)==1:
    for n in range(N):
        i1,i2=strs[n]
        if l[i1]==0 and l[i2]==0:
            break
        else:
            if l[i1]==0:
                l[i1]+=1
                l[i2]-=1
                ans.append(ds[i1])
            else: # l[i2]==0
                l[i1]-=1
                l[i2]+=1
                ans.append(ds[i2])
    else:
        print('Yes')
        print(*ans,sep='\n')
        exit()
    print('No')
    exit()

i1,i2=strs[0]
if l[i1]==0 and l[i2]==0:
    print('No')
else:
    print('Yes')
    for n in range(N):
        i1,i2=strs[n]
        if n < N-1:
            i1_n,i2_n=strs[n+1]
        else:
            i1_n,i2_n=i1,i2
        if l[i1]*l[i2]==0:
            if l[i1]==0:
                l[i1]+=1
                l[i2]-=1
                ans.append(ds[i1])
            else:
                l[i1]-=1
                l[i2]+=1
                ans.append(ds[i2])
        elif sum(l)==2 and l[i1]==1 and l[i2]==1 and n<N-1 and [i1,i2]!=[i1_n,i2_n]:
            if i1==i1_n:
                l[i1]+=1
                l[i2]-=1
                ans.append(ds[i1])
            else:
                l[i1]-=1
                l[i2]+=1
                ans.append(ds[i2])
        else:
            l[i1]+=1
            l[i2]-=1
            ans.append(ds[i1])
    print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
ls = []
for _ in range(n):
    ls.append(input())

abc = {'A': a, 'B': b, 'C': c}

def answer():
    solution = []
    if a + b + c == 0:
        return False, None
    elif a + b + c == 1:
        for s in ls:
            s0, s1 = s[0], s[1]
            if abc[s0] == 1:
                abc[s0] -= 1
                abc[s1] += 1
                solution.append(s1)
            elif abc[s1] == 1:
                abc[s0] += 1
                abc[s1] -= 1
                solution.append(s0)
            else:
                return False, None
        return True, solution
    else:
        for i, s in enumerate(ls):
            s0, s1 = s[0], s[1]
            if abc[s0] == 0 and abc[s1] == 0:
                return False, None
            elif i == n - 1:
                if abc[s0] == 0:
                    abc[s0] += 1
                    abc[s1] -= 1
                    solution.append(s0)
                else:
                    abc[s0] -= 1
                    abc[s1] += 1
                    solution.append(s1)
            else:
                if abc[s0] == 1 and (s0 in ls[i + 1]):
                    abc[s0] += 1
                    abc[s1] -= 1
                    solution.append(s0)
                else:
                    abc[s0] -= 1
                    abc[s1] += 1
                    solution.append(s1)
        return True, solution


yn, solution = answer()
if yn:
    print('Yes')
    for s in solution:
        print(s)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n,a,b,c=map(int,readline().split())
s_=read().split()
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if i!=n-1:
            if s_[i+1]=="AC" and c==0 and a<=1:
                a,b,ans=(a+1,b-1,ans+"A")
            elif s_[i+1]=="BC" and c==0 and b<=1:
                a,b,ans=(a-1,b+1,ans+"B")
            else:
                a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        else:
            a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if i!=n-1:
            if s_[i+1]=="AB" and b==0 and a<=1:
                a,c,ans=(a+1,c-1,ans+"A")
            elif s_[i+1]=="BC" and b==0 and c<=1:
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        else:
            a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if i!=n-1:
            if s_[i+1]=="AB" and a==0 and b<=1:
                b,c,ans=(b+1,c-1,ans+"B")
            elif s_[i+1]=="AC" and a==0 and c<=1:
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        else:
            b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
ls = []
for _ in range(n):
    ls.append(input())

abc = {'A': a, 'B': b, 'C': c}

def answer():
    solution = []
    if a + b + c == 0:
        return False, None
    elif a + b + c == 1:
        for s in ls:
            s0, s1 = s[0], s[1]
            if abc[s0] == 1:
                abc[s0] -= 1
                abc[s1] += 1
                solution.append(s1)
            elif abc[s1] == 1:
                abc[s0] += 1
                abc[s1] -= 1
                solution.append(s0)
            else:
                return False, None
        return True, solution
    else:
        for i, s in enumerate(ls):
            s0, s1 = s[0], s[1]
            if abc[s0] == 0 and abc[s1] == 0:
                return False, None
            if i == n - 1:
                abc[s0] -= 1
                abc[s1] += 1
                solution.append(s1)
            else:
                if abc[s0] == 1 and (s0 in ls[i + 1]):
                    abc[s0] += 1
                    abc[s1] -= 1
                    solution.append(s0)
                else:
                    abc[s0] -= 1
                    abc[s1] += 1
                    solution.append(s1)
        return True, solution


yn, solution = answer()
if yn:
    print('Yes')
    for s in solution:
        print(s)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
s_=[input() for i in range(n)]
ans=""
for i in range(n):
    s1,s2=s_[i]
    if s2=="B":
        if i!=n-1:
            if (s_[i+1] is "AC") and c==0 and a<=1:
                a,b,ans=(a+1,b-1,ans+"A")
            elif (s_[i+1] is "BC") and c==0 and b<=1:
                a,b,ans=(a-1,b+1,ans+"B")
            else:
                a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        else:
            a,b,ans=(a-1,b+1,ans+"B") if a>b else (a+1,b-1,ans+"A")
        if min(a,b)==-1:
            print("No")
            break
    elif s1=="A":
        if i!=n-1:
            if (s_[i+1] is "AB") and b==0 and a<=1:
                a,c,ans=(a+1,c-1,ans+"A")
            elif (s_[i+1] is "BC") and b==0 and c<=1:
                a,c,ans=(a-1,c+1,ans+"C")
            else:
                a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        else:
            a,c,ans=(a-1,c+1,ans+"C") if a>c else (a+1,c-1,ans+"A")
        if min(a,c)==-1:
            print("No")
            break
    else:
        if i!=n-1:
            if (s_[i+1] is "AB") and a==0 and b<=1:
                b,c,ans=(b+1,c-1,ans+"B")
            elif (s_[i+1] is "AC") and a==0 and c<=1:
                b,c,ans=(b-1,c+1,ans+"C")
            else:
                b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        else:
            b,c,ans=(b-1,c+1,ans+"C") if b>c else (b+1,c-1,ans+"B")
        if min(b,c)==-1:
            print("No")
            break
else:
    print("Yes")
    for j in range(n):
        print(ans[j])

Submission status: Wrong Answer

Solution code:
N,A,B,C=[int(s) for s in input().split()]
ls=[A,B,C]
car=['A','B','C']
ans=[]
for _ in range(N):
  S=input()
  if S=='AB':
    i,j,minus=[0,1,2]
  elif S=='BC':
    i,j,minus=[1,2,0]
  else:
    i,j,minus=[2,0,1]
  ls[minus]-=1
  if ls[minus]<0:
    print('No')
    break
  if ls[i]<ls[j]:
    ls[i]+=1
    ans.append(car[i])
  else:
    ls[j]+=1
    ans.append(car[j])
else:
  print('Yes')
  for e in ans:
    print(e)

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())
    D = {"AB" : (0, 1), "AC" : (0, 2), "BC" : (1, 2)}
    E = {0 : 'A', 1 : 'B', 2 : 'C'}
    queries = [D[input()] for _ in range(n)]
    s = a + b + c

    if s != 2:
        ans = []
        state = [a, b, c]
        for i, j in queries:
            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 = set()
    dp.add((a, b, c))
    prev = [{}] * n
    for k in range(n):
        ndp = set()
        i, j = queries[k]
        for state in dp:
            if state[i] > 0:
                next = list(state)
                next[i] -= 1
                next[j] += 1
                next = tuple(next)
                ndp.add(next)
                prev[k][next] = state
            if state[j] > 0:
                next = list(state)
                next[j] -= 1
                next[i] += 1
                next = tuple(next)
                ndp.add(next)
                prev[k][next] = state
        dp = ndp

    if not dp:
        print("No")
        return

    for now in dp:
        break

    ans = []
    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()