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:
n,a,b,c=map(int,input().split())
s=list(input() for i in range(n))
ans=["Yes"]

if a+b+c==0:
  print("No")
  
if a+b+c==1:
  for j in range(n):
    
    if s[j]=="AB":
      if c==1:
        ans="No"
        print(ans)
        break        
      else:
        continue
      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:
        ans="No"
        break
        print(ans)        
      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:
        ans="No"
        print(ans)
        break
      if b==1:
        b-=1
        c+=1
        ans+="C"
      if c==1:
        b+=1
        c-=1
        ans+="B"  
  print(*ans,sep="\n")
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:
        ans="No"
        print(ans)
        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:
        ans="No"
        print(ans)
        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:
        ans="No"
        print(ans)
        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:
    ans="No"
    print(ans)
  
  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"
    print(*ans,sep="\n")

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:
  ans="No"
  
if a+b+c==1:
  for j in range(n):
    
    if s[j]=="AB":
      if c==1:
        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:
        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:
        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:
        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:
        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:
        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"        

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:
    ans="No"
  
  else:
    for j in range(n):
      if s[j]=="AB":
        if a>b:
          a-=1
          b+=1
          ans+="B"
        if a<b:
          a+=1
          b-=1
          ans+="A"
        
      if s[j]=="AC":
        if a>c:
          a-=1
          c+=1
          ans+="C"
        if a<c:
          a+=1
          c-=1
          ans+="A"     

      if s[j]=="BC":
        if b>c:
          b-=1
          c+=1
          ans+="C"
        if b<c:
          b+=1
          c-=1
          ans+="B"

print(*ans,sep="\n")

Submission status: Runtime Error

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

s = list()
d = {'A': 0, 'B': 1, 'C': 2}

for i in range(n):
    s.append(input())

def search(a, s):
    if(len(s)==0):
        return []
    for i in range(2):
        # 減算可能
        if(a[d[s[0][1-i]]]>0):
            a[d[s[0][i]]] +=1
            a[d[s[0][1-i]]] -=1
            r = search(a, s[1:])
            if(r is not None):
                r.append(s[0][i])
                return r
            a[d[s[0][i]]] +=1
            a[d[s[0][1-i]]] -=1
    return None

ans = search([a,b,c], s)
if(ans is None):
    print('No')
else:
    print('Yes')
    for i in reversed(range(n)):
        print(ans[i])

Submission status: Runtime Error

Solution code:
from functools import reduce

class ABC:
    def __init__(self, A,B,C):
        self.current = dict(A=A,B=B,C=C)
        self.selected = []
        self.answer = None

    def inc(self, X):
        self.current[X] += 1
    
    def dec(self, X):
        self.current[X] -= 1
    
    def select(self, X, Y):
        self.inc(X)
        self.dec(Y)
        self.selected.append(X)
        #print("select", X, "current", self.current)
    
    def get(self, X):
        return self.current[X]

    def command(self, cmds):
        for i, cmd in enumerate(cmds):
            if self.get(cmd[0]) == 0:
                self.select(cmd[1], cmd[0])

            elif self.get(cmd[0]) ==  self.get(cmd[1]) == 1:
                another = (set("ABC") - set(cmd)).pop()
                if self.get(another) == 0 and i < len(cmds) -1:
                    next_cmds = cmd[i+1:]
                    for j, next_cmd in enumrate(next_cmds):
                        if next_cmd == cmd:
                            continue
                        else:
                            X = (set(cmd) & set(next_cmd)).pop()
                            XX = (set(cmd) - set([X])).pop()
                            if j % 2 == 0:
                                self.select(X, XX)
                            else:
                                self.select(XX, X)

                else:
                    self.select(cmd[0], cmd[1])
            else:
                small, big = sorted(cmd, key=lambda key: self.get(key))
                self.select(small, big)

            if list(map(lambda x: x < 0, self.current.values())).count(True) > 0:
                self.answer = "No"
                return
                
        self.answer = "Yes"

    def print_answer(self):
        print(self.answer)
        if self.answer == 'Yes':
            print("\n".join(self.selected))

def test():
    A,B,C = 2, 3, 4
    import numpy as np
    XX = ["AB", "BC", "CA"]
    cmds = [XX[i] for i in np.random.randint(0,3, [10])]
    current = ABC(A,B,C)
    current.command(cmds)
    print(current.selected)
    print(current.current)
    current.print_answer()



