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 = list(map(int, input().split()))
slist = []
d = {'A': a, 'B':b, 'C':c}
flag = 1
soln = []
corner_case = False
prev = None

# Greedy approach is wrong
for i in range(n):
    s = input()

    if corner_case:
        # increment and decrememnt based on the future.
        if prev[0] in s:
            d[prev[0]] += 1
            d[prev[1]] -= 1
            soln.append(prev[0])
        else:
            d[prev[0]] -= 1
            d[prev[1]] += 1
            soln.append(prev[1])

        corner_case = False

    # greedy
    if d[s[0]] > d[s[1]]:
        d[s[0]] -= 1
        d[s[1]] += 1
        soln.append(s[1])

    elif d[s[0]] < d[s[1]]:
        d[s[1]] -= 1
        d[s[0]] += 1
        soln.append(s[0])

    else: #d[s[0]] == d[s[1]]
        corner_case = True
        prev = s

    
    if d[s[1]]<0 or d[s[0]]<0:
        print('No')
        flag = 0
        break

if flag:
    print('Yes')
    print('\n'.join(soln))

Submission status: Wrong Answer

Solution code:
X = [0, 0, 0]
N, X[0], X[1], X[2] = map(int, input().split())
Q = []
for _ in range(N):
    s = input()
    if s == 'AB':
        Q.append([0, 1])
    elif s == 'AC':
        Q.append([0, 2])
    else:
        Q.append([1, 2])
ans = True
ret = ''
for i, j in Q:
    if X[i] == X[j] == 0:
        ans = False
        break
    elif X[i] < X[j]:
        ret += chr(ord(str(i)) + 17)
        X[i] += 1
        X[j] -= 1
    else:
        ret += chr(ord(str(j)) + 17)
        X[i] -= 1
        X[j] += 1
if ans:
    print('Yes')
    print('\n'.join(ret))
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		if abc[i1]+abc[i2]==0:
			ans="No"
		elif abc[i1]+abc[i2]==1:
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			else:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif s==2 and abc[i1]*abc[i2] and i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
if ans=="Yes":
	for i in range(n):
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())

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

history = [] 
flag = True
  
for _ in range(n):
  s = input()
  if s == 'AB':
    if a == 0 and b == 0:
      print('No')
      flag = False
      break
    elif a == 0:
      history.append('A')
      a += 1
      b -= 1
    elif b == 0:
      history.append('B')
      a -= 1
      b += 1
    else:
      if a >= b:
        history.append('B')
        a -= 1
        b += 1
      else:
        history.append('A')
        a += 1
        b -= 1
  elif s == "AC":
    if a == 0 and c == 0:
      print("No")
      flag = False
      break
    elif c == 0:
      history.append('C')
      a -= 1
      c += 1
    else:
      if a >= c:
        history.append('C')
        a -= 1
        c += 1
      else:
        history.append('A')
        a += 1
        c -= 1
  elif s == "BC":
    if b == 0 and c == 0:
      print("No")
      flag = False
      break
    elif b == 0:
      history.append('B')
      b += 1
      c -= 1
    elif c == 0:
      history.append('C')
      b -= 1
      c += 1
    else:
      if b >= c:
        history.append('C')
        b -= 1
        c += 1
      else:
        history.append('B')
        b += 1
        c -= 1
if flag:
  print("Yes")
  print('\n'.join(history))

Submission status: Wrong Answer

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

N,A,B,C=input().split()
D=dict()
D["A"]=int(A)
D["B"]=int(B)
D["C"]=int(C)

S=[]
for i in range(int(N)):
    S.append(input())

ans="Yes"
F=[]
for i,s in enumerate(S):
    if D[s[0]]==0 and D[s[1]]==0:
        ans="No"
        break
    if D[s[0]]==0 and D[s[1]]==1:
        F.append(s[0])
    if D[s[0]]==1 and D[s[1]]==0:
        F.append(s[1])
    if D[s[0]]==1 and D[s[1]]==1:
        if D["A"]+D["B"]+D["C"]==2:
            if i!=int(N)-1:
                s0=S[i+1][0]
                s1=S[i+1][1]
                if s[0]==s0 or s[0]==s1:
                    F.append(s[0])
                if s[1]==s0 or s[1]==s1:
                    F.append(s[1])
    if len(F)!=i+1:
        if D[s[0]]<D[s[1]]:
            F.append(s[0])
        if D[s[0]]>D[s[1]]:
            F.append(s[1])
        if D[s[0]]==D[s[1]]:
            F.append(s[0])

    D[F[-1]]+=1
    if s[0]==F[-1]: D[s[1]]-=1
    if s[1]==F[-1]: D[s[0]]-=1
    #print(D)
    #print(F)

