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:
# -*- coding: utf-8 -*-
import sys 
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
Q, A, B, C = map(int, readline().split())
N = A + B + C
SS = []
for _ in range(Q):
    S = readline().decode().rstrip()
    a = 0 if S == 'AB' else 1 if S == 'AC' else 2 
    SS.append(a)
ans = [] 
if N == 0:
    print('No')
    sys.exit()
elif N == 1:
    nums = [A,B,C]
    for i in range(Q):
        num = SS[i]
        i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)  
        if nums[i] == nums[j]:
            print('No')
            sys.exit()
        if nums[i] > nums[j]:
            i,j = j,i
        nums[i] += 1
        nums[j] -= 1
else:
    nums = [A,B,C]
    num = SS[0]
    i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2) 
    if nums[i] == 0 and nums[j] == 0:
        print('No')
        sys.exit()
    if N == 2:
        for i in range(Q):
            num = SS[i]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] == 1 and nums[j] == 1:
                if i < Q-1 and SS[i] != SS[i+1]:
                    ni,nj = (0,1) if SS[i+1] == 0 else (0,2) if SS[i+1] == 1 else (1,2)
                    a = Counter([i,j,ni,nj])
                    b = a.most_common()[0][0] ## 2つあるの探す 
                    if j == b:
                        i,j = j,i   
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
    else:
        for i in range(Q):
            num = SS[i]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
        
print('Yes')
ABC = 'ABC'
answer = list(ABC[i] for i in ans)
print('\n'.join(answer))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys 
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
Q, A, B, C = map(int, readline().split())
N = A + B + C
SS = []
for _ in range(Q):
    S = readline().decode().rstrip()
    a = 0 if S == 'AB' else 1 if S == 'AC' else 2 
    SS.append(a)
ans = [] 
if N == 0:
    print('No')
    sys.exit()
elif N == 1:
    nums = [A,B,C]
    for i in range(Q):
        num = SS[i]
        i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)  
        if nums[i] == nums[j]:
            print('No')
            sys.exit()
        if nums[i] > nums[j]:
            i,j = j,i
        nums[i] += 1
        nums[j] -= 1
else:
    nums = [A,B,C]
    num = SS[0]
    i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2) 
    if nums[i] == 0 and nums[j] == 0:
        print('No')
        sys.exit()
    if N == 2:
        for i in range(Q):
            num = SS[i]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] == 1 and nums[j] == 1:
                if SS[i] != SS[i+1]:
                    ni,nj = (0,1) if SS[i+1] == 0 else (0,2) if SS[i+1] == 1 else (1,2)
                    a = Counter([i,j,ni,nj])
                    b = a.most_common()[0][0] ## 2つあるの探す 
                    if j == b:
                        i,j = j,i   
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
    else:
        for i in range(Q):
            num = SS[i]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
        
print('Yes')
ABC = 'ABC'
answer = list(ABC[i] for i in ans)
print('\n'.join(answer))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys 
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
Q, A, B, C = map(int, readline().split())
N = A + B + C
SS = []
for _ in range(Q):
    S = readline().decode().rstrip()
    a = 0 if S == 'AB' else 1 if S == 'AC' else 2 
    SS.append(a)
ans = [] 
if N == 0:
    print('No')
    sys.exit()
elif N == 1:
    nums = [A,B,C]
    for i in range(Q):
        num = SS[i]
        i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)  
        if nums[i] == nums[j]:
            print('No')
            sys.exit()
        if nums[i] > nums[j]:
            i,j = j,i
        nums[i] += 1
        nums[j] -= 1
else:
    nums = [A,B,C]
    num = SS[0]
    i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2) 
    if nums[i] == 0 and nums[j] == 0:
        print('No')
        sys.exit()
    if N == 2:
        for i in range(Q):
            num = SS[i]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] == 1 and nums[j] == 1:
                if SS[i] != SS[i+1]:
                    ni,nj = (0,1) if SS[i+1] == 1 else (0,2) if SS[i+1] == 2 else (1,2)
                    a = Counter([i,j,ni,nj])
                    b = a.most_common()[0][0] ## 2つあるの探す   
                    if j == b:
                        i,j = j,i   
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
    else:
        for i in range(Q):
            num = SS[i]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
        
print('Yes')
ABC = 'ABC'
answer = list(ABC[i] for i in ans)
print('\n'.join(answer))

