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:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))


n,a,b,c = mi()
s = []
for i in range(n):
    s.append(input())
z = a+ b+ c
flag = True
ans = []
if z != 2:
    for i in range(n):
        if s[i] == "AB":
            if a == 0 and b == 0:
                print("No")
                exit()
            if a > b:
                a -= 1
                b += 1
                ans.append("B")
            else:
                a += 1
                b -= 1
                ans.append("A")

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

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

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

Submission status: Accepted

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

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

q = []
ans = 'Yes'
for n in range(N):
    s = S[n]
    if n < N-1:
        s1 = S[n+1]
    else:
        s1 = ''
    
    if s == 'AB':
        if A ==0 and B==0:
            ans = 'No'
            break
        elif A == B:
            if s1 == 'AC':
                A += 1
                B -= 1
                q.append('A')
            elif s1 == 'BC':
                A -= 1
                B += 1
                q.append('B')
            else:
                A -= 1
                B += 1
                q.append('B')
        
        elif A > B:
            A -= 1
            B += 1
            q.append('B')
        else:
            A += 1
            B -= 1
            q.append('A')
            
    elif s == 'AC':
        if A ==0 and C==0:
            ans = 'No'
            break
        elif A == C:
            if s1 == 'AB':
                A += 1
                C -= 1
                q.append('A')
            elif s1 == 'BC':
                A -= 1
                C += 1
                q.append('C')
            else:
                A -= 1
                C += 1
                q.append('C')
        elif A > C:
            A -= 1
            C += 1
            q.append('C')
        else:
            A += 1
            C -= 1
            q.append('A')
                    
    elif s == 'BC':
        if C ==0 and B==0:
            ans = 'No'
            break
        elif C == B:
            if s1 == 'AC':
                C += 1
                B -= 1
                q.append('C')
            elif s1 == 'AB':
                C -= 1
                B += 1
                q.append('B')
            else:
                C -= 1
                B += 1
                q.append('B')
        elif C > B:
            C -= 1
            B += 1
            q.append('B')
        else:
            C += 1
            B -= 1
            q.append('C')

            
if ans == 'No':
    print(ans)
else:
    print(ans)
    for key in q:
        print(key)

Submission status: Accepted

Solution code:
n,a,b,c = map(int, input().split())
ans = []
ans_ = "Yes"
ss = [0]
sss = {}
for i in range(n):
  s = str(input())
  ss.append(s)
  sss[i] = s
sss[n] = "a"
for num,s in enumerate(ss):
  if num == 0:
    continue
  else:
    if s == "AB":
      if a == 0 and b == 0:
        ans_ = "No"
        break
      if a > b:
        a -= 1
        b += 1
        ans.append("B")
      elif a < b:
        a += 1
        b -= 1
        ans.append("A")
      else:
        if sss[num] == "BC":
          a -= 1
          b += 1
          ans.append("B")
        else:
          a += 1
          b -= 1
          ans.append("A")
    if s == "BC":
      if c == 0 and b == 0:
        ans_ = "No"
        break
      if c > b:
        c -= 1
        b += 1
        ans.append("B")
      elif c < b:
        c += 1
        b -= 1
        ans.append("C")
      else:
        if sss[num] == "AB":
          c -= 1
          b += 1
          ans.append("B")
        else:
          c += 1
          b -= 1
          ans.append("C") 
    if s == "AC":
      if a == 0 and c == 0:
        ans_ = "No"
        break
      if a > c:
        a -= 1
        c += 1
        ans.append("C")
      elif a < c:
        a += 1
        c -= 1
        ans.append("A")
      else:
        if sss[num] == "BC":
          a -= 1
          c += 1
          ans.append("C")
        else:
          a += 1
          c -= 1
          ans.append("A")
if ans_ == "No":
   print(ans_)
else:
  print(ans_)
  for i in ans:
    print(i)

Submission status: Accepted

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

Submission status: Accepted

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

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

AB, AC, BC = 'AB', 'AC', 'BC'

n0 = [A, B, C].count(0)

F = [A, B, C]
H = ['A', 'B', 'C']
G = {AB: (0, 1), BC:(1,2), AC:(0, 2)}

if n0 == 3:
    print('No')
elif n0 == 2 and sum([A, B, C]) == 1:
    path = []
    for com in S:
        a,b = G[com]
        if F[a] == F[b] == 0:
            print('No')
            exit(0)
        else:
            if F[a] == 1:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
    print('Yes')
    for p in path:
        print(p)
