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:
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def greedy(first, second, ch1, ch2):
    global ans
    if (first < second):
        first += 1
        second -= 1
        ans.append(ch1)
    else:
        first -= 1
        second += 1
        ans.append(ch2)
    return first, second

n,a,b,c = MAP()

ans = []
flag = 0

queries = []

for _ in range(n):
    queries.append(input())

for _ in range(n):
    zz = queries[_]
    if(zz=='AB'):
        if(a==b==0):
            flag = 1
            break
        if(a==b==1):
            foo = 0
            for j in range(_+1, n):
                if(queries[j]=="AC"):
                    a+=1
                    b-=1
                    ans.append("A")
                    foo = 1
                    break
                elif(queries[j]=="BC"):
                    b+=1
                    a-=1
                    foo = 1
                    ans.append("B")
                    break
            if(foo==0):
                a+=1
                b-=1
                ans.append("A")
        else:
            a,b = greedy(a, b,"A","B")

    elif(zz=="AC"):
        if (a == c == 0):
            flag = 1
            break
        if (a == c == 1):
            foo = 0
            for j in range(_ + 1, n):
                if (queries[j] == "BC"):
                    a -= 1
                    c+= 1
                    ans.append("C")
                    foo = 1
                    break
                elif (queries[j] == "AB"):
                    c-= 1
                    a += 1
                    foo = 1
                    ans.append("A")
                    break
            if (foo == 0):
                a += 1
                c -= 1
                ans.append("A")
        else:
            a, c = greedy(a, c,"A","C")
    else:
        if (b == c == 0):
            flag = 1
            break
        if (b == c == 1):
            foo = 0
            for j in range(_ + 1, n):
                if (queries[j] == "AB"):
                    c -= 1
                    b+= 1
                    ans.append("B")
                    foo = 1
                    break
                elif (queries[j] == "AC"):
                    c+= 1
                    b-= 1
                    foo = 1
                    ans.append("C")
                    break
            if (foo == 0):
                b += 1
                c -= 1
                ans.append("B")
        else:
            b, c = greedy(b, c, "B", "C")


if(flag==1):
    print("No")
else:
    print("Yes")
    for el in ans:
        print(el)


'''
6 9 1
AC
BC
AB
BC
AC
BC
AB
AB
C
C
A
C
C
C
B
A


5 9 2
5 8 3
6 7 3
6 6 4
5 6 5
5 5 6
6 4 6





'''

Submission status: Accepted

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

if A+B+C==0:
  print("No")
elif A+B+C==1:
  ans_list=[]
  for _ in range(N):
    com=input()
    if com=="AB":
      if A==1:
        A-=1
        B+=1
        ans_list.append("B")
      elif B==1:
        A+=1
        B-=1
        ans_list.append("A")
      else:
        print("No")
        break
    elif com=="AC":
      if A==1:
        A-=1
        C+=1
        ans_list.append("C")
      elif C==1:
        A+=1
        C-=1
        ans_list.append("A")
      else:
        print("No")
        break
    elif com=="BC":
      if B==1:
        B-=1
        C+=1
        ans_list.append("C")
      elif C==1:
        B+=1
        C-=1
        ans_list.append("B")
      else:
        print("No")
        break
  else:
    print("Yes")
    for ans in ans_list:
      print(ans)
else:
  com_list=[]
  for i in range(N):
    com_list.append(input())
  
  for i in range(N):
    com=com_list[i]
    if com=="AB":
      if i==0 and A==0 and B==0:
        print("No")
        break
      elif i==0:
        print("Yes")
      
      if i<N-1 and A+B+C==2 and A==1 and B==1 and com!=com_list[i+1]:
        if com_list[i+1]=="AC":
          A+=1
          B-=1
          print("A")
        elif com_list[i+1]=="BC":
          A-=1
          B+=1
          print("B")
      else:
        if A>=B:
          A-=1
          B+=1
          print("B")
        else:
          A+=1
          B-=1
          print("A")
    elif com=="AC":
      if i==0 and A==0 and C==0:
        print("No")
        break
      elif i==0:
        print("Yes")
      
      if i<N-1 and A+B+C==2 and A==1 and C==1 and com!=com_list[i+1]:
        if com_list[i+1]=="AB":
          A+=1
          C-=1
          print("A")
        elif com_list[i+1]=="BC":
          A-=1
          C+=1
          print("C")
      else:
        if A>=C:
          A-=1
          C+=1
          print("C")
        else:
          A+=1
          C-=1
          print("A")
    elif com=="BC":
      if i==0 and B==0 and C==0:
        print("No")
        break
      elif i==0:
        print("Yes")
      
      if i<N-1 and A+B+C==2 and B==1 and C==1 and com!=com_list[i+1]:
        if com_list[i+1]=="AB":
          B+=1
          C-=1
          print("B")
        elif com_list[i+1]=="AC":
          B-=1
          C+=1
          print("C")
      else:
        if B>=C:
          B-=1
          C+=1
          print("C")
        else:
          B+=1
          C-=1
          print("B")

