Problem:


Score : 600 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

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

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

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

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

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

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

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

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

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

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys, bisect, math, itertools, string, queue, copy
# import numpy as np
# import scipy
from collections import Counter,defaultdict,deque
from itertools import permutations, combinations
from heapq import heappop, heappush
from fractions import gcd
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(input())
def inpm(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
def inpls(): return list(input().split())
def inplm(n): return list(int(input()) for _ in range(n))
def inplL(n): return [list(input()) for _ in range(n)]
def inplT(n): return [tuple(input()) for _ in range(n)]
def inpll(n): return [list(map(int, input().split())) for _ in range(n)]
def inplls(n): return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,a,b,c = inpm()
    ans = ['p' for _ in range(n)]
    stock_ab = deque()
    stock_ac = deque()
    stock_bc = deque()
    AB = deque()
    AC = deque()
    BC = deque()
    for i in range(n):
        s = input()
        if s == 'AB':
            if a>0 or b>0:
                a -= 1
                b -= 1
                AB.append(i)
            if a <= 0 and b <= 0:
                if AC and BC:
                    j = AC.popleft()
                    ans[j] = 'A'
                    k = BC.popleft()
                    ans[k] = 'B'
                else:
                    print('No')
                    return
        if s == 'AC':
            if a>0 or c>0:
                a -= 1
                c -= 1
                AC.append(i)
            if a <= 0 and c <= 0:
                if AB and BC:
                    j = AB.popleft()
                    ans[j] = 'A'
                    k = BC.popleft()
                    ans[k] = 'C'
                else:
                    print('No')
                    return
        if s == 'BC':
            if b>0 or c>0:
                c -= 1
                b -= 1
                BC.append(i)
            if c <= 0 and b <= 0:
                if AB and AC:
                    j = AB.popleft()
                    ans[j] = 'B'
                    k = AC.popleft()
                    ans[k] = 'C'
                else:
                    print('No')
                    return
    print(a,b,c)
    print(AB,AC,BC)
    print('Yes')
    if a >= 0 and b >= 0 and c < 0:
        if c + len(AC) + len(BC) < 0:
            print('No')
            return
        while AC:
            i = AC.popleft()
            ans[i] = 'C'
        while BC:
            i = BC.popleft()
            ans[i] = 'C'
        while AB:
            i = AB.popleft()
            ans[i] = 'A'
    if a >= 0 and b < 0 and c >= 0:
        if b + len(AB) + len(BC) < 0:
            print('No')
            return
        while AB:
            i = AB.popleft()
            ans[i] = 'B'
        while BC:
            i = BC.popleft()
            ans[i] = 'B'
        while AC:
            i = AC.popleft()
            ans[i] = 'A'
    if a < 0 and b >= 0 and c >= 0:
        if c + len(AC) + len(BC) < 0:
            print('No')
            return
        while AC:
            i = AC.popleft()
            ans[i] = 'C'
        while BC:
            i = BC.popleft()
            ans[i] = 'C'
        while AB:
            i = AB.popleft()
            ans[i] = 'A'
    for i in range(n):
        print(ans[i])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque

n, a, b, c = map(int, input().split())
ans = deque()
for i in range(n):
    s = input()
    if s == "AB":
        if a == b == 0:
            print("No")
            sys.exit()
        else:
            if a < b:
                a += 1
                b -= 1
                ans.append('A')
            else:
                a -= 1
                b += 1
                ans.append('B')
    elif s == "BC":
        if b == c == 0:
            print("No")
            sys.exit()
        else:
            if b < c:
                b += 1
                c -= 1
                ans.append('B')
            else:
                b -= 1
                c += 1
                ans.append('C')
    else:
        if c == a == 0:
            print("No")
            sys.exit()
        else:
            if c < a:
                c += 1
                a -= 1
                ans.append('C')
            else:
                c -= 1
                a += 1
                ans.append('A')

while len(ans):
    print(ans.popleft())

Submission status: Wrong Answer

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()
	stat = [0,0,0]
	Q = []
	for i in range(N):
		s = inputStr()
		Q.append(s)
		if s == "AB":
			stat[0] += 1
			stat[1] += 1
		elif s == "AC":
			stat[0] += 1
			stat[2] += 1
		elif s == "BC":
			stat[1] += 1
			stat[2] += 1

	delA = False
	delB = False
	delC = False
	if stat[0] <= A:
		delA = True
	if stat[1] <= B:
		delB = True
	if stat[2] <= C:
		delC = True

	targetA = False
	targetB = False
	targetC = False
	AA = A - stat[0]
	BB = B - stat[1]
	CC = C - stat[2]
	tmp = min(AA,BB,CC)
	if AA == tmp:
		targetA = True
	if BB == tmp:
		targetB = True
	if CC == tmp:
		targetC = True

	#print(stat)
	#print("{} {} {}".format(delA,delB,delC))

	hon = "Yes"
	ans = []
	for s in Q:
		#print(i)
		#print("{} {} {}".format(A,B,C))
		if s == "AB":
			stat[0] -= 1
			stat[1] -= 1
			if delA == True:
				B += 1
				A -= 1
				ans.append("B")
			elif delB == True:
				B -= 1
				A += 1
				ans.append("A")
			elif A < B:
				B -= 1
				A += 1
				ans.append("A")
			else:
				B += 1
				A -= 1
				ans.append("B")
			if A < 0 or B < 0:
				hon = "No"
				break
		elif s == "AC":
			stat[0] -= 1
			stat[2] -= 1
			if delA == True:
				C += 1
				A -= 1
				ans.append("C")
			elif delC == True:
				C -= 1
				A += 1
				ans.append("A")
			elif A < C:
				C -= 1
				A += 1
				ans.append("A")
			else:
				C += 1
				A -= 1
				ans.append("C")
			if A < 0 or C < 0:
				hon = "No"
				break
		elif s == "BC":
			stat[1] -= 1
			stat[2] -= 1
			if delB == True:
				C += 1
				B -= 1
				ans.append("C")
			elif delC == True:
				C -= 1
				B += 1
				ans.append("B")
			elif B < C:
				C -= 1
				B += 1
				ans.append("B")
			else:
				C += 1
				B -= 1
				ans.append("C")
			if C < 0 or B < 0:
				hon = "No"
				break

		if stat[0] <= A:
			delA = True
		if stat[1] <= B:
			delB = True
		if stat[2] <= C:
			delC = True

	if hon == "No":
		print(hon)
	else:
		print(hon)
		for i in ans:
			print(i)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# 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: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
INF = float("inf")


def yes():
    print("Yes")  # type: str


def no():
    print("No")  # type: str


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    ABC = [A, B, C]
    ans = []
    d = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
    for i in range(N):
        j, k = d[s[i]]
        if ABC[j] == ABC[k] == 0:
            no()
            return
        if ABC[j] > ABC[k]:
            ABC[j] -= 1
            ABC[k] += 1
            ans.append("ABC"[k])
        else:
            ABC[j] += 1
            ABC[k] -= 1
            ans.append("ABC"[j])
    yes()
    for a in ans:
        print(a)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, A, B, C, s)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# author: Stephen Zhang  time: 2020/5/3