print(ans)
if ans=="Yes":
    for f in F:
        print(f)

Submission status: Wrong Answer

Solution code:
n, a, b, c = list(map(int, input().split()))
slist = []
d = {'A': a, 'B':b, 'C':c}
flag = 1
soln = []
corner_case = False
prev = None

# Greedy approach is wrong
for i in range(n):
    s = input()

    if corner_case:
        common = None
        # increment and decrememnt based on the future.
        if prev[0] in s:
            d[prev[0]] += 1
            d[prev[1]] -= 1
        else:
            d[prev[0]] -= 1
            d[prev[1]] += 1
        corner_case = False

    # greedy
    if d[s[0]] > d[s[1]]:
        d[s[0]] -= 1
        d[s[1]] += 1
        soln.append(s[1])

    elif d[s[0]] < d[s[1]]:
        d[s[1]] -= 1
        d[s[0]] += 1
        soln.append(s[0])

    else: #d[s[0]] == d[s[1]]
        corner_case = True
        prev = s

    
    if d[s[1]]<0 or d[s[0]]<0:
        print('No')
        flag = 0
        break

if flag:
    print('Yes')
    print('\n'.join(soln))

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())

if a + b + c == 0:
  print('NO')

history = [] 
flag = True
  
for _ in range(n):
  s = input()
  if s == 'AB':
    if a == 0 and b == 0:
      print('NO')
      flag = False
      break
    elif a == 0:
      history.append('A')
      a += 1
      b -= 1
    elif b == 0:
      history.append('B')
      a -= 1
      b += 1
    else:
      if a >= b:
        history.append('B')
        a -= 1
        b += 1
      else:
        history.append('A')
        a += 1
        b -= 1
  elif s == "AC":
    if a == 0 and c == 0:
      print("NO")
      flag = False
      break
    elif c == 0:
      history.append('C')
      a -= 1
      c += 1
    else:
      if a >= c:
        history.append('C')
        a -= 1
        c += 1
      else:
        history.append('A')
        a += 1
        c -= 1
  elif s == "BC":
    if b == 0 and c == 0:
      print("NO")
      flag = False
      break
    elif b == 0:
      history.append('B')
      b += 1
      c -= 1
    elif c == 0:
      history.append('C')
      b -= 1
      c += 1
    else:
      if b >= c:
        history.append('C')
        b -= 1
        c += 1
      else:
        history.append('B')
        b += 1
        c -= 1
if flag:
  print("YES")
  print('\n'.join(history))

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())

if a + b + c == 0:
  print('no')

history = [] 
flag = True
  
for _ in range(n):
  s = input()
  if s == 'AB':
    if a == 0 and b == 0:
      print('no')
      flag = False
      break
    elif a == 0:
      history.append('A')
      a += 1
      b -= 1
    elif b == 0:
      history.append('B')
      a -= 1
      b += 1
    else:
      if a >= b:
        history.append('B')
        a -= 1
        b += 1
      else:
        history.append('A')
        a += 1
        b -= 1
  elif s == "AC":
    if a == 0 and c == 0:
      print("no")
      flag = False
      break
    elif c == 0:
      history.append('C')
      a -= 1
      c += 1
    else:
      if a >= c:
        history.append('C')
        a -= 1
        c += 1
      else:
        history.append('A')
        a += 1
        c -= 1
  elif s == "BC":
    if b == 0 and c == 0:
      print("no")
      flag = False
      break
    elif b == 0:
      history.append('B')
      b += 1
      c -= 1
    elif c == 0:
      history.append('C')
      b -= 1
      c += 1
    else:
      if b >= c:
        history.append('C')
        b -= 1
        c += 1
      else:
        history.append('B')
        b += 1
        c -= 1
if flag:
  print("yes")
  print('\n'.join(history))

Submission status: Wrong Answer

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

N,A,B,C=input().split()
D=dict()
D["A"]=int(A)
D["B"]=int(B)
D["C"]=int(C)

S=[]
for i in range(int(N)):
    S.append(input())

