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:
def solve():
    n, a, b, c = map(int, input().split())
    s = [input() for _ in range(n)]
    out = [[0, 0, 0] for _ in range(n)]
    tmp = [[0, 0, 0] for _ in range(n)]

    if s[0] == "AB":
        out[0] = [-1, 1, 0]
    elif s[0] == "BC":
        out[0] = [0, -1, 1]
    elif s[0] == "AC":
        out[0] = [-1, 0, 1]

    for i in range(n):
        if s[i] == "AB":
            tmp[i] = [1, 1, 0]
        if s[i] == "BC":
            tmp[i] = [0, 1, 1]
        if s[i] == "AC":
            tmp[i] = [1, 0, 1]

    for i in range(1, n):
        for j in range(3):
            out[i][j] = out[i-1][j] * tmp[i][j] * -1

        for j in range(3):
            if sum(out[i]) != 0 and tmp[i][j] != 0 and out[i][j] == 0:
                out[i][j] = -(sum(out[i]))
                break

    original = [a, b, c]
    check = 0
    for i in out:
        a += i[0]
        b += i[1]
        c += i[2]
        if not (0 <= a and 0 <= b and 0 <= c):
            check = 1

    if 0 <= a and 0 <= b and 0 <= c and not check:
        print("Yes")
        for i in out:
            for j in range(3):
                if i[j] == 1:
                    print(chr(ord("A") + j))
        return

    [a, b, c] = original
    check = 0
    for i in out:
        a -= i[0]
        b -= i[1]
        c -= i[2]
        if not (0 <= a and 0 <= b and 0 <= c):
            check = 1

    if 0 <= a and 0 <= b and 0 <= c and not check:
        print("Yes")
        for i in out:
            for j in range(3):
                if i[j] == -1:
                    print(chr(ord("A") + j))
        return
    print("No")

solve()

Submission status: Wrong Answer

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

def output(a_ab, b_ab, a_ac, c_ac, b_bc, c_bc):
    for round in rounds:
        if round == "AB":
            if a_ab > 0:
                print("A")
                a_ab -= 1
            else:
                print("B")

        if round == "AC":
            if a_ac > 0:
                print("A")
                a_ac -= 1
            else:
                print("C")

        if round == "BC":
            if b_bc > 0:
                print("B")
                b_bc -= 1
            else:
                print("C")
                

ab = rounds.count("AB")
bc = rounds.count("BC")
ac = rounds.count("AC")