step,A,B,C=map(int,input().split())
executable = True

for i in range(0,step):
    usrInput = input()
    if usrInput == 'AB':
        if A > 0:
            A-=1
            B+=1
        elif B>0:
            A+=1
            B-=1
        else:
            executable = False
            break
    elif usrInput == 'AC':
        if A > 0:
            A-=1
            C+=1
        elif C>0:
            A+=1
            C-=1
        else:
            executable = False
            break
    else:
        if B > 0:
            B-=1
            C+=1
        elif C>0:
            B+=1
            C-=1
        else:
            executable = False
            break
            
if executable:
    print("Yes")
else:
    print("No")

Submission status: Wrong Answer

Solution code:
x = [int(i) for i in input().split()]
n = x[0]
a = x[1]
b = x[2]
c = x[3]
ans = []
flag = True
for i in range(n):
    p = input()
    if p == 'AB':
        if a < b :
            a+= 1
            b-= 1
            ans.append('A')
        else :
            b += 1
            a -= 1
            ans.append('B')
    elif p == 'BC':
        if b < c :
            b += 1
            c -= 1
            ans.append('B')
        else :
            c += 1
            b -= 1
            ans.append('C')
    elif p == 'AC':
        if c < a :
            c += 1
            a -=1
            ans.append('C')
        else :
            a += 1
            c -= 1
            ans.append('A')
    if a<0 or b < 0 or c <0 :
        flag = False
        break
