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:
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))

#################

N,A,B,C = LI()
s = ['0']*N
for i in range(N):
    s[i] = str(input())

d = defaultdict(int)
d['A'] = A
d['B'] = B
d['C'] = C

def plus_minus(s,t):
    for c in ['A','B','C']:
        if (c in s) and (c in t):
            plus = c
    if s[0] == plus:
        minus = s[1]
    else:
        minus = s[0]
    return plus, minus 

def min_max(s):
    if d[s[0]] >= d[s[1]]:
        return s[1],s[0]
    else:
        return s[0],s[1]

if A+B+C == 0:
    print('No')
elif A+B+C == 1:
    ans = []
    for i in range(N):
        if d[s[i][0]] == d[s[i][1]] == 0:
            print('No')
            exit()
        else:
            if d[s[i][0]]:
                d[s[i][0]] -= 1
                d[s[i][1]] += 1
                ans.append(s[i][1])
            else:
                d[s[i][1]] -= 1
                d[s[i][0]] += 1
                ans.append(s[i][0])
    print('Yes')
    for a in ans:
        print(a)
else:
    if d[s[0][0]] == d[s[0][1]] == 0:
        print('No')
    else:
        print('Yes')
        for i in range(N):
            if d[s[i][0]] == 0:
                d[s[i][0]] += 1
                d[s[i][1]] -= 1
                print(s[i][0])
            elif d[s[i][1]] == 0:
                d[s[i][1]] += 1
                d[s[i][0]] -= 1
                print(s[i][1])
            elif d[s[i][0]] == d[s[i][1]] == 1:
                if i != N-1:
                    p,m = plus_minus(s[i],s[i+1])
                    d[p] += 1
                    d[m] -= 1
                    print(p)
                else:
                    d[s[i][0]] += 1
                    d[s[i][1]] -= 1
                    print(s[i][0])
            else:
                min_,max_ = min_max(s[i])
                d[min_] += 1
                d[max_] -= 1
                print(min_)

Submission status: Accepted

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

flag = 0
ans = []
S = A+B+C

if S >= 3:
    for i in range(N):
        if s[i] == "AB":
            if A+B <= 0:
                flag = 1
                break
            if A >= B:
                ans.append("B")
                B += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                B -= 1

        if s[i] == "AC":
            if A+C <= 0:
                flag = 1
                break
            if A >= C:
                ans.append("C")
                C += 1
                A -= 1
            else:
                ans.append("A")
                A += 1
                C -= 1

        if s[i] == "BC":
            if B+C <= 0:
                flag = 1
                break
            if B >= C:
                ans.append("C")
                C += 1
                B -= 1
            else:
                ans.append("B")
                B += 1
                C -= 1
    if flag == 1:
        print("No")
    else:
        print("Yes")
        for i in ans:
            print(i)

else:
    
    for i in range(N):
        #print(A,B,C)
        if s[i] == "AB":
            if A+B <= 0:
                flag = 1
                break
            if B == 0:
                ans.append("B")
                B += 1
                A -= 1
            elif A == 0:
                ans.append("A")
                A += 1
                B -= 1
            else:
                if s[i+1] == "AC":
                    ans.append("A")
                    A += 1
                    B -= 1
                else:
                    ans.append("B")
                    B += 1
                    A -= 1

        if s[i] == "AC":
            if A+C <= 0:
                flag = 1
                break
            if C == 0:
                ans.append("C")
                C += 1
                A -= 1
            elif A == 0:
                ans.append("A")
                A += 1
                C -= 1
            else:
                if s[i+1] == "BC":
                    ans.append("C")
                    C += 1
                    A -= 1
                else:
                    ans.append("A")
                    A += 1
                    C -= 1

        if s[i] == "BC":
            if B+C <= 0:
                flag = 1
                break
            if C == 0:
                ans.append("C")
                C += 1
                B -= 1
            elif B == 0:
                ans.append("B")
                B += 1
                C -= 1
            else:
                if s[i+1] == "AC":
                    ans.append("C")
                    C += 1
                    B -= 1
                else:
                    ans.append("B")
                    B += 1
                    C -= 1

    if flag == 1:
        print("No")
    else:
        print("Yes")
        for i in ans:
            print(i)

Submission status: Accepted

Solution code:
import sys

alpha = 'ABC'
N, A, B, C = map(int, input().split())
S = []
for s in sys.stdin.readlines():
    s = list(s)
    i, j, _ = s
    S.append((alpha.index(i), alpha.index(j)))