for a_ab in range(ab + 1):
    b_ab = ab - a_ab
    a_rest = a + a_ab - b_ab
    b_rest = b + b_ab - a_ab

    a_ac = max(0, (ac - a_rest + 1)//2)
    c_ac = ac - a_ac
    
    b_bc = max(0, (bc - b_rest + 1)//2)
    c_bc = bc - b_bc
    if a_ab < 0 or b_ab < 0 or a_ac < 0 or c_ac < 0 or b_bc < 0 or c_bc < 0:
        continue
    if a + a_ab - b_ab + a_ac - c_ac >= 0 and b + b_ab - a_ab + b_bc - c_bc >= 0 and c + c_bc - b_bc + c_ac - a_ac >= 0:
        print("Yes")
        output(a_ab, b_ab, a_ac, c_ac, b_bc, c_bc)
        exit()

print("No")

Submission status: Wrong Answer

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

ops = ''

N, A, B, C = LI()
order = []
nums = [[0,0,0] for _ in range(N)]
abc = 'ABC'
for i in range(N):
    s = S()
    order.append(s)
    for j in range(3):
        if abc[j] in s:
            nums[i][j] += 1
turn = 0
cumsum_nums = [[0, 0, 0] for _ in range(N+1)]

def process(_arr, i, j):
    global ops, cumsum_nums, turn
    arr = _arr.copy()
    if arr[i] > arr[j]:
        i, j = j, i
    if arr[i] == arr[j]:
        i_count = cumsum_nums[-1][i] - cumsum_nums[turn][i]
        j_count = cumsum_nums[-1][j] - cumsum_nums[turn][j]
        if i_count < j_count:
            i, j = j, i

    arr[i] += 1
    arr[j] -= 1
    if i == 0:
        c = 'A'
    if i == 1:
        c = 'B'
    if i == 2:
        c = 'C'
    ops += c
    return arr

def main():
    global turn
    for j in range(3):
        for i in range(N):
            cumsum_nums[i+1][j] = cumsum_nums[i][j] + nums[i][j]

    arr = [A, B, C]
    for i in range(N):
        s = order[i]
        if s == 'AB':
            arr = process(arr, 0, 1)
        if s == 'BC':
            arr = process(arr, 1, 2)
        if s == 'AC':
            arr = process(arr, 0, 2)
        turn += 1
        for elem in arr:
            if elem < 0:
                print('No')
                return
    print('Yes')
    for c in ops:
        print(c)

main()

Submission status: Wrong Answer

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


n,a,b,c = mi()
s = []
for i in range(n):
    s.append(input())
z = a+ b+ c
flag = True
ans = []
if z != 2:
    for i in range(n):
        if s[i] == "AB":
            if a == 0 and b == 0:
                print("No")
                exit()
            if a > b:
                a -= 1
                b += 1
                ans.append("B")
            else:
                a += 1
                b -= 1
                ans.append("A")

        if s[i] == "BC":
            if c == 0 and b == 0:
                print("No")
                exit()
            if c > b:
                c -= 1
                b += 1
                ans.append("B")
            else:
                c += 1
                b -= 1
                ans.append("C")
        if s[i] == "AC":
            if c == 0 and a == 0:
                print("No")
                exit()
            if c > a:
                c -= 1
                a += 1
                ans.append("A")
            else:
                c += 1
                a -= 1
                ans.append("C")
    print("Yes")
    for l in range(n):
        print(ans[l])

else:
    for i in range(n):
        if s[i] == "AB":
            if a == 0 and b == 0:
                print("No")
                exit()
            if a > b:
                a -= 1
                b += 1
                ans.append("B")
            elif a == b:
                if i == n-1:
                    ans.append("B")
                elif s[i+1] == "BC":
                    a -= 1
                    b += 1
                    ans.append("B")
                else:
                    a += 1
                    b -= 1
                    ans.append("A")
            else:
                a += 1
                b -= 1
                ans.append("A")

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

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
d = [a,b,c]
q = [input() for i in range(n)]
x = {'A':0,'B':1,'C':2}
y = ['A','B','C']

if d.count(0)==3:
    print('No')
elif d.count(0)==2:
    ans = []
    for i in range(n):
        p = q[i]
        if p=='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 p=='BC':
            if b==1:
                b-=1
                c+=1
                ans.append('C')
            elif c==1:
                c-=1
                b+=1
                ans.append('B')
            else:
                print('No')
                exit()
        else:
            if a==1:
                a-=1
                c+=1
                ans.append('C')
            elif c==1:
                c-=1
                a+=1
                ans.append('A')
            else:
                print('No')
                exit()
    print('Yes')
    print('\n'.join(ans))
elif d.count(0)==1:
    ans = []
    flag = 0
    for i in range(n-1):
        if flag:
            flag = 0
            continue
        s = x[q[i][0]]
        t = x[q[i][1]]
        if d[s]==0:
            d[s]+=1
            d[t]-=1
            ans.append(y[s])
        elif d[t]==0:
            d[s]-=1
            d[t]+=1
            ans.append(y[t])
        else:
            flag = 1
            if q[i]==q[i+1]:
                ans.append(y[s])
                ans.append(y[t])
            elif q[i][0] in q[i+1]:
                ans.append(y[s])
                d[t] -= 1
                if q[i][0] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[x[q[i+1][0]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[x[q[i+1][1]]]+=1
            else:
                ans.append(y[t])
                d[s] -= 1
                if q[i][1] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[x[q[i+1][0]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[x[q[i+1][1]]]+=1
    if len(ans)!=n:
        s = x[q[-1][0]]
        t = x[q[-1][1]]
        if d[s]>0:
            ans.append(y[t])
        else:
            ans.append(y[s])
    print('Yes')
    print('\n'.join(ans))
else:
    ans = []
    flag = 0
    for i in range(n-1):
        s = x[q[i][0]]
        t = x[q[i][1]]
        if flag:
            flag = 0
            continue
        if d[0]>0 and d[1]>0 and d[2]>0:
            ans.append(y[s])
            d[s]+=1
            d[t]-=1
            continue
        if d[s]==0:
            d[s]+=1
            d[t]-=1
            ans.append(y[s])
        elif d[t]==0:
            d[s]-=1
            d[t]+=1
            ans.append(y[t])
        else:
            flag = 1
            if q[i]==q[i+1]:
                ans.append(y[s])
                ans.append(y[t])
            elif q[i][0] in q[i+1]:
                ans.append(y[s])
                d[t] -= 1
                if q[i][0] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[x[q[i+1][0]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[x[q[i+1][1]]]+=1
            else:
                ans.append(y[t])
                d[s] -= 1
                if q[i][1] != q[i+1][0]:
                    ans.append(q[i+1][0])
                    d[x[q[i+1][0]]]+=1
                else:
                    ans.append(q[i+1][1])
                    d[x[q[i+1][1]]]+=1
    if len(ans) != n:
        s = x[q[-1][0]]
        t = x[q[-1][1]]
        if d[s] >0 :
            ans.append(y[t])
        else:
            ans.append(y[s])
    print('Yes')
    print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
inpl = lambda: list(map(int,input().split()))
N, A, B, C = inpl()
X = [A, B, C]
ans = []
for _ in range(N):
    s = input()
    p = ord(s[0]) - ord('A')
    q = ord(s[1]) - ord('A')
    if X[p] == X[q] == 0:
        print('No')
        exit()
    elif X[p] <= X[q]:
        ans.append(s[0])
    else:
        ans.append(s[1])
else:
    print('Yes')
    for i in range(N):
        print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
#from fractions import gcd
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

n, a, b, c = MAP()
abc  = [a, b, c]
arr = [['AB', 0, 1], ['AC', 0, 2], ['BC', 1, 2]]
s = [STR() for _ in range(n)]

cnt = [0, 0, 0]

for i in range(n):
    for j in range(3):
        if s[i] == arr[j][0]:
            cnt[arr[j][1]] += 1
            cnt[arr[j][2]] += 1

pls = [max(0, cnt[i] - abc[i]) for i in range(3)]
if sum(pls) > n:
    print('No')
    exit()

tmp = ['A', 'B', 'C']

print('Yes')
for ss in s:
    for j in range(3):
        if ss == arr[j][0]:
            flag = True
            for k in range(1, 3):
                if pls[arr[j][k]] > 0 and flag:
                    print(tmp[arr[j][k]])
                    pls[arr[j][k]] -= 1
                    flag = False
            if flag:
                print('A')
            break

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().strip().split())
ca,cb,cc = a,b,c
ans  = []
f = 0
for _ in range(n):
    st = input()
    if st=="AB":
        if ca>cb:
            ca -= 1
            cb += 1
            ans.append("A")
        elif cb>0:
            cb -= 1
            ca += 1
            ans.append("B")
        else:
            f = 1
    elif st=="BC":
        if cc>cb:
            cc -= 1
            cb += 1
            ans.append("C")
        elif cb>0:
            cb -= 1
            cc += 1
            ans.append("B")
        else:
            f = 1
    else:
        if ca>cc:
            ca -= 1
            cc += 1
            ans.append("A")
        elif cc>0:
            cc -= 1
            ca += 1
            ans.append("C")
        else:
            f = 1
if not f:
    print("Yes")
    for val in ans:
        print(val)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
#F
N,A,B,C = map(int,input().split())
S = [str(input()) for i in range(N)]
Ac = [0]*N
Bc = [0]*N
Cc = [0]*N

oa = 0
ob = 0
oc = 0
for i in range(N):
    s = S[N-i-1]
    x,y = s
    if x == "A" or y == "A":
        oa += 1
    Ac[i] = oa
    if x == "B" or y == "B":
        ob += 1
    Bc[i] = ob
    if x == "C" or y == "C":
        oc += 1
    Cc[i] = oc
        
Ac = Ac[::-1]
Bc = Bc[::-1]
Cc = Cc[::-1]
    
flag = True
ans = []
for i,s in enumerate(S):
    #print(i,s)
    if s == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        elif A == 0:
            A+=1
            B-=1
            ans.append("A")
        elif B == 0:
            A-=1
            B+=1
            ans.append("B")
        else:
            if A-Ac[i] >= B-Bc[i]:
                A-=1
                B+=1
                ans.append("B")
            else:
                A+=1
                B-=1
                ans.append("A")
    if s == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        elif A == 0:
            A+=1
            C-=1
            ans.append("A")
        elif C == 0:
            A-=1
            C+=1
            ans.append("C")
        else:
            if A-Ac[i] >= C-Cc[i]:
                A-=1
                C+=1
                ans.append("C")
            else:
                A+=1
                C-=1
                ans.append("A")
    if s == "BC":
        if C == 0 and B == 0:
            flag = False
            break
        elif C == 0:
            C+=1
            B-=1
            ans.append("C")
        elif B == 0:
            C-=1
            B+=1
            ans.append("B")
        else:
            if C-Cc[i] >= B-Bc[i]:
                C-=1
                B+=1
                ans.append("B")
            else:
                C+=1
                B-=1
                ans.append("C")
                
if flag:
    print("Yes")
    for a in ans:
        print(a)
else:
    print("No")

Submission status: Wrong Answer

Solution code:

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

SL = []
nonZero = 0
nonZero += A>0
nonZero += B>0
nonZero += C>0

for i in range(N):
    s = input()
    SL.append(s)


def op2(f, t):
    global A, B, C
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1

def op(f, t):
    global A, B, C
    if f=="A":
        A -=1
    elif f=="B":
        B -=1
    elif f=="C":
        C -=1
    if t=="A":
        A +=1
    elif t=="B":
        B +=1
    elif t=="C":
        C +=1
    print(t)

def show():
    print("Yes")
    global A, B, C
    for s in SL:
        if s=="AB":
            if A > B:
                op("A", "B")
            else: op("B", "A")
        elif s=="BC":
            if B >C:
                op("B", "C")
            else: op("C", "B")
        elif s=="AC":
            if A >C:
                op("A", "C")
            else: op("C", "A")


def fight():
    global A, B, C
    pA = A
    pB = B
    pC = C
    for s in SL:
        if s=="AB":
            if A > B:
                op2("A", "B")
            else: op2("B", "A")
        elif s=="BC":
            if B >C:
                op2("B", "C")
            else: op2("C", "B")
        elif s=="AC":
            if A >C:
                op2("A", "C")
            else: op2("C", "A")
        if min(A, B, C) <= -1:
            print("No")
            exit()
    else:
        A = pA
        B = pB
        C = pC
        show()



# main


if nonZero >=2:
    show()


elif nonZero == 1:
    if max(A, B, C) >= 2:
        s = SL[0]
        if A!=0:
            if s == "BC":
                print("No")
                exit()
        if B!=0:
            if s == "AC":
                print("No")
                exit()
        if C!=0:
            if s == "AB":
                print("No")
                exit()
        show()
    else:
        fight()
else:
    print("No")

Submission status: Wrong Answer

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 i in range(n):
    s = ss[i]
    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[i][0] > cnt[i][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[i][0] > cnt[i][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[i][2] > cnt[i][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: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
s = []
for i in range(N):
    s.append(input())
   
print("No")

Submission status: Wrong Answer

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

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
            if u <= nAC:
                v = (nAC + nBC + C  - 2 * mAC) // 2
                if u <= v and v <= nBC:
                    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'])

    p = dict()
    p['A'] = A
    p['B'] = B
    p['C'] = C

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
ans_list= []
chk = 0
count = 1
for i in range(N):
    s = input()
    if chk == 1:
        if s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                else:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 2:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 3:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                else:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 0:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    if s == "AB" and A == 0 and B == 0:
        print("No")
        break
    elif s == "BC" and B == 0 and C == 0:
        print("No")
        break
    elif s == "AC" and A == 0 and C == 0:
        print("No")
        break
    else:
        if s == "AB":
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 0:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B != 1 :
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B == 1:
                chk = 1
            else:
                A += 1
                B -= 1
                ans_list.append(["A"])
        elif s == "AC":
            if A == 0:
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 0:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and C != 1 :
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 1 and A != 1:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and C == 1:
                chk = 2
            else:
                A += 1
                C -= 1
                ans_list.append(["A"])
        else:
            if B == 0:
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 0:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C != 1 :
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 1 and B != 1:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C == 1:
                chk = 3
            else:
                B += 1
                C -= 1
                ans_list.append(["B"])
if chk == 1:
    for i in range(count):
        if i%2 == 0:
            A += 1
            B -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            B += 1
            ans_list.append(["B"])
elif chk == 2:
        if i%2 == 0:
            A += 1
            C -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            C += 1
            ans_list.append(["C"])
elif chk == 3:
        if i%2 == 0:
            B += 1
            C -= 1
            ans_list.append(["B"])
        else:
            B -= 1
            C += 1
            ans_list.append(["C"])
if len(ans_list) == N:
    print("Yes")
    for i in range(N):
        print(*ans_list[i])