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:
n,a,b,c = map(int,input().split())
s_list = []
for i in range(n):
    s = input()
    s_list.append(s)

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

move = []
if a+b+c == 1:
    for i in range(n):
        if s_list[i] == "AB":
            if a == 0:
                a+=1
                b-=1
                move.append("A")
            else:
                b+=1
                a-=1
                move.append("B")
        if s_list[i] == "BC":
            if b == 0:
                b+=1
                c-=1
                move.append("B")
            else:
                c+=1
                b-=1
                move.append("C")
        if s_list[i] == "AC":
            if a == 0:
                a+=1
                c-=1
                move.append("A")
            else:
                c+=1
                a-=1
                move.append("C")
        if min(a,b,c) < 0:
            print("No")
            exit()

    print("Yes")
    for i in move:
        print(i)            
    
else:
    for i in range(n):
        if i < n-1:
            s_next = s_list[i+1]
        if s_list[i] == "AB":
            if a == 1 and b == 1 and s_next != "AB":
                if s_next == "BC":
                    b+=1
                    a-=1
                    move.append("B")
                else:
                    a+=1
                    b-=1
                    move.append("A")
            else:
                if a < b:
                    a+=1
                    b-=1
                    move.append("A")
                else:
                    b+=1
                    a-=1
                    move.append("B")
        if s_list[i] == "BC":
            if b == 1 and c == 1 and s_next != "BC":
                if s_next == "AB":
                    b+=1
                    c-=1
                    move.append("B")
                else:
                    c+=1
                    b-=1
                    move.append("C")
            else:
                if b < c:
                    b+=1
                    c-=1
                    move.append("B")
                else:
                    c+=1
                    b-=1
                    move.append("C")
        if s_list[i] == "AC":
            if a == 1 and c == 1 and s_next != "AC":
                if s_next == "AB":
                    a+=1
                    c-=1
                    move.append("A")
                else:
                    c+=1
                    a-=1
                    move.append("C")
            else:
                if a < c:
                    a+=1
                    c-=1
                    move.append("A")
                else:
                    c+=1
                    a-=1
                    move.append("C") 

        if min(a,b,c) < 0:
            print("No")
            exit()
    print("Yes")
    for i in move:
        print(i)

Submission status: Accepted

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

if sum(d.values()) == 0:
    print('No')
elif sum(d.values()) == 1:
    ans = []
    for s in S:
        if d[s[0]] == d[s[1]] == 0:
            print('No')
            exit()
        elif d[s[0]] > d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
    print('Yes')
    print(*ans, sep='\n')
else:
    ans = []
    for i, s in enumerate(S):
        if d[s[0]] == d[s[1]] == 0:
            print('No')
            exit()
        elif d[s[0]] > d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        elif d[s[0]] < d[s[1]]:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
        elif d[s[0]] == d[s[1]] == 1 and i != N-1 and S[i] != S[i+1]:
            if s[0] in S[i+1]:
                d[s[0]] += 1
                d[s[1]] -= 1
                ans.append(s[0])
            else:
                d[s[0]] -= 1
                d[s[1]] += 1
                ans.append(s[1])
        else:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
    print('Yes')
    print(*ans, sep='\n')

Submission status: Accepted

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

if sum(d.values()) == 0:
    print('No')
elif sum(d.values()) == 1:
    ans = []
    for s in S:
        if d[s[0]] == d[s[1]] == 0:
            print('No')
            exit()
        elif d[s[0]] > d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        else:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
    print('Yes')
    print(*ans, sep='\n')
