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:
s = list(map(int, input().split()))
n = s[0]
a = s[1]
b = s[2]
c = s[3]

s = []
for i in range(n):
    str = input()
    s.append(str)

if a + b + c >= 2:
    if a == 0 and b == 0 and s[0] == "AB":
        print("No")
    elif a == 0 and c == 0 and s[0] == "BC":
        print("No")
    elif a == 0 and c == 0 and s[0] == "AC":
        print("No")
    else:
        print("Yes")

        for i in range(n):
            if s[i] == "AB":
                if a < 2 and b > 2:
                    b = b - 1
                    a = a + 1
                    print("A")
                elif a > 2 and b < 2:
                    b = b + 1
                    a = a - 1
                    print("B")
                elif a == 1 and b == 1:
                    if i != n - 1:
                        if s[i + 1] == "BC":
                            b = b + 1
                            a = a - 1
                            print("B")
                        else:
                            a = a + 1
                            b = b - 1
                            print("A")
                    else:
                        a = a + 1
                        b = b - 1
                        print("A")
                elif a == 0:
                    a = a + 1
                    b = b - 1
                    print("A")
                elif b == 0:
                    b = b + 1
                    a = a - 1
                    print("B")

            elif s[i] == "BC":
                if b < 2 and c > 2:
                    c = c - 1
                    b = b + 1
                    print("B")
                elif b > 2 and c < 2:
                    c = c + 1
                    b = b - 1
                    print("C")
                elif b == 1 and c == 1:
                    if i != n - 1:
                        if s[i + 1] == "AC":
                            c = c + 1
                            b = b - 1
                            print("C")
                        else:
                            b = b + 1
                            c = c - 1
                            print("B")
                    else:
                        b = b + 1
                        c = c - 1
                        print("B")
                elif b == 0:
                    b = b + 1
                    c = c - 1
                    print("B")
                elif c == 0:
                    c = c + 1
                    b = b - 1
                    print("C")

            elif s[i] == "AC":
                if c < 2 and a > 2:
                    a = a - 1
                    c = c + 1
                    print("C")
                elif c > 2 and a < 2:
                    a = a + 1
                    c = c - 1
                    print("A")
                elif c == 1 and a == 1:
                    if i != n - 1:
                        if s[i + 1] == "BC":
                            c = c + 1
                            a = a - 1
                            print("C")
                        else:
                            a = a + 1
                            c = c - 1
                            print("A")
                    else:
                        c = c + 1
                        a = a - 1
                        print("C")
                elif a == 0:
                    a = a + 1
                    c = c - 1
                    print("A")
                elif c == 0:
                    c = c + 1
                    a = a - 1
                    print("C")

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
ab=[0]*n
ac=[0]*n
bc=[0]*n
AB=0
BC=0
AC=0
ip=[0]*n
for i in range(n):
	ip[i]=input()
	if ip[i]=="AB":
		AB+=1
	elif ip[i]=="BC":
		BC+=1
	else:
		AC+=1
	ab[i]=AB
	bc[i]=BC
	ac[i]=AC
res=[0]*n
ans="Yes"
for i in range(n):
	AB=ab[n-1]-ab[i]
	BC=bc[n-1]-bc[i]
	AC=ac[n-1]-ac[i]
	A=AB+AC-a
	B=AB+BC-b
	C=AC+BC-c
	if ip[i]=="AB":
		if A+(C-A)>B:
			res[i]="A"
			a+=1
			b-=1
		else:
			res[i]="B"
			a-=1
			b+=1
	if ip[i]=="BC":
		if B+(A-B)>C:
			res[i]="B"
			b+=1
			c-=1
		else:
			res[i]="C"
			b-=1
			c+=1
	if ip[i]=="AC":
		if A+(B-A)>C:
			res[i]="A"
			a+=1
			c-=1
		else:
			res[i]="C"
			a-=1
			c+=1
	if a<0 or b<0 or c<0:
		ans="No"
		break
if ans=="Yes":
	print(ans)
	for i in range(n):
		print(res[i])
else:
	print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)
