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
dict_in={"AB":[0,1],"AC":[0,2],"BC":[1,2]}
dict_out={0:"A",1:"B",2:"C"}
n,a,b,c=map(int,input().split())
var=[a,b,c]
s=[dict_in[input()] for i in range(n)]
ans=[]
for i in range(n):
  if var[s[i][0]]>var[s[i][1]]:
    var[s[i][0]]-=1
    var[s[i][1]]+=1
    ans.append(dict_out[s[i][1]])
  elif var[s[i][0]]<var[s[i][1]]:
    var[s[i][0]]+=1
    var[s[i][1]]-=1
    ans.append(dict_out[s[i][0]])
  else:
    if var[s[i][0]]==0 and var[s[i][1]]==0:
      print("No")
      sys.exit()
    inc=s[i][1]
    for j in range(i+1,n):
      if not(s[i][0] in s[j]):
        inc=s[i][1]
        break
      elif not(s[i][1] in s[j]):
        inc=s[i][0]
        var[s[i][0]]+=1
        var[s[i][1]]-=1
        break
    if inc==s[i][1]:
      var[s[i][0]]-=1
      var[s[i][1]]+=1
    ans.append(dict_out[inc])
print("Yes")
for i in range(len(ans)):
  print(ans[i])

Submission status: Accepted

Solution code:
N,A,B,C = list(map(int,input().split()))

S = [input() for i in range(N)]

if A+B+C==0:
    print("No")
elif A+B+C==1:
    ans = []
    flag = True
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                flag = False
                break
            else:
                if A==0:
                    ans.append("A")
                else:
                    ans.append("B")
                A,B = B,A
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    ans.append("B")
                else:
                    ans.append("C")
                B,C = C,B
        elif S[i]=="AC":
            if A==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if A==0:
                    ans.append("A")
                else:
                    ans.append("C")
                A,C = C,A
    if flag:
        print("Yes")
        for a in ans:
            print(a)
elif A+B+C>=2:
    ans = []
    flag = True
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    B += 1
                    A -= 1
                    ans.append("B")
                elif A==0:
                    B -= 1
                    A += 1
                    ans.append("A")
                elif A==1 and B==1:
                    if i<N-1:
                        if S[i+1]=="AB":
                            A+=1
                            B-=1
                            ans.append("A")
                        elif S[i+1]=="BC":
                            A-=1
                            B+=1
                            ans.append("B")
                        elif S[i+1]=="AC":
                            A+=1
                            B-=1
                            ans.append("A")
                    else:
                        A+=1
                        B-=1
                        ans.append("A")
                else:
                    if A<=B:
                        A+=1
                        B-=1
                        ans.append("A")
                    else:
                        A-=1
                        B+=1
                        ans.append("B")
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    B += 1
                    C -= 1
                    ans.append("B")
                elif C==0:
                    B -= 1
                    C += 1
                    ans.append("C")
                elif B==1 and C==1:
                    if i<N-1:
                        if S[i+1]=="AB":
                            C-=1
                            B+=1
                            ans.append("B")
                        elif S[i+1]=="BC":
                            B+=1
                            C-=1
                            ans.append("B")
                        elif S[i+1]=="AC":
                            B-=1
                            C+=1
                            ans.append("C")
                    else:
                        B+=1
                        C-=1
                        ans.append("B")
                else:
                    if B<=C:
                        B+=1
                        C-=1
                        ans.append("B")
                    else:
                        B-=1
                        C+=1
                        ans.append("C")
        if S[i]=="AC":
            if A==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if C==0:
                    C += 1
                    A -= 1
                    ans.append("C")
                elif A==0:
                    C -= 1
                    A += 1
                    ans.append("A")
                elif A==1 and C==1:
                    if i<N-1:
                        if S[i+1]=="AB":
                            A+=1
                            C-=1
                            ans.append("A")
                        elif S[i+1]=="BC":
                            A-=1
                            C+=1
                            ans.append("C")
                        elif S[i+1]=="AC":
                            A+=1
                            C-=1
                            ans.append("A")
                    else:
                        A+=1
                        C-=1
                        ans.append("A")
                else:
                    if A<=C:
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
    if flag:
        print("Yes")
        for a in ans:
            print(a)

Submission status: Accepted

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