else:
    ans = []
    for i, s in enumerate(S):
        if d[s[0]] == d[s[1]] == 0:
            print('No')
            exit()
        elif d[s[0]] > d[s[1]]:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
        elif d[s[0]] < d[s[1]]:
            d[s[0]] += 1
            d[s[1]] -= 1
            ans.append(s[0])
        elif d[s[0]] == d[s[1]] == 1 and i != N-1 and S[i] != S[i+1]:
            # elif sum(d.values()) == 2 and d[s[0]] == d[s[1]] == 1 and i != N-1 and S[i] != S[i+1]:
            if s[0] in S[i+1]:
                d[s[0]] += 1
                d[s[1]] -= 1
                ans.append(s[0])
            else:
                d[s[0]] -= 1
                d[s[1]] += 1
                ans.append(s[1])
        else:
            d[s[0]] -= 1
            d[s[1]] += 1
            ans.append(s[1])
    print('Yes')
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:

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

x = [A, B, C]
d = {"A": 0, "B": 1, "C": 2}
if sum(x) == 0:
    print("No")
else:
    flag = True
    ans = []
    for i, s in enumerate(S):
        n, m = list(s)

        if x[d[n]] == x[d[m]] == 0:
            flag = False
            break

        if x[d[n]] > x[d[m]]:
            x[d[n]] -= 1
            x[d[m]] += 1
            ans.append(m)
        elif x[d[n]] < x[d[m]]:
            x[d[n]] += 1
            x[d[m]] -= 1
            ans.append(n)
        elif i < N - 1 and S[i] != S[i + 1]:
            # x[d[n]] == x[d[m]]
            if n in S[i + 1]:
                x[d[n]] += 1
                x[d[m]] -= 1
                ans.append(n)
            else:
                x[d[m]] += 1
                x[d[n]] -= 1
                ans.append(m)
        else:
            # x[d[n]] == x[d[m]] at last
            x[d[n]] += 1
            x[d[m]] -= 1
            ans.append(n)

    if flag:
        print("Yes")
        print(*ans, sep="\n")
    else:
        print("No")

Submission status: Accepted

Solution code:
def main():
    import sys

    def input(): return sys.stdin.readline().rstrip()

    def stoidx(s):
        if s == 'AB':
            return (0, 1)
        elif s == 'BC':
            return (1, 2)
        else:
            return (0, 2)


    def answer(ans):
        print('Yes')
        for a in ans:
            print(a)

    def add_answer(x):
        if s[x[0]] > s[x[1]]:
            s[x[0]] -= 1
            s[x[1]] += 1
            ans.append(abc[x[1]])
        else:
            s[x[0]] += 1
            s[x[1]] -= 1
            ans.append(abc[x[0]])
        
    n, *s = map(int, input().split())
    sl = []
    for _ in range(n):
        sl.append(stoidx(input()))
    abc = 'ABC'
    ans = []
    if sum(s) == 0:
        print('No')
        return

    elif sum(s) == 1:
        for x in sl:
            if s[x[0]] == 0 and s[x[1]] == 0:
                print('No')
                return
            add_answer(x)

    else:
        for i, x in enumerate(sl):
            if s[x[0]] == 0 and s[x[1]] == 0:
                print('No')
                return
                
            if i < n-1 and s[x[0]] == 1 and s[x[1]] == 1:
                nx = sl[i+1]
                if x != nx and sum(s)==2:
                    if x[0] in nx:
                        s[x[0]] += 1
                        s[x[1]] -= 1
                        ans.append(abc[x[0]])
                    else:
                        s[x[0]] -= 1
                        s[x[1]] += 1
                        ans.append(abc[x[1]])
                    continue
            add_answer(x)
    answer(ans)


if __name__ == '__main__':
    main()

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:
    d=[]
    for i in range(n):
        s=input()
        if s=="AB":
            if a+b==0:
                print("No")
                exit()
            else:
                if a==0:
                    d.append("A")
                else:
                    d.append("B")
                a,b=b,a
        if s=="BC":
            if c+b==0:
                print("No")
                exit()
            else:
                if b==0:
                    d.append("B")
                else:
                    d.append("C")
                c,b=b,c
        if s=="AC":
            if a+c==0:
                print("No")
                exit()
            else:
                if a==0:
                    d.append("A")
                else:
                    d.append("C")
                a,c=c,a 
    print("Yes")
    for i in d:
        print(i)
