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=0
s=[]
ans=[]

def dfs(a,b,c,i,root):
    if i==n:
        global ans
        ans=root.copy()
        return True
    l=False
    r=False
    if s[i]=='AB':
        if a>0:
            root.append('B')
            l=dfs(a-1,b+1,c,i+1,root)
            root.pop()
        if b>0:
            root.append('A')
            r=dfs(a+1,b-1,c,i+1,root)
            root.pop()
    elif s[i]=='AC':
        if a>0:
            root.append('C')
            l=dfs(a-1,b,c+1,i+1,root)
            root.pop()
        if c>0:
            root.append('A')
            r=dfs(a+1,b,c-1,i+1,root)
            root.pop()
    else:
        if b>0:
            root.append('C')
            l=dfs(a,b-1,c+1,i+1,root)
            root.pop()
        if c>0:
            root.append('B')
            r=dfs(a,b+1,c-1,i+1,root)
            root.pop()
    return l or r

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

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

Submission status: Runtime Error

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

flag = False
ans1 = []

def func(a, b, c, i, ans):
    global flag
    global ans1
    if flag:
        return
    if a<0 or b<0 or c<0:
        return
    if i==n:
        flag = True
        ans1 = ans
        return
    if s[i]=="AB":
        ans.append("A")
        func(a+1, b-1, c, i+1, ans.copy())
        ans = ans[:-1]
        ans.append("B")
        func(a-1, b+1, c, i+1, ans.copy())        
    if s[i]=="AC":
        ans.append("A")
        func(a+1, b, c-1, i+1, ans.copy())
        ans = ans[:-1]
        ans.append("C")
        func(a-1, b, c+1, i+1, ans.copy())     
    if s[i]=="BC":
        ans.append("B")
        func(a, b+1, c-1, i+1, ans.copy())
        ans = ans[:-1]
        ans.append("C")
        func(a, b-1, c+1, i+1, ans.copy())
    
        
func(a,b,c,0,[])
if flag:
    print("Yes")
    [print(ans_) for ans_ in ans1]
else:
    print("No")

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
s = [input() for i in range(n)]+[0]
# if [a,b,c].count(0) >= 2:
#     print("No")
ans = ""

# print("Yes")
for i in range(n):
    if s[i] == "AB":
        if a==1 and b==1 and if s[i+1] =="BC":
            ans+="B"
            b+=1
            a-=1
        elif a<=b:
            ans+="A"
            a+=1
            b-=1
        else:
            ans+="B"
            b+=1
            a-=1
    if s[i] == "AC":
        if a==1 and c==1 and s[i+1] =="BC":
            ans+="C"
            c+=1
            a-=1
        elif a<=c:
            ans+="A"
            a+=1
            c-=1
        else:
            ans+="C"
            c+=1
            a-=1       
    if s[i] == "BC":
        if b==1 and c==1 and s[i+1] =="AC":
            ans+="C"
            c+=1
            b-=1
        elif b<=c:
            ans+="B"
            b+=1
            c-=1
        else:
            ans+="C"
            c+=1
            b-=1   
    if a<0 or b<0 or c<0:
        print("No") 
        exit()
print("Yes")
for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
n=0
s=[]
ans=[]

def dfs(a,b,c,i,root):
    if i==n:
        global ans
        ans=root.copy()
        return True
    l=False
    r=False
    if s[i]=='AB':
        if a>0:
            root.append('B')
            l=dfs(a-1,b+1,c,i+1,root)
            root.pop()
        if not l and b>0:
            root.append('A')
            r=dfs(a+1,b-1,c,i+1,root)
            root.pop()
    elif s[i]=='AC':
        if a>0:
            root.append('C')
            l=dfs(a-1,b,c+1,i+1,root)
            root.pop()
        if not l and c>0:
            root.append('A')
            r=dfs(a+1,b,c-1,i+1,root)
            root.pop()
    else:
        if b>0:
            root.append('C')
            l=dfs(a,b-1,c+1,i+1,root)
            root.pop()
        if not l and c>0:
            root.append('B')
            r=dfs(a,b+1,c-1,i+1,root)
            root.pop()
    return l or r

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

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