L = [A, B, C]
ret = []
for i in range(N) :
  x, y = s[i]
  done = False
  if L[x] + 1 >= 0 and L[y] - 1 >= 0 :
    L[x] += 1
    L[y] -= 1
    if i + 1 != len(s) :
      u, v = s[i + 1]
      if (L[u] + 1 >= 0 and L[v] - 1 >= 0) or (L[u] - 1 >= 0 and L[v] + 1 >= 0) :
        done = True
        ret.append(x)
    else :
      done = True
      ret.append(x)
    if not done :
      L[x] -= 1
      L[y] += 1
        
  if not done and L[x] - 1 >= 0 and L[y] + 1 >= 0 :
    L[x] -= 1
    L[y] += 1
    if i + 1 != len(s) :
      u, v = s[i + 1]
      if (L[u] - 1 >= 0 and L[v] + 1 >= 0) or (L[u] + 1 >= 0 and L[v] - 1 >= 0) :
        done = True
        ret.append(y)    
    else :
      done = True
      ret.append(y)
    if not done :
      L[x] += 1
      L[y] -= 1
  
  if not done :
    print('No')
    break
else :
  print('Yes')
  for r in ret :
    print(chr(ord('A') + r))

Submission status: Accepted

Solution code:
def solve():
    ans = []
    for i, (x, y) in enumerate(s):
        if num[x]>num[y]: x, y = y, x
        if num[x]==num[y] and i < n-1:
            if y == s[i+1][0] or y == s[i+1][1]:
                x, y = y, x
        num[x]+=1
        num[y]-=1
        ans.append(x)
        if num[y]<0: return False
    return ans
from collections import defaultdict
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
num = defaultdict(int)
num['A'] = a
num['B'] = b
num['C'] = c
ans = solve()
if ans:
    print('Yes')
    print(*ans, sep = '\n')
else:
    print('No')

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read

#n = int(input())
n,*a = [int(i) for i in readline().split()]
s = read().split()

ans = ""
ok = 1

d = {"AB":0,"BC":1,"AC":2}
d2 = "ABC"

if sum(a) >= 3 or sum(a) <= 1:
    for i in s:
        v = d[i]
        w = (v+1)%3
        if a[w] < a[v]: v,w = w,v
        a[v] += 1
        a[w] -= 1
        ans += d2[v]
        if a[w] < 0: ok = 0
    
        #print(i,a,d2[v])
    if ok:
        print("Yes")
        print(*ans,sep="\n")
    else:
        print("No")


#d = {"AB":0,"BC":1,"AC":2}
#d2 = "ABC"

else: # (1,1,0) or (2,0,0)型
    for idx,c in enumerate(s):
        v = d[c]
        w = (v+1)%3
        
        if a[v]==1 and a[w]==1 and idx<n-1 and s[idx+1] != c:#先読み
            vv = d[s[idx+1]]
            ww = (vv+1)%3
            r = [0]*3
            r[v] += 1; r[w] += 1; r[vv] += 1; r[ww] += 1
            vv = r.index(2)
            ww = (vv+1)%3 if vv==v else (vv-1)%3
            a[vv] += 1
            a[ww] -= 1
            ans += d2[vv]
            if a[ww] < 0: ok = 0
        else: 
            if a[w] < a[v]: v,w = w,v
            a[v] += 1
            a[w] -= 1
            ans += d2[v]
            if a[w] < 0: ok = 0
        
        #print(a,c,ans[-1])
        
    if ok:
        print("Yes")
        print(*ans,sep="\n")
    else:
        print("No")

Submission status: Accepted

Solution code:
def solve():
    ans = []
    for i, (x, y) in enumerate(s):
        if num[x]>num[y]: x, y = y, x
        if num[x]==num[y] and i < n-1:
            if y == s[i+1][0] or y == s[i+1][1]:
                x, y = y, x
        num[x]+=1
        num[y]-=1
        ans.append(x)
        if num[y]<0: return False
    return ans
from collections import defaultdict
import sys
n, a, b, c = map(int, input().split())
num = defaultdict(int)
s = [input() for _ in range(n)]
num['A'] = a
num['B'] = b
num['C'] = c
id = ['A', 'B', 'C']
ans = solve()
if ans:
    print('Yes')
    print(*ans, sep = '\n')
else:
    print('No')

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1
ans=[]
def dame():
  print("No")
  exit()