#input = sys.stdin.readline
N,A,B,C = MI()
s = LS2(N)
abc = [A,B,C]
dic = {'A':0,'B':1,'C':2}
redic = {0:'A',1:'B',2:'C'}
f=0
select = []
for i in range(N):
    order = s[i][0]
    x = dic[order[0]]
    y = dic[order[1]]
    xnum=abc[x]
    ynum=abc[y]
    if xnum>=ynum:
        select.append(y)
        abc[x] -= 1
        abc[y] += 1
    else:
        select.append(x)
        abc[x] += 1
        abc[y] -= 1
    for i in abc:
        if i<0:
            print('No')
            f=1
            break
    if f==1:
        break
else:
    print('Yes')
    [print(redic[x]) for x in select]

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
YES = "Yes"  # type: str
NO = "No"  # type: str

def cCount(s,c):
    return 1 if s[0]==c or s[1] == c else 0

def solve(n: int, A: int, B: int, C: int, s: "List[str]"):
    # S = {}
    # S[(A,B,C)] = ""
    # for i in range(n):
    #     N = {}
    #     d = s[i]
    #     for a,b,c in S:
    #         path = S[(a,b,c)]
    #         if d == "AB":
    #             if b >= n-i:
    #                 N[(a+1,b-1,c)] = path + "A"
    #                 continue
    #             if a >= n-i:
    #                 N[(a-1,b+1,c)] = path + "B"
    #                 continue
    #             if b > 0:
    #                 # A+1,B-1
    #                 N[(min(n,a+1),b-1,c)] = path + "A"
    #             if a > 0:
    #                 N[(a-1,min(n,b+1),c)] = path + "B"
    #         elif d == "BC":
    #             if b >= n - i:
    #                 N[(a, b - 1, c + 1)] = path + "C"
    #                 continue
    #             if c >= n - i:
    #                 N[(a, b+1, c-1)] = path + "B"
    #                 continue
    #             if c > 0:
    #                 # b +1,c -1
    #                 N[(a, min(n,b + 1), c-1)] = path + "B"
    #             if b > 0:
    #                 N[(a, b -1, min(n,c+1))] = path + "C"
    #         else:
    #             if c >= n - i:
    #                 N[(a + 1, b, c-1)] = path + "A"
    #                 continue
    #             if a >= n - i:
    #                 N[(a - 1, b, c+1)] = path + "C"
    #                 continue
    #             if c > 0:
    #                 N[(min(n,a+1),b,c-1)] = path + "A"
    #             if a > 0:
    #                 N[(a-1,b,min(n,c+1))] = path + "C"
    #     if len(N) == 0:
    #         print(NO)
    #         exit()
    #     S = N
    # print(YES)
    # for a,b,c in S:
    #     p = S[(a,b,c)]
    #     for i in p:
    #         print(i)
    #     break
    S = [[0,0,0] for i in range(n)]
    prev = [0,0,0]
    for i in range(n):
        command = s[i]
        prev = S[-1-i] = [prev[0]+cCount(command,"A"),prev[1]+cCount(command,"B"),prev[2]+cCount(command,"C")]
    path = []

    for i in range(n):
        if A < 0 or B < 0 or C < 0:
            print(NO)
            exit()
        command = s[i]
        if command == "AB":
            if S[i][0] <= A:
                path.append("B")
                B+=1
                A-=1
                continue
            if S[i][1] <= B:
                path.append("A")
                A+=1
                B-=1
                continue
            if S[i][0] - A >= S[i][1] - B:
                path.append("A")
                A+=1
                B-=1
            else:
                path.append("B")
                A-=1
                B+=1
        elif command == "BC":
            if S[i][1] <= B:
                path.append("C")
                B -= 1
                C += 1
                continue
            if S[i][2] <= C:
                path.append("C")
                B += 1
                C -= 1
                continue
            if S[i][1] - B >= S[i][2] - C:
                path.append("B")
                B += 1
                C -= 1
            else:
                path.append("C")
                B -= 1
                C += 1
        else:
            if S[i][0] <= A:
                path.append("C")
                C += 1
                A -= 1
                continue
            if S[i][2] <= C:
                path.append("A")
                A += 1
                C -= 1
                continue
            if S[i][0] - A >= S[i][2] - C:
                path.append("A")
                A += 1
                C -= 1
            else:
                path.append("C")
                A -= 1
                C +=1
    if A < 0 or B < 0 or C < 0:
        print(NO)
        exit()
    print(YES)
    for i in path:
        print(i)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
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:

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


