Problem:


Score : 600 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

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

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

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

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

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

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

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

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

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

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
s=[input()for _ in range(n)]
if (a+b+c)==0:
  print("No")
if (a+b+c)==1:
  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
if (a+b+c)>=3:
  print("Yes")
  for i in s:
    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

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int,input().split())
ans = []
flag = True
cnt = 0
while True:
  cnt += 1
  if cnt > N:
    break
  s = input()
  if s == 'AB':
    if A == B == 0:
      flag = False
      break
    elif A == B == 1 and C == 0:
      cnt += 1
      if cnt > N:
        ans.append('A')
        break
      else:
        s = input()
        if s == 'AB':
          ans.append('A')
          ans.append('B')
          continue
        elif s == 'AC':
          ans.append('A')
          ans.append('C')
          B -= 1
          C += 1
          continue
        else:
          ans.append('B')
          ans.append('C')
          A -= 1
          C += 1
          continue
    elif A >= B:
      A -= 1
      B += 1
      ans.append('B')
      continue
    else:
      B -= 1
      A += 1
      ans.append('A')
      continue
  elif s == 'BC':
    if B == C == 0:
      flag = False
      break
    elif B == C == 1 and A == 0:
      cnt += 1
      if cnt > N:
        ans.append('B')
        break
      else:
        s = input()
        if s == 'BC':
          ans.append('B')
          ans.append('C')
          continue
        elif s == 'AB':
          ans.append('B')
          ans.append('A')
          B -= 1
          A += 1
          continue
        else:
          ans.append('C')
          ans.append('A')
          C -= 1
          A += 1
          continue
    elif B >= C:
      B -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      B += 1
      ans.append('B')
      continue 
  else:
    if C == A == 0:
      flag = False
      break
    elif C == A == 1 and B == 0:
      cnt += 1
      if cnt > N:
        ans.append('C')
        break
      else:
        s = input()
        if s == 'AC':
          ans.append('C')
          ans.append('A')
          continue
        elif s == 'BC':
          ans.append('C')
          ans.append('B')
          A -= 1
          B += 1
          continue
        else:
          ans.append('A')
          ans.append('B')
          C -= 1
          B += 1
          continue
    elif A >= C:
      A -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      A += 1
      ans.append('A')
      continue
if flag:
  print('Yes')
  for item in ans:
    print(item)
else:
  print('No')

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int,input().split())
ans = []
flag = True
cnt = 0
while True:
  if A < 0 or B < 0 or C < 0:
    flag = False
    break
  cnt += 1
  if cnt > N:
    break
  s = input()
  if s == 'AB':
    if A == B == 1 and C == 0:
      cnt += 1
      if cnt > N:
        ans.append('A')
        break
      else:
        s = input()
        if s == 'AB':
          ans.append('A')
          ans.append('B')
          continue
        elif s == 'AC':
          ans.append('A')
          ans.append('C')
          B -= 1
          C += 1
          continue
        else:
          ans.append('B')
          ans.append('C')
          A -= 1
          C += 1
          continue
    elif A >= B:
      A -= 1
      B += 1
      ans.append('B')
      continue
    else:
      B -= 1
      A += 1
      ans.append('A')
      continue
  elif s == 'BC':
    if B == C == 1 and A == 0:
      cnt += 1
      if cnt > N:
        ans.append('B')
        break
      else:
        s = input()
        if s == 'BC':
          ans.append('B')
          ans.append('C')
          continue
        elif s == 'AB':
          ans.append('B')
          ans.append('A')
          B -= 1
          A += 1
          continue
        else:
          ans.append('C')
          ans.append('A')
          C -= 1
          A += 1
          continue
    elif B >= C:
      B -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      B += 1
      ans.append('B')
      continue 
  else:
    if C == A == 1 and B == 0:
      cnt += 1
      if cnt > N:
        ans.append('C')
        break
      else:
        s = input()
        if s == 'AC':
          ans.append('C')
          ans.append('A')
          continue
        elif s == 'BC':
          ans.append('C')
          ans.append('B')
          A -= 1
          B += 1
          continue
        else:
          ans.append('A')
          ans.append('B')
          C -= 1
          B += 1
          continue
    elif A >= C:
      A -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      A += 1
      ans.append('A')
      continue
