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 = []
flag = True
cnt = 0
while True:
  cnt += 1
  if cnt > N:
    break
  s = input()
  if s == 'AB':
    if A == B == 0:
      flag = False
      break
    elif A == B == 1:
      if cnt == N:
        ans.append('A')
        break
      else:
        cnt += 1
        s = input()
        if s == 'AB':
          ans.append('A')
          ans.append('B')
          continue
        elif s == 'AC':
          ans.append('A')
          ans.append('C')
          B -= 1
          C += 1
          continue
        else:
          ans.append('B')
          ans.append('C')
          A -= 1
          C += 1
          continue
    elif A >= B:
      A -= 1
      B += 1
      ans.append('B')
      continue
    else:
      B -= 1
      A += 1
      ans.append('A')
      continue
  elif s == 'BC':
    if B == C == 0:
      flag = False
      break
    elif B == C == 1:
      if cnt == N:
        ans.append('B')
        break
      else:
        cnt += 1
        s = input()
        if s == 'BC':
          ans.append('B')
          ans.append('C')
          continue
        elif s == 'AB':
          ans.append('B')
          ans.append('A')
          C -= 1
          A += 1
          continue
        else:
          ans.append('C')
          ans.append('A')
          B -= 1
          A += 1
          continue
    elif B >= C:
      B -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      B += 1
      ans.append('B')
      continue 
  else:
    if C == A == 0:
      flag = False
      break
    elif C == A == 1:
      if cnt == N:
        ans.append('C')
        break
      else:
        cnt += 1
        s = input()
        if s == 'AC':
          ans.append('C')
          ans.append('A')
          continue
        elif s == 'BC':
          ans.append('C')
          ans.append('B')
          A -= 1
          B += 1
          continue
        else:
          ans.append('A')
          ans.append('B')
          C -= 1
          B += 1
          continue
    elif A >= C:
      A -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      A += 1
      ans.append('A')
      continue
if flag:
  print('Yes')
  for item in ans:
    print(item)
else:
  print('No')

Submission status: Accepted

Solution code:
N, A, B, C = map(int,input().split())
ans = []
flag = True
cnt = 0
while True:
  cnt += 1
  if cnt > N:
    break
  s = input()
  if s == 'AB':
    if A == B == 0:
      flag = False
      break
    elif A == B == 1 and C == 0:
      if cnt == N:
        ans.append('A')
        break
      else:
        cnt += 1
        s = input()
        if s == 'AB':
          ans.append('A')
          ans.append('B')
          continue
        elif s == 'AC':
          ans.append('A')
          ans.append('C')
          B -= 1
          C += 1
          continue
        else:
          ans.append('B')
          ans.append('C')
          A -= 1
          C += 1
          continue
    elif A >= B:
      A -= 1
      B += 1
      ans.append('B')
      continue
    else:
      B -= 1
      A += 1
      ans.append('A')
      continue
  elif s == 'BC':
    if B == C == 0:
      flag = False
      break
    elif B == C == 1 and A == 0:
      if cnt == N:
        ans.append('B')
        break
      else:
        cnt += 1
        s = input()
        if s == 'BC':
          ans.append('B')
          ans.append('C')
          continue
        elif s == 'AB':
          ans.append('B')
          ans.append('A')
          C -= 1
          A += 1
          continue
        else:
          ans.append('C')
          ans.append('A')
          B -= 1
          A += 1
          continue
    elif B >= C:
      B -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      B += 1
      ans.append('B')
      continue 
  else:
    if C == A == 0:
      flag = False
      break
    elif C == A == 1 and B == 0:
      if cnt == N:
        ans.append('C')
        break
      else:
        cnt += 1
        s = input()
        if s == 'AC':
          ans.append('C')
          ans.append('A')
          continue
        elif s == 'BC':
          ans.append('C')
          ans.append('B')
          A -= 1
          B += 1
          continue
        else:
          ans.append('A')
          ans.append('B')
          C -= 1
          B += 1
          continue
    elif A >= C:
      A -= 1
      C += 1
      ans.append('C')
      continue
    else:
      C -= 1
      A += 1
      ans.append('A')
      continue