else:
    if (S[0] == AB and A == 0 and B == 0) or \
     (S[0] == AC and A == 0 and C == 0) or (S[0] == BC and B == 0 and C == 0):
        print('No')
        exit(0)

    path = []
    for i,com in enumerate(S):
        a,b = G[com]
        if F[a] == 0 or F[b] == 0:
            if F[b] == 0:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
        else:
            if i < len(S) - 1 and F[a] == F[b]:
                if b in G[S[i + 1]]:
                    a, b = b, a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
            else:
                if F[a] > F[b]:
                    a,b = b,a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
        assert all(v >= 0 for v in F)
    print('Yes')
    for p in path:
        print(p)

Submission status: Accepted

Solution code:
def pattern_100():
    pass


def pattern_200_110():
    pass


n, a, b, c = map(int, input().split())
abc = [a, b, c]
total = sum(abc)
operate_pattern = [[0, 1], [1, 2], [2, 0]]
s = []
for i in range(n):
    tmp = input()
    if tmp == 'AB':
        s.append(0)
    elif tmp == 'AC':
        s.append(2)
    else:
        s.append(1)
answer = []
flag = True
for i in range(n):
    op = operate_pattern[s[i]]
    if abc[op[0]] ==1 and abc[op[1]] ==1:
        if i==n-1:
            answer.append(op[0])
        else:
            if (s[i]+1)%3 ==s[i+1]:
                answer.append(op[1])
                abc[op[1]] +=1
                abc[op[0]] -=1
            else:
                answer.append(op[0])
                abc[op[0]] += 1
                abc[op[1]] -= 1
    elif abc[op[0]] == 0:
        if abc[op[1]] == 0:
            flag =False
            break
        else:
            answer.append(op[0])
            abc[op[0]] +=1
            abc[op[1]] -=1
    elif abc[op[1]] == 0:
        if abc[op[0]] == 0:
            flag =False
            break
        else:
            answer.append(op[1])
            abc[op[1]] +=1
            abc[op[0]] -=1
    else:
        if abc[op[0]] > abc[op[1]]:
            answer.append(op[1])
            abc[op[1]] +=1
            abc[op[0]] -=1
        else:
            answer.append(op[0])
            abc[op[0]] +=1
            abc[op[1]] -=1

if flag:
    print('Yes')
    for i in answer:
        if i == 0:
            print('A')
        elif i == 1:
            print('B')
        else:
            print('C')
else:
    print('No')

Submission status: Accepted

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

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

def add(c, comb):
    global A, B, C
    if comb == 'AB':
        if c == 'A':
            ans.append('A')
            A += 1
            B -= 1
        else:
            ans.append('B')
            A -= 1
            B += 1
    if comb == 'BC':
        if c == 'B':
            ans.append('B')
            B += 1
            C -= 1
        else:
            ans.append('C')
            B -= 1
            C += 1
    if comb == 'AC':
        if c == 'A':
            ans.append('A')
            A += 1
            C -= 1
        else:
            ans.append('C')
            A -= 1
            C += 1

for i in range(N):
    if s[i] == 'AB':
        if A == B == 0:
            print('No')
            exit()
        if i < N - 1 and A == B == 1 and C == 0 and s[i+1] != 'AB':
            if s[i+1] == 'BC':
                add('B', 'AB')
            else:
                add('A', 'AB')
        else:
            if A > B:
                add('B', 'AB')
            else:
                add('A', 'AB')
    if s[i] == 'BC':
        if B == C == 0:
            print('No')
            exit()
        if i < N - 1 and B == C == 1 and A == 0 and s[i+1] != 'BC':
            if s[i+1] == 'AB':
                add('B', 'BC')
            else:
                add('C', 'BC')
        else:
            if B > C:
                add('C', 'BC')
            else:
                add('B', 'BC')
    if s[i] == 'AC':
        if A == C == 0:
            print('No')
            exit()
        if i < N - 1 and A == C == 1 and B == 0 and s[i+1] != 'AC':
            if s[i+1] == 'AB':
                add('A', 'AC')
            else:
                add('C', 'AC')
        else:
            if A > C:
                add('C', 'AC')
            else:
                add('A', 'AC')
print('Yes')
print(*ans, sep='\n')

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
ans = "Yes\n"
def step(add, remove):
    global A
    global B
    global C
    global ans
    ans += add
    ans += "\n"
    if add == "A":
        A += 1
    elif add == "B":
        B += 1
    else:
        C += 1
    if remove == "A":
        A -= 1
    elif remove == "B":
        B -= 1
    else:
        C -= 1

