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:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def check(s):
    if dict_x[s[0]] == 0 and dict_x[s[1]] == 0:
        return "NOT"
    elif  dict_x[s[0]] > 0 and dict_x[s[1]] == 0:
        return s[1]
    elif  dict_x[s[0]] == 0 and dict_x[s[1]] > 0:
        return s[0]
    elif dict_x[s[0]] == 1 and dict_x[s[1]] == 1:
        return "EVEN"
    else:
        if dict_x[s[0]] > dict_x[s[1]]:
            return s[1]
        else:
            return s[0] 
def update(s, mozi):
    if s[1] == mozi:
        dict_x[s[0]] -= 1
        dict_x[s[1]] += 1
    else:
        dict_x[s[0]] += 1
        dict_x[s[1]] -= 1

n, a, b, c = map(int, input().split())
dict_x = {"A": a, "B": b, "C": c}
s_list = []
ans_list = []
flag = 1
for i in range(0, n):
    s = input()
    s_list.append(s)
if a + b + c == 0:
    flag = 0
elif a + b + c == 1:
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        else:
            ans_list.append(x)
            update(s, x)
elif a + b + c == 2:
    i = 0
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        elif x == "EVEN":
            if i == len(s_list) - 1:
                ans_list.append(s[0])
                update(s, x)
            elif s == s[i + 1]:
                ans_list.append(s[0])
                update(s, x)
            else:
                if s[0] in s[i + 1]:
                    ans_list.append(s[0])
                    update(s, s[0])
                else:
                    ans_list.append(s[1])
                    update(s, s[1])
        else:
            ans_list.append(x)
            update(s, x)
        i += 1
else:
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        elif x == "EVEN":
            ans_list.append(s[0])
            update(s, s[0])
        else:
            ans_list.append(x)
            update(s, x)

if flag == 1:
    print("Yes")
    for ans in ans_list:
        print(ans)
else:
    print("No")

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def check(s):
    if dict_x[s[0]] == 0 and dict_x[s[1]] == 0:
        return "NOT"
    elif  dict_x[s[0]] > 0 and dict_x[s[1]] == 0:
        return s[1]
    elif  dict_x[s[0]] == 0 and dict_x[s[1]] > 0:
        return s[0]
    elif dict_x[s[0]] == 1 and dict_x[s[1]] == 1:
        return "EVEN"
    else:
        if dict_x[s[0]] > dict_x[s[1]]:
            return s[1]
        else:
            return s[0] 
def update(s, mozi):
    if s[1] == mozi:
        dict_x[s[0]] -= 1
        dict_x[s[1]] += 1
    else:
        dict_x[s[0]] += 1
        dict_x[s[1]] -= 1

n, a, b, c = map(int, input().split())
dict_x = {"A": a, "B": b, "C": c}
s_list = []
ans_list = []
flag = 1
for i in range(0, n):
    s = input()
    s_list.append(s)
if a + b + c == 0:
    flag = 0
elif a + b + c == 1:
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        else:
            ans_list.append(x)
            update(s, x)
elif a + b + c == 2:
    i = 0
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        elif x == "EVEN":
            if i == len(s_list) - 1:
                ans_list.append(s[0])
                update(s, x)
            elif s == s[i + 1]:
                ans_list.append(s[0])
                update(s, x)
            else:
                if s[0] in s[i + 1]:
                    ans_list.append(s[0])
                    update(s, s[0])
                else:
                    ans_list.append(s[1])
                    update(s, s[1])
        else:
            ans_list.append(x)
            update(s, x)
        i += 1
else:
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        else:
            ans_list.append(x)
            update(s, x)

if flag == 1:
    print("Yes")
    for ans in ans_list:
        print(ans)
else:
    print("No")

Submission status: Runtime Error

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
def check(s):
    if dict_x[s[0]] == 0 and dict_x[s[1]] == 0:
        return "NOT"
    elif  dict_x[s[0]] > 0 and dict_x[s[1]] == 0:
        return s[1]
    elif  dict_x[s[0]] == 0 and dict_x[s[1]] > 0:
        return s[0]
    elif dict_x[s[0]] == 1 and dict_x[s[1]] == 1:
        return "EVEN"
    else:
        if dict_x[s[0]] > dict_x[s[1]]:
            return s[1]
        else:
            return s[0] 
def update(s, mozi):
    if s[1] == mozi:
        dict_x[s[0]] -= 1
        dict_x[s[1]] += 1
    else:
        dict_x[s[0]] += 1
        dict_x[s[1]] -= 1

