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 sys


N, A, B, C = map(int, input().split())
stock = [A, B, C]

s = [input().rstrip('\n') for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])
if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] in target[i+1] and stock[target[i][1]] >= 1:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            elif stock[target[i][0]] >= 1:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
            else:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Wrong Answer

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

def end():
    print("No")
    exit()

def under_zero(x):
    if x < 0:
        return True
    else:
        return False

def solve(string,a,b,c):

    if string == "AB":
        if a>=b:
            a -= 1
            b += 1
            log.append("B")
            if under_zero(a):
                return end()
        else:
            a += 1
            b -= 1
            log.append("A")
            if under_zero(b):
                return end()
    elif string == "AC":
        if a>=c:
            a -= 1
            c += 1
            log.append("C")
            if under_zero(a):
                return end()
        else:
            a += 1
            c -= 1
            log.append("A")
            if under_zero(c):
                return end()
    else:
        if b>=c:
            b -= 1
            c += 1
            log.append("C")
            if under_zero(b):
                return end()
        else:
            b += 1
            c -= 1
            log.append("B")
            if under_zero(c):
                return end()

def check(i,a,b,c):
    now = list(s[i])
    next = list(s[i+1])

    for i in now:
        if i in next:
            d = i

    #print(d)

    # 連続でAが来る
    if 'A' == d and now[1] == 'B':
        a += 1
        b -= 1
        log.append("A")

    elif 'A' == d and now[1] == 'C':
        a += 1
        c -= 1
        log.append("A")

    # bが重複
    if 'B' == d and now[0] == 'A':
        a -= 1
        b += 1
        log.append("B")

    elif 'B' == d and now[1] == 'C':
        b += 1
        c -= 1
        log.append("B")

    # cが重複
    if 'C' == d and now[0] == 'A':
        c += 1
        a -= 1
        log.append("C")

    elif 'C' == d and now[0] == 'B':
        c += 1
        b -= 1
        log.append("C")

    return

for i in range(n):
    if i != n-1:
        if (a + b + c) == 2 and (a == 1 and b == 1) or (a == 1 and c == 1) or (c == 1 and b == 1):
            check(i,a,b,c)
    else:
        solve(s[i],a,b,c)
else:
    print("Yes")
    print(*log, sep='\n')

Submission status: Wrong Answer

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

answers = ["" for _ in range(n)]
for i in range(n):
    sel = input()
    if sel == 'AB':
        if a > b:
            answers[i] = "B"
            b += 1
            a -= 1
        else:
            answers[i] = "A"
            a += 1
            b -= 1
    elif sel == 'BC':
        if b > c:
            answers[i] = "C"
            c += 1
            b -= 1
        else:
            answers[i] = "B"
            b += 1
            c -= 1
    else:
        if c > a:
            answers[i] = "A"
            a += 1
            c -= 1
        else:
            answers[i] = "C"
            c += 1
            a -= 1
if a < 0 or b < 0 or c < 0:
    print("No")
else:
    print("Yes")
    for a in answers:
        print(a)

Submission status: Wrong Answer

Solution code:
import sys


N, A, B, C = map(int, input().split())
stock = [A, B, C]

s = [input().rstrip('\n') for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])
if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] in target[i+1] and stock[target[i][1]] >= 1:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            else:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Wrong Answer

Solution code:
import sys


N, A, B, C = map(int, input().split())
stock = [A, B, C]

s = [input().rstrip('\n') for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    print(s[i])
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])
if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] in target[i+1] and stock[target[i][1]] >= 1:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            else:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Wrong Answer

Solution code:
import sys


N, A, B, C = map(int, input().split())
stock = [A, B, C]

s = [input() for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])

if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] in target[i+1] and stock[target[i][1]] >= 1:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            else:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Wrong Answer

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

s = [input() for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])

if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] == target[i+1][0] or target[i][0] == target[i+1][1]:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            else:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Wrong Answer

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

s=[]

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

if a+b+c==0:
    print('No')
    exit()
        
