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
input = lambda: sys.stdin.readline().rstrip()
D = {"AB": 2, "AC": 1, "BC": 0}
N, a, b, c = map(int, input().split())
X = [a, b, c]
S = [D[input()] for _ in range(N)]
ANS = []
if max(X) == 0:
    print("No")
    exit()

if sum(X) == 1:
    t = 0 if a else 1 if b else 2
    for s in S:
        if X[s]:
            print("No")
            exit()
        t ^= s ^ 3
        ANS.append(t)
        X[s-1] ^= 1
        X[s-2] ^= 1
    print("Yes")
    print("\n".join([chr(t+65) for t in ANS]))
    exit()

s = S[0]
if X[s-1] + X[s-2] == 0:
    print("No")
    exit()

for i, s in enumerate(S):
    if X[s-1] > X[s-2]:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    elif X[s-1] < X[s-2]:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1
    elif i == N - 1 or S[i+1] == (s-1) % 3:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    else:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1

print("Yes")
print("\n".join([chr(t+65) for t in ANS]))

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from itertools import accumulate, combinations, permutations, product
from math import gcd
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n,a,b,c=map(int,input().split())
s=[input() for i in range(n)]
# print(s)
abc=[a,b,c]
dic={"A":0,"B":1,"C":2}

anslst=[]

for i in range(n-1):
    tmp=0
    v=dic[s[i][0]]
    w=dic[s[i][1]]
    x=dic[s[i+1][0]]
    y=dic[s[i+1][1]]
    # どっちも0は終了
    if abc[v]==abc[w]==0:
        print("No")
        quit()
    # 片方が0なのでもう片方から引く
    if abc[v]==0:
        anslst.append(s[i][0])
        abc[v]+=1
        abc[w]-=1
    elif abc[w]==0:
        anslst.append(s[i][1])
        abc[v]-=1
        abc[w]+=1
    # 今回と次が同じだから平均化
    elif s[i]==s[i+1]:
        if abc[v]>abc[w]:
            anslst.append(s[i][1])
            abc[v]-=1
            abc[w]+=1
        else:
            anslst.append(s[i][0])
            abc[v]+=1
            abc[w]-=1
    # 今回と次Aが出るからAに足す
    elif v==x:
        anslst.append(s[i][0])
        abc[v]+=1
        abc[w]-=1
    # 今回と次Cが出るからCに足す
    elif w==y:
        anslst.append(s[i][1])
        abc[v]-=1
        abc[w]+=1
    # 今回と次Bが出るからBに足す
    else:
        if v==y:
            anslst.append(s[i][0])
            abc[v]+=1
            abc[w]-=1
        else:
            anslst.append(s[i][1])
            abc[v]-=1
            abc[w]+=1
    # print(abc)

if abc[dic[s[-1][0]]]==abc[dic[s[-1][1]]]==0:
    print("No")
    quit()
else:
    if abc[dic[s[-1][0]]]>abc[dic[s[-1][1]]]:
        anslst.append(s[-1][1])
    else:
        anslst.append(s[-1][0])
    
print("Yes")
for i in range(n):
    print(anslst[i])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,a,b,c = list(map(int,input().split()))
ls = [""]*n
moji = [""]*n
for i in range(n):
    moji[i] = input()
for i in range(n):
    s = moji[i]
    if s == "AB":
        if a > b:
            a -= 1;b+=1
            ls[i] = 'B'
        elif a < b or i == n-1:
            a += 1;b-=1
            ls[i] = 'A'
        else:
            if moji[i+1] == "AC":
                b -= 1;a += 1
                ls[i] = "A"
            else:
                a -= 1;b+=1
                ls[i] = "B"
    if s == "BC":
        if c > b:
            c -= 1;b+=1
            ls[i] = 'B'
        elif c < b or i == n-1:
            c += 1;b-=1
            ls[i] = 'C'
        else:
            if moji[i+1] == "AC":
                b -= 1;c += 1
                ls[i] = "C"
            else:
                c -= 1;b+=1
                ls[i] = "B"

    if s == "AC":
        if a > c:
            a -= 1;c+=1
            ls[i] = 'C'
        elif a < c or n-1 == i:
            a += 1;c-=1
            ls[i] = 'A'
        else:
            if moji[i+1] == "AB":
                c -= 1;a += 1
                ls[i] = "A"
            else:
                a -= 1;c+=1
                ls[i] = "C"
    if a < 0 or b < 0 or c < 0:break

