Problem p02692 (python)

Fixed submission:
#!/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__':
    main()

Buggy submission (verdict: Runtime Error):
#!/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()

Buggy submission (verdict: Runtime Error):
#!/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()

Buggy submission (verdict: Runtime Error):
#!/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()

Buggy submission (verdict: Runtime Error):
#!/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()

Buggy submission (verdict: Runtime Error):
#!/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()