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())
ans_list= []
chk = 0
count = 1
for i in range(N):
    s = input()
    if chk == 1:
        if s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A -= 1
                    B += 1
                    ans_list.append(["B"])
                else:
                    A += 1
                    B -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 2:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                else:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "BC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    A -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    A += 1
                    C -= 1
                    ans_list.append(["A"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    elif chk == 3:
        if s == "AB":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                else:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                num -= 1
            chk = 0
            count = 1
        elif s == "AC":
            num = count
            for j in reversed(range(count)):
                if num%2 == 1:
                    B -= 1
                    C += 1
                    ans_list.append(["C"])
                else:
                    B += 1
                    C -= 1
                    ans_list.append(["B"])
                num -= 1
            chk = 0
            count = 1
        else:
            count += 1
            continue
    if s == "AB" and A == 0 and B == 0:
        print("No")
        break
    elif s == "BC" and B == 0 and C == 0:
        print("No")
        break
    elif s == "AC" and A == 0 and C == 0:
        print("No")
        break
    else:
        if s == "AB":
            if A == 0:
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 0:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B != 1 :
                A += 1
                B -= 1
                ans_list.append(["A"])
            elif B == 1 and A != 1:
                A -= 1
                B += 1
                ans_list.append(["B"])
            elif A == 1 and B == 1:
                chk = 1
            else:
                A += 1
                B -= 1
                ans_list.append(["A"])
        elif s == "AC":
            if A == 0:
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 0:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and C != 1 :
                A += 1
                C -= 1
                ans_list.append(["A"])
            elif C == 1 and A != 1:
                A -= 1
                C += 1
                ans_list.append(["C"])
            elif A == 1 and C == 1:
                chk = 2
            else:
                A += 1
                C -= 1
                ans_list.append(["A"])
        else:
            if B == 0:
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 0:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C != 1 :
                B += 1
                C -= 1
                ans_list.append(["B"])
            elif C == 1 and B != 1:
                B -= 1
                C += 1
                ans_list.append(["C"])
            elif B == 1 and C == 1:
                chk = 3
            else:
                B += 1
                C -= 1
                ans_list.append(["B"])
if chk == 1:
    for i in range(count):
        if i%2 == 0:
            A += 1
            B -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            B += 1
            ans_list.append(["B"])
elif chk == 2:
    for i in range(count):
        if i%2 == 0:
            A += 1
            C -= 1
            ans_list.append(["A"])
        else:
            A -= 1
            C += 1
            ans_list.append(["C"])
elif chk == 3:
    for i in range(count):
        if i%2 == 0:
            B += 1
            C -= 1
            ans_list.append(["B"])
        else:
            B -= 1
            C += 1
            ans_list.append(["C"])
if len(ans_list) == N:
    print("Yes")
    for i in range(N):
        print(*ans_list[i])

Submission status: Accepted

Solution code:
import bisect
import collections
import sys

sys.setrecursionlimit(100000)
input = sys.stdin.readline
ACMOD = 1000000007
INF = 1 << 62


def lmi():
    return list(map(int, input().split()))


def llmi(n):
    return [lmi() for _ in range(n)]


N, A, B, C = lmi()
S = [input().strip() for _ in range(N)]

a = A
b = B
total = A + B + C
ans = []
if total != 2:
    for s in S:
        c = total - a - b
        if s == "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 == "AC":
            if a == 0 and c == 0:
                print("No")
                exit()
            if a > c:
                a -= 1
                ans.append('C')
            else:
                a += 1
                ans.append('A')
        if s == "BC":
            if b == 0 and c == 0:
                print("No")
                exit()
            if b > c:
                b -= 1
                ans.append('C')
            else:
                b += 1
                ans.append('B')
        # print(a,b,c)

    # print(ans, a, b, c)
    print('\n'.join(['Yes'] + ans))
    exit()

case = {(A, B, C): []}  # 1,1 1,0 0,1 0,0 2,0 0,2
ans = []

for i, s in enumerate(S):
    # while True:
    #     s = input().strip()
    new_case = {}
    for (a, b, c), v in case.items():
        if s == "AB":
            if a == 0 and b == 0:
                continue
            elif a == 1 and b == 1:
                new_case[(0, 2, 0)] = list(v) + ['B']
                v.append('A')
                new_case[(2, 0, 0)] = v
                # two case
                pass
            else:
                v.append('A' if b else 'B')
                new_case[(abs(a - 1), abs(b - 1), c)] = v
        if s == "AC":
            if a == 0 and c == 0:
                continue
            if a == c:
                new_case[(2, 0, 0)] = list(v) + ['A']
                v.append('C')
                new_case[(0, 0, 2)] = v

                # two case
                pass
            else:
                v.append('C' if a else 'A')
                new_case[(abs(a - 1), b, abs(c - 1))] = v

        if s == "BC":
            if b == 0 and c == 0:
                continue
            elif b == c:
                new_case[(0, 2, 0)] = list(v) + ['B']
                v.append('C')
                new_case[(0, 0, 2)] = v
            else:
                v.append('B' if c else 'C')
                new_case[(a, abs(b - 1), abs(c - 1))] = v

    if not new_case:
        print("No")
        exit()
    case = new_case
    if len(case) == 1:
        key = list(case.keys())[0]
        ans += case[key]
        case[key] = []

print('\n'.join(['Yes'] + ans + list(case.values())[0]))

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
opts = []
for _ in range(N):
    opt = input()
    opts.append((ord(opt[0]) - ord("A"), ord(opt[1]) - ord("A")))

choices = []
nums = [A, B, C]
for i, opt in enumerate(opts):
    if nums[opt[0]] == 0 and nums[opt[1]] == 0:
        print("No")
        break
    if nums[opt[0]] == 1 and nums[opt[1]] == 1:
        if i < N - 1 and opt != opts[i + 1]:
            choice = (set(opt) & set(opts[i + 1])).pop()
            nums[choice] += 1
            nums[choice ^ opt[0] ^ opt[1]] -= 1
            choices.append(choice)
            continue
    if nums[opt[0]] >= nums[opt[1]]:
        choices.append(opt[1])
        nums[opt[1]] += 1
        nums[opt[0]] -= 1
    else:
        choices.append(opt[0])
        nums[opt[0]] += 1
        nums[opt[1]] -= 1
else:
    print("Yes")
    for c in choices:
        print(chr(c + ord("A")))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N,A,B,C = map(int, input().split())
list_choice = [input() for i in range(N)]
list_choice.append('')

def operation(A,B,C,S,nextS):
  if S=='AB':
    if max(A,B)==0:
      return ('No','',0,0,0)
    elif (A<B) or (A==1 and B==1 and nextS=='AC'):
      return ('Yes','A',A+1,B-1,C)
    else:
      return ('Yes','B',A-1,B+1,C)
  elif S=='AC':
    if max(A,C)==0:
      return ('No','',0,0,0)
    elif (A<C) or (A==1 and C==1 and nextS=='AB'):
      return ('Yes','A',A+1,B,C-1)
    else:
      return ('Yes','C',A-1,B,C+1)
  else: # S=='BC'
    if max(B,C)==0:
      return ('No','',0,0,0)
    elif (B<C) or (B==1 and C==1 and nextS=='AB'):
      return ('Yes','B',A,B+1,C-1)
    else:
      return ('Yes','C',A,B-1,C+1)
  
def func(A,B,C,list_choice):
  list_add = []
  for i in range(N):
    answer,add,A,B,C = operation(A,B,C,list_choice[i],list_choice[i+1])
    if answer=='No':
      return ('No', [])
    else:
      list_add.append(add)
  return ('Yes',list_add)
  
answer, list_add = func(A,B,C,list_choice)
print(answer)
if answer == 'Yes':
  for add in list_add:
    print(add)

Submission status: Accepted

Solution code:
n, *x = map(int, input().split())
k = 'ABC'
ans = []
s = [input() for _ in range(n)]
for p in range(n):
    sp = s[p]
    if sp == 'AB':
        i = 0
        j = 1
    elif sp == 'BC':
        i = 1
        j = 2
    else:
        i = 0
        j = 2
    if x[i] == x[j] == 0:
        print('No')
        break
    if x[i] == x[j] and p < n - 1:
        sp1 = s[p + 1]
        if k[i] in sp1:
            x[i] += 1
            x[j] -= 1
            ans.append(k[i])
        else:
            x[i] -= 1
            x[j] += 1
            ans.append(k[j])
    elif x[i] < x[j]:
        x[i] += 1
        x[j] -= 1
        ans.append(k[i])
    else:
        x[i] -= 1
        x[j] += 1
        ans.append(k[j])
else:
    print('Yes')
    print(*ans, sep='\n')

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
ans = []
D = {'A':a, 'B':b, 'C':c}
sx = SR(n)
for i in range(n):
    s = sx[i]
    s1 = s[0]
    s2 = s[1]
    if D[s1] == D[s2] == 0:
        print('No')
        exit()
    elif D[s[0]] == D[s[1]] == 1 and i + 1 < n:
        if s1 in sx[i + 1]:
            D[s1] += 1
            D[s2] -= 1
            ans += [s1]
        else:
            D[s2] += 1
            D[s1] -= 1
            ans += [s2]
    else:
        if D[s1] > D[s2]:
            ans += [s2]
            D[s1] -= 1
            D[s2] += 1
        else:
            ans += [s1]
            D[s1] += 1
            D[s2] -= 1

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

Submission status: Accepted

Solution code:
def g_solve(N, A, B, C):
  seq = []
  for _ in range(N):
    s = input()
    if s == 'AB':
      if A > B:
        A -= 1
        B += 1
        seq.append('B')
      else:
        A += 1
        B -= 1
        seq.append('A')
    elif s == 'AC':
      if A > C:
        A -= 1
        C += 1
        seq.append('C')
      else:
        A += 1
        C -= 1
        seq.append('A')
    elif s == 'BC':
      if B > C:
        B -= 1
        C += 1
        seq.append('C')
      else:
        B += 1
        C -= 1
        seq.append('B')
    if A < 0 or B < 0 or C < 0:
      print('No')
      return
  print('Yes')
  print('\n'.join(seq))    


def dp_solve(N, a,b,c):
  state = {}
  state[(a, b, c)] = []
  for _ in range(N):
    s = input()
    next_state = {}
    for a, b, c in state:
      if s == 'AB':
        if b-1 >= 0:
            next_state[(a+1, b-1, c)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b+1, c)] = ['B', state[(a,b,c)]]
      if s == 'AC':
        if c-1 >= 0:
          next_state[(a+1, b, c-1)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b, c+1)] = ['C', state[(a,b,c)]]
      if s == 'BC':
        if c-1 >= 0:
          next_state[(a, b+1, c-1)] = ['B', state[(a,b,c)]]
        if b-1 >= 0:
          next_state[(a, b-1, c+1)] = ['C', state[(a,b,c)]]
    state = next_state
    if not state:
      print('No')
      return
    
  print('Yes')
  ans = []
  seq = list(state.values())[0]
  while seq:
    ans.append(seq[0])
    
    seq = seq[1]
  ans.reverse()
  for a in ans:
    print(a)
    
  
  