if a < 0 or b < 0 or c < 0:
    print("No")
else:
    print("Yes")
    for i in range(n):
        print(ls[i])

Submission status: Accepted

Solution code:
#import sys
#input = sys.stdin.readline
def one(D, S):
    d = {"A":0, "B":1, "C":2}
    ANS = []
    for a, b in S:
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        else:
            D[x] -= 1
            D[y] += 1
            ANS.append(b)
    print("Yes")
    print("\n".join(ANS))
            
def two(D,S,N):
    d = {"A":0, "B":1, "C":2}
    e = {0:"A", 1:"B",2:"C"}
    ANS = []
    for i in range(N):
        a, b = S[i]
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        elif D[y] == 0: 
            D[x] -= 1
            D[y] += 1
            ANS.append(b)
        else: #(1,1)
            if i+1 > N-1:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
                continue
            if S[i] == S[i+1]:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
                continue
            if not a in S[i+1]:
                x,y = y, x
                a, b = b,a
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
    print("Yes")
    print("\n".join(ANS))
    
def main():
    N, A, B, C = map( int, input().split())
    S = [ tuple( input()) for _ in range(N)]
    d = {"A":0, "B":1, "C":2}
    D = [A,B,C]
    ANS = []
    if sum(D) == 1:
        one(D, S)
        return
    if sum(D) == 2:
        two(D,S,N)
        return
    for a, b in S:
        x = d[a]
        y = d[b]
        if D[x] == 0 and D[y] == 0:
            print("No")
            return
        if D[x] == 0:
            D[x] += 1
            D[y] -= 1
            ANS.append(a)
        elif D[x] == 1:
            if D[y] == 0:
                D[x] -= 1
                D[y] += 1
                ANS.append(b)
            else:
                D[x] += 1
                D[y] -= 1
                ANS.append(a)
        else:
            D[x] -= 1
            D[y] += 1
            ANS.append(b)

    print("Yes")
    print("\n".join(ANS))
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def no():
    print("No")
    exit()

n, a, b, c = map(int, input().split())
ans = []
s = [input() for _ in range(n)]
for i in range(n):
    if s[i] == "AB":
        if a == b == 1 and not i == n - 1:
            if (not c == 0) or (s[i + 1] == "AB"):
                a += 1
                b -= 1
                ans.append("A")
            else:
                if s[i + 1] == "AC":
                    a += 1
                    b -= 1
                    ans.append("A")
                else:
                    b += 1
                    a -= 1
                    ans.append("B")
        elif a + b > 0:
            if a <= b:
                a += 1
                b -= 1
                ans.append("A")
            else:
                b += 1
                a -= 1
                ans.append("B")
        else:
            no()
    elif s[i] == "BC":
        if b == c == 1 and not i == n - 1:
            if (not a == 0) or (s[i + 1] == "BC"):
                b += 1
                c -= 1
                ans.append("B")
            else:
                if s[i + 1] == "AB":
                    b += 1
                    c -= 1
                    ans.append("B")
                else:
                    c += 1
                    b -= 1
                    ans.append("C")
        elif b + c > 0:
            if b <= c:
                b += 1
                c -= 1
                ans.append("B")
            else:
                c += 1
                b -= 1
                ans.append("C")
        else:
            no()
    elif s[i] == "AC":
        if c == a == 1 and not i == n - 1:
            if (not b == 0) or (s[i + 1] == "AC"):
                c += 1
                a -= 1
                ans.append("C")
            else:
                if s[i + 1] == "BC":
                    c += 1
                    a -= 1
                    ans.append("C")
                else:
                    a += 1
                    c -= 1
                    ans.append("A")
        elif c + a > 0:
            if c <= a:
                c += 1
                a -= 1
                ans.append("C")
            else:
                a += 1
                c -= 1
                ans.append("A")
        else:
            no()
print("Yes")
for i in ans:
    print(i)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


L = []
ans = "Yes"

if A == 0 and B == 0 and C == 0:
    ans = "No"
    
