Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Accepted

Solution code:
n,a,b,c=map(int,input().split())
if(1):
    r=[]
    ff=0
    r1=[]
    for i in range(n):
        s=input().rstrip("\n")
        r1.append(s)
    for i in range(n):
        s=r1[i]
        if(s=='AB'):
            if(a==b==0):
                ff=1
                break
            if(a==0 and b!=0):
                a+=1
                b-=1
                r.append('A')
            elif(b==0 and a!=0):
                b+=1
                a-=1
                r.append('B')
            elif(a==b==1):
                if(i!=n-1):
                    if('A' in s and 'A' in r1[i+1]):
                        a+=1
                        b-=1
                        r.append('A')
                    else:
                        b+=1
                        a-=1
                        r.append('B')
                else:
                    r.append('B')
            else:
                if(a>b):
                    a-=1
                    b+=1
                    r.append('B')
                else:
                    b-=1
                    a+=1
                    r.append('A')
        elif(s=='BC'):
            if(b==c==0):
                ff=1
                break
            if(b==0 and c!=0):
                b+=1
                c-=1
                r.append('B')
            elif(b!=0 and c==0):
                b-=1
                c+=1
                r.append('C')
            elif(c==b==1):
                if(i!=n-1):
                    if('B' in s and 'B' in r1[i+1]):
                        b+=1
                        c-=1
                        r.append('B')
                    else:
                        c+=1
                        b-=1
                        r.append('C')
                else:
                    r.append('C')
            else:
                if(c>b):
                    c-=1
                    b+=1
                    r.append('B')
                else:
                    b-=1
                    c+=1
                    r.append('C')
        else:
            if(a==c==0):
                ff=1
                break
            if(a==0 and c!=0):
                a+=1
                c-=1
                r.append('A')
            elif(c==0 and a!=0):
                c+=1
                a-=1
                r.append('C')
            elif(a==c==1):
                if(i!=n-1):
                    if('A' in s and 'A' in r1[i+1]):
                        a+=1
                        c-=1
                        r.append('A')
                    else:
                        c+=1
                        a-=1
                        r.append('C')
                else:
                    r.append('C')
            else:
                if(c>a):
                    c-=1
                    a+=1
                    r.append('A')
                else:
                    a-=1
                    c+=1
                    r.append('C')
    if(ff):
        print('No')
    else:
        print('Yes')
        for i in r:
            print(i)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N, *ABC = map(int, rl().split())
    s = [rl().rstrip() for _ in range(N)]
    
    if sum(ABC) == 0:
        print('No')
    elif sum(ABC) == 1:
        ans = ['Yes']
        for si in s:
            idx0 = ord(si[0]) - ord('A')
            idx1 = ord(si[1]) - ord('A')
            if ABC[idx0] == 0 and ABC[idx1] == 0:
                print('No')
                return
            if ABC[idx0] == 0:
                ABC[idx0] += 1
                ABC[idx1] -= 1
                ans.append(si[0])
            else:
                ABC[idx0] -= 1
                ABC[idx1] += 1
                ans.append(si[1])
        print(*ans, sep='\n')
    else:
        if ABC[ord(s[0][0]) - ord('A')] == 0 and ABC[ord(s[0][1]) - ord('A')] == 0:
            print('No')
        else:
            ans = ['Yes']
            for i, si in enumerate(s):
                if any(num < 0 for num in ABC):
                    raise Exception
                idx0 = ord(si[0]) - ord('A')
                idx1 = ord(si[1]) - ord('A')
                if ABC[idx0] == 0:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
                elif ABC[idx1] == 0:
                    ABC[idx0] -= 1
                    ABC[idx1] += 1
                    ans.append(si[1])
                elif sum(ABC) == 2 and ABC[idx0] == 1 and ABC[idx1] == 1 and i != N - 1 and si != s[i + 1]:
                    if si[0] in s[i + 1]:
                        ABC[idx0] += 1
                        ABC[idx1] -= 1
                        ans.append(si[0])
                    else:
                        ABC[idx0] -= 1
                        ABC[idx1] += 1
                        ans.append(si[1])
                else:
                    if ABC[idx0] <= ABC[idx1]:
                        ABC[idx0] += 1
                        ABC[idx1] -= 1
                        ans.append(si[0])
                    else:
                        ABC[idx0] -= 1
                        ABC[idx1] += 1
                        ans.append(si[1])
            print(*ans, sep='\n')


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

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

