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

s=[]
ans=[]
 
def dfs(a,b,c,i,root):
    if i==n:
        global ans
        ans=root.copy()
        return True
    l=False
    r=False
    if s[i]=='AB':
        if a>0:
            root.append('B')
            l=dfs(a-1,b+1,c,i+1,root)
            root.pop()
        if not l and b>0:
            root.append('A')
            r=dfs(a+1,b-1,c,i+1,root)
            root.pop()
    elif s[i]=='AC':
        if a>0:
            root.append('C')
            l=dfs(a-1,b,c+1,i+1,root)
            root.pop()
        if not l and c>0:
            root.append('A')
            r=dfs(a+1,b,c-1,i+1,root)
            root.pop()
    else:
        if b>0:
            root.append('C')
            l=dfs(a,b-1,c+1,i+1,root)
            root.pop()
        if not l and c>0:
            root.append('B')
            r=dfs(a,b+1,c-1,i+1,root)
            root.pop()
    return l or r
 
sys.setrecursionlimit(10**6)
n,a,b,c=map(int,input().split())
s=['']*n
for i in range(n):
    s[i]=input()
flag=dfs(a,b,c,0,[])
 
if flag:
    print("Yes")
    for t in ans:
        print(t)
else:
    print("No")

Submission status: Accepted

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

ans=[]
 
flag=True
 
for i in range(n):
    if s[i]=='AB':
        if a==0 and b==0:
            flag=False
            break
        if a==0 or (b!=0 and a==1 and s[i+1]=='AC'):
            ans.append('A')
            a+=1
            b-=1
        else:
            ans.append('B')
            a-=1
            b+=1
    elif s[i]=='AC':
        if a==0 and c==0:
            flag=False
            break
        if a==0 or (c!=0 and a==1 and s[i+1]=='AB'):
            ans.append('A')
            a+=1
            c-=1
        else:
            ans.append('C')
            a-=1
            c+=1
    else:
        if c==0 and b==0:
            flag=False
            break
        if c==0 or (b!=0 and c==1 and s[i+1]=='AC'):
            ans.append('C')
            c+=1
            b-=1
        else:
            ans.append('B')
            c-=1
            b+=1

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

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
s = [input() for _ in range(n)]
c2v = {'A':a,'B':b,'C':c}
sum_v = sum(c2v.values())
if sum_v == 0:
    print('No')
    exit()
if sum_v == 1:
    ans = []
    for ss in s:
        if c2v[ss[0]] == 0 and c2v[ss[1]] == 0:
            print('No')
            exit()
        elif c2v[ss[0]] == 0 and c2v[ss[1]] == 1:
            ans.append(ss[0])
            c2v[ss[0]] += 1
            c2v[ss[1]] -= 1
        elif c2v[ss[0]] == 1 and c2v[ss[1]] == 0:
            ans.append(ss[1])
            c2v[ss[1]] += 1
            c2v[ss[0]] -= 1
    print('Yes')
    for ans_i in ans:
        print(ans_i)

if sum_v >= 2:
    ans = []
    for ss_i,ss in enumerate(s):
        if c2v[ss[0]] == 0 and c2v[ss[1]] == 0:
            print('No')
            exit()
        elif c2v[ss[0]] == 0 and c2v[ss[1]] >= 1:
            ans.append(ss[0])
            c2v[ss[0]] += 1
            c2v[ss[1]] -= 1
        elif c2v[ss[0]] >= 1 and c2v[ss[1]] == 0:
            ans.append(ss[1])
            c2v[ss[1]] += 1
            c2v[ss[0]] -= 1
        else: # どっちから引いてもとりあえず大丈夫な場合は次のターンでダメな状態、（0,0）に陥らないように次に出てくる方に加算する。
            if ss_i+1 == n: # 最後だから適当でいい
                ans.append(ss[0])
                c2v[ss[0]] += 1
                c2v[ss[1]] -= 1
                continue
            next_ss = s[ss_i+1]
            if ss[0] in next_ss:
                ans.append(ss[0])
                c2v[ss[0]] += 1
                c2v[ss[1]] -= 1
            else:
                ans.append(ss[1])
                c2v[ss[1]] += 1
                c2v[ss[0]] -= 1
    print('Yes')
    for ans_i in ans:
        print(ans_i)

Submission status: Accepted

