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
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

def plus_minus(s,t):
    for c in ['A','B','C']:
        if (c in s) and (c in t):
            plus = c
        if (c in s) and (c not in t):
            minus = c
    return plus, minus 

N,A,B,C = LI()
s = ['0']*N
for i in range(N):
    s[i] = str(input())

d = defaultdict(int)
d['A'] = A
d['B'] = B
d['C'] = C

if A+B+C == 0:
    print('No')
elif A+B+C == 1:
    ans = []
    for i in range(N):
        if d[s[i][0]] == d[s[i][1]] == 0:
            print('No')
            exit()
        else:
            if d[s[i][0]]:
                d[s[i][0]] -= 1
                d[s[i][1]] += 1
                ans.append(s[i][1])
            else:
                d[s[i][1]] -= 1
                d[s[i][0]] += 1
                ans.append(s[i][0])
    print('Yes')
    for a in ans:
        print(a)
else:
    if d[s[0][0]] == d[s[0][1]] == 0:
        print('No')
    else:
        print('Yes')
        for i in range(N):
            if d[s[i][0]] == 0:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                print(s[i][0])
            elif d[s[i][1]] == 0:
                d[s[i][1]] += 1
                d[s[i][0]] -= 1
                print(s[i][1])
            elif d[s[i][0]] == d[s[i][1]] == 1:
                if i != N-1:
                    p,m = plus_minus(s[i],s[i+1])
                    d[p] += 1
                    d[m] -= 1
                    print(p)
                else:
                    d[s[i][0]] += 1
                    d[s[i][1]] -= 1
                    print(s[i][0])
            else:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                print(s[i][0])

Submission status: Runtime Error

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

ABC = [A, B, C]
result = ['N'] * (N + 1)


def dfs(i, s):  # iを深さ、sを選択（0,1）とする
    if result[N] == 'Yes':
        return
    if i == N:
        result[N] = 'Yes'
        return

    if si[i] == 'AB' and s == 0:
        ABC[0] = ABC[0] + 1
        ABC[1] = ABC[1] - 1
        result[i] = 'A'
    elif si[i] == 'AB' and s == 1:
        ABC[0] = ABC[0] - 1
        ABC[1] = ABC[1] + 1
        result[i] = 'B'
    elif si[i] == 'AC' and s == 0:
        ABC[0] = ABC[0] + 1
        ABC[2] = ABC[2] - 1
        result[i] = 'A'
    elif si[i] == 'AC' and s == 1:
        ABC[0] = ABC[0] - 1
        ABC[2] = ABC[2] + 1
        result[i] = 'C'
    elif si[i] == 'BC' and s == 0:
        ABC[1] = ABC[1] + 1
        ABC[2] = ABC[2] - 1
        result[i] = 'B'
    elif si[i] == 'BC' and s == 1:
        ABC[1] = ABC[1] - 1
        ABC[2] = ABC[2] + 1
        result[i] = 'C'
    if ABC[0] < 0 or ABC[1] < 0 or ABC[2] < 0:
        return

    dfs(i + 1, 0)
    dfs(i + 1, 1)
    dfs(i, 1)


dfs(0, 0)

if result[N] == 'Yes':
    print('Yes')
    for i in range(N):
        print(result[i])
else:
    print('No')

Submission status: Runtime Error

Solution code:

N, a, b, c, *S = open(0).read().split()
N, a, b, c = map(int, (N, a, b, c))

flg = a*b*c == 0 and a+b+c=2

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if flg and a*b==1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            elif s_ == 'BC':
                ans.append('B')
                a -= 1
                b += 1
                continue

        ans.append('B' if a > b else 'A')
        if a > b:
            a -= 1
            b += 1
        else:
            a += 1
            b -= 1

    elif s == 'AC':
        if flg and a*c == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            elif s_ == 'BC':
                ans.append('C')
                a -= 1
                c += 1
                continue

        ans.append('C' if a > c else 'A')
        if a > c:
            a -= 1
            c += 1
        else:
            a += 1
            c -= 1

    elif s == 'BC':
        if flg and b*c == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
            elif s_ == 'AC':
                ans.append('C')
                b -= 1
                c += 1
                continue

        ans.append('C' if b > c else 'B')
        if b > c:
            b -= 1
            c += 1
        else:
            b += 1
            c -= 1

    if a < 0 or b < 0 or c < 0:
        print('No')
        exit()