if flag:
  print('Yes')
  for item in ans:
    print(item)
else:
  print('No')

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    def calc(x, y):
        cnt[x] += 1
        cnt[y] -= 1
        res.append(S[x])

    n, *cnt = map(int, input().split())
    S = "ABC"
    query = []
    for _ in range(n):
        s = input().rstrip()
        a = 0 if s == "AB" else 0 if s == "AC" else 1
        b = 1 if s == "AB" else 2 if s == "AC" else 2
        query.append((a, b))
    query.append((None, None))

    res = []
    for i in range(n):
        a, b = query[i]
        if cnt[a] == cnt[b] == 0:
            print("No")
            return
        elif cnt[a] == 0:
            calc(a, b)
        elif cnt[b] == 0:
            calc(b, a)
        else:
            next_a, next_b = query[i + 1]
            if a == next_a or a == next_b:
                calc(a, b)
            else:
                calc(b, a)
    print("Yes")
    print(*res, sep="\n")


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
n = [A, B, C]
c = ["A", "B", "C"]
S = [input() for i in range(N)]
op = []

for i in range(N):
	if S[i] == "AB":
		op.append((0, 1))
	elif S[i] == "BC":
		op.append((1, 2))
	else:
		op.append((2, 0))

dec = []
for i in range(N):
	j1 = op[i][0]
	j2 = op[i][1]
	if n[j1] < n[j2]:
		dec.append(c[j1])
		n[j1] += 1
		n[j2] -= 1
	elif n[j2] < n[j1]:
		dec.append(c[j2])
		n[j2] += 1
		n[j1] -= 1
	elif i < N - 1:
		if j1 in op[i + 1]:
			if j1 == op[i + 1][0]:
				j3 = op[i + 1][1]
			else:
				j3 = op[i + 1][0]
			if n[j3] < n[j1]:
				dec.append(c[j1])
				n[j1] += 1
				n[j2] -= 1
			else:
				dec.append(c[j2])
				n[j2] += 1
				n[j1] -= 1
		else:
			if j2 == op[i + 1][0]:
				j3 = op[i + 1][1]
			else:
				j3 = op[i + 1][0]
			if n[j3] < n[j2]:
				dec.append(c[j2])
				n[j2] += 1
				n[j1] -= 1
			else:
				dec.append(c[j1])
				n[j1] += 1
				n[j2] -= 1
	else:
		dec.append(c[j1])
		n[j1] += 1
		n[j2] -= 1
	if n[0] < 0 or n[1] < 0 or n[2] < 0:
		print("No")
		exit()
print("Yes")
print("\n".join(dec))

Submission status: Accepted

Solution code:
def check(x,y,z,i,ops):
    if x == 1 and y == 1 and z == 0 and i < len(ops) - 1:        
        return (ops[i], ops[i+1]) in [("AB", "AC"), ("BC", "AB"), ("AC", "AB")]
    return (x <= y)

A=0
B=1
C=2
nabc = [int(_x) for _x in input().split()]
n = nabc[0]
abc = nabc[1:]
ops = []
for i in range(n):
    ops.append(input())

result = []
for i in range(n):
    op = ops[i]
    if op == "AB":
        (s,t,u) = (A,B,C)
    elif op == "BC":
        (s,t,u) = (B,C,A)
    else:
        (s,t,u) = (A,C,B)
    if check(abc[s],abc[t],abc[u],i,ops):
        result.append(s)
        abc[s] += 1
        abc[t] -= 1
    else:
        result.append(t)
        abc[s] -= 1
        abc[t] += 1
    if abc[s]<0 or abc[t]<0:
        print("No")
        break
