Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

Solution code:
from collections import deque
from itertools import product
import sys
import math
import numpy as np
import bisect

sys.setrecursionlimit(200000)
input = sys.stdin.readline


def read():
    N, A, B, C = map(int, input().strip().split())
    S = []
    for i in range(N):
        s = input().strip()
        S.append(s)
    return N, A, B, C, S


def choose_at_corner_state(A, B, C, s):
    """次の一手を返す"""
    if A == 0 and C == 0:
        if s == "AC":
            return ("X", -1, -1, -1)
        elif s == "BC":
            return ("B", A, B-1, C+1)
        elif s == "AB":
            return ("A", A+1, B-1, C)
    elif A == 0 and B == 0:
        if s == "AC":
            return ("A", A+1, B, C-1)
        elif s == "BC":
            return ("B", A, B+1, C-1)
        elif s == "AB":
            return ("X", -1, -1, -1)
    elif B == 0 and C == 0:
        if s == "AC":
            return ("A", A-1, B, C+1)
        elif s == "BC":
            return ("X", -1, -1, -1)
        elif s == "AB":
            return ("B", A-1, B+1, C)
    raise ValueError()


def is_error_state(A, B, C):
    if A < 0 or B < 0 or C < 0:
        return True
    return False


def is_corner_state(A, B, C):
    if A == 0 and B == 0:
        return True
    elif A == 0 and C == 0:
        return True
    elif B == 0 and C == 0:
        return True
    return False


def choose_at_normal_state(A, B, C, s0, s1):
    """先読みして、次の一手を返す"""
    if s0 == "BC":
        op0, a0, b0, c0 = ("C", A, B-1, C+1)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
        op0, a0, b0, c0 = ("B", A, B+1, C-1)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
    elif s0 == "AC":
        op0, a0, b0, c0 = ("C", A-1, B, C+1)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
        op0, a0, b0, c0 = ("A", A+1, B, C-1)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
    elif s0 == "AB":
        op0, a0, b0, c0 = ("B", A-1, B+1, C)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
        op0, a0, b0, c0 = ("A", A+1, B-1, C)
        if is_error_state(a0, b0, c0):
            pass
        elif is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
    return ("X", -1, -1, -1)


def trial(N, A, B, C, S):
    
    if A + B + C == 0:
        return False, []
    
    ans, choices = True, []

    S = S + [S[-1]]  # S[-1] is terminal state

    if A + B + C == 1:
        for i in range(N):
            x, A, B, C = choose_at_corner_state(A, B, C, S[i])
            if x == "X":
                return False, []
            else:
                choices.append(x)
    else:
        for i in range(N):
            if is_corner_state(A, B, C):
                x, A, B, C = choose_at_corner_state(A, B, C, S[i])
            else:
                x, A, B, C = choose_at_normal_state(A, B, C, S[i], S[i+1])
            if x == "X":
                return False, []
            else:
                choices.append(x)
    return ans, choices


def solve(N, A, B, C, S):
    ans, choices = trial(N, A, B, C, S)
    if ans:
        print("Yes")
        for c in choices:
            print(c)
    else:
        print("No")


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("{}".format(outputs))

Submission status: Wrong Answer

Solution code:
from collections import deque
from itertools import product
import sys
import math
import numpy as np
import bisect

sys.setrecursionlimit(200000)
input = sys.stdin.readline


def read():
    N, A, B, C = map(int, input().strip().split())
    S = []
    for i in range(N):
        s = input().strip()
        S.append(s)
    return N, A, B, C, S


def choose_at_corner_state(A, B, C, s):
    """次の一手を返す"""
    if A == 0 and C == 0:
        if s == "AC":
            return ("X", -1, -1, -1)
        elif s == "BC":
            return ("B", A, B-1, C+1)
        elif s == "AB":
            return ("A", A+1, B-1, C)
    elif A == 0 and B == 0:
        if s == "AC":
            return ("A", A+1, B, C-1)
        elif s == "BC":
            return ("B", A, B+1, C-1)
        elif s == "AB":
            return ("X", -1, -1, -1)
    elif B == 0 and C == 0:
        if s == "AC":
            return ("A", A-1, B, C+1)
        elif s == "BC":
            return ("X", -1, -1, -1)
        elif s == "AB":
            return ("B", A-1, B+1, C)
    raise ValueError()