Submission status: Runtime Error

Solution code:
def solve(data,siji):
    global henkan
    ans=[]
    for s in siji:
        hiku,tasu=henkan[s]
        if(data[hiku]<data[tasu]):
            temp=hiku
            hiku=tasu
            tasu=temp
        #print(hiku,tasu,data)
        data[hiku]-=1
        data[tasu]+=1
        ans.append(tasu)
        if(data[hiku]<0):
            return [False,[]]
    return [True,ans]

n,a,b,c=map(int,input().split())
siji=[]
henkan=[(0,1),(0,2),(1,2)]
for i in range(n):
    s=input()
    if(s=="AB"):
        siji.append(0)
    elif(s=="AC"):
        siji.append(1)
    else:
        siji.append(2)



data=[a,b,c]
zeros=0
gokei=sum(data)
for i in range(3):
    if(data[i]==0):
        zeros+=1

if(zeros==3):
    koate=solve([0,0,0],siji)
elif(zeros==0):
    kotae=solve([1,1,1],siji)
    #print(kotae)
elif(gokei>=3):
    kotae=solve(data,siji)
elif(gokei==1):
    kotae=solve(data,siji)
elif(gokei==2):
    koate=solve(data,siji)





if(not kotae[0]):
    print("No")
else:
    print("Yes")
    alpha=["A","B","C"]
    for k  in kotae[1]:
        print(alpha[k])

Submission status: Runtime Error

Solution code:
def resolve():
    n,a,b,c = map(int, input().split())
    s = [input() for i in range(n)]
    p = solve(0, s, n, a, b, c, list(), True)
    if p is None:
        p = solve(0, s, n, a, b, c, list(), False)
    
    if p is None:
        print("No")
    else:
        print("Yes")
        for x in p:
            print(x)


def solve(i, s, n, a, b, c, p, plus_first):
    x = s[i]
    if x == "AB":
        if plus_first:
            a += 1
            b -= 1
            p.append("A")
        else:
            a -= 1
            b += 1
            p.append("B")

    elif x == "AC":
        if plus_first:
            a += 1
            c -= 1
            p.append("A")
        else:
            a -= 1
            c += 1
            p.append("C")

    elif x == "BC":
        if plus_first:
            b += 1
            c -= 1
            p.append("B")
        else:
            b -= 1
            c += 1
            p.append("C")

    if a>=0 and b>=0 and c>=0:
        if i+1 < n:
            f = solve(i+1, s, n, a, b, c, p, True)
            if f is not None:
                return f
            
            s = solve(i+1, s, n, a, b, c, p, False)
            if s is not None: 
                return s

        else:
            return p

    p.pop()
    return None

resolve()

Submission status: Runtime Error

Solution code:
from collections import deque
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
ans = []
if (a==0 and  b==0 and c==1) or (a==0 and  b==1 and c==0) or (a==1 and  b==0 and c==0):
    for i in range(n):
        if s[i] == "AB":
            if a >= 1:
                a -= 1
                b += 1
                ans.append("B")
            elif b >= 1:
                a += 1
                b -= 1
                ans.append("A")
            else:
                print("No")
                exit()
        elif s[i] == "AC":
            if a >= 1:
                a -= 1
                c += 1
                ans.append("C")
            elif c >= 1:
                a += 1
                c -= 1
                ans.append("A")
            else:
                print("No")
                exit()
        else:
            if b >= 1:
                b -= 1
                c += 1
                ans.append("C")
            elif c >= 1:
                b += 1
                c -= 1
                ans.append("B")
            else:
                print("No")
                exit()
    print("Yes")
    for i in ans:
        print(i)
    exit()