else:
    print("Yes")
    for r in result:
        if r==A: print("A")
        if r==B: print("B")
        if r==C: print("C")

Submission status: Accepted

Solution code:
# greedyな選択：数字が小さいほうを増やし大きいほうを減らす。等しい場合どっちでもいい（とりあえず左を増やす）。
# 
# 初手 1 1 1 なら絶対成功 (greedy)
# どこかの状態で 1 1 1 を作れるなら絶対成功 (greedy)
# 0 1 2 なら絶対成功 (greedy)
# 0 0 3 で、初手がAB以外なら絶対成功 (greedy)
# 0 0 0 なら絶対失敗
#
# 初手で成否が分からないのは
# 0 1 1
# 0 0 2
# 0 0 1
#
# 0 0 1 は greedyにやるしかない
# 0 0 2 は、少なくとも1回はgreedy
#
# 結局、各回のgreedyでは問題になるのは
# 0 1 1 で 1 1 のペアでどっちを増やすか選ぶときのみ
# これは1個先を読めば十分


def select(x, y, ops, i, z):
    # true -> x を増やす
    if not(x == 1 and y == 1 and z == 0 and i < len(ops) - 1):
        if x <= y:
            return True
        else:
            return False

    # x = y = 1, z = 0, i < len(ops) - 1
    # 先読みして xz なら x を増やす
    # 先読みして xy なら y を増やす
    # 先読みして xy なら どっちでもいい
    op = ops[i]
    nop = ops[i+1]

    oo = (op, nop)
    if oo in [("AB", "AC"), ("BC", "AB"), ("AC", "AB")]:
        return True

    return False

def main():
    nabc = [int(_x) for _x in input().split()]
    n = nabc[0]
    a = nabc[1]
    b = nabc[2]
    c = nabc[3]

    ops = []
    for i in range(n):
        ops.append(input())

    result = []
    for i in range(n):
        op = ops[i]
        if op == "AB":
            if select(a, b, ops, i, c):
                result.append("A")
                a+=1
                b-=1
            else:
                result.append("B")
                a-=1
                b+=1
        if op == "AC":
            if select(a, c, ops, i, b):
                result.append("A")
                a+=1
                c-=1
            else:
                result.append("C")
                a-=1
                c+=1
        if op == "BC":
            if select(b, c, ops, i, a):
                result.append("B")
                b+=1
                c-=1
            else:
                result.append("C")
                b-=1
                c+=1
        if a <0 or b<0 or c<0:
            print("No")
            return

    print("Yes")
    for r in result:
        print(r)

main()

Submission status: Accepted

Solution code:
# 初手 1 1 1 なら絶対成功 (greedy)
# どこかの状態で 1 1 1 を作れるなら絶対成功 (greedy)
# 0 1 2 なら絶対成功 (greedy)
# 0 0 3 で、初手がAB以外なら絶対成功 (greedy)
# 0 0 0 なら絶対失敗
#
# 初手で成否が分からないのは
# 0 1 1
# 0 0 2
# 0 0 1
#
# 0 0 1 は greedyにやるしかない
# 0 0 2 は、少なくとも1回はgreedy
#
# 結局、各回のgreedyでは問題になるのは
# 0 1 1 のときのみ
# これは1個先を読めば十分


def select(x, y, ops, i, z):
    # true -> x を増やす
    if not(x == 1 and y == 1 and i < len(ops) - 1):
        if x < y:
            return True
        else:
            return False

    # x = y = 1, i < len(ops) - 1
    # 先読みして xz なら x を増やす
    # 先読みして xy なら y を増やす
    # 先読みして xy なら どっちでもいい
    op = ops[i]
    nop = ops[i+1]

    oo = (op, nop)
    if op == nop:
        return True

    if oo in [("AB", "AC"), ("BC", "AB"), ("AC", "AB")]:
        return True

    return False