N, A, B, C = [int(x) for x in input().split(' ')]
if A + B + C < 20:
  dp_solve(N, A, B, C)
else:
  g_solve(N, A, B, C)

Submission status: Accepted

Solution code:
d=dict()
n,d["A"],d["B"],d["C"]=map(int,input().split())
s=[input() for i in range(n)]
ans=[]
i=0
while i<n:
    x,y=s[i][0],s[i][1]
    if d[x]==0 and d[y]==0:
        print("No")
        quit()
    elif d[x]==0:
        ans.append(x)
        d[x]+=1
        d[y]-=1
    elif d[y]==0:
        ans.append(y)
        d[x]-=1
        d[y]+=1
    else:
        if i==n-1:
            ans.append(x)
            break
        if y in s[i+1]:
            ans.append(y)
            d[x]-=1
            d[y]+=1
        else:
            ans.append(x)
            d[x]+=1
            d[y]-=1
    i+=1
print("Yes")
for i in ans:
    print(i)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
state = {'A':A, 'B':B, 'C':C}
ans = []
if A+B+C >= 3 or A+B+C == 1:
    for _ in range(N):
        s = input()
        if state[s[0]] == 0 and state[s[1]] == 0:
            print('No')
            exit()
        elif state[s[0]] >= state[s[1]]:
            ans.append(s[1])
            state[s[0]] -= 1
            state[s[1]] += 1
        else:
            ans.append(s[0])
            state[s[0]] += 1
            state[s[1]] -= 1