n, a, b, c = map(int, input().split())
dict_x = {"A": a, "B": b, "C": c}
s_list = []
ans_list = []
flag = 1
for i in range(0, n):
    s = input()
    s_list.append(s)
if a + b + c == 0:
    flag = 0
elif a + b + c == 1:
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        else:
            ans_list.append(x)
            update(s, x)
elif a + b + c == 2:
    i = 0
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        elif x == "EVEN":
            if i == len(s_list):
                ans_list.append(s[0])
                update(s, x)
            elif s == s[i + 1]:
                ans_list.append(s[0])
                update(s, x)
            else:
                if s[0] in s[i + 1]:
                    ans_list.append(s[0])
                    update(s, s[0])
                else:
                    ans_list.append(s[1])
                    update(s, s[1])
        else:
            ans_list.append(x)
            update(s, x)
        i += 1
else:
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        else:
            ans_list.append(x)
            update(s, x)

if flag == 1:
    print("Yes")
    for ans in ans_list:
        print(ans)
else:
    print("No")

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())
s=[0]*n
for i in range(n):
    s[i]=input()
l=[0]*n
for i in range(n):
    if s[i]=="AB":
        if a==b==0:
            print("No")
            exit()
        if a+b+c==2:
            if a==b==1:
                if s[i+1]=="AC":
                    a+=1
                    b-=1
                    l[i]="A"
                    continue
                elif s[i+1]=="BC":
                    a-=1
                    b+=1
                    l[i]="B"
                    continue
        if a>=b:
            a-=1
            b+=1
            l[i]="B"
        else:
            a+=1
            b-=1
            l[i]="A"
    if s[i]=="BC":
        if c==b==0:
            print("No")
            exit()
        if a+b+c==2:
            if b==c==1:
                if s[i+1]=="AB":
                    c-=1
                    b+=1
                    l[i]="B"
                    continue
                elif s[i+1]=="AC":
                    b-=1
                    c+=1
                    l[i]="C"
                    continue
        if b>=c:
            b-=1
            c+=1
            l[i]="C"
        else:
            b+=1
            c-=1
            l[i]="B"
    if s[i]=="AC":
        if a==c==0:
            print("No")
            exit()
        if a+b+c==2:
            if a==c==1:
                if s[i+1]=="AB":
                    c-=1
                    a+=1
                    l[i]="A"
                    continue
                elif s[i+1]=="BC":
                    a-=1
                    c+=1
                    l[i]="C"
                    continue
        if a>=c:
            a-=1
            c+=1
            l[i]="C"
        else:
            a+=1
            c-=1
            l[i]="A"
if l[-1]!=0:
    print("Yes")
    for i in range(n):
        print(l[i])

Submission status: Runtime Error

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

si = [input() for i in range(n)]

if a == 0 and b == 0 and c== 0:
    print("No")
    exit()

s1 = 0
s2 = 0

abc = [a,b,c]
moji = ['A','B','C']

def checkmoji(s):
    if si[i] == "AB":
        s1 = 0
        s2 = 1
    elif si[i] == 'AC':
        s1 = 0
        s2 = 2
    elif si[i] == 'BC':
        s1 = 1
        s2 = 2
    return s1,s2

ans = []

for i in range(n):
    s1,s2 = checkmoji(si[i])
    if abc[s1] <= 0 and abc[s2] <= 0:
        print('No')
        exit()
    if abc[s2] == 0:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(moji[s2])
    elif abc[s1] == 0:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(moji[s1])
    else:
        """
        if i == n-1:
            ans.append(moji[s1])
            break
        """
        s1_a,s2_a = checkmoji(si[i+1])
        if s1 == s1_a or s1 == s2_a:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(moji[s2])
        else:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(moji[s1])
print('Yes')
for i in range(len(ans)):
    print(ans[i])

Submission status: Runtime Error

Solution code:
# import numpy as np
# import queue
# import heapq