Submission status: Wrong Answer

Solution code:
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, A, B, C = map(int, readline().split())
ans = ''
for _ in range(N):
    S = readline().decode().rstrip()
    if S == 'AB':
        if A < B:
            A += 1; B -= 1
            ans += 'A'
        else:
            B += 1; A -= 1
            ans += 'B'
    elif S == 'AC':
        if A < C:
            A += 1; C -= 1
            ans += 'A'
        else:
            C += 1; A -= 1
            ans += 'C'
    else:
        if B < C:
            B += 1; C -= 1
            ans += 'B'
        else:
            C += 1; B -= 1
            ans += 'C'
    if A < 0 or B < 0 or C < 0:
        print('No')
        sys.exit()
print('Yes')
print('\n'.join(ans))

Submission status: Wrong Answer

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

if A+B+C == 0:
    print('No')
    exit()

D = []

if A+B+C == 1:
    for i in range(N):
        if (A == 1 and s[i] == 'BC') or (B == 1 and s[i] == 'AC') or (C == 1 and s[i] == 'AB'):
            print('No')
            exit()
        else:
            if A == 1:
                if s[i] == 'AB':
                    A = 0
                    B = 1
                    D.append('B')
                else:
                    A = 0
                    C = 1
                    D.append('C')
            elif B == 1:
                if s[i] == 'AB':
                    B = 0
                    A = 1
                    D.append('A')
                else:
                    B = 0
                    C = 1
                    D.append('C')
            else:
                if s[i] == 'BC':
                    C = 0
                    B = 1
                    D.append('B')
                else:
                    C = 0
                    A = 1
                    D.append('A')
    print('Yes')
    print(*D,sep='\n')
    exit()

if (B == 0 and C == 0 and s[0] == 'BC') or (A == 0 and C == 0 and s[0] == 'AC') or (A == 0 and B == 0 and s[0] == 'AB'):
    print('No')
    exit()

print('Yes')

for i in range(N):
    if A+B+C > 2:
        if A == 0 and s[i] == 'AB':
            D.append('A')
            A = 1
            B -= 1
        elif A == 0 and s[i] == 'AC':
            D.append('A')
            A = 1
            C -= 1
        elif B == 0 and s[i] == 'AB':
            D.append('B')
            B = 1
            A -= 1
        elif B == 0 and s[i] == 'BC':
            D.append('B')
            B = 1
            C -= 1
        elif C == 0 and s[i] == 'AC':
            D.append('C')
            C = 1
            A -= 1
        elif C == 0 and s[i] == 'BC':
            D.append('C')
            C = 1
            B -= 1
        elif s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
    else:  # A+B+C == 2 の場合
        if A == 1 and B == 1 and s[i] == 'AB':
            if i <= N-2 and s[i+1] == 'BC':
                D.append('B')
                A -= 1
                B += 1
            else:
                D.append('A')
                A += 1
                B -= 1
        elif A == 1 and C == 1 and s[i] == 'AC':
            if i <= N-2 and s[i+1] == 'AB':
                D.append('A')
                A += 1
                C -= 1
            else:
                D.append('C')
                A -= 1
                C += 1
        elif B == 1 and C == 1 and s[i] == 'BC':
            if i <= N-2 and s[i+1] == 'AB':
                D.append('B')
                B += 1
                C -= 1
            else:
                D.append('C')
                B -= 1
                C += 1
        elif A == 0 and s[i] == 'AB':
            D.append('A')
            A = 1
            B -= 1
        elif A == 0 and s[i] == 'AC':
            D.append('A')
            A = 1
            C -= 1
        elif B == 0 and s[i] == 'AB':
            D.append('B')
            B = 1
            A -= 1
        elif B == 0 and s[i] == 'BC':
            D.append('B')
            B = 1
            C -= 1
        elif C == 0 and s[i] == 'AC':
            D.append('C')
            C = 1
            A -= 1
        elif C == 0 and s[i] == 'BC':
            D.append('C')
            C = 1
            B -= 1
        elif s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
print(*D,sep='\n')

Submission status: Wrong Answer

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

if A+B+C == 0:
    print('No')
    exit()