elif A+B+C == 2:
    sn = 0
    for i in range(N):
        if sn != 0:
            s = sn
            sn = 0
        else:   
            s = input()
        if state[s[0]] == 0 and state[s[1]] == 0:
            print('No')
            exit()
        elif state[s[0]] == state[s[1]]:
            if i == N-1:
                ans.append(s[1])
                state[s[0]] -= 1
                state[s[1]] += 1
            else:
                sn = input()
                if s[0] in sn:
                    ans.append(s[0])
                    state[s[0]] += 1
                    state[s[1]] -= 1
                else:
                    ans.append(s[1])
                    state[s[0]] -= 1
                    state[s[1]] += 1
        elif state[s[0]] > state[s[1]]:
            ans.append(s[1])
            state[s[0]] -= 1
            state[s[1]] += 1
        else:
            ans.append(s[0])
            state[s[0]] += 1
            state[s[1]] -= 1
else:
    print('No')
    exit()

print('Yes')
for i in range(N):
    print(ans[i])

Submission status: Accepted

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

sum = a + b + c
lst = []
if sum != 2:
    for _ in range(n):
        s = input()
        if s == "AB":
            if a < b:
                a += 1
                b -= 1
                lst.append("A")
            else:
                a -= 1
                b += 1
                if a < 0:
                    print("No")
                    break
                lst.append("B")
        elif s == "AC":
            if a < c:
                a += 1
                c -= 1
                lst.append("A")
            else:
                a -= 1
                c += 1
                if a < 0:
                    print("No")
                    break
                lst.append("C")
        else:
            if c < b:
                c += 1
                b -= 1
                lst.append("C")
            else:
                c -= 1
                b += 1
                if c < 0:
                    print("No")
                    break
                lst.append("B")
    else:
        print("Yes")
        for alpha in lst:
            print(alpha)
