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:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, A, B, C = mapint()
costs = {'A': A, 'B': B, 'C': C}
old_a, old_b = list(input())

if costs[old_a]==0 and costs[old_b]==0:
    print('No')
    exit()

commands = []
for _ in range(N-1):
    nx_a, nx_b = list(input())
    if costs[old_a]==0 and costs[old_b]==0:
        print('No')
        exit()
    elif costs[old_a]==0:
        costs[old_a] += 1
        costs[old_b] -= 1
        commands.append(old_a)
        
    elif costs[old_b]==0:
        costs[old_b] += 1
        costs[old_a] -= 1
        commands.append(old_b)

    else:
        if old_a==nx_a or old_a==nx_b:
            commands.append(old_a)
            costs[old_a] += 1
        else:
            commands.append(old_b)
            costs[old_b] += 1
    old_a, old_b = nx_a, nx_b

if costs[old_a]==0 and costs[old_b]==0:
    print('No')
    exit()
elif costs[old_a]==0:
    costs[old_a] += 1
    costs[old_b] -= 1
    commands.append(old_a)
    
else:
    costs[old_b] += 1
    costs[old_a] -= 1
    commands.append(old_b)

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

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
valid = True
array = []
for i in range(N):
    s = input()
    if s == "AB":
        if A == B == 0:
            valid = False
            break
        elif A <= B:
            A += 1
            B -= 1
            array.append("A")
        else:
            A -= 1
            B += 1
            array.append("B")
    elif s == "AC":
        if A == C == 0:
            valid = False
            break
        elif A <= C:
            A += 1
            C -= 1
            array.append("A")
        else:
            A -= 1
            C += 1
            array.append("C")
    else:
        if B == C == 0:
            valid = False
            break
        elif B <= C:
            B += 1
            C -= 1
            array.append("B")
        else:
            B -= 1
            C += 1
            array.append("C")
if valid:
    print("Yes")
    for i in range(len(array)):
        print(array[i])
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, a, b, c = map(int, input().split())
S = [input().rstrip() for _ in range(n)]
D = ["AB", "BC", "CA"]
X = [a+b, b+c, c+a]
L = []
for s in S:
  if s == "AC":
    s = "CA"
  k = D.index(s)
  if X[k] == 0:
    print("No")
    exit()
  if X[(k+1)%3] < X[(k+2)%3]:
    X[(k+1)%3] += 1
    X[(k+2)%3] -= 1
    L.append(s[1])
  else:
    X[(k+2)%3] += 1
    X[(k+1)%3] -= 1
    L.append(s[0])
print("Yes")
print(*L, sep="\n")

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
s=[input() for _ in range(n)]
abc=[a,b,c]
d={'A':0,'B':1,'C':2}
dr={0:'A',1:'B',2:'C'}
ans=[]
for i,x in enumerate(s):
  x=[d[y] for y in list(x)]
  if abc[x[0]]==abc[x[1]]==1 and i<n-1:
    nx=s[i+1]
    nx=[d[y] for y in list(s[i+1])]
    for j,y in enumerate(x):
      if y in nx:
        abc[y]+=1
        abc[x[(j+1)%2]]-=1
        ans.append(dr[y])
  elif abc[x[0]]>abc[x[1]]:
    abc[x[1]]+=1
    abc[x[0]]-=1
    ans.append(dr[x[1]])
  else:
    abc[x[0]]+=1
    abc[x[1]]-=1
    ans.append(dr[x[0]])
  for y in abc:
    if y<0:
      print('No')
      exit()
  
print('Yes')
print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
ans=['Yes']
for _ in range(n):
  s=input()
  if s=='AC':
    if a<=c:
      a+=1
      c-=1
      ans+=['A']
    else:
      c+=1
      a-=1
      ans+=['C']
  elif s=='AB':
    if a<b:
      a+=1
      b-=1
      ans+=['A']
    else:
      b+=1
      a-=1
      ans+=['B']
  if s=='BC':
    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')
    break
else:
  print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