def main():

    #test()

    N, A ,B, C = map(int, input().split())
    S = [input() for i in range(N)]
    
    current = ABC(A,B,C)
    current.command(S)
    current.print_answer()



if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def abc(d, down_text, up_text):
    d[down_text] -= 1
    d[up_text] += 1


def main():
    n, a, b, c = map(int, input().split())
    ans_list = []
    dct = {"A": a, "B": b, "C": c}

    b_txt = input()
    if dct[b_txt[0]] == dct[b_txt[1]]:
        print('No')
        exit()

    for _ in range(n - 1):
        a_txt = input()
        l_txt = b_txt[0]
        r_txt = b_txt[1]

        if dct[l_txt] < dct[r_txt]:
            abc(dct, r_txt, l_txt)
            ans_list.append(l_txt)

        elif dct[l_txt] > dct[r_txt]:
            abc(dct, l_txt, r_txt)
            ans_list.append(r_txt)

        else:
            if dct[l_txt] == dct[r_txt] == 0:
                print('No')
                break
            else:
                if l_txt in a_txt:
                    abc(dct, r_txt, l_txt)
                    ans_list.append(l_txt)
                else:
                    abc(dct, l_txt, r_txt)
                    ans_list.append(r_txt)
        b_txt = a_txt
    else:
        if dct[a_txt[0]] == dct[a_txt[1]] == 0:
            print('No')
            exit()
        elif dct[a_txt[0]] < dct[a_txt[1]]:
            ans_list.append(a_txt[0])
        else:
            ans_list.append(a_txt[1])
        print('Yes')
        print(*ans_list, sep='\n')


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
def main():
    #input
    N, A, B, C = map(int, input().split())
    s = [""] * (N+1)
    for i in range(N):
        s[i] = str(input())

    #output
    dp = [""] * N
    a, b, c = [0]*(N+1), [0]*(N+1), [0]*(N+1)
    a[0], b[0], c[0] = A, B, C

    for i in range(N):
        if a[i] + b[i] + c[i] == 0:
            print("No")
            exit()
        elif a[i] + b[i] + c[i] == 1:
            if s[i] == "AB":
                if a[i] == 1:
                    a[i+1] = a[i] - 1
                    b[i+1] = b[i] + 1
                    c[i+1] = c[i]
                    dp[i] = "B"
                if b[i] == 1:
                    a[i+1] = a[i] + 1
                    b[i+1] = b[i] - 1
                    c[i+1] = c[i]
                    dp[i] = "A"
                if c[i] == 1:
                    print("No")
                    exit()
            elif s[i] == "BC":
                if a[i] == 1:
                    print("No")
                    eixt()
                if b[i] == 1:
                    a[i+1] = a[i]
                    b[i+1] = b[i] - 1
                    c[i+1] = c[i] + 1
                    dp[i] = "C"
                if c[i] == 1:
                    a[i+1] = a[i]
                    b[i+1] = b[i] + 1
                    c[i+1] = c[i] - 1
                    dp[i] = "B"
            elif s[i] == "AC":
                if a[i] == 1:
                    a[i+1] = a[i] - 1
                    b[i+1] = b[i]
                    c[i+1] = c[i] + 1
                    dp[i] = "C"
                if b[i] == 1:
                    print("No")
                    eixt()
                if c[i] == 1:
                    a[i+1] = a[i] + 1
                    b[i+1] = b[i]
                    c[i+1] = c[i] - 1
                    dp[i] = "A"
        elif a[i] + b[i] + c[i] >= 2:
            if s[i] == "AB":
                if a[i] == 0 and b[i] == 0:
                    print("No")
                    exit()
                elif a[i] == 0 and b[i] > 0:
                    a[i+1] = a[i] + 1
                    b[i+1] = b[i] - 1
                    c[i+1] = c[i]
                    dp[i] = "A"
                elif a[i] > 0 and b[i] == 0:
                    a[i+1] = a[i] - 1
                    b[i+1] = b[i] + 1
                    c[i+1] = c[i]
                    dp[i] = "B"
                elif a[i] == 1 and b[i] == 1 and c[i] == 0:
                    if i != N and s[i+1] != "AB":
                        if s[i+1] == "BC":
                            a[i+1] = a[i] - 1
                            b[i+1] = b[i] + 1
                            c[i+1] = c[i]
                            dp[i] = "B"
                        elif s[i+1] == "AC":
                            a[i+1] = a[i] + 1
                            b[i+1] = b[i] - 1
                            c[i+1] = c[i]
                            dp[i] = "A"
                else:
                    if a[i] >= b[i]:
                        a[i+1] = a[i] - 1
                        b[i+1] = b[i] + 1
                        c[i+1] = c[i]
                        dp[i] = "B"
                    else:
                        a[i+1] = a[i] + 1
                        b[i+1] = b[i] - 1
                        c[i+1] = c[i]
                        dp[i] = "A"
            elif s[i] == "BC":
                if b[i] == 0 and b[i] == 0:
                    print("No")
                    exit()
                elif b[i] == 0 and c[i] > 0:
                    b[i+1] = b[i] + 1
                    c[i+1] = c[i] - 1
                    a[i+1] = a[i]
                    dp[i] = "B"
                elif b[i] > 0 and c[i] == 0:
                    b[i+1] = b[i] - 1
                    c[i+1] = c[i] + 1
                    a[i+1] = a[i]
                    dp[i] = "C"
                elif b[i] == 1 and c[i] == 1 and a[i] == 0:
                    if i != N and s[i+1] != "BC":
                        if s[i+1] == "CA":
                            b[i+1] = b[i] - 1
                            c[i+1] = c[i] + 1
                            a[i+1] = a[i]
                            dp[i] = "C"
                        elif s[i+1] == "AB":
                            b[i+1] = b[i] + 1
                            c[i+1] = c[i] - 1
                            a[i+1] = a[i]
                            dp[i] = "B"
                else:
                    if b[i] >= c[i]:
                        b[i+1] = b[i] - 1
                        c[i+1] = c[i] + 1
                        a[i+1] = a[i]
                        dp[i] = "C"
                    else:
                        b[i+1] = b[i] + 1
                        c[i+1] = c[i] - 1
                        a[i+1] = a[i]
                        dp[i] = "B"
            elif s[i] == "AC":
                if c[i] == 0 and a[i] == 0:
                    print("No")
                    exit()
                elif c[i] == 0 and a[i] > 0:
                    c[i+1] = c[i] + 1
                    a[i+1] = a[i] - 1
                    b[i+1] = b[i]
                    dp[i] = "C"
                elif c[i] > 0 and a[i] == 0:
                    c[i+1] = c[i] - 1
                    a[i+1] = a[i] + 1
                    b[i+1] = b[i]
                    dp[i] = "A"
                elif c[i] == 1 and a[i] == 1 and b[i] == 0:
                    if i != N and s[i+1] != "AC":
                        if s[i+1] == "AB":
                            c[i+1] = c[i] - 1
                            a[i+1] = a[i] + 1
                            b[i+1] = b[i]
                            dp[i] = "A"
                        elif s[i+1] == "BC":
                            c[i+1] = c[i] + 1
                            a[i+1] = a[i] - 1
                            b[i+1] = b[i]
                            dp[i] = "C"
                else:
                    if c[i] >= a[i]:
                        c[i+1] = c[i] - 1
                        a[i+1] = a[i] + 1
                        b[i+1] = b[i]
                        dp[i] = "A"
                    else:
                        c[i+1] = c[i] + 1
                        a[i+1] = a[i] - 1
                        b[i+1] = b[i]
                        dp[i] = "C"

    print("Yes")
    for v in dp:
        print(v)
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