val = [A,B,C]
# 0,0を作ってはいけない
# 基本的には小さいほうに1を足す
# 両方とも1の場合は、次の操作で選ばれるほうに1を足す
# 両方とも次の操作で選ばれるときはどちらに足してもよい

S = [readline().rstrip() for i in range(N)]
ans = []

def get_ind(s):
  res = []
  for c in s:
    if c == "A":
      res.append(0)
    elif c == "B":
      res.append(1)
    elif c == "C":
      res.append(2)
  return res

def get_alpha(x):
  return "ABC"[x]

for i in range(N):
  ind_x,ind_y = get_ind(S[i])
  if val[ind_x] == 0 and val[ind_y] == 0:
    print("No")
    exit(0)
  if val[ind_x] < val[ind_y]:
    ans += [get_alpha(ind_x)]
    val[ind_x] += 1
    val[ind_y] -= 1
  elif val[ind_x] > val[ind_y]:
    ans += [get_alpha(ind_y)]
    val[ind_y] += 1
    val[ind_x] -= 1
  else:
    if i + 1 < N and get_alpha(ind_x) in S[i + 1]:
      ans += [get_alpha(ind_x)]
      val[ind_x] += 1
      val[ind_y] -= 1
    else:
      ans += [get_alpha(ind_y)]
      val[ind_y] += 1
      val[ind_x] -= 1
      
print("Yes")
for a in ans:
  print(a)

Submission status: Accepted

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

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

# 0,0を作ってはいけない
# 基本的には小さいほうに1を足す
# 両方とも1の場合は、次の操作で選ばれるほうに1を足す
# 両方とも次の操作で選ばれるときはどちらに足してもよい

S = [readline().rstrip() for i in range(N)]
ans = []

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

Submission status: Accepted

Solution code:
import numpy as np
def resolve():
    N,A,B,C = map(int,input().split())
    Nlist = []
    YesOrNo = True
    ABC = str(input())
    for i in range(N):
        ABC_old = ABC
        if i < N-1:
            ABC = str(input())

        if ABC_old == "AB":
            if ABC == "AC":
                add1 = np.sign(B - A + 0.1)
            else:
                add1 = np.sign(B - A - 0.1)

            if add1 < 0:
                Nlist.append("B")
            else:
                Nlist.append("A")
            A = A + add1
            B = B - add1
            if A < 0 or B < 0:
                YesOrNo = False
                print('No')
                break
        
        if ABC_old == "AC":
            if ABC == "AB":
                add1 = np.sign(C - A + 0.1)
            else:
                add1 = np.sign(C - A - 0.1)

            if add1 < 0:
                Nlist.append("C")
            else:
                Nlist.append("A")
            A = A + add1
            C = C - add1
            if A < 0 or C < 0:
                YesOrNo = False
                print('No')
                break
        
        if ABC_old == "BC":
            if ABC == "AB":
                add1 = np.sign(C - B + 0.1)
            else:
                add1 = np.sign(C - B - 0.1)

            if add1 < 0:
                Nlist.append("C")
            else:
                Nlist.append("B")
            B = B + add1
            C = C - add1
            if B < 0 or C < 0:
                YesOrNo = False
                print('No')
                break

    if YesOrNo:
        print("Yes")
        s_Nlist = '\n'.join(Nlist)
        print(s_Nlist)
resolve()

Submission status: Accepted

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

