Problem:


Score : 600 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

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

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

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

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

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

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

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

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

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

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

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

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,A,B,C=map(int,input().split(' '))
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: Wrong Answer

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

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#人生終わり
#解説解答
n,a,b,c = map(int,input().split())
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('C')
      else:
        ans.append('A')
  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: Wrong Answer

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

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
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==1 and s[i+1]=='AC' or a==0:
            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==1 and s[i+1]=='AB' or a==0:
            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==1 and s[i+1]=='AC' or c==0:
            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: Wrong Answer

Solution code:
n,a,b,c=map(int,input().split())
lisa=[a]+[None]*n
lisb=[b]+[None]*n
lisc=[c]+[None]*n
lis=[0]*n
for i in range(n):
    lis[i]=input()
t=[None]*n
for i in range(n):
    if lis[i]=="AB":
        if lisa[i]==0 and lisb[i]==0:
            print("No")
            exit()
        elif lisa[i]>=lisb[i]:
            t[i]="B"
            lisa[i+1]=lisa[i]-1
            lisb[i+1]=lisb[i]+1
            lisc[i+1]=lisc[i]
        else:
            t[i]="A"
            lisa[i+1]=lisa[i]+1
            lisb[i+1]=lisb[i]-1
            lisc[i+1]=lisc[i]
    elif lis[i]=="BC":
        if lisb[i]==0 and lisc[i]==0:
            print("No")
            exit()
        elif lisb[i]>=lisc[i]:
            t[i]="C"
            lisb[i+1]=lisb[i]-1
            lisc[i+1]=lisc[i]+1
            lisa[i+1]=lisa[i]
        else:
            t[i]="B"
            lisb[i+1]=lisb[i]+1
            lisc[i+1]=lisc[i]-1
            lisa[i+1]=lisa[i]
    else:
        if lisa[i]==0 and lisc[i]==0:
            print("No")
            exit()
        elif lisa[i]>=lisc[i]:
            t[i]="C"
            lisa[i+1]=lisa[i]-1
            lisc[i+1]=lisc[i]+1
            lisb[i+1]=lisb[i]
        else:
            t[i]="A"
            lisa[i+1]=lisa[i]+1
            lisc[i+1]=lisc[i]-1
            lisb[i+1]=lisb[i]
print("Yes")
for i in t:
    print(i)

Submission status: Wrong Answer

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('C')
      else:
        ans.append('A')
  print('Yes')
  for x in ans:
    print(x)
  exit()
p = []
for _ in range(n):
  p.append(input())
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: Wrong Answer

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

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

def sol(start):
    now = start
    for s in S:
        if not now in s:
            return 'ABC'.replace(now, '')
        now = s.replace(now, '')
    return start

ans = []
for a in 'ABC':
    if cnt[a] == 0:
        continue
    for s in sol(a):
        if cnt[s] == 0:
            break
    else:
        ans.append(a)

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

def out(T):
    now = {'A': 0, 'B': 0, 'C': 0}
    for s in T:
        now[s] += 1

    ans = []
    for i, s in enumerate(S):
        if now[s[0]] > now[s[1]]:
            ans.append(s[1])
            now[s[0]] -= 1
            now[s[1]] += 1
            continue
        if now[s[0]] < now[s[1]]:
            ans.append(s[0])
            now[s[0]] += 1
            now[s[1]] -= 1
            continue
        if i == N - 1:
            ans.append(s[0])
            continue
        nx = S[i + 1]
        if nx == s:
            ans.append(s[0])
            now[s[0]] += 1
            now[s[1]] -= 1
            continue
        k = list(set(list(s)) & set(list(nx)))
        k = k[0]
        ans.append(k)
        now[k] += 1
        k = s.replace(k, '')
        now[k] -= 1

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

out(ans[0])

Submission status: Wrong Answer

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

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

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

if a+b+c >= 2:
  if nums[command[0][0]] == 0 and nums[command[0][1]] == 0:
    print("No")
    exit()
  print("Yes")
  for i in range(n):
    if nums[command[i][0]] == 0:
      print(command[i][0])
      nums[command[i][0]] = 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:
      print(command[i][0])
      nums[command[i][0]] = nums[command[i][0]]+1
      nums[command[i][1]] = nums[command[i][1]]-1

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

Solution code:
import sys


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

    if A + B + C == 0:
        print('No')
        exit()

    choice = []
    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')

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


main()

Submission status: Wrong Answer

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

Solution code:
import sys


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

    choice = []
    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')

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


main()

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
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
            elif a < b:
                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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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
        if v[pre_id2] < 0:
          print('No')
          quit()
      else: # pre_s2 in [s1, s2]
        v[pre_id2] += 2
        v[pre_id1] -= 2
        if v[pre_id1] < 0:
          print('No')
          quit()
  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')