if A+B+C == 1:
    D = []
    for i in range(N):
        if (A == 1 and s[i] == 'BC') or (B == 1 and s[i] == 'AC') or (C == 1 and s[i] == 'AB'):
            print('No')
            exit()
        else:
            if A == 1:
                if s[i] == 'AB':
                    A = 0
                    B = 1
                    D.append('B')
                else:
                    A = 0
                    C = 1
                    D.append('C')
            elif B == 1:
                if s[i] == 'AB':
                    B = 0
                    A = 1
                    D.append('A')
                else:
                    B = 0
                    C = 1
                    D.append('C')
            else:
                if s[i] == 'BC':
                    C = 0
                    B = 1
                    D.append('B')
                else:
                    C = 0
                    A = 1
                    D.append('A')
    print('Yes')
    for i in range(N):
        print(D[i])
    exit()

if (B == 0 and C == 0 and s[0] == 'BC') or (A == 0 and C == 0 and s[0] == 'AC') or (A == 0 and B == 0 and s[0] == 'AB'):
    print('No')
    exit()

print('Yes')

D = []

for i in range(N):
    if A+B+C > 2:
        if A == 0 and s[i] == 'AB':
            D.append('A')
            A = 1
            B -= 1
        elif A == 0 and s[i] == 'AC':
            D.append('A')
            A = 1
            C -= 1
        elif B == 0 and s[i] == 'AB':
            D.append('B')
            B = 1
            A -= 1
        elif B == 0 and s[i] == 'BC':
            D.append('B')
            B = 1
            C -= 1
        elif C == 0 and s[i] == 'AC':
            D.append('C')
            C = 1
            A -= 1
        elif C == 0 and s[i] == 'BC':
            D.append('C')
            C = 1
            B -= 1
        elif s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
    else:
        if A == 1 and B == 1 and s[i] == 'AB':
            if i <= N-2 and s[i+1] == 'BC':
                D.append('B')
                A -= 1
                B += 1
            else:
                D.append('A')
                A += 1
                B -= 1
        elif A == 1 and C == 1 and s[i] == 'AC':
            if i <= N-2 and s[i+1] == 'AB':
                D.append('A')
                A += 1
                C -= 1
            else:
                D.append('C')
                A -= 1
                C += 1
        elif B == 1 and C == 1 and s[i] == 'BC':
            if i <= N-2 and s[i+1] == 'AB':
                D.append('B')
                B += 1
                C -= 1
            else:
                D.append('C')
                B -= 1
                C += 1
        elif A == 0 and s[i] == 'AB':
            D.append('A')
            A = 1
            B -= 1
        elif A == 0 and s[i] == 'AC':
            D.append('A')
            A = 1
            C -= 1
        elif B == 0 and s[i] == 'AB':
            D.append('B')
            B = 1
            A -= 1
        elif B == 0 and s[i] == 'BC':
            D.append('B')
            B = 1
            C -= 1
        elif C == 0 and s[i] == 'AC':
            D.append('C')
            C = 1
            A -= 1
        elif C == 0 and s[i] == 'BC':
            D.append('C')
            C = 1
            B -= 1
        elif s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
for i in range(N):
    print(D[i])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys

N,A,B,C = list(map(int, input().rstrip().split()))
s_list = [input().strip() for _ in range(N)]
#-----

def calc(select :str, x :int, y :int, cnt_x :int, cnt_y :int) -> (int,int,int,int):
    if x == y == 0:
        print("No")
        sys.exit()
    
    
    if (x == 0)  or  (x - cnt_x) <= (y - cnt_y):
        x +=1
        y -= 1
        cnt_x -= 1
        cnt_y -= 1
        ans_add.append(select[0])
    
    elif (y == 0)  or  (x - cnt_x) > (y - cnt_y):
        x -=1
        y += 1
        cnt_x -= 1
        cnt_y -= 1
        ans_add.append(select[1])
    
    if (x < 0) or (y < 0):
        print("No")
        sys.exit()
    
    return (x, y, cnt_x, cnt_y)

#-----

# counter of the number of A , B, C .
cnt = [0,0,0]


for s in s_list:
    if s == "AB":
        cnt[0] += 1
        cnt[1] += 1
    elif s == "AC":
        cnt[0] += 1
        cnt[2] += 1
    elif s == "BC":
        cnt[1] += 1
        cnt[2] += 1


ans_add = []