print('Yes')
[print(r) for r in ans]

Submission status: Runtime Error

Solution code:
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
# if s.count(0) <= 1 and sum(s) >= 3:
#     for i,t in enumerate(T):
#         nxt = [0,1,2]
#         nxt.remove(t)
#         if s[nxt[0]] < s[nxt[1]]:
#             ans[i] = ABC[nxt[0]]
#             s[nxt[0]] += 1
#             s[nxt[1]] -= 1
#         else:
#             ans[i] = ABC[nxt[1]]
#             s[nxt[0]] -= 1
#             s[nxt[1]] += 1
#     print('Yes')
#     print('\n'.join(ans))
if sum(s) == 2:
    if s.count(0) == 1:
        now = s.index(0)
        i = 0
    else:
        if T[0] != s.index(2):
            now = T[0]
            i = 1
            nxt = [0,1,2]
            nxt.remove(T[0])
            nxt.remove(s.index(2))
            ans[0] = ABC[nxt]
        else:
            print('No')
            exit(0)
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = ABC[now]
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        if T[i] == now:
            for j in range(3):
                if j not in [now,T[i]]:ans[i] = ABC[j]
        else:
            ans[i] = ABC[now]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i,a,b) for(i=a;i<b;i++)
#define foe(i,a,b) for(i=a;i<=b;i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector <long long int>
#define pii pair <int,int>
#define pll pair <long long int, long long int>
#define vpii vector< pair<int,int> >
#define vpll vector < pair <long long int,long long int> >
#define boost ios::sync_with_stdio(false); cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

