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
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")

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")

Submission status: Runtime Error

Solution code:

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:
    print("No")
    exit()
  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)

Submission status: Runtime Error

Solution code:
*I, = open(0).read().split()
N, A, B, C = map(int, I[:4])
S = I[4:]

Cnt = {"A": A, "B": B, "C": C}

ans = [""] * N
def dfs(i):
    if i == N:
        print("Yes")
        print("\n".join(ans))
        quit()

    for L, R in (S[i], reversed(S[i])):
        if Cnt[R] > 0:
            Cnt[L] += 1
            Cnt[R] -= 1

            ans[i] = L
            dfs(i + 1)

            Cnt[L] -= 1
            Cnt[R] += 1


dfs(0)
print("No")

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
SN = [input() for _ in range(N)]

ABC = {'A': A, 'B': B, 'C': C}
ans = [""] * N
# print('ABC : ' + str(ABC))

# Recursiveにすることによって、途中で負の値になってもいくらでも遡ることが可能となる
def recursive_exec(i):
  # 最後まで辿り着いたらクリア
  if i == N:
    print('Yes')
    print("\n".join(ans))
    quit()
  for L, R in (SN[i], reversed(SN[i])):	# ex) L, R in ('AB', 'BA')
    if ABC[L] > 0: # LでもRでもどちらでも良い
      ABC[L] -= 1
      ABC[R] += 1
      ans[i] = L

      # Next
      recursive_exec(i+1)

      # 0以下になったら戻して別の文字でやってみる (reversed(SN[i]))
      ABC[L] += 1
      ABC[R] -= 1

recursive_exec(0)
print('No')

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
SN = [input() for _ in range(N)]

ABC = {'A': A, 'B': B, 'C': C}
ans = [""] * N
# print('ABC : ' + str(ABC))

# Recursiveにすることによって、途中で負の値になってもいくらでも遡ることが可能となる
def recursive_exec(i):
  # 最後まで辿り着いたらクリア
  if i == N:
    print('OK')
    print("\n".join(ans))
    quit()
  for L, R in (SN[i], reversed(SN[i])):	# ex) L, R in ('AB', 'BA')
    if ABC[L] > 0: # LでもRでもどちらでも良い
      ABC[L] -= 1
      ABC[R] += 1
      ans[i] = L

      # Next
      recursive_exec(i+1)

      # 0以下になったら戻して別の文字でやってみる (reversed(SN[i]))
      ABC[L] += 1
      ABC[R] -= 1

recursive_exec(0)
print('NG')

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int, input().split())
ans = []
number = dict()
number['A'] = a
number['B'] = b
number['C'] = c
query = [input() for i in range(n)]
for i in range(n):
    s = query[i]
    s_1, s_2 = s[0], s[1]
    if number[s_1] == 0 and number[s_2] == 0:
        print("No")
        exit()
    else:
        if number[s_1] > number[s_2]:
            number[s_1] -= 1
            number[s_2] += 1
            ans.append(s_2)
        elif number[s_1] < number[s_2]:
            number[s_2] -= 1
            number[s_1] += 1
            ans.append(s_1)
        else:
            if i == n-1:
                number[s_2] -= 1
                number[s_1] += 1
                ans.append(s_1)
            else:
                if query[i+1] == s_1 + s_2:
                    number[s_2] -= 1
                    number[s_1] += 1
                    ans.append(s_1)
                else:
                    n_1,n_2 = query[i+1]
                    if s_1 in set(n_1,n_2):
                        number[s_2] -= 1
                        number[s_1] += 1
                        ans.append(s_1)
                    else:
                        number[s_1] -= 1
                        number[s_2] += 1
                        ans.append(s_2)

    #print("A:{} B:{} C:{}".format(number['A'],number['B'],number['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())

q = [(a, b, c, "")]

