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: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

def dfs(i, d, ans):
    if d[0]<0 or d[1]<0 or d[2]<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    s, t = cv(S[i][0]), cv(S[i][1])
    d[s] += 1
    d[t] -= 1
    #print(i, D[s], D[t])
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    d[s] -= 2
    d[t] += 2
    #print(i, D[s], D[t])
    ans[i] = S[i][1]
    dfs(i+1, d, ans)
    d[s] += 1
    d[t] -= 1

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2
'''
def dfs(i, A, B, C, ans):
    if A<0 or B<0 or C<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    if S[i] == 'AB':
        ans[i] = 'A'
        dfs(i+1, A+1, B-1, C, ans)
        ans[i] = 'B'
        dfs(i+1, A-1, B+1, C, ans)
    if S[i] == 'BC':
        ans[i] = 'B'
        dfs(i+1, A, B+1, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A, B-1, C+1, ans)
    if S[i] == 'AC':
        ans[i] = 'A'
        dfs(i+1, A+1, B, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A-1, B, C+1, ans)
'''
N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N
dfs(0, D, ans)
print('No')

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N, *D = mi()
S = [sys.stdin.readline()[:2] for _ in range(N)]
ans = [0]*N

def cv(m):
    if m == 'A':
        return 0
    if m == 'B':
        return 1
    if m == 'C':
        return 2

def dfs(i, A, B, C, ans):
    if A<0 or B<0 or C<0:
        return
    if i == N:
        print('Yes')
        for m in ans:
            print(m)
        exit()
    #s, t = cv(S[i][0]), cv(S[i][1])
    '''
    D[s] += 1
    D[t] -= 1
    ans[i] = S[i][0]
    dfs(i+1, D, ans)
    D_[s] -= 1
    D_[t] += 1
    ans_[i] = S[i][1]
    dfs(i+1, D_, ans_)
    '''
    if S[i] == 'AB':
        ans[i] = 'A'
        dfs(i+1, A+1, B-1, C, ans)
        ans[i] = 'B'
        dfs(i+1, A-1, B+1, C, ans)
    if S[i] == 'BC':
        ans[i] = 'B'
        dfs(i+1, A, B+1, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A, B-1, C+1, ans)
    if S[i] == 'AC':
        ans[i] = 'A'
        dfs(i+1, A+1, B, C-1, ans)
        ans[i] = 'C'
        dfs(i+1, A-1, B, C+1, ans)

dfs(0, D[0], D[1], D[2], ans)
print('No')

Submission status: Accepted

Solution code:


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

hashmap=dict()
hashmap={"A":A,"B":B,"C":C}
arr=[int(0) for i in range(N)]
for i in range(N):
    arr[i]=input()
    
output=[]

for i in range(N):
    string=arr[i]
    first=string[0]
    second=string[1]

    if hashmap[first]==0 and hashmap[second]==0:
        print("No")
        break
    elif hashmap[first]==0:
        hashmap[first]+=1
        hashmap[second]-=1
        output.append(first)
    elif hashmap[second]==0:
        hashmap[second]+=1
        hashmap[first]-=1
        output.append(second)
    elif i<N-1 and (first in arr[i+1]):
        hashmap[first]+=1
        hashmap[second]-=1
        output.append(first)
    elif i<N-1 and (second in arr[i+1]):
        hashmap[second]+=1
        hashmap[first]-=1
        output.append(second)
    else:
        output.append(first)
else:
    print("Yes")
    for i in output:
        print(i)

Submission status: Accepted

Solution code:
ABC = [0] * 3
N, ABC[0], ABC[1], ABC[2] = map(int, input().split())
s = [''] * N
for i in range(N):
	s[i] = input()
a = ''
b = True
for i in range(N):
	if not ABC[ord(s[i][0]) - ord('A')]:
		if not ABC[ord(s[i][1]) - ord('A')]:
			b = False
			break
		ABC[ord(s[i][0]) - ord('A')] += 1
		ABC[ord(s[i][1]) - ord('A')] -= 1
		a += s[i][0]
	elif not ABC[ord(s[i][1]) - ord('A')]:
		ABC[ord(s[i][0]) - ord('A')] -= 1
		ABC[ord(s[i][1]) - ord('A')] += 1
		a += s[i][1]
	else:
		if i == N - 1:
			ABC[ord(s[i][0]) - ord('A')] += 1
			ABC[ord(s[i][1]) - ord('A')] -= 1
			a += s[i][0]
		else:
			if s[i][0] == s[i + 1][0] or s[i][0] == s[i + 1][1]:
				ABC[ord(s[i][0]) - ord('A')] += 1
				ABC[ord(s[i][1]) - ord('A')] -= 1
				a += s[i][0]
			else:
				ABC[ord(s[i][0]) - ord('A')] -= 1
				ABC[ord(s[i][1]) - ord('A')] += 1
				a += s[i][1]