n, a, b, c = map(int, input().split())
d = {"A": a, "B": b, "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:
        exit(print("No"))
    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:
n, a, b, c = map(int, input().split())
s = []
for i in range(n):
    si = input()
    s.append(si)

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

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

Submission status: Accepted

Solution code:
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,a,b,c = inm()
s = []
for i in range(n):
    s.append(ins())
ans = []
for i in range(n):
    #ddprint(f"{i=} si {s[i]} {a=} {b=} {c=}")
    if s[i]=='AB':
        if a==b==0:
            print('No')
            exit()
        elif a>b or a==b and i<n-1 and s[i+1]=='BC':
            ans.append('B')
            a -= 1
            b += 1
        else:
            ans.append('A')
            b -= 1
            a += 1
    if s[i]=='AC':
        if a==c==0:
            print('No')
            exit()
        elif a>c or a==c and i<n-1 and s[i+1]=='BC':
            ans.append('C')
            a -= 1
            c += 1
        else:
            ans.append('A')
            c -= 1
            a += 1
    if s[i]=='BC':
        if c==b==0:
            print('No')
            exit()
        elif c>b or c==b and i<n-1 and s[i+1]=='AB':
            ans.append('B')
            c -= 1
            b += 1
        else:
            ans.append('C')
            b -= 1
            c += 1
print('Yes')
for x in ans:
    print(x)

Submission status: Accepted

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

N, A, B, C = map(int, input().split())
s = [input()[:-1] for _ in range(N)]
ans = []

if A+B+C<=1 or A+B+C>=3:
    for si in s:
        if si=='AB':
            if A==0 and B==0:
                print('No')
                exit()
                
            if A<B:
                A += 1
                B -= 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
        elif si=='AC':
            if A==0 and C==0:
                print('No')
                exit()
                
            if A<C:
                A += 1
                C -= 1
                ans.append('A')
            else:
                A -= 1
                C += 1
                ans.append('C')
        else:
            if B==0 and C==0:
                print('No')
                exit()
                
            if B<C:
                B += 1
                C -= 1
                ans.append('B')
            else:
                B -= 1
                C += 1
                ans.append('C')

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

    exit()
    
for i in range(N):
    if s[i]=='AB':
        if A==0 and B==0:
            print('No')
            exit()
            
        if A==0:
            A += 1
            B -= 1
            ans.append('A')
        elif B==0:
            A -= 1
            B += 1
            ans.append('B')
        else:
            if i<N-1 and s[i+1]=='AC':
                A += 1
                B -= 1
                ans.append('A')
            else:
                A -= 1
                B += 1
                ans.append('B')
    elif s[i]=='AC':
        if A==0 and C==0:
            print('No')
            exit()
            
        if A==0:
            A += 1
            C -= 1
            ans.append('A')
        elif C==0:
            A -= 1
            C += 1
            ans.append('C')
        else:
            if i<N-1 and s[i+1]=='AB':
                A += 1
                C -= 1
                ans.append('A')
            else:
                A -= 1
                C += 1
                ans.append('C')
    else:
        if B==0 and C==0:
            print('No')
            exit()
            
        if B==0:
            B += 1
            C -= 1
            ans.append('B')
        elif C==0:
            B -= 1
            C += 1
            ans.append('C')
        else:
            if i<N-1 and s[i+1]=='AB':
                B += 1
                C -= 1
                ans.append('B')
            else:
                B -= 1
                C += 1
                ans.append('C')

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)
input = sys.stdin.readline
N, A, B, C = map(int, input().split())
S = [input().strip("\n").replace("A", "0").replace("B", "1").replace("C", "2") for _ in range(N)]
Ans = [None] * N

def search(i, num):
    if i == N:
        if min(num) >= 0: return True
        else: return False
    if min(num) < 0: return False
    L = [num[0], num[1], num[2]]
    f = S[i][0]
    s = S[i][1]
    L[int(f)] += 1
    L[int(s)] -= 1
    if search(i+1, L):
        Ans[i] = f
        return True
    L[int(f)] -= 2
    L[int(s)] += 2
    if search(i+1, L):
        Ans[i] = s
        return True
    return False

def solve():
    if search(0, [A, B, C]):
        print("Yes")
        for i, a in enumerate(Ans):
            if a == "0": print("A")
            elif a == "1": print("B")
            else: print("C")
    else: print("No")
    #print(Ans)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
s = ["" for i in range(N)]
for i in range(N):
    s[i] = input()
   
valid = True
ans = []
all_sum = A + B + C
if all_sum >= 3:
    for op in s:
        if op == "AB":
            if A > B:
                A -= 1
                B += 1
                ans.append("B")
            else:
                A += 1
                B -= 1
                ans.append("A")
        elif op == "BC":
            if B > C:
                B -= 1
                C += 1
                ans.append("C")
            else:
                B += 1
                C -= 1
                ans.append("B")
        else:
            if A > C:
                A -= 1
                C += 1
                ans.append("C")
            else:
                A += 1
                C -= 1
                ans.append("A")
        if A < 0 or B < 0 or C < 0:
            valid = False
            break
        
    if valid:
        print("Yes")
        for c in ans:
            print(c)
    else:
        print("No")