ans=['Yes']
for _ in range(n):
  s=input()
  if s=='AC':
    if a<=c:
      a+=1
      c-=1
      ans+=['A']
    else:
      c+=1
      a-=1
      ans+=['C']
  elif s=='AB':
    if a<=b:
      a+=1
      b-=1
      ans+=['A']
    else:
      b+=1
      a-=1
      ans+=['B']
  if s=='BC':
    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')
    break
else:
  print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
SS, res = [], []
for _ in range(N):
    SS.append(input().rstrip())
SS += ["#"]
for i in range(N):
    S = SS[i]
    if S=="AB":
        if A==B==1:
            if SS[i+1]=="BC": A-1, B+1; res.append("B")
            else: A+1, B-1; res.append("A")
        elif A>B: A, B = A-1, B+1; res.append("B")
        else: A, B = A+1, B-1; res.append("A")
    if S=="AC":
        if A==C==1:
            if SS[i+1]=="BC": A-1, C+1; res.append("C")
            else: A+1, C-1; res.append("A")
        elif A>C: A, C = A-1, C+1; res.append("C")
        else: A, C = A+1, C-1; res.append("A")
    if S=="BC":
        if B==C==1:
            if SS[i+1]=="AC": B-1, C+1; res.append("C")
            else: B+1, C-1; res.append("B")
        elif B>C: B, C = B-1, C+1; res.append("C")
        else: B, C = B+1, C-1; res.append("B")
    if A<0 or B<0 or C<0:
        print("No")
        exit()
print("Yes")
print(*res, sep="\n")

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
res = []
for _ in range(N):
    S = input().rstrip()
    if S=="AB":
        if A>B: A, B = A-1, B+1; res.append("B")
        else: A, B = A+1, B-1; res.append("A")
    if S=="AC":
        if A>C: A, C = A-1, C+1; res.append("C")
        else: A, C = A+1, C-1; res.append("A")
    if S=="BC":
        if B>C: B, C = B-1, C+1; res.append("C")
        else: B, C = B+1, C-1; res.append("B")
    if A<0 or B<0 or C<0:
        print("No")
        exit()
print("Yes")
print(*res, sep="\n")

Submission status: Wrong Answer

Solution code:
def f(s):
    if s == "A":
        return A
    elif s == "B":
        return B
    elif s == "C":
        return C


def inc(s):
    global A, B, C

    if s == "A":
        A += 1
    elif s == "B":
        B += 1
    elif s == "C":
        C += 1


def dec(s):
    global A, B, C

    if s == "A":
        A -= 1
    elif s == "B":
        B -= 1
    elif s == "C":
        C -= 1


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

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

elif A + B + C == 1:
    ans = []
    for _ in range(N):
        s = input()
        if f(s[0]) == f(s[1]) == 0:
            print("No")
            exit()
        else:
            if f(s[0]) == 1:
                ans.append(s[1])
                dec(s[0])
                inc(s[1])
            elif f(s[1]) == 1:
                ans.append(s[0])
                inc(s[0])
                dec(s[1])
    print("Yes")
    for i in range(N):
        print(ans[i])

elif A + B + C >= 2:
    s = []
    for i in range(N):
        s.append(input())

    # 共に0を選ぶことは初手でしか起こり得ない
    # 2回目以降は、0は1個以下
    if f(s[0][0]) == f(s[0][1]) == 0:
        print("No")
        exit()

    ans = []
    for i in range(len(s)):
        if f(s[i][0]) >= 1 and f(s[i][1]) == 0:
            ans.append(s[i][1])
            dec(s[i][0])
            inc(s[i][1])
        elif f(s[i][0]) == 0 and f(s[i][1]) >= 1:
            ans.append(s[i][0])
            inc(s[i][0])
            dec(s[i][1])
        # (1,1,0)の状態で(1,1)を選んだ時 and 最後の手ではない and 次に違う手が予定されている
        elif (A + B + C == 2) and (f(s[i][0]) == f(s[i][1]) == 1) and (i < N - 1):
            # s[i][0]を次に操作する場合
            if s[i][0] == s[i + 1][0] or s[i][0] == s[i + 1][1]:
                ans.append(s[i][0])
                inc(s[i][0])
                dec(s[i][1])
            # s[i][1]を次に操作する場合
            elif s[i][1] == s[i + 1][0] or s[i][1] == s[i + 1][1]:
                ans.append(s[i][1])
                dec(s[i][0])
                inc(s[i][1])
        else:
            ans.append(s[i][0])
            inc(s[i][0])
            dec(s[i][1])
    print("Yes")
    # print(ans)
    for i in range(N):
        print(ans[i])