def is_corner_state(A, B, C):
    if A == 0 and B == 0:
        return True
    elif A == 0 and C == 0:
        return True
    elif B == 0 and C == 0:
        return True
    return False


def choose_at_normal_state(A, B, C, s0, s1):
    """先読みして、次の一手を返す"""
    if s0 == "BC":
        op0, a0, b0, c0 = ("C", A, B-1, C+1)
        if is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
        op0, a0, b0, c0 = ("B", A, B+1, C-1)
        if is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
    elif s0 == "AC":
        op0, a0, b0, c0 = ("C", A-1, B, C+1)
        if is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
        op0, a0, b0, c0 = ("A", A+1, B, C-1)
        if is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
    elif s0 == "AB":
        op0, a0, b0, c0 = ("B", A-1, B+1, C)
        if is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
        op0, a0, b0, c0 = ("A", A+1, B-1, C)
        if is_corner_state(a0, b0, c0):
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        else:
            return (op0, a0, b0, c0)
    return ("X", -1, -1, -1)


def trial(N, A, B, C, S):
    
    if A + B + C == 0:
        return False, []
    
    ans, choices = True, []

    S = S + [S[-1]]  # S[-1] is terminal state

    if A + B + C == 1:
        for i in range(N):
            x, A, B, C = choose_at_corner_state(A, B, C, S[i])
            if x == "X":
                return False, []
            else:
                choices.append(x)
    else:
        for i in range(N):
            if is_corner_state(A, B, C):
                x, A, B, C = choose_at_corner_state(A, B, C, S[i])
            else:
                x, A, B, C = choose_at_normal_state(A, B, C, S[i], S[i+1])
            if x == "X":
                return False, []
            else:
                choices.append(x)
    return ans, choices


def solve(N, A, B, C, S):
    ans, choices = trial(N, A, B, C, S)
    if ans:
        print("Yes")
        for c in choices:
            print(c)
    else:
        print("No")


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("{}".format(outputs))

Submission status: Wrong Answer

Solution code:
from collections import deque
from itertools import product
import sys
import math
import numpy as np
import bisect

sys.setrecursionlimit(200000)
input = sys.stdin.readline


def read():
    N, A, B, C = map(int, input().strip().split())
    S = []
    for i in range(N):
        s = input().strip()
        S.append(s)
    return N, A, B, C, S


def choose_at_corner_state(A, B, C, s):
    """次の一手を返す"""
    if A == 0 and C == 0:
        if s == "AC":
            return ("X", -1, -1, -1)
        elif s == "BC":
            return ("B", A, B-1, C+1)
        elif s == "AB":
            return ("A", A+1, B-1, C)
    elif A == 0 and B == 0:
        if s == "AC":
            return ("A", A+1, B, C-1)
        elif s == "BC":
            return ("B", A, B+1, C-1)
        elif s == "AB":
            return ("X", -1, -1, -1)
    elif B == 0 and C == 0:
        if s == "AC":
            return ("A", A-1, B, C+1)
        elif s == "BC":
            return ("X", -1, -1, -1)
        elif s == "AB":
            return ("B", A-1, B+1, C)
    raise ValueError()


def is_corner_state(A, B, C):
    if A == 0 and B == 0:
        return True
    elif A == 0 and C == 0:
        return True
    elif B == 0 and C == 0:
        return True
    return False