for _ in range(n):
    x = input()
    l = len(q)
    for _ in range(l):
        a, b, c, steps = q.pop(0)
        if x == "AB":
            if max(a, b) > 0:
                if a > b:
                    q.append((a-1, b+1, c, steps+"B"))
                elif b > a:
                    q.append((a+1, b-1, c, steps+"A"))
                else:
                    q.append((a-1, b+1, c, steps+"B"))
                    q.append((a+1, b-1, c, steps+"A"))
        elif x == "AC":
            if max(a, c) > 0:
                if a > c:
                    q.append((a-1, b, c+1, steps+"C"))
                elif c > a:
                    q.append((a+1, b, c-1, steps+"A"))
                else:
                    q.append((a-1, b, c+1, steps+"C"))
                    q.append((a+1, b, c-1, steps+"A"))
        elif x == "BC":
            if max(b, c) > 0:
                if b > c:
                    q.append((a, b-1, c+1, steps+"C"))
                elif c > b:
                    q.append((a, b+1, c-1, steps+"B"))
                else:
                    q.append((a, b+1, c-1, steps+"B"))
                    q.append((a, b-1, c+1, steps+"C"))

i = q[-1]
if all(i[:-1]) and len(i[-1])==n:
    print("Yes")
    for char in i[-1]:
        print(char)
else: print("No")

Submission status: Runtime Error

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

A, B, C = min(A, 2), min(B, 2), min(C, 2)
X = [0, A, B, C]  # 1-indexecの方が楽そう
F = {1: 'A', 2: 'B', 3: 'C'}

ans = []
for k, s in enumerate(S):
    if s == 'AB':
        i, j = 1, 2
    elif s == 'BC':
        i, j = 2, 3
    elif s == 'AC':
        i, j = 1, 3

    if X[i] > X[j]:
        X[i] -= 1
        X[j] += 1
        ans.append(j)

    elif X[i] < X[j]:
        X[i] += 1
        X[j] -= 1
        ans.append(i)

    else:
        if (X[i] == X[j] == 1) and (k < N):
            if ((S[k + 1][0] == F[i]) and (S[k + 1][1] == F[i ^ j])) or ((S[k + 1][0] == F[i ^ j]) and (S[k + 1][1] == F[i])):
                X[i] += 1
                X[j] -= 1
                ans.append(i)
            else:
                X[j] += 1
                X[i] -= 1
                ans.append(j)
        else:
            X[i] -= 1
            X[j] += 1
            ans.append(j)

    if any([x < 0 for x in X]):
        print('No')
        exit()

print('Yes')
for a in ans:
    print(F[a])

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())

S = []
for _ in range(N):
  s = int(input())
  S.append(s)

ans = "Yes"
turn = []  

if A+B+C == 0:
  ans = "No"
elif A+B+C == 1:
  while ans == "Yes":
    for s in S:
      if s == "AB":
        if A + B == 0:
          ans = "No"
        elif A == 0:
          A = 1
          B = 0
          turn.append("A")
        else B == 0:
          B = 1
          A = 0
          turn.append("B")
      elif s == "BC":
        if C + B == 0:
          ans = "No"
        elif C == 0:
          C = 1
          B = 0
          turn.append("C")
        else B == 0:
          B = 1
          C = 0
          turn.append("B")
      else:
        if A + C == 0:
          ans = "No"
        elif A == 0:
          A = 1
          C = 0
          turn.append("A")
        else C == 0:
          C = 1
          A = 0
          turn.append("C")
else:
  if S[0] == "AB" and A+B == 0:
    ans = "No"
  elif S[0] == "BC" and B+C==0:
    ans = "No"
  elif S[0] == "AC" and A+C==0:
    ans = "No"
  while ans == "Yes":
    for s in S:
      if s == "AB":
        if A <= B or A==0:
          A += 1
          B -= 1
          turn.append("A")
        else:
          B += 1
          A -= 1
          turn.append("B")
      if s == "BC":
        if C <= B or C==0:
          C += 1
          B -= 1
          turn.append("C")
        else:
          B += 1
          C -= 1
          turn.append("B")
      if s == "AC":
        if A <= C or A==0:
          A += 1
          C -= 1
          turn.append("A")
        else:
          C += 1
          A -= 1
          turn.append("C")

if ans == "No":
  print("No")
else:
  print("Yes")
  for t in turn:
    print(t)

Submission status: Runtime Error

Solution code:
sys.setrecursionlimit(10 ** 5 + 10)

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

ans = list()
l = list()

for n in range(N):
    l.append(input())

def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and b == 0:
            return False
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
    if ss == 'AC':
        if a == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
    if ss == 'BC':
        if b == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
    return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for x in reversed(ans):
        print(x)
else:
    print('No')

Submission status: Runtime Error

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

ans = list()
l = list()

