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: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
import collections
import copy
import heapq
from collections import defaultdict
from heapq import heappop, heappush
import itertools
input = sys.stdin.readline
from collections import defaultdict
from heapq import heappop, heappush

from decimal import *

##### リストの 二分木検索 #####
# bisect_left(lists, 3)
# bisect_right(lists, 3)

##### プライオリティキュー #####
# heapq.heapify(a) #リストaのheap化
# heapq.heappush(a,x) #heap化されたリストaに要素xを追加
# heapq.heappop(a) #heap化されたリストaから最小値を削除＆その最小値を出力

# heapq.heappush(a, -x) #最大値を取り出す時は、pushする時にマイナスにして入れよう
# heapq.heappop(a) * (-1) #取り出す時は、-1を掛けて取り出すこと

##### タプルリストのソート #####
# sorted(ans) #(a, b) -> 1st : aの昇順, 2nd : bの昇順
# sorted(SP, key=lambda x:(x[0],-x[1])) #(a, b) -> 1st : aの昇順, 2nd : bの降順
# sorted(SP, key=lambda x:(-x[0],x[1])) #(a, b) -> 1st : aの降順, 2nd : bの昇順
# sorted(SP, key=lambda x:(-x[0],-x[1])) #(a, b) -> 1st : aの降順, 2nd : bの降順

# sorted(SP, key=lambda x:(x[1])) #(a, b) -> 1st : bの昇順
# sorted(SP, key=lambda x:(-x[1])) #(a, b) -> 1st : bの降順

##### 累乗 #####
# pow(x, y, z) -> x**y % z

##### 割り算の切り上げ #####
# tmp = -(-4 // 3)

##### dict の for文 #####
# for k, v in d.items():
#     print(k, v)

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N,A,B,C = inputMap()
	stat = [0,0,0]
	statAB = [0,0,0]
	Q = []
	for i in range(N):
		s = inputStr()
		Q.append(s)
		if s == "AB":
			stat[0] += 1
			stat[1] += 1
			statAB[0] += 1
		elif s == "AC":
			stat[0] += 1
			stat[2] += 1
			statAB[1] += 1
		elif s == "BC":
			stat[1] += 1
			stat[2] += 1
			statAB[2] += 1

	delA = False
	delB = False
	delC = False
	if stat[0] <= A:
		delA = True
	if stat[1] <= B:
		delB = True
	if stat[2] <= C:
		delC = True

	targetA = False
	targetB = False
	targetC = False


	#print(stat)
	#print("{} {} {}".format(delA,delB,delC))

	hon = "Yes"
	ans = []
	for s in Q:
		#print(i)
		#print("{} {} {}".format(A,B,C))
		if s == "AB":
			statAB[0] -=1
			stat[0] -= 1
			stat[1] -= 1
			if delA == True:
				B += 1
				A -= 1
				ans.append("B")
			elif delB == True:
				B -= 1
				A += 1
				ans.append("A")
			elif targetA == True and B-1 >= 0:
				B -= 1
				A += 1
				ans.append("A")
			elif targetB == True and A-1 >= 0:
				B += 1
				A -= 1
				ans.append("B")
			elif statAB[0] < statAB[1]:
				B -= 1
				A += 1
				ans.append("A")
			elif A < B:
				B -= 1
				A += 1
				ans.append("A")
			else:
				B += 1
				A -= 1
				ans.append("B")
			if A < 0 or B < 0:
				hon = "No"
				break
		elif s == "AC":
			statAB[1] -=1
			stat[0] -= 1
			stat[2] -= 1
			if delA == True:
				C += 1
				A -= 1
				ans.append("C")
			elif delC == True:
				C -= 1
				A += 1
				ans.append("A")
			elif targetA == True and C-1 >= 0:
				C -= 1
				A += 1
				ans.append("A")
			elif targetC == True and A-1 >= 0:
				C += 1
				A -= 1
				ans.append("C")
			elif statAB[1] < statAB[2]:
				C += 1
				A -= 1
				ans.append("C")
			elif A < C:
				C -= 1
				A += 1
				ans.append("A")
			else:
				C += 1
				A -= 1
				ans.append("C")
			if A < 0 or C < 0:
				hon = "No"
				break
		elif s == "BC":
			statAB[2] -=1
			stat[1] -= 1
			stat[2] -= 1
			if delB == True:
				C += 1
				B -= 1
				ans.append("C")
			elif delC == True:
				C -= 1
				B += 1
				ans.append("B")
			elif targetB == True and C-1 >= 0:
				C -= 1
				B += 1
				ans.append("B")
			elif targetC == True and B-1 >= 0:
				C += 1
				B -= 1
				ans.append("C")
			elif statAB[0] < statAB[2]:
				C -= 1
				B += 1
				ans.append("B")
			elif B < C:
				C -= 1
				B += 1
				ans.append("B")
			else:
				C += 1
				B -= 1
				ans.append("C")
			if C < 0 or B < 0:
				hon = "No"
				break

		if stat[0] <= A:
			delA = True
		if stat[1] <= B:
			delB = True
		if stat[2] <= C:
			delC = True

	if hon == "No":
		print(hon)
	else:
		print(hon)
		for i in ans:
			print(i)


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# nCr mod m
# rがn/2に近いと非常に重くなる
def combination(n, r, mod=10**9+7):
    r = min(r, n-r)
    res = 1
    for i in range(r):
        res = res * (n - i) * modinv(i+1, mod) % mod
    return res