else:
    d=[]
    s=[input()for i in range(n)]+["AB"]
    for i in range(n):
        if s[i]=="AB":
            if a+b==0:
                print("No")
                exit()
            if a+b+c==2 and a==1 and b==1 and s[i+1]!=s[i]:
                if s[i+1]=="AC":
                    d.append("A")
                    a+=1
                    b-=1
                else:
                    d.append("B")
                    b+=1
                    a-=1
            elif a<b:
                d.append("A")
                a+=1
                b-=1
            else:
                d.append("B")
                b+=1
                a-=1
        elif s[i]=="BC":
            if c+b==0:
                print("No")
                exit()
            if a+b+c==2 and c==1 and b==1 and s[i+1]!=s[i]:
                if s[i+1]=="AB":
                    d.append("B")
                    c-=1
                    b+=1
                else:
                    d.append("C")
                    c+=1
                    b-=1
            elif b<c:
                d.append("B")
                b+=1
                c-=1
            else:
                d.append("C")
                c+=1
                b-=1
        elif s[i]=="AC":
            if c+a==0:
                print("No")
                exit()
            if a+b+c==2 and c==1and a==1 and s[i+1]!=s[i]:
                if s[i+1]=="AB":
                    d.append("A")
                    a+=1
                    c-=1
                else:
                    d.append("C")
                    c+=1
                    a-=1
            elif a<c:
                d.append("A")
                a+=1
                c-=1
            else:
                d.append("C")
                c+=1
                a-=1
    print("Yes")
    for i in d:
        print(i)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
dt = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
Q = [dt[input()] for _ in range(N)]


class Game:
    def __init__(self, ABC):
        self.ABC = ABC
        self.ds = {0: 'A', 1: 'B', 2: 'C'}
        self.ans = []

    def add_q0(self, q0, q1):
        self.ans.append(self.ds[q0])
        self.ABC[q0] += 1
        self.ABC[q1] -= 1

    def add_q1(self, q0, q1):
        self.ans.append(self.ds[q1])
        self.ABC[q0] -= 1
        self.ABC[q1] += 1


def solve1(ABC):
    G = Game(ABC)
    for q0, q1 in Q:
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        else:
            G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