num = [A, B, C]


ope = []
for idx, (i, j) in enumerate(S):
    for k in range(3):
        if k not in (i, j):
            break

    if num[i] == 0 and num[j] == 0:
        ans = 'No'
        break
    
    if num[i] == 1 and num[j] == 1 and num[k] == 0 and idx < N - 1:
        if i in S[idx + 1]:
            plus, minus = i, j
        else:
            plus, minus = j, i
    else:
        if num[i] < num[j]:
            plus, minus = i, j
        else:
            plus, minus = j, i
    
    num[plus] += 1
    num[minus] -= 1
    ope.append(alpha[plus])
    # print(idx, num)

else:
    ans = 'Yes'

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

Submission status: Accepted

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]
            elif d[s[i][0]] < d[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]

    if len(ans) == N:
        print("Yes")
        for c in ans:
            print(c)
    else:
        print("No")

if __name__ == '__main__':
    main()

Submission status: Accepted

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: Accepted

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

s = [0] * N
for i in range(N):
  s[i] = str(input())
  
d = {"AB":(0, 1, 2), "BC": (2, 1, 0), "AC": (0, 2, 1)}  
dl = ["A", "B", "C"]  

if ABC == [0, 0, 0]:
  print("No")
  quit()

ans = [0] * N  
if A + B + C == 1:
  for n in range(N):
    i, j, k = d[s[n]]
    #print(i, j, k)
    if ABC[i] == 1:
      ABC[i] = 0
      ABC[j] = 1
      ans[n] = dl[j]
    elif ABC[j] == 1:
      ABC[i] = 1
      ABC[j] = 0
      ans[n] = dl[i]
    else:
      print("No")
      quit()
  print("Yes")
  for i in range(N):
    print(ans[i])
    
else:
  for n in range(N):
    i, j, k = d[s[n]]
    #print(i, j, k)
    if ABC[i] == 0:
      if ABC[j] == 0:
        print("No")
        quit()
      else:
        ABC[i] += 1
        ABC[j] -= 1
        ans[n] = dl[i]
    elif ABC[j] == 0:
      ABC[i] -= 1
      ABC[j] += 1
      ans[n] = dl[j]
    else:
      if n < N - 1:
        if (ABC[i] == 1) or (ABC[j] == 1):
          if s[n] != s[n + 1]:
            n1, n2, n3 = d[s[n + 1]]
            if (i == n1) or (i == n2):
              ABC[i] += 1
              ABC[j] -= 1
              ans[n] = dl[i]
            else:
              ABC[i] -= 1
              ABC[j] += 1
              ans[n] = dl[j]
              
          else:
            ABC[i] -= 1
            ABC[j] += 1
            ans[n] = dl[j]
        else:
          ABC[i] -= 1
          ABC[j] += 1
          ans[n] = dl[j]
      else:
        ABC[i] -= 1
        ABC[j] += 1
        ans[n] = dl[j]
        
  print("Yes")
  for i in range(N):
    print(ans[i])

Submission status: Accepted

Solution code:

itos = ["A", "B", "C"]

def swap(ABC, f, t):
    ABC[f] -= 1
    ABC[t] += 1
    return itos[t]


def main():
    N, A, B, C = map(int, input().split())
    S = [input() for i in range(N)]
    ABC = [A, B, C]
    StoP = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
    itos = ["A", "B", "C"]
    L = []
    for i in range(N):
        x, y = StoP[S[i]]

        if ABC[x] == 0 and ABC[y] == 0:
            print("No")
            return
        if ABC[x] == 1 and ABC[y] == 1 and i < N - 1 and S[i] != S[i+1]:
            x2, y2 = StoP[S[i+1]]
            if x in [x2, y2]:
                L.append(swap(ABC, y, x))
            else:
                L.append(swap(ABC, x, y))
        elif ABC[x] < ABC[y]:
            L.append(swap(ABC, y, x))
        else:
            L.append(swap(ABC, x, y))

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

Submission status: Accepted

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

n, a, b, c = map(int, input().split())
count = {
    'A': a,
    'B': b,
    'C': c
}
queries = []
for _ in range(n):
    query = tuple(input().replace('\n', ''))
    queries.append(query)


def solve(plus, minus):
    count[plus] += 1
    count[minus] -= 1
    ans.append(plus)


ans = []
for idx, (x, y) in enumerate(queries):
    if count[x] == count[y] == 0:
        print('No')
        exit()

    if count[x] == count[y] == 1:
        if idx == n-1:
            solve(x, y)
            break

        nex = queries[idx+1]
        if x in nex:
            solve(x, y)
        else:
            solve(y, x)

    elif count[x] >= count[y]:
        solve(y, x)

    else:
        solve(x, y)

