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 Ii():return int(sys.stdin.buffer.readline())
def Mi():return map(int,sys.stdin.buffer.readline().split())
def Li():return list(map(int,sys.stdin.buffer.readline().split()))
n,a,b,c = Mi()
 
st = ''
for i in range(n):
  s = input()
  if 'AB' == s or 'BA' == s:
    if a < b:
      a += 1
      b -= 1
      st += 'A\n'
    else:
      a -= 1
      b += 1
      st += 'B\n'
  elif 'AC' == s or 'CA' == s:
    if a < c:
      a += 1
      c -= 1
      st += 'A\n'
    else:
      a -= 1
      c += 1
      st += 'C\n'
  else:
    if b < c:
      b += 1
      c -= 1
      st += 'B\n'
    else:
      b -= 1
      c += 1
      st += 'C\n'
  if a < 0 or b < 0 or c < 0:
    print('No')
    exit(0)
    
print('Yes')
print(st)

Submission status: Wrong Answer

Solution code:
import sys
def Ii():return int(sys.stdin.buffer.readline())
def Mi():return map(int,sys.stdin.buffer.readline().split())
def Li():return list(map(int,sys.stdin.buffer.readline().split()))
n,a,b,c = Mi()

st = ''
for i in range(n):
  s = input()
  if 'AB' == s or 'BA' == s:
    if a < b:
      a += 1
      b -= 1
      st += 'A\n'
    else:
      a -= 1
      b += 1
      st += 'B\n'
  elif 'AC' == s or 'CA' == s:
    if a < c:
      a += 1
      c -= 1
      st += 'A\n'
    else:
      a -= 1
      c += 1
      st += 'C\n'
  else:
    if b < c:
      b += 1
      c -= 1
      st += 'B\n'
    else:
      b -= 1
      c += 1
      st += 'C\n'
  print(a,b,c)
  if a < 0 or b < 0 or c < 0:
    print('No')
    exit(0)
    
print('Yes')
print(st)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,raw_input().split(' ')) #8 6 9 1
moves = [raw_input() for _ in range(n)]

q = {'A': a, 'B':b, 'C':c}
cur = {'AC': +float('inf'), 'AB':float('inf'), 'BC': float('inf')}
dp = {}
for j in range(len(moves) -1 , -1, -1):
	cur[moves[j]] = j
	dp[j] = {k :cur[k] for k in cur}

flag = True
ans = []
for i,mov in enumerate(moves):
	u,v = mov
	if q[u] == 0 and q[v] == 0:
		flag = False
		break
	if q[u] == 0 or q[v] == 0:
		if q[u] == 0:
			ans.append(u)
			q[u] += 1
			q[v] -= 1

		elif q[v] == 0:
			ans.append(v)
			q[v] += 1
			q[u] -= 1
	else:
		w  = ''.join([l for l in 'ABC' if l != u and l != v])
		if q[w] > 0:
			if q[u] >= q[v]:
				q[u] -=1
				q[v] +=1
				ans.append(u)
			else:
				q[v] -=1
				q[u] +=1
				ans.append(v)
		elif q[w] == 0:
			uu = ''.join(sorted([w,u]))
			vv = ''.join(sorted([w,v]))
			if dp[i][uu] < dp[i][vv]:
				ans.append(u)
				q[v]-=1
				q[u]+=1
			else:
				ans.append(v)
				q[u]-=1
				q[v]+=1

if flag:
	print 'Yes'
	for aa in ans:
		print aa
else:
	print 'No'

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,raw_input().split(' ')) #8 6 9 1
moves = [raw_input() for _ in range(n)]

q = {'A': a, 'B':b, 'C':c}
cur = {'AC': +float('inf'), 'AB':float('inf'), 'BC': float('inf')}
dp = {}
for j in range(len(moves) -1 , -1, -1):
	cur[moves[j]] = j
	dp[j] = {k :cur[k] for k in cur}