# mを法とするaの乗法的逆元
def modinv(a, mod=10**9+7):
    return pow(a, mod-2, mod)

def egcd(a, b):
    if a == 0:
        return b, 0, 1
    else:
        g, y, x = egcd(b % a, a)
        return g, x - (b // a) * y, y

# nHr mod m
# 問題によって、combination()を切り替えること
def H(n, r, mod=10**9+7):
	# comb = Combination(n+r-1, mod)
	# return comb(n+r-1, r)
    return combination(n+r-1, r, mod)


class Combination:
    """
    O(n)の前計算を1回行うことで，O(1)でnCr mod mを求められる
    n_max = 10**6のとき前処理は約950ms (PyPyなら約340ms, 10**7で約1800ms)
    使用例：
    comb = Combination(1000000)
    print(comb(5, 3))  # 10
    """
    def __init__(self, n_max, mod=10**9+7):
        self.mod = mod
        self.modinv = self.make_modinv_list(n_max)
        self.fac, self.facinv = self.make_factorial_list(n_max)

    def __call__(self, n, r):
        return self.fac[n] * self.facinv[r] % self.mod * self.facinv[n-r] % self.mod

    def make_factorial_list(self, n):
        # 階乗のリストと階乗のmod逆元のリストを返す O(n)
        # self.make_modinv_list()が先に実行されている必要がある
        fac = [1]
        facinv = [1]
        for i in range(1, n+1):
            fac.append(fac[i-1] * i % self.mod)
            facinv.append(facinv[i-1] * self.modinv[i] % self.mod)
        return fac, facinv

    def make_modinv_list(self, n):
        # 0からnまでのmod逆元のリストを返す O(n)
        modinv = [0] * (n+1)
        modinv[1] = 1
        for i in range(2, n+1):
            modinv[i] = self.mod - self.mod//i * modinv[self.mod%i] % self.mod
        return modinv

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
# dfs のサンプル
def dfs(graph,parent,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int, input().split())
command = []
nums = {"A":a, "B":b, "C":c}
for i in range(n):
  command.append(input())

if a+b+c == 0:
  print("No")
  exit()
if a+b+c == 1:
  out = ["Yes"]

  for i in range(n):
    if nums[command[i][0]] == 0 and nums[command[i][1]] == 0:
      print("No")
      exit()
    elif nums[command[i][0]] == 0:
      out.append(command[i][0])
      nums[command[i][0]] = 1
      nums[command[i][1]] = 0
    elif nums[command[i][1]] == 0:
      out.append(command[i][1])
      nums[command[i][1]] = 1
      nums[command[i][0]] = 0
  for i in range(n+1):
    print(out[i])

if a+b+c >= 2:
  if nums[command[0][0]] == 0 and nums[command[0][1]] == 0:
    print("No")
    exit()
  print("Yes")
  for i in range(n):
    if nums[command[i][0]] == 0:
      print(command[i][0])
      nums[command[i][0]] = 1
      nums[command[i][1]] = 0
    elif nums[command[i][1]] == 0:
      print(command[i][1])
      nums[command[i][1]] = 1
      nums[command[i][0]] = 0
    elif a+b+c==2 and nums[command[0][0]] == 1 and nums[command[0][1]] == 1\
    and i < n-1 and command[i] != command[i+1]:
      if command[i][0] in command[i+1]:
        print(command[i][0])
        nums[command[i][0]] = 1
        nums[command[i][1]] = 0
      else:
        print(command[i][1])
        nums[command[i][1]] = 1
        nums[command[i][0]] = 0        
    else:
      print(command[i][0])
      nums[command[i][0]] = 1
      nums[command[i][1]] = 0

Submission status: Wrong Answer

Solution code:
N, A, B, C = map(int, input().split())
inputs = tuple(tuple(input()) for _ in range(N))
dic = {
    "A": A,
    "B": B,
    "C": C
}
ans = [0] * N
for depth in range(N):
    key1, key2 = inputs[depth]
    if dic[key1] == dic[key2]:
        if not dic[key1]:
            print("No")
            quit()
        elif dic[key1] == 1 and depth + 1 < N:
            next_key1, next_key2 = inputs[depth + 1]
            common_key, *_ = {key1, key2} & {next_key1, next_key2}
            else_key, *_ = {key1, key2} - {common_key}
            dic[common_key] += 1
            dic[else_key] -= 1
            ans[depth] = common_key
            continue
    dic[key1] += 1
    dic[key2] -= 1
    ans[depth] = key1

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

Submission status: Wrong Answer

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

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for s in sl:
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if yusen == "A":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if yusen == "B":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if yusen == "C":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)