if b:
	print('Yes')
	for i in a:
		print(i)
else:
	print('No')

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
cc = []
for i in range(n):
  ff = input().strip()
  cc.append((ff))
  
chk = True
ans = []
for num,q in enumerate(cc):
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if a == 1 and b == 1:
      if num == n-1:
        ans.append('A')
      else:
        if cc[num+1][0] == 'A':
          a += 1
          b -= 1
          ans.append('A')
        else:
          a -= 1
          b += 1
          ans.append('B')       
    elif m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if c == 1 and a == 1:
      if num == n-1:
        ans.append('A')
      else:
        if cc[num+1][0] == 'A':
          a += 1
          c -= 1
          ans.append('A')
        else:
          a -= 1
          c += 1
          ans.append('C')       
    elif m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if c == 1 and b == 1:
      if num == n-1:
        ans.append('B')
      else:      
        if cc[num+1][1] == 'C':
          c += 1
          b -= 1
          ans.append('C')
        else:
          c -= 1
          b += 1
          ans.append('B')       
    elif m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
s = []
for _ in range(n):
    s.append(input())
if a + b + c == 0:
    print('No')
elif a + b + c == 1:
    ans = ''
    for i in range(n):
        if s[i] == 'AB':
            if a == 1 and b == 0:
                a = 0
                b = 1
                ans += 'B'
            elif a == 0 and b == 1:
                a = 1
                b = 0
                ans += 'A'
            else:
                print('No')
                exit()
        elif s[i] == 'BC':
            if b == 1 and c == 0:
                b = 0
                c = 1
                ans += 'C'
            elif b == 0 and c == 1:
                b = 1
                c = 0
                ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a == 1 and c == 0:
                a = 0
                c = 1
                ans += 'C'
            elif a == 0 and c == 1:
                a = 1
                c = 0
                ans += 'A'
            else:
                print('No')
                exit()
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans = ''
    s2 = 'AC'
    for i in range(n):
        s1 = s[i]
        if i != n-1:
            s2 = s[i+1]
        if s1 == 'AB':
            if a >= 1 and b == 0:
                a -= 1
                b = 1
                ans += 'B'
            elif a == 0 and b >= 1:
                a = 1
                b -= 1
                ans += 'A'
            elif a >= 1 and b >= 1:
                if s2 == 'BC':
                    b += 1
                    a -= 1
                    ans += 'B'
                else:
                    a += 1
                    b -= 1
                    ans += 'A'
            else:
                print('No')
                exit()
        elif s1 == 'BC':
            if b >= 1 and c == 0:
                b -= 1
                c = 1
                ans += 'C'
            elif b == 0 and c >= 1:
                b = 1
                c -= 1
                ans += 'B'
            elif b >= 1 and c >= 1:
                if s2 == 'AC':
                    c += 1
                    b -= 1
                    ans += 'C'
                else:
                    b += 1
                    c -= 1
                    ans += 'B'
            else:
                print('No')
                exit()
        else:
            if a >= 1 and c == 0:
                a -= 1
                c = 1
                ans += 'C'
            elif a == 0 and c >= 1:
                a = 1
                c -= 1
                ans += 'A'
            elif a >= 1 and c >= 1:
                if s2 == 'AB':
                    a += 1
                    c -= 1
                    ans += 'A'
                else:
                    a -= 1
                    c += 1
                    ans += 'C'
            else:
                print('No')
                exit()
        # s1 = s2
    print('Yes')
    for i in range(n):
        print(ans[i])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
N,A,B,C = map(int,input().split())
Q = []
ans = []
f = 0
def dfs(f,a,b,c):
    if f == N:return True
    if Q[f] == 'AB':
        if a+b == 0:return False
        if b>0 and dfs(f+1,a+1,b-1,c):
            ans.append('A')
            return True
        if a>0 and dfs(f+1,a-1,b+1,c):
            ans.append('B')
            return True
        
        return False
    if Q[f] == 'BC':
        if c+b == 0:return False
        if c>0 and dfs(f+1,a,b+1,c-1):
            ans.append('B')
            return True
        if b>0 and dfs(f+1,a,b-1,c+1):
            ans.append('C')
            return True
        
        return False
    if Q[f] == 'AC':
        if a+c == 0:return False
        if c>0 and dfs(f+1,a+1,b,c-1):
            ans.append('A')
            return True
        if a>0 and dfs(f+1,a-1,b,c+1):
            ans.append('C')
            return True
        
        return False
    