elif n == 1:
    s = input()
    if s == "AB":
        if a == b == 0:
            print("No")
        else:
            print("Yes")
            if a > b:
                print("B")
            else:
                print("A")
    elif s == "AC":
        if a == c == 0:
            print("No")
        else:
            print("Yes")
            if a > c:
                print("C")
            else:
                print("A")
    else:
        if c == b == 0:
            print("No")
        else:
            print("Yes")
            if c > b:
                print("B")
            else:
                print("C")            
                
else:
    s = input()
    flag = True
    flag2 = True
    for _ in range(n - 1):
        if s == "AB":
            if a == b == 0:
                print("No")
                flag2 = False
                break
            elif a != b:
                if a > b:
                    lst.append("B")
                    a -= 1
                    b += 1
                else:
                    lst.append("A")
                    a += 1
                    b -= 1
                s = input()
                flag = True
            else:
                s = input()
                if s == "AB":
                    lst.append("A")
                    a = 2
                    b = 0
                elif s == "AC":
                    lst.append("A")
                    a = 2
                    b = 0
                else:
                    lst.append("B")
                    b = 2
                    a = 0
                    
        elif s == "AC":
            if a == c == 0:
                print("No")
                flag2 = False
                break
            elif a != c:
                if a > c:
                    lst.append("C")
                    a -= 1
                    c += 1
                else:
                    lst.append("A")
                    a += 1
                    c -= 1
                s = input()
                flag = True
            else:
                s = input()
                if s == "AC":
                    lst.append("A")
                    a = 2
                    c = 0
                elif s == "AB":
                    lst.append("A")
                    a = 2
                    c = 0
                else:
                    lst.append("C")
                    c = 2
                    a = 0
                    
        else:
            if b == c == 0:
                print("No")
                flag2 = False
                break
            elif b != c:
                if b > c:
                    lst.append("C")
                    b -= 1
                    c += 1
                else:
                    lst.append("B")
                    b += 1
                    c -= 1
                s = input()
                flag = True
            else:
                s = input()
                if s == "BC":
                    lst.append("B")
                    b = 2
                    c = 0
                elif s == "AB":
                    lst.append("B")
                    b = 2
                    c = 0
                else:
                    lst.append("C")
                    c = 2
                    b = 0
    else:
        if flag:
            if s == "AB":
                if a == b == 0:
                    print("No")
                    flag2 = False
                if a < b:
                    lst.append("A")
                else:
                    lst.append("B")
            elif s == "AC":
                if a == c == 0:
                    print("No")
                    flag2 = False
                if a < c:
                    lst.append("A")
                else:
                    lst.append("C")
            else:
                if b == c == 0:
                    print("No")
                    flag2 = False
                if b < c:
                    lst.append("B")
                else:
                    lst.append("C")
    if flag2:
        print("Yes")
        for alpha in lst:
            print(alpha)