def main():
    nabc = [int(_x) for _x in input().split()]
    n = nabc[0]
    a = nabc[1]
    b = nabc[2]
    c = nabc[3]

    ops = []
    for i in range(n):
        ops.append(input())

    result = []
    for i in range(n):
        op = ops[i]
        if op == "AB":
            if select(a, b, ops, i, c):
                result.append("A")
                a+=1
                b-=1
            else:
                result.append("B")
                a-=1
                b+=1
        if op == "AC":
            if select(a, c, ops, i, b):
                result.append("A")
                a+=1
                c-=1
            else:
                result.append("C")
                a-=1
                c+=1
        if op == "BC":
            if select(b, c, ops, i, a):
                result.append("B")
                b+=1
                c-=1
            else:
                result.append("C")
                b-=1
                c+=1
        if a <0 or b<0 or c<0:
            print("No")
            return

    print("Yes")
    for r in result:
        print(r)

main()

Submission status: Accepted

Solution code:
def select(x, y, ops, i, z):
    # true -> x を増やす
    if not(x == 1 and y == 1 and i < len(ops) - 1):
        if x < y:
            return True
        else:
            return False

    # x = y = 1, i < len(ops) - 1
    # 先読みして xz なら x を増やす
    # 先読みして xy なら y を増やす
    # 先読みして xy なら どっちでもいい
    op = ops[i]
    nop = ops[i+1]

    oo = (op, nop)
    if op == nop:
        return True

    if oo in [("AB", "AC"), ("BC", "AB"), ("AC", "AB")]:
        return True

    return False

def main():
    nabc = [int(_x) for _x in input().split()]
    n = nabc[0]
    a = nabc[1]
    b = nabc[2]
    c = nabc[3]

    ops = []
    for i in range(n):
        ops.append(input())

    result = []
    for i in range(n):
        op = ops[i]
        if op == "AB":
            if select(a, b, ops, i, c):
                result.append("A")
                a+=1
                b-=1
            else:
                result.append("B")
                a-=1
                b+=1
        if op == "AC":
            if select(a, c, ops, i, b):
                result.append("A")
                a+=1
                c-=1
            else:
                result.append("C")
                a-=1
                c+=1
        if op == "BC":
            if select(b, c, ops, i, a):
                result.append("B")
                b+=1
                c-=1
            else:
                result.append("C")
                b-=1
                c+=1                
        if a <0 or b<0 or c<0:
            print("No")
            return

    print("Yes")
    for r in result:
        print(r)

main()

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N, A, B, C = getNM()
que = [input() for i in range(N)]
que.append(que[-1])
res = []
for i in range(N):
    q = que[i]

    if q == 'AB':
        if A > B:
            A -= 1
            B += 1
            res.append('B')
        elif A < B:
            A += 1
            B -= 1
            res.append('A')
        else:
            if que[i + 1] == 'AB':
                A -= 1
                B += 1
                res.append('B')
            elif que[i + 1] == 'AC':
                A += 1
                B -= 1
                res.append('A')
            elif que[i + 1] == 'BC':
                A -= 1
                B += 1
                res.append('B')
    elif q == 'AC':
        if A > C:
            A -= 1
            C += 1
            res.append('C')
        elif A < C:
            A += 1
            C -= 1
            res.append('A')
        else:
            if que[i + 1] == 'AC':
                A -= 1
                C += 1
                res.append('C')
            elif que[i + 1] == 'AB':
                A += 1
                C -= 1
                res.append('A')
            elif que[i + 1] == 'BC':
                A -= 1
                C += 1
                res.append('C')
    else:
        if B > C:
            B -= 1
            C += 1
            res.append('C')
        elif B < C:
            B += 1
            C -= 1
            res.append('B')
        else:
            if que[i + 1] == 'BC':
                B -= 1
                C += 1
                res.append('C')
            elif que[i + 1] == 'AB':
                B += 1
                C -= 1
                res.append('B')
            elif que[i + 1] == 'AC':
                B -= 1
                C += 1
                res.append('C')
    if A < 0 or B < 0 or C < 0:
        print('No')
        exit()