const int MAX = 1e5 + 5;
bool dp[MAX][5][5][5];
int main()
{
	boost;
	int n, a, b, c;
	cin >> n >> a >> b >> c;
	memset(dp, 0, sizeof dp);
	
	for(int i = 0; i < 5; i++) {
		for(int j = 0; j < 5; j++) {
			for(int k = 0; k < 5; k++) {
				if(a >= i && b >= j && c >= k)
				dp[0][i][j][k] = 1;
			}
		}
	}
	vector <string> v;
	for(int i = 1; i <= n; i++) {
		string s;
		cin >> s;
		v.pb(s);
		if(s == "AB") {
			// A++, B--
			for(int j = 0; j < 4; j++) {
				for(int k = 1; k < 5; k++) {
					for(int l = 0; l < 5; l++) {
						if(dp[i - 1][j][k][l])
						dp[i][j + 1][k - 1][l] = 1;
					}
				}
			}
			// For A--,B++
			for(int j = 1; j < 5; j++) {
				for(int k = 0; k < 4; k++) {
					for(int l = 0; l < 5; l++) {
						if(dp[i - 1][j][k][l])
						dp[i][j - 1][k + 1][l] = 1;
					}
				}
			}
		}
		else if(s == "AC") {
			// A++, C--
			for(int j = 0; j < 4; j++) {
				for(int k = 0; k < 5; k++) {
					for(int l = 1; l < 5; l++) {
						if(dp[i - 1][j][k][l])
						dp[i][j + 1][k][l - 1] = 1;
					}
				}
			}
			// For A--, C++
			for(int j = 1; j < 5; j++) {
				for(int k = 0; k < 5; k++) {
					for(int l = 0; l < 4; l++) {
						if(dp[i - 1][j][k][l])
						dp[i][j - 1][k][l + 1] = 1;
					}
				}
			}
		}
		else {
			// B++, C--
			for(int j = 0; j < 5; j++) {
				for(int k = 0; k < 4; k++) {
					for(int l = 1; l < 5; l++) {
						if(dp[i - 1][j][k][l])
						dp[i][j][k + 1][l - 1] = 1;
					}
				}
			}
			// B--,C++
			for(int j = 0; j < 5; j++) {
				for(int k = 1; k < 5; k++) {
					for(int l = 0; l < 4; l++) {
						if(dp[i - 1][j][k][l])
						dp[i][j][k - 1][l + 1] = 1;
					}
				}
			}
		}
	}
	bool ok = 0;
	for(int i = 0; i < 5; i++) {
		for(int j = 0; j < 5; j++) {
			for(int k = 0; k < 5; k++) {
				if(dp[n][i][j][k]) {
					ok = 1;
					break;
				}
			}
		}
	}
	if(!ok) cout << "No";
	else {
		cout << "Yes\n";
		string ans = "";
		int ca = 4, cb = 4, cc = 4;
		for(int i = n; i > 0; i--) {
			string s = v[i - 1];
			bool ok = 0;
			if(s == "AB") {
				for(int j = ca; j >= 0; j--) {
					for(int k = cb; k >= 0; k--) {
						for(int l = cc; l >= 0; l--) {
							if(dp[i][j][k][l]) {
								if(j < 4 && k > 0 && dp[i - 1][j + 1][k - 1][l]) {
									ans += "B";
									ca = j + 1, cb = k - 1, cc = l;
									ok = 1;
									break;
								}
								if(k < 4 && j > 0 && dp[i - 1][j - 1][k + 1][l]) {
									ans += "A";
									ca = j - 1, cb = k + 1, cc = l;
									ok = 1;
									break;
								}
							}
						}
						if(ok) break;
					}
					if(ok) break;
				}
			}
			else if(s == "BC") {
				for(int j = ca; j >= 0; j--) {
					for(int k = cb; k >= 0; k--) {
						for(int l = cc; l >= 0; l--) {
							if(dp[i][j][k][l]) {
								if(k < 4 && l > 0 && dp[i - 1][j][k + 1][l - 1]) {
									ans += "C";
									ca = j, cb = k + 1, cc = l - 1;
									ok = 1;
									break;
								}
								if(k > 0 && l < 4 && dp[i - 1][j][k - 1][l + 1]) {
									ans += "B";
									ca = j, cb = k - 1, cc = l + 1;
									ok = 1;
									break;
								}
							}
						}
						if(ok) break;
					}
					if(ok) break;
				}
			}
			else {
				for(int j = ca; j >= 0; j--) {
					for(int k = cb; k >= 0; k--) {
						for(int l = cc; l >= 0; l--) {
							if(dp[i][j][k][l]) {
								if(j < 4 && l > 0 && dp[i - 1][j + 1][k][l - 1]) {
									ans += "C";
									ca = j + 1, cb = k, cc = l - 1;
									ok = 1;
									break;
								}
								if(j > 0 && l < 4 && dp[i - 1][j - 1][k][l + 1]) {
									ans += "A";
									ca = j - 1, cb = k, cc = l + 1;
									ok = 1;
									break;
								}
							}
						}
						if(ok) break;
					}
					if(ok) break;
				}
			}
		}
		reverse(all(ans));
		for(char c : ans)
		cout << c << '\n';
	}
}

Submission status: Runtime Error

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

ABC = [A, B, C]
result = ['N'] * (N + 1)