##### dict の for文 #####
# for k, v in d.items():
#     print(k, v)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N,A,B,C = inputMap()
	ans = []
	flg = False
	if 2 == A+B+C:
		flg = True

	inp = []
	for i in range(N):
		tmp = inputStr()
		inp.append(tmp)

	if flg == False:
		for i,val in enumerate(inp):
			if val == "AB":
				if A < B:
					A += 1
					B -= 1
					ans.append("A")
				else:
					A -= 1
					B += 1
					ans.append("B")
			elif val == "AC":
				if A < C:
					A += 1
					C -= 1
					ans.append("A")
				else:
					A -= 1
					C += 1
					ans.append("C")
			else:
				if B < C:
					B += 1
					C -= 1
					ans.append("B")
				else:
					B -= 1
					C += 1
					ans.append("C")

			if A < 0 or B < 0 or C < 0:
				print("No")
				sys.exit()

	else:
		for i,val in enumerate(inp):
			if val == "AB":
				if A == 1 and B == 1:
					nextInp = inp[i+1]
					if nextInp == "AB":
						A += 1
						B -= 1
						ans.append("A")
					elif nextInp == "AC":
						A += 1
						B -= 1
						ans.append("A")
					else:
						A -= 1
						B += 1
						ans.append("B")
				else:
					if A < B:
						A += 1
						B -= 1
						ans.append("A")
					else:
						A -= 1
						B += 1
						ans.append("B")
			elif val == "AC":
				if A == 1 and C == 1:
					nextInp = inp[i+1]
					if nextInp == "AB":
						A += 1
						C -= 1
						ans.append("A")
					elif nextInp == "AC":
						A += 1
						C -= 1
						ans.append("A")
					else:
						C += 1
						A -= 1
						ans.append("C")
				else:
					if A < C:
						A += 1
						C -= 1
						ans.append("A")
					else:
						A -= 1
						C += 1
						ans.append("C")
			else:
				if B == 1 and C == 1:
					nextInp = inp[i+1]
					if nextInp == "AB":
						B += 1
						C -= 1
						ans.append("A")
					elif nextInp == "AC":
						C += 1
						B -= 1
						ans.append("C")
					else:
						C += 1
						B -= 1
						ans.append("C")
				else:
					if B < C:
						B += 1
						C -= 1
						ans.append("B")
					else:
						B -= 1
						C += 1
						ans.append("C")

			if A < 0 or B < 0 or C < 0:
				print("No")
				sys.exit()

	print("Yes")
	for i in ans:
		print(i)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# N 個のボールを K グループに分ける場合のパターン数