print('Yes')
for c in ans:
    print(c)

Submission status: Accepted

Solution code:
def f(A, i, j, k, nex,res):
    if A[i] == A[j] == 0:
        print("No")
        exit()
    elif A[i] == A[j] == 1 and A[k] == 0:
        if i in nex:
            A[i] += 1
            A[j] -= 1
            return "ABC"[i]
        else:
            A[i] -= 1
            A[j] += 1
            return "ABC"[j]
    else:
        A[i] += 1
        A[j] -= 1
        return "ABC"[i]
    

def main():
    N, A, B, C = [int(_n) for _n in input().split()]
    S = []
    for i in range(N):
        s = input()
        S.append(s)
    if [A,B,C].count(0) == 3:
        print("No")
        return
    res = ""
    arr = [A,B,C]
    S.append("")
    for i,s in enumerate(S[:-1]):
        n = []
        if S[i+1] == "AB":
            n = [0,1]
        if S[i+1] == "BC":
            n = [2,1]
        if S[i+1] == "AC":
            n = [0,2]
        if s == "AB":
            if arr[0] > arr[1]:
                res += f(arr, 1, 0, 2, n,res)
            else:
                res += f(arr,0,1,2,n,res)
        elif s == "BC":
            if arr[1] > arr[2]:
                res += f(arr, 2, 1, 0, n,res)
            else:
                res += f(arr,1,2,0,n,res)
        elif s == "AC":
            if arr[0] > arr[2]:
                res += f(arr, 2,0, 1, n,res)
            else:
                res += f(arr,0,2,1,n,res)
    print("Yes")
    for s in res:
        print(s)
main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1<<30)
d = 'ABC'
N, *v = map(int, input().split())
e = [tuple(map(d.index, input())) for _ in range(N)]
p = []
def f(i):
	if i == len(e):
		print('Yes')
		print('\n'.join(p))
		raise SystemExit
	k, j = e[i]
	for a, s in ((k, j), (j, k)):
		if v[s] == 0: continue
		v[a] += 1
		v[s] -= 1
		p.append(d[a])
		f(i+1)
		v[a] -= 1
		v[s] += 1
		p.pop()
f(0)
print('No')

Submission status: Accepted

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

n,a,b,c = map(int,input().split())
ans = []
abc = [a,b,c]

d = {'AB\n':(0,1),'AC\n':(0,2),'BC\n':(1,2)}

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

if(a+b+c >= 3)|(a+b+c==1):
    for _ in range(n):
        s = input()
        x,y = d[s]
        if(abc[x]==0)&(abc[y]==0):
            print('No')
            exit()
        if(abc[x] < abc[y]):
            x,y = y,x
        abc[x] -= 1
        abc[y] += 1
        ans.append(y)

elif(a+b+c==2):
    ss = [input() for _ in range(n)]
    if(max(abc)==2):
        #(2,0,0)
        case = True
    else:
        case = False

    for i in range(n):
        s = ss[i]
        x,y = d[s]
        if(case)|(i==n-1):
            if(abc[x]==0)&(abc[y]==0):
                print('No')
                exit()
            if(abc[x] < abc[y]):
                x,y = y,x
            abc[x] -= 1
            abc[y] += 1
            case = False
        else:
            if(abc[x]!=abc[y]):
                if(abc[x] < abc[y]):
                    x,y = y,x
                abc[x] -= 1
                abc[y] += 1
                case = False
            else:
                sn = ss[i+1]
                if(x in d[sn]):
                    x,y = y,x
                abc[x] -= 1
                abc[y] += 1
                case = True
        ans.append(y)

print('Yes')
for i in ans:
    print(chr(i+ord('A')))

Submission status: Accepted

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