def score(start, queries):
    def rec(nums, d):
        if d >= len(queries):
            return -sum(n==0 for n in nums)
        i,j = queries[d]
        nums[i] += 1
        nums[j] -= 1
        x = rec(nums, d+1)
        nums[i] -= 2
        nums[j] += 2
        y = rec(nums, d+1)
        nums[i] += 1
        nums[j] -= 1
        return max(x,y)
    return rec(start, 0)



Q = []
for _ in range(N):
    s = input()
    if s == 'AB':
        i,j = 0,1
    elif s == 'BC':
        i,j = 1,2
    else:
        i,j = 2,0

    Q.append((i,j))

nums = [A,B,C]
res = []
flag = True
for q,(i,j) in enumerate(Q):
    queries = Q[q+1:min(q+4, len(Q))]
    nums[i] += 1
    nums[j] -= 1
    x = score(nums, queries)
    nums[i] -= 2
    nums[j] += 2
    y = score(nums, queries) 
    nums[i] += 1
    nums[j] -= 1
    if x > y:
        res.append(i)
        nums[i] += 1
        nums[j] -= 1
    else:
        res.append(j)
        nums[i] -= 1
        nums[j] += 1
    if min(nums) < 0:
        flag = False
        break


if not flag:
    print('No')
else:
    print('Yes')
    res = tuple(str(chr(v+ord('A'))) for v in res)
    print(*res,sep='\n')

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys

YES = "Yes"  # type: str
NO = "No"  # type: str


def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    if A ==0 and B ==0 and C ==0:
        print(NO)
        return 

    def yes():
        tmp = [A,B,C]
        print(YES)
        for i in range(N):
            a = tmp[0]
            b = tmp[1]
            c = tmp[2]
            if s[i] == "AB":
                if a<=b:
                    a += 1
                    b -= 1
                    print("A")
                else:
                    a -=1
                    b += 1
                    print("B")
            elif s[i] == "AC":
                if a<=c:
                    a += 1
                    c -= 1
                    print('A')
                else:
                    a -=1
                    c += 1
                    print('C')
            else:
                if b<=c:
                    b += 1
                    c -= 1
                    print('B')

                else:
                    b -=1
                    c += 1
                    print('C')
            tmp = [a,b,c]
        return 

    if A+B+C>2 and [A,B,C].count(0) <= 1:
        yes()
    else:
        if A+B+C>2:
            if C>0:
                if s[0] == "AB":
                    print(NO)
                else:
                    yes()
            elif A>0:
                if s[0] == "BC":
                    print(NO)
                else:
                    yes()
            else: 
                if s[0] == "AC":
                    print(NO)
                else:
                    yes()
        else:
            if [A,B,C].count(0) == 2:
                tmp = [A,B,C]
                for i in range(N):
                    a = tmp[0]
                    b = tmp[1]
                    c = tmp[2]
                    if s[i] == "AB":
                        if a<=b:
                            a += 1
                            b -= 1
                        else:
                            a -=1
                            b += 1
                    elif s[i] == "AC":
                        if a<=c:
                            a += 1
                            c -= 1
                        else:
                            a -=1
                            c += 1
                    else:
                        if b<=c:
                            b += 1
                            c -= 1
                        else:
                            b -=1
                            c += 1
                    if a < 0 or b<0 or c<0:
                        print(NO)
                        return
                    tmp = [a,b,c]
                yes()
            elif [A,B,C].count(0) == 1:
                yes()

        return 



    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:
n, a, b, c = list(map(int, input().split()))
res = ["0"]*n
for i in range(n):
    q = input()
    if q == "AB":
        if a>b:
            a -= 1
            b += 1
            res[i] = "B"
        else:
            a += 1
            b -= 1
            res[i] = "A"
    elif q == "AC":
        if a>c:
            a -= 1
            c += 1
            res[i] = "C"
        else:
            a += 1
            c -= 1
            res[i] = "A"
    elif q == "BC":
        if b>c:
            b -= 1
            c += 1
            res[i] = "C"
        else:
            c -= 1
            b += 1
            res[i] = "B"