flag = True
ans = []
for i,mov in enumerate(moves):
	u,v = mov
	if q[u] == 0 and q[v] == 0:
		flag = False
		break
	if q[u] == 0 or q[v] == 0:
		if q[u] == 0:
			ans.append(u)
			q[u] += 1
			q[v] -= 1

		elif q[v] == 0:
			q[v] += 1
			q[u] -= 1
			ans.append(v)
	else:
		w  = ''.join([l for l in 'ABC' if l != u and l != v])
		if q[w] > 0:
			if q[u] >= q[v]:
				q[u] -=1
				q[v] +=1
				ans.append(u)
			else:
				q[v] -=1
				q[u] +=1
				ans.append(v)
		elif q[w] == 0:
			uu = ''.join(sorted([w,u]))
			vv = ''.join(sorted([w,v]))
			if dp[i][uu] < dp[i][vv]:
				ans.append(v)
				q[v]-=1
				q[u]+=1
			else:
				ans.append(u)
				q[u]-=1
				q[v]+=1


if flag:
	print 'Yes'
	for aa in ans:
		print aa
else:
	print 'No'

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,raw_input().split(' ')) #8 6 9 1
moves = [raw_input() for _ in range(n)]

q = {'A': a, 'B':b, 'C':c}
cur = {'AC': +float('inf'), 'AB':float('inf'), 'BC': float('inf')}
dp = {}
for j in range(len(moves) -1 , -1, -1):
	cur[moves[j]] = j
	dp[j] = {k :cur[k] for k in cur}

flag = True
ans = []
for i,mov in enumerate(moves):

	u,v = mov[0], mov[1]
	if q[u] == 0 and q[v] == 0:
		flag = False
		break
	if q[u] == 0 or q[v] == 0:
		if u == 0:
			ans.append(u)
			q[u] += 1
			q[v] -= 1

		elif v == 0:
			q[v] += 1
			q[u] -= 1
			ans.append(v)
	else:
		w  = ''.join([l for l in 'ABC' if l != u and l != v])
		if q[w] > 0:
			if q[u] >= q[v]:
				q[u] -=1
				q[v] +=1
				ans.append(u)
			else:
				q[v] -=1
				q[u] +=1
				ans.append(v)
		elif q[w] == 0:
			uu = ''.join(sorted([w,u]))
			vv = ''.join(sorted([w,v]))
			if dp[i][uu] < dp[i][vv]:
				ans.append(v)
				q[v]-=1
				q[u]+=1
			else:
				ans.append(u)
				q[u]-=1
				q[v]+=1


if flag:
	print 'Yes'
	for aa in ans:
		print aa
else:
	print 'No'

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,raw_input().split(' ')) #8 6 9 1
moves = [raw_input() for _ in range(n)]

q = {'A': a, 'B':b, 'C':c}
cur = {'AC': +float('inf'), 'AB':float('inf'), 'BC': float('inf')}
dp = {}
for j in range(len(moves) -1 , -1, -1):
	cur[moves[j]] = j
	dp[j] = {k :cur[k] for k in cur}

flag = True
ans = []
for i,mov in enumerate(moves):

	u,v = mov[0], mov[1]
	if q[u] == 0 and q[v] == 0:
		flag = False
		break
	if q[u] == 0 or q[v] == 0:
		if u == 0:
			ans.append(u)
			q[u] += 1
			q[v] -= 1

		elif v == 0:
			q[v] -= 1
			q[u] += 1
	else:
		w  = ''.join([l for l in 'ABC' if l != u and l != v])
		if q[w] > 0:
			if q[u] >= q[v]:
				q[u] -=1
				q[v] +=1
				ans.append(u)
			else:
				q[v] -=1
				q[u] +=1
				ans.append(v)
		elif q[w] == 0:
			uu = ''.join(sorted([w,u]))
			vv = ''.join(sorted([w,v]))
			if dp[i][uu] < dp[i][vv]:
				ans.append(v)
				q[v]-=1
				q[u]+=1
			else:
				ans.append(u)
				q[u]-=1
				q[v]+=1


if flag:
	print 'Yes'
	for aa in ans:
		print aa
else:
	print 'No'

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,raw_input().split(' ')) #8 6 9 1
moves = [raw_input() for _ in range(n)]

q = {'A': a, 'B':b, 'C':c}
cur = {'AC': +float('inf'), 'AB':float('inf'), 'BC': float('inf')}
dp = {}
for j in range(len(moves) -1 , -1, -1):
	cur[moves[j]] = j
	dp[j] = {k :cur[k] for k in cur}

