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: Runtime Error

Solution code:
def main():
    n, a, b, c = map(int, input().split())
    s_list = [input() for _ in [0]*n]

    if a == b == c == 0:
        print("No")
    elif a+b+c == 1:
        if a == 1:
            color = "A"
        elif b == 1:
            color = "B"
        else:
            color = "C"
        l = []
        for i in s_list:
            if color not in i:
                print("No")
                return
            j = list(i)
            j.remove(color)
            color = j[0]
            l.append(color)
        print("Yes")
        for i in l:
            print(i)
    else:
        print(1/0)
        s_list += ["ABC"]
        l = []
        for j, i in enumerate(s_list[:-1]):
            if i == "AB":
                if a >= 2:
                    l.append("B")
                    a -= 1
                    b += 1
                elif b >= 2:
                    l.append("A")
                    a += 1
                    b -= 1
                elif a == b == 0:
                    print("No")
                    return
                elif a == 1 and b == 0:
                    l.append("B")
                    a -= 1
                    b += 1
                elif a == 0 and b == 1:
                    l.append("A")
                    a += 1
                    b -= 1
                else:
                    if "A" in s_list[j+1]:
                        l.append("A")
                        a += 1
                        b -= 1
                    else:
                        l.append("B")
                        a -= 1
                        b += 1
            elif i == "BC":
                if b >= 2:
                    l.append("C")
                    b -= 1
                    c += 1
                elif c >= 2:
                    l.append("B")
                    b += 1
                    c -= 1
                elif b == c == 0:
                    print("No")
                    return
                elif b == 1 and c == 0:
                    l.append("C")
                    b -= 1
                    c += 1
                elif b == 0 and c == 1:
                    l.append("B")
                    b += 1
                    c -= 1
                else:
                    if "B" in s_list[j+1]:
                        l.append("B")
                        b += 1
                        c -= 1
                    else:
                        l.append("C")
                        b -= 1
                        c += 1
            else:
                if c >= 2:
                    l.append("A")
                    c -= 1
                    a += 1
                elif a >= 2:
                    l.append("C")
                    c += 1
                    a -= 1
                elif c == a == 0:
                    print("No")
                    return
                elif c == 1 and a == 0:
                    l.append("A")
                    c -= 1
                    a += 1
                elif b == 0 and c == 1:
                    l.append("C")
                    c += 1
                    a -= 1
                else:
                    if "C" in s_list[j+1]:
                        l.append("C")
                        c += 1
                        a -= 1
                    else:
                        l.append("A")
                        c -= 1
                        a += 1
        print("Yes")
        for i in l:
            print(i)


main()

Submission status: Runtime Error

Solution code:
def main():
    n, a, b, c = map(int, input().split())
    s_list = [input() for _ in [0]*n]

    if a == b == c == 0:
        print("No")
    elif a+b+c == 1:
        if a == 1:
            color = "A"
        elif b == 1:
            color = "B"
        else:
            color = "C"
        l = []
        for i in s_list:
            if color not in i:
                print("No")
                return
            j = list(i)
            j.remove(i)
            color = j[0]
            l.append(color)
        print("Yes")
        for i in l:
            print(i)
    else:
        s_list += ["ABC"]
        l = []
        for j, i in enumerate(s_list[:-1]):
            if i == "AB":
                if a >= 2:
                    l.append("B")
                    a -= 1
                    b += 1
                elif b >= 2:
                    l.append("A")
                    a += 1
                    b -= 1
                elif a == b == 0:
                    print("No")
                    return
                elif a == 1 and b == 0:
                    l.append("B")
                    a -= 1
                    b += 1
                elif a == 0 and b == 1:
                    l.append("A")
                    a += 1
                    b -= 1
                else:
                    if "A" in s_list[j+1]:
                        l.append("A")
                        a += 1
                        b -= 1
                    else:
                        l.append("B")
                        a -= 1
                        b += 1
            elif i == "BC":
                if b >= 2:
                    l.append("C")
                    b -= 1
                    c += 1
                elif c >= 2:
                    l.append("B")
                    b += 1
                    c -= 1
                elif b == c == 0:
                    print("No")
                    return
                elif b == 1 and c == 0:
                    l.append("C")
                    b -= 1
                    c += 1
                elif b == 0 and c == 1:
                    l.append("B")
                    b += 1
                    c -= 1
                else:
                    if "B" in s_list[j+1]:
                        l.append("B")
                        b += 1
                        c -= 1
                    else:
                        l.append("C")
                        b -= 1
                        c += 1
            else:
                if c >= 2:
                    l.append("A")
                    c -= 1
                    a += 1
                elif a >= 2:
                    l.append("C")
                    c += 1
                    a -= 1
                elif c == a == 0:
                    print("No")
                    return
                elif c == 1 and a == 0:
                    l.append("A")
                    c -= 1
                    a += 1
                elif b == 0 and c == 1:
                    l.append("C")
                    c += 1
                    a -= 1
                else:
                    if "C" in s_list[j+1]:
                        l.append("C")
                        c += 1
                        a -= 1
                    else:
                        l.append("A")
                        c -= 1
                        a += 1
        print("Yes")
        for i in l:
            print(i)