if (a==0 and  b==1 and c==1) or (a==1 and  b==0 and c==1) or (a==1 and  b==1 and c==0):
    que = deque([[a, b, c, []]])
    if not que:
        print("No")
        exit()
    for i in range(n):
        if not que:
            print("No")
            exit()
        len_que = len(que)
        for _ in range(len_que):
            a, b, c, ans = que.leftpop()
            if s[i] == "AB":
                if a>=1 and b==0:
                    a -= 1
                    b += 1
                    ans.append("B")
                elif b>=1 and a==0:
                    a += 1
                    b -= 1
                    ans.append("A")
                elif a==0 and b==0: continue
            elif s[i] == "AC":
                if a>=1 and c==0:
                    a -= 1
                    c += 1
                    ans.append("C")
                elif c>=1and a==0:
                    a += 1
                    c -= 1
                    ans.append("A")
                elif a==0 and c==0: continue
            else:
                if b >= 1 and c==0:
                    b -= 1
                    c += 1
                    ans.append("C")
                elif c >= 1and b==0:
                    b += 1
                    c -= 1
                    ans.append("B")
                elif b==0 and c==0: continue  
            que.append([a, b, c, ans])
    a, b, c, ans = que.leftpop()
    for i in ans:
        print(i)

if a==0 and  b==0:
    if s[0] == "AB":
        print("No")
        exit()
elif a==0 and  c==0:
    if s[0] == "AC":
        print("No")
        exit()
elif b==0 and  c==0:
    if s[0] == "BC":
        print("No")
        exit()

print("Yes")
for i in range(n):
    if s[i] == "AB":
        if a >= b:
            a -= 1
            b += 1
            print("B")
        else:
            a += 1
            b -= 1
            print("A")
    elif s[i] == "AC":
        if a >= c:
            a -= 1
            c += 1
            print("C")
        else:
            a += 1
            c -= 1
            print("A")
    else:
        if b >= c:
            b -= 1
            c += 1
            print("C")
        else:
            b += 1
            c -= 1
            print("B")

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
ss = [input()for i in range(n)]
ok = True
ans = []
cnt = [[0]*3 for i in range(n)]
for i in range(n-1,-1,-1):
    
    s = ss[i]
    p = s[0]
    q = s[1]
    cnt[i][ord(p) - ord('A')] +=1
    cnt[i][ord(q) - ord('A')] +=1
    if i == 0:continue
    cnt[i-1][0] = cnt[i][0]
    cnt[i-1][1] = cnt[i][1]
    cnt[i-1][2] = cnt[i][2]

    
for s in ss:
    if s == 'AB':
        if a == 0 and b == 0:
            ok = False
            break
        
        if a == 0:
            ans.append('A')
            a += 1
            b -= 1
        elif b == 0:
            ans.append('B')
            b += 1
            a -= 1
        else:
            if cnt[0] > cnt[1]:
                a += 1
                b -= 1
                ans.append('A')
            else:
                ans.append('B')
                a -= 1
                b += 1
    elif s == 'AC':
        if a == 0 and c == 0:
            ok = False
            break
        if a == 0:
            ans.append('A')
            a += 1
            c -= 1
        elif c == 0:
            ans.append('C')
            c += 1
            a -= 1
        else:
            if cnt[0] > cnt[2]:
                a += 1
                c -= 1
                ans.append('A')
            else:
                ans.append('C')
                a -= 1
                c += 1
    else:
        if c == 0 and b == 0:
            ok = False
            break
        
        if b == 0:
            ans.append('B')
            c -= 1
            b += 1
        elif c == 0:
            ans.append('C')
            c += 1
            b -= 1
        else:
            if cnt[2] > cnt[1]:
                c += 1
                b -= 1
                ans.append('C')
            else:
                ans.append('B')
                c -= 1
                b += 1
if ok:
    print('Yes')
    for x in ans:
        print(x)
else:
    print('No')

