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:
import sys

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

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

Submission status: Runtime Error

Solution code:
import sys

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

if a+b+c==0:
    print('No')
elif a+b+c==1:
    ans=[]
    for i in range(n):
        s=str(input())
        if s=='AB':
            if a==b==0:
                print('No')
                sys.exit()
            elif a==1:
                a-=1
                b+=1
                ans.append('B')
            else:
                a+=1
                b-=1
                ans.append('A')
        elif s=='BC':
            if b==c==0:
                print('No')
                sys.exit()
            elif b==1:
                b-=1
                c+=1
                ans.append('C')
            else:
                b+=1
                c-=1
                ans.append('B')
        else:
            if c==a==0:
                print('No')
            elif c==1:
                c-=1
                a+=1
                ans.append('A')
            else:
                a-=1
                c+=1
                ans.append('C')
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans=[]
    S=[]
    for i in range(n):
        S.append(str(input()))
    for i in range(n):
        s=S[i]
        if s=='AB':
            if a==b==0:
                print('No')
                sys.exit()
            elif a==0:
                a+=1
                b-=1
                ans.append('A')
            elif b==0:
                b+=1
                a-=1
                ans.append('B')
            elif a==b==1 and i!=n and s!=S[i+1]:
                if S[i+1]=='BC':
                    b+=1
                    a-=1
                    ans.append('B')
                else:
                    a+=1
                    b-=1
                    ans.append('A')
            else:
                a+=1
                b-=1
                ans.append('A')
        elif s=='BC':
            if b==c==0:
                print('No')
                sys.exit()
            elif b==0:
                b+=1
                c-=1
                ans.append('B')
            elif c==0:
                c+=1
                b-=1
                ans.append('C')
            elif b==c==1 and i!=n and s!=S[i+1]:
                if S[i+1]=='CA':
                    c+=1
                    b-=1
                    ans.append('C')
                else:
                    b+=1
                    c-=1
                    ans.append('B')
            else:
                b+=1
                c-=1
                ans.append('B')
        else:
            if c==a==0:
                print('No')
                sys.exit()
            elif c==0:
                c+=1
                a-=1
                ans.append('C')
            elif a==0:
                a+=1
                c-=1
                ans.append('A')
            elif c==a==1 and i!=n and s!=S[i+1]:
                if S[i+1]=='AB':
                    a+=1
                    c-=1
                    ans.append('A')
                else:
                    c+=1
                    a-=1
                    ans.append('C')
            else:
                c+=1
                a-=1
                ans.append('C')
    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 i in range(N)]
for i in range(N):
    s[i] = input()
   
valid = True
ans = []
all_sum = A + B + C
if all_sum >= 3:
    for op in s:
        if op == "AB":
            if A > B:
                A -= 1
                B += 1
                ans.append("B")
            else:
                A += 1
                B -= 1
                ans.append("A")
        elif op == "BC":
            if B > C:
                B -= 1
                C += 1
                ans.append("C")
            else:
                B += 1
                C -= 1
                ans.append("B")
        else:
            if A > C:
                A -= 1
                C += 1
                ans.append("C")
            else:
                A += 1
                C -= 1
                ans.append("A")
        if A < 0 or B < 0 or C < 0:
            valid = False
            break
        
    if valid:
        print("Yes")
        for c in ans:
            print(c)
    else:
        print("No")
else:
    for i in range(N):
        op = s[i]
        if op == "AB":
            if A > B:
                A -= 1
                B += 1
                ans.append("B")
            elif A < B:
                A += 1
                B -= 1
                ans.append("A")
            else:
                if i != N - 1 and "A" in op[i]:
                    A += 1
                    B -= 1
                    ans.append("A")
                else:
                    A -= 1
                    B += 1
                    ans.append("B")
        elif op == "AC":
            if A > C:
                A -= 1
                C += 1
                ans.append("C")
            elif A < C:
                A += 1
                C -= 1
                ans.append("A")
            else:
                if i != N - 1 and "A" in op[i]:
                    A += 1
                    C -= 1
                    ans.append("A")
                else:
                    A -= 1
                    C += 1
                    ans.append("C")
        else:
            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 and "B" in op[i]:
                    B += 1
                    C -= 1
                    ans.append("B")
                else:
                    B -= 1
                    C += 1
                    ans.append("C")
        if A < 0 or B < 0 or C < 0:
            valid = False
            break
    if valid:
        print("Yes")
        for c in ans:
            print(c)
    else:
        print("No")