Submission status: Accepted

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

val=[a,b,c]
flag=True
ansls=list()

for i in range(n):
    s1=ord(op[i][0])-ord('A')
    s2=ord(op[i][1])-ord('A')
    if val[s1]>val[s2]:
        ansls.append(s2)
        val[s2]+=1
        val[s1]-=1
    elif val[s1]<val[s2] or i+1==n:
        ansls.append(s1)
        val[s1]+=1
        val[s2]-=1
    else:
        s1n = ord(op[i+1][0])-ord('A')
        s2n = ord(op[i+1][1])-ord('A')
        nxt = set([s1,s2]) & set([s1n,s2n])
        nxt = list(nxt)[0]
        ansls.append(nxt)
        val[nxt]+=1
        if nxt==s1:
            val[s2]-=1
        else:
            val[s1]-=1
    if min(val)<0:
        flag=False
        break

if flag:
    print('Yes')
    for i in range(n):
        print(chr(ansls[i]+ord('A')))
else:
    print('No')

Submission status: Accepted

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

N, A, B, C = list(map(int, input().split()))
s = [0] * (N+1)

for i in range(N):
    s[i] = input()
s[N] = 'AB'

ans = [0] * N
for i in range(N):
    if s[i] == 'AB':
        if A > B:
            A -= 1
            B += 1
            ans[i] = 'B'
        elif A < B:
            A += 1
            B -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'BC':
                A -= 1
                B += 1
                ans[i] = 'B'
            else:
                A += 1
                B -= 1
                ans[i] = 'A'
    elif s[i] == 'AC':
        if A > C:
            A -= 1
            C += 1
            ans[i] = 'C'
        elif A < C:
            A += 1
            C -= 1
            ans[i] = 'A'
        else:
            if s[i+1] == 'AB':
                A += 1
                C -= 1
                ans[i] = 'A'
            else:
                A -= 1
                C += 1
                ans[i] = 'C'
    elif s[i] == 'BC':
        if B > C:
            B -= 1
            C += 1
            ans[i] = 'C'
        elif B < C:
            B += 1
            C -= 1
            ans[i] = 'B'
        else:
            if s[i+1] == 'AB':
                B += 1
                C -= 1
                ans[i] = 'B'
            else:
                B -= 1
                C += 1
                ans[i] = 'C'

    if A < 0 or B < 0 or C < 0:
        print('No')
        sys.exit()
print('Yes')
for i in range(N):
    print(ans[i])

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
S = []
T = []
r = 0
for i in range(n):
    S.append(input())