for i in range(N):
    s = S[i]
    if s == "AB":
        if A < B:
            A += 1
            B -= 1
            L.append("A")
        elif A > B:
            A -= 1
            B += 1
            L.append("B")
        else:
            if i < N-1 and S[i+1] == "BC":
                B += 1
                A -= 1
                L.append("B")
            else:
                A += 1
                B -= 1
                L.append("A")
    elif s == "BC":
        if B < C:
            B += 1
            C -= 1
            L.append("B")
        elif B > C:
            B -= 1
            C += 1
            L.append("C")
        else:
            if i < N-1 and S[i+1] == "AB":
                B += 1
                C -= 1
                L.append("B")
            else:
                C += 1
                B -= 1
                L.append("C")
    else:
        if A < C:
            A += 1
            C -= 1
            L.append("A")
        elif A > C:
            A -= 1
            C += 1
            L.append("C")
        else:
            if i < N-1 and S[i+1] == "BC":
                C += 1
                A -= 1
                L.append("C")
            else:
                A += 1
                C -= 1
                L.append("A")
    if A*B*C < 0:
        ans = "No"

        
print(ans)
if ans == "Yes":
    for l in L:
        print(l)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,a,b,c = inpl()
a = [a,b,c]
s = [input() for _ in range(n)]
res = []
for i,x in enumerate(s):
    if x == 'AB':
        x,y = 0,1
    elif x == 'AC':
        x,y = 0,2
    else:
        x,y = 1,2
    if a[x] > a[y]:
        a[x] -= 1
        a[y] += 1
        res.append(y)
    elif a[x] < a[y]:
        a[x] += 1
        a[y] -= 1
        res.append(x)
    elif a[x] == a[y] == 1 and i != n-1:
        nx,ny = list(s[i+1])
        nx = ord(nx) - ord('A')
        ny = ord(ny) - ord('A')
        if (x,y) == (nx,ny):
            a[x] -= 1
            a[y] += 1
            res.append(y)
        elif x == nx:
            a[nx] += 1
            a[y] -= 1
            res.append(nx)
        elif y == nx:
            a[nx] += 1
            a[x] -= 1
            res.append(nx)
        elif x == ny:
            a[ny] += 1
            a[y] -= 1
            res.append(ny)
        else:
            a[ny] += 1
            a[x] -= 1
            res.append(ny)
    else:
        a[x] -= 1
        a[y] += 1
        res.append(y)
    if a[x] < 0 or a[y] < 0:
        print('No')
        quit()
print('Yes')
for i,x in enumerate(res):
    print(chr(ord('A') + x))

Submission status: Accepted

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

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

ans = []
tower = [a,b,c]
s2num = {"AB":[0,1],"BC":[1,2],"AC":[0,2]}
num2s = {0:"A",1:"B",2:"C"}
flag =True

def check_large(l,r,t):
    if t[l]>t[r]:
        return l,r,False
    elif t[l]<t[r]:
        return r,l,False
    else:
        return l,r,True

for i in range(n):
    s = ss[i]
    l,r = s2num[s]
    big,small,f = check_large(l,r,tower)
    if f:
        if i+1 < n:
            nex = s2num[ss[i+1]]
            if big in nex:
                small,big = big,small

    if tower[big] <= 0:
        flag = False
        break
    else:
        tower[big]-=1
        tower[small]+=1
        ans.append(num2s[small])

if flag:
    print("Yes")
    for i in range(n):
        print(ans[i])
else:
    print("No")

Submission status: Accepted

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

n, a, b, c = map(int, input().split())
s = []
f = []
d = dict()
d['A'] = a
d['B'] = b
d['C'] = c
flag = 1

for _ in range(n):
    f.append(input().rstrip())

for x in range(n):
    e = f[x]

    if d[e[0]] == d[e[1]] == 1 and x < n - 1:
        if e[0] in f[x + 1] and e[1] not in f[x + 1]:
            s.append(e[0])
            d[e[1]] -= 1
            d[e[0]] += 1
        else:
            s.append(e[1])
            d[e[0]] -= 1
            d[e[1]] += 1

    elif d[e[0]] >= d[e[1]]:
        s.append(e[1])
        d[e[0]] -= 1
        d[e[1]] += 1
    else:
        s.append(e[0])
        d[e[1]] -= 1
        d[e[0]] += 1

    if d[e[0]] < 0 or d[e[1]] < 0:
        flag = 0
        break

if flag == 0:
    print("No")
else:
    print("Yes")
    print("\n".join(s))