Solution code:
from itertools import combinations

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

def sol():
    now = {'A': A, 'B': B, 'C': C}
    ret = []
    for i, s in enumerate(S):
        u, v = s[0], s[1]
        if now[u] == now[v] == 0:
            return []
        if now[u] > now[v]:
            ret.append(v)
            now[u] -= 1
            now[v] += 1
            continue
        if now[u] < now[v]:
            ret.append(u)
            now[u] += 1
            now[v] -= 1
            continue
        if i == N - 1:
            ret.append(u)
            continue

        nx = S[i + 1]
        if u in nx:
            ret.append(u)
            now[u] += 1
            now[v] -= 1
            continue
        if v in nx:
            ret.append(v)
            now[v] += 1
            now[u] -= 1
            continue

    return ret

ans = sol()
if len(ans) == 0:
    print('No')
    exit()

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

Submission status: Accepted

Solution code:
from itertools import combinations

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

cnt = {'A': A, 'B': B, 'C': C}

def out(T):
    now = {'A': A, 'B': B, 'C': C}

    ans = []
    for i, s in enumerate(S):
        u, v = s[0], s[1]
        if now[u] == now[v] == 0:
            return []
        if now[u] > now[v]:
            ans.append(v)
            now[u] -= 1
            now[v] += 1
            continue
        if now[u] < now[v]:
            ans.append(u)
            now[u] += 1
            now[v] -= 1
            continue
        if i == N - 1:
            ans.append(u)
            continue

        nx = S[i + 1]
        if u in nx:
            ans.append(u)
            now[u] += 1
            now[v] -= 1
            continue
        if v in nx:
            ans.append(v)
            now[v] += 1
            now[u] -= 1
            continue

    return ans

ans = out('')
if len(ans) == 0:
    print('No')
    exit()
print('Yes')
print(*ans, sep='\n')

# for s in combinations(['', '', 'A', 'B', 'C'], r=3):
#     s = ''.join(s)
#     for t in s:
#         if cnt[t] == 0:
#             break
#     else:
#         ans = out(s)
#         if len(ans) == 0:
#             continue
#         print('Yes')
#         print(*ans, sep='\n')
#         exit()

# print('No')

Submission status: Accepted

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.append(r)
        else:
            if L>=R:
                d[l] -=1
                d[r] +=1
                ans.append(r)
            else:
                d[l] +=1
                d[r] -=1
                ans.append(l)
    print('Yes')
    for i in ans:
        print(i)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,a,b,c=map(int,input().split())
s=[]
for i in range(n):
    sub=input().rstrip()
    s.append(sub)
if a+b+c==0:
    print('No')
    sys.exit()
if a+b+c==1:
    com=[]
    for i in s:
        if i=='AB':
            if a==0 and b==0:
                print('No')
                sys.exit()
            else:
                if a>b:
                    com.append('B')
                else:
                    com.append('A')
                a,b=b,a
        elif i=='AC':
            if a==0 and c==0:
                print('No')
                sys.exit()
            else:
                if a>c:
                    com.append('C')
                else:
                    com.append('A')
                a,c=c,a
        elif i=='BC':
            if b==0 and c==0:
                print('No')
                sys.exit()
            else:
                if b>c:
                    com.append('C')
                else:
                    com.append('B')
                b,c=c,b
    print('Yes')
    for i in com:
        print(i)
    sys.exit()
if (s[0]=='AB' and a**2+b**2==0) or (s[0]=='AC' and a**2+c**2==0) or (s[0]=='BC' and c**2+b**2==0):
    print('No')
else:
    print('Yes')
    for i in range(n):
        check=True
        if s[i]=='AB':
            at=a+1
            bt=b-1
            ct=c
            if bt<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('A')
            else:
                a-=1
                b+=1
                print('B')
        elif s[i]=='AC':
            at=a+1
            bt=b
            ct=c-1
            if ct<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('A')
            else:
                a-=1
                c+=1
                print('C')
        elif s[i]=='BC':
            at=a
            bt=b+1
            ct=c-1
            if ct<0:
                check=False
            if i<n-1:
                if (s[i+1]=='AB' and at**2+bt**2==0) or (s[i+1]=='AC' and at**2+ct**2==0) or (s[i+1]=='BC' and ct**2+bt**2==0):
                    check=False
            if check:
                a,b,c=at,bt,ct
                print('B')
            else:
                b-=1
                c+=1
                print('C')