Submission status: Runtime Error

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

flag = False
ans1 = []

def func(a, b, c, i, ans):
    global flag
    global ans1
    if a<0 or b<0 or c<0:
        return
    if i==n:
        flag = True
        ans1 = ans
        return
    if s[i]=="AB":
        ans.append("A")
        func(a+1, b-1, c, i+1, ans.copy())
        ans = ans[:-1]
        ans.append("B")
        func(a-1, b+1, c, i+1, ans.copy())        
    if s[i]=="AC":
        ans.append("A")
        func(a+1, b, c-1, i+1, ans.copy())
        ans = ans[:-1]
        ans.append("C")
        func(a-1, b, c+1, i+1, ans.copy())     
    if s[i]=="BC":
        ans.append("B")
        func(a, b+1, c-1, i+1, ans.copy())
        ans = ans[:-1]
        ans.append("C")
        func(a, b-1, c+1, i+1, ans.copy())
    
        
func(a,b,c,0,[])
if flag:
    print("Yes")
    [print(ans_) for ans_ in ans1]
else:
    print("No")

Submission status: Runtime Error

Solution code:


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

abc = {"A": a, "B": b, "C": c}
ans = [None] * n
# 3つある→ aをなくしにかかる
# 2つある→ bを1にしにかかる
# 0, 1, c→ c>=2のとき、
No = False
for i,s in enumerate(ss):
    zero = [i for i, item in abc.items() if item==0]
    one = [i for i, item in abc.items() if item==1]
    two = [i for i, item in abc.items() if item>=2]
#     print(s)
#     print(zero, one, two)
    if len(zero)==0:
        if s in ["AB", "AC"]:
            ans[i] = "A"
            abc["A"] += 1
            abc[s[1]] -= 1
        else:
            ans[i] = "C"
            abc["C"] += 1
            abc["B"] -= 1
    elif len(zero)==1 and len(two)>=1:
        z = zero[0]
        t = two[0]
        if z in s:
            # zに加える
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
        else:
            # twoから引く
            tmp = s.replace(t, "")
            ans[i] = tmp
            abc[tmp] += 1
            abc[t] -= 1
    elif len(zero)==1 and len(one)==2:
        z = zero[0]
        if s[0] in one and s[1] in one:
            # １手先を見て、
            ns = ss[i+1]
            if z not in ns:
                # 次も同じなのでどちらでもよい
                ans[i] = one[0]
                abc[one[0]] += 1
                abc[one[1]] -= 1
            else:
                # nsのzでないほうに加える
                tmp = ns.replace(z, "")
                ans[i] = tmp
                abc[tmp] += 1
                abc[s.replace(tmp, "")] -= 1
        else:
            # 0に加える
            z = zero[0]
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
    else:
        # 0が２つ以上
        assert len(zero) >= 2
        if len(zero)==3:
            No = True
            break
        elif s[0] in zero and s[1] in zero:
            No = True
            break
        else:
            # zに加える
            z = (set(s) & set(zero)).pop()
            ans[i] = z
            abc[z] += 1
            abc[s.replace(z, "")] -= 1
            
if No:
    print("No")
else:
    print("Yes")
    print("\n".join(ans))

Submission status: Runtime Error

Solution code:
import sys 
sys.setrecursionlimit(60000)

N, A, B, C = map(int, input().split(" "))
commands = []
data = {
    'A': A,
    'B': B,
    'C': C
}
ans = [] 

def bt( i ) :
    if i >= N :
        return True
    
    for x, y in [ commands[i], commands[i][::-1] ] :
        if data[x] == 0 :
            continue
        data[x] -= 1
        data[y] += 1
        ans.append(y)
        if bt(i+1 ) :
            return True
        ans.pop() 
        data[x] += 1
        data[y] -= 1
    
    return False