flag = True
ans = []
for i,mov in enumerate(moves):

	u,v = mov[0], mov[1]
	if q[u] == 0 and q[v] == 0:
		flag = False
		break
	if q[u] == 0 or q[v] == 0:
		if u == 0:
			ans.append(u)
			q[u] += 1
			q[v] -= 1

		elif v == 0:
			q[v] -= 1
			q[u] += 1
	else:
		w  = ''.join([l for l in 'ABC' if l != u and l != v])
		if q[w] > 0:
			q[u] -=1
			q[v] +=1
			ans.append(u)
		elif q[w] == 0:
			uu = ''.join(sorted([w,u]))
			vv = ''.join(sorted([w,v]))
			if dp[i][uu] < dp[i][vv]:
				ans.append(v)
				q[v]-=1
				q[u]+=1
			else:
				ans.append(u)
				q[u]-=1
				q[v]+=1


if flag:
	print 'Yes'
	for aa in ans:
		print aa
else:
	print 'No'

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,raw_input().split(' ')) #8 6 9 1
moves = [raw_input() for _ in range(n)]

q = {'A': a, 'B':b, 'C':c}
cur = {'AC': +float('inf'), 'AB':float('inf'), 'BC': float('inf')}
dp = {}
for j in range(len(moves) -1 , -1, -1):
	cur[moves[j]] = j
	dp[j] = {k :cur[k] for k in cur}

flag = True
ans = []
for i,mov in enumerate(moves):
	print mov
	u,v = mov[0], mov[1]
	if u == 0 and v == 0:
		flag = False
		break
	if u == 0 or v == 0:
		if u == 0:
			ans.append(u)
			q[u] += 1
			q[v] -= 1

		elif v == 0:
			q[v] -= 1
			q[u] += 1
	else:
		w  = ''.join([l for l in 'ABC' if l != u and l != v])
		if q[w] > 0:
			q[u] -=1
			q[v] +=1
			ans.append(u)
		elif q[w] == 0:
			uu = ''.join(sorted([w,u]))
			vv = ''.join(sorted([w,v]))
			if dp[i][uu] < dp[i][vv]:
				ans.append(v)
				q[v]-=1
				q[u]+=1
			else:
				ans.append(u)
				q[u]-=1
				q[v]+=1


if flag:
	print 'Yes'
	for aa in ans:
		print aa
else:
	print 'No'

Submission status: Wrong Answer

Solution code:
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

def rl():
    return list(map(int, input().split()))


def solve():
    n, a, b, c = rl()
    ways = {(a,b,c): []}

    for i in range(n):
        # print (ways)
        s = input()
        new_ways = {}
        if s == "AB":
            for way, path in ways.items():
                lway = list(way)
                if lway[0] > 0:
                    newway = lway
                    newway[0] -= 1
                    newway[1] += 1
                    newpath = path + ["B"]
                    new_ways[tuple(newway)] = newpath
                if lway[1] > 0:
                    newway = lway
                    newway[1] -= 1
                    newway[0] += 1
                    newpath = path + ["A"]
                    new_ways[tuple(newway)] = newpath
        elif s == "AC":
            for way, path in ways.items():
                lway = list(way)
                if lway[0] > 0:
                    newway = lway
                    newway[0] -= 1
                    newway[2] += 1
                    newpath = path + ["C"]
                    new_ways[tuple(newway)] = newpath
                if lway[2] > 0:
                    newway = lway
                    newway[2] -= 1
                    newway[0] += 1
                    newpath = path + ["A"]
                    new_ways[tuple(newway)] = newpath
        else:
            for way, path in ways.items():
                lway = list(way)
                if lway[1] > 0:
                    newway = lway
                    newway[1] -= 1
                    newway[2] += 1
                    newpath = path + ["C"]
                    new_ways[tuple(newway)] = newpath
                if lway[2] > 0:
                    newway = lway
                    newway[2] -= 1
                    newway[1] += 1
                    newpath = path + ["B"]
                    new_ways[tuple(newway)] = newpath

        ways = new_ways

    if not ways:
        print ("No")
        return
    else:
        print ("Yes")
        for k, path in ways.items():
            for e in path:
                print (e)
            return




mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split())
li=[A,B,C]
ans=""
for i in range(N):
  S=input()
  target=[0,1,2]
  ta1 = target[ord(S[0])-ord("A")]
  ta2 = target[ord(S[1])-ord("A")]
  if li[ta1] == 0 and li[ta2] == 0:
    print("No")
    exit()
  if li[ta1] >= li[ta2]:
    ans+=chr(ord("A")+ta2)
    li[ta1]-=1
    li[ta2]+=1
  else:
    ans+=chr(ord("A")+ta1)
    li[ta1]+=1
    li[ta2]-=1