def dfs(i, s):  # iを深さ、sを選択（0,1）とする
    if i == N:
        result[N] = 'Yes'
        return

    if si[i] == 'AB' and s == 0:
        ABC[0] = ABC[0] + 1
        ABC[1] = ABC[1] - 1
        result[i] = 'A'
    elif si[i] == 'AB' and s == 1:
        ABC[0] = ABC[0] - 1
        ABC[1] = ABC[1] + 1
        result[i] = 'B'
    elif si[i] == 'AC' and s == 0:
        ABC[0] = ABC[0] + 1
        ABC[2] = ABC[2] - 1
        result[i] = 'A'
    elif si[i] == 'AC' and s == 1:
        ABC[0] = ABC[0] - 1
        ABC[2] = ABC[2] + 1
        result[i] = 'C'
    elif si[i] == 'BC' and s == 0:
        ABC[1] = ABC[1] + 1
        ABC[2] = ABC[2] - 1
        result[i] = 'B'
    elif si[i] == 'BC' and s == 1:
        ABC[1] = ABC[1] - 1
        ABC[2] = ABC[2] + 1
        result[i] = 'C'
    if ABC[0] < 0 or ABC[1] < 0 or ABC[2] < 0:
        return

    dfs(i + 1, 0)
    dfs(i + 1, 1)


dfs(0, 0)
dfs(0, 1)

if result[N] == 'Yes':
    print('Yes')
    for i in range(N):
        print(result[i])
else:
    print('No')

Submission status: Runtime Error

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

ans=[]

flag=True

for i in range(n):
    if s[i]=='AB':
        if a==0 and b==0:
            flag=False
            break
        if a==1 and s[i+1]=='AC' or a==0:
            ans.append('A')
            a+=1
            b-=1
        else:
            ans.append('B')
            a-=1
            b+=1
    elif s[i]=='AC':
        if a==0 and c==0:
            flag=False
            break
        if a==1 and s[i+1]=='AB' or a==0:
            ans.append('A')
            a+=1
            c-=1
        else:
            ans.append('C')
            a-=1
            c+=1
    else:
        if c==0 and b==0:
            flag=False
            break
        if c==1 and s[i+1]=='AC' or c==0:
            ans.append('C')
            c+=1
            b-=1
        else:
            ans.append('B')
            c-=1
            b+=1
if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Submission status: Runtime Error

Solution code:
def solve(data,siji,hairyo=False):
    global henkan
    ans=[]
    cnt=0
    for s in siji:
        cnt+=1
        hiku,tasu=henkan[s]
        if(not hairyo):
            if(data[hiku]<data[tasu]):
                temp=hiku
                hiku=tasu
                tasu=temp
        else:
            if(data[hiku]==data[tasu] and cnt<len(siji)):
                dotira=-1
                for i in range(cnt,len(siji)):
                    if(siji[i]!=s):
                        dotira=siji[i]
                        break
                if(dotira!=-1):
                    if(dotira+s==1 or (dotira==0 and s==2) ):
                        #print("反転",hiku,tasu)
                        temp=hiku
                        hiku=tasu
                        tasu=temp
            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,[]]
    #print(ans)
    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):
    print("kore")
    kotae=solve(data,siji,True)


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

ans=[]

flag=True

for i in range(n):
    if s[i]=='AB':
        if a==0 and b==0:
            flag=False
            break
        if a==1 and b==1 and s[i+1]=='AC' or a==0:
            ans.append('A')
            a+=1
            b-=1
        else:
            ans.append('B')
            a-=1
            b+=1
    elif s[i]=='AC':
        if a==0 and c==0:
            flag=False
            break
        if a==1 and c==1 and s[i+1]=='AB' or a==0:
            ans.append('A')
            a+=1
            c-=1
        else:
            ans.append('C')
            a-=1
            c+=1
    else:
        if c==0 and b==0:
            flag=False
            break
        if c==1 and b==1 and s[i+1]=='AC' or c==0:
            ans.append('C')
            c+=1
            b-=1
        else:
            ans.append('B')
            c-=1
            b+=1
if flag:
    print("Yes")
    for i in ans:
        print(i)