n,A,B,C = map(int,input().split())
S = [input() for i in range(n)]
for i in range(n-1):
  si = S[i];sii = S[i+1]
  if si == "AB":
    if A == 0 and B == 0:dame()
    elif A == 0 or B >= 2:A+=1;ans.append("A");B-=1
    elif B == 0 or A >= 2:A-=1;B+=1;ans.append("B")
    else:
      if sii == "AC":
        A+=1;ans.append("A");B-=1
      else:
        A-=1;B+=1;ans.append("B")

  if si == "AC":
    if A == 0 and C == 0:dame()
    elif A == 0 or C >= 2:A+=1;ans.append("A");C-=1
    elif C == 0 or A >= 2:A-=1;C+=1;ans.append("C")
    else:
      if sii == "AB":
        A+=1;ans.append("A");C-=1
      else:
        A-=1;C+=1;ans.append("C")

  if si == "BC":
    if C == 0 and B == 0:dame()
    elif C == 0 or B >= 2:C+=1;ans.append("C");B-=1
    elif B == 0 or C >= 2:C-=1;B+=1;ans.append("B")
    else:
      if sii == "AC":
        C+=1;ans.append("C");B-=1
      else:
        C-=1;B+=1;ans.append("B")

si = S[n-1]
if si == "AB":
  if A == 0 and B == 0:dame()
  elif A == 0 or B >= 2:A+=1;ans.append("A");B-=1
  else:A-=1;B+=1;ans.append("B")
if si == "AC":
  if A == 0 and C == 0:dame()
  elif A == 0 or C >= 2:A+=1;ans.append("A");C-=1
  else:A-=1;C+=1;ans.append("C")
if si == "BC":
  if C == 0 and B == 0:dame()
  elif C == 0 or B >= 2:C+=1;ans.append("C");B-=1
  else:C-=1;B+=1;ans.append("B")


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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

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

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

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    def f(s):
        return ord(s)-ord("A")
    n,a,b,c = LI()
    l = [a,b,c]
    ans = []
    s = [list(map(f, input())) for i in range(n)]
    for i in range(n):
        si = s[i]
        si.sort()
        if l[si[0]] == l[si[1]] == 0:
            print("No")
            return
        if l[si[0]] == l[si[1]] == 1 and min(l) == 0 and i+1 < n:
            nsi = s[i+1]
            nsi.sort()
            if nsi == si:
                if l[si[0]] < l[si[1]]:
                    l[si[0]] += 1
                    l[si[1]] -= 1
                    ans.append(chr(si[0]+ord("A")))
                else:
                    l[si[1]] += 1
                    l[si[0]] -= 1
                    ans.append(chr(si[1]+ord("A")))
            else:
                k = [0]*3
                for i in si+nsi:
                    k[i] += 1
                    if k[i] == 2:
                        break
                l[i] += 1
                for j in si:
                    if j != i:
                        break
                l[j] -= 1
                ans.append(chr(i+ord("A")))
        elif l[si[0]] < l[si[1]]:
            l[si[0]] += 1
            l[si[1]] -= 1
            ans.append(chr(si[0]+ord("A")))
        else:
            l[si[1]] += 1
            l[si[0]] -= 1
            ans.append(chr(si[1]+ord("A")))
    print("Yes")
    for i in ans:
        print(i)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

N, A, B, C = map(int, input().split())
command = [sorted(list(input())) for _ in range(N)]

ans = []


def game(i, a, b, c, prev):
    if i == N:
        ans.append(prev)
        return True
    x, y = command[i]
    if x == "A" and y == "B":
        if b:
            bl = game(i + 1, a + 1, b - 1, c, x)
            if bl:
                ans.append(prev)
                return True
        if a:
            bl = game(i + 1, a - 1, b + 1, c, y)
            if bl:
                ans.append(prev)
                return True
    elif x == "A" and y == "C":
        if c:
            bl = game(i + 1, a + 1, b, c - 1, x)
            if bl:
                ans.append(prev)
                return True
        if a:
            bl = game(i + 1, a - 1, b, c + 1, y)
            if bl:
                ans.append(prev)
                return True
    else:
        if c:
            bl = game(i + 1, a, b + 1, c - 1, x)
            if bl:
                ans.append(prev)
                return True
        if b:
            bl = game(i + 1, a, b - 1, c + 1, y)
            if bl:
                ans.append(prev)
                return True

    return False


bl = game(0, A, B, C, "?")
if not bl:
    print("No")
else:
    print("Yes")
    ans.pop()
    for x in ans[::-1]:
        print(x)

Submission status: Accepted

Solution code:
import sys,bisect
input = sys.stdin.readline

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

ab = []
ac = []
bc = []


s = [0]*n
for i in range(n):
    inp = input()
    #print(inp)
    if inp == "AB\n":
        ab.append(i)
    elif inp == "AC\n":
        ac.append(i)
        s[i] = 1
    else:
        bc.append(i)
        s[i] = 2