for s in s_list:
    if s == "AB":
        A, B, cnt[0], cnt[1] = calc(s, A, B, cnt[0], cnt[1])
    
    elif s == "AC":
        A, C, cnt[0], cnt[2] = calc(s, A, C, cnt[0], cnt[2])
    
    elif s == "BC":
        B, C, cnt[1], cnt[2] = calc(s, B, C, cnt[1], cnt[2])


print("Yes")

for s in ans_add:
    print(s)

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B, C = list(map(int, input().split(' ')))
    S = [input() for _ in range(N)]
    D = {
        'AB': A + B,
        'BC': B + C,
        'AC': A + C
    }
    ans = list()
    for s in S:
        if D[s] == 0:
            print('No')
            return
        if s == 'AB':
            if D['BC'] < D['AC']:
                ans.append('B')
                D['BC'] += 1
                D['AC'] -= 1
            else:
                ans.append('A')
                D['BC'] -= 1
                D['AC'] += 1
        elif s == 'BC':
            if D['AB'] < D['AC']:
                ans.append('B')
                D['AB'] += 1
                D['AC'] -= 1
            else:
                ans.append('C')
                D['AB'] -= 1
                D['AC'] += 1
        else:  # s == 'AC'
            if D['AB'] < D['BC']:
                ans.append('A')
                D['AB'] += 1
                D['BC'] -= 1
            else:
                ans.append('C')
                D['AB'] -= 1
                D['BC'] += 1
    print('Yes')
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#import numpy as np
#import math
#from decimal import *
#from numba import njit

#@njit
def main():
    N,A,B,C = map(int, input().split())
    array = [A,B,C]
    string = ['A','B','C']
    si = [] # AB:0,BC:1,AC:2
    if A+B+C <= 0:
        print('No')
        return
    for _ in range(N):
        s = input()
        si += (0 if s == 'AB' else 1 if s == 'BC' else 2),

    ans = []
    for i in range(len(si)):
        s = si[i]
        index = -3 if s == 0 else -2 if s == 1 else -1
        if array[index] == 0:
            array[index] += 1
            array[index+1] -= 1
            ans += string[index],
        elif array[index+1] == 0:
            array[index] -= 1
            array[index+1] += 1
            ans += string[index+1],
        elif array[index] == 1 and array[index+1] == 1:
            if i+1 >= len(si):
                array[index] += 1
                array[index+1] -= 1
                ans += string[index],
            else:
                if si[i+1] == 1:
                    array[index] += 1
                    array[index+1] -= 1
                    ans += string[index],
                else:
                    array[index] -= 1
                    array[index+1] += 1
                    ans += string[index+1],
        elif array[index] == 1:
            array[index] += 1
            array[index+1] -= 1
            ans += string[index],
        else:
            array[index] -= 1
            array[index+1] += 1
            ans += string[index+1],

        if array[0] < 0 or array[1] < 0 or array[2] < 0:
            print('No')
            return
        
    print('Yes')
    for s in ans:
        print(s)

main()