def add(c1,c2):
    global a,b,c
    ans.append(c1)
    if c1=="A":
        a+=1
    if c1=="B":
        b+=1
    if c1=="C":
        c+=1
    if c2=="A":
        a-=1
    if c2=="B":
        b-=1
    if c2=="C":
        c-=1

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

    if que=="AB":
        if a==b==0:
            print("No")
            exit()
        if a==b==1:
            if nxt=="NONE":
                add("A","B")
            elif "A" in nxt:
                add("A","B")
            else:
                add("B","A")
        elif a>=b:
            add("B","A")
        else:
            add("A","B")
    elif que=="AC":
        if a==c==0:
            print("No")
            exit()
        if a==c==1:
            if nxt=="NONE":
                add("A","C")
            elif "A" in nxt:
                add("A","C")
            else:
                add("C","A")
        elif a>=c:
            add("C","A")
        else:
            add("A","C")
    else: # BC
        if b==c==0:
            print("No")
            exit()
        if b==c==1:
            if nxt=="NONE":
                add("B","C")
            elif "B" in nxt:
                add("B","C")
            else:
                add("C","B")
        elif b>=c:
            add("C","B")
        else:
            add("B","C")

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

Submission status: Accepted

Solution code:
n,a,b,c = map(int, input().split())
d = {}
d["A"] = a
d["B"] = b
d["C"] = c

pl = [input() for _ in range(n)]

res = []
for i in range(n):
    p = pl[i]
    if d[p[0]]==d[p[1]]==0:
        print("No")
        exit()
    elif d[p[0]]==0:
        res.append(p[0])
        d[p[0]] += 1
        d[p[1]] -= 1
    elif d[p[1]]==0:
        res.append(p[1])
        d[p[0]] -= 1
        d[p[1]] += 1
    elif i==n-1:
        res.append(p[0])
    elif p[0] in pl[i+1]:
        res.append(p[0])
        d[p[0]] += 1
        d[p[1]] -= 1
    else:
        res.append(p[1])
        d[p[0]] -= 1
        d[p[1]] += 1

    



print("Yes")
for p in res:
    print(p)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1<<30)

idx = 'ABC'
N, *vals = map(int, input().split())
evs = [tuple(map(idx.index, input())) for _ in range(N)]
path = []

def f(i):
	if i == len(evs):
		print('Yes')
		print('\n'.join(path))
		raise SystemExit

	k, j = evs[i]
	for add, sub in ((k, j), (j, k)):
		if vals[sub] == 0: continue
		vals[add] += 1
		vals[sub] -= 1
		path.append(idx[add])

		f(i+1)

		vals[add] -= 1
		vals[sub] += 1
		path.pop()

f(0)

print('No')

Submission status: Accepted

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
depth = 0
while depth < N:
    key1, key2 = inputs[depth]
    if dic[key1] < dic[key2]:
        dic[key1] += 1
        dic[key2] -= 1
        ans[depth] = key1
    elif dic[key1] > dic[key2]:
        dic[key2] += 1
        dic[key1] -= 1
        ans[depth] = key2
    else:
        if not dic[key1]:
            break
        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
        else:
            dic[key1] += 1
            dic[key2] -= 1
            ans[depth] = key1
    depth += 1
else:
    print("Yes")
    print(*ans, sep="\n")
    quit()
print("No")

Submission status: Accepted

Solution code:
import sys
import math
from decimal import Decimal, ROUND_HALF_UP, ROUND_HALF_EVEN
from collections import deque
def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def S(): return sys.stdin.readline().rstrip()
def LS(): return sys.stdin.readline().split()
def LS2(N): return [sys.stdin.readline().split() for i in range(N)]
def FILL(i,h): return [i for j in range(h)]
def FILL2(i,h,w): return [FILL(i,w) for j in range(h)]
def FILL3(i,h,w,d): return [FILL2(i,w,d) for j in range(h)]
def FILL4(i,h,w,d,d2): return [FILL3(i,w,d,d2) for j in range(h)]
def sisha(num,digit): return Decimal(str(num)).quantize(Decimal(digit),rounding=ROUND_HALF_UP)
#'0.01'や'1E1'などで指定、整数に戻すならintをかます
MOD = 1000000007
INF = float("inf")
sys.setrecursionlimit(10**5+10)
#input = sys.stdin.readline
N,A,B,C = MI()
s = LS2(N)
abc = [A,B,C]
dic = {'A':0,'B':1,'C':2}
redic = {0:'A',1:'B',2:'C'}
f=0
select = []
for i in range(N):
    order = s[i][0]
    x = dic[order[0]]
    y = dic[order[1]]
    xnum=abc[x]
    ynum=abc[y]
    if xnum>ynum:
        select.append(y)
        abc[x] -= 1
        abc[y] += 1
    elif xnum==ynum:
        if i<N-1:
            next = s[i+1][0]
            nextx = dic[next[0]]
            nexty = dic[next[1]]
            if x==nextx or x==nexty:
                select.append(x)
                abc[x] += 1
                abc[y] -= 1
            elif y==nextx or y==nexty:
                select.append(y)
                abc[y] += 1
                abc[x] -= 1
            else:
                print(00)
        else:
            select.append(x)
            abc[x] += 1
            abc[y] -= 1
    else:
        select.append(x)
        abc[x] += 1
        abc[y] -= 1
    for i in abc:
        if i<0:
            print('No')
            f=1
            break
    if f==1:
        break