Submission status: Accepted

Solution code:
import sys
K = list(map(int,input().split()))
N = K[0]
S = sum(K[1:4])

if S == 0:
  print("No")
  sys.exit()

s = input()
out = [0 for _ in range(N)]

for ct in range(N):
  #print(K)
  if ct < N-1:
    s_next = input()
    
  if s == "AB":
    i = 1
    j = 2
  elif s == "BC":
    i = 2
    j = 3
  else:
    i = 1
    j = 3
  
  if K[i] + K[j] == 0:
      print("No")
      sys.exit()
  elif K[i] > K[j]:
    K[i] -= 1
    K[j] += 1
    log = j
  elif K[i] < K[j]:
    K[i] += 1
    K[j] -= 1
    log = i
  elif K[i] == K[j]:
    if ct == N-1:
      K[i] += 1
      K[j] -= 1
      log = i
      out[ct] = i
      break
      
    if s_next == "AB":
      i_n = 1
      j_n = 2
    elif s_next == "BC":
      i_n = 2
      j_n = 3
    else:
      i_n = 1
      j_n = 3
      
    if i_n == i or j_n == i:
      K[i] += 1
      K[j] -= 1
      log = i
    else:
      K[j] += 1
      K[i] -= 1  
      log = j
  #print(log)
  out[ct] = log
  if ct < N-1:
    s = s_next

print("Yes")
for o in out:
  print(chr(64+o))

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():

    N, A, B, C = in_nn()
    S = [in_s() for _ in range(N)]

    ans = []
    for i in range(N):
        s = S[i]
        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                exit()
            elif A == 0:
                A += 1
                B -= 1
                ans.append('A')
            elif B == 0:
                A -= 1
                B += 1
                ans.append('B')
            elif A > B:
                A -= 1
                B += 1
                ans.append('B')
            elif A == 1 and B == 1 and i < N - 1 and S[i + 1] != 'AB':
                if 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')
        elif s == 'BC':
            if B == 0 and C == 0:
                print('No')
                exit()
            elif B == 0:
                B += 1
                C -= 1
                ans.append('B')
            elif C == 0:
                B -= 1
                C += 1
                ans.append('C')
            elif B > C:
                B -= 1
                C += 1
                ans.append('C')
            elif B == 1 and C == 1 and i < N - 1 and S[i + 1] != 'BC':
                if S[i + 1] == 'AC':
                    B -= 1
                    C += 1
                    ans.append('C')
                elif S[i + 1] == 'AB':
                    B += 1
                    C -= 1
                    ans.append('B')
            else:
                B += 1
                C -= 1
                ans.append('B')
        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                exit()
            elif A == 0:
                A += 1
                C -= 1
                ans.append('A')
            elif C == 0:
                A -= 1
                C += 1
                ans.append('C')
            elif A > C:
                A -= 1
                C += 1
                ans.append('C')
            elif A == 1 and C == 1 and i < N - 1 and S[i + 1] != 'AC':
                if S[i + 1] == 'BC':
                    A -= 1
                    C += 1
                    ans.append('C')
                elif S[i + 1] == 'AB':
                    A += 1
                    C -= 1
                    ans.append('A')
            else:
                A += 1
                C -= 1
                ans.append('A')

    print('Yes')
    print('\n'.join(ans))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

n, a, b, c = map(int, input().split())
ans = []
ss = []
for _ in range(n):
    ss.append(input().strip())
ss.append('')

def ab():
    global a
    global b
    global c
    ans.append('B')
    a -= 1
    b += 1
def ba():
    global a
    global b
    global c
    ans.append('A')
    a += 1
    b -= 1
def ac():
    global a
    global b
    global c
    ans.append('C')
    a -= 1
    c += 1
def ca():
    global a
    global b
    global c
    ans.append('A')
    a += 1
    c -= 1
def bc():
    global a
    global b
    global c
    ans.append('C')
    b -= 1
    c += 1
def cb():
    global a
    global b
    global c
    ans.append('B')
    b += 1
    c -= 1