else:
    print("No")

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().split())
l = []
i = 0
while i < n:
    s = input()
    if s == "AB" and a == 0 and b == 0:
        print(No)
        exit()
    elif s == "BC" and b == 0 and c == 0:
        print(No)
        exit()
    elif s == "CA" and c == 0 and a == 0:
        print(No)
        exit()
    elif s == "AB" and a == 1 and b == 0:
        l.append("B")
        a, b = b, a
        i += 1
    elif s == "BC" and b == 1 and c == 0:
        l.append("C")
        b, c = c, b
        i += 1
    elif s == "CA" and c == 1 and a == 0:
        l.append("A")
        c, a = a, c
        i += 1
    elif s == "AB" and a == 1 and b == 1:
        if c > 0:
            l.append("A")
            a = 2
            b = 0
        else:
            i += 1
            t = input()
            if t == "AB":
                l.append("A")
                l.append("B")
            elif t == "BC":
                l.append("B")
                l.append("C")
                a = 1
                c = 1
            else:
                l.append("A")
                l.append("C")
                b = 1
                c = 1
        i += 1
    elif s == "BC" and b == 1 and c == 1:
        if a > 0:
            l.append("B")
            b = 2
            c = 0
        else:
            i += 1
            t = input()
            if t == "BC":
                l.append("B")
                l.append(C)
            elif t == "CA":
                l.append("C")
                l.append("A")
                b = 1
                a = 1
            else:
                l.append("B")
                l.append("A")
                c = 1
                a = 1
        i += 1
    elif s == "CA" and c == 1 and a == 1:
        if b > 0:
            l.append("B")
            c = 2
            a = 0
        else:
            i += 1
            t = input()
            if t == "BC":
                l.append("B")
                l.append("C")
            elif t == "CA":
                l.append("C")
                l.append("A")
                c = 1
                a = 1
            else:
                l.append("B")
                l.append("A")
                c = 1
                a = 1
        i += 1
    elif s == "AB" and a == 0 and b > 1:
        l.append("A")
        a = 1
        b -= 1
        i += 1
    elif s == "BC" and b == 0 and c > 1:
        l.append("B")
        b = 1
        c -= 1
        i += 1
    elif s == "CA" and c == 0 and a > 1:
        l.append("C")
        c = 1
        a -= 1
        i += 1
    elif s == "AB":
        l.append("A")
        a += 1
        b -= 1
        i += 1
    elif s == "BC":
        l.append("B")
        b += 1
        c -= 1
        i += 1
    else:
        l.append("C")
        c += 1
        a -= 1
        i += 1
for i in range(len(l)):
    print(l[i])

Submission status: Runtime Error

Solution code:
def solve(data,siji,hairyo=False):
    global henkan
    ans=[]
    cnt=0
    for s in siji:
        cnt+=1
        hiku,tasu=henkan[s]
        if(not hairyo):
            if(data[hiku]<data[tasu]):
                temp=hiku
                hiku=tasu
                tasu=temp
        else:
            if(data[hiku]==data[tasu] and cnt<len(siji)):
                dotira=-1
                for i in range(cnt,len(siji)):
                    if(siji[i]!=s):
                        dotira=siji[i]
                        break
                if(dotira!=-1):
                    if(dotira+s==1 or (dotira==0 and s==2) ):
                        #print("反転",hiku,tasu)
                        temp=hiku
                        hiku=tasu
                        tasu=temp
            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,[]]
    #print(ans)
    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):
    #print("kore")
    kotae=solve(data,siji,True)


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:
N, A, B, C = map(int, input().split())
dic = {'A':A,'B':B,'C':C}
S = [input() for _ in range(N)]

def solve(N,dic,S):
    ans = ['Yes']
    for i in range(N):
        if dic[S[i][0]]>dic[S[i][1]]:
            dic[S[i][0]]-=1
            dic[S[i][1]]+=1
            ans.append(S[i][1])
        elif dic[S[i][0]]<dic[S[i][1]] or dic[S[i][0]]>1:
            dic[S[i][1]]-=1
            dic[S[i][0]]+=1
            ans.append(S[i][0])
        elif dic[S[i][0]]==0:
            return ['No']
        else:
            if S[i][0] in S[i+1]:
                dic[S[i][0]]+=1
                dic[S[i][1]]-=1
                ans.append(S[i][0])
            else:
                dic[S[i][1]]+=1
                dic[S[i][0]]-=1
                ans.append(S[i][1])
    return ans