for i in range(n):
    if S[i] == "AB":
        if a < b:
            a += 1
            b -= 1
            T.append("A")
        elif a > b:
            a -= 1
            b += 1
            T.append("B")
        else:
            if i < n-1:
                if S[i+1] == "AC":
                    a += 1
                    b -= 1
                    T.append("A")
                else:
                    a -= 1
                    b += 1
                    T.append("B")
            else:
                a += 1
                b -= 1
                T.append("A")
    elif S[i] == "AC":
        if a < c:
            a += 1
            c -= 1
            T.append("A")
        elif a > c:
            a -= 1
            c += 1
            T.append("C")
        else:
            if i < n-1:
                if S[i+1] == "AB":
                    a += 1
                    c -= 1
                    T.append("A")
                else:
                    a -= 1
                    c += 1
                    T.append("C")
            else:
                a += 1
                c -= 1
                T.append("A")
    else:
        if b < c:
            b += 1
            c -= 1
            T.append("B")
        elif b > c:
            b -= 1
            c += 1
            T.append("C")
        else:
            if i < n-1:
                if S[i+1] == "AB":
                    b += 1
                    c -= 1
                    T.append("B")
                else:
                    b -= 1
                    c += 1
                    T.append("C")
            else:
                b += 1
                c -= 1
                T.append("B")
    if a < 0 or b < 0 or c < 0:
        r = 1
        break;
if r == 0:
    print("Yes")
    for i in range(n):
        print(T[i])
else:
    print("No")

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def main():
  N, A, B, C = map(int, input().split())
  values = [A, B, C]
  moves = [input().strip() for _ in range(N)] + ['AB']
  cmds = []

  for move, next_move in zip(moves[:-1], moves[1:]):
    if move == 'AB':
      i, j, lc, rc = 0, 1, 'A', 'B'
    elif move == 'AC':
      i, j, lc, rc = 0, 2, 'A', 'C'
    elif move == 'BC':
      i, j, lc, rc = 1, 2, 'B', 'C'

    if next_move == 'AB':
      ni, nj = 0, 1
    elif next_move == 'AC':
      ni, nj = 0, 2
    elif next_move == 'BC':
      ni, nj = 1, 2

    if values[i] == 0 and values[j] == 0:
      cmds = None
      break
    elif values[i] < values[j]:
      cmds.append(lc)
      values[i] += 1
      values[j] -= 1
    elif values[i] > values[j]:
      cmds.append(rc)
      values[i] -= 1
      values[j] += 1
    elif ni == i or nj == i:
      cmds.append(lc)
      values[i] += 1
      values[j] -= 1
    elif ni == j or nj == j:
      cmds.append(rc)
      values[i] -= 1
      values[j] += 1
    else:
      cmds.append(lc)
      values[i] += 1
      values[j] -= 1

  if cmds is not None:
    print('Yes')
    for c in cmds:
      print(c)
  else:
    print('No')


if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
v = [A, B, C]
s = [-1 for i in range(N)]
for i in range(N):
    t = input()
    if t == "AB":
        s[i] = 0
    if t == "BC":
        s[i] = 1
    if t == "AC":
        s[i] = 2
ans = [-1 for i in range(N)]
OK = True

for i in range(N):
    p = s[i]
    q = (s[i]+1)%3
    if v[p] < v[q]:
        ans[i] = p
        v[p] += 1
        v[q] -= 1
    elif v[p] > v[q]:
        ans[i] = q
        v[p] -= 1
        v[q] += 1
    elif v[p] > 0:
        if i == N-1:
            ans[i] = p
            v[p] += 1
            v[q] -= 1
        elif s[i+1] == q:
            ans[i] = q
            v[p] -= 1
            v[q] += 1
        else:
            ans[i] = p
            v[p] += 1
            v[q] -= 1
    else:
        OK = False
        break

ch = ["A", "B" ,"C"]
if OK:
    print("Yes")
    for i in range(N):
        print(ch[ans[i]])
else:
    print("No")