for i, s in enumerate(ss):
    if i == n:
        break

    if s == 'AB':
        if a == 0 and b == 0:
            print('No')
            exit()
        elif a == 1 and b == 1:
            nx = ss[i + 1]
            if nx == 'BC':
                ab()
            else:
                ba()
        elif a >= b:
            ab()
        else:
            ba()
    elif s == 'BC':
        if b == 0 and c == 0:
            print('No')
            exit()
        elif b == 1 and c == 1:
            nx = ss[i + 1]
            if nx == 'AC':
                bc()
            else:
                cb()
        elif b >= c:
            bc()
        else:
            cb()
    elif s == 'AC':
        if a == 0 and c == 0:
            print('No')
            exit()
        elif a == 1 and c == 1:
            nx = ss[i + 1]
            if nx == 'AB':
                ca()
            else:
                ac()
        elif a >= c:
            ac()
        else:
            ca()

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

Submission status: Accepted

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

Submission status: Accepted

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

abc = [a, b, c]

def yes():
    print('Yes')
    for c in ans:
        print(chr(c+ord('A')))

def no():
    print('No')
    exit()

def move(s, t):
    ans.append(s)
    abc[s] += 1
    abc[t] -= 1

play = []

for i in range(n):
    s = bytes(input(), 'ascii')
    s, t = s[0]-ord('A'), s[1]-ord('A')

    play.append((s, t))

ans = []
for i, (s, t) in enumerate(play):
    if abc[s] == 0 and abc[t] == 0:
        no()
        break
    if abc[s] == abc[t]:
        if s in play[(i+1)%n]:
            move(s, t)
        else:
            move(t, s)
    elif abc[s] < abc[t]:
        move(s, t)
    else:
        move(t, s)

yes()

Submission status: Accepted

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

d = {'A':a,'B':b,'C':c}

ans = ['Yes']

for i in range(n):
    if d[s[i][0]] == 0 and d[s[i][1]] == 0:
        print('No')
        exit()
    if d[s[i][0]] == 1 and d[s[i][1]] == 1 and n-i > 1:
        d[s[i][0]] = 2
        d[s[i][1]] = 0
        if d[s[i+1][0]] == 0 and d[s[i+1][1]] ==0:
            d[s[i][0]] = 0
            d[s[i][1]] = 2
            ans.append(s[i][1])
        else:
            ans.append(s[i][0])
    elif d[s[i][0]] >= d[s[i][1]]:
        d[s[i][0]] = d[s[i][0]] - 1
        d[s[i][1]] = d[s[i][1]] + 1
        ans.append(s[i][1])
    else:
        d[s[i][0]] = d[s[i][0]] + 1
        d[s[i][1]] = d[s[i][1]] - 1
        ans.append(s[i][0])

for i in range(n+1):
    print(ans[i])

Submission status: Accepted

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()
    moves = []
    ans = []
    abc = [a,b,c]
    for i in range(n):
        moves.append(input())
    moves.append(moves[-1])

    for i in range(len(moves) - 1):
        curr = moves[i]
        nxt = moves[i + 1]
        x = {"A":0, "B":1, "C":2}[curr[0]]
        y = {"A":0, "B":1, "C":2}[curr[1]]
        nx = {"A":0, "B":1, "C":2}[nxt[0]]
        ny = {"A":0, "B":1, "C":2}[nxt[1]]

        if abc[x] == 0 and abc[y] == 0:
            print ("No")
            return
        elif abc[x] == 0:
            abc[x] += 1
            abc[y] -= 1
            ans.append(curr[0])
        elif abc[y] == 0:
            abc[x] -= 1
            abc[y] += 1
            ans.append(curr[1])
        elif x == y and nx == ny:
            abc[x] += 1
            abc[y] -= 1
            ans.append(curr[0])
        else:
            common = list(set([x,y]).intersection(set([nx,ny])))[0]
            if x == common:
                other = y
                ans.append(curr[0])
            else:
                other = x
                ans.append(curr[1])
            abc[common] += 1
            abc[other] -= 1

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


mode = 's'

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

Submission status: Accepted

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

history = []

def move_to_flatten(s):
    global A, B, C, history
    if s == "AB":
        if A >= B:
            A -= 1
            B += 1
            history.append("B")
        else:
            B -= 1
            A += 1
            history.append("A")
    elif s == "BC":
        if B >= C:
            B -= 1
            C += 1
            history.append("C")
        else:
            C -= 1
            B += 1
            history.append("B")
    elif s == "AC":
        if A >= C:
            A -= 1
            C += 1
            history.append("C")
        else:
            C -= 1
            A += 1
            history.append("A")
        