for i in range(N):
    s = input()
    Q.append(s)
dfs(0,A,B,C)
# print(ans)
if len(ans) == N:
    print('Yes')
    ans = ans[::-1]
    for i in ans:
        print(i)
else:
    print('No')

Submission status: Accepted

Solution code:
n, *abc = map(int, input().split())
cond = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
legend = ['A', 'B', 'C']
ans = []
prev_s = ''
for _ in range(n):
    s = input()
    idx1, idx2 = cond[s]
    if abc[idx1] == abc[idx2] == 0 and ans:
        prev_x = ans.pop()
        if prev_x == prev_s[0]:
            prev_y = prev_s[1]
        else:
            prev_y = prev_s[0]
        ans.append(prev_y)
        abc[legend.index(prev_x)] -= 2
        abc[legend.index(prev_y)] += 2

    if abc[idx1] < abc[idx2]:
        abc[idx1] += 1
        abc[idx2] -= 1
        ans.append(legend[idx1])
    else:
        abc[idx2] += 1
        abc[idx1] -= 1
        ans.append(legend[idx2])
    prev_s = s
    if -1 in abc:
        print('No')
        break
else:
    print('Yes')
    for x in ans:
        print(x)

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split())
S=[]
d={'A':A, 'B':B, 'C':C}
for i in range(N):
    S.append(input())
out=[]

for i in range(N):
    s=S[i]
    s0=s[0]
    s1=s[1]

    if d[s0]==d[s1]==0:
        print('No')
        break
    elif d[s0]==0:
        d[s0]+=1
        d[s1]-=1
        out.append(s0)
    elif d[s1]==0:
        d[s0]-=1
        d[s1]+=1
        out.append(s1)
    elif i == N-1:
        out.append(s0)
    elif s0 in S[i+1]:
        d[s0]+=1
        d[s1]-=1
        out.append(s0)
    else:
        d[s0]-=1
        d[s1]+=1
        out.append(s1)
else:
    print('Yes')
    for o in out:
        print(o)

Submission status: Accepted

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

ANS = ['']*N
for i in range(N):
    if S[i]=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        if i != N-1 and A==B and S[i+1]!='AB':
            if S[i+1]=='AC':
                ANS[i] = 'A'
                A += 1
                B -= 1
            elif S[i+1]=='BC':
                ANS[i] = 'B'
                B += 1
                A -= 1

        elif A<B:
            ANS[i] = 'A'
            A += 1
            B -= 1
        else:
            ANS[i] = 'B'
            A -= 1
            B += 1
            

    elif S[i]=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        if i != N-1 and A==C and S[i+1]!='AC':
            if S[i+1]=='AB':
                ANS[i] = 'A'
                A += 1
                C -= 1
            elif S[i+1]=='BC':
                ANS[i] = 'C'
                C += 1
                A -= 1
        elif A<C:
            ANS[i] = 'A'
            A += 1
            C -= 1
        else:
            ANS[i] = 'C'
            A -= 1
            C += 1


    elif S[i]=='BC':
        if C==0 and B==0:
            print('No')
            exit()
        if i != N-1 and C==B and S[i+1]!='BC':
            if S[i+1]=='AC':
                ANS[i] = 'C'
                C += 1
                B -= 1
            elif S[i+1]=='AB':
                ANS[i] = 'B'
                B += 1
                C -= 1
        elif C<B:
            ANS[i] = 'C'
            C += 1
            B -= 1
        else:
            ANS[i] = 'B'
            C -= 1
            B += 1
if A<0 or B<0 or C<0:
    print('No')
    exit()

print('Yes')
for a in ANS:
    print(a)

Submission status: Accepted

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

Submission status: Accepted

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

flag = True

for x in range(len(S)):
    if S[x] == "AB":
        if A == 0 and B == 0:
            flag = False
            break
        if A == 0:
            A += 1
            B -= 1
            add.append("A")
        elif B == 0:
            B += 1
            A -= 1
            add.append("B")
        elif x + 1 < len(S) and A + B + C == 2 and S[x + 1] != "AB":
            if S[x + 1] == "AC":
                A += 1
                B -= 1
                add.append("A")
            else:
                B += 1
                A -= 1
                add.append("B")
        elif A < B:
            A += 1
            B -= 1
            add.append("A")
        else:
            B += 1
            A -= 1
            add.append("B")
    elif S[x] == "AC":
        if A == 0 and C == 0:
            flag = False
            break
        if A == 0:
            A += 1
            C -= 1
            add.append("A")
        elif C == 0:
            C += 1
            A -= 1
            add.append("C")
        elif x + 1 < len(S) and A + B + C == 2 and S[x + 1] != "AC":
            if S[x + 1] == "BC":
                C += 1
                A -= 1
                add.append("C")
            else:
                A += 1
                C -= 1
                add.append("A")
        elif A < C:
            A += 1
            C -= 1
            add.append("A")
        else:
            C += 1
            A -= 1
            add.append("C")
    else:
        if B == 0 and C == 0:
            flag = False
            break
        if B == 0:
            B += 1
            C -= 1
            add.append("B")
        elif C == 0:
            C += 1
            B -= 1
            add.append("C")
        elif x + 1 < len(S) and A + B + C == 2 and S[x + 1] != "BC":
            if S[x + 1] == "AB":
                B += 1
                C -= 1
                add.append("B")
            else:
                C += 1
                B -= 1
                add.append("C")
        elif B < C:
            B += 1
            C -= 1
            add.append("B")
        else:
            C += 1
            B -= 1
            add.append("C")