else:
    for i in range(N):
        op = s[i]
        if op == "AB":
            if A > B:
                A -= 1
                B += 1
                ans.append("B")
            elif A < B:
                A += 1
                B -= 1
                ans.append("A")
            else:
                if i != N - 1 and "A" in s[i + 1]:
                    A += 1
                    B -= 1
                    ans.append("A")
                else:
                    A -= 1
                    B += 1
                    ans.append("B")
        elif op == "AC":
            if A > C:
                A -= 1
                C += 1
                ans.append("C")
            elif A < C:
                A += 1
                C -= 1
                ans.append("A")
            else:
                if i != N - 1 and "A" in s[i + 1]:
                    A += 1
                    C -= 1
                    ans.append("A")
                else:
                    A -= 1
                    C += 1
                    ans.append("C")
        else:
            if B > C:
                B -= 1
                C += 1
                ans.append("C")
            elif B < C:
                B += 1
                C -= 1
                ans.append("B")
            else:
                if i != N - 1 and "B" in s[i + 1]:
                    B += 1
                    C -= 1
                    ans.append("B")
                else:
                    B -= 1
                    C += 1
                    ans.append("C")
        if A < 0 or B < 0 or C < 0:
            valid = False
            break
    if valid:
        print("Yes")
        for c in ans:
            print(c)
    else:
        print("No")

Submission status: Accepted

Solution code:
from collections import deque
import sys
sys.setrecursionlimit(500000)
# N < 10^5, A, B, C < 10^9
# s = ('AB', 'AC', 'BC')
# A >= 0, B >= 0, C >= 0
# N回操作できるか？できるなら道筋を
# 各ステップで2通りのどちらかしか選択できない。
# DPだと思う
# (A, B, C, i, '')

def f(x, dic):
    if x[4] == '':
        return 1
    else:
        if f(dic[x], dic):
            print(x[4])
            return 1

N, A, B, C = [int(i) for i in input().split()]
S = [input() for _ in range(N)]
i = 0
dic = {}
que = deque([(A, B, C, i, '')])
while len(que) != 0:
    x = que.pop()
    i = x[3]
    if x[3] == N:
        print('Yes')
        f(x, dic)
        exit()
    if S[i] == 'AB':
        if x[1] > 0:
            newx = (x[0]+1, x[1]-1, x[2], x[3]+1, 'A')
            dic[newx] = x
            que.append(newx)
        if x[0] > 0:
            newx = (x[0]-1, x[1]+1, x[2], x[3]+1, 'B')
            dic[newx] = x
            que.append(newx)
    elif S[i] == 'BC':
        if x[2] > 0:
            newx = (x[0], x[1]+1, x[2]-1, x[3]+1, 'B')
            dic[newx] = x
            que.append(newx)
        if x[1] > 0:
            newx = (x[0], x[1]-1, x[2]+1, x[3]+1, 'C')
            dic[newx] = x
            que.append(newx)
    elif S[i] == 'AC':
        if x[2] > 0:
            newx = (x[0]+1, x[1], x[2]-1, x[3]+1, 'A')
            dic[newx] = x
            que.append(newx)
        if x[0] > 0:
            newx = (x[0]-1, x[1], x[2]+1, x[3]+1, 'C')
            dic[newx] = x
            que.append(newx)
    else:
        print('error')
print('No')

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache
from collections import defaultdict
from collections import deque
inf = float('inf')
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**6)
def input(): return sys.stdin.readline().rstrip()
def read():
  return int(readline())
def reads():
  return map(int, readline().split())
n,a,b,c=reads()
lsAB=deque()
lsBC=deque()
lsAC=deque()
dic=defaultdict(int)
k=-1
lsS=[]
for i in range(n):
  s=input()
  lsS.append(s)
  if "AB" in s:
    lsAB.append(i)
    a-=1
    b-=1
  if "BC" in s:
    lsBC.append(i)
    b-=1
    c-=1
  if "AC" in s:
    lsAC.append(i)
    c-=1
    a-=1
  if a<0:
    a+=2
    try:
      hoge=lsAB[0]
    except:hoge=inf
    try:
      hoge2=lsAC[0]
    except:hoge2=inf   
    #print(hoge,hoge2)
    if min(hoge,hoge2)>i:
      print("No")
      exit()
    if hoge<hoge2:
        dic[hoge]=0
        lsAB.popleft()
    else:
        dic[hoge2]=0
        lsAC.popleft()
  if b<0:
    b+=2
    try:
      hoge=lsAB[0]
    except:hoge=inf   
    try:
      hoge2=lsBC[0]
    except:hoge2=inf   
    if min(hoge,hoge2)>i:
      print("No")
      exit()
    if hoge<hoge2:
        dic[hoge]=1
        lsAB.popleft()
    else:
        dic[hoge2]=0
        lsBC.popleft()
  if c<0:
    c+=2
    try:
      hoge=lsBC[0]
    except:hoge=inf
    try:
      hoge2=lsAC[0]
    except:hoge2=inf   
    #print(hoge,hoge2)
    if min(hoge,hoge2)>i:
      print("No")
      exit()
    if hoge<hoge2:
        dic[hoge]=1
        lsBC.popleft()
    else:
        dic[hoge2]=1
        lsAC.popleft()