if flag:
  print('Yes')
  for item in ans:
    print(item)
else:
  print('No')

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int,input().split())
ans = []
flag = True
cnt = 0
while True:
  if A < 0 or B < 0 or C < 0:
    flag = False
    break
  cnt += 1
  if cnt > N:
    break
  s = input()
  if s == 'AB':
    if A == B == 1:
      cnt += 1
      if cnt > N:
        ans.append('A')
        break
      else:
        s = input()
        if s == 'AB':
          ans.append('A')
          ans.append('B')
          continue
        elif s == 'AC':
          ans.append('A')
          ans.append('C')
          B -= 1
          C += 1
          continue
        else:
          ans.append('B')
          ans.append('C')
          A -= 1
          C += 1
          continue
    elif A >= B:
      A -= 1
      B += 1
      ans.append('B')
      continue
    else:
      B -= 1
      A += 1
      ans.append('A')
      continue
  elif s == 'BC':
    if B == C == 1:
      cnt += 1
      if cnt > N:
        ans.append('B')
        break
      else:
        s = input()
        if s == 'BC':
          ans.append('B')
          ans.append('C')
          continue
        elif s == 'AB':
          ans.append('B')
          ans.append('A')
          B -= 1
          A += 1
          continue
        else:
          ans.append('C')
          ans.append('A')
          C -= 1
          A += 1
          continue
    elif B >= C:
      B -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      B += 1
      ans.append('B')
      continue 
  else:
    if C == A == 1:
      cnt += 1
      if cnt > N:
        ans.append('C')
        break
      else:
        s = input()
        if s == 'AC':
          ans.append('C')
          ans.append('A')
          continue
        elif s == 'BC':
          ans.append('C')
          ans.append('B')
          A -= 1
          B += 1
          continue
        else:
          ans.append('A')
          ans.append('B')
          C -= 1
          B += 1
          continue
    elif A >= C:
      A -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      A += 1
      ans.append('A')
      continue
if flag:
  print('Yes')
  for item in ans:
    print(item)
else:
  print('No')

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int,input().split())
ans = []
flag = True
cnt = 0
while True:
  if A < 0 or B < 0 or C < 0:
    flag = False
    break
  cnt += 1
  if cnt > N:
    break
  s = input()
  if s == 'AB':
    if A == B == 1:
      cnt += 1
      if cnt > N:
        ans.append('A')
        break
      else:
        s = input()
        if s == 'AB':
          ans.append('A')
          ans.append('B')
          continue
        elif s == 'AC':
          ans.append('A')
          ans.append('C')
          B -= 1
          C += 1
          continue
        else:
          ans.append('B')
          ans.append('C')
          A -= 1
          C += 1
          continue
    elif A >= B:
      A -= 1
      B += 1
      ans.append('B')
      continue
    else:
      B -= 1
      A += 1
      ans.append('A')
      continue
  elif s == 'BC':
    if B == C == 1:
      cnt += 1
      if cnt > N:
        ans.append('B')
        break
      else:
        s = input()
        if s == 'BC':
          ans.append('B')
          ans.append('C')
          continue
        elif s == 'AB':
          ans.append('B')
          ans.append('A')
          B -= 1
          A += 1
          continue
        else:
          ans.append('C')
          ans.append('A')
          C -= 1
          A += 1
          continue
    elif B >= C:
      B -= 1
      C += 1
      ans.append('B')
      continue
    else:
      C -= 1
      B += 1
      ans.append('C')
      continue 
  else:
    if C == A == 1:
      cnt += 1
      if cnt > N:
        ans.append('C')
        break
      else:
        s = input()
        if s == 'AC':
          ans.append('C')
          ans.append('A')
          continue
        elif s == 'BC':
          ans.append('C')
          ans.append('B')
          A -= 1
          B += 1
          continue
        else:
          ans.append('A')
          ans.append('B')
          C -= 1
          B += 1
          continue
    elif A >= C:
      A -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      A += 1
      ans.append('A')
      continue