def choose_at_normal_state(A, B, C, s0, s1):
    """先読みして、次の一手を返す"""
    if s0 == "BC":
        if is_corner_state(A, B-1, C+1):
            op0, a0, b0, c0 = ("C", A, B-1, C+1)
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        elif is_corner_state(A, B+1, C-1):
            op0, a0, b0, c0 = ("B", A, B+1, C-1)
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        if A + B + C == 2:
            return ("X", -1, -1, -1)
        else:
            # どちらでも良いので、適当な手を返す
            op0, a0, b0, c0 = ("B", A, B+1, C-1)
            return (op0, a0, b0, c0)
    elif s0 == "AC":
        if is_corner_state(A-1, B, C+1):
            op0, a0, b0, c0 = ("C", A-1, B, C+1)
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        if is_corner_state(A+1, B, C-1):
            op0, a0, b0, c0 = ("A", A+1, B, C-1)
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        if A + B + C == 2:
            return ("X", -1, -1, -1)
        else:
            # どちらでも良いので、適当な手を返す
            op0, a0, b0, c0 = ("A", A+1, B, C-1)
            return (op0, a0, b0, c0)
    elif s0 == "AB":
        if is_corner_state(A-1, B+1, C):
            op0, a0, b0, c0 = ("B", A-1, B+1, C)
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        if is_corner_state(A+1, B-1, C):
            op0, a0, b0, c0 = ("A", A+1, B-1, C)
            op1, a1, b1, c1 = choose_at_corner_state(a0, b0, c0, s1)
            if op1 != "X":
                return (op0, a0, b0, c0)
        if A + B + C == 2:
            return ("X", -1, -1, -1)
        else:
            # どちらでも良いので、適当な手を返す
            op0, a0, b0, c0 = ("A", A+1, B-1, C)
            return (op0, a0, b0, c0)
    raise ValueError()


def trial(N, A, B, C, S):
    
    if A + B + C == 0:
        return False, []
    
    ans, choices = True, []

    S = S + [S[-1]]  # S[-1] is terminal state

    if A + B + C == 1:
        for i in range(N):
            x, A, B, C = choose_at_corner_state(A, B, C, S[i])
            if x == "X":
                return False, []
            else:
                choices.append(x)
    else:
        for i in range(N):
            if is_corner_state(A, B, C):
                x, A, B, C = choose_at_corner_state(A, B, C, S[i])
            else:
                x, A, B, C = choose_at_normal_state(A, B, C, S[i], S[i+1])
            if x == "X":
                return False, []
            else:
                choices.append(x)
    return ans, choices


def solve(N, A, B, C, S):
    ans, choices = trial(N, A, B, C, S)
    if ans:
        print("Yes")
        for c in choices:
            print(c)
    else:
        print("No")


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("{}".format(outputs))

Submission status: Wrong Answer

Solution code:
x = [int(a) for a in input().split()]
N, A, B, C = x
v = [A, B, C]

if sum(v) >= 3:
  decisions = []
  flg = True
  for i in range(N):
    s1, s2 = input()
    i1 = ord(s1)-ord("A")
    i2 = ord(s2)-ord("A")
    if v[i1] == 0 and v[i2] == 0:
      flg = False
      break
    if v[i1] > v[i2]:
      decisions.append(chr(i2+65))
      v[i1] -= 1
      v[i2] += 1
    else:
      decisions.append(chr(i1+65))
      v[i1] += 1
      v[i2] -= 1
  if flg:
    print("Yes")
    print("\n".join(decisions))
  else:
    print("No")
else:
  import sys
  sys.setrecursionlimit(10**6)
  v = tuple(v)
  i1s = []
  i2s = []
  for i in range(N):
    s1, s2 = input()
    i1s.append(ord(s1)-ord("A"))
    i2s.append(ord(s2)-ord("A"))
  f = []
  for i in range(N):
    f.append({})

  def get(i, s):
    if s in f[i]:
      return f[i][s]
    if i == N-1:
      i1 = i1s[i]
      i2 = i2s[i]
      if s[i1] == 0 and s[i2] == 0:
        f[i][s] = False
      else:
        f[i][s] = True
      return f[i][s]
    i1 = i1s[i]
    i2 = i2s[i]
    if s[i1] == 0 and s[i2] == 0:
      f[i][s] = False
      return False
    s1 = list(s)
    s1[i1] -= 1
    s1[i2] += 1
    s1 = tuple(s1)

    s2 = list(s)
    s2[i2] -= 1
    s2[i1] += 1
    s2 = tuple(s2)
    
    f1 = get(i+1, s1) if s1[i1] >= 0 else False
    f2 = get(i+1, s2) if s2[i2] >= 0 else False
    if f1 or f2:
      f[i][s] = True
      return True
    else:
      f[i][s] = False
      return False
  ans = get(0, v)
  if ans:
    print("Yes")
    s = tuple(v)
    for i in range(N):
      #print(s)
      i1 = i1s[i]
      i2 = i2s[i]
      if i == N-1:
        if s[i1] > 0:
          print(chr(i1+65))
        else:
          print(chr(i2+65))
        break
      s1 = list(s)
      s1[i1] -= 1
      s1[i2] += 1
      s1 = tuple(s1)
      s2 = list(s)
      s2[i2] -= 1
      s2[i1] += 1
      s2 = tuple(s2)
      f1 = get(i+1, s1)
      f2 = get(i+1, s2)
      if f1:
        print(chr(i1+65))
        s = s1
      else:
        print(chr(i2+65))
        s = s2
  else:
    print("No")