Submission status: Accepted

Solution code:
# F

N, A, B, C = list(map(int, input().split()))
d = {'A': A, 'B': B, 'C': C}
res = []
SN = [input() for i in range(N)]
for i in range(N):
    s = SN[i]
    s0 = s[0]
    s1 = s[1]
    
    if d[s0] == d[s1] == 0:
        print('No')
        exit()
    elif d[s0] == 0:
        d[s0] += 1
        d[s1] -= 1
        res.append(s0)
    elif d[s1] == 0:
        d[s0] -= 1
        d[s1] += 1
        res.append(s1)
    elif i == N-1:
        res.append(s0)
    elif s0 in SN[i+1]:
        d[s0] += 1
        d[s1] -= 1
        res.append(s0)
    else:
        d[s0] -= 1
        d[s1] += 1
        res.append(s1)
        
print('Yes')
for r in res:
    print(r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def main():
    N, A, B, C = [int(v) for v in input().split()]

    total = A + B + C

    if total == 0:
        print('No')
        exit()

    choice = []
    if total != 2:
        for _ in range(N):
            s = sys.stdin.readline().rstrip()
            if s == 'AB':
                if A == 0 and B == 0:
                    print('No')
                    exit()
                if A < B:
                    B -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    B += 1
                    choice.append('B')
            elif s == 'BC':
                if B == 0 and C == 0:
                    print('No')
                    exit()
                if B < C:
                    C -= 1
                    B += 1
                    choice.append('B')
                else:
                    B -= 1
                    C += 1
                    choice.append('C')
            elif s == 'AC':
                if A == 0 and C == 0:
                    print('No')
                    exit()
                if A < C:
                    C -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    C += 1
                    choice.append('C')
    else:
        s = sys.stdin.readline().rstrip()
        for _ in range(N - 1):
            s1 = sys.stdin.readline().rstrip()
            if s == 'AB':
                if A == 0 and B == 0:
                    print('No')
                    exit()
                if A < B:
                    B -= 1
                    A += 1
                    choice.append('A')
                elif A > B:
                    A -= 1
                    B += 1
                    choice.append('B')
                elif s1 == 'AC':
                    A += 1
                    B -= 1
                    choice.append('A')
                else:
                    A -= 1
                    B += 1
                    choice.append('B')

            elif s == 'BC':
                if B == 0 and C == 0:
                    print('No')
                    exit()
                if B < C:
                    C -= 1
                    B += 1
                    choice.append('B')
                elif B > C:
                    B -= 1
                    C += 1
                    choice.append('C')
                elif s1 == 'AC':
                    B -= 1
                    C += 1
                    choice.append('C')
                else:
                    C -= 1
                    B += 1
                    choice.append('B')

            elif s == 'AC':
                if A == 0 and C == 0:
                    print('No')
                    exit()
                if A < C:
                    C -= 1
                    A += 1
                    choice.append('A')
                elif A > C:
                    A -= 1
                    C += 1
                    choice.append('C')
                elif s1 == 'AB':
                    C -= 1
                    A += 1
                    choice.append('A')
                else:
                    A -= 1
                    C += 1
                    choice.append('C')
            s = s1

        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                exit()
            if A < B:
                B -= 1
                A += 1
                choice.append('A')
            else:
                A -= 1
                B += 1
                choice.append('B')

        elif s == 'BC':
            if B == 0 and C == 0:
                print('No')
                exit()
            if B < C:
                C -= 1
                B += 1
                choice.append('B')
            else:
                B -= 1
                C += 1
                choice.append('C')

        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                exit()
            if A < C:
                C -= 1
                A += 1
                choice.append('A')
            else:
                A -= 1
                C += 1
                choice.append('C')

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


main()

Submission status: Accepted

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

u = ["A","B","C"]
ss = ["BC","AC","AB"]
t = [A,B,C]
trace = []
ans = "Yes"
for i in range(N):
  idx = ss.index(s[i])
  if t[(idx+1)%3] == 0 and t[(idx+2)%3] == 0:
    ans = "No"
    break
  elif t[(idx+2)%3] == 0:
    t[(idx+1)%3] -= 1
    t[(idx+2)%3] += 1
    trace.append(u[(idx+2)%3])
  elif t[(idx+1)%3] == 0:
    t[(idx+1)%3] += 1
    t[(idx+2)%3] -= 1
    trace.append(u[(idx+1)%3])
  elif i+1<N and s[i+1] == ss[(idx+1)%3]:
    t[(idx+1)%3] -= 1
    t[(idx+2)%3] += 1
    trace.append(u[(idx+2)%3])
  else:
    t[(idx+1)%3] += 1
    t[(idx+2)%3] -= 1
    trace.append(u[(idx+1)%3])
print(ans)
if ans == "Yes":
  for v in trace:
    print(v)

Submission status: Accepted

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]] = nums[command[i][0]] +1
      nums[command[i][1]] = nums[command[i][1]] -1
    elif nums[command[i][1]] == 0:
      print(command[i][1])
      nums[command[i][1]] = nums[command[i][1]]+1
      nums[command[i][0]] = nums[command[i][0]]-1
    elif a+b+c==2 and nums[command[i][0]] == 1 and nums[command[i][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]] = nums[command[i][0]]+1
        nums[command[i][1]] = nums[command[i][1]]-1
      else:
        print(command[i][1])
        nums[command[i][1]] = nums[command[i][1]]+1
        nums[command[i][0]] = nums[command[i][0]]-1       
    else:
      if nums[command[i][0]] < nums[command[i][1]]:
        print(command[i][0])
        nums[command[i][0]] = nums[command[i][0]]+1
        nums[command[i][1]] = nums[command[i][1]]-1
      else:
        print(command[i][1])
        nums[command[i][1]] = nums[command[i][1]]+1
        nums[command[i][0]] = nums[command[i][0]]-1