Submission status: Wrong Answer

Solution code:
def f(s):
    if s == "A":
        return A
    elif s == "B":
        return B
    elif s == "C":
        return C


def inc(s):
    global A, B, C

    if s == "A":
        A += 1
    elif s == "B":
        B += 1
    elif s == "C":
        C += 1


def dec(s):
    global A, B, C

    if s == "A":
        A -= 1
    elif s == "B":
        B -= 1
    elif s == "C":
        C -= 1


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

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

elif A + B + C == 1:
    ans = []
    for _ in range(N):
        s = input()
        if f(s[0]) == f(s[1]) == 0:
            print("No")
            exit()
        else:
            if f(s[0]) == 1:
                ans.append(s[1])
                dec(s[0])
                inc(s[1])
            elif f(s[1]) == 1:
                ans.append(s[0])
                inc(s[0])
                dec(s[1])
    print("Yes")
    for i in range(N):
        print(ans[i])

elif A + B + C >= 2:
    s = []
    for i in range(N):
        s.append(input())

    if f(s[0][0]) == f(s[0][1]) == 0:
        print("No")
        exit()

    ans = []
    for i in range(len(s)):
        if f(s[i][0]) >= 1 and f(s[i][1]) == 0:
            ans.append(s[i][1])
            dec(s[i][0])
            inc(s[i][1])
        elif f(s[i][0]) == 0 and f(s[i][1]) >= 1:
            ans.append(s[i][0])
            inc(s[i][0])
            dec(s[i][1])
        elif (A + B + C == 2) and (f(s[i][0]) == f(s[i][1]) == 1) and (i < N - 1) and (s[i] != s[i + 1]):
            if s[i][0] == s[i + 1][0] or s[i][0] == s[i + 1][1]:
                ans.append(s[i][0])
                inc(s[i][0])
                dec(s[i][1])
            elif s[i][1] == s[i + 1][0] or s[i][1] == s[i + 1][1]:
                ans.append(s[i][1])
                dec(s[i][0])
                inc(s[i][1])
        else:
            ans.append(s[i][0])
            inc(s[i][0])
            dec(s[i][1])
    print("Yes")
    for i in range(N):
        print(ans[i])

Submission status: Wrong Answer

Solution code:
def f(s):
    if s == "A":
        return A
    elif s == "B":
        return B
    elif s == "C":
        return C


def inc(s):
    global A, B, C

    if s == "A":
        A += 1
    elif s == "B":
        B += 1
    elif s == "C":
        C += 1


def dec(s):
    global A, B, C

    if s == "A":
        A -= 1
    elif s == "B":
        B -= 1
    elif s == "C":
        C -= 1


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

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

elif A + B + C == 1:
    ans = []
    for _ in range(N):
        s = input()
        if f(s[0]) == f(s[1]) == 0:
            print("No")
            exit()
        else:
            if f(s[0]) == 1:
                ans.append(s[1])
                dec(s[0])
                inc(s[1])
            elif f(s[1]) == 1:
                ans.append(s[0])
                inc(s[0])
                dec(s[1])
    print("Yes")
    for i in range(N):
        print(ans[i])