def sunuke(N, K, mod=10**9+7):
	if N < K or K-1 < 0:
		return 0
	else:
		return combination(N-1, K-1, mod)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Runtime Error

Solution code:
#input
def main():
    #input
    N, A, B, C = map(int, input().split())
    s = [""] * (N+1)
    for i in range(N):
        s[i] = str(input())

    #output
    """i回目の施行後、A, B, Cの数をそれぞれa[i], b[i], c[i]とする。
    a+b+cは一定である。
    i回目までのABの個数をp[i], BCの個数をq[i], CAの個数をr[i]とする。
    このうちAを選択した回数をx, Bを選択した回数をy, Cを選択した回数をzとする。
    x+y+z = i,
    i回目の施行の時、s[i+1]を参照する。
    例えばs[i]が"AB"だった時、a[i] + b[i] = 0ならば、施行は継続できない。
    a[i] + b[i] > 0の時、s[i+1]を参照して、
    s[i+1] = "AB"ならば、i回目はABどちらを動かしても問題ない。
    s[i+1] = "BC"ならば、Aを-1、Bを+1する。
    s[i+1] = "CA"ならば、Aを+1、Bを-1する。"""

    dp = [""] * N
    a, b, c = [0]*(N+1), [0]*(N+1), [0]*(N+1)
    a[0], b[0], c[0] = A, B, C

    for i in range(N):
        if s[i] == "AB":
            if a[i] + b[i] == 0:
                print("No")
                exit()
            elif a[i] + b[i] > 0:
                if a[i] == 0:
                    a[i+1] = a[i] + 1
                    b[i+1] = b[i] - 1
                    c[i+1] = c[i]
                    dp[i] = "A"
                elif b[i] == 0:
                    a[i+1] = a[i] - 1
                    b[i+1] = b[i] + 1
                    c[i+1] = c[i]
                    dp[i] = "B"
                elif s[i+1] == "AB":
                    if a[i] >= b[i]:
                        a[i+1] = a[i] - 1
                        b[i+1] = b[i] + 1
                        c[i+1] = c[i]
                        dp[i] = "B"
                    elif a[i] < b[i]:
                        a[i+1] = a[i] + 1
                        b[i+1] = b[i] - 1
                        c[i+1] = c[i]
                        dp[i] = "A"
                elif s[i+1] == "BC":
                    a[i+1] = a[i] - 1
                    b[i+1] = b[i] + 1
                    c[i+1] = c[i]
                    dp[i] = "B"
                elif s[i+1] == "AC":
                    a[i+1] = a[i] + 1
                    b[i+1] = b[i] - 1
                    c[i+1] = c[i]
                    dp[i] = "A"
                elif s[i+1] == "":
                    if a[i] >= b[i]:
                        a[i+1] = a[i] - 1
                        b[i+1] = b[i] + 1
                        c[i+1] = c[i]
                        dp[i] = "B"
                    elif a[i] < b[i]:
                        a[i+1] = a[i] + 1
                        b[i+1] = b[i] - 1
                        c[i+1] = c[i]
                        dp[i] = "A"
        elif s[i] == "BC":
            if b[i] + c[i] == 0:
                print("No")
                exit()
            elif b[i] + c[i] > 0:
                if b[i] == 0:
                    b[i+1] = b[i] + 1
                    c[i+1] = c[i] - 1
                    a[i+1] = a[i]
                    dp[i] = "B"
                elif c[i] == 0:
                    b[i+1] = b[i] - 1
                    c[i+1] = c[i] + 1
                    a[i+1] = a[i]
                    dp[i] = "C"
                elif s[i+1] == "BC":
                    if b[i] >= c[i]:
                        b[i+1] = b[i] - 1
                        c[i+1] = C[i] + 1
                        a[i+1] = a[i]
                        dp[i] = "C"
                    elif b[i] < c[i]:
                        b[i+1] = b[i] + 1
                        c[i+1] = c[i] - 1
                        a[i+1] = a[i]
                        dp[i] = "B"
                elif s[i+1] == "AC":
                    b[i+1] = b[i] - 1
                    c[i+1] = c[i] + 1
                    a[i+1] = a[i]
                    dp[i] = "C"
                elif s[i+1] == "AB":
                    b[i+1] = b[i] + 1
                    c[i+1] = c[i] - 1
                    a[i+1] = a[i]
                    dp[i] = "B"
                elif s[i+1] == "":
                    if b[i] >= c[i]:
                        b[i+1] = b[i] - 1
                        c[i+1] = c[i] + 1
                        a[i+1] = a[i]
                        dp[i] = "C"
                    elif b[i] < c[i]:
                        b[i+1] = b[i] + 1
                        c[i+1] = c[i] - 1
                        a[i+1] = a[i]
                        dp[i] = "B"
        elif s[i] == "AC":
            if c[i] + a[i] == 0:
                print("No")
                exit()
            elif c[i] + a[i] > 0:
                if c[i] == 0:
                    c[i+1] = c[i] + 1
                    a[i+1] = a[i] - 1
                    b[i+1] = b[i]
                    dp[i] = "C"
                elif a[i] == 0:
                    c[i+1] = c[i] - 1
                    a[i+1] = a[i] + 1
                    b[i+1] = b[i]
                    dp[i] = "A"
                elif s[i+1] == "AC":
                    if c[i] > a[i]:
                        c[i+1] = c[i] - 1
                        a[i+1] = a[i] + 1
                        b[i+1] = b[i]
                        dp[i] = "A"
                    elif c[i] < a[i]:
                        c[i+1] = c[i] + 1
                        a[i+1] = a[i] - 1
                        b[i+1] = b[i]
                        dp[i] = "C"
                elif s[i+1] == "AB":
                    c[i+1] = c[i] - 1
                    a[i+1] = a[i] + 1
                    b[i+1] = b[i]
                    dp[i] = "A"
                elif s[i+1] == "BC":
                    c[i+1] = c[i] + 1
                    a[i+1] = a[i] - 1
                    b[i+1] = b[i]
                    dp[i] = "C"
                elif s[i+1] == "":
                    if c[i] >= a[i]:
                        c[i+1] = c[i] - 1
                        a[i+1] = a[i] + 1
                        b[i+1] = b[i]
                        dp[i] = "A"
                    elif c[i] < a[i]:
                        c[i+1] = c[i] + 1
                        a[i+1] = a[i] - 1
                        b[i+1] = b[i]
                        dp[i] = "C"

    print("Yes")
    for v in dp:
        print(v)
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().strip().split())
L = [input().strip() for _ in range(n)]

# global ans
# ans=[]

def dfs(a,b,c,route):
    global ans
    if a<0 or b<0 or c<0:
        return None
    
    depth=len(route)
    if depth==n:
        return route
    
    if L[depth]=="AB":
        newroute=dfs(a+1,b-1,c,route+"A")
        if newroute:
            return newroute
        newroute=dfs(a-1,b+1,c,route+"B")
        if newroute:
            return newroute
    elif L[depth]=="AC":
        newroute=dfs(a+1,b,c-1,route+"A")
        if newroute:
            return newroute
        newroute=dfs(a-1,b,c+1,route+"C")
        if newroute:
            return newroute
    elif L[depth]=="BC":
        newroute=dfs(a,b+1,c-1,route+"B")
        if newroute:
            return newroute
        newroute=dfs(a,b-1,c+1,route+"C")
        if newroute:
            return newroute
    
    return None

ans=dfs(a,b,c,"")
if ans:
    print("Yes")
    [print(c) for c in ans]
else:
    print("No")