print("Yes")
for s in ans:
  print(s)

Submission status: Wrong Answer

Solution code:
def main():
    n, a, b, c = map(int, input().split())
    s = [input() for _ in range(n)]
    if a+b+c == 0:
        print("No")
        return
    num = dict(A=a, B=b, C=c)
    ans = []
    if a+b+c == 1:
        for si in s:
            if num[si[0]] == 1:
                ans.append(si[1])
                num[si[0]], num[si[1]] = 0, 1
            elif num[si[1]] == 1:
                ans.append(si[0])
                num[si[0]], num[si[1]] = 1, 0
            else:
                print("No")
                return
        print("Yes")
        print("\n".join(ans))
    else:
        for i in range(n):
            si = s[i]
            if num[si[0]] >= 1 and num[si[1]] >= 1:
                if i < n-1:
                    sii = s[i+1]
                    if sii[0] == si[0]:
                        ans.append(si[0])
                        num[si[0]] += 1
                        num[si[1]] -= 1
                    else:
                        ans.append(si[1])
                        num[si[0]] -= 1
                        num[si[1]] += 1
                else:
                    ans.append(si[0])
            elif num[si[0]] >= 1 and num[si[1]] == 0:
                ans.append(si[1])
                num[si[1]] += 1
                num[si[0]] -= 1
            elif num[si[1]] >= 1 and num[si[0]] == 0:
                ans.append(si[0])
                num[si[0]] += 1
                num[si[1]] -= 1   
            else:
                print("No")
                return
        print("Yes")
        print("\n".join(ans))
            
                
        
    
        

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B, C = list(map(int, input().split()))
    S = [{'AB':(0, 1), 'BC': (1, 2), 'AC': (0, 2)}[input()] for _ in range(N)]
    ABC = [A, B, C]
    RR = ['A', 'B', 'C']
    R = []
    for i, (s1, s2) in enumerate(S):
        if ABC[s1] == 0 and ABC[s2] == 0:
            return False, None
        if ABC[s1] < ABC[s2]:
            s1, s2 = s2, s1
        ABC[s1] -= 1
        ABC[s2] += 1
        R.append(RR[s2])
    return True, R

a, s = main()
if not a:
    print('No')
else:
    print('Yes')
    print('\n'.join(s))

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
if(1):
    r=[]
    ff=0
    r1=[]
    for i in range(n):
        s=input().rstrip("\n")
        r1.append(s)
    for i in range(n):
        s=r1[i]
        if(s=='AB'):
            if(a==b==0):
                ff=1
                break
            if(a==0 and b!=0):
                a+=1
                b-=1
                r.append('A')
            elif(b==0 and a!=0):
                b+=1
                a-=1
                r.append('B')
            elif(a==b==1):
                if(i!=n-1):
                    if('A' in s and 'A' in r1[i+1]):
                        a+=1
                        b-=1
                        r.append('A')
                    else:
                        b+=1
                        a-=1
                        r.append('B')
                else:
                    r.append('B')
            else:
                a-=1
                b+=1
                r.append('B')
        elif(s=='BC'):
            if(b==c==0):
                ff=1
                break
            if(b==0 and c!=0):
                b+=1
                c-=1
                r.append('B')
            elif(b!=0 and c==0):
                b-=1
                c+=1
                r.append('C')
            elif(c==b==1):
                if(i!=n-1):
                    if('B' in s and 'B' in r1[i+1]):
                        b+=1
                        c-=1
                        r.append('B')
                    else:
                        c+=1
                        b-=1
                        r.append('C')
                else:
                    r.append('C')
            else:
                b-=1
                c+=1
                r.append('C')
        else:
            if(a==c==0):
                ff=1
                break
            if(a==0 and c!=0):
                a+=1
                c-=1
                r.append('A')
            elif(c==0 and a!=0):
                c+=1
                a-=1
                r.append('C')
            elif(a==c==1):
                if(i!=n-1):
                    if('A' in s and 'A' in r1[i+1]):
                        a+=1
                        c-=1
                        r.append('A')
                    else:
                        c+=1
                        a-=1
                        r.append('C')
                else:
                    r.append('C')
            else:
                a-=1
                c+=1
                r.append('C')
    if(ff):
        print('No')
    else:
        print('Yes')
        for i in r:
            print(i)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