Submission status: Wrong Answer

Solution code:
x = [int(a) for a in input().split()]
N, A, B, C = x
v = [A, B, C]

if sum(v) >= 0:
  decisions = []
  flg = True
  for i in range(N):
    s1, s2 = input()
    i1 = ord(s1)-ord("A")
    i2 = ord(s2)-ord("A")
    if v[i1] == 0 and v[i2] == 0:
      flg = False
      break
    if v[i1] > v[i2]:
      decisions.append(chr(i2+65))
      v[i1] -= 1
      v[i2] += 1
    else:
      decisions.append(chr(i1+65))
      v[i1] += 1
      v[i2] -= 1
  if flg:
    print("Yes")
    print("\n".join(decisions))
  else:
    print("No")
else:
  v = tuple(v)
  i1s = []
  i2s = []
  for i in range(N):
    s1, s2 = input()
    i1s.append(ord(s1)-ord("A"))
    i2s.append(ord(s2)-ord("A"))
  f = []
  for i in range(N):
    f.append({})

  def get(i, s):
    if s in f[i]:
      return f[i][s]
    if i == N-1:
      i1 = i1s[i]
      i2 = i2s[i]
      if s[i1] == 0 and s[i2] == 0:
        f[i][s] = False
      else:
        f[i][s] = True
      return f[i][s]
    i1 = i1s[i]
    i2 = i2s[i]
    if s[i1] == 0 and s[i2] == 0:
      f[i][s] = False
      return False
    s1 = list(s)
    s1[i1] -= 1
    s1[i2] += 1
    s1 = tuple(s1)

    s2 = list(s)
    s2[i2] -= 1
    s2[i1] += 1
    s2 = tuple(s2)
    
    f1 = get(i+1, s1) if s1[i1] >= 0 else False
    f2 = get(i+1, s2) if s2[i2] >= 0 else False
    if f1 or f2:
      f[i][s] = True
      return True
    else:
      f[i][s] = False
      return False
  ans = get(0, v)
  if ans:
    print("Yes")
    s = tuple(v)
    for i in range(N):
      #print(s)
      i1 = i1s[i]
      i2 = i2s[i]
      if i == N-1:
        if s[i1] > 0:
          print(chr(i1+65))
        else:
          print(chr(i2+65))
        break
      s1 = list(s)
      s1[i1] -= 1
      s1[i2] += 1
      s1 = tuple(s1)
      s2 = list(s)
      s2[i2] -= 1
      s2[i1] += 1
      s2 = tuple(s2)
      f1 = get(i+1, s1)
      f2 = get(i+1, s2)
      if f1:
        print(chr(i1+65))
        s = s1
      else:
        print(chr(i2+65))
        s = s2
  else:
    print("No")

Submission status: Wrong Answer

Solution code:
x = [int(a) for a in input().split()]
N, A, B, C = x
v = [A, B, C]