ans="Yes"
F=[]
for i,s in enumerate(S):
    if D[s[0]]==0 and D[s[1]]==0:
        ans="No"
        break
    if D[s[0]]==0 and D[s[1]]==1:
        F.append(s[0])
    if D[s[0]]==1 and D[s[1]]==0:
        F.append(s[1])
    if D[s[0]]==1 and D[s[1]]==1:
        if D["A"]+D["B"]+D["C"]==2:
            if i!=int(N)-1:
                s0=S[i+1][0]
                s1=S[i+1][1]
                if s[0]==s0 or s[0]==s1:
                    F.append(s[0])
                if s[1]==s0 or s[1]==s1:
                    F.append(s[1])
    if len(F)!=i+1:
        if D[s[0]]<D[s[1]]:
            F.append(s[0])
        if D[s[0]]>D[s[1]]:
            F.append(s[1])
        if D[s[0]]==D[s[1]]:
            F.append(s[0])

    D[F[-1]]+=1
    if s[0]==F[-1]: D[s[1]]+=1
    if s[1]==F[-1]: D[s[0]]+=1
    #print(D)
    #print(F)

print(ans)
if ans=="Yes":
    for f in F:
        print(f)

Submission status: Wrong Answer

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, a, b, c = MAP()
C = {0: a, 1: b, 2: c}
S = []
for i in range(N):
    s = input()
    c1, c2 = s
    S.append((ord(c1)-65, ord(c2)-65))

ans = []
for i in range(N-1):
    cur1, cur2 = S[i]
    if C[cur1] == C[cur2] == 0:
        No()
        exit()
    elif C[cur1] == 0:
        ans.append(cur1)
        C[cur1] += 1
        C[cur2] -= 1
    elif C[cur2] == 0:
        ans.append(cur2)
        C[cur2] += 1
        C[cur1] -= 1
    elif C[cur1] == C[cur2] == 1 and C[3-cur1-cur2] == 0:
        if cur1 in S[i+1] and 3-cur1-cur2 in S[i+1]:
            ans.append(cur1)
            C[cur1] += 1
            C[cur2] -= 1
        else:
            ans.append(cur2)
            C[cur2] += 1
            C[cur1] -= 1
    else:
        ans.append(cur1)
        C[cur1] += 1
        C[cur2] -= 1
cur1, cur2 = S[-1]
if C[cur1] == C[cur2] == 0:
    No()
    exit()
elif C[cur1] == 0:
    ans.append(cur1)
    C[cur1] += 1
    C[cur2] -= 1
else:
    ans.append(cur2)
    C[cur2] += 1
    C[cur1] -= 1
Yes()
for i in range(N):
    print(chr(ans[i]+65))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict

N,A,B,C=map(int, sys.stdin.readline().split())
S=[ sys.stdin.readline().strip() for _ in range(N) ]

#A,B,Cの累積登場回数
RA=[0]
RB=[0]
RC=[0]
for s in S:
    if "A" in s: RA.append(RA[-1]+1) 
    else: RA.append(RA[-1]) 
    if "B" in s: RB.append(RB[-1]+1) 
    else: RB.append(RB[-1]) 
    if "C" in s: RC.append(RC[-1]+1) 
    else: RC.append(RC[-1]) 

L=[]    #i回目の選択で1を足す変数の名前を順番に格納

for i,s in enumerate(S):
    i+=1    #1-indexed     
    if s=="AB":
        if A<B:
            B-=1
            A+=1
            L.append("A")
        elif B<A:
            A-=1
            B+=1
            L.append("B")
        elif A==B==0:
            print "No"
            quit()
        elif A==B:
            a_nokori=RA[N]-RA[i]
            b_nokori=RB[N]-RB[i]
            if a_nokori<=b_nokori:
                B-=1
                A+=1
                L.append("A")
            else:
                A-=1
                B+=1
                L.append("B")
    elif s=="AC":
        if A<C:
            C-=1
            A+=1
            L.append("A")
        elif C<A:
            A-=1
            C+=1
            L.append("C")
        elif A==C==0:
            print "No"
            quit()
        elif A==C:
            a_nokori=RA[N]-RA[i]
            c_nokori=RC[N]-RC[i]
            if a_nokori<=c_nokori:
                C-=1
                A+=1
                L.append("A")
            else:
                A-=1
                C+=1
                L.append("C")
    elif s=="BC":
        if B<C:
            C-=1
            B+=1
            L.append("B")
        elif C<B:
            B-=1
            C+=1
            L.append("C")
        elif B==C==0:
            print "No"
            quit()
        elif B==C:
            b_nokori=RB[N]-RB[i]
            c_nokori=RC[N]-RC[i]
            if b_nokori<=c_nokori:
                C-=1
                B+=1
                L.append("B")
            else:
                B-=1
                C+=1
                L.append("C")