if a >=0 and b >= 0 and c >= 0:
    print("Yes")
    for i in res:
        print(i)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B, C = (int(_) for _ in input().split())
    query = [input() for _ in range(N)]
    output = []

    def yaru(a, b):
        if a >= b:
            a -= 1
            b += 1
            x = 0
        else:
            a += 1
            b -= 1
            x = 1
        return a, b, x

    for s in query:
        if s == 'AB':
            A, B, x = yaru(A, B)
            if x == 0: output.append('B')
            else: output.append('A')
        if s == 'BC':
            B, C, x = yaru(B, C)
            if x == 0: output.append('C')
            else: output.append('B')
        if s == 'AC':
            A, C, x = yaru(A, C)
            if x == 0: output.append('C')
            else: output.append('A')
    print('Yes' if min(A, B, C) >= 0 else 'No')
    print(*output, sep='\n')

    return

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
if ((a == 0) + (b == 0) + (c == 0) == 1 and (a == 1) + (b == 1) + (c == 1) == 2):
    pass
else:
    s = "Yes"
    for _ in range(n):
        t = input()
        if (t == "AB"):
            if (a < b):
                s += "\nA"
                a += 1
                b -= 1
            else:
                s += "\nB"
                b += 1
                a -= 1
        if (t == "AC"):
            if (a < c):
                s += "\nA"
                a += 1
                c -= 1
            else:
                s += "\nC"
                c += 1
                a -= 1
        if (t == "BC"):
            if (b < c):
                s += "\nB"
                b += 1
                c -= 1
            else:
                s += "\nC"
                c += 1
                b -= 1
        if (a < 0 or b < 0 or c < 0):
            print("No")
            exit()
    print(s)

Submission status: Wrong Answer

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

# greedy
for _ in range(N):
    s = input()
    if s == "AB":
        if A > B:
            B+=1
            A-=1
            str_list.append("B")
        else:
            A+=1
            B-=1
            str_list.append("A")
    elif s=="AC":
        if A > C:
            C+=1
            A-=1
            str_list.append("C")
        else:
            A+=1
            C-=1
            str_list.append("A")
    elif s=="BC":
        if B > C:
            C+=1
            B-=1
            str_list.append("C")
        else:
            B+=1
            C-=1
            str_list.append("B")

    if A<0 or B<0 or C<0:
        str_list = []
        print("No")
        break
if str_list != []:
    print("Yes")
    for tmp in str_list:
        print(tmp)

Submission status: Wrong Answer

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

res_list = []
flag = 1

for i in range(n):
    if flag == 0:
        break
    if ss[i] == 'AB':
        if a == 0 and b == 0:
            flag = 0
        elif a == 0:
            a, b = 1, b-1
            res_list.append('A')
        elif b == 0:
            a, b = a-1, 1
            res_list.append('B')
        elif a == 1 and b == 1:
            if i == n-1:
                res_list.append('A')
            elif ss[i+1] == 'BC':
                res_list.append('B')
                a, b = 0, 2
            elif ss[i+1] == 'AC':
                res_list.append('A')
                a, b = 2, 0
            else:
                res_list.append('A')
                a, b = 2, 0
        else:
            res_list.append('A')
            a, b = a+1, b-1
    elif ss[i] == 'BC':
        if b == 0 and c == 0:
            flag = 0
        elif b == 0:
            b, c = 1, c-1
            res_list.append('B')
        elif c == 0:
            b, c = b-1, 1
            res_list.append('C')
        elif b == 1 and c == 1:
            if i == n-1:
                res_list.append('B')
            elif ss[i+1] == 'AB':
                res_list.append('B')
                b, c = 2, 0
            elif ss[i+1] == 'AC':
                res_list.append('C')
                b, c = 0, 2
            else:
                res_list.append('B')
                b, c = 2, 0
        else:
            res_list.append('B')
            b, c = b+1, c-1
    else:
        if a == 0 and c == 0:
            flag = 0
        elif a == 0:
            a, c = 1, c-1
            res_list.append('A')
        elif c == 0:
            a, c = a-1, 1
            res_list.append('C')
        elif a == 1 and c == 1:
            if i == n-1:
                res_list.append('A')
            elif ss[i+1] == 'AB':
                res_list.append('A')
                a, c = 2, 0
            elif ss[i+1] == 'BC':
                res_list.append('C')
                a, c = 0, 2
            else:
                res_list.append('A')
                a, c = 2, 0
        else:
            res_list.append('A')
            a, c = a+1, c-1

if flag == 0:
    print('No')
else:
    print('Yes')
    for res in res_list:
        print(res)

Submission status: Wrong Answer