res = []
for i in range(n):
    if s[i] == 0:
        if a == b:
            if a == 0:
                print("No")
                exit()
            p = bisect.bisect_left(ac,i)
            q = bisect.bisect_left(bc,i)
            if p >= len(ac):
                a -= 1
                b += 1
                res.append("B")
            elif q >= len(bc):
                a += 1
                b -= 1
                res.append("A")
            else:
                if ac[p] > bc[q]:
                    a -= 1
                    b += 1
                    res.append("B")
                else:
                    a += 1
                    b -= 1
                    res.append("A")
        elif a > b:
            a -= 1
            b += 1
            res.append("B")
        else:
            a += 1
            b -= 1
            res.append("A")
    elif s[i] == 1:
        if a == c:
            if a == 0:
                print("No")
                exit()
            p = bisect.bisect_left(ab,i)
            q = bisect.bisect_left(bc,i)
            if p >= len(ab):
                a -= 1
                c += 1
                res.append("C")
            elif q >= len(bc):
                a += 1
                c -= 1
                res.append("A")
            else:
                if ab[p] > bc[q]:
                    a -= 1
                    c += 1
                    res.append("C")
                else:
                    a += 1
                    c -= 1
                    res.append("A")
        elif a > c:
            a -= 1
            c += 1
            res.append("C")
        else:
            a += 1
            c -= 1
            res.append("A")
    elif s[i] == 2:
        if b == c:
            if b == 0:
                print("No")
                exit()
            p = bisect.bisect_left(ab,i)
            q = bisect.bisect_left(ac,i)
            if p >= len(ab):
                b -= 1
                c += 1
                res.append("C")
            elif q >= len(ac):
                b += 1
                c -= 1
                res.append("B")
            else:
                if ab[p] > ac[q]:
                    b -= 1
                    c += 1
                    res.append("C")
                else:
                    b += 1
                    c -= 1
                    res.append("B")
        elif b > c:
            b -= 1
            c += 1
            res.append("C")
        else:
            b += 1
            c -= 1
            res.append("B")   

print("Yes")
print(*res,sep='\n')

Submission status: Accepted

Solution code:
n, *a = [int(i) for i in input().split()]
abc = "ABC"
xy = []
for _ in range(n):
    xy.append([abc.index(i) for i in input()])
i = 0
ans = []
while i < n:
    if a[xy[i][0]] == 1 and a[xy[i][1]] == 1:
        j = list(set([0, 1, 2])-set(xy[i]))[0]
        if i == n - 1 or a[j] != 0 or xy[i] == xy[i+1]:
            if a[xy[i][0]] > a[xy[i][1]]:
                ans.append(abc[xy[i][1]])
                a[xy[i][0]] -= 1
                a[xy[i][1]] += 1
            else:
                ans.append(abc[xy[i][0]])
                a[xy[i][1]] -= 1
                a[xy[i][0]] += 1
            i += 1
        else:
            k = list(set(xy[i + 1])-set([j]))[0]
            ans.append(abc[k])
            a[k] += 1
            a[list(set(xy[i]) - set([k]))[0]] -= 1
            i += 1

    else:
        if a[xy[i][0]] > a[xy[i][1]]:
            ans.append(abc[xy[i][1]])
            a[xy[i][0]] -= 1
            a[xy[i][1]] += 1
        else:
            ans.append(abc[xy[i][0]])
            a[xy[i][1]] -= 1
            a[xy[i][0]] += 1
        i += 1
    if min(a) < 0:
        print("No")
        exit()
print("Yes")
for i in ans:
    print(i)

Submission status: Accepted

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

for i in range(N):
	s = S[i]
	if s == "AB":
		if A > B:
			command.append("B")
			B += 1
			A -= 1
		elif A < B:
			command.append("A")
			A += 1
			B -= 1
		else:
			if A == 0:
				print("No")
				exit()
			if i == N-1:
				command.append("A")
				continue
			nxt = S[i+1]
			if nxt == "AB":
				command.append("A")
				A += 1
				B -= 1
			elif nxt == "BC":
				command.append("B")
				B += 1
				A -= 1
			else:
				command.append("A")
				A += 1
				B -= 1
	elif s == "BC":
		if B > C:
			command.append("C")
			C += 1
			B -= 1
		elif B < C:
			command.append("B")
			B += 1
			C -= 1
		else:
			if B == 0:
				print("No")
				exit()
			if i == N-1:
				command.append("B")
				continue
			nxt = S[i+1]
			if nxt == "BC":
				command.append("B")
				B += 1
				C -= 1
			elif nxt == "AC":
				command.append("C")
				C += 1
				B -= 1
			else:
				command.append("B")
				B += 1
				C -= 1
	elif s == "AC":
		if C > A:
			command.append("A")
			A += 1
			C -= 1
		elif C < A:
			command.append("C")
			C += 1
			A -= 1
		else:
			if C == 0:
				print("No")
				exit()
			if i == N-1:
				command.append("C")
				continue
			nxt = S[i+1]
			if nxt == "AC":
				command.append("C")
				C += 1
				A -= 1
			elif nxt == "AB":
				command.append("A")
				A += 1
				C -= 1
			else:
				command.append("C")
				C += 1
				A -= 1