elif A + B + C >= 2:
    s = []
    for i in range(N):
        s.append(input())

    # 共に0を選ぶことは初手でしか起こり得ない
    # 2回目以降は、0は1個以下
    if f(s[0][0]) == f(s[0][1]) == 0:
        print("No")
        exit()

    ans = []
    for i in range(len(s)):
        if f(s[i][0]) >= 1 and f(s[i][1]) == 0:
            ans.append(s[i][1])
            dec(s[i][0])
            inc(s[i][1])
        elif f(s[i][0]) == 0 and f(s[i][1]) >= 1:
            ans.append(s[i][0])
            inc(s[i][0])
            dec(s[i][1])
        # (1,1,0)の状態で(1,1)を選んだ時 and 最後の手ではない and 次に違う手が予定されている
        elif (A + B + C == 2) and (f(s[0]) == f(s[1]) == 1) and (i < N - 1) and (s[i] != s[i + 1]):
            # s[i][0]を次に操作する場合
            if s[i][0] == s[i + 1][0] or s[i][0] == s[i + 1][1]:
                ans.append(s[i][0])
                inc(s[i][0])
                dec(s[i][1])
            # s[i][1]を次に操作する場合
            elif s[i][1] == s[i + 1][0] or s[i][1] == s[i + 1][1]:
                ans.append(s[i][1])
                dec(s[i][0])
                inc(s[i][1])
        else:
            ans.append(s[i][0])
            inc(s[i][0])
            dec(s[i][1])
    print("Yes")
    # print(ans)
    for i in range(N):
        print(ans[i])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,A,B,C = map(int,input().split())
S = [input().rstrip() for i in range(N)]

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

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

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N,A,B,C = map(int,input().split())
S = [input().rstrip() for i in range(N)]

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

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

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

Submission status: Wrong Answer

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

S = stdin.read().split()


if (A and B or B and C or C and A) and A+B+C >= 3:
    ans = []
    ans.append('Yes')
    for s in S:
        if s == 'AB':
            if A < B:
                A += 1
                B -= 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
        elif s == 'AC':
            if A < C:
                C -= 1
                A += 1
                ans.append('A')
            else:
                A -= 1
                C += 1
                ans.append('C')
        else:
            if B < C:
                C -= 1
                B += 1
                ans.append('B')
            else:
                B -= 1
                C += 1
                ans.append('C')
    print('\n'.join(ans))
    exit()

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if A == B:
            if A == 0:
                print('No')
                exit()
            if i == N-1:
                ans.append('A')
                print('Yes')
                break

            else:
                if S[i+1] == 'AC':
                    A += 1
                    B -= 1
                    ans.append('A')
                elif S[i+1] == 'BC':
                    A -= 1
                    B += 1
                    ans.append('B')
        elif A < B:
            A += 1
            B -= 1
            ans.append('A')
        else:
            A -= 1
            B += 1
            ans.append('B')
    elif s == 'AC':
        if A == C:
            if A == 0:
                print('No')
                exit()
            if i == N-1:
                ans.append('A')
                print('Yes')
                break
            else:
                if S[i+1] == 'AB':
                    A += 1
                    C -= 1
                    ans.append('A')
                elif S[i+1] == 'BC':
                    A -= 1
                    C += 1
                    ans.append('C')
        elif A < C:
            A += 1
            C -= 1
            ans.append('A')
        else:
            A -= 1
            C += 1
            ans.append('C')
    else:
        if B == C:
            if B == 0:
                print('No')
                exit()
            if i == N-1:
                ans.append('B')
                print('Yes')
                break
            else:
                if S[i+1] == 'AB':
                    B += 1
                    C -= 1
                    ans.append('B')
                elif S[i+1] == 'AC':
                    B -= 1
                    C += 1
                    ans.append('C')
        elif B < C:
            B += 1
            C -= 1
            ans.append('B')
        else:
            B -= 1
            C += 1
            ans.append('C')
else:
    print('Yes')
print('\n'.join(ans))

Submission status: Wrong Answer

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

S = stdin.read().split()