main()

Submission status: Runtime Error

Solution code:
def main():
    n, a, b, c = map(int, input().split())
    s_list = [input() for _ in [0]*n]

    if a == b == c == 0:
        print("No")
    elif a+b+c == 1:
        if a == 1:
            color = "A"
        elif b == 1:
            color = "B"
        else:
            color = "C"
        l = []
        for i in s_list:
            if color not in i:
                print("No")
                return
            j = list(i)
            j.remove(i)
            color = j[0]
            l.append(color)
        print("Yes")
        for i in l:
            print(i)
    else:
        s_list += ["ABC"]
        l = []
        for j, i in enumerate(s_list[::-1]):
            if i == "AB":
                if a >= 2:
                    l.append("B")
                    a -= 1
                    b += 1
                elif b >= 2:
                    l.append("A")
                    a += 1
                    b -= 1
                elif a == b == 0:
                    print("No")
                    return
                elif a == 1 and b == 0:
                    l.append("B")
                    a -= 1
                    b += 1
                elif a == 0 and b == 1:
                    l.append("A")
                    a += 1
                    b -= 1
                else:
                    if "A" in s_list[j+1]:
                        l.append("A")
                        a += 1
                        b -= 1
                    else:
                        l.append("B")
                        a -= 1
                        b += 1
            if i == "BC":
                if b >= 2:
                    l.append("C")
                    b -= 1
                    c += 1
                elif c >= 2:
                    l.append("B")
                    b += 1
                    c -= 1
                elif b == c == 0:
                    print("No")
                    return
                elif b == 1 and c == 0:
                    l.append("C")
                    b -= 1
                    c += 1
                elif b == 0 and c == 1:
                    l.append("B")
                    b += 1
                    c -= 1
                else:
                    if "B" in s_list[j+1]:
                        l.append("B")
                        b += 1
                        c -= 1
                    else:
                        l.append("C")
                        b -= 1
                        c += 1
            if i == "AC":
                if c >= 2:
                    l.append("A")
                    c -= 1
                    a += 1
                elif a >= 2:
                    l.append("C")
                    c += 1
                    a -= 1
                elif c == a == 0:
                    print("No")
                    return
                elif c == 1 and a == 0:
                    l.append("A")
                    c -= 1
                    a += 1
                elif b == 0 and c == 1:
                    l.append("C")
                    c += 1
                    a -= 1
                else:
                    if "C" in s_list[j+1]:
                        l.append("C")
                        c += 1
                        a -= 1
                    else:
                        l.append("A")
                        c -= 1
                        a += 1
        print("Yes")
        for i in l:
            print(i)


main()

Submission status: Runtime Error