Submission status: Runtime Error

Solution code:
from collections import deque
# N < 10^5, A, B, C < 10^9
# s = ('AB', 'AC', 'BC')
# A >= 0, B >= 0, C >= 0
# N回操作できるか？できるなら道筋を
# 各ステップで2通りのどちらかしか選択できない。
# DPだと思う
# (A, B, C, i, '')

def f(x, dic):
    if x[4] == '':
        return 1
    else:
        if f(dic[x], dic):
            print(x[4])
            return 1

N, A, B, C = [int(i) for i in input().split()]
S = [input() for _ in range(N)]
i = 0
dic = {}
que = deque([(A, B, C, i, '')])
while len(que) != 0:
    x = que.pop()
    i = x[3]
    if x[3] == N:
        print('Yes')
        f(x, dic)
        exit()
    if S[i] == 'AB':
        if x[1] > 0:
            newx = (x[0]+1, x[1]-1, x[2], x[3]+1, 'A')
            dic[newx] = x
            que.append(newx)
        if x[0] > 0:
            newx = (x[0]-1, x[1]+1, x[2], x[3]+1, 'B')
            dic[newx] = x
            que.append(newx)
    elif S[i] == 'BC':
        if x[2] > 0:
            newx = (x[0], x[1]+1, x[2]-1, x[3]+1, 'B')
            dic[newx] = x
            que.append(newx)
        if x[1] > 0:
            newx = (x[0], x[1]-1, x[2]+1, x[3]+1, 'C')
            dic[newx] = x
            que.append(newx)
    elif S[i] == 'AC':
        if x[2] > 0:
            newx = (x[0]+1, x[1], x[2]-1, x[3]+1, 'A')
            dic[newx] = x
            que.append(newx)
        if x[0] > 0:
            newx = (x[0]-1, x[1], x[2]+1, x[3]+1, 'C')
            dic[newx] = x
            que.append(newx)
    else:
        print('error')
print('No')

Submission status: Runtime Error

Solution code:
from collections import deque
# N < 10^5, A, B, C < 10^9
# s = ('AB', 'AC', 'BC')
# A >= 0, B >= 0, C >= 0
# N回操作できるか？できるなら道筋を
# 各ステップで2通りのどちらかしか選択できない。
# DPだと思う
# (A, B, C, i)
que = deque()
def func(x, S):
    i = x[3]
    if x[3] == N:
        print('Yes')
        return 1
    if S[i] == 'AB':
        if x[1] > 0:
            result = func((x[0]+1, x[1]-1, x[2], x[3]+1), S)
            if result:
                que.append('A')
                return 1
        if x[0] > 0:
            result = func((x[0]-1, x[1]+1, x[2], x[3]+1), S)
            if result:
                que.append('B')
                return 1
    elif S[i] == 'BC':
        if x[2] > 0:
            result = func((x[0], x[1]+1, x[2]-1, x[3]+1), S)
            if result:
                que.append('B')
                return 1

        if x[1] > 0:
            result = func((x[0], x[1]-1, x[2]+1, x[3]+1), S)
            if result:
                que.append('C')
                return 1
    elif S[i] == 'AC':
        if x[2] > 0:
            result = func((x[0]+1, x[1], x[2]-1, x[3]+1), S)
            if result:
                que.append('A')
                return 1
        if x[0] > 0:
            result = func((x[0]-1, x[1], x[2]+1, x[3]+1), S)
            if result:
                que.append('C')
                return 1
    return 0


N, A, B, C = [int(i) for i in input().split()]
S = [input() for _ in range(N)]
i = 0

result = func((A, B, C, i), S)
if result == 1:
    while len(que) > 0:
        print(que.pop())
else:
    print('No')

Submission status: Runtime Error