Submission status: Wrong Answer

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    def conv(s):
        ret = []
        for i in s:
            if i == 'A':
                ret.append(0)
            elif i == 'B':
                ret.append(1)
            else:
                ret.append(2)
        return ret

    def inv(i):
        if i == 0:
            return 'A'
        elif i == 1:
            return 'B'
        else:
            return 'C'

    def answer(ans):
        print('Yes')
        for a in ans:
            print(a)
        return -1

    def add_answer(x):
        if s[x[0]] > s[x[1]]:
            s[x[0]] -= 1
            s[x[1]] += 1
            ans.append(inv(x[1]))
        else:
            s[x[0]] += 1
            s[x[1]] -= 1
            ans.append(inv(x[0]))
        
    n, *s = map(int, input().split())
    idx_list = []
    for i in range(n):
        idx_list.append(conv(input()))

    ans = []
    if sum(s) == 0:
        print('No')
        exit(0)
    elif sum(s) == 1:
        for x in idx_list:
            if s[x[0]] == s[x[1]]:
                print('No')
                exit(0)
            add_answer(x)
        answer(ans)
    else:
        for i, x in enumerate(idx_list):
            if s[x[0]] == 0 and s[x[1]] == 0:
                print('No')
                exit(0)
            if i < n-1 and s[x[0]] == 1 and s[x[1]] == 1:
                nx = idx_list[i+1]
                if x != nx:
                    if x[0] in nx:
                        s[x[0]] += 1
                        s[x[1]] -= 1
                        ans.append(inv(x[0]))
                    else:
                        s[x[0]] -= 1
                        s[x[1]] += 1
                        ans.append(inv(x[1]))
            else:
                add_answer(x)
        answer(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans_list=[]
for _ in range(N):
  com=input()
  if com=="AB":
    if A>=B:
      A-=1
      B+=1
      ans_list.append("B")
    else:
      A+=1
      B-=1
      ans_list.append("A")
  elif com=="AC":
    if A>=C:
      A-=1
      C+=1
      ans_list.append("C")
    else:
      A+=1
      C-=1
      ans_list.append("A")
  else:
    if B>=C:
      B-=1
      C+=1
      ans_list.append("C")
    else:
      B+=1
      C-=1
      ans_list.append("B")
      
  if A<0 or B<0 or C<0:
    print("No")
    break
else:
  print("Yes")
  for ans in ans_list:
    print(ans)

Submission status: Wrong Answer

Solution code:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n,a,b,c = MAP()

ans = []
flag = 0
for _ in range(n):
    zz = input()
    if(zz=='AB'):
        if(a<b):
            a+=1
            b-=1
            ans.append("A")
        else:
            a-=1
            b+=1
            ans.append("B")
    elif(zz=="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):
        flag = 1
        break
if(flag==1):
    print("No")
else:
    print("Yes")
    for el in ans:
        print(el)

Submission status: Wrong Answer

Solution code:
import sys
K = list(map(int,input().split()))
N = K[0]
S = sum(K[1:3])

if S == 0:
  print("No")
  sys.exit()

s = input()
out = [0 for _ in range(N)]

for ct in range(N):
  #print(K)
  if ct < N-1:
    s_next = input()
    
  if s == "AB":
    i = 1
    j = 2
  elif s == "BC":
    i = 2
    j = 3
  else:
    i = 1
    j = 3
  
  if K[i] + K[j] == 0:
      print("No")
      sys.exit()
  elif K[i] > K[j]:
    K[i] -= 1
    K[j] += 1
    log = j
  elif K[i] < K[j]:
    K[i] += 1
    K[j] -= 1
    log = i
  elif K[i] == K[j]:
    if ct == N-1:
      K[i] += 1
      K[j] -= 1
      log = i
      out[ct] = i
      break
      
    if s_next == "AB":
      i_n = 1
      j_n = 2
    elif s_next == "BC":
      i_n = 2
      j_n = 3
    else:
      i_n = 1
      j_n = 3
      
    if i_n == i or j_n == i:
      K[i] += 1
      K[j] -= 1
      log = i
    else:
      K[j] += 1
      K[i] -= 1  
      log = j
  #print(log)
  out[ct] = log
  if ct < N-1:
    s = s_next

print("Yes")
for o in out:
  print(chr(64+o))

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N, A, B, C = in_nn()

    ans = []
    for i in range(N):
        s = in_s()
        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                exit()
            elif A == 0:
                A += 1
                B -= 1
                ans.append('A')
            elif B == 0:
                A -= 1
                B += 1
                ans.append('B')
            elif A > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
        elif s == 'BC':
            if B == 0 and C == 0:
                print('No')
                exit()
            elif B == 0:
                B += 1
                C -= 1
                ans.append('B')
            elif C == 0:
                B -= 1
                C += 1
                ans.append('C')
            elif B > C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                exit()
            elif A == 0:
                A += 1
                C -= 1
                ans.append('A')
            elif C == 0:
                A -= 1
                C += 1
                ans.append('C')
            elif A > C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
choice = []
ok=1
for i in range(N):
    s = input()
    if s=='AB':
        if A >= B:
            A -= 1
            B += 1
            choice.append('B')
        else:
            A += 1
            B -= 1
            choice.append('A')
    if s=='AC':
        if A >= C:
            A -= 1
            C += 1
            choice.append('C')
        else:
            A += 1
            C -= 1
            choice.append('A')
    if s=='BC':
        if B >= C:
            B -= 1
            C += 1
            choice.append('C')
        else:
            B += 1
            C -= 1
            choice.append('B')

    if (A < 0) or (B < 0) or (C < 0):
        print('No')
        ok=0
        break

if ok == 1:
  print('Yes')
  for i in range(N):
      print(choice[i])

Submission status: Wrong Answer

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