for i, s in enumerate(ss):
    zeros = (A, B, C).count(0)
    if zeros == 0:
        move_to_flatten(s)
    elif zeros == 1:
        if A + B + C == 2:
            if A == 0:
                if s == "BC":
                    if i+1 < N and 'B' in ss[i+1]: 
                        C -= 1
                        B += 1
                        history.append('B')
                    else:
                        B -= 1
                        C += 1
                        history.append('C')
                else:
                    move_to_flatten(s)
            elif B == 0:
                if s == "AC":
                    if i+1 < N and 'C' in ss[i+1]:
                        A -= 1
                        C += 1
                        history.append('C')
                    else:
                        C -= 1
                        A += 1
                        history.append('A')
                else:
                    move_to_flatten(s)
            elif C == 0:
                if s == "AB":
                    if i+1 < N and 'A' in ss[i+1]:
                        B -= 1
                        A += 1
                        history.append('A')
                    else:
                        A -= 1
                        B += 1
                        history.append('B')
                else:
                    move_to_flatten(s)    
        else:
            move_to_flatten(s)
    elif zeros == 2:
        if A != 0 and s == "BC":
            print("No")
            exit()
        elif B != 0 and s == "AC":
            print("No")
            exit()
        elif C != 0 and s == "AB":
            print("No")
            exit()
        move_to_flatten(s)
    else:
        print("No")
        exit()

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n, *abc = LI()
    s = SR(n)
    d = {"A": 0, "B": 1, "C": 2}
    ans = [None] * n
    for i in range(n - 1):
        S1, S2 = s[i][0], s[i][1]
        d1, d2 = d[S1], d[S2]
        if abc[d1] == abc[d2] == 0:
            print("No")
            return
        else:
            if abc[d1] == abc[d2] == 1:
                S11, S12 = s[i+1][0], s[i+1][1]
                d11, d12 = d[S11], d[S12]
                if d1 in [d11, d12]:
                    abc[d1] += 1
                    abc[d2] -= 1
                    ans[i] = S1
                else:
                    abc[d1] -= 1
                    abc[d2] += 1
                    ans[i] = S2
            else:
                if abc[d1] <= abc[d2]:
                    abc[d1] += 1
                    abc[d2] -= 1
                    ans[i] = S1
                else:
                    abc[d2] += 1
                    abc[d1] -= 1
                    ans[i] = S2

    S1, S2 = s[-1][0], s[-1][1]
    d1, d2 = d[S1], d[S2]
    if abc[d1] == abc[d2] == 0:
        print("No")
        return
    if abc[d1] <= abc[d2]:
        abc[d1] += 1
        abc[d2] -= 1
        ans[-1] = S1
    else:
        abc[d2] += 1
        abc[d1] -= 1
        ans[-1] = S2
    print("Yes")
    print(*ans, sep="\n")
    return


#main
if __name__ <= '__main__':
    solve()

Submission status: Accepted

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] or sii[1] == 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: Accepted

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
        if A + B + C == 2 and ABC[s1] == 1 and ABC[s2] == 1 and i < N - 1 and S[i] != S[i + 1]:
            s1n, s2n = S[i + 1]
            if s1 in [s1n, s2n]:
                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: Accepted

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    d = {'A':A, 'B':B, 'C':C}
    l = []
    for _ in range(N):
        s = input()
        l.append(s)
    r = []
    for i in range(N):
        x, y  = l[i][0], l[i][1]
        z = 'A'
        for j in d:
            if j not in l[i]:
                z = j
        if d[x] > d[y]:
            d[x] -= 1
            d[y] += 1
            r.append(y)
        elif d[y] > d[x]:
            d[x] += 1
            d[y] -= 1
            r.append(x)
        else:
            if d[x] > 1 or i == N-1:
                d[x] += 1
                d[y] -= 1
                r.append(x)
            elif set(l[i]) == set(l[i+1]):
                d[x] += 1
                d[y] -= 1
                r.append(x)
            else:
                x2 = 'A'
                for j in l[i+1]:
                    if j != z:
                        if j == y:
                            x, y = y, x
                if d[z] > 1:
                    d[x] -= 1
                    d[y] += 1
                    r.append(y)
                else:
                    d[x] += 1
                    d[y] -= 1
                    r.append(x)
            if d[x] < 0 or d[y] < 0 or d[z] < 0:
                return False, r
    return True, r
x, y = main()
if x:
    print('Yes')
    for i in y:
        print(i)
else:
    print('No')