Solution code:
from collections import deque
# N < 10^5, A, B, C < 10^9
# s = ('AB', 'AC', 'BC')
# A >= 0, B >= 0, C >= 0
# N回操作できるか？できるなら道筋を
# 各ステップで2通りのどちらかしか選択できない。
# DPだと思う
# (A, B, C, i)
que = deque()
def func(x):
    i = x[3]
    if x[3] == N:
        print('Yes')
        return 1
    if S[i] == 'AB':
        if x[1] > 0:
            result = func((x[0]+1, x[1]-1, x[2], x[3]+1))
            if result:
                que.append('A')
                return 1
        if x[0] > 0:
            result = func((x[0]-1, x[1]+1, x[2], x[3]+1))
            if result:
                que.append('B')
                return 1
    elif S[i] == 'BC':
        if x[2] > 0:
            result = func((x[0], x[1]+1, x[2]-1, x[3]+1))
            if result:
                que.append('B')
                return 1

        if x[1] > 0:
            result = func((x[0], x[1]-1, x[2]+1, x[3]+1))
            if result:
                que.append('C')
                return 1
    elif S[i] == 'AC':
        if x[2] > 0:
            result = func((x[0]+1, x[1], x[2]-1, x[3]+1))
            if result:
                que.append('A')
                return 1
        if x[0] > 0:
            result = func((x[0]-1, x[1], x[2]+1, x[3]+1))
            if result:
                que.append('C')
                return 1
    return 0


N, A, B, C = [int(i) for i in input().split()]
S = [input() for _ in range(N)]
i = 0

result = func((A, B, C, i))
if result == 1:
    while len(que) > 0:
        print(que.pop())
else:
    print('No')

Submission status: Runtime Error

Solution code:

def resolve():
    N, A, B, C = map(int, input().split())
    S = [input() for _ in range(N)]

    ans = []
    def dfs(now, a, b, c):
        if now == N:
            return True
        if S[now] == "AB":
            if a == 0 and b == 0:
                return False
            if 0 < a and dfs(now+1, a-1, b+1, c):
                ans.append("B")
                return True
            if 0 < b and dfs(now+1, a+1, b-1, c):
                ans.append("A")
                return True
            return False
        if S[now] == "AC":
            if a == 0 and c == 0:
                return False
            if 0 < a and dfs(now+1, a-1, b, c+1):
                ans.append("C")
                return True
            if 0 < c and dfs(now+1, a+1, b, c-1):
                ans.append("A")
                return True
            return False
        if S[now] == "BC":
            if c == 0 and b == 0:
                return False
            if 0 < b and dfs(now+1, a, b-1, c+1):
                ans.append("C")
                return True
            if 0 < c and dfs(now+1, a, b+1, c-1):
                ans.append("B")
                return True
            return False

    dfs(0, A, B, C)
    if len(ans) == 0:
        print("No")
        return
    else:
        print("Yes")
        [print(a) for a in ans[::-1]]


if __name__ == "__main__":
    resolve()

Submission status: Runtime Error

Solution code:
def put(ret, val):
    if val == 0:
        ret.append('A')
    elif val == 1:
        ret.append('B')
    else:
        ret.append('C')


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

s = []

for i in range(n):
    tmp = input()
    if tmp == 'AB':
        val = 0
    elif tmp == 'BC':
        val = 1
    else:
        val = 2
    s.append(val)

ret = []
if a + b + c == 0:
    print('No')
    exit()
elif a + b + c == 1:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 1:
            t[item] = 0
            t[(item+1) % 3] = 1
            put(ret, (item+1) % 3)
        elif t[(item+1) % 3] == 1:
            t[item] = 1
            t[(item+1) % 3] = 0
            put(ret, item)
        else:
            print('No')
            exit()
elif a + b + c == 2:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put((item+1) % 3)
        elif t[item] < t[(item+1) % 3]:
            t[item] += 1
            t[(item+1) % 3] -= 1
            put(item)
        elif t[item] == 0:
            print('No')
            exit()
        elif t[item] == 2:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, (item+1) % 3)
        else:
            if len(s) > i + 1:
                if s[i+1] == (item + 1) % 3:
                    t[item] -= 1
                    t[(item+1) % 3] += 1
                    put(ret, (item+1) % 3)
                else:
                    t[item] += 1
                    t[(item+1) % 3] -= 1
                    put(ret, item)
            else:
                put(ret, item)

else:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, (item+1) % 3)
        else:
            t[item] += 1
            t[(item+1) % 3] -= 1
            put(ret, item)

print('Yes')
for item in ret:
    print(item)

Submission status: Runtime Error