if flag is True and A >= 0 and B >= 0 and C >= 0:
    print("Yes")
    for y in add:
        print(y)
else:
    print("No")

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float('inf')
MOD=10**9+7

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

s=[input() for _ in range(n)]

#不可能な場合
if a+b+c==0:
    print("No")
    exit()
elif a+b+c==1:
    result=[]
    for order in s:
        if "AB" == order:
            if c==1:
                print("No")
                exit()
            elif a==1:
                a-=1
                b+=1
                result.append("B")
            else:
                b-=1
                a+=1
                result.append("A")
        elif "AC" == order:
            if b==1:
                print("No")
                exit()
            elif a==1:
                a-=1
                c+=1
                result.append("C")
            else:
                c-=1
                a+=1
                result.append("A")
        elif "BC" == order:
            if a==1:
                print("No")
                exit()
            elif b==1:
                b-=1
                c+=1
                result.append("C")
            else:
                c-=1
                b+=1
                result.append("B")
    print("Yes")
    for item in result:
        print(item)
    exit()

else:
    result=[]
    for index,order in enumerate(s):
        if "AB" == order:
            if a==0 and b==0:
                print("No")
                exit()
            elif a==1 and b==1:
                if index <n-1:
                    if s[index+1]=="AB":
                        result.append("B")
                        a-=1
                        b+=1
                    elif s[index+1]=="AC":
                        result.append("A")
                        a+=1
                        b-=1
                    elif s[index+1]=="BC":
                        result.append("B")
                        a-=1
                        b+=1
                else:
                    a-=1
                    b+=1
                    result.append("B")
            else:
                if a<b:
                    a+=1
                    b-=1
                    result.append("A")
                else:
                    a-=1
                    b+=1
                    result.append("B")
                    
#ここっからまた変わるよ
        elif "AC" == order:
            if a==0 and c==0:
                print("No")
                exit()
            elif a==1 and c==1:
                if index <n-1:
                    if s[index+1]=="AB":
                        result.append("A")
                        a+=1
                        c-=1
                    elif s[index+1]=="AC":
                        result.append("A")
                        a+=1
                        c-=1
                    elif s[index+1]=="BC":
                        result.append("C")
                        a-=1
                        c+=1
                else:
                    a-=1
                    c+=1
                    result.append("C")
            else:
                if a<c:
                    a+=1
                    c-=1
                    result.append("A")
                else:
                    a-=1
                    c+=1
                    result.append("C")

#ここっからまた変わるよ
        elif "BC" == order:
            if b==0 and c==0:
                print("No")
                exit()
            elif b==1 and c==1:
                if index <n-1:
                    if s[index+1]=="AB":
                        result.append("B")
                        b+=1
                        c-=1
                    elif s[index+1]=="AC":
                        result.append("C")
                        c+=1
                        b-=1
                    elif s[index+1]=="BC":
                        result.append("C")
                        b-=1
                        c+=1
                else:
                    c-=1
                    b+=1
                    result.append("B")
            else:
                if b<c:
                    b+=1
                    c-=1
                    result.append("B")
                else:
                    b-=1
                    c+=1
                    result.append("C")

        
    print("Yes")
    for item in result:
        print(item)
    exit()

Submission status: Accepted

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


def add_ans(s1, s2):
    d[s1] += 1
    d[s2] -= 1
    ans.append(s1)


for i in range(N):
    now = s[i]
    x = now[0]
    y = now[1]
    if d[x] == d[y] == 0:
        print('No')
        exit()
    elif d[x] == 0:
        add_ans(x, y)
    elif d[y] == 0:
        add_ans(y, x)
    elif i == N - 1:
        add_ans(x, y)
    elif x in s[i + 1]:
        add_ans(x, y)
    else:
        add_ans(y, x)

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