Solution code:
n,a,b,c = list(map(int,input().split()))
an=0;d={};l=[];x=y=z=0
for i in range(n):
  s=input();l.append(s)
  x+=int(s[0]=='A')
  z+=int(s[-1]=='C')
  y+=int(s[-1]=='B')+int(s[0]=='B')
x=a-x;y=b-y;z=c-z
if (x<0)+(y<0)+(z<0)>=2:
  print("No");exit()
a=[]
for i in range(n):
  if l[i]=='AC':
    if x>=0:
      z+=1;a.append('A')
    else:
      x+=1;a.append('C')
  if l[i]=='AB':
    if x>=0:
      y+=1;a.append('A')
    else:
      x+=1;a.append('B')
  if l[i]=='BC':
    if y>=0:
      z+=1;a.append('B')
    else:
      y+=1;a.append('C')
print("Yes")
for i in a:
  print(i)

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
k = 0
if a == 0:
  k += 1
if b == 0:
  k += 1
if c == 0:
  k += 1

order = [0]*n
answer = [0]*n
for i in range(n):
  s = input()
  if s == "AB":
    order[i] = 1
  elif s == "BC":
    order[i] = 2
  else:
    order[i] = 3
ans = True
if k == 3:
  ans = False
if k == 2:
  for i in range(n):
    key =  order[i]
    if key == 1:
      if a == 0 and b == 0:
        ans = False
        break
      elif a == 0:
        a += 1
        b -= 1
        answer[i] = "A"
      elif b == 0:
        answer[i] = "B"
        a -= 1
        b += 1
      elif i < n-1 and a == 1 and (order[i+1] == 1 or order[i+1] == 3):
        a += 1
        b -= 1
        answer[i] = "A"
      elif i < n-1 and b == 1 and (order[i+1] == 2 or order[i+1] == 1):
        b += 1
        a -= 1
        answer[i] = "B"
      else:
        if a == b and i < n-1 and order[i+1] != 1:
          if order[i+1] == 2:
            a -= 1
            b += 1
            answer[i] = "B"
          else:
            b -= 1
            a += 1
            answer[i] = "A"
        elif a < b:
          a += 1
          b -= 1
          answer[i] = "A"
        else:
          a -= 1
          b += 1
          answer[i] = "B"
    if key == 2:
      if c == 0 and b == 0:
        ans = False
        break
      elif b == 0:
        b += 1
        c -= 1
        answer[i] = "B"
      elif c == 0:
        c += 1
        b -= 1
        answer[i] = "C"
      elif i < n-1 and c == 1 and (order[i+1] == 2 or order[i+1] == 3):
        c += 1
        b -= 1
        answer[i] = "C"
      elif i < n-1 and b == 1 and (order[i+1] == 1 or order[i+1] == 2):
        b += 1
        c -= 1
        answer[i] = "B"
      else:
        if c == b and i < n-1 and order[i+1] != 2:
          if order[i+1] == 1:
            c -= 1
            b += 1
            answer[i] = "B"
          else:
            b -= 1
            c += 1
            answer[i] = "C"
        elif b < c:
          b += 1
          c -= 1
          answer[i] = "B"
        else:
          b -= 1
          c += 1
          answer[i] = "C"
    if key == 3:
      if a == 0 and c == 0:
        ans = False
        break
      elif a == 0:
        a += 1
        c -= 1
        answer[i] = "A"
      elif c == 0:
        c += 1
        a -= 1
        answer[i] = "C"
      elif i < n-1 and a == 1 and (order[i+1] == 1 or order[i+1] == 3):
        a += 1
        c -= 1
        answer[i] = "A"
      elif i < n-1 and c == 1 and (order[i+1] == 2 or order[i+1] == 3):
        c += 1
        a -= 1
        answer[i] = "C"
      else:
        if a == c and i < n-1 and order[i+1] != 3:
          if order[i+1] == 2:
            a -= 1
            c += 1
            answer[i] = "C"
          else:
            c -= 1
            a += 1
            answer[i] = "A"
        elif a < c:
          a += 1
          c -= 1
          answer[i] = "A"
        else:
          a -= 1
          c += 1
          answer[i] = "C"