Submission status: Accepted

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]]==d[z[Y]]==0:
        print('No')
        exit()
    for i in range(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)
            elif 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: Accepted

Solution code:
#人生終わり
#解説解答
n,a,b,c = map(int,input().split())
if a + b + c == 0:
  print('No')
  exit()
elif a + b + c == 1:
  ans = []
  for _ in range(n):
    q = input()
    if q == 'AB':
      if a + b == 0:
        print('No')
        exit()
      a,b = b,a
      if a:
        ans.append('A')
      else:
        ans.append('B')
    elif q == 'BC':
      if b + c == 0:
        print('No')
        exit()
      b,c = c,b
      if b:
        ans.append('B')
      else:
        ans.append('C')
    else:
      if c + a == 0:
        print('No')
        exit()
      c,a = a,c
      if a:
        ans.append('A')
      else:
        ans.append('C')
  print('Yes')
  for x in ans:
    print(x)
  exit()
p = []
for _ in range(n):
  p.append(input())
if a + b == 0 and p[0] == 'AB':
    print('No')
    exit()
if b + c == 0 and p[0] == 'BC':
    print('No')
    exit()
if a + c == 0 and p[0] == 'AC':
    print('No')
    exit()
print('Yes')
for i in range(n):
  if p[i] == 'AB':
    if a == 0:
      a += 1
      b -= 1
      print('A')
    elif b == 0:
      a -= 1
      b += 1
      print('B')
    elif i != n-1:
      if p[i+1] == 'BC':
        a -= 1
        b += 1
        print('B')
      else:
        a += 1
        b -= 1
        print('A')
    else:
      a += 1
      b -= 1
      print('A')
  elif p[i] == 'BC':
    if b == 0:
      b += 1
      c -= 1
      print('B')
    elif c == 0:
      b -= 1
      c += 1
      print('C')
    elif i != n-1:
      if p[i+1] == 'AC':
        b -= 1
        c += 1
        print('C')
      else:
        b += 1
        c -= 1
        print('B')
    else:
      b += 1
      c -= 1
      print('B')
  else:
    if a == 0:
      a += 1
      c -= 1
      print('A')
    elif c == 0:
      a -= 1
      c += 1
      print('C')
    elif i != n-1:
      if p[i+1] == 'BC':
        a -= 1
        c += 1
        print('C')
      else:
        a += 1
        c -= 1
        print('A')
    else:
      a += 1
      c -= 1
      print('A')