if(1):
    r=[]
    ff=0
    for i in range(n):
        s=input().rstrip("\n")
        if(s=='AB'):
            if(a>b):
                a-=1
                b+=1
                r.append('B')
            else:
                a+=1
                b-=1
                if(b==-1):
                    ff=1
                r.append('A')
        elif(s=='BC'):
            if(b>c):
                b-=1
                c+=1
                r.append('C')
            else:
                c-=1
                b+=1
                if(c==-1):
                    ff=1
                r.append('B')
        else:
            if(a>c):
                a-=1
                c+=1
                r.append('C')
            else:
                a+=1
                c-=1
                if(c==-1):
                    ff=1
                r.append('A')
    if(ff):
        print('No')
    else:
        print('Yes')
        for i in r:
            print(i)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
if(1):
    r=[]
    ff=0
    for i in range(n):
        s=input().rstrip("\n")
        if(s=='AB'):
            if(a>b):
                a-=1
                b+=1
                r.append('B')
            else:
                a+=1
                b-=1
                if(b==0):
                    ff=1
                r.append('A')
        elif(s=='BC'):
            if(b>c):
                b-=1
                c+=1
                r.append('C')
            else:
                c-=1
                b+=1
                if(c==0):
                    ff=1
                r.append('B')
        else:
            if(a>c):
                a-=1
                c+=1
                r.append('C')
            else:
                a+=1
                c-=1
                if(c==0):
                    ff=1
                r.append('A')
    if(ff):
        print('No')
    else:
        print('Yes')
        for i in r:
            print(i)

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
if([a,b,c].count(0)<=1):
    r=[]
    ff=0
    for i in range(n):
        s=input().rstrip("\n")
        if(s=='AB'):
            if(a>b):
                a-=1
                b+=1
                r.append('B')
            else:
                a+=1
                b-=1
                if(b==0):
                    ff=1
                r.append('A')
        elif(s=='BC'):
            if(b>c):
                b-=1
                c+=1
                r.append('C')
            else:
                c-=1
                b+=1
                if(c==0):
                    ff=1
                r.append('B')
        else:
            if(a>c):
                a-=1
                c+=1
                r.append('C')
            else:
                a+=1
                c-=1
                if(c==0):
                    ff=1
                r.append('A')
    if(ff):
        print('No')
    else:
        print('Yes')
        for i in r:
            print(i)
else:
    for i in range(n):
        s=input().rstrip('\n')
    print('No')

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
if([a,b,c].count(0)<=1):
    print('Yes')
    for i in range(n):
        s=input().rstrip("\n")
        if(s=='AB'):
            if(a>b):
                a-=1
                b+=1
                print('B')
            else:
                a+=1
                b-=1
                print('A')
        elif(s=='BC'):
            if(b>c):
                b-=1
                c+=1
                print('C')
            else:
                c-=1
                b+=1
                print('B')
        else:
            if(a>c):
                a-=1
                c+=1
                print('C')
            else:
                a+=1
                c-=1
                print('A')