def solve(writer):
    N, A, B, C = map(int, input().split())
    variables = {
        'A': A,
        'B': B,
        'C': C
    }

    queries = (list(input()) for _ in range(N))

    buffer = list()
    b = ''

    for i in range(N):
        if not buffer:
            t1, t2 = next(queries)
        else:
            b = buffer.pop()
            if b == 'pass':
                continue
            else:
                t1, t2 = list(b)

        v1 = variables[t1]
        v2 = variables[t2]

        if v1 == v2 == 0:
            return False
        elif v1 == v2 == 1:
            if i == N-1:
                return True

            nt1, nt2 = next(queries)
            if len({[t1, t2, nt1, nt2]}) == 2:
                buffer.append('pass')
                writer.append(t1)
                writer.append(t2)
                continue

            if t1 in {nt1, nt2}:
                variables[t1] += 1
                variables[t2] -= 1
                writer.append(t1)
            else:
                variables[t1] -= 1
                variables[t2] += 1
                writer.append(t2)

            buffer.append(''.join([nt1, nt2]))

        elif v1 > v2:
            variables[t1] -= 1
            variables[t2] += 1
            writer.append(t2)
        elif v2 > v1:
            variables[t1] += 1
            variables[t2] -= 1
            writer.append(t1)
        else:
            variables[t1] += 1
            variables[t2] -= 1
            writer.append(t1)

    return True


def main():
    writer = list()
    ok = solve(writer)
    if ok:
        print('Yes')
        for w in writer:
            print(w)
    else:
        print('No')


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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


S, stack = [], []
N = 0


def main() -> None:
    global N
    N, A, B, C = rmi()
    for n in range(N):
        S.append(r())
    enable = dfs(stack, 0, A, B, C)
    w('Yes' if enable else 'No')
    if enable:
        wm(*stack, delim='\n')


def dfs(stack, index, a, b, c) -> bool:
    if N <= index:
        return True
    inst = S[index]
    if inst == 'AB':
        if a <= 0 and b <= 0:
            return False
        if 0 < a:
            stack.append('B')
            result = dfs(stack, index + 1, a - 1, b + 1, c)
            if result:
                return True
            else:
                stack.pop()
        if 0 < b:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b - 1, c)
            if result:
                return True
            else:
                stack.pop()
    elif inst == 'AC':
        if a <= 0 and c <= 0:
            return False
        if 0 < a:
            stack.append('C')
            result = dfs(stack, index + 1, a - 1, b, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if 0 < c:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b, c - 1)
            if result:
                return True
            else:
                stack.pop()
    else:
        if b <= 0 and c <= 0:
            return False
        if 0 < b:
            stack.append('C')
            result = dfs(stack, index + 1, a, b - 1, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if 0 < c:
            stack.append('B')
            result = dfs(stack, index + 1, a, b + 1, c - 1)
            if result:
                return True
            else:
                stack.pop()
    return False


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10**10)
    main()

Submission status: Runtime Error

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

import sys

sys.setrecursionlimit(10**8)


S, stack = [], []
N = 0


def main() -> None:
    global N
    N, A, B, C = rmi()
    for n in range(N):
        S.append(r())
    enable = dfs(stack, 0, A, B, C)
    w('Yes' if enable else 'No')
    if enable:
        wm(*stack, delim='\n')