if flag:
  print('Yes')
  for item in ans:
    print(item)
else:
  print('No')

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    def calc(x, y):
        cnt[x] += 1
        cnt[y] += 1
        res.append(S[x])

    n, *cnt = map(int, input().split())
    S = "ABC"
    query = []
    for _ in range(n):
        s = input().rstrip()
        a = 0 if s == "AB" else 0 if s == "AC" else 1
        b = 1 if s == "AB" else 2 if s == "AC" else 2
        query.append((a, b))
    query.append((None, None))

    res = []
    for i in range(n):
        a, b = query[i]
        if cnt[a] == cnt[b] == 0:
            print("No")
            return
        elif cnt[a] == 0:
            calc(a, b)
        elif cnt[b] == 0:
            calc(b, a)
        else:
            next_a, next_b = query[i + 1]
            if a == next_a or a == next_b:
                calc(a, b)
            else:
                calc(b, a)
    print("Yes")
    print(*res, sep="\n")


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
n = [A, B, C]
c = ["A", "B", "C"]
S = [input() for i in range(N)]
op = []

for i in range(N):
	if S[i] == "AB":
		op.append((0, 1))
	elif S[i] == "BC":
		op.append((1, 2))
	else:
		op.append((2, 0))

dec = []
for i in range(N):
	j1 = op[i][0]
	j2 = op[i][1]
	if n[j1] < n[j2]:
		dec.append(c[j1])
		n[j1] += 1
		n[j2] -= 1
	elif n[j2] < n[j1]:
		dec.append(c[j2])
		n[j2] += 1
		n[j1] -= 1
	elif i < N - 1:
		if j1 in op[i + 1]:
			if j1 == op[i + 1][0]:
				j3 = op[i + 1][1]
			else:
				j3 = op[i + 1][0]
			if n[j3] < n[j1]:
				dec.append(c[j2])
				n[j2] += 1
				n[j1] -= 1
			else:
				dec.append(c[j1])
				n[j1] += 1
				n[j2] -= 1
		else:
			if j2 == op[i + 1][0]:
				j3 = op[i + 1][1]
			else:
				j3 = op[i + 1][0]
			if n[j3] < n[j2]:
				dec.append(c[j1])
				n[j1] += 1
				n[j2] -= 1
			else:
				dec.append(c[j2])
				n[j2] += 1
				n[j1] -= 1
	else:
		dec.append(c[j1])
		n[j1] += 1
		n[j2] -= 1
	if n[0] < 0 or n[1] < 0 or n[2] < 0:
		print("No")
		exit()
print("Yes")
print("\n".join(dec))

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
cnt = [0] * 3
S = [input() for i in range(N)]
for s in S:
	if s == "AB":
		cnt[0] += 1
		cnt[1] += 1
	elif s == "BC":
		cnt[1] += 1
		cnt[2] += 1
	else:
		cnt[2] += 1
		cnt[0] += 1

choices = []
for s in S:
	if s == "AB":
		a = A - cnt[0]
		b = B - cnt[1]
		cnt[0] -= 1
		cnt[1] -= 1
		if a < b:
			A += 1
			B -= 1
			choices.append("A")
		else:
			A -= 1
			B += 1
			choices.append("B")
	elif s == "BC":
		b = B - cnt[1]
		c = C - cnt[2]
		cnt[1] -= 1
		cnt[2] -= 1
		if b < c:
			B += 1
			C -= 1
			choices.append("B")
		else:
			B -= 1
			C += 1
			choices.append("C")
	else:
		c = C - cnt[2]
		a = A - cnt[0]
		cnt[2] -= 1
		cnt[0] -= 1
		if c < a:
			C += 1
			A -= 1
			choices.append("C")
		else:
			C -= 1
			A += 1
			choices.append("A")
	if A < 0 or B < 0 or C < 0:
		print("No")
		exit()