Submission status: Wrong Answer

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

a,b,c = (A,B,C)
trace = []
ans = "Yes"
for i in range(N):
  if s[i] == "AB":
    if a == 0 and b == 0:
      ans == "No"
      break
    elif a == 1 and b == 0:
      a -= 1
      b += 1
      trace.append("B")
    elif a == 0 and b == 1:
      a += 1
      b -= 1
      trace.append("A")
    elif i+1<N and s[i+1] =="BC":
      a -= 1
      b += 1
      trace.append("B")
    else:
      a += 1
      b -= 1
      trace.append("A")
  if s[i] == "BC":
    if b == 0 and c == 0:
      ans == "No"
      break
    elif b == 1 and c == 0:
      b -= 1
      c += 1
      trace.append("C")
    elif b == 0 and c == 1:
      b += 1
      c -= 1
      trace.append("B")
    elif i+1<N and s[i+1] =="AC":
      b -= 1
      c += 1
      trace.append("C")
    else:
      b += 1
      c -= 1
      trace.append("B")
  if s[i] == "AC":
    if a == 0 and c == 0:
      ans == "No"
      break
    elif a == 1 and c == 0:
      a -= 1
      c += 1
      trace.append("C")
    elif a == 0 and c == 1:
      a += 1
      c -= 1
      trace.append("A")
    elif i+1<N and s[i+1] =="AB":
      a -= 1
      c += 1
      trace.append("C")
    else:
      a += 1
      c -= 1
      trace.append("A")
print(ans)
if ans == "Yes":
  for v in trace:
    print(v)

Submission status: Wrong Answer

Solution code:
import sys
n,a,b,c=map(int,input().split())
def dif(a,b,c):
  return max(a,b,c)-min(a,b,c)
ans=[None]*n
for i in range(n):
  s=input()
  if s=='AB':
    if dif(a-1,b+1,c)>dif(a+1,b-1,c):
      a+=1
      b-=1
      ans[i]='A'
    else:
      a-=1
      b+=1
      ans[i]='B'
  elif s=='BC':
    if dif(a,b-1,c+1)>dif(a,b+1,c-1):
      b+=1
      c-=1
      ans[i]='B'
    else:
      b-=1
      c+=1
      ans[i]='C'
  else:
    if dif(a-1,b,c+1)<dif(a+1,b,c-1):
      a-=1
      c+=1
      ans[i]='C'
    else:
      a+=1
      c-=1
      ans[i]='A'
  if a<0 or b<0 or c<0:
    print('No')
    sys.exit()