else:
    print('Yes')
    [print(redic[x]) for x in select]

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,a,b,c = list(map(int,input().split()))
abc = [a,b,c]
s = [tuple(map(lambda x:ord(x)-97,list(input().lower()))) for i in range(n)]

from copy import deepcopy

s.append(deepcopy(s[-1]))
ret = True
ret_choice = []
ra = ret_choice.append
for i,j in zip(s,s[1::]):
    #i[0]で+1,i[1]で-1をする場合
    if abc[i[0]] >=3  and abc[i[1]] >=3:
        abc[i[0]]+=1
        abc[i[1]]-=1
        ra(i[0])
    elif abc[i[0]] >=3 and abc[i[1]] < 3:
        abc[i[0]]-=1
        abc[i[1]]+=1
        ra(i[1])
    elif abc[i[0]] <3 and abc[i[1]] >= 3:
        abc[i[0]]+=1
        abc[i[1]]-=1
        ra(i[0])
    elif abc[i[0]]==0:
        abc[i[0]]+=1
        abc[i[1]]-=1
        ra(i[0])
    elif abc[i[1]]==0:
        abc[i[0]]-=1
        abc[i[1]]+=1
        ra(i[1])
    elif i[0] in j:
        abc[i[0]]+=1
        abc[i[1]]-=1
        ra(i[0])
    else:
        abc[i[0]]-=1
        abc[i[1]]+=1
        ra(i[1])
    if -1 in abc:
        ret = False
        break

if ret:
    print("Yes")
    for i in (map(lambda x:chr(x+65),ret_choice)):
        print(i)
else:
    print("No")

Submission status: Accepted

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




S=[0]*N
for i in range(N):
    s=input()
    S[i]=s
memo=[0]*N
for i in range(N):  
    if S[i]=="AB":
        if A==0 and B==0:
            print("No")
            exit()
        if A==1 and B==1:
            if i+1<N:
                if S[i+1]=="BC" and C==0:
                    B+=1
                    A-=1
                    memo[i]="B"
                    continue
                elif S[i+1]=="AC" and C==0:
                    A+=1
                    B-=1
                    memo[i]="A"
                    continue
            A+=1
            B-=1
            memo[i]="A"
        elif A>B:
            A-=1
            B+=1
            memo[i]="B"
        elif B>A:
            A+=1
            B-=1
            memo[i]="A"
        else:
            A+=1
            B-=1
            memo[i]="A"



    if S[i]=="AC":
        if A==0 and C==0:
            print("No")
            exit()
        if A==1 and C==1:
            if i+1<N:
                if S[i+1]=="BC" and B==0:
                    C+=1
                    A-=1
                    memo[i]="C"
                    continue
                elif S[i+1]=="AB" and B==0:
                    A+=1
                    C-=1
                    memo[i]="A"
                    continue
            A+=1
            C-=1
            memo[i]="A"
        elif A>C:
            A-=1
            C+=1
            memo[i]="C"
        elif C>A:
            A+=1
            C-=1
            memo[i]="A"
        else:
            A+=1
            C-=1
            memo[i]="A"

    if S[i]=="BC":
        if C==0 and B==0:
            print("No")
            exit()
        if B==1 and C==1:
            if i+1<N:
                if S[i+1]=="AC" and A==0:
                    C+=1
                    B-=1
                    memo[i]="C"
                    continue
                elif S[i+1]=="AB" and A==0:
                    B+=1
                    C-=1
                    memo[i]="B"
                    continue
            B+=1
            C-=1
            memo[i]="B"
        elif B>C:
            B-=1
            C+=1
            memo[i]="C"
        elif C>B:
            B+=1
            C-=1
            memo[i]="B"
        else:
            B+=1
            C-=1
            memo[i]="B"

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

Submission status: Accepted

Solution code:
import sys
def input():
	return sys.stdin.readline()[:-1]

def included(x):
	if x == "AB":
		return 0, 1
	elif x == "BC":
		return 1, 2
	else:
		return 0, 2

def ans(x):
	l = ["A", "B", "C"]
	return l[x]