if k <= 1:
  for i in range(n):
    if order[i] == 1:
      if a == 0:
        a += 1
        b -= 1
        answer[i] = "A"
      elif b == 0:
        b += 1
        a -= 1
        answer[i] = "B"
      else:
        if a < b:
          a += 1
          b -= 1
          answer[i] = "A"
        else:
          answer[i] = "B"
          b += 1
          a -= 1
    if order[i] == 2:
      if c == 0:
        c += 1
        b -= 1
        answer[i] = "C"
      elif b == 0:
        b += 1
        c -= 1
        answer[i] = "B"
      else:
        if c < b:
          c += 1
          b -= 1
          answer[i] = "C"
        else:
          answer[i] = "B"
          b += 1
          c -= 1  
    if order[i] == 3:
      if a == 0:
        a += 1
        c -= 1
        answer[i] = "A"
      elif c == 0:
        c += 1
        a -= 1
        answer[i] = "C"
      else:
        if a < c:
          a += 1
          c -= 1
          answer[i] = "A"
        else:
          answer[i] = "C"
          c += 1
          a -= 1
if ans:
  print("Yes")
  for v in answer:
    print(v)
else:
  print("No")

Submission status: Wrong Answer

Solution code:
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)


def input():
    return stdin.readline().strip()


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


def dfs(index, hp):
    if index == 0:
        return ''

    s1, s2 = strings[index-1]
    if hp[s1] > 0:
        hp2 = hp.copy()
        hp2[s1] -= 1
        hp2[s2] += 1
        ret = dfs(index - 1, hp2)
        if ret != False:
            return ret + s2
    if hp[s2] > 0:
        hp2 = hp.copy()
        hp2[s2] -= 1
        hp2[s1] += 1
        ret = dfs(index - 1, hp2)
        if ret != False:
            return ret + s1

    return False


d = {'A': A, 'B': B, 'C': C}
ans = dfs(N, d)
if ans == False:
    print('No')
else:
    for c in ans:
        print(c)

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
now = [[a,b,c,[]]]
for _ in range(n):
  q = input()
  tmp = []
  if q == 'AB':
    for x in now:
      a,b,c,his = x
      if a > 0:
        tmp.append([a-1,b+1,c,his+['B']])
      if b > 0:
        tmp.append([a+1,b-1,c,his+['A']])
  elif q == 'AC':
    for x in now:
      a,b,c,his = x
      if a > 0:
        tmp.append([a-1,b,c+1,his+['C']])
      if c > 0:
        tmp.append([a+1,b,c-1,his+['A']])
  else:
    for x in now:
      a,b,c,his = x
      if b > 0:
        tmp.append([a,b-1,c+1,his+['C']])
      if c > 0:
        tmp.append([a,b+1,c-1,his+['B']])
  if not tmp:
    print('No')
    exit()
  now = []
  now2 = []
  for d in range(3):
    m = max(tmp,key=lambda x:x[0])
    if m[:3] not in now2:
      now.append(m)
      now2.append(m[:3])
    m = min(tmp,key=lambda x:x[0])
    if m[:3] not in now2:
      now.append(m)
      now2.append(m[:3])
print('Yes')
ans = now[0][3]
for s in ans:
  print(s)

Submission status: Wrong Answer

Solution code:
import sys

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

lst = []
for i in range(n):
  s = input()
  if s == "AB":
    
    if a < b:
      a += 1
      b -= 1
      lst.append("A")
    else:
      a -= 1
      b += 1
      lst.append("B")
      
  if s == "BC":  
    
    if b < c:
      b += 1
      c -= 1
      lst.append("B")
    else:
      b -= 1
      c += 1
      lst.append("C")
      
  if s == "AC": 
    if a < c:
      a += 1
      c -= 1
      lst.append("A")
    else:
      a -= 1
      c += 1
      lst.append("C")
    
  if (a < 0) or (b < 0) or (c < 0):
    print("No")
    sys.exit()
    
print("Yes")
for i in range(n):
  print(lst[i])

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
s = [input() for _ in range(n)]
ans = []
if a*b*c == 0:
  print("No")
  quit()
def f(i,j,k,l):
  if i == j == 1:
    print("No")
    quit()
  if i > j:
    ans.append(l)
    i -= 1
    j += 1
  else:
    ans.append(k)
    i += 1
    j -= 1
  return 1


for l in s:
  if l == "AB":
    f(a,b,"A","B")
  elif l == "BC":
    f(b,c,"B","C")
  else:
    f(a,c,"A","C")
print("Yes")
for l in ans:
  print(l)