for n in range(N):
    l.append(input())

def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and b == 0:
            return False
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        return False
    if ss == 'AC':
        if a == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        return False
    if ss == 'BC':
        if b == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        return False
    return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for x in reversed(ans):
        print(x)
else:
    print('No')

Submission status: Runtime Error

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

ans = list()
l = list()

for n in range(N):
    l.append(input())

def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and b == 0:
            return False
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        return False
    elif ss == 'AC':
        if a == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        return False
    elif ss == 'BC':
        if b == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        return False
    return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for x in reversed(ans):
        print(x)
else:
    print('No')

Submission status: Runtime Error

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

ans = list()
l = list()

for n in range(N):
    l.append(input())

def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and b == 0:
            return False
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        return False
    elif ss == 'AC':
        if a == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        return False
    elif ss == 'BC':
        if b == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for x in reversed(ans):
        print(x)
else:
    print('No')

Submission status: Runtime Error

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

ans = list()
l = list()

for n in range(N):
    l.append(input())

def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and b == 0:
            return False
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        else:
            return False
    elif ss == 'AC':
        if a == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        else:
            return False
    elif ss == 'BC':
        if b == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        else:
            return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for x in reversed(ans):
        print(x)
else:
    print('No')

Submission status: Runtime Error

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

ans = list()
l = list()

for i in range(N):
    l.append(input())

def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and b == 0:
            return False
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        else:
            return False
    elif ss == 'AC':
        if a == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        else:
            return False
    elif ss == 'BC':
        if b == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        else:
            return False

dfs(0, A, B, C)

print('No')

Submission status: Runtime Error

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

ans = list()
l = list()

for i in range(N):
    l.append(input())

def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and  b == 0:
            return False
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        else:
            return False
    elif ss == 'AC':
        if a == 0 and  c == 0:
            return False
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        else:
            return False
    elif ss == 'BC':
        if b == 0 and  c == 0:
            return False
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        else:
            return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for x in reversed(ans):
        print(x)
else:
    print('No')

Submission status: Runtime Error

Solution code:
stdin = open(0)

N, A, B, C = map(int, stdin.readline().split())
S = stdin.read().split()

ans = [""] * N
def dfs(i, a, b, c):
    if a < 0 or b < 0 or c < 0:
        return

    if i == N:
        print("Yes")
        print("\n".join(ans))
        quit()

    if S[i] == "AB":
        ans[i] = "A"
        dfs(i + 1, a + 1, b - 1, c)

        ans[i] = "B"
        dfs(i + 1, a - 1, b + 1, c)

    elif S[i] == "BC":
        ans[i] = "B"
        dfs(i + 1, a, b + 1, c - 1)

        ans[i] = "C"
        dfs(i + 1, a, b - 1, c + 1)
    else:
        ans[i] = "A"
        dfs(i + 1, a + 1, b, c - 1)

        ans[i] = "C"
        dfs(i + 1, a - 1, b, c + 1)

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

Submission status: Runtime Error

Solution code:
ans=''
def dfs(a,b,c,t):
    global ans
    if a<0 or b<0 or c<0:
        return False
    if t==N:
        return True
    if s[t]=='AB':
        if dfs(a-1,b+1,c,t+1):
            ans+='B'
            return True
        elif dfs(a+1,b-1,c,t+1):
            ans+='A'
            return True
    if s[t]=='BC':
        if dfs(a,b-1,c+1,t+1):
            ans+='C'
            return True
        elif dfs(a,b+1,c-1,t+1):
            ans+='B'
            return True
    if s[t]=='AC':
        if dfs(a+1,b,c-1,t+1):
            ans+='A'
            return True
        if dfs(a-1,b,c+1,t+1):
            ans+='C'
            return True
    return False

N,A,B,C=map(int,input().split())
s=[input() for i in range(N)]

if dfs(A,B,C,0):
    print('Yes')
    print(*ans[::-1],sep='\n')
else:
    print('No')

Submission status: Runtime Error

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

ans = list()
l = list()

for i in range(N):
    l.append(input())