print('Yes')
for i in res:
    print(i)

Submission status: Accepted

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    variables = {
        "A": A,
        "B": B,
        "C": C
    }
    ans = [None] * N
    S = [None] * N
    for i in range(N):
        S[i] = input()
    for i in range(N):
        c1, c2 = S[i]
        if variables[c1] == variables[c2] == 0:
            print("No")
            return
        elif variables[c1] == variables[c2] and i+1 < N:
            if c1 in S[i+1]:
                ans[i] = c1
                variables[c1] += 1
                variables[c2] -= 1
            else:
                ans[i] = c2
                variables[c1] -= 1
                variables[c2] += 1
        elif variables[c1] > variables[c2]:
            ans[i] = c2
            variables[c1] -= 1
            variables[c2] += 1
        else:
            ans[i] = c1
            variables[c1] += 1
            variables[c2] -= 1
    print("Yes")
    for i in range(N):
        print(ans[i])


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
s = [input() for i in range(n)]
dic = {'A':a,'B':b,'C':c}

ans = ["Yes"]
for i in range(n):
    if dic[s[i][0]] == 0 and dic[s[i][1]] == 0:
        print("No")
        exit()
    elif dic[s[i][0]] == 0:
        dic[s[i][0]] += 1
        dic[s[i][1]] -= 1
        ans.append(s[i][0])
    elif dic[s[i][1]] == 0:
        dic[s[i][1]] += 1
        dic[s[i][0]] -= 1
        ans.append(s[i][1])
    elif i != n-1:
        if s[i][0] in s[i+1]:
            dic[s[i][0]] += 1
            dic[s[i][1]] -= 1
            ans.append(s[i][0])
        else:
            dic[s[i][1]] += 1
            dic[s[i][0]] -= 1
            ans.append(s[i][1])
    else:
        dic[s[i][0]] += 1
        dic[s[i][1]] -= 1
        ans.append(s[i][0])

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, a, b, c = map(int, readline().split())

    s = [input() for _ in range(n)]
    m = []

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

        print("Yes")
        for x in m:
            print(x)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
vars = {"A": A, "B": B, "C": C}
sum_ABC = A+B+C
S = [input() for _ in range(N)]
Ans = []
for s, s_next in zip(S, S[1:]):
    if vars[s[0]] > vars[s[1]] or (vars[s[0]]==vars[s[1]] and s[1] in s_next):
        Ans.append(s[1])
        vars[s[1]] += 1
        vars[s[0]] -= 1
    else:# vars[s[0]] < vars[s[1]]:
        Ans.append(s[0])
        vars[s[0]] += 1
        vars[s[1]] -= 1
    if vars[s[0]] < 0 or vars[s[1]] < 0:
        print("No")
        exit()
s = S[-1]
if vars[s[0]] > vars[s[1]]:
    Ans.append(s[1])
    vars[s[1]] += 1
    vars[s[0]] -= 1
else:
    Ans.append(s[0])
    vars[s[0]] += 1
    vars[s[1]] -= 1
if vars[s[0]] < 0 or vars[s[1]] < 0:
    print("No")
    exit()
print("Yes")
print("\n".join(Ans))

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
nums = [A, B, C]
steps = [input() for i in range(N)]
chars = ["A", "B", "C"]
operations = []
can_select = True
for i in range(N):
    s = steps[i]
    left_idx = chars.index(s[0])
    right_idx = chars.index(s[1])
    if (nums[left_idx] == 0 and nums[right_idx] == 0) or nums[left_idx] < 0 or nums[right_idx] < 0:
        can_select = False
        break
    elif nums[left_idx] == 1 and nums[right_idx] == 1 and i != N-1:
        if chars[left_idx] in list(steps[i + 1]):
            nums[left_idx] += 1
            nums[right_idx] -= 1
            operations.append(chars[left_idx])
        else:
            nums[left_idx] -= 1
            nums[right_idx] += 1
            operations.append(chars[right_idx])
    else:
        if nums[left_idx] < nums[right_idx]:
            nums[left_idx] += 1
            nums[right_idx] -= 1
            operations.append(chars[left_idx])
        else:
            nums[left_idx] -= 1
            nums[right_idx] += 1
            operations.append(chars[right_idx])