if sum(v) >= 3:
  decisions = []
  flg = True
  for i in range(N):
    s1, s2 = input()
    i1 = ord(s1)-ord("A")
    i2 = ord(s2)-ord("A")
    if v[i1] == 0 and v[i2] == 0:
      flg = False
      break
    if v[i1] > v[i2]:
      decisions.append(chr(i2+65))
      v[i1] -= 1
      v[i2] += 1
    else:
      decisions.append(chr(i1+65))
      v[i1] += 1
      v[i2] -= 1
  if flg:
    print("Yes")
    print("\n".join(decisions))
  else:
    print("No")
else:
  print("No")

Submission status: Wrong Answer

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

nums = {"A": a, "B": b, "C": c}

s = []
for i in range(n):
    line = input()
    s.append(line)

def add(a, b):
    nums[a] += 1
    nums[b] -= 1
    ans.append(a)

flag = True

ans = []

for i in range(n):
    a = s[i][0]
    b = s[i][1]

    if nums[a] == 0 and nums[b] == 0:
        print("No")
        flag = False
        break

    if nums[a] == 0:
        add(a, b)
        
    elif nums[b] == 0:
        add(b, a)
        
    else:
        if i != n - 1:
            na = s[i + 1][0]
            nb = s[i + 1][1]

            if (a == na or a == nb):
                add(a, b)

            else:
                add(b, a)

        else:
            add(a, b)

print("Yes")

for i in ans:
    print(i)

Submission status: Wrong Answer

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

YES = "Yes"  # type: str
NO = "No"  # type: str


class Impossible(Exception):
    pass

def f(q, qq, state):
    '''
    >>> f((0, 1), None, [2, 1, 0])
    0
    >>> f((0, 1), None, [1, 2, 0])
    1
    >>> f((0, 1), None, [0, 1, 0])
    1
    >>> f((1, 2), None, [0, 1, 0])
    1
    >>> f((0, 2), None, [1, 0, 0])
    0
    >>> f((0, 1), (0, 3), [1, 1, 0])
    1
    >>> f((0, 1), (0, 3), [1, 2, 0])
    1
    >>> f((0, 1), [], [1, 1, 0])
    0
    >>> f((0, 1), None, [0, 0, 0])
    -1
    >>> f((1, 2), None, [0, 0, 0])
    -1
    >>> f((0, 1), (0, 3), [1, 0, 0])
    0
    '''
    n, m = q
    if state[n] == state[m] == 0:
        return -1
    if state[n] == state[m] == 1:
        if n in qq:
            return m
        return n
    if state[n] > state[m]:
        return n
    return m
        

def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    D = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
    E = 'ABC'
    Q = tuple(map(lambda x: D[x], s))
    state = [A, B, C]
    for q, qq in zip(Q, Q[1:] + (tuple(),)):
        if -1 in state:
            raise 
        r = f(q, qq, state)
        if r == -1:
            raise Impossible
        yield E[r]
        state[r] -= 1
        state[q[q.index(r)^1]] += 1
    if -1 in state:
        raise 


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    try:
        ans = list(solve(N, A, B, C, s))
        print(YES)
        print(*ans, sep='\n')
    except Impossible:
        print(NO)

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    test()
    main()

Submission status: Wrong Answer

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

YES = "Yes"  # type: str
NO = "No"  # type: str


class Impossible(Exception):
    pass

def f(q, qq, state):
    '''
    >>> f((0, 1), None, [2, 1, 0])
    0
    >>> f((0, 1), None, [1, 2, 0])
    1
    >>> f((0, 1), None, [0, 1, 0])
    1
    >>> f((1, 2), None, [0, 1, 0])
    1
    >>> f((0, 2), None, [1, 0, 0])
    0
    >>> f((0, 1), (0, 3), [1, 1, 0])
    1
    >>> f((0, 1), (0, 3), [1, 2, 0])
    1
    >>> f((0, 1), [], [1, 1, 0])
    0
    >>> f((0, 1), None, [0, 0, 0])
    -1
    >>> f((1, 2), None, [0, 0, 0])
    -1
    >>> f((0, 1), (0, 3), [1, 0, 0])
    0
    '''
    n, m = q
    if state[n] == state[m] == 0:
        return -1
    if state[n] == state[m] == 1:
        if n in qq:
            return m
        return n
    if state[n] > state[m]:
        return n
    return m
        