if A >=1 and B >= 1 and C >= 1 :
    print("Yes")
    for _ in range(N) :
        command = input()
        if data[command[0]] > data[command[1]] :
            data[command[0]] -= 1
            data[command[1]] += 1
            print(command[1])
        else :
            data[command[0]] += 1
            data[command[1]] -= 1
            print(command[0]) 
else :
    for _ in range(N) :
        commands.append(input())
    
    if bt(0) :
        print("Yes")
        for c in ans :
            print(c)
    else :
        print("No")

Submission status: Runtime Error

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

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

if a+b==0 or b+c==0 or a+c==0:
  print("No")
else:
  print("Yes")
  for one in lis:
    i = one[0]
    j = one[1]
    if now[i] == 0:
  	  print(i)
    else:
      print(j)

Submission status: Runtime Error

Solution code:
#include <cstdio>
#include <iostream>
#include <string>
using namespace std; 
int main() {
  string op; 
  long n, a, b, c, g = 0; 
  scanf("%ld%ld%ld%ld", &n, &a, &b, &c); 
  char tableau[200000] = {'S'}; 
  for (long i= 0; i< n; i ++) {
    cin >> op; 
    if (op == "AB") {
    	if (a >= b) {
          b ++; 
          a--; 
          tableau[g] = 'B';  
        }
        else {
          a ++; 
          b --; 
          tableau[g] = 'A'; 
        }
    }
    else if (op == "BC") {
    	if (b >= c) {
          c ++; 
          b --; 
          tableau[g] = 'C';  
        }
        else {
          b ++; 
          c --; 
          tableau[g] = 'B';  
        }
    }
    else {
    	if (a >= c) {
          c ++; 
          a--; 
          tableau[g] = 'C'; 
        }
        else {
          a ++; 
          c --; 
          tableau[g] = 'A'; 
        }
    }
    g ++; 
  }
  if ((a <= 0) || (b <= 0) || (c <= 0)) {
    printf("%s\n", "No"); 
  }
  else {
    printf("%s\n", "Yes"); 
    for (long i = 0; i < g; i ++) {
      cout << tableau[i] << '\n'; 
    }
  }
  return 0; 
}

Submission status: Runtime Error

Solution code:
idx = 'ABC'
N, *hi = map(int, input().split())
lo = list(hi)

sets = [set() for _ in range(3)]
ans = []
for I in range(N):
	i, j = map(idx.index, input())
	if hi[i] == hi[j] == 0:
		print('No')
		raise SystemExit

	if hi[i] == 0:
		lo[i] = hi[i] = 1
		hi[j] -= 1
		ans.append(idx[i])
	elif hi[j] == 0:
		lo[j] = hi[j] = 1
		hi[i] -= 1
		ans.append(idx[j])
	else:
		ans.append('')
		sets[i].add(I)
		sets[j].add(I)
		for k in (i, j):
			if lo[k] == 0:
				lo[k] += 1
				hi[k] -= 1

				while True:
					J = sets[k].pop()
					if ans[J] != '': continue
					ans[J] = idx[k]
					break
			else:
				lo[k] -= 1
				hi[k] += 1

for i, s in enumerate(sets):
	for j in s:
		if ans[j] == '':
			ans[j] = idx[i]

print('Yes')
print('\n'.join(ans))

Submission status: Runtime Error

Solution code:
def play(i, a, b, c, ans):
  if i==n:
    if a>=0 and b>=0 and c>=0:
      return 1, ans
    else:
      return 0, ans
    
  if X[i] == 'AB':
    if play(i+1, a+1, b-1, c,ans)[0] == 1:
      ans.append('A')
      return 1, ans
    elif play(i+1, a-1, b+1, c, ans)[0] ==1:
      ans.append('B')
      return 1, ans
    else:
      return 0, ans
  elif X[i] == 'AC':
    if play(i+1, a+1, b, c-1,ans)[0] == 1:
      ans.append('A')
      return 1, ans
    elif play(i+1, a-1, b, c+1, ans)[0] == 1:
      ans.append('C')
      return 1, ans
    else:
      return 0, ans
  else:
    if play(i+1, a, b+1, c-1,ans)[0] == 1:
      ans.append('B')
      return 1, ans
    elif play(i+1, a, b-1, c+1, ans)[0] == 1:
      ans.append('C')
      return 1, ans
    else:
      return 0, ans