if (A and B or B and C or C and A) and A+B+C >= 3:
    ans = []
    ans.append('Yes')
    for s in S:
        if s == 'AB':
            if A < B:
                B -= 1
                A += 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
        elif s == 'AC':
            if A < C:
                C -= 1
                A += 1
                ans.append('A')
            else:
                A -= 1
                C += 1
                ans.append('C')
        else:
            if B < C:
                C -= 1
                B += 1
                ans.append('B')
            else:
                B -= 1
                C += 1
                ans.append('C')
    print('\n'.join(ans))
    exit()

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if A == B:
            if A == 0:
                print('No')
                exit()
            if i == N-1:
                ans.append('A')
                print('Yes')
                break

            else:
                if S[i+1] == 'AC':
                    A += 1
                    B -= 1
                    ans.append('A')
                elif S[i+1] == 'BC':
                    A -= 1
                    B += 1
                    ans.append('B')
        elif A < B:
            A += 1
            B -= 1
            ans.append('A')
        else:
            A -= 1
            B += 1
            ans.append('B')
    elif s == 'AC':
        if A == C:
            if A == 0:
                print('No')
                exit()
            if i == N-1:
                ans.append('A')
                print('Yes')
                break
            else:
                if S[i+1] == 'AB':
                    A += 1
                    C -= 1
                    ans.append('A')
                elif S[i+1] == 'BC':
                    A -= 1
                    C += 1
                    ans.append('C')
        elif A < C:
            A += 1
            C -= 1
            ans.append('A')
        else:
            A -= 1
            C += 1
            ans.append('C')
    else:
        if B == C:
            if B == 0:
                print('No')
                exit()
            if i == N-1:
                ans.append('B')
                print('Yes')
                break
            else:
                if S[i+1] == 'BB':
                    B += 1
                    C -= 1
                    ans.append('B')
                elif S[i+1] == 'BC':
                    B -= 1
                    C += 1
                    ans.append('C')
        elif B < C:
            B += 1
            C -= 1
            ans.append('B')
        else:
            B -= 1
            C += 1
            ans.append('C')
else:
    print('Yes')
print('\n'.join(ans))

Submission status: Wrong Answer

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

S = stdin.read().split()


if (A and B or B and C or C and A) and A+B+C >= 3:
    ans = []
    ans.append('Yes')
    for s in S:
        if s == 'AB':
            if A < B:
                B -= 1
                A += 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
        elif s == 'AC':
            if A < C:
                C -= 1
                A += 1
                ans.append('A')
            else:
                A -= 1
                C += 1
                ans.append('C')
        else:
            if B < C:
                C -= 1
                B += 1
                ans.append('B')
            else:
                B -= 1
                C += 1
                ans.append('C')
    print('\n'.join(ans))
    exit()

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if A == B:
            if A == 0:
                print('No')
                exit()
            if i == N-1:
                ans.append('A')
                print('Yes')
                break

            else:
                if S[i+1] == 'AC':
                    A += 1
                    B -= 1
                    ans.append('A')
                elif S[i+1] == 'BC':
                    A -= 1
                    B += 1
                    ans.append('B')
        elif A < B:
            A += 1
            B -= 1
            ans.append('A')
        else:
            A -= 1
            B += 1
            ans.append('B')
    if s == 'AC':
        if A == C:
            if A == 0:
                print('No')
                exit()
            if i == N-1:
                ans.append('A')
                print('Yes')
                break
            else:
                if S[i+1] == 'AB':
                    A += 1
                    C -= 1
                    ans.append('A')
                elif S[i+1] == 'BC':
                    A -= 1
                    C += 1
                    ans.append('C')
        elif A < C:
            A += 1
            C -= 1
            ans.append('A')
        else:
            A -= 1
            C += 1
            ans.append('C')
    else:
        if B == C:
            if B == 0:
                print('No')
                exit()
            if i == N-1:
                ans.append('B')
                print('Yes')
                break
            else:
                if S[i+1] == 'BB':
                    B += 1
                    C -= 1
                    ans.append('B')
                elif S[i+1] == 'BC':
                    B -= 1
                    C += 1
                    ans.append('C')
        elif B < C:
            B += 1
            C -= 1
            ans.append('B')
        else:
            B -= 1
            C += 1
            ans.append('C')
print('\n'.join(ans))

Submission status: Wrong Answer

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