Submission status: Accepted

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 i in range(n):
    s = sl[i]
    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 n-1 == i:
                ansl.append("A")
                a += 1
                b -= 1
            elif sl[i+1] == "AC":
                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 n-1 == i:
                ansl.append("B")
                b += 1
                c -= 1
            elif  sl[i+1] == "AB":
                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 n-1 == i:
                ansl.append("C")
                c += 1
                a -= 1
            elif sl[i+1] == "BC":
                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: Accepted

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

v = [A, B, C]
id_d = {'A':0, 'B':1, 'C':2}
ans = [0] * N
for i, s in enumerate(S):
  s1, s2 = s
  id1, id2 = id_d[s1], id_d[s2]
  v1, v2 = v[id1], v[id2]
  if v1 == v2 == 0:
    if i == 0:
      print('No')
      quit()
    else:
      pre_s1, pre_s2 = S[i-1]
      pre_id1, pre_id2 = id_d[pre_s1], id_d[pre_s2]
      if pre_s1 in [s1, s2]:
        v[pre_id1] += 2
        v[pre_id2] -= 2
        ans[i-1] = pre_s1
        if v[pre_id2] < 0:
          print('No')
          quit()
      else: # pre_s2 in [s1, s2]
        v[pre_id2] += 2
        v[pre_id1] -= 2
        ans[i-1] = pre_s2
        if v[pre_id1] < 0:
          print('No')
          quit()
  v1, v2 = v[id1], v[id2]
  if v1 >= v2:
    ans[i] = s2
    v[id1] -= 1
    v[id2] += 1
  else:
    ans[i] = s1
    v[id1] += 1
    v[id2] -= 1

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

Submission status: Accepted

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

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

res = []
for i, p in enumerate(pl):
    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
def input():
    return sys.stdin.readline().strip()
n, a, b, c = map(int, input().split())
s = []
for _ in range(n):
    i = input()
    if i == 'AB':
        s.append(2)
    elif i == 'AC':
        s.append(1)
    else:
        s.append(0)

# 'No'の処理
if a == 0 and b == 0 and c == 0:
    print('No')
    exit()

if a == 0 and b == 0:
    if s[0] == 2:
        print('No')
        exit()
if a == 0 and c == 0:
    if s[0] == 1:
        print('No')
        exit()
if b == 0 and c == 0:
    if s[0] == 0:
        print('No')
        exit()

ans = []
if a + b + c == 1:
    for i in range(n):
        if s[i] == 0:
            if b > c:
                ans.append('C')
                b -= 1
                c += 1
            else:
                ans.append('B')
                b += 1
                c -= 1

        elif s[i] == 1:
            if a > c:
                ans.append('C')
                a -= 1
                c += 1
            else:
                ans.append('A')
                a += 1
                c -= 1

        else:
            if a > b:
                ans.append('B')
                a -= 1
                b += 1
            else:
                ans.append('A')
                a += 1
                b -= 1
        if a < 0 or b < 0 or c < 0:
            print('No')
            exit()
    
    print('Yes')
    for i in ans:
        print(i)
    exit()

# 0 <= i < n-1
print('Yes')
for i in range(n-1):
    if s[i] == 0:
        if b > c:
            print('C')
            b -= 1
            c += 1
        elif b < c:
            print('B')
            b += 1
            c -= 1
        elif s[i+1] == 2:
            print('B')
            b += 1
            c -= 1
        else:
            print('C')
            b -= 1
            c += 1

    elif s[i] == 1:
        if a > c:
            print('C')
            a -= 1
            c += 1
        elif a < c:
            print('A')
            a += 1
            c -= 1
        elif s[i+1] == 0:
            print('C')
            a -= 1
            c += 1
        else:
            print('A')
            a += 1
            c -= 1

    else:
        if a > b:
            print('B')
            a -= 1
            b += 1
        elif a < b:
            print('A')
            a += 1
            b -= 1
        elif s[i+1] == 1:
            print('A')
            a += 1
            b -= 1
        else:
            print('B')
            a -= 1
            b += 1

# i = n-1
if s[n-1] == 0:
    if b > c:
        print('C')
        b -= 1
        c += 1
    else:
        print('B')
        b += 1
        c -= 1

elif s[n-1] == 1:
    if a > c:
        print('C')
        a -= 1
        c += 1
    else:
        print('A')
        a += 1
        c -= 1

else:
    if a > b:
        print('B')
        a -= 1
        b += 1
    else:
        print('A')
        a += 1
        b -= 1