if can_select:
    print("Yes")
    for i in operations:
        print(i)
else:
    print("No")

Submission status: Accepted

Solution code:
'''
自宅用PCでの解答
'''
import math
#import numpy as np
import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7
dir = [(-1,0),(0,-1),(1,0),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,a,b,c = map(int,ipt().split())
    s = a+b+c
    q = [input() for i in range(n)]
    ans = []

    for i,qi in enumerate(q):
        if qi == "AB":
            if a == b == 0:
                print("No")
                exit()
            elif a == 0:
                ans.append("A")
                a += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                a -= 1
                b += 1
            elif i == n-1:
                ans.append("A")
            elif a == b == 1:
                if q[i+1] == "BC":
                    ans.append("B")
                    b += 1
                    a -= 1
                else:
                    ans.append("A")
                    b -= 1
                    a += 1
            else:
                if a > b:
                    ans.append("B")
                    b += 1
                    a -= 1
                else:
                    ans.append("A")
                    a += 1
                    b -= 1
        elif qi == "BC":
            if c == b == 0:
                print("No")
                exit()
            elif c == 0:
                ans.append("C")
                c += 1
                b -= 1
            elif b == 0:
                ans.append("B")
                c -= 1
                b += 1
            elif i == n-1:
                ans.append("C")
            elif c == b == 1:
                if q[i+1] == "AB":
                    ans.append("B")
                    b += 1
                    c -= 1
                else:
                    ans.append("C")
                    b -= 1
                    c += 1
            else:
                if c > b:
                    ans.append("B")
                    b += 1
                    c -= 1
                else:
                    ans.append("C")
                    c += 1
                    b -= 1
        else:
            if a == c == 0:
                print("No")
                exit()
            elif a == 0:
                ans.append("A")
                a += 1
                c -= 1
            elif c == 0:
                ans.append("C")
                a -= 1
                c += 1
            elif i == n-1:
                ans.append("A")
            elif a == c == 1:
                if q[i+1] == "BC":
                    ans.append("C")
                    c += 1
                    a -= 1
                else:
                    ans.append("A")
                    c -= 1
                    a += 1
            else:
                if a > c:
                    ans.append("C")
                    c += 1
                    a -= 1
                else:
                    ans.append("A")
                    a += 1
                    c -= 1

    print("Yes")
    for i in ans:
        print(i)
    return None

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def solve(N,dic,S):
    ans = ['Yes']
    for i in range(N):
        if dic[S[i][0]]>dic[S[i][1]]:
            dic[S[i][0]]-=1
            dic[S[i][1]]+=1
            ans.append(S[i][1])
        elif dic[S[i][0]]<dic[S[i][1]] or dic[S[i][0]]>1:
            dic[S[i][1]]-=1
            dic[S[i][0]]+=1
            ans.append(S[i][0])
        elif dic[S[i][0]]==0:
            return ['No']
        elif i==N-1:
            dic[S[i][0]]+=1
            dic[S[i][1]]-=1
            ans.append(S[i][0])
        else:
            if S[i][0] in S[i+1]:
                dic[S[i][0]]+=1
                dic[S[i][1]]-=1
                ans.append(S[i][0])
            else:
                dic[S[i][1]]+=1
                dic[S[i][0]]-=1
                ans.append(S[i][1])
    return ans
print(*solve(N,dic,S),sep='\n')