print "Yes"
for x in L:
    print x

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict

N,A,B,C=map(int, sys.stdin.readline().split())
S=[ sys.stdin.readline().strip() for _ in range(N) ]

#A,B,Cの累積登場回数
RA=[0]
RB=[0]
RC=[0]
for s in S:
    if "A" in s: RA.append(RA[-1]+1) 
    else: RA.append(RA[-1]) 
    if "B" in s: RB.append(RB[-1]+1) 
    else: RB.append(RB[-1]) 
    if "C" in s: RC.append(RC[-1]+1) 
    else: RC.append(RC[-1]) 

L=[]    #i回目の選択で1を足す変数の名前を順番に格納

for i,s in enumerate(S):
    i+=1    #1-indexed     
    if s=="AB":
        if A<B:
            B-=1
            A+=1
            L.append("A")
        elif B<A:
            A-=1
            B+=1
            L.append("B")
        elif A==B==0:
            print "No"
            quit()
        elif A==B:
            a_nokori=RA[N]-RA[i]
            b_nokori=RB[N]-RB[i]
            if a_nokori<=b_nokori:
                B+=1
                A-=1
                L.append("B")
            else:
                A+=1
                B-=1
                L.append("A")
    elif s=="AC":
        if A<C:
            C-=1
            A+=1
            L.append("A")
        elif C<A:
            A-=1
            C+=1
            L.append("C")
        elif A==C==0:
            print "No"
            quit()
        elif A==C:
            a_nokori=RA[N]-RA[i]
            c_nokori=RC[N]-RC[i]
            if a_nokori<=c_nokori:
                C+=1
                A-=1
                L.append("C")
            else:
                A+=1
                C-=1
                L.append("A")
    elif s=="BC":
        if B<C:
            C-=1
            B+=1
            L.append("B")
        elif C<B:
            B-=1
            C+=1
            L.append("C")
        elif B==C==0:
            print "No"
            quit()
        elif B==C:
            b_nokori=RB[N]-RB[i]
            c_nokori=RC[N]-RC[i]
            if b_nokori<=c_nokori:
                C+=1
                B-=1
                L.append("C")
            else:
                B+=1
                C-=1
                L.append("B")

print "Yes"
for x in L:
    print x

Submission status: Wrong Answer

Solution code:
import sys
def input() : return sys.stdin.readline().strip()

def main():
    N, A, B, C = map(int, input().split())
    s = tuple(input() for _ in range(N))
    d = {}
    d["A"] = A
    d["B"] = B
    d["C"] = C
    ans = []
    for i in range(N-1):
        l1, r1 = s[i]
        l2, r2 = s[i+1]

        if d["A"]*d["B"]*d["C"]:
            temp = [d[l1], d[r1]]
            max_val = max(temp)
            max_index = temp.index(max_val)
            
            if max_index == 0:
                ans.append(r1)
                d[l1] -= 1
                d[r1] += 1
            else:
                ans.append(l1)
                d[l1] += 1
                d[r1] -= 1
        else:
            if d[l1] * d[r1]:
                if l1 == l2 and r1 == r2:
                    ans.append(l1)
                    d[l1] += 1
                    d[r1] -= 1
                else:
                    for c in [l1, r1]:
                        if [l1, r1, l2, r2].count(c) == 1:
                            d[c] -= 1
                        else:
                            ans.append(c)
                            d[c] += 1
            else:
                temp = [d[l1], d[r1]]
                max_val = max(temp)
                max_index = temp.index(max_val)
            
                if max_index == 0:
                    ans.append(r1)
                    d[l1] -= 1
                    d[r1] += 1
                else:
                    ans.append(l1)
                    d[l1] += 1
                    d[r1] -= 1

    l1, r1 = s[N-1]
    temp = [d[l1], d[r1]]
    max_val = max(temp)
    max_index = temp.index(max_val)
            
    if max_index == 0:
        ans.append(r1)
        d[l1] -= 1
        d[r1] += 1
    else:
        ans.append(l1)
        d[l1] += 1
        d[r1] -= 1

    if any([d[c] < 0 for c in "ABC"]):
            print("No")
    else:
        print("Yes")
        print(*ans, sep="\n")

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict

N,A,B,C=map(int, sys.stdin.readline().split())
S=[ sys.stdin.readline().strip() for _ in range(N) ]