print('Yes')
print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    before=[-1,-1,-1]
    ans=[-1]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]]!=-1:
                n[d[s]]+=1
                n[(d[s]-1)%3]-=1
                ans[before[d[s]]]=(d[s]-1)%3
                before[d[s]]=-1
            print('No')
            exit()
        if n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            ans[i]=d[s]
            before[(d[s]+1)%3]=i
    print('Yes')
    d2=['A','B','C']
    for a in ans:
        print(d2[a])
                
                    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
ans=[]
issyo=[]
check=[a,b,c]
if a+b+c==0:
    print('No')
    exit()
for i in range(n):
    s=input()
    if s=='AB':
        if a>=b:
            check[0]-=1
            check[1]+=1
            ans.append('B')
        else:
            check[1]-=1
            check[0]+=1
            ans.append('A')
    if s=='AC':
        if a>c:
            check[0]-=1
            check[2]+=1
            ans.append('C')
        else:
            check[2]-=1
            check[0]+=1
            ans.append('A')
    if s=='BC':
        if c>b:
            check[2]-=1
            check[1]+=1
            ans.append('B')
        else:
            check[1]-=1
            check[2]+=1
            ans.append('C')
    if any([check[0]<0,check[1]<0,check[2]<0]):
        print('No')
        exit()
print('Yes')
for i in range(n):
  print(ans[i])

Submission status: Wrong Answer

Solution code:
import collections
def main():

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

    ans = ""
    s = []
    for _ in range(N):
        s.append(input())

    d = dict()
    d['A'] = A
    d['B'] = B
    d['C'] = C
    ans = ""
    for i in range(N):
        if d[s[i][0]] == 0 and d[s[i][1]] == 0:
            break
        elif d[s[i][0]] == 0:
            d[s[i][0]] += 1
            d[s[i][1]] -= 1
            ans += s[i][0]
        elif d[s[i][1]] == 0:
            d[s[i][1]] += 1
            d[s[i][0]] -= 1
            ans += s[i][1]
        else:
            if d[s[i][0]] == 1 and d[s[i][1]] == 1 and d['A'] + d['B'] + d['C'] == 2:
                if i < N-1 and s[i] != s[i+1] and s[i][0] in s[i+1]:
                    d[s[i][0]] += 1
                    d[s[i][1]] -= 1
                    ans += s[i][0]
                else:
                    d[s[i][1]] += 1
                    d[s[i][0]] -= 1
                    ans += s[i][1]
            else:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                ans += s[i][0]
    if len(ans) == N:
        print("Yes")
        for c in ans:
            print(c)
    else:
        print("No")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int, input().split())
s=[input() for i in range(n)]
ans=[]
from collections import defaultdict
d = defaultdict(int)
d["a"]=a
d["b"]=b
d["c"]=c
def add(c1,c2):
    global d
    ans.append(c1)
    d[c1]+=1
    d[c2]-=1

for i in range(n):
    que = s[i]
    nxt="NONE"
    if i!=n-1:
        nxt = s[i+1]

    q1,q2=que

    if d[q1]==d[q2]==0:
        print("No")
        exit()
    if d[q1]==d[q2]==1:
        if q1 in nxt:
            add(q1,q2)
        else:
            add(q2,q1)
    elif d[q1]>=d[q2]:
        add(q2,q1)
    else:
        add(q1,q2)

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

Submission status: Wrong Answer

Solution code:
def tvg(n, a, b, c):
    arr = [a, b, c]
    tab = ["A", "B", "C"]
    lkp = {"AB": 0, "BC": 1, "AC": 2}
    plk = ((0,1), (1,2), (0,2))
    lkc = {"A": 0, "B": 1, "C": 2}
    req, rec, stp = [0, 0, 0], [-1] * n, [None] * n
    for i in range(n):
        choice = input()
        rec[i] = lkp[choice]
        req[lkc[choice[0]]] += 1
        req[lkc[choice[1]]] += 1

    def step(i, j, k):
        arr[k] -= 1
        arr[j] += 1
        stp[i] = tab[j]

    for i in range(n):
        j, k = plk[rec[i]]
        
        if arr[j] > arr[k]:
            step(i, k, j)
        elif arr[j] == arr[k] and arr[j] == 1:
            if req[j] > req[k]:
                step(i, j, k)
            else:
                step(i, k, j)
        else:
            step(i, j, k)
        if arr[j] < 0 or arr[k] < 0:
            print("No")
            return
        req[j] -= 1
        req[k] -= 1
    print("Yes")
    for c in stp:
        print(c)
        if not c:
            assert False