def getvalue(variable):
    global A
    global B
    global C
    if variable == "A":
        return A
    elif variable == "B":
        return B
    else:
        return C

res = ""
for _ in range(N):
    act = input()
    if A == 0 and B == 0 and C == 0:
        ans = "No"
        res = ""
        break
    elif A != 0 and B != 0 and C != 0:
        step(act[0], act[1])
    elif ((A == 0) != (B == 0)) != (C == 0):
        if res == "":
            if getvalue(act[1]) == 0:
                step(act[1], act[0])
            elif getvalue(act[0]) == 0 :
                step(act[0], act[1])
            else:
                res = act
        else:
            if res[0] in act:
                i = (act.index(res[0])+1)%2
                step(res[0], res[1])
                step(act[i], res[0])
                res = ""
            else:
                i = (act.index(res[1])+1)%2
                step(res[1], res[0])
                step(act[i], res[1])
                res = ""
    else:
        if getvalue(act[0]) != 0:
            step(act[1], act[0])
        elif getvalue(act[1]) != 0:
            step(act[0], act[1])
        else:
            ans = "No"
            res = ""
            break
if res != "":
    step(res[0], res[1])
    res = ""
if ans[-1] == "\n":
    ans = ans[:-1]
print(ans)

Submission status: Accepted

Solution code:
def task(n, a, b, c, s):
    choices = []

    for i in range(0, n):
        seta = 'A' in s[i]
        setb = 'B' in s[i]
        setc = 'C' in s[i]

        picka = seta
        pickb = setb
        pickc = setc

        if seta and a - 1 < 0:
            pickb = False
            pickc = False

        if setb and b - 1 < 0:
            picka = False
            pickc = False

        if setc and c - 1 < 0:
            picka = False
            pickb = False

        nextseta = True
        nextsetb = True
        nextsetc = True

        if i < n - 1:
            nextseta = 'A' in s[i + 1]
            nextsetb = 'B' in s[i + 1]
            nextsetc = 'C' in s[i + 1]

        if picka and nextseta:
            pickb = False
            pickc = False
        elif pickb and nextsetb:
            picka = False
            pickc = False
        elif pickc and nextsetc:
            picka = False
            pickb = False

        if seta and picka:
            choices.append('A')
            a += 1
            if setb:
                b -= 1
            if setc:
                c -= 1
        elif setb and pickb:
            choices.append('B')
            b += 1
            if seta:
                a -= 1
            if setc:
                c -= 1
        elif setc and pickc:
            choices.append('C')
            c += 1
            if seta:
                a -= 1
            if setb:
                b -= 1
        else:
            print('No')
            return

    print('Yes')
    for choice in choices:
        print(choice)


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

task(n, a, b, c, s)

Submission status: Accepted

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

Submission status: Accepted

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