#A,B,Cの累積登場回数
RA=[0]
RB=[0]
RC=[0]
for s in S:
    if "A" in s: RA.append(RA[-1]+1) 
    else: RA.append(RA[-1]) 
    if "B" in s: RB.append(RB[-1]+1) 
    else: RB.append(RB[-1]) 
    if "C" in s: RC.append(RC[-1]+1) 
    else: RC.append(RC[-1]) 

L=[]    #i回目の選択で1を足す変数の名前を順番に格納

for i,s in enumerate(S):
    i+=1    #1-indexed     
    if s=="AB":
        if A<B:
            B-=1
            A+=1
            L.append("A")
        elif B<A:
            A-=1
            B+=1
            L.append("B")
        elif A==B==0:
            print "No"
            quit()
        elif A==B:
            a_nokori=RA[N]-RA[i]
            b_nokori=RB[N]-RB[i]
            if a_nokori<=b_nokori:
                B+=1
                A-=1
                L.append("B")
            else:
                A+=1
                B-=1
                L.append("A")
    elif s=="AC":
        if A<C:
            C-=1
            A+=1
            L.append("A")
        elif C<A:
            A-=1
            C+=1
            L.append("C")
        elif A==C==0:
            print "No"
            quit()
        elif A==C:
            a_nokori=RA[N]-RA[i]
            c_nokori=RC[N]-RC[i]
            if a_nokori<=c_nokori:
                C+=1
                A-=1
                L.append("C")
            else:
                A+=1
                C-=1
                L.append("A")
    elif s=="BC":
        if B<C:
            C-=1
            B+=1
            L.append("B")
        elif C<B:
            B-=1
            C+=1
            L.append("C")
        elif B==C==0:
            print "No"
            quit()
        elif B==C:
            b_nokori=RB[N]-RB[i]
            c_nokori=RC[N]-RC[i]
            if b_nokori<=c_nokori:
                C+=1
                B-=1
                L.append("C")
            else:
                B+=1
                C-=1
                L.append("B")

for x in L:
    print x

Submission status: Wrong Answer

Solution code:
import sys,random
n,a,b,c=map(int,input().split())
abc={'A':a,'B':b,'C':c}
l=[input() for i in range(n)]
ans=[None]*n
def f(x,y):
  if x==0:
    return [1,-1,0]
  elif y==0:
    return [-1,1,1]
  else:
    ra=random.randrange(0,2)
    if ra==0:
      return [1,-1,0]
    else:
      return [-1,1,1]
for i in range(n):
  s=l[i]
  if abc[s[0]]+abc[s[1]]==0:
    print('No')
    sys.exit()
  elif abc[s[0]]==abc[s[1]]==1 and sum(abc.values())==2 and i!=n-1 and s!=l[i+1]:
    co=set([s[0],s[1]])&set([l[i+1][0],l[i+1][1]])
    dif=set([s[0],s[1]])-co
    co=list(co)[0]
    dif=list(dif)[0]
    ans[i]=co
    abc[co]+=1
    abc[dif]+=1
  else:
    r=f(abc[s[0]],abc[s[1]])
    ans[i]=s[r[2]]
    abc[s[0]]+=r[0]
    abc[s[1]]+=r[1]
print('Yes')
print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()
N,A,B,C=map(int,input().split())
S=[input() for i in range(N)]
idleA=10**20
idleB=10**10
idleC=1
stack=[[A,B,C]]
lst={}
dp=[defaultdict(str) for i in range(N)]
def main(a,b,c):
    return(a*idleA+b*idleB+c)
for n in range(N):
    s=S[n]
    #nlst=set()
    nstack=[]
    while stack:
        #print(stack)
        a,b,c=stack.pop()
        if s=="AB":
            if a>0:
                Q=main(a-1,b+1,c)
                if not dp[n][Q]:
                    dp[n][Q]="B"
                    nstack.append([a-1,b+1,c])
            if b>0:
                Q=main(a+1,b-1,c)
                if not dp[n][Q]:
                    dp[n][Q]="A"
                    nstack.append([a+1,b-1,c])
        elif s=="BC":
            if b>0:
                Q=main(a,b-1,c+1)
                if not dp[n][Q]:
                    dp[n][Q]="C"
                    nstack.append([a,b-1,c+1])
            if c>0:
                Q=main(a,b+1,c-1)
                if not dp[n][Q]:
                    dp[n][Q]="B"
                    nstack.append([a,b+1,c-1])
        elif s=="AC":
            if a>0:
                Q=main(a-1,b,c+1)
                if not dp[n][Q]:
                    dp[n][Q]="C"
                    nstack.append([a-1,b,c+1])
            if c>0:
                Q=main(a+1,b,c-1)
                if not dp[n][Q]:
                    dp[n][Q]="A"
                    nstack.append([a+1,b,c-1])

    stack=nstack