print("Yes")
for c in command:
	print(c)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
import os
import sys
from io import BytesIO, IOBase


def main():
    n,a,b,c=map(int,input().split())
    d={}
    d['A']=a
    d['B']=b
    d['C']=c

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

    ans=[0]*n
    for i in range(n):
        s=ss[i]
        pos=0
        if d[s[0]]>d[s[1]]:
            pos=0
        elif d[s[1]]>d[s[0]] or i==n-1:
            pos=1
        elif s[0] in ss[i+1]:
            pos=1
        else:
            pos=0

        d[s[pos]]-=1
        d[s[pos^1]]+=1
        ans[i]=ord(s[pos^1])
    
        # print(d)

        if d['A']<0 or d['B']<0 or d['C']<0 :
            print("No")
            exit(0)
    print("Yes")
    print("\n".join(chr(x) for x in ans))

# region fastio

BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

# endregion

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

ANS=[]
for i in range(N):
    s=S[i]
    if s=="AB":
        if A==B==1 and i<N-1:
            if S[i+1]=="AC":
                A+=1
                B-=1
                ANS.append("A")
            else:
                B+=1
                A-=1
                ANS.append("B")

        else:
            if min(A,B)==A:
                A+=1
                B-=1
                ANS.append("A")
            else:
                B+=1
                A-=1
                ANS.append("B")

    elif s=="BC":
        if B==C==1 and i<N-1:
            if S[i+1]=="AC":
                C+=1
                B-=1
                ANS.append("C")
            else:
                B+=1
                C-=1
                ANS.append("B")

        else:
            if min(B,C)==B:
                B+=1
                C-=1
                ANS.append("B")
            else:
                C+=1
                B-=1
                ANS.append("C")

    else:
        if A==C==1 and i<N-1:
            if S[i+1]=="AB":
                A+=1
                C-=1
                ANS.append("A")
            else:
                C+=1
                A-=1
                ANS.append("C")

        else:
            if min(A,C)==A:
                A+=1
                C-=1
                ANS.append("A")
            else:
                C+=1
                A-=1
                ANS.append("C")

    if min(A,B,C)<0:
        print("No")
        sys.exit()

print("Yes")
print("\n".join(ANS))

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


N, A, B, C = map(int, input().split())
Ss = [input().rstrip() for _ in range(N)]



ok = True
ans = []
for i, S in enumerate(Ss):
    if S == "AC":
        if A > C:
            T = "C"
            C += 1
            A -= 1
        elif A < C:
            T = "A"
            A += 1
            C -= 1
        elif i != N-1:
            if "A" in Ss[i+1]:
                T = "A"
                A += 1
                C -= 1
            else:
                T = "C"
                A -= 1
                C += 1
        else:
            T = "A"
            A += 1
            C -= 1
    elif S == "AB":
        if A > B:
            T = "B"
            B += 1
            A -= 1
        elif A < B:
            T = "A"
            A += 1
            B -= 1
        elif i != N-1:
            if "A" in Ss[i+1]:
                T = "A"
                A += 1
                B -= 1
            else:
                T = "B"
                A -= 1
                B += 1
        else:
            T = "A"
            A += 1
            B -= 1
    else:
        if B > C:
            T = "C"
            C += 1
            B -= 1
        elif B < C:
            T = "B"
            B += 1
            C -= 1
        elif i != N-1:
            if "B" in Ss[i+1]:
                T = "B"
                B += 1
                C -= 1
            else:
                T = "C"
                B -= 1
                C += 1
        else:
            T = "B"
            B += 1
            C -= 1
    if A < 0 or B < 0 or C < 0:
        ok = False
        break
    ans.append(T)

if ok:
    print("Yes")
    print("\n".join(ans))
else:
    print("No")