def dfs(stack, index, a, b, c) -> bool:
    if N <= index:
        return True
    inst = S[index]
    if inst == 'AB':
        if a <= 0 and b <= 0:
            return False
        if 0 < a:
            stack.append('B')
            result = dfs(stack, index + 1, a - 1, b + 1, c)
            if result:
                return True
            else:
                stack.pop()
        if 0 < b:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b - 1, c)
            if result:
                return True
            else:
                stack.pop()
    elif inst == 'AC':
        if a <= 0 and c <= 0:
            return False
        if 0 < a:
            stack.append('C')
            result = dfs(stack, index + 1, a - 1, b, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if 0 < c:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b, c - 1)
            if result:
                return True
            else:
                stack.pop()
    else:
        if b <= 0 and c <= 0:
            return False
        if 0 < b:
            stack.append('C')
            result = dfs(stack, index + 1, a, b - 1, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if 0 < c:
            stack.append('B')
            result = dfs(stack, index + 1, a, b + 1, c - 1)
            if result:
                return True
            else:
                stack.pop()
    return False


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> tuple:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    import sys
    sys.setrecursionlimit(10**10)
    main()

Submission status: Runtime Error

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


S, stack = [], []
N = 0


def main() -> None:
    global N
    N, A, B, C = rmi()
    for n in range(N):
        S.append(r())
    enable = dfs(stack, 0, A, B, C)
    w('Yes' if enable else 'No')
    if enable:
        wm(*stack, delim='\n')


def dfs(stack, index, a, b, c) -> bool:
    if index == N:
        return True
    inst = S[index]
    if inst == 'AB':
        if a <= 0 and b <= 0:
            return False
        if a > 0:
            stack.append('B')
            result = dfs(stack, index + 1, a - 1, b + 1, c)
            if result:
                return True
            else:
                stack.pop()
        if b > 0:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b - 1, c)
            if result:
                return True
            else:
                stack.pop()
    elif inst == 'AC':
        if a == c == 0:
            return False
        if a > 0:
            stack.append('C')
            result = dfs(stack, index + 1, a - 1, b, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if c > 0:
            stack.append('A')
            result = dfs(stack, index + 1, a + 1, b, c - 1)
            if result:
                return True
            else:
                stack.pop()
    else:
        if b == c == 0:
            return False
        if b > 0:
            stack.append('C')
            result = dfs(stack, index + 1, a, b - 1, c + 1)
            if result:
                return True
            else:
                stack.pop()
        if c > 0:
            stack.append('B')
            result = dfs(stack, index + 1, a, b + 1, c - 1)
            if result:
                return True
            else:
                stack.pop()
    return False


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> int:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(*data, delim: str = ' ') -> None:
    print(delim.join(map(str, data)))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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


def calc(x, y):
    if x - 1 >= 0:
        return (x - 1, y)
    elif y - 1 >= 0:
        return (x, y - 1)


answers = []
for i in range(n):
    s = list(map(str(input().input())))
    old_a = a
    old_b = b
    old_c = c

    if s == 'AB':
        a, b = calc(a, b)
    elif s == 'AC':
        a, c = calc(a, c)
    elif s == 'BC':
        b, c = calc(b, c)

    if a > old_a:
        answers.append('A')
    elif b > old_b:
        answers.append('B')
    elif c > old_c:
        answers.append('C')

    if a >= 0 and b >= 0 and c >= 0:
        print('No')
        quit()


print('Yes')
for ans in answers:
    print(ans)

Submission status: Runtime Error

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

from typing import Iterable

S = []
stack = []


def main() -> None:
    N, A, B, C = rmi()
    for n in range(N):
        S.append(r())
    enable = trace(stack, 0, [A, B, C], N)
    w('Yes' if enable else 'No')
    if enable:
        w('\n'.join(stack))


def trace(stack: list, index: int, ABC: list, N: int):
    if index == N:
        return True
    a, b, c = ABC
    inst = S[index]
    if inst == 'AB':
        if a == b == 0:
            return False
        if a > 0:
            stack.append('B')
            result = trace(stack, index + 1, [a - 1, b + 1, c], N)
            if result:
                return True
            else:
                stack.pop()
        if b > 0:
            stack.append('A')
            result = trace(stack, index + 1, [a + 1, b - 1, c], N)
            if result:
                return True
            else:
                stack.pop()
    elif inst == 'AC':
        if a == c == 0:
            return False
        if a > 0:
            stack.append('C')
            result = trace(stack, index + 1, [a - 1, b, c + 1], N)
            if result:
                return True
            else:
                stack.pop()
        if c > 0:
            stack.append('A')
            result = trace(stack, index + 1, [a + 1, b, c - 1], N)
            if result:
                return True
            else:
                stack.pop()
    else:
        if b == c == 0:
            return False
        if b > 0:
            stack.append('C')
            result = trace(stack, index + 1, [a, b - 1, c + 1], N)
            if result:
                return True
            else:
                stack.pop()
        if c > 0:
            stack.append('B')
            result = trace(stack, index + 1, [a, b + 1, c - 1], N)
            if result:
                return True
            else:
                stack.pop()


def r() -> str:
    return input().strip()


def ri() -> int:
    return int(r())


def rmi(delim: str = ' ') -> int:
    return tuple(map(int, input().split(delim)))


def w(data) -> None:
    print(data)


def wm(data: Iterable) -> None:
    print(' '.join(list(map(str, data))))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N, *ABC = map(int, input().split())
s = [input() for _ in range(N)]

yes_flag = True
ans_list = []

if sum(ABC) == 0:
    yes_flag = False

for i in range(N):
    if not yes_flag:
        break
    u = ord(s[i][0]) - 65
    v = ord(s[i][1]) - 65
    if ABC[u] + ABC[v] == 0:
        yes_flag = False
    elif ABC[u] + ABC[v] == 1:
        if ABC[u] == 1:
            ans_list += [s[i][0]]
            ABC[u] -= 1
            ABC[v] += 1
        else:
            ans_list += [s[i][1]]
            ABC[v] -= 1
            ABC[u] += 1
    elif ABC[u] + ABC[v] == 2:
        # 両方1の場合だけケアが必要
        if ABC[u] * ABC[v] == 1:
            if i == N-1:
                ans_list += [s[i][0]]
            else:
                if ABC[u] in s[i+1]:
                    ans_list += [s[i][0]]
                    ABC[u] -= 1
                    ABC[v] += 1
                else:
                    ans_list += [s[i][1]]
                    ABC[v] -= 1
                    ABC[u] += 1
        else:
            if ABC[u] > 1:
                ans_list += [s[i][0]]
                ABC[u] -= 1
                ABC[v] += 1
            else:
                ans_list += [s[i][1]]
                ABC[v] -= 1
                ABC[u] += 1
    else:
        if ABC[u] > 1:
            ans_list += [s[i][0]]
            ABC[u] -= 1
            ABC[v] += 1
        else:
            ans_list += [s[i][1]]
            ABC[v] -= 1
            ABC[u] += 1

if yes_flag:
    print("Yes")
    print(*ans_list)

else:
    print("No")

Submission status: Runtime Error

Solution code:
# input
N, A, B, C = map(int, input().split())
S = [input() for _ in range(N)]
V = {"A":A, "B":B, "C":C}

# process
a, b, c = A, B, C
op = ""
for i, s in enumerate(S):
    s0 = s[0]
    s1 = s[1]
    if V[s0] + V[s1] == 0:
        break
    
    x, y = s0, s1
    if V[s1] == 0 or (i < N-1 and s1 in S[i+1]):
        x, y = s1, s0

    V[x] += 1
    V[y] -= 1
    op += x

    print("{} {} {}".format(s, V, op))

# output
if len(op) != N:
    print("No")
else:
    print("Yes")
    [print(x) for x in op]

Submission status: Runtime Error

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

if a+b+c==0:
  del ans[:]
  ans+=["No"]
  
if a+b+c==1:
  for j in range(n):
    
    if s[j]=="AB":
      if c==1:
        del ans[:]
        ans+=["No"]
        break        
      if a==1:
        a-=1
        b+=1
        ans+="B"
      if b==1:
        a+=1
        b-=1
        ans+="A"

    if s[j]=="AC":
      if b==1:
        del ans[:]
        ans+=["No"]
        break       
      if a==1:
        a-=1
        c+=1
        ans+="C"
      if c==1:
        a+=1
        c-=1
        ans+="A" 

    if s[j]=="BC":
      if a==1:
        del ans[:]
        ans+=["No"]
        break
      if b==1:
        b-=1
        c+=1
        ans+="C"
      if c==1:
        b+=1
        c-=1
        ans+="B"  

if a+b+c==2:
  for j in range(n):
    
    if s[j]=="AB":
      if a==2:
        a-=1
        b+=1
        ans+="B"
      if b==2:
        a+=1
        b-=1
        ans+="A" 
      if c==2:
        del ans[:]
        ans+=["No"]
        break     
      if a==1 and b==1:
        if s[j+1]=="AB" or "AC":
          a+=1
          b-=1
          ans+="A"
        if s[j+1]=="BC":
          a-=1
          b+=1
          ans+="B"
      if a==1 and c==1:
        a-=1
        b+=1
        ans+="B"
      if b==1 and c==1:
        a+=1
        b-=1
        ans+="A"
    
    if s[j]=="AC":
      if a==2:
        a-=1
        c+=1
        ans+="C"
      if c==2:
        a+=1
        c-=1
        ans+="A" 
      if b==2:
        del ans[:]
        ans+="No"
        break     
      if a==1 and c==1:
        if s[j+1]=="AB" or "AC":
          a+=1
          c-=1
          ans+="A"
        if s[j+1]=="BC":
          a-=1
          c+=1
          ans+="C"
      if a==1 and b==1:
        a-=1
        c+=1
        ans+="C"
      if b==1 and c==1:
        a+=1
        c-=1
        ans+="A"

    if s[j]=="BC":
      if c==2:
        c-=1
        b+=1
        ans+="B"
      if b==2:
        c+=1
        b-=1
        ans+="C" 
      if a==2:
        del ans[:]
        ans+="No"
        break     
      if c==1 and b==1:
        if s[j+1]=="AB" or "BC":
          b+=1
          c-=1
          ans+="B"
        if s[j+1]=="AC":
          b-=1
          c+=1
          ans+="C"
      if a==1 and c==1:
        c-=1
        b+=1
        ans+="B"
      if b==1 and a==1:
        c+=1
        b-=1
        ans+="C"        
  print(*ans,sep="\n")

if a+b+c>=3:
  if s[0]=="AB" and a+b==0 or s[0]=="AC" and a+c==0 or s[0]=="BC" and b+c==0:
    del ans[:]
    ans+=["No"]
  
  else:
    for j in range(n):
      if s[j]=="AB":
        if a>b:
          a-=1
          b+=1
          ans+="B"
        elif a<=b:
          a+=1
          b-=1
          ans+="A"
        
      if s[j]=="AC":
        if a>c:
          a-=1
          c+=1
          ans+="C"
        elif a<=c:
          a+=1
          c-=1
          ans+="A"     

      if s[j]=="BC":
        if b>c:
          b-=1
          c+=1
          ans+="C"
        elif b<=c:
          b+=1
          c-=1
          ans+="B"
for an in ans:
  print(an)

Submission status: Runtime Error

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

if a+b+c==0:
  del ans[:]
  ans+=["No"]
  
if a+b+c==1:
  for j in range(n):
    
    if s[j]=="AB":
      if c==1:
        del ans[:]
        ans+=["No"]
        break        
      if a==1:
        a-=1
        b+=1
        ans+="B"
      if b==1:
        a+=1
        b-=1
        ans+="A"

    if s[j]=="AC":
      if b==1:
        del ans[:]
        ans+=["No"]
        break       
      if a==1:
        a-=1
        c+=1
        ans+="C"
      if c==1:
        a+=1
        c-=1
        ans+="A" 

    if s[j]=="BC":
      if a==1:
        del ans[:]
        ans+=["No"]
        break
      if b==1:
        b-=1
        c+=1
        ans+="C"
      if c==1:
        b+=1
        c-=1
        ans+="B"  

if a+b+c==2:
  for j in range(n):
    
    if s[j]=="AB":
      if a==2:
        a-=1
        b+=1
        ans+="B"
      if b==2:
        a+=1
        b-=1
        ans+="A" 
      if c==2:
        del ans[:]
        ans+=["No"]
        break     
      if a==1 and b==1:
        if s[j+1]=="AB" or "AC":
          a+=1
          b-=1
          ans+="A"
        if s[j+1]=="BC":
          a-=1
          b+=1
          ans+="B"
      if a==1 and c==1:
        a-=1
        b+=1
        ans+="B"
      if b==1 and c==1:
        a+=1
        b-=1
        ans+="A"
    
    if s[j]=="AC":
      if a==2:
        a-=1
        c+=1
        ans+="C"
      if c==2:
        a+=1
        c-=1
        ans+="A" 
      if b==2:
        del ans[:]
        ans+="No"
        break     
      if a==1 and c==1:
        if s[j+1]=="AB" or "AC":
          a+=1
          c-=1
          ans+="A"
        if s[j+1]=="BC":
          a-=1
          c+=1
          ans+="C"
      if a==1 and b==1:
        a-=1
        c+=1
        ans+="C"
      if b==1 and c==1:
        a+=1
        c-=1
        ans+="A"

    if s[j]=="BC":
      if c==2:
        c-=1
        b+=1
        ans+="B"
      if b==2:
        c+=1
        b-=1
        ans+="C" 
      if a==2:
        del ans[:]
        ans+="No"
        break     
      if c==1 and b==1:
        if s[j+1]=="AB" or "BC":
          b+=1
          c-=1
          ans+="B"
        if s[j+1]=="AC":
          b-=1
          c+=1
          ans+="C"
      if a==1 and c==1:
        c-=1
        b+=1
        ans+="B"
      if b==1 and a==1:
        c+=1
        b-=1
        ans+="C"        
  print(*ans,sep="\n")

if a+b+c>=3:
  if s[0]=="AB" and a+b==0 or s[0]=="AC" and a+c==0 or s[0]=="BC" and b+c==0:
    del ans[:]
    ans+="No"
  
  else:
    for j in range(n):
      if s[j]=="AB":
        if a>b:
          a-=1
          b+=1
          ans+="B"
        elif a<=b:
          a+=1
          b-=1
          ans+="A"
        
      if s[j]=="AC":
        if a>c:
          a-=1
          c+=1
          ans+="C"
        elif a<=c:
          a+=1
          c-=1
          ans+="A"     

      if s[j]=="BC":
        if b>c:
          b-=1
          c+=1
          ans+="C"
        elif b<=c:
          b+=1
          c-=1
          ans+="B"
for an in ans:
  print(an)