if nstack:
    print("Yes")
    lst=[]
    a,b,c=nstack.pop()
    lst.append([dp[n][main(a,b,c)],a,b,c])
    for n in range(N-1)[::-1]:
        s=S[n+1]
        X,a,b,c=lst[-1]
        if s=="AB":
            if a>0:
                Q=main(a-1,b+1,c)
                if dp[n][Q]:
                    lst.append([dp[n][Q],a-1,b+1,c])
                    continue
            if b>0:
                Q=main(a+1,b-1,c)
                if dp[n][Q]:
                    lst.append([dp[n][Q],a+1,b-1,c])
                    continue
        elif s=="BC":
            if b>0:
                Q=main(a,b-1,c+1)
                if dp[n][Q]:
                    lst.append([dp[n][Q],a,b-1,c+1])
                    continue
            if c>0:
                Q=main(a,b+1,c-1)
                if dp[n][Q]:
                    lst.append([dp[n][Q],a,b+1,c-1])
                    continue
        elif s=="AC":
            if a>0:
                Q=main(a-1,b,c+1)
                if dp[n][Q]:
                    lst.append([dp[n][Q],a-1,b,c+1])
                    continue
            if c>0:
                Q=main(a+1,b,c-1)
                if dp[n][Q]:
                    lst.append([dp[n][Q],a+1,b,c-1])
                    continue
        #print("a")
    for X,a,b,c in list(reversed(lst)):
        print(X)
else:
    print("No")

Submission status: Wrong Answer

Solution code:
import sys,random
n,a,b,c=map(int,input().split())
abc={'A':a,'B':b,'C':c}
l=[input() for i in range(n)]
ans=[None]*n
def f(x,y):
  if x==0:
    return [1,-1,0]
  elif y==0:
    return [-1,1,1]
  else:
    ra=random.randrange(0,2)
    if ra==0:
      return [1,-1,0]
    else:
      return [-1,1,1]
for i in range(n):
  s=l[i]
  if abc[s[0]]+abc[s[1]]==0:
    print('No')
    sys.exit()
  elif abc[s[0]]==abc[s[1]]==1 and i!=n-1 and s!=l[i+1]:
    co=set([s[0],s[1]])&set([l[i+1][0],l[i+1][1]])
    dif=set([s[0],s[1]])-co
    co=list(co)[0]
    dif=list(dif)[0]
    ans[i]=co
    abc[co]+=1
    abc[dif]+=1
  else:
    r=f(abc[s[0]],abc[s[1]])
    ans[i]=s[r[2]]
    abc[s[0]]+=r[0]
    abc[s[1]]+=r[1]
print('Yes')
print(*ans,sep='\n')

Submission status: Wrong Answer

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

N,A,B,C=input().split()
D=dict()
D["A"]=int(A)
D["B"]=int(B)
D["C"]=int(C)

S=[]
for i in range(int(N)):
    S.append(input())

ans="Yes"
F=[]
for i,s in enumerate(S):
    if D[s[0]]==0 and D[s[1]]==0:
        ans="No"
        break
    if D[s[0]]==0 and D[s[1]]==1:
        D[s[0]]=1
        D[s[1]]=0
        F.append(s[0])
    if D[s[0]]==1 and D[s[1]]==0:
        D[s[0]]=0
        D[s[1]]=1
        F.append(s[1])
    if D[s[0]]==1 and D[s[1]]==1:
        if D["A"]+D["B"]+D["C"]==2:
            if i!=int(N)-1:
                s0=S[i+1][0]
                s1=S[i+1][1]
                if s[0]==s0 or s[0]==s1:
                    D[s[0]]=2
                    D[s[1]]=0
                    F.append(s[0])
                if s[1]==s0 or s[1]==s1:
                    D[s[0]]=0
                    D[s[1]]=2
                    F.append(s[1])
    if len(F)!=i+1:
        D[s[0]]+=1
        D[s[1]]-=1
        F.append(s[0])



print(ans)
if ans=="Yes":
    for f in F:
        print(f)