Solution code:
def put(ret, val):
    if val == 0:
        ret.append('A')
    elif val == 1:
        ret.append('B')
    else:
        ret.append('C')


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

s = []

for i in range(n):
    tmp = input()
    if tmp == 'AB':
        val = 0
    elif tmp == 'BC':
        val = 1
    else:
        val = 2
    s.append(val)

ret = []
if a + b + c == 0:
    print('No')
    exit()
elif a + b + c == 1:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 1:
            t[item] = 0
            t[(item+1) % 3] = 1
            put(ret, (item+1) % 3)
        elif t[(item+1) % 3] == 1:
            t[item] = 1
            t[(item+1) % 3] = 0
            put(ret, item)
        else:
            print('No')
            exit()
elif a + b + c == 2:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put((item+1) % 3)
        elif t[item] < t[(item+1) % 3]:
            t[item] += 1
            t[(item+1) % 3] -= 1
            put(item)
        elif t[item] == 0:
            print('No')
            exit()
        elif t[item] == 2:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put((item+1) % 3)
        else:
            if len(s) > i + 1:
                if s[i+1] == (item + 1) % 3:
                    t[item] -= 1
                    t[(item+1) % 3] += 1
                    put((item+1) % 3)
                else:
                    t[item] += 1
                    t[(item+1) % 3] -= 1
                    put(item)
            else:
                put(item)

else:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, (item+1) % 3)
        else:
            t[item] += 1
            t[(item+1) % 3] -= 1
            put(ret, item)

print('Yes')
for item in ret:
    print(item)

Submission status: Runtime Error

Solution code:
def put(ret, val):
    if val == 0:
        ret.append('A')
    elif val == 1:
        ret.append('B')
    else:
        ret.append('C')


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

s = []

for i in range(n):
    tmp = input()
    if tmp == 'AB':
        val = 0
    elif tmp == 'BC':
        val = 1
    else:
        val = 2
    s.append(val)

ret = []
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 1:
            t[item] = 0
            t[(item+1) % 3] = 1
            put(ret, (item+1) % 3)
        elif t[(item+1) % 3] == 1:
            t[item] = 1
            t[(item+1) % 3] = 0
            put(ret, item)
        else:
            print('No')
            exit()
elif a + b + c == 2:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put((item+1) % 3)
        elif t[item] < t[(item+1) % 3]:
            t[item] += 1
            t[(item+1) % 3] -= 1
            put(item)
        elif t[item] == 0:
            print('No')
            exit()
        elif t[item] == 2:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put((item+1) % 3)
        else:
            if len(s) > i + 1:
                if s[i+1] == (item + 1) % 3:
                    t[item] -= 1
                    t[(item+1) % 3] += 1
                    put(item+1)
                else:
                    t[item] += 1
                    t[(item+1) % 3] -= 1
                    put(item)
            else:
                put(item)

else:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, (item+1) % 3)
        else:
            t[item] += 1
            t[(item+1) % 3] -= 1
            put(ret, item)

print('Yes')
for item in ret:
    print(item)

Submission status: Runtime Error

Solution code:
def put(ret, val):
    if val == 0:
        ret.append('A')
    elif val == 1:
        ret.append('B')
    else:
        ret.append('C')


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

s = []

for i in range(n):
    tmp = input()
    if tmp == 'AB':
        val = 0
    elif tmp == 'BC':
        val = 1
    else:
        val = 2
    s.append(val)

ret = []
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 1:
            t[item] = 0
            t[(item+1) % 3] = 1
            put(ret, (item+1) % 3)
        elif t[(item+1) % 3] == 1:
            t[item] = 1
            t[(item+1) % 3] = 0
            put(ret, item)
        else:
            print('No')
            exit()
elif a + b + c == 2:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put((item+1) % 3)
        elif t[item] < t[(item+1) % 3]:
            t[item] += 1
            t[(item+1) % 3] -= 0
            put(item)
        elif t[item] == 0:
            print('No')
            exit()
        elif t[item] == 2:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put((item+1) % 3)
        else:
            if len(s) > i + 1:
                if s[i+1] == (item + 1) % 3:
                    t[item] -= 1
                    t[(item+1) % 3] += 1
                    put(item+1)
                else:
                    t[item] += 1
                    t[(item+1) % 3] -= 0
                    put(item)
            else:
                put(item)

else:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, (item+1) % 3)
        else:
            t[item] += 1
            t[(item+1) % 3] -= 0
            put(ret, item)

print('Yes')
for item in ret:
    print(item)

Submission status: Runtime Error

Solution code:
def put(ret, val):
    if val == 0:
        ret.append('A')
    elif val == 1:
        ret.append('B')
    else:
        ret.append('C')


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

s = []

for i in range(n):
    tmp = input()
    if tmp == 'AB':
        val = 0
    elif tmp == 'BC':
        val = 1
    else:
        val = 2
    s.append(val)

ret = []
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 1:
            t[item] = 0
            t[(item+1) % 3] = 1
            put(ret, (item+1) % 3)
        elif t[(item+1) % 3] == 1:
            t[item] = 1
            t[(item+1) % 3] = 0
            put(ret, item)
        else:
            print('No')
            exit()
elif a + b + c == 2:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put((item+1) % 3)
        elif t[item] < t[(item+1) % 3]:
            t[item] += 1
            t[(item+1) % 3] -= 0
            put(item)
        elif t[item] == 0:
            print('No')
            exit()
        elif t[item] == 2:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put((item+1) % 3)
        else:
            if len(s) > i + 1:
                if s[i+1] == (item + 1) % 3:
                    t[item] -= 1
                    t[(item+1) % 3] += 1
                    put(item+1)
                else:
                    t[item] += 1
                    t[(item+1) % 3] -= 0
                    put(item)
            else:
                put(item)

else:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, (item+1) % 3)
        else:
            t[item] += 1
            t[(item+1) % 3] -= 0
            put(ret, item)

print('Yes')
for item in ret:
    print(item)

Submission status: Runtime Error

Solution code:
def put(ret, val):
    if val == 0:
        ret.append('A')
    elif val == 1:
        ret.append('B')
    else:
        ret.append('C')


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

s = []

for i in range(n):
    tmp = input()
    if tmp == 'AB':
        val = 0
    elif tmp == 'BC':
        val = 1
    else:
        val = 2
    s.append(val)

ret = []
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 1:
            t[item] = 0
            t[(item+1) % 3] = 1
            put(ret, item+1)
        elif t[(item+1) % 3] == 1:
            t[item] = 1
            t[(item+1) % 3] = 0
            put(ret, item)
        else:
            print('No')
            exit()
elif a + b + c == 2:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(item+1)
        elif t[item] < t[(item+1) % 3]:
            t[item] += 1
            t[(item+1) % 3] -= 0
            put(item)
        elif t[item] == 0:
            print('No')
            exit()
        elif t[item] == 2:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(item+1)
        else:
            if len(s) > i + 1:
                if s[i+1] == (item + 1) % 3:
                    t[item] -= 1
                    t[(item+1) % 3] += 1
                    put(item+1)
                else:
                    t[item] += 1
                    t[(item+1) % 3] -= 0
                    put(item)
            else:
                put(item)

else:
    t = []
    t.append(a)
    t.append(b)
    t.append(c)
    for i, item in enumerate(s):
        if t[item] == 0 and t[(item+1) % 3] == 0:
            print('No')
            exit()
        if t[item] > t[(item+1) % 3]:
            t[item] -= 1
            t[(item+1) % 3] += 1
            put(ret, item+1)
        else:
            t[item] += 1
            t[(item+1) % 3] -= 0
            put(ret, item)

print('Yes')
for item in ret:
    print(item)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

N,A,B,C = map(int,input().split())
S = [ input() for _ in range(N) ]
res = [None] * N

def dfs(i, a, b, c):
  if a < 0 or b < 0 or c < 0:
    return
  elif i == N:
    print("Yes")
    for r in res: print(r)
    exit()
  else:
    if S[i] == "AB":
      res[i] = "A"
      dfs(i+1, a+1, b-1, c)
      res[i] = "B"
      dfs(i+1, a-1, b+1, c)
    elif S[i] == "AC":
      res[i] = "A"
      dfs(i+1, a+1, b, c-1)
      res[i] = "C"
      dfs(i+1, a-1, b, c+1)
    else:
      res[i] = "B"
      dfs(i+1, a, b+1, c-1)
      res[i] = "C"
      dfs(i+1, a, b-1, c+1)

dfs(0, A,B,C)
print("No")