Submission status: Accepted

Solution code:
from collections import deque
from copy import copy

n, a, b, c = map(int, input().split())
s = [input().rstrip() for _ in range(n)]

m = {'A': a, 'B': b, 'C': c}
ans = []
for i in range(n):
    if m[s[i][0]] > m[s[i][1]]:
        ans.append(s[i][1])
        m[s[i][0]] -= 1
        m[s[i][1]] += 1
    elif m[s[i][0]] < m[s[i][1]]:
        ans.append(s[i][0])
        m[s[i][0]] += 1
        m[s[i][1]] -= 1
    else:
        if m[s[i][0]] == 0:
            print('No')
            exit()
        if i != n-1:
            if s[i][0] in s[i+1]:
                ans.append(s[i][0])
                m[s[i][0]] += 1
                m[s[i][1]] -= 1
            else:
                ans.append(s[i][1])
                m[s[i][0]] -= 1
                m[s[i][1]] += 1
        else:
            ans.append(s[i][0])

print('Yes')
print('\n'.join(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
D = {"AB": 2, "AC": 1, "BC": 0}
N, a, b, c = map(int, input().split())
X = [a, b, c]
S = [D[input()] for _ in range(N)]
ANS = []
if max(X) == 0 or X[S[0]-1] + X[S[0]-2] == 0:
    print("No")
    exit()

if sum(X) == 1:
    t = 0 if a else 1 if b else 2
    for s in S:
        if X[s]:
            print("No")
            exit()
        t ^= s ^ 3
        ANS.append(t)
        X[s-1] ^= 1
        X[s-2] ^= 1
else:
    for i, s in enumerate(S):
        if X[s-1] > X[s-2] or X[s-1] == X[s-2] and i < N - 1 and S[i+1] == (s-1) % 3:
            ANS.append((s-2) % 3)
            X[s-1] -= 1
            X[s-2] += 1
        else:
            ANS.append((s-1) % 3)
            X[s-1] += 1
            X[s-2] -= 1

print("Yes")
print("\n".join([chr(t+65) for t in ANS]))

Submission status: Accepted

Solution code:
def func(S,life):
  #life = life
  ans_chr = []
  for i in range(len(S)):
    
    s = S[i]
    if life[s[0]] > life[s[1]]:
      life[s[0]]-=1
      life[s[1]]+=1
      ans_chr.append(s[1])
      #print(life)
      continue
    elif life[s[0]] < life[s[1]]:
      life[s[1]]-=1
      life[s[0]]+=1
      ans_chr.append(s[0])
      #print(life)
      continue
    else:
      if life[s[0]] ==0 and  life[s[1]] ==0:
        return ["No"]
      else:
        if i==len(S)-1:
          ans_chr.append(s[0])
          continue
        else:
          if s[0] in S[i+1]:
            life[s[1]]-=1
            life[s[0]]+=1
            ans_chr.append(s[0])
            #print(life)
          else:
            life[s[0]]-=1
            life[s[1]]+=1
            ans_chr.append(s[1])
            #print(life)
  return ["Yes",ans_chr]
     
      
            
          

lst = list(map(int,input().split()))
N = lst[0]
A,B,C = lst[1:]
life = {"A":A,"B":B,"C":C}
S = []
for i in range(N):
  s = input()
  S.append(s)
ans = func(S,life)
if ans[0]=="No":
  print("No")
else:
  print("Yes")
  for c in ans[1]:
    print(c)

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
abc = {'A': a, 'B': b, 'C': c}
s = [input() for _ in range(n)]
ans = []
for i in range(n):
    s1, s2 = s[i][0], s[i][1]

    if abc[s1] == abc[s2] == 0:
        print('No')
        exit()
    elif abc[s1] == abc[s2] == 1 and i < n-1:
        if s[i+1] == s1 + s2:
            ans += [s1]
            abc[s1] += 1
            abc[s2] -= 1
        elif s1 in s[i+1]:
            ans += [s1]
            abc[s1] += 1
            abc[s2] -= 1
        else:
            ans += [s2]
            abc[s2] += 1
            abc[s1] -= 1
    else:
        if abc[s1] <= abc[s2]:
            ans += [s1]
            abc[s1] += 1
            abc[s2] -= 1
        else:
            ans += [s2]
            abc[s2] += 1
            abc[s1] -= 1

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