if flag == False : print("No")
else :
    print('Yes')
    print(*ans,sep ='\n')

Submission status: Wrong Answer

Solution code:
import collections
def main():

    N, A, B, C = map(int, input().split())
    cnt = dict()
    cnt['AB'] = 0
    cnt['AC'] = 0
    cnt['BC'] = 0
    hist = []
    for _ in range(N):
        s = input()
        cnt[s] += 1
        hist.append(s)

    nAB = cnt['AB']
    nAC = cnt['AC']
    nBC = cnt['BC']

    isfound = False
    info = dict()
    info['AB'] = 0
    info['AC'] = 0
    info['BC'] = 0

    for mAB in range(0, nAB+1):
        mAC = (nAB + nAC - A - 2* mAB + 1) // 2
        if mAC < 0: mAC = 0
        if mAC <= nAC:
            u = (2*mAB + nBC - nAB - B + 1) // 2
            if u < 0: u = 0
            v = (nAC + nBC + C  - 2 * mAC) // 2
            if u <= v and v <= nBC and u <= nAC:
                isfound = True
                info['AB'] = mAB
                info['AC'] = mAC
                info['BC'] = u
                break

    # print(A + info['AB']+info['AC'])
    # print(B - info['AB'] + info['BC'])
    # print(C - info['AC'] - info['BC'])

    if not isfound:
        print("No")
    else:
        print("Yes")
        for s in hist:
            if info[s] > 0:
                print(s[0])
                info[s] -= 1
            else:
                print(s[1])

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
readline = sys.stdin.readline

N, A, B, C = map(int, readline().split())
S = [readline().strip() for i in range(N)]

AB, AC, BC = 'AB', 'AC', 'BC'

n0 = [A, B, C].count(0)

F = [A, B, C]
H = ['A', 'B', 'C']
G = {AB: (0, 1), BC:(1,2), AC:(0, 2)}

if n0 == 3:
    print('No')
elif n0 == 2 and sum([A, B, C]) == 1:
    path = []
    for com in S:
        a,b = G[com]
        if F[a] == F[b] == 0:
            print('No')
            exit(0)
        else:
            if F[a] == 1:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
    print('Yes')
    for p in path:
        print(p)
else:
    if (S[0] == AB and A == 0 and B == 0) or \
     (S[0] == AC and A == 0 and C == 0) or (S[0] == BC and B == 0 and C == 0):
        print('No')
        exit(0)

    path = []
    for i,com in enumerate(S):
        a,b = G[com]
        if a == 0 or b == 0:
            if b == 0:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
        else:
            if i < len(S) - 1 and H[a] == H[b]:
                if b in G[S[i + 1]]:
                    a, b = b, a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
            else:
                if F[a] > F[b]:
                    a,b = b,a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
    print('Yes')
    for p in path:
        print(p)

Submission status: Wrong Answer

Solution code:
d={}
count={}

d['A']=0
d['B']=0
d['C']=0

count['A']=0
count['B']=0
count['C']=0

ip=[int(x) for x in input().split()]
n=ip[0]
d['A']=ip[1]
d['B']=ip[2]
d['C']=ip[3]

flag=1
a=[]
for i in range(n):
	s=input()
	a.append(s)
	count[s[0]]+=1
	count[s[1]]+=1