print("Yes")
for i in range(N):
	print(choices[i])

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
dic = {'A':a,'B':b,'C':c}
s = [input() for i in range(n)]
ans = ["Yes"]
for i in range(n):
    if dic[s[i][0]] == 0 and dic[s[i][1]] == 0:
        print("No")
        exit()
    elif dic[s[i][0]] == 0:
        dic[s[i][0]] += 1
        dic[s[i][1]] -= 1
        ans.append(s[i][0])
    elif dic[s[i][1]] == 0:
        dic[s[i][1]] += 1
        dic[s[i][0]] -= 1
        ans.append(s[i][1])
    elif a+b+c == 2 and i != n-1:
        if s[i][0] in s[i+1]:
            dic[s[i][0]] += 1
            dic[s[i][1]] -= 1
            ans.append(s[i][0])
        else:
            dic[s[i][1]] += 1
            dic[s[i][0]] -= 1
            ans.append(s[i][1])
    else:
        dic[s[i][0]] += 1
        dic[s[i][1]] -= 1
        ans.append(s[i][0])

print(*ans,sep="\n")

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
dic = {'A':a,'B':b,'C':c}
s = [input() for i in range(n)]
ans = ["Yes"]
for i in range(n):
    if dic[s[i][0]] == 0 and dic[s[i][1]] == 0:
        print("No")
        exit()
    elif dic[s[i][0]] == 0:
        dic[s[i][0]] += 1
        dic[s[i][1]] -= 1
        ans.append(s[i][0])
    elif dic[s[i][1]] == 0:
        dic[s[i][1]] += 1
        dic[s[i][0]] -= 1
        ans.append(s[i][1])
    elif a+b+c == 2 and i != n-1:
        if s[i][0] in s[i+1]:
            dic[s[i][0]] += 1
            dic[s[i][1]] -= 1
            ans.append(s[i][0])
        else:
            dic[s[i][1]] += 1
            dic[s[i][0]] -= 1
            ans.append(s[i][1])
    else:
        dic[s[i][0]] += 1
        dic[s[i][1]] -= 1
        ans.append(s[i][0])
print(ans)

Submission status: Wrong Answer

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

if sum([A,B,C])==0:
  print('No')
  exit()

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

if sum([A,B,C])==1:
  for i in range(N):
    if s[i]=='AB':
      if A<=B:
        A += 1
        B -= 1
      else:
        A -= 1
        B += 1
    elif s[i]=='BC':
      if B<=C:
        B += 1
        C -= 1
      else:
        B -= 1
        C += 1
    else:
      if C<=A:
        C += 1
        A -= 1
      else:
        C -= 1
        A += 1
    if min(A,B,C)<0:
      print('No')
      exit()
    
print('Yes')
for i in range(N):
    if s[i]=='AB':
      if (A,B)==(1,1):
        if i<N-1 and 'A' in s[i+1]:
          A += 1
          B -= 1
          print('A')
          continue
        elif i<N-1:
          A -= 1
          B += 1
          print('B')
          continue
      if A<=B:
        A += 1
        B -= 1
        print('A')
      else:
        A -= 1
        B += 1
        print('B')
    elif s[i]=='BC':
      if (B,C)==(1,1):
        if i<N-1 and 'B' in s[i+1]:
          B += 1
          C -= 1
          print('B')
          continue
        elif i<N-1:
          B -= 1
          C += 1
          print('C')
          continue
      if B<=C:
        B += 1
        C -= 1
        print('B')
      else:
        B -= 1
        C += 1
        print('C')
    else:
      if (C,A)==(1,1):
        if i<N-1 and 'C' in s[i+1]:
          C += 1
          A -= 1
          print('C')
          continue
        elif i<N-1:
          C -= 1
          A += 1
          print('A')
          continue
      if C<=A:
        C += 1
        A -= 1
        print('C')
      else:
        C -= 1
        A += 1
        print('A')