print("Yes")
for i in range(n):
  print(lsS[i][dic[i]])

Submission status: Accepted

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

flg = True
D = []
pat = {'AB':[0,1], 'BC':[1,2], 'AC':[0,2]}

for i in range(N):
  l = input()
  D.append(pat[l])
  
for i in range(N):
  ai, bi = D[i]
  an, bn = D[i+1] if i < N-1 else [0,1] # nanndemoyoi

  if X[ai] == 0 and X[bi] == 0:
    flg = False
    break
  elif X[ai] == 0:
    ANS.append(ai)
    X[ai] += 1
    X[bi] -= 1
  elif X[bi] == 0:
    ANS.append(bi)
    X[bi] += 1
    X[ai] -= 1
  else:
    if ai == an or ai == bn:
      pl, mi = ai, bi
    else:
      pl, mi = bi, ai
    # 先読みで使う方
    ANS.append(pl)
    X[pl] += 1
    X[mi] -= 1
      
if not flg:
  print('No')
else:
  print('Yes')
  dd = {0:'A', 1:'B', 2:'C'}
  for ans in ANS:
    print(dd[ans])

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline
def inpS(): return inp().rstrip().decode()
readlines = sys.stdin.buffer.readlines 
 

def resolve():
    N, A, B, C = map(int, inp().split())
    S = [inpS() for _ in range(N)]
 
    ans = []
    def dfs(now, a, b, c):
        if now == N:
            return True
        if S[now] == "AB":
            if a == 0 and b == 0:
                return False
            if 0 < a and dfs(now+1, a-1, b+1, c):
                ans.append("B")
                return True
            if 0 < b and dfs(now+1, a+1, b-1, c):
                ans.append("A")
                return True
            return False
        if S[now] == "AC":
            if a == 0 and c == 0:
                return False
            if 0 < a and dfs(now+1, a-1, b, c+1):
                ans.append("C")
                return True
            if 0 < c and dfs(now+1, a+1, b, c-1):
                ans.append("A")
                return True
            return False
        if S[now] == "BC":
            if c == 0 and b == 0:
                return False
            if 0 < b and dfs(now+1, a, b-1, c+1):
                ans.append("C")
                return True
            if 0 < c and dfs(now+1, a, b+1, c-1):
                ans.append("B")
                return True
            return False
 
    dfs(0, A, B, C)
    if len(ans) == 0:
        print("No")
        return
    else:
        print("Yes")
        [print(a) for a in ans[::-1]]
 
 
if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
def operate(S,A,B,C,N):
    OP = []
    for i in range(N):
        if S[i] == "AB":
            if A == 0 and B == 0:
                return -1
            else:
                if A == 1 and B == 1 and i < N-1:
                    if S[i+1] == "BC":
                        OP.append("B")
                        A,B = A-1,B+1
                    else:
                        OP.append("A")
                        A,B = A+1,B-1
                elif A >= B:
                    OP.append("B")
                    A,B = A-1,B+1
                else:
                    OP.append("A")
                    A,B = A+1,B-1
        if S[i] == "BC":
            if B == 0 and C == 0:
                return -1
            else:
                if B == 1 and C == 1 and i < N-1:
                    if S[i+1] == "AB":
                        OP.append("B")
                        B,C = B+1,C-1
                    else:
                        OP.append("C")
                        B,C = B-1,C+1
                elif B >= C:
                    OP.append("C")
                    B,C = B-1,C+1
                else:
                    OP.append("B")
                    B,C = B+1,C-1
        if S[i] == "AC":
            if A == 0 and C == 0:
                return -1
            else:
                if A == 1 and C == 1 and i < N-1:
                    if S[i+1] == "BC":
                        OP.append("C")
                        A,C = A-1,C+1
                    else:
                        OP.append("A")
                        A,C = A+1,C-1
                elif C >= A:
                    OP.append("A")
                    C,A = C-1,A+1
                else:
                    OP.append("C")
                    C,A = C+1,A-1
    return OP

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

    ans = operate(S,A,B,C,N)
    if ans != -1:
        print("Yes")
        for i in range(N):
            print(ans[i])
    else:
        print("No")

if __name__ == "__main__":
    main()