def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    D = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
    E = 'ABC'
    Q = tuple(map(lambda x: D[x], s))
    state = [A, B, C]
    for q, qq in zip(Q, Q[1:] + (tuple(),)):
        if -1 in state:
            raise Impossible
        r = f(q, qq, state)
        if r == -1:
            raise Impossible
        yield E[r]
        state[r] -= 1
        state[q[q.index(r)^1]] += 1
    if -1 in state:
        raise Impossible


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    try:
        ans = list(solve(N, A, B, C, s))
        print(YES)
        print(*ans, sep='\n')
    except Impossible:
        print(NO)

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    test()
    main()

Submission status: Wrong Answer

Solution code:
def step(a,b,c,s):
    kanou=True
    action=""
    
    if s=="AB":
        if a==0 and b==0:
            kanou = False
        if a>=b:
            action = "B"
            a-=1
            b+=1
        else:
            action = "A"
            a+=1
            b-=1
            
    if s=="AC":
        if a==0 and c==0:
            kanou = False
        if a>=c:
            action = "C"
            a-=1
            c+=1
        else:
            action = "A"
            a+=1
            c-=1
            
    if s=="BC":
        if b==0 and c==0:
            kanou = False
        if b>=c:
            action = "C"
            b-=1
            b+=1
        else:
            action = "B"
            b+=1
            c-=1
    return a,b,c,action,kanou
        

n,a,b,c = map(int,input().split())
s=["0"]*n
for i in range(n):
    s[i] = input()

ans=[]
case110 = [[1,1,0],[1,0,1],[0,1,1]]
case200 = [[2,0,0],[0,2,0],[0,0,2]]

if a+b+c==2:
    for i,ss in enumerate(s):
        
        if i!=n-1 and [a,b,c] == [1,1,0] and ss=="AB":
            if s[i+1] =="AC":
                a+=1
                b-=1
                ans.append("A")
            else:
                a-=1
                b+=1
                ans.append("B")
                
        if i!=n-1 and [a,b,c] == [1,0,1] and ss=="AC":
            if s[i+1] =="AB":
                a+=1
                c-=1
                ans.append("A")
            else:
                a-=1
                c+=1
                ans.append("C")
                
        if i!=n-1 and [a,b,c] == [0,1,1] and ss=="BC":
            if s[i+1] =="AB":
                b+=1
                c-=1
                ans.append("B")
            else:
                b-=1
                c+=1
                ans.append("C")
                
        else:
            a,b,c,action, kanou = step(a,b,c,ss)
            if kanou==False:
                break
            else:
                ans.append(action)



else:
    for i,ss in enumerate(s):
        a,b,c,action, kanou = step(a,b,c,ss)
        if kanou==False:
            break
        else:
            ans.append(action)

if kanou==False:
    print("No")
else:
    print("Yes")
    for i in range(n):
        print(ans[i])

Submission status: Wrong Answer

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

YES = "Yes"  # type: str
NO = "No"  # type: str


class Impossible(Exception):
    pass

def f(q, qq, state):
    '''
    >>> f((0, 1), None, [2, 1, 0])
    0
    >>> f((0, 1), None, [0, 1, 0])
    1
    >>> f((0, 1), (0, 3), [1, 1, 0])
    1
    >>> f((0, 1), None, [0, 0, 0])
    -1
    '''
    n, m = q
    if state[n] == state[m] == 0:
        return -1
    if state[n] == state[m] == 1:
        if n in qq:
            return m
        return n
    if state[n] > state[m]:
        return n
    return m
        

def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    D = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
    E = 'ABC'
    Q = tuple(map(lambda x: D[x], s))
    state = [A, B, C]
    for q, qq in zip(Q, Q[1:] + (tuple(),)):
        r = f(q, qq, state)
        if r == -1:
            raise Impossible
        yield E[r]
        state[r] -= 1
        state[q[q.index(r)^1]] += 1


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    try:
        ans = list(solve(N, A, B, C, s))
        print(YES)
        print(*ans, sep='\n')
    except Impossible:
        print(NO)

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    test()
    main()