def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    print(ss)
    if ss == 'AB':
        if a == 0 and  b == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.insert(0, 'B')
            return True
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.insert(0, 'A')
            return True
        else:
            return False
    elif ss == 'AC':
        if a == 0 and  c == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.insert(0, 'C')
            return True
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.insert(0, 'A')
            return True
        else:
            return False
    elif ss == 'BC':
        if b == 0 and  c == 0:
            return False
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.insert(0, 'C')
            return True
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.insert(0, 'B')
            return True
        else:
            return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for x in ans:
        print(x)
else:
    print('No')

Submission status: Runtime Error

Solution code:
N, A, B, C = [int(i) for i in input().split()]
 
ans = list()
l = list()
 
for i in range(N):
    l.append(input())
 
 
def dfs(i, a, b, c):
    if i == N:
        return True
 
    ss = l[i]
    if ss == 'AB':
        if a == 0 and  b == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        else:
            return False
    elif ss == 'AC':
        if a == 0 and  c == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        else:
            return False
    elif ss == 'BC':
        if b == 0 and  c == 0:
            return False
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        else:
            return False
 
dfs(0, A, B, C)
 
if len(ans) > 0:
    print('Yes')
    for a in reversed(ans):
        print(a)
else:
    print('No')

Submission status: Runtime Error

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

ans = list()
l = list()

for i in range(N):
    l.append(input())


def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and  b == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        else:
            return False
    elif ss == 'AC':
        if a == 0 and  c == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        else:
            return False
    elif ss == 'BC':
        if b == 0 and  c == 0:
            return False
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        else:
            return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for a in reversed(ans):
        print(a)
else:
    print('No')
~

Submission status: Runtime Error

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

ans = list()
l = list()

for i in range(N):
    l.append(input())


def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and  b == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        else:
            return False
    elif ss == 'AC':
        if a == 0 and  c == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        else:
            return False
    elif ss == 'BC':
        if b == 0 and  c == 0:
            return False
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        else:
            return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for a in ans:
        print(a)
else:
    print('No')

Submission status: Runtime Error

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

ans = list()
l = list()

for i in range(N):
    l.append(input())


def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and  b == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
        elif b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        else:
            return False
    elif ss == 'AC':
        if a == 0 and  c == 0:
            return False
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
        elif c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        else:
            return False
    elif ss == 'BC':
        if b == 0 and  c == 0:
            return False
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
        elif c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        else:
            return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for a in ans:
        print(a)
else:
    print('No')

Submission status: Runtime Error

Solution code:
n,A,B,C=map(int,input().split())
r=[A,B,C]
g=list()
for i in range(n):
    g.append(str(input()))
    
def re(string):
    if string=="AB":
        return(0,1)
    elif string=="AC":
        return(0,2)
    else:
        return(1,2)
    
def al(num):
    if num==0:
        return "A"
    elif num==1:
        return "B"
    else:
        return "C"
    
flag=0
result=list()
for i in range(n):
    a,b=re(g[i])
    if r[a]==0 and r[b]==0:
        print("No")
        flag=1
        break
    elif r[a]>r[b]:
        r[a]-=1
        r[b]+=1
        result.append(al(b))
    elif r[a]<r[b]:
        r[a]+=1
        r[b]-=1
        result.append(al(a))
    else:
        x,y=re(g[i+1])
        if (x,y)==(a,b):
            r[a]+=1
            r[b]-=1
            result.append(al(a))
        else:
            if x in (a,b):
                r[x]+=2
                r[a]-=1;r[b]-=1
                result.append(al(x))
            else:
                r[y]+=2
                r[a]-=1;r[b]-=1
                result.append(al(y))
        
if flag==0:
    print("Yes")
    for item in result:
        print(item)

Submission status: Runtime Error

Solution code:
n, a, b, c = list(map(int, input().split()))
s = [list(input()) for _ in range(n)]
d = {"A": a, "B": b, "C": c}
ans = []
for i, [j, k] in enumerate(s):
    if d[j] == 0 and d[k] == 0:
        print("No")
        break
    elif d[j] > d[k]:
        calc(k, j)
    elif d[j] < d[k]:
        calc(j, k)
    elif i + 1 != n:
        if j == "A":
            if s[i + 1][1] == "C":
                calc(k, j)
            else:
                calc(j, k)        
        else:
            if s[i + 1] == ["A", "C"]:
                calc(k, j)
            else:
                calc(j, k)
    else:
        calc(j, k)
else:
    print("Yes")
    for i in ans:
        print(i)