n, a, b, c = map(int, input().split())
nums = [a, b, c]
s = [input() for _ in range(n)]
res = []
for i, x in enumerate(s):
	y, z = included(x)
	if nums[y] == nums[z] == 0:
		print("No")
		break
	if i == n-1:
		if nums[y] >= nums[z]:
			res.append(ans(z))
		elif nums[z] > nums[y]:
			res.append(ans(y))
	else:
		if nums[y] > nums[z]:
			res.append(ans(z))
			nums[y] -= 1
			nums[z] += 1
		elif nums[z] > nums[y]:
			res.append(ans(y))
			nums[z] -= 1
			nums[y] += 1
		elif z in included(s[i+1]):
			res.append(ans(z))
			nums[y] -= 1
			nums[z] += 1
		else:
			res.append(ans(y))
			nums[z] -= 1
			nums[y] += 1
else:
	print("Yes")
	print(*res, sep="\n")

Submission status: Accepted

Solution code:

N, a, b, c, *S = open(0).read().split()
N, a, b, c = map(int, (N, a, b, c))

flg = a*b*c == 0 and a+b+c == 2

ans = []
for i, s in enumerate(S):
    if s == 'AB':
        if flg and a*b == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            elif s_ == 'BC':
                ans.append('B')
                a -= 1
                b += 1
                continue

        ans.append('B' if a > b else 'A')
        if a > b:
            a -= 1
            b += 1
        else:
            a += 1
            b -= 1

    elif s == 'AC':
        if flg and a*c == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            elif s_ == 'BC':
                ans.append('C')
                a -= 1
                c += 1
                continue

        ans.append('C' if a > c else 'A')
        if a > c:
            a -= 1
            c += 1
        else:
            a += 1
            c -= 1

    elif s == 'BC':
        if flg and b*c == 1 and i < N-1:
            s_ = S[i+1]
            if s_ == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
            elif s_ == 'AC':
                ans.append('C')
                b -= 1
                c += 1
                continue

        ans.append('C' if b > c else 'B')
        if b > c:
            b -= 1
            c += 1
        else:
            b += 1
            c -= 1

    if a < 0 or b < 0 or c < 0:
        print('No')
        exit()


print('Yes')
[print(r) for r in ans]

Submission status: Accepted

Solution code:
#!python3

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


def main():
    l = [A, B, C]
    d = {"AB": (0, 1, 2), "AC": (0, 2, 1), "BC": (1, 2, 0)}
    dl = ["A", "B", "C"]
    ans = []
    for n in range(N):
        i, j, k = d[S[n]]
        if l[i] < l[j]:
            l[i] += 1
            l[j] -= 1
            ans.append(dl[i])
        elif l[i] > l[j]:
            l[i] -= 1
            l[j] += 1
            ans.append(dl[j])
        elif l[i] == 0:
            print("No")
            return
        else:
            if n < N - 1 and j == d[S[n + 1]][2]:
                i, j = j, i
            l[i] -= 1
            l[j] += 1
            ans.append(dl[j])
        
    print("Yes")
    print("\n".join(ans))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1<<30)

idx = 'ABC'
N, *vals = map(int, input().split())
evs = [tuple(map(idx.index, input())) for _ in range(N)]

def f(i, vals, path):
	if i == len(evs):
		print('Yes')
		print('\n'.join(path))
		raise SystemExit

	k, j = evs[i]
	for add, sub in ((k, j), (j, k)):
		if vals[sub] == 0: continue
		vals[add] += 1
		vals[sub] -= 1
		path.append(idx[add])

		f(i+1, vals, path)

		vals[add] -= 1
		vals[sub] += 1
		path.pop()

f(0, vals, [])

print('No')

Submission status: Accepted

Solution code:
from sys import stdin, setrecursionlimit

setrecursionlimit(10 ** 9)


def input():
    return stdin.readline().strip()


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


def dfs(index, hp):
    if index == N:
        return ''

    s1, s2 = strings[index]
    if hp[s1] > 0:
        hp2 = hp.copy()
        hp2[s1] -= 1
        hp2[s2] += 1
        ret = dfs(index + 1, hp2)
        if ret != False:
            return  s2 + ret
    if hp[s2] > 0:
        hp2 = hp.copy()
        hp2[s2] -= 1
        hp2[s1] += 1
        ret = dfs(index + 1, hp2)
        if ret != False:
            return s1 + ret

    return False


d = {'A': A, 'B': B, 'C': C}
ans = dfs(0, d)
if ans == False:
    print('No')
else:
    print('Yes')
    for c in ans:
        print(c)