def solve2(ABC):
    G = Game(ABC)
    for i, (q0, q1) in enumerate(Q):
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        elif G.ABC[q1] == 0:
            G.add_q1(q0, q1)
        elif G.ABC[q0] == G.ABC[q1] == 1 and sum(ABC) == 2 and i < N - 1:
            if q0 in Q[i + 1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
        else:
            # 余裕の無い方に足しておく
            if G.ABC[q0] <= G.ABC[q1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


S = A + B + C
if S == 0:
    print('No')
elif S == 1:
    solve1([A, B, C])
else:
    solve2([A, B, C])

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
dt = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
Q = [dt[input()] for _ in range(N)]


class Game:
    def __init__(self, ABC):
        self.ABC = ABC
        self.ds = {0: 'A', 1: 'B', 2: 'C'}
        self.ans = []

    def add_q0(self, q0, q1):
        self.ans.append(self.ds[q0])
        self.ABC[q0] += 1
        self.ABC[q1] -= 1

    def add_q1(self, q0, q1):
        self.ans.append(self.ds[q1])
        self.ABC[q0] -= 1
        self.ABC[q1] += 1


def solve1(ABC):
    G = Game(ABC)
    for q0, q1 in Q:
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        else:
            G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


def solve2(ABC):
    G = Game(ABC)
    for i, (q0, q1) in enumerate(Q):
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        elif G.ABC[q1] == 0:
            G.add_q1(q0, q1)
        elif G.ABC[q0] == G.ABC[q1] == 1 and i < N - 1:
            if q0 in Q[i + 1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
        else:
            # 余裕の無い方に足しておく
            if G.ABC[q0] <= G.ABC[q1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


def solve3(ABC):
    G = Game(ABC)
    for q0, q1 in Q:
        if G.ABC[q0] == G.ABC[q1] == 0:
            print('No')
            exit()
        elif G.ABC[q0] == 0:
            G.add_q0(q0, q1)
        elif G.ABC[q1] == 0:
            G.add_q1(q0, q1)
        else:
            # 余裕の無い方に足しておく
            if G.ABC[q0] <= G.ABC[q1]:
                G.add_q0(q0, q1)
            else:
                G.add_q1(q0, q1)
    print('Yes')
    print(*G.ans, sep='\n')


S = A + B + C
if S == 0:
    print('No')
elif S == 1:
    solve1([A, B, C])
elif S == 2:
    solve2([A, B, C])
else:
    solve3([A, B, C])

Submission status: Accepted

Solution code:
N,*X=map(int,input().split())
S=[input() for i in range(N)]
f=1
d={'AB':(0,1),'AC':(0,2),'BC':(1,2)}
x='ABC'
a=[]
for i in range(N):
    if X[d[S[i]][0]]==1 and X[d[S[i]][1]]==1 and X.count(0)==1 and i<N-1:
        if S[i][0] in S[i+1]:
            X[d[S[i]][0]]+=1
            X[d[S[i]][1]]-=1
            a.append(x[d[S[i]][0]])
        else:
            X[d[S[i]][0]]-=1
            X[d[S[i]][1]]+=1
            a.append(x[d[S[i]][1]])
    elif X[d[S[i]][0]]<X[d[S[i]][1]]:
        X[d[S[i]][0]]+=1
        X[d[S[i]][1]]-=1
        a.append(x[d[S[i]][0]])
    else:
        X[d[S[i]][0]]-=1
        X[d[S[i]][1]]+=1
        a.append(x[d[S[i]][1]])
    if -1 in X:
        f=0;break
print(['No','Yes'][f])
if f:print(*a,sep='\n')

Submission status: Accepted

Solution code:

n,a,b,c = list(map(int, input().split()))
s = [[0]*2 for _ in range(n)]
S = []
for i in range(n):
  X = input()
  S += X
  cnt = 0
  for x in X:
    if x == 'A':
      s[i][cnt] = 0
    elif x == 'B':
      s[i][cnt] = 1
    else:
      s[i][cnt] = 2
    cnt += 1

if (a + b + c) == 0:exit(print('No'))
abc = [a,b,c]
ABC = ['A','B','C']


ans = [0] * n

def zero(xy):
  x = xy[0]
  y = xy[1]
  if abc[x] == 0 and abc[y] == 0:
    return -1,-1
  elif abc[x] == 0:
    return x,y
  elif abc[y] == 0:
    return y,x
  else:
    return 3,3

def main():
  global ans
  global abc
  
  for i in range(n):
    x,y = zero(s[i])
    if x == -1:exit(print('No'))
    if x == 3:
      if i < n-1:
        if s[i][1] in s[i+1]:
          x = s[i][1]
          y = s[i][0]
        else:
          x = s[i][0]
          y = s[i][1]
      else:
        x = s[i][0]
        y = s[i][1]
    ans[i] = x
    abc[x] += 1
    abc[y] -= 1

main()
print('Yes')
for i in range(n):
  print(ABC[ans[i]])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys 
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
Q, A, B, C = map(int, readline().split())
N = A + B + C
SS = []
for _ in range(Q):
    S = readline().decode().rstrip()
    a = 0 if S == 'AB' else 1 if S == 'AC' else 2 
    SS.append(a)
ans = [] 
if N == 0:
    print('No')
    sys.exit()
elif N == 1:
    nums = [A,B,C]
    for n in range(Q):
        num = SS[n]
        i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)  
        if nums[i] == nums[j]:
            print('No')
            sys.exit()
        if nums[i] > nums[j]:
            i,j = j,i
        nums[i] += 1
        nums[j] -= 1
        ans.append(i)
else:
    nums = [A,B,C]
    num = SS[0]
    i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2) 
    if nums[i] == 0 and nums[j] == 0:
        print('No')
        sys.exit()
    if N == 2:
        for n in range(Q):
            num = SS[n]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] == 1 and nums[j] == 1 and n < Q-1:
                if SS[n] != SS[n+1]:
                    ni,nj = (0,1) if SS[n+1] == 0 else (0,2) if SS[n+1] == 1 else (1,2)
                    a = Counter([i,j,ni,nj])
                    b = a.most_common()[0][0] ## 2つあるの探す 
                    if j == b:
                        i,j = j,i   
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
    else:
        for n in range(Q):
            num = SS[n]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
        
print('Yes')
ABC = 'ABC'
answer = list(ABC[i] for i in ans)
print('\n'.join(answer))

Submission status: Accepted

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

if A+B+C == 0:
    print('No')
    exit()

D = []

if A+B+C == 1:
    for i in range(N):
        if (A == 1 and s[i] == 'BC') or (B == 1 and s[i] == 'AC') or (C == 1 and s[i] == 'AB'):
            print('No')
            exit()
        else:
            if A == 1:
                if s[i] == 'AB':
                    A = 0
                    B = 1
                    D.append('B')
                else:
                    A = 0
                    C = 1
                    D.append('C')
            elif B == 1:
                if s[i] == 'AB':
                    B = 0
                    A = 1
                    D.append('A')
                else:
                    B = 0
                    C = 1
                    D.append('C')
            else:
                if s[i] == 'BC':
                    C = 0
                    B = 1
                    D.append('B')
                else:
                    C = 0
                    A = 1
                    D.append('A')
    print('Yes')
    print(*D,sep='\n')
    exit()

if (B == 0 and C == 0 and s[0] == 'BC') or (A == 0 and C == 0 and s[0] == 'AC') or (A == 0 and B == 0 and s[0] == 'AB'):
    print('No')
    exit()

print('Yes')

for i in range(N):
    if A+B+C > 2:
        if A == 0 and s[i] == 'AB':
            D.append('A')
            A = 1
            B -= 1
        elif A == 0 and s[i] == 'AC':
            D.append('A')
            A = 1
            C -= 1
        elif B == 0 and s[i] == 'AB':
            D.append('B')
            B = 1
            A -= 1
        elif B == 0 and s[i] == 'BC':
            D.append('B')
            B = 1
            C -= 1
        elif C == 0 and s[i] == 'AC':
            D.append('C')
            C = 1
            A -= 1
        elif C == 0 and s[i] == 'BC':
            D.append('C')
            C = 1
            B -= 1
        elif A == 0 and B == 1 and s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
        elif A == 0 and C == 1 and s[i] == 'BC':
            D.append('C')
            C += 1
            B -= 1
        elif B == 0 and A == 1 and s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif B == 0 and C == 1 and s[i] == 'AC':
            D.append('C')
            C += 1
            A -= 1
        elif C == 0 and A == 1 and s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif C == 0 and B == 1 and s[i] == 'AB':
            D.append('B')
            B += 1
            A -= 1
        elif s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
    else:  # A+B+C == 2 の場合
        if A == 1 and B == 1 and s[i] == 'AB':
            if i <= N-2 and s[i+1] == 'BC':
                D.append('B')
                A -= 1
                B += 1
            else:
                D.append('A')
                A += 1
                B -= 1
        elif A == 1 and C == 1 and s[i] == 'AC':
            if i <= N-2 and s[i+1] == 'AB':
                D.append('A')
                A += 1
                C -= 1
            else:
                D.append('C')
                A -= 1
                C += 1
        elif B == 1 and C == 1 and s[i] == 'BC':
            if i <= N-2 and s[i+1] == 'AB':
                D.append('B')
                B += 1
                C -= 1
            else:
                D.append('C')
                B -= 1
                C += 1
        elif A == 0 and s[i] == 'AB':
            D.append('A')
            A = 1
            B -= 1
        elif A == 0 and s[i] == 'AC':
            D.append('A')
            A = 1
            C -= 1
        elif B == 0 and s[i] == 'AB':
            D.append('B')
            B = 1
            A -= 1
        elif B == 0 and s[i] == 'BC':
            D.append('B')
            B = 1
            C -= 1
        elif C == 0 and s[i] == 'AC':
            D.append('C')
            C = 1
            A -= 1
        elif C == 0 and s[i] == 'BC':
            D.append('C')
            C = 1
            B -= 1
        elif s[i] == 'AB':
            D.append('A')
            A += 1
            B -= 1
        elif s[i] == 'AC':
            D.append('A')
            A += 1
            C -= 1
        elif s[i] == 'BC':
            D.append('B')
            B += 1
            C -= 1
print(*D,sep='\n')

Submission status: Accepted

Solution code:
def main():
    N, A, B, C = list(map(int, input().split(' ')))
    S = [input() for _ in range(N)]
    D = {
        'AB': A + B,
        'BC': B + C,
        'AC': A + C
    }
    ans = list()
    for i, s in enumerate(S):
        if D[s] == 0:
            print('No')
            return
        if s == 'AB':
            if (D['BC'] < D['AC']) or \
                (i < N - 1 and S[i + 1] == 'BC' and D['BC'] == D['AC'] == 1):
                ans.append('B')
                D['BC'] += 1
                D['AC'] -= 1
            else:
                ans.append('A')
                D['BC'] -= 1
                D['AC'] += 1
        elif s == 'BC':
            if (D['AB'] < D['AC']) or \
                (i < N - 1 and S[i + 1] == 'AB' and D['AB'] == D['AC'] == 1):
                ans.append('B')
                D['AB'] += 1
                D['AC'] -= 1
            else:
                ans.append('C')
                D['AB'] -= 1
                D['AC'] += 1
        else:  # s == 'AC'
            if (D['AB'] < D['BC']) or \
                (i < N - 1 and S[i + 1] == 'AB' and D['AB'] == D['BC'] == 1):
                ans.append('A')
                D['AB'] += 1
                D['BC'] -= 1
            else:
                ans.append('C')
                D['AB'] -= 1
                D['BC'] += 1
    print('Yes')
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#import numpy as np
#import math
#from decimal import *
#from numba import njit

#@njit
def main():
    N,A,B,C = map(int, input().split())
    array = [A,B,C]
    string = ['A','B','C']
    si = [] # AB:0,BC:1,AC:2
    if A+B+C <= 0:
        print('No')
        return
    for _ in range(N):
        s = input()
        si += (0 if s == 'AB' else 1 if s == 'BC' else 2),

    ans = []
    for i in range(len(si)):
        s = si[i]
        index = -3 if s == 0 else -2 if s == 1 else -1
        if array[index] == 0:
            array[index] += 1
            array[index+1] -= 1
            ans += string[index],
        elif array[index+1] == 0:
            array[index] -= 1
            array[index+1] += 1
            ans += string[index+1],
        elif array[index] == 1 and array[index+1] == 1:
            if i+1 >= len(si):
                array[index] += 1
                array[index+1] -= 1
                ans += string[index],
            else:
                if si[i+1] != (index+1)%3:
                    array[index] += 1
                    array[index+1] -= 1
                    ans += string[index],
                else:
                    array[index] -= 1
                    array[index+1] += 1
                    ans += string[index+1],
        elif array[index] == 1:
            array[index] += 1
            array[index+1] -= 1
            ans += string[index],
        else:
            array[index] -= 1
            array[index+1] += 1
            ans += string[index+1],

        if array[0] < 0 or array[1] < 0 or array[2] < 0:
            print('No')
            return
        
    print('Yes')
    for s in ans:
        print(s)

main()

Submission status: Accepted

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

if __name__ == "__main__":
    main()