if __name__ == "__main__":
    import sys
    n, a, b, c = [int(_) for _ in input().split()]
    tvg(n, a, b, c)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    before=[-1,-1,-1]
    ans=[(-1,-1)]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]]!=-1:
                n[d[s]]+=1
                n[(d[s]-1)%3]-=1
                before[d[s]]=-1
            print('No')
            exit()
        if n[d[s]]==0:
            ans[i]=(d[s],(d[s]+1)%3)
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=((d[s]+1)%3,d[s])
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            ans[i]=(d[s],(d[s]+1)%3)
            before[(d[s]+1)%3]=i
    
    print('Yes')
    d2=['A','B','C']
    for a,b in ans:
        print(d2[a])
                
                    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int, input().split())
command = []
nums = {"A":a, "B":b, "C":c}
for i in range(n):
  command.append(input())

if a+b+c == 0:
  print("No")
  exit()
if a+b+c == 1:
  if nums[command[0][0]] == 0 and nums[command[0][1]] == 0:
    print("No")
    exit()
  print("Yes")
  for i in range(n):
    if nums[command[i][0]] == 0:
      print(command[i][0])
      nums[command[i][0]] = 1
      nums[command[i][1]] = 0
    elif nums[command[i][1]] == 0:
      print(command[i][1])
      nums[command[i][1]] = 1
      nums[command[i][0]] = 0
if a+b+c >= 2:
  if nums[command[0][0]] == 0 and nums[command[0][1]] == 0:
    print("No")
    exit()
  print("Yes")
  for i in range(n):
    if nums[command[i][0]] == 0:
      print(command[i][0])
      nums[command[i][0]] = 1
      nums[command[i][1]] = 0
    elif nums[command[i][1]] == 0:
      print(command[i][1])
      nums[command[i][1]] = 1
      nums[command[i][0]] = 0
    elif a+b+c==2 and nums[command[0][0]] == 1 and nums[command[0][1]] == 1\
    and i < n-1 and command[i] != command[i+1]:
      if command[i][0] in command[i+1]:
        print(command[i][0])
        nums[command[i][0]] = 1
        nums[command[i][1]] = 0
      else:
        print(command[i][1])
        nums[command[i][1]] = 1
        nums[command[i][0]] = 0        
    else:
      print(command[i][0])
      nums[command[i][0]] = 1
      nums[command[i][1]] = 0

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split(' '))
S = [input() for i in range(N)]
ans = []
if A+B+C==0:
    print('No')
else:
    d = {"A":A,"B":B,"C":C}
    for n,i in enumerate(S):
        l,r=list(i)
        L,R=d[l],d[r]
        if L==R==0:
            print('No')
            exit()
        elif L==0:
            ans.append(l)
            d[l] +=1
            d[r] -=1
        elif R==0:
            d[l] -=1
            d[r] +=1
            ans.append(r)
        elif l==r==1 and n != N-1 and i != S[n+1]:
            if l in S[n+1]:
                d[l] +=1
                d[r] -=1
                ans.append(l)
            else:
                d[l] -=1
                d[r] +=1
                ans.apppend(r)
        else:
            ans.append(l)
            d[l] += 1
            d[r] -=1