ans = []
for i in range(n):
    command = s[i]
    if command == 'AB':
        if a < b:
            ans.append('A')
            a += 1
            b -= 1
        elif a*b == 1 and i != n-1:
            c_tmp = s[i+1]
            if c_tmp == 'AC':
                ans.append('A')
                a += 1
                b -= 1
            else:
                ans.append('B')
                b += 1
                a -= 1
        else:
            ans.append('B')
            b += 1
            a -= 1

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun May  3 22:01:32 2020
"""

import sys
#import numpy as np

sys.setrecursionlimit(10 ** 9)
#def input():
#    return sys.stdin.readline()[:-1]
mod = 10**9+7

#N = int(input())
N,A,B,C = map(int,input().split())
s = []
for i in range(N):
    s.append(input())

ans = ['Yes']
No = 'No'
ab,ac,bc = 'AB', 'AC', 'BC'
a,b,c = 'A', 'B', 'C'

for i in range(N):
    if s[i] == ab:
        flg = True
        if A == 1 and B ==1:
            if i == N -1:
                flg = True
            elif s[i+1] == ac:
                flg = True
            else:
                flg = False
        elif A <= B:
            flg = True
        elif A > B:
            flg = False
        if flg:
            A += 1
            B -= 1
            ans.append(a)
        else:
            A -= 1
            B += 1
            ans.append(b)

    elif s[i] == ac:
        flg = True
        if A == 1 and C ==1:
            if i == N -1:
                flg = True
            elif s[i+1] == ab:
                flg = True
            else:
                flg = False
        elif A <= C:
            flg = True
        elif A > C:
            flg = False
        if flg:
            A += 1
            C -= 1
            ans.append(a)
        else:
            A -= 1
            C += 1
            ans.append(c)

    elif s[i] == bc:
        flg = True
        if B == 1 and C ==1:
            if i == N -1:
                flg = True
            elif s[i+1] == ab:
                flg = True
            else:
                flg = False
        elif B <= C:
            flg = True
        elif B > C:
            flg = False
        if flg:
            B += 1
            C -= 1
            ans.append(b)
        else:
            B -= 1
            C += 1
            ans.append(c)
    if A < 0 or B < 0 or C < 0:
        print(No)
        sys.exit()

for a in ans:
    print(a)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,A,B,C=MI()
    D = { "A":A,"B":B,"C":C }
    ans = []
    S=[ST() for _ in range(N)]
    for i,s in enumerate(S):
        if(D[s[0]]==D[s[1]]==0):
            print("No")
            return
        if(D[s[0]]<D[s[1]]):
            ans.append(s[0])
            D[s[0]]+=1
            D[s[1]]-=1
        elif(D[s[0]]>D[s[1]] or i==N-1):
            ans.append(s[1])
            D[s[0]]-=1
            D[s[1]]+=1
        else:
            ns = S[i+1]
            if(s[0] in ns):
                ans.append(s[0])
                D[s[0]]+=1
                D[s[1]]-=1
            else:
                ans.append(s[1])
                D[s[0]]-=1
                D[s[1]]+=1
        # print(D)

    print("Yes")
    print(*ans,sep="\n")
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10**6)

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

dx = {"AB":0,"BC":1,"AC":2}
ans = ["Yes"]

def dfs(i):
    if i==n:
        print(*ans,sep="\n")
        exit()
    x = dx[s[i]]
    y = (x+1)%3
    for j in [x,y]:
        if a[j]:
            ans.append("ABC"[x+y-j])
            a[j] -= 1
            a[x+y-j] += 1
            dfs(i+1)
            ans.pop()    
            a[j] += 1
            a[x+y-j] -= 1

dfs(0)
print("No")

Submission status: Accepted

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

keep = False

answer = []

miss = False

for i in range(n):
    s = input()

    if keep:
        if kept == "AB":
            if s == "AB":
                a += 1
                b -= 1
                answer.append('A')
            elif s == "AC":
                a += 1
                b -= 1
                answer.append('A')
            elif s == "BC":
                a -= 1
                b += 1
                answer.append('B')
        if kept == "AC":
            if s == "AB":
                a += 1
                c -= 1
                answer.append('A')
            elif s == "AC":
                a += 1
                c -= 1
                answer.append('A')
            elif s == "BC":
                a -= 1
                c += 1
                answer.append('C')
        if kept == "BC":
            if s == "AB":
                b += 1
                c -= 1
                answer.append('B')
            elif s == "AC":
                b -= 1
                c += 1
                answer.append('C')
            elif s == "BC":
                b += 1
                c -= 1
                answer.append('B')

    keep = False

    if s == "AB":
        if a == b:
            if a == 1:
                keep = True
                kept = "AB"
            else:
                a += 1
                b -= 1
                answer.append('A')
        else:
            if a > b:
                a -= 1
                b += 1
                answer.append('B')
            else:
                a += 1
                b -= 1
                answer.append('A')

    elif s == "AC":
        if a == c:
            if a == 1:
                keep = True
                kept = "AC"
            else:
                a += 1
                c -= 1
                answer.append('A')
        else:
            if a > c:
                a -= 1
                c += 1
                answer.append('C')
            else:
                a += 1
                c -= 1
                answer.append('A')
    elif s == "BC":
        if b == c:
            if b == 1:
                keep = True
                kept = "BC"
            else:
                b += 1
                c -= 1
                answer.append('B')
        else:
            if b > c:
                b -= 1
                c += 1
                answer.append('C')
            else:
                b += 1
                c -= 1
                answer.append('B')

    if a < 0 or b < 0 or c < 0:
        miss = True
        break

    if keep and i == (n-1):
        if kept == "AB":
            answer.append('A')
        elif kept == "AC":
            answer.append('A')
        elif kept == "BC":
            answer.append('B')

if miss:
    print("No")
else:
    print("Yes")
    for ans in answer:
        print(ans)

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
s = [input() for i in range(n)]+[0]
# if [a,b,c].count(0) >= 2:
#     print("No")
ans = ""

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