if __name__=="__main__":
  n, a, b, c = map(int,input().split())
  X = []
  ans = []
  for i in range(n):
    X.append(input())
  result = play(0, a, b, c, ans)
  if result[0] ==1:
    print('Yes')
    for i in range(len(result[1])):
      print(result[1][i])
  else:
    print('No')

Submission status: Runtime Error

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

def check(i, A, B, C):
    if A < 0 or B < 0 or C < 0:
        return False
    if Q[i] == 'AB':
        return not (A == 0 and B == 0)
    if Q[i] == 'AC':
        return not (A == 0 and C == 0)
    return not (B == 0 and C == 0)

def solve(L, A, B, C):
    if len(L) == N:
        return L
    i = len(L)
    if i == N-1:
        if Q[i] == 'AB':
            if A == 0 and B == 0:
                return 'No'
            if A == 0:
                return solve(L + ['A'], A+1, B-1, C)
            return solve(L + ['B'], A-1, B+1, C)
        if Q[i] == 'AC':
            if A == 0 and C == 0:
                return 'No'
            if A == 0:
                return solve(L + ['A'], A+1, B, C-1)
            return solve(L + ['C'], A-1, B, C+1)
        if Q[i] == 'BC':
            if B == 0 and C == 0:
                return 'No'
            if B == 0:
                return solve(L + ['B'], A, B+1, C-1)
            return solve(L + ['C'], A, B-1, C+1)
    if not check(i, A, B, C):
        return 'No'
    if Q[i] == 'AB':
        if check(i+1, A+1, B-1, C):
            return solve(L + ['A'], A+1, B-1, C)
        if check(i+1, A-1, B+1, C):
            return solve(L + ['B'], A-1, B+1, C)
        return 'No'
    if Q[i] == 'AC':
        if check(i+1, A+1, B, C-1):
            return solve(L + ['A'], A+1, B, C-1)
        if check(i+1, A-1, B, C+1):
            return solve(L + ['C'], A-1, B, C+1)
        return 'No'
    if Q[i] == 'BC':
        if check(i+1, A, B+1, C-1):
            return solve(L + ['B'], A, B+1, C-1)
        if check(i+1, A, B-1, C+1):
            return solve(L + ['C'], A, B-1, C+1)
        return 'No'

ans = solve([], A, B, C)

if ans == 'No':
    print('No')
else:
    print('Yes')
    for i in ans:
        print(i)

Submission status: Runtime Error

Solution code:
def play(i, a, b, c, ans):
  if i==n:
    if a>=0 and b>=0 and c>=0:
      return 1, ans
    else:
      return 0, ans
    
  if X[i] == 'AB':
    if play(i+1, a+1, b-1, c,ans)[0] == 1:
      ans.append('A')
      return 1, ans
    elif play(i+1, a-1, b+1, c, ans)[0] ==1:
      ans.append('B')
      return 1, ans
    else:
      return 0, ans
  elif X[i] == 'AC':
    if play(i+1, a+1, b, c-1,ans)[0] == 1:
      ans.append('A')
      return 1, ans
    elif play(i+1, a-1, b, c+1, ans)[0] == 1:
      ans.append('C')
      return 1, ans
    else:
      return 0, ans
  else:
    if play(i+1, a, b+1, c-1,ans)[0] == 1:
      ans.append('B')
      return 1, ans
    elif play(i+1, a, b-1, c+1, ans)[0] == 1:
      ans.append('C')
      return 1, ans
    else:
      return 0, ans