Submission status: Wrong Answer

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

if sum([A,B,C])==0:
  print('No')
  exit()

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

if sum([A,B,C])==1:
  for i in range(N):
    if s[i]=='AB':
      if A<=B:
        A += 1
        B -= 1
      else:
        A -= 1
        B += 1
    elif s[i]=='BC':
      if B<=C:
        B += 1
        C -= 1
      else:
        B -= 1
        C += 1
    else:
      if C<=A:
        C += 1
        A -= 1
      else:
        C -= 1
        A += 1
    if min(A,B,C)<0:
      print('No')
      exit()
    
print('Yes')
for i in range(N):
    if s[i]=='AB':
      if A<=B:
        A += 1
        B -= 1
        print('A')
      else:
        A -= 1
        B += 1
        print('B')
    elif s[i]=='BC':
      if B<=C:
        B += 1
        C -= 1
        print('B')
      else:
        B -= 1
        C += 1
        print('C')
    else:
      if C<=A:
        C += 1
        A -= 1
        print('C')
      else:
        C -= 1
        A += 1
        print('A')

Submission status: Wrong Answer

Solution code:
import sys
import math
import bisect


# \n


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


def main():
    N, A, B, C = map(int, input().split())
    Q = [input() for _ in range(N)]
    rep = []

    for i in range(N):
        q = Q[i]
        if q == "AB":
            if A == 1 and B == 1 and i!=N-1:
                qn = Q[i+1]
                if qn =="BC":
                    A -= 1
                    B += 1
                    rep.append("B")
                else:
                    A += 1
                    B -= 1
                    rep.append("A")
            elif A >= B:
                A -= 1
                B += 1
                rep.append("B")
            else:
                A += 1
                B -= 1
                rep.append("A")



        elif q == "BC":
            if C== 1 and B == 1 and i != N - 1:
                qn = Q[i + 1]
                if qn == "AB":
                    C -= 1
                    B += 1
                    rep.append("B")
                else:
                    C += 1
                    B -= 1
                    rep.append("C")
            elif C >= B:
                C -= 1
                B += 1
                rep.append("B")
            else:
                C += 1
                B -= 1
                rep.append("C")
        elif q =="AC":
            if A== 1 and C == 1 and i != N - 1:
                qn = Q[i + 1]
                if qn == "BC":
                    A -= 1
                    C += 1
                    rep.append("C")
                else:
                    A += 1
                    B -= 1
                    rep.append("A")

            if A >= C:
                A -= 1
                C += 1
                rep.append("C")
            else:
                A += 1
                C -= 1
                rep.append("A")


        if A < 0 or B < 0 or C < 0:
            print("No")
            exit()
    print("Yes")
    print(*rep, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
import math
import bisect
#\n


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


def main():
    N, A, B, C = map(int, input().split())
    rep = []
    for i in range(N):
        q = input()
        if q == "AB":
            if A >= B:
                A -= 1
                B += 1
                rep.append("B")
            else:
                A+=1
                B-=1
                rep.append("A")
        elif q == "BC":
            if C >= B:
                C -= 1
                B += 1
                rep.append("B")
            else:
                C+=1
                B-=1
                rep.append("C")
        else:
            if A >= C:
                A -= 1
                C += 1
                rep.append("C")
            else:
                A+=1
                C-=1
                rep.append("A")

            pass
        if A<0 or B<0 or C<0:
            print("No")
            exit()

    print(*rep, sep="\n")


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

if a+b+c == 0:
    print("No")
    exit()

move = []
if a+b+c == 1:
    for i in range(n):
        if s_list[i] == "AB":
            if a == 0:
                a+=1
                b-=1
                move.append("A")
            else:
                b+=1
                a-=1
                move.append("B")
        if s_list[i] == "BC":
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            else:
                c+=1
                b-=1
                move.append("C")
        if s_list[i] == "AC":
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            else:
                c+=1
                a-=1
                move.append("C")
        if min(a,b,c) < 0:
            print("No")
            exit()

    print("Yes")
    for i in move:
        print(i)            
    
else:
    for i in range(n):
        if i < n-1:
            s_next = s_list[i+1]
        if s_list[i] == "AB":
            if a == 1 and b == 1 and s_next != "AB":
                if s_next == "BC":
                    b+=1
                    a-=1
                    move.append("B")
                else:
                    a+=1
                    b-=1
                    move.append("A")
            elif a == 0:
                a+=1
                b-=1
                move.append("A")
            elif b == 0:
                b+=1
                a-=1
                move.append("B")
            else:
                if a < b:
                    a+=1
                    b-=1
                    move.append("A")
                else:
                    b+=1
                    a-=1
                    move.append("B")
        if s_list[i] == "BC":
            if b == 1 and c == 1 and s_next != "BC":
                if s_next == "AB":
                    b+=1
                    c-=1
                    move.append("B")
                else:
                    c+=1
                    b-=1
                    move.append("C")
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            elif c == 0:
                c+=1
                b-=1
                move.append("C")
            else:
                if b < c:
                    b+=1
                    c-=1
                    move.append("B")
                else:
                    c+=1
                    b-=1
                    move.append("C")
        if s_list[i] == "AC":
            if a == 1 and c == 1 and s_next != "AC":
                if s_next == "AB":
                    a+=1
                    c-=1
                    move.append("A")
                else:
                    c+=1
                    a-=1
                    move.append("C")
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            elif c == 0:
                c+=1
                a-=1
                move.append("C")
            else:
                if a < c:
                    a+=1
                    c-=1
                    move.append("A")
                else:
                    c+=1
                    a-=1
                    move.append("C") 

        if min(a,b,c) < 0:
            print("No")
            exit()
    print("Yes")
    for i in move:
        print(i)

Submission status: Wrong Answer

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

if a+b+c == 0:
    print("No")
    exit()

move = []
if a+b+c == 1:
    for i in range(n):
        if s_list[i] == "AB":
            if a == 0:
                a+=1
                b-=1
                move.append("A")
            else:
                b+=1
                a-=1
                move.append("B")
        if s_list[i] == "BC":
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            else:
                c+=1
                b-=1
                move.append("C")
        if s_list[i] == "AC":
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            else:
                c+=1
                a-=1
                move.append("C")
        if min(a,b,c) < 0:
            print("No")
            exit()

    print("Yes")
    for i in move:
        print(i)            
    
else:
    for i in range(n):
        if i < n-1:
            s_next = s_list[i+1]
        if s_list[i] == "AB":
            if a == 1 and b == 1 and s_next != "AB":
                if s_next == "BC":
                    b+=1
                    a-=1
                    move.append("B")
                else:
                    a+=1
                    b-=1
                    move.append("A")
            elif a == 0:
                a+=1
                b-=1
                move.append("A")
            elif b == 0:
                b+=1
                a-=1
                move.append("B")
            else:
                a+=1
                b-=1
                move.append("A")
        if s_list[i] == "BC":
            if b == 1 and c == 1 and s_next != "BC":
                if s_next == "AB":
                    b+=1
                    c-=1
                    move.append("B")
                else:
                    c+=1
                    b-=1
                    move.append("C")
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            elif c == 0:
                c+=1
                b-=1
                move.append("C")
            else:
                b+=1
                c-=1
                move.append("B")
        if s_list[i] == "AC":
            if a == 1 and c == 1 and s_next != "AC":
                if s_next == "AB":
                    a+=1
                    c-=1
                    move.append("A")
                else:
                    c+=1
                    a-=1
                    move.append("C")
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            elif c == 0:
                c+=1
                a-=1
                move.append("C")
            else:
                a+=1
                c-=1
                move.append("A")     

        if min(a,b,c) < 0:
            print("No")
            exit()
    print("Yes")
    for i in move:
        print(i)