Solution code:
"""
import copy

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


s = s[::-1]
ans = []

tmp = [[0,0,0]]

for s1 in s:
    for i in range(len(tmp)):
        t = tmp.pop(0)
        if ans:
            ans1 = ans.pop(0)
        else:
            ans1 = ''
        if s1 == 'AB':
            if t[0] == 0 and t[1] == 0:
                tmp.append([t[0]+1,t[1],t[2]])
                tmp.append([t[0],t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'A')
            elif t[0] == 0:
                tmp.append([t[0]+1,t[1]-1,t[2]])
                ans.append(ans1+'B')
            elif t[1] == 0:
                tmp.append([t[0]-1,t[1]+1,t[2]])
                ans.append(ans1+'A')
            else:
                tmp.append([t[0]+1,t[1]-1,t[2]])
                tmp.append([t[0]-1,t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'A')
        elif s1 == 'BC':
            if t[1] == 0 and t[2] == 0:
                tmp.append([t[0],t[1],t[2]+1])
                tmp.append([t[0],t[1]+1,t[2]])
                ans.append(ans1+'B')
                ans.append(ans1+'C')
            elif t[1] == 0:
                tmp.append([t[0],t[1]+1,t[2]-1])
                ans.append(ans1+'C')
            elif t[2] == 0:
                tmp.append([t[0],t[1]-1,t[2]+1])
                ans.append(ans1+'B')
            else:
                tmp.append([t[0],t[1]-1,t[2]+1])
                tmp.append([t[0],t[1]+1,t[2]-1])
                ans.append(ans1+'B')
                ans.append(ans1+'C')
        else:
            if t[2] == 0 and t[0] == 0:
                tmp.append([t[0]+1,t[1],t[2]])
                tmp.append([t[0],t[1],t[2]+1])
                ans.append(ans1+'C')
                ans.append(ans1+'A')

            elif t[2] == 0:
                tmp.append([t[0]-1,t[1],t[2]+1])
                ans.append(ans1+'A')

            elif t[0] == 0:
                tmp.append([t[0]+1,t[1],t[2]-1])
                ans.append(ans1+'C')

            else:
                tmp.append([t[0]+1,t[1],t[2]-1])
                tmp.append([t[0]-1,t[1],t[2]+1])
                ans.append(ans1+'C')
                ans.append(ans1+'A')

    li = [True]*len(tmp)
    for i in range(len(tmp)):
        for j in range(i+1, len(tmp)):
            for k in range(3):
                if tmp[i][k] <= tmp[j][k]:
                    li[j] = False
    tmp2 = []
    ans2 = []
    for i in range(len(tmp)):
        if li[i]:
            tmp2.append(tmp[i])
            ans2.append(ans[i])
    tmp = copy.copy(tmp2)
    ans = copy.copy(ans2)

for i in range(len(tmp)):
    if a >= tmp[i][0] and b >= tmp[i][1] and c >= tmp[i][2]:
        print('Yes')
        for j in range(n):
            print(ans[i][-j-1])
        exit()

print('No')
"""

n,a,b,c = map(int,input().split())
s = []
for _ in range(n):
    s.append(input())
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    ans = ''
    for i in range(n):
        if s[i] == 'AB':
            if a == 1 and b == 0:
                a = 0
                b = 1
                ans += 'B'
            elif a == 0 and b == 1:
                a = 1
                b = 0
                ans += 'A'
            else:
                print('No')
                exit()
        elif s[i] == 'BC':
            if b == 1 and c == 0:
                b = 0
                c = 1
                ans += 'C'
            elif b == 0 and c == 1:
                b = 1
                c = 0
                ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a == 1 and c == 0:
                a = 0
                c = 1
                ans += 'C'
            elif a == 0 and c == 1:
                a = 1
                c = 0
                ans += 'A'
            else:
                print('No')
                exit()
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans = ''
    for i in range(n):
        s1 = s[i]
        if i != n-1:
            s2 = s[i+1]
        if s1 == 'AB':
            if 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:
                if i != n-1 or s2 == 'BC':
                    b += 1
                    a -= 1
                    ans += 'B'
                else:
                    a += 1
                    b -= 1
                    ans += 'A'
            else:
                print('No')
                exit()
        elif s1 == 'BC':
            if 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:
                if i != n-1 or s2 == 'AC':
                    c += 1
                    b -= 1
                    ans += 'C'
                else:
                    b += 1
                    c -= 1
                    ans += 'B'
            else:
                print('No')
                exit()
        else:
            if 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:
                if i != n-1 or s2 == 'AB':
                    a += 1
                    c -= 1
                    ans += 'A'
                else:
                    a -= 1
                    c += 1
                    ans += 'C'
            else:
                print('No')
                exit()
        s1 = s2
    print('Yes')
    for i in range(n):
        print(ans[i])

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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