ans=[]
for i in range(n):
	s=a[i]
	count[s[0]]-=1
	count[s[1]]-=1
	if d[s[0]]>d[s[1]]:
		ans.append(s[1])
		d[s[1]]+=1
		d[s[0]]-=1
	elif d[s[1]]>d[s[0]]:
		ans.append(s[0])
		d[s[1]]-=1
		d[s[0]]+=1
	else:
		if count[s[0]]>count[s[1]]:
			d[s[0]]+=1
			d[s[1]]-=1
			ans.append(s[0])
		else:
			d[s[1]]+=1
			d[s[0]]-=1
			ans.append(s[1])
	if d['A']<0 or d['B']<0 or d['C']<0:
		flag=0

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

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
ans = "Yes\n"
def step(add, remove):
    global A
    global B
    global C
    global ans
    ans += add
    ans += "\n"
    if add == "A":
        A += 1
    elif add == "B":
        B += 1
    else:
        C += 1
    if remove == "A":
        A -= 1
    elif remove == "B":
        B -= 1
    else:
        C -= 1

def getvalue(variable):
    global A
    global B
    global C
    if variable == "A":
        return A
    elif variable == "B":
        return B
    else:
        return C

res = ""
for _ in range(N):
    act = input()
    if A != 0 and B != 0 and C != 0:
        step(act[0], act[1])
    elif ((A == 0) != (B == 0)) != (C == 0):
        if res == "":
            if getvalue(act[1]) == 0:
                step(act[1], act[0])
            elif getvalue(act[0]) == 0 :
                step(act[0], act[1])
            else:
                res = act
        else:
            if res[0] in act:
                i = (act.index(res[0])+1)%2
                step(res[0], res[1])
                step(act[i], res[0])
                res = ""
            else:
                i = (act.index(res[1])+1)%2
                step(res[1], res[0])
                step(act[i], res[1])
                res = ""
    else:
        if getvalue(act[0]) != 0:
            step(act[1], act[0])
        elif getvalue(act[1]) != 0:
            step(act[0], act[1])
        else:
            ans = "No"
            res = ""
            break
if res != "":
    step(res[0], res[1])
    res = ""
if ans[-1] == "\n":
    ans = ans[:-1]
print(ans)

Submission status: Wrong Answer

Solution code:
temp=[]
x=input().split(" ")
number=int(x[0])
a=int(x[1])
b=int(x[2])
c=int(x[3])
i=0
result=""
while i < number:
    temp.append(input())
    if temp[i]=="AB":
        if a<=b:
            a+=1
            b-=1
            result+="A\n"
        else:
            a-=1
            b+=1
            result += "B\n"
    elif temp[i]=="BC":
        if b<=c:
            b+=1
            c-=1
            result += "B\n"
        else:
            b-=1
            c+=1
            result += "C\n"
    else:
        if c<=a:
            c+=1
            a-=1
            result += "C\n"
        else:
            c-=1
            a+=1
            result += "A\n"
    i = i + 1
toa=0-a
tob=0-b
toc=0-c
if a<0 or b<0 or c<0:
    result_1=result.split("\n")
    if toa>0 and b>=0 and c>=0:
        i = 0
        newresult = ""
        while i < number:
            if temp[i] == "AB":
                if result_1[i]=='B' and b > 0 and toa>0:
                    b-=1
                    toa-=1
                    newresult+="A\n"
                else:
                    newresult += result_1[i] + "\n"
            elif temp[i] == "BC":
                    newresult += result_1[i]+"\n"
            else:
                if result_1[i] == 'C' and c > 0 and toa > 0:
                    c -= 1
                    toa -= 1
                    newresult += "A\n"
                else:
                    newresult += result_1[i] + "\n"
            i = i + 1
    elif tob>0 and a>=0 and c>=0:
        i = 0
        newresult = ""
        while i < number:
            if temp[i] == "AB":
                if result_1[i]=='A' and a>0 and tob>0:
                    a-=1
                    tob-=1
                    newresult+="B\n"
                else:
                    newresult += result_1[i] + "\n"
            elif temp[i] == "AC":
                    newresult += result_1[i]+"\n"
            else:
                if result_1[i] == 'C' and c > 0 and tob > 0:
                    c -= 1
                    tob -= 1
                    newresult += "B\n"
                else:
                    newresult += result_1[i]+"\n"
            i = i + 1
    elif toc>0 and a>=0 and b>=0:
        i = 0
        newresult = ""
        while i < number:
            if temp[i] == "AC":
                if result_1[i]=='A' and a>0 and toc>0:
                    a-=1
                    toc-=1
                    newresult+="C\n"
                else:
                    newresult += result_1[i]+"\n"
            elif temp[i] == "AB":
                    newresult += result_1[i]+"\n"
            else:
                if result_1[i] == 'B' and b > 0 and toc > 0:
                    b -= 1
                    toc -= 1
                    newresult += "C\n"
                else:
                    newresult += result_1[i] + "\n"
            i = i + 1
    if a < 0 or b < 0 or c < 0:
        print("NO")
    else:
        print("YES")
        print(newresult)