if __name__=="__main__":
  n, a, b, c = map(int,input().split())
  X = []
  ans = []
  for i in range(n):
    X.append(input())
  result = play(0, a, b, c, ans)
  if result[0] ==1:
    print('Yes')
    for i in range(len(result[1])):
      print(result[1][len(result[1])-1-i])
  else:
    print('No')

Submission status: Runtime Error

Solution code:
def cal():
    n, a, b, c = int(input().split(" "))
    answer = []
    flag = 0
    for i in range(n):
        s = input()
        if s == 'AB':
            if a >= b:
                b += 1
                c -= 1
                answer.append('B')
            else:
                a += 1
                c -= 1
                answer.append('A')
        elif s == 'AC':
            if a >= c:
                c += 1
                b -= 1
                answer.append('C')
            else:
                a += 1
                b -= 1
                answer.append('A')
        elif s == 'BC':
            if b >= c:
                c += 1
                a -= 1
                answer.append('C')
            else:
                b += 1
                a -= 1
                answer.append('B')
        if a < 0 or b < 0 or c < 0:
            flag = 1
            break
    if flag == 1:
        print('No')
    else:
        print("Yes")
        for each in answer:
            print(each)



cal()

Submission status: Runtime Error

Solution code:
def play(i, a, b, c, ans):
  if i==n:
    if a>=0 and b>=0 and c>=0:
      return 1, ans
    else:
      return 0, ans
    
  if X[i] == 'AB':
    if play(i+1, a+1, b-1, c,ans)[0] == 1:
      ans.append('A')
      return 1, ans
    elif play(i+1, a-1, b+1, c, ans)[0] ==1:
      ans.append('B')
      return 1, ans
    else:
      return 0, ans
  elif X[i] == 'AC':
    if play(i+1, a+1, b, c-1,ans)[0] == 1:
      ans.append('A')
      return 1, ans
    elif play(i+1, a-1, b, c+1, ans)[0] == 1:
      ans.append('C')
      return 1, ans
    else:
      return 0, ans
  else:
    if play(i+1, a, b+1, c-1,ans)[0] == 1:
      ans.append('B')
      return 1, ans
    elif play(i+1, a, b-1, c+1, ans)[0] == 1:
      ans.append('C')
      return 1, ans
    else:
      return 0, ans

if __name__=="__main__":
  n, a, b, c = map(int,input().split())
  X = []
  ans = []
  for i in range(n):
    X.append(input())
  result = play(0, a, b, c, ans)
  if result[0] ==1:
    print('Yes')
    for i in range(len(result[1])):
      print(result[1][i])
  else:
    print('NO')

Submission status: Runtime Error

Solution code:
result = []
def calc(a, b, c, i):
    if i >= N:
        if a < 0 or b < 0 or c < 0:
            return False
        return True
    command = s[i]
    if command == "AB":
        if calc(a+1, b-1, c, i+1):
            result.append("A")
            return True
        if calc(a-1, b+1, c, i+1):
            result.append("B")
            return True
        return False
    elif command == "AC":
        if calc(a+1, b, c-1, i+1):
            result.append("A")
            return True
        if calc(a-1, b, c+1, i+1):
            result.append("C")
            return True
        return False
    elif command == "BC":
        if calc(a, b-1, c+1, i+1):
            result.append("C")
            return True
        if calc(a, b+1, c-1, i+1):
            result.append("B")
            return True
        return False
def main():
    global N, s
    N, A, B, C = map(int, input().split())
    s = [input() for _ in range(N)]
    if calc(A, B, C, 0):
        print("Yes")
        for c in result[::-1]:
            print(c)
    else:
        print("No")
main()

Submission status: Runtime Error

Solution code:
I=lambda:list(map(int,input().split()))
n,a,b,c=I()
for i in range(n):
  s=input()
  if 'A' in s:
    a=-1
  if 'B' in s:
  	b-=1
   if 'c' in s:
    c-=1