ans=[]
if a+b+c==1:
    for i in range(n):
        if s[i]=="AB":
            if a==1:
                a-=1
                b+=1
                ans.append("B")
            elif b==1:
                a+=1
                b-=1
                ans.append("A")
            else:
                print("No")
                exit()
        elif s[i]=="AC":
            if a==1:
                a-=1
                c+=1
                ans.append("C")
            elif c==1:
                a+=1
                c-=1
                ans.append("A")
            else:
                print("No")
                exit()
        elif s[i]=="BC":
            if b==1:
                b-=1
                c+=1
                ans.append("C")
            elif c==1:
                b+=1
                c-=1
                ans.append("B")
            else:
                print("No")
                exit()

if a+b+c > 1:

    for i in range(n):
        if s[i]=="AB":
            if a+b==0:
                print("No")
                exit()
            elif a+b==1 or i==n-1 or a==0 or b==0:
                if a>=1:
                    a-=1
                    b+=1
                    ans.append("B")
                elif b>=1:
                    a+=1
                    b-=1
                    ans.append("A")
                else:
                    print("No")
                    exit()
            elif a+b>1:
                if s[i+1]=="AB" or s[i+1]=="AC":
                    a+=1
                    b-=1
                    ans.append("A")
                elif s[i+1]=="BC":
                    a-=1
                    b+=1
                    ans.append("B")

        if s[i]=="AC":
            if a+c==0:
                print("No")
                exit()
            elif a+c==1 or i==n-1 or a==0 or c==0:
                if a>=1:
                    a-=1
                    c+=1
                    ans.append("C")
                elif c>=1:
                    a+=1
                    c-=1
                    ans.append("A")
                else:
                    print("No")
                    exit()                    
            elif a+c>1:
                if s[i+1]=="AB" or s[i+1]=="AC":
                    a+=1
                    c-=1
                    ans.append("A")
                elif s[i+1]=="BC":
                    a-=1
                    c+=1
                    ans.append("C")


        elif s[i]=="BC":
            if b+c==0:
                print("No")
            if b+c==1 or i==n-1 or b==0 or c==0:
                if b>=1:
                    b-=1
                    c+=1
                    ans.append("C")
                elif c>=1:
                    b+=1
                    c-=1
                    ans.append("B")
                else:
                    print("No")
                    exit()

            elif b+c >1:
                if s[i+1]=="AB" or s[i+1]=="AB":
                    b+=1
                    c-=1
                    ans.append("B")
                elif s[i+1]=="AC":
                    a-=1
                    c+=1
                    ans.append("C")

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

Submission status: Wrong Answer

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

s = [input() for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])

if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
# =============================================================================
#         for i in range(N-1):
#             if target[i][0] in target[i+1]:
#                 stock[target[i][0]] += 1
#                 stock[target[i][1]] -= 1
#                 print(to_char[target[i][0]])
#             else:
#                 stock[target[i][1]] += 1
#                 stock[target[i][0]] -= 1
#                 print(to_char[target[i][1]])
#         if stock[target[-1][0]] == 0:
#             print(to_char[target[-1][0]])
#         else:
#             print(to_char[target[-1][1]])
# =============================================================================

Submission status: Wrong Answer

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

s = [input() for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])

if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] in target[i+1] and stock[target[i][1]]>=1:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            else:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Wrong Answer

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

s=[]

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

if a+b+c==0:
    print('No')
    exit()
        
ans=[]
if a+b+c==1:
    for i in range(n):
        if s[i]=="AB":
            if a==1:
                a-=1
                b+=1
                ans.append("B")
            elif b==1:
                a+=1
                b-=1
                ans.append("A")
            else:
                print("No")
                exit()
        elif s[i]=="AC":
            if a==1:
                a-=1
                c+=1
                ans.append("C")
            elif c==1:
                a+=1
                c-=1
                ans.append("A")
            else:
                print("No")
                exit()
        elif s[i]=="BC":
            if b==1:
                b-=1
                c+=1
                ans.append("C")
            elif c==1:
                b+=1
                c-=1
                ans.append("B")
            else:
                print("No")
                exit()