else:
    print("YES")
    print(result)

Submission status: Wrong Answer

Solution code:

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A,B,C=MI()
    S=[]
    SS=A+B+C
    for i in range(N):
        s=input()
        S.append(s)
    
    ans=[]
    flag=1
    for i in range(N):
        s=S[i]
        
        if s=="AB":
            if A==0:
                A+=1
                B-=1
                ans.append("A")
            elif B==0:
                A-=1
                B+=1
                ans.append("B")
            elif A==SS-1:
                A-=1
                B+=1
                ans.append("B")
            else:
                A+=1
                B-=1
                ans.append("A")
                
        elif s=="AC":
            if A==0:
                A+=1
                C-=1
                ans.append("A")
            elif C==0:
                A-=1
                C+=1
                ans.append("C")
            elif A==SS-1:
                A-=1
                C+=1
                ans.append("C")
            else:
                A+=1
                C-=1
                ans.append("A")
        
        else:
            if B==0:
                B+=1
                C-=1
                ans.append("B")
            elif C==0:
                B-=1
                C+=1
                ans.append("C")
            elif B==SS-1:
                B-=1
                C+=1
                ans.append("C")
            else:
                B+=1
                C-=1
                ans.append("B")
                
        if A<0 or B<0 or C<0:
            flag=-1
            break
                
        
        
        
        
        if A<0 or B<0 or C<0:
            flag=-1
            break
        
    if flag==-1:
        print("No")
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()

Submission status: Wrong Answer

Solution code:
import sys

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

ans = []
for i in range(n):
	s = input()
	if s == 'AB':
		if a <= 0 and b <= 0:
			print('No')
			sys.exit()
		if a < b:
			b -= 1
			a += 1
			ans.append('A')
		elif a >= b:
			a -= 1
			b += 1
			ans.append('B')
	if s == 'BC':
		if b <= 0 and c <= 0:
			print('No')
			sys.exit()
		if b < c:
			c -= 1
			b += 1
			ans.append('B')
		elif b >= c:
			b -= 1
			c += 1
			ans.append('C')
	if s == 'AC':
		if a <= 0 and c <= 0:
			print('No')
			sys.exit()
		if a < c:
			c -= 1
			a += 1
			ans.append('A')
		elif a >= c:
			a -= 1
			c += 1
			ans.append('C')
print('Yes')
for i in range(len(ans)):
	print(ans[i])

Submission status: Wrong Answer

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

    S=[input() for i in range(N)]
    ans =[]
    for s in S:
        if s =="AB":
            if A ==B ==0:
                print("No")
                return
            elif A>B:
                A-=1
                B+=1
                ans.append("B")
            else:
                A+=1
                B-=1
                ans.append("A")

        elif s =="BC":
            if C ==B ==0:
                print("No")
                return
            elif C>B:
                C-=1
                B+=1
                ans.append("B")
            else:
                C+=1
                B-=1
                ans.append("C")

        elif s =="AC":
            if C ==A ==0:
                print("No")
                return
            elif C>A:
                C-=1
                A+=1
                ans.append("A")
            else:
                C+=1
                A-=1
                ans.append("C")


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







if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split()))
ans = []
for _ in range(n):
    move = input()
    if move == 'AB':
        if a <= b:
            a += 1
            b -= 1
            ans.append('A')
        else:
            a -= 1
            b += 1
            ans.append('B')
    elif move == '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')
        exit()
if a >= 0 and b >= 0 and c >= 0:
    print('Yes')
    for i in ans:
        print(i)
else:
    print('No')