print('Yes')
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
import sys
n, a, b, c = map(int, input().split())
tempa =0
tempc =0
li_a = []
li_ab = []
li_bc = []
li_c = []
for i in range(n):
    s = input()
    if s == 'AB':
        if b >= 1:
            a+=1
            tempa += 1
            b-=1
            li_a.append(i)
        else:
            if a >= 1:
                a-=1
                b+=1
                li_ab.append(i)
            elif tempc >= 1:
                if c >= 2:
                    tempc -=1
                    c -=2
                    b += 2
                    a+=1
                    b-=1
                    tempa+=1
                    li_a.append(i)
                    k = li_c.pop()
                    li_bc.append(k)
                else:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()
    else:
        if b >= 1:
            c+=1
            tempc += 1
            b-=1
            li_c.append(i)
        else:
            if c >= 1:
                c-=1
                b+=1
                li_bc.append(i)
            elif tempa>=1:
                if a >= 2:
                    tempa -= 1
                    a-=2
                    b+=2
                    c+=1
                    b-=1
                    tempc+=1
                    li_c.append(i)
                    k = li_a.pop()
                    li_ab.append(k)
                else:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()
print('Yes')
for i in range(n):
    if i in li_a:
        print('A')
    elif i in li_ab:
        print('B')
    elif i in li_bc:
        print('B')
    elif i in li_c:
        print('C')
    else:
        print('No')

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split(' '))
S = [input() for i in range(N)]
ans = []
if A+B+C==0:
    print('No')
else:
    d = {"A":A,"B":B,"C":C}
    for n,i in enumerate(S):
        l,r=list(i)
        L,R=d[l],d[r]
        if L==R==0:
            print('No')
            exit()
        elif L==0:
            ans.append(i[0])
            d[l] +=1
            d[r] -=1
            ans.append(l)
        elif R==0:
            d[l] -=1
            d[r] +=1
            ans.append(r)
        elif l==r==1 and n != N-1 and i != S[n+1]:
            if l in S[n+1]:
                d[l] +=1
                d[r] -=1
                ans.append(l)
            else:
                d[l] -=1
                d[r] +=1
                ans.apppend(r)
        else:
            ans.append(l)
            d[l] += 1
            d[r] -=1
print('Yes')
for i in ans:
    print(i)

Submission status: Wrong Answer

Solution code:
import collections
def main():

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

    ans = ""
    s = []
    for _ in range(N):
        s.append(input())

    d = dict()
    d['A'] = A
    d['B'] = B
    d['C'] = C
    ans = ""
    for i in range(N):
        if d[s[i][0]] == 0 and d[s[i][1]] == 0:
            break
        elif d[s[i][0]] == 0:
            d[s[i][0]] += 1
            d[s[i][1]] -= 1
            ans += s[i][0]
        elif d[s[i][1]] == 0:
            d[s[i][1]] += 1
            d[s[i][0]] -= 1
            ans += s[i][1]
        else:
            if d[s[i][0]] == 1 and d[s[i][1]] == 1:
                if i < N-1 and s[i] != s[i+1] and s[i][0] in s[i+1]:
                    d[s[i][0]] += 1
                    d[s[i][1]] -= 1
                    ans += s[i][0]
                else:
                    d[s[i][1]] += 1
                    d[s[i][0]] -= 1
                    ans += s[i][1]
            else:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                ans += s[i][0]
    if len(ans) == N:
        print("Yes")
        for c in ans:
            print(c)
    else:
        print("No")

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
q = [(0, 1) if si == 'AB' else (1, 2) if si == 'BC' else (0, 2) for si in (input() for _ in range(n))]
ans = []
ch = ['A', 'B', 'C']
d = [a, b, c]
tot = sum(d)
if tot == 0:
    print('No')
elif tot == 1:
    for s in q:
        p, q = s
        if d[p] == d[q] == 0:
            print('No')
            break
        elif d[p] == 0:
            d[p], d[q] = 1, 0
            ans.append(ch[p])
        else:
            d[p], d[q] = 0, 1
            ans.append(ch[q])
    else:
        print('Yes', *ans, sep='\n')