print(*solve(N,dic,S),sep='\n')

Submission status: Runtime Error

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

ABC = [A, B, C]
result = ['N'] * (N + 1)


def dfs(i, s):  # iを深さ、sを選択（0,1）とする
    if i == N:
        result[N] = 'Yes'
        return

    if si[i] == 'AB' and s == 0:
        ABC[0] = ABC[0] + 1
        ABC[1] = ABC[1] - 1
        result[i] = 'A'
    elif si[i] == 'AB' and s == 1:
        ABC[0] = ABC[0] - 1
        ABC[1] = ABC[1] + 1
        result[i] = 'B'
    elif si[i] == 'AC' and s == 0:
        ABC[0] = ABC[0] + 1
        ABC[1] = ABC[2] - 1
        result[i] = 'A'
    elif si[i] == 'AC' and s == 1:
        ABC[0] = ABC[0] - 1
        ABC[1] = ABC[2] + 1
        result[i] = 'C'
    elif si[i] == 'BC' and s == 0:
        ABC[0] = ABC[1] + 1
        ABC[1] = ABC[2] - 1
        result[i] = 'B'
    elif si[i] == 'BC' and s == 1:
        ABC[0] = ABC[1] - 1
        ABC[1] = ABC[2] + 1
        result[i] = 'C'
    if ABC[0] < 0 or ABC[1] < 0 or ABC[2] < 0:
        return

    dfs(i + 1, 0)
    dfs(i + 1, 1)


dfs(0, 0)

if result[N] == 'Yes':
    print('Yes')
    for i in range(N):
        print(result[i])
else:
    print('No')

Submission status: Runtime Error

Solution code:
def main():
    n,a,b,c=map(int,input().split())
    ans=[]
    for i in range(n):
        s=input()
        if s=="AB":
                if a>b:
                    a-=1
                    b+=1
                    ans.append("B")
                else:
                    a+=1
                    b-=1
                    ans.append("A")
    
        elif s=="BC":
                if c>b:
                    c-=1
                    b+=1
                    ans.append("B")
                else:
                    c+=1
                    b-=1
                    ans.append("C")
                
        elif s=="CA":
                if c>a:
                    c-=1
                    a+=1
                    ans.append("A")
                else:
                    c+=1
                    a-=1
                    ans.append("C")

        if min(a,min(b,c))<0:
            print('No')
            exit()
    else:
        print("Yes")
        for i in range(n):
            print(ans[i])

if __name__=="__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
def input() : return sys.stdin.readline().strip()

def main():
    N, A, B, C = map(int, input().split())
    s = tuple(input() for _ in range(N))
    d = {}
    d["A"] = A
    d["B"] = B
    d["C"] = C
    ans = []
    for i in range(N-1):
        l1, r1 = s[i]
        l2, r2 = s[i+1]

        if l1 == l2 and r1 == r2:
            temp = [d[l1], d[r1]]
            max_val = max(temp)
            max_index = temp.index(max_val)
            
            if max_index == 0:
                ans.append(r1)
                d[l1] -= 1
                d[r1] += 1
            else:
                ans.append(l1)
                d[l1] += 1
                d[r1] -= 1
        else:
            for c in [l1, r1]:
                if [l1, r1, l2, r2].count(c) == 1:
                    d[c] -= 1
                else:
                    ans.append(c)
                    d[c] += 1

    l1, r1 = s[N-1]
    temp = [d[l1], d[r1]]
    max_val = max(temp)
    max_index = temp.index(max_val)
            
    if max_index == 0:
        ans.append(r1)
        d[l1] -= 1
        d[r1] += 1
    else:
        ans.append(l1)
        d[l1] += 1
        d[r1] -= 1

    if any([d[c] < 0 in "ABC"]):
            print("No")
    else:
        print("Yes")
        print(*ans, sep="\n")

if __name__ == "__main__":
    main()