te=[a,b,c]
an=0
for i in te:
  i<0:
    an+=1
if an>1:
  print('No')

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())
k=[]
if a+b+c==0:
  print('No')
elif a+b+c==1:
  if a==1:
    current=0
  if b==1:
    current=1
  if c==1:
    current=2
  breakflag=0
  for i in range(n):
    s=input()
    if s=='AB':
      if current==2:
        breakflag=1
      else:
        current=1-current
        k.append(current)
    if s=='AC':
      if current==1:
        breakflag=1
      else:
        current=2-current
        k.append(current)
    if s=='BC':
      if current==0:
        breakflag=1
      else:
        current=3-current
        k.append(current)
    if breakflag==1:
      print('No')
      break
    if i==n-1:
      print('Yes')
      for j in range(n):
        if k[j]==0:
          print('A')
        elif k[j]==1:
          print('B')
        else:
          print('C')
elif a+b+c==2:
  s=input()
  for i in range(n-1):
    s2=input()
    if s=='AB':
      if a==0 and b==0:
        breakflag=1
      elif a>b:
        a-=1
        b+=1
        k.append('A')
      elif b>a:
        b-=1
        a+=1
        k.append('B')
      else:
        if s2=='AC':
          a+=1
          b-=1
          k.append('A')
        else:
          a-=1
          b+=1
          k.append('B')
    elif s=='AC':
      if a==0 and c==0:
        breakflag=1
      elif a>c:
        a-=1
        c+=1
        k.append('A')
      elif c>a:
        c-=1
        a+=1
        k.append('C')
      else:
        if s2=='AB':
          a+=1
          c-=1
          k.append('A')
        else:
          a-=1
          c+=1
          k.append('C')
    elif s=='BC':
      if b==0 and c==0:
        breakflag=1
      elif b>c:
        b-=1
        c+=1
        k.append('B')
      elif c>b:
        c-=1
        b+=1
        k.append('C')
      else:
        if s2=='AB':
          b+=1
          c-=1
          k.append('B')
        else:
          b-=1
          c+=1
          k.append('C')
    if breakflag==1:
      print('No')
      break
    s=s2.copy()
  if breakflag==0:
    if s=='AB':
      if a==0 and b==0:
        print('No')
        breakflag=1
      elif a>=b:
        k.append('A')
      else:
        k.append('B')
    elif s=='AC':
      if a==0 and c==0:
        print('No')
        breakflag=1
      elif a>=c:
        k.append('A')
      else:
        k.append('C')
    else:
      if b==0 and c==0:
        print('No')
        breakflag=1
      elif b>=c:
        k.append('B')
      else:
        k.append('C')  
  if breakflag==0:
    print('Yes')
    for i in k:
      print(i)
else:
  breakflag=0
  for i in range(n):
    s=input()
    if s=='AB':
      if a==0 and b==0:
        breakflag=1
      elif a<b:
        a+=1
        b-=1
        k.append('A')
      else:
        b+=1
        a-=1
        k.append('B')
    if s=='AC':
      if a==0 and c==0:
        breakflag=1
      elif a<c:
        a+=1
        c-=1
        k.append('A')
      else:
        c+=1
        a-=1
        k.append('C')
    if s=='BC':
      if b==0 and c==0:
        breakflag=1
      elif b<c:
        b+=1
        c-=1
        k.append('B')
      else:
        c+=1
        b-=1
        k.append('C')
    if breakflag==1:
      print('No')
      break
  if breakflag==0:
    print('Yes')
    for i in k:
      print(i)

Submission status: Runtime Error

Solution code:
I=lambda:list(map(int,input().split()))
n,a,b,c=I()
for i in range(n):
  s=input()
  if 'A' in s:
    a=-=1
  if 'B' in s:
  	b-=1
   if 'c' in s:
    c-=1
te=[a,b,c]
an=0
for i in te:
  i<0:
    an+=1
if an>1:
  print('No')