else:
    nxta = ['x'] * len(q)
    t = 'x'
    for i in reversed(range(len(q))):
        nxta[i] = t
        if q[i] == 'AB':
            t = 'B'
        elif q[i] == 'AC':
            t = 'C'
    for s, na, snxt in zip(q, nxta, q[1:] + [(1, 2)]):
        p, q = s
        if d[p] == d[q] == 0:
            print('No')
            break
        if p == 0:
            if d[p] == 0:
                d[p], d[q] = d[p] + 1, d[q] - 1
                ans.append(ch[p])
            else:
                if snxt[0] == 0 and s[1] != snxt[1]:
                    d[p], d[q] = d[p] + 1, d[q] - 1
                    ans.append(ch[p])
                else:
                    d[p], d[q] = d[p] - 1, d[q] + 1
                    ans.append(ch[q])
        else:
            if d[p] == 0:
                d[p], d[q] = d[p] + 1, d[q] - 1
                ans.append(ch[p])
            elif d[q] == 0:
                d[p], d[q] = d[p] - 1, d[q] + 1
                ans.append(ch[q])
            elif na == 'B':
                d[p], d[q] = d[p] + 1, d[q] - 1
                ans.append(ch[p])
            else:
                d[p], d[q] = d[p] - 1, d[q] + 1
                ans.append(ch[q])
    else:
        print('Yes', *ans, sep='\n')

Submission status: Wrong Answer

Solution code:
def change(d,ans,X,Y):
    z = {'A': 0, 'B': 1, 'C': 2}
    d[z[X]]+=1
    d[z[Y]]-=1
    ans.append(X)
    return d,ans

n,a,b,c = map(int, input().split())
d = [a,b,c]
q = [input() for i in range(n)]
z = {'A': 0, 'B': 1, 'C': 2}

if sum(d)==0:
    print('No')
elif sum(d)==1:
    ans = []
    for i in range(n):
        X = q[i][0]
        Y = q[i][1]
        if d[z[X]]>0:
            d,ans = change(d,ans,Y,X)
        elif d[z[Y]]>0:
            d,ans = change(d,ans,X,Y)
        else:
            print('No')
            exit()
    print('Yes')
    print('\n'.join(ans))
else:
    ans = []
    X = q[0][0]
    Y = q[0][1]
    if d[z[X]]>0:
        d,ans = change(d,ans,Y,X)
    elif d[z[Y]]>0:
        d,ans = change(d,ans,X,Y)
    else:
        print('No')
        exit()
    for i in range(1,n-1):
        X = q[i][0]
        Y = q[i][1]
        if d[z[X]]==0:
            d,ans = change(d,ans,X,Y)
        elif d[z[Y]]==0:
            d,ans = change(d,ans,Y,X)
        else:
            if X in q[i+1]:
                d,ans = change(d,ans,X,Y)
            if Y in q[i+1]:
                d,ans = change(d,ans,Y,X)
    X = q[-1][0]
    Y = q[-1][1]
    if d[z[X]]>0:
        ans.append(Y)
    else:
        ans.append(X)
    print('Yes')
    print('\n'.join(ans))

Submission status: Wrong Answer

Solution code:
def tvg(n, a, b, c):
    arr = [a, b, c]
    tab = ["A", "B", "C"]
    lkp = {"AB": 0, "BC": 1, "AC": 2}
    plk = ((0,1), (1,2), (0,2))
    lkc = {"A": 0, "B": 1, "C": 2}
    req, rec, stp = [0, 0, 0], [-1] * n, [None] * n
    for i in range(n):
        choice = input()
        rec[i] = lkp[choice]
        req[lkc[choice[0]]] += 1
        req[lkc[choice[1]]] += 1

    for i in range(n):
        j, k = plk[rec[i]]
        
        if arr[j] > arr[k]:
            arr[j] -= 1
            arr[k] += 1
            stp[i] = tab[k]
        elif arr[j] == arr[k] and arr[j] == 1:
            if req[j] > req[k]:
                arr[k] -= 1
                arr[j] += 1
                stp[i] = tab[j]
            else:
                arr[j] -= 1
                arr[k] += 1
                stp[i] = tab[k]
        else:
        # elif arr[j] <= arr[k] and arr[k] != 1:
            arr[k] -= 1
            arr[j] += 1
            stp[i] = tab[j]
        if arr[j] < 0 or arr[k] < 0:
            print("No")
            return
        req[j] -= 1
        req[k] -= 1
    print("Yes")
    for c in stp:
        print(c)


if __name__ == "__main__":
    import sys
    n, a, b, c = [int(_) for _ in input().split()]
    tvg(n, a, b, c)