if a+b+c > 1:

    for i in range(n):
        if s[i]=="AB":
            if a+b==0:
                print("No")
                exit()
            elif a+b==1 or i==n-1:
                if a>=1:
                    a-=1
                    b+=1
                    ans.append("B")
                elif b>=1:
                    a+=1
                    b-=1
                    ans.append("A")
                else:
                    print("No")
                    exit()
            elif a+b>1:
                if s[i+1]=="AB" or s[i+1]=="AC":
                    a+=1
                    b-=1
                    ans.append("A")
                elif s[i+1]=="BC":
                    a-=1
                    b+=1
                    ans.append("B")

        if s[i]=="AC":
            if a+c==0:
                print("No")
                exit()
            elif a+c==1 or i==n-1:
                if a>=1:
                    a-=1
                    c+=1
                    ans.append("C")
                elif c>=1:
                    a+=1
                    c-=1
                    ans.append("A")
                else:
                    print("No")
                    exit()                    
            elif a+c>1:
                if s[i+1]=="AB" or s[i+1]=="AC":
                    a+=1
                    c-=1
                    ans.append("A")
                elif s[i+1]=="BC":
                    a-=1
                    c+=1
                    ans.append("C")


        elif s[i]=="BC":
            if b+c==0:
                print("No")
            if b+c==1 or i==n-1:
                if b>=1:
                    b-=1
                    c+=1
                    ans.append("C")
                elif c>=1:
                    b+=1
                    c-=1
                    ans.append("B")
                else:
                    print("No")
                    exit()

            elif b+c >1:
                if s[i+1]=="AB" or s[i+1]=="AB":
                    b+=1
                    c-=1
                    ans.append("B")
                elif s[i+1]=="AC":
                    a-=1
                    c+=1
                    ans.append("C")

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

Submission status: Wrong Answer

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

s = [input() for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])

if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] in target[i+1]:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            else:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Wrong Answer

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

def end():
    print("No")
    exit()

def under_zero(x):
    if x < 0:
        return True
    else:
        return False

def solve(string,a,b,c):

    if string == "AB":
        if a>b:
            a -= 1
            b += 1
            log.append("B")
            if under_zero(a):
                return end()
        else:
            a += 1
            b -= 1
            log.append("A")
            if under_zero(b):
                return end()
    elif string == "AC":
        if a>c:
            a -= 1
            c += 1
            log.append("C")
            if under_zero(a):
                return end()
        else:
            a += 1
            c -= 1
            log.append("A")
            if under_zero(c):
                return end()
    else:
        if b>c:
            b -= 1
            c += 1
            log.append("C")
            if under_zero(b):
                return end()
        else:
            b += 1
            c -= 1
            log.append("B")
            if under_zero(c):
                return end()

def check(i,a,b,c):
    now = list(s[i])
    next = list(s[i+1])

    for i in now:
        if i in next:
            d = i

    #print(d)

    # 連続でAが来る
    if 'A' == d and next[1] == 'B':
        a += 1
        b -= 1
        log.append("A")

    elif 'A' == d and next[1] == 'C':
        a += 1
        c -= 1
        log.append("A")

    # bが重複
    if 'B' == d and next[0] == 'A':
        a -= 1
        b += 1
        log.append("B")

    elif 'B' == d and next[1] == 'C':
        b += 1
        c -= 1
        log.append("B")

    # cが重複
    if 'C' == d and next[0] == 'A':
        c += 1
        a -= 1
        log.append("C")

    elif 'C' == d and next[0] == 'B':
        c += 1
        b -= 1
        log.append("C")

    return

for i in range(n):
    if i != n-1:
        if (a + b + c) == 2 and (a == 1 and b == 1) or (a == 1 and c == 1) or (c == 1 and b == 1):
            check(i,a,b,c)
    else:
        solve(s[i],a,b,c)
else:
    print("Yes")
    print(*log, sep='\n')

Submission status: Wrong Answer

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def _solve(A, B, C, S):
    for c in 'ABC':
        counter = {'A': A, 'B': B, 'C': C}
        answer = []
        if counter[c] == 0:
            continue
        counter[c] = 1
        for other in 'ABC':
            if c != other:
                counter[c] = 0
        for i, s in enumerate(S):
            if counter[s[0]] > 0:
                counter[s[1]] += 1
                counter[s[0]] -= 1
                answer.append(s[1])
            elif counter[s[1]] > 0:
                counter[s[1]] -= 1
                counter[s[0]] += 1
                answer.append(s[0])
            else:
                answer = None
                break
        if answer is not None:
            return answer
    return None