else:
    for i in range(n):
        s=input().rstrip('\n')
    print('No')

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, *ABC = map(int, rl().split())
    s = [rl().rstrip() for _ in range(N)]
    
    if sum(ABC) == 0:
        print('No')
    elif sum(ABC) == 1:
        ans = ['Yes']
        for si in s:
            idx0 = ord(si[0]) - ord('A')
            idx1 = ord(si[1]) - ord('A')
            if ABC[idx0] == 0 and ABC[idx1] == 0:
                print('No')
                return
            if ABC[idx0] == 0:
                ABC[idx0] += 1
                ABC[idx1] -= 1
                ans.append(si[0])
            else:
                ABC[idx0] -= 1
                ABC[idx1] += 1
                ans.append(si[1])
        print(*ans, sep='\n')
    else:
        if ABC[ord(s[0][0]) - ord('A')] == 0 and ABC[ord(s[0][1]) - ord('A')] == 0:
            print('No')
        else:
            ans = ['Yes']
            for i, si in enumerate(s):
                if any(num < 0 for num in ABC):
                    raise Exception
                idx0 = ord(si[0]) - ord('A')
                idx1 = ord(si[1]) - ord('A')
                if ABC[idx0] == 0:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
                elif ABC[idx1] == 0:
                    ABC[idx0] -= 1
                    ABC[idx1] += 1
                    ans.append(si[1])
                elif sum(ABC) == 2 and ABC[idx0] == 1 and ABC[idx1] == 1 and i != N - 1 and si != s[i + 1]:
                    if si[0] in s[i + 1]:
                        ABC[idx0] += 1
                        ABC[idx1] -= 1
                        ans.append(si[0])
                    else:
                        ABC[idx0] -= 1
                        ABC[idx1] += 1
                        ans.append(si[1])
                else:
                    if ABC[idx0] <= ABC[idx1]:
                        ABC[idx0] += 1
                        ABC[idx1] -= 1
                        ans.append(si[0])
                    else:
                        ABC[idx1] -= 1
                        ABC[idx1] += 1
                        ans.append(si[1])
            print(*ans, sep='\n')


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, *ABC = map(int, rl().split())
    s = [rl().rstrip() for _ in range(N)]
    
    if sum(ABC) == 0:
        print('No')
    elif sum(ABC) == 1:
        ans = ['Yes']
        for si in s:
            idx0 = ord(si[0]) - ord('A')
            idx1 = ord(si[1]) - ord('A')
            if ABC[idx0] == 0 and ABC[idx1] == 0:
                print('No')
                return
            if ABC[idx0] == 0:
                ABC[idx0] += 1
                ABC[idx1] -= 1
                ans.append(si[0])
            else:
                ABC[idx0] -= 1
                ABC[idx1] += 1
                ans.append(si[1])
        print(*ans, sep='\n')
    else:
        if ABC[ord(s[0][0]) - ord('A')] == 0 and ABC[ord(s[0][1]) - ord('A')] == 0:
            print('No')
        else:
            ans = ['Yes']
            for i, si in enumerate(s):
                idx0 = ord(si[0]) - ord('A')
                idx1 = ord(si[1]) - ord('A')
                if ABC[idx0] == 0:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
                elif ABC[idx1] == 0:
                    ABC[idx0] -= 1
                    ABC[idx1] += 1
                    ans.append(si[1])
                elif sum(ABC) == 2 and ABC[idx0] == 1 and ABC[idx1] == 1 and i != N - 1 and si != s[i + 1]:
                    if si[0] in s[i + 1]:
                        ABC[idx0] += 1
                        ABC[idx1] -= 1
                        ans.append(si[0])
                    else:
                        ABC[idx0] -= 1
                        ABC[idx1] += 1
                        ans.append(si[1])
                else:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
            print(*ans, sep='\n')


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, *ABC = map(int, rl().split())
    s = [rl().rstrip() for _ in range(N)]
    
    if sum(ABC) == 0:
        print('No')
    elif sum(ABC) == 1:
        ans = ['Yes']
        for si in s:
            idx0 = ord(si[0]) - ord('A')
            idx1 = ord(si[1]) - ord('A')
            if ABC[idx0] == 0 and ABC[idx1] == 0:
                print('No')
                return
            if ABC[idx0] == 0:
                ABC[idx0] += 1
                ABC[idx1] -= 1
                ans.append(si[0])
            else:
                ABC[idx0] -= 1
                ABC[idx1] += 1
                ans.append(si[1])
    else:
        if ABC[ord(s[0][0]) - ord('A')] == 0 and ABC[ord(s[0][1]) - ord('A')] == 0:
            print('No')
        else:
            ans = ['Yes']
            for i, si in enumerate(s):
                idx0 = ord(si[0]) - ord('A')
                idx1 = ord(si[1]) - ord('A')
                if ABC[idx0] == 0:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
                elif ABC[idx1] == 0:
                    ABC[idx0] -= 1
                    ABC[idx1] += 1
                    ans.append(si[1])
                elif sum(ABC) == 2 and ABC[idx0] == 1 and ABC[idx1] == 1 and i != N - 1 and si != s[i + 1]:
                    if si[0] in s[i + 1]:
                        ABC[idx0] += 1
                        ABC[idx1] -= 1
                        ans.append(si[0])
                    else:
                        ABC[idx0] -= 1
                        ABC[idx1] += 1
                        ans.append(si[1])
                else:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
            print(*ans, sep='\n')


if __name__ == '__main__':
    solve()