def solve():
    N, A, B, C = read_ints()
    S = []
    for _ in range(N):
        S.append(input().strip())
    answer = _solve(A, B, C, S)
    if answer is None:
        answer = _solve(A, B, C, S)
        if answer is None:
            print('No')
            return
    print('Yes')
    for a in answer:
        print(a)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

def end():
    print("No")
    exit()

def under_zero(x):
    if x < 0:
        return True
    else:
        return False

def solve(string,a,b,c):

    if string == "AB":
        if a>b:
            a -= 1
            b += 1
            log.append("B")
            if under_zero(a):
                return end()
        else:
            a += 1
            b -= 1
            log.append("A")
            if under_zero(b):
                return end()
    elif string == "AC":
        if a>c:
            a -= 1
            c += 1
            log.append("C")
            if under_zero(a):
                return end()
        else:
            a += 1
            c -= 1
            log.append("A")
            if under_zero(c):
                return end()
    else:
        if b>c:
            b -= 1
            c += 1
            log.append("C")
            if under_zero(b):
                return end()
        else:
            b += 1
            c -= 1
            log.append("B")
            if under_zero(c):
                return end()

def check(i,a,b,c):
    now = list(s[i])
    next = list(s[i+1])

    for i in now:
        if i in next:
            d = i

    #print(d)

    # aが重複
    if 'A' == d and now[1] == 'B':
        a += 1
        b -= 1
        log.append("A")

    elif 'A' == d and now[1] == 'C':
        a += 1
        c -= 1
        log.append("A")

    # bが重複
    if 'B' == d and now[0] == 'A':
        a -= 1
        b += 1
        log.append("B")

    elif 'B' == d and now[1] == 'C':
        b += 1
        c -= 1
        log.append("B")

    # cが重複
    if 'C' == d and now[0] == 'A':
        c += 1
        a -= 1
        log.append("C")

    elif 'C' == d and now[0] == 'B':
        c += 1
        b -= 1
        log.append("C")

    return

for i in range(n):
    if i != n-1:
        if (a + b + c) == 2 and (a == 1 and b == 1) or (a == 1 and c == 1) or (c == 1 and b == 1):
            check(i,a,b,c)
    else:
        solve(s[i],a,b,c)
else:
    print("Yes")
    print(*log, sep='\n')

Submission status: Wrong Answer

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

def end():
    print("No")
    exit()

def under_zero(x):
    if x < 0:
        return True
    else:
        return False

def solve(string,a,b,c):

    if string == "AB":
        if a>b:
            a -= 1
            b += 1
            log.append("B")
            if under_zero(a):
                return end()
        else:
            a += 1
            b -= 1
            log.append("A")
            if under_zero(b):
                return end()
    elif string == "AC":
        if a>c:
            a -= 1
            c += 1
            log.append("C")
            if under_zero(a):
                return end()
        else:
            a += 1
            c -= 1
            log.append("A")
            if under_zero(c):
                return end()
    else:
        if b>c:
            b -= 1
            c += 1
            log.append("C")
            if under_zero(b):
                return end()
        else:
            b += 1
            c -= 1
            log.append("B")
            if under_zero(c):
                return end()

def check(i,a,b,c):
    now = list(s[i])
    next = list(s[i+1])

    for i in now:
        if i in next:
            d = i

    #print(d)

    # aが重複
    if 'A' == d and now[1] == 'B':
        a += 1
        b -= 1
        log.append("A")

    elif 'A' == d and now[1] == 'C':
        a += 1
        c -= 1
        log.append("A")

    # bが重複
    if 'B' == d and now[0] == 'A':
        a -= 1
        b += 1
        log.append("B")

    elif 'B' == d and now[1] == 'C':
        b += 1
        c -= 1
        log.append("B")

    # cが重複
    if 'C' == d and now[0] == 'A':
        c += 1
        a -= 1
        log.append("C")

    elif 'C' == d and now[0] == 'B':
        c += 1
        b -= 1
        log.append("C")

    return

for i in range(n):
    if (a + b + c) == 2 and i != n-1 and a != 2 and b != 2 and c != 2:
        check(i,a,b,c)
    else:
        solve(s[i],a,b,c)
else:
    print("Yes")
    print(*log, sep='\n')