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:
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]

d = {"AB": 0, "AC": 0, "BC": 0}

for e in s:
    d[e] += 1

da = d["AB"] + d["AC"]
db = d["AB"] + d["BC"]
dc = d["AC"] + d["BC"]

aa = a + da
bb = b + db
cc = c + dc

amx = aa // 2
bmx = bb // 2
cmx = cc // 2

ab = "AB"
bc = "BC"
ac = "AC"

if amx >= bmx >= cmx:
    li = [ac, ab, bc]
    mx = [amx, bmx, cmx]
    dd = [0, 1, 2]
elif amx >= cmx >= bmx:
    li = [ab, ac, bc]
    mx = [amx, cmx, bmx]
    dd = [0, 2, 1]
elif bmx >= amx >= cmx:
    li = [bc, ab, ac]
    mx = [bmx, amx, cmx]
    dd = [1, 0, 2]
elif bmx >= cmx >= amx:
    li = [ab, bc, ac]
    mx = [bmx, cmx, amx]
    dd = [1, 2, 0]
elif cmx >= amx >= bmx:
    li = [bc, ac, ab]
    mx = [cmx, amx, bmx]
    dd = [2, 0, 1]
else:
    li = [ac, bc, ab]
    mx = [cmx, bmx, amx]
    dd = [2, 1, 0]

cnt = {ab: [0] * 3, ac: [0] * 3, bc: [0] * 3}
int2c = ["A", "B", "C"]
c2int = {"A": 0, "B": 1, "C": 2}
for key in li:
    val = d[key]

    while val:
        val -= 1
        i = 0
        while mx[i] == 0 or int2c[dd[i]] not in key:
            i += 1
            if i == 3:
                break
        if i == 3:
            print("No")
            exit()
        mx[i] -= 1

        if int2c[dd[i]] == key[0]:
            cnt[key][c2int[key[1]]] += 1
        elif int2c[dd[i]] == key[1]:
            cnt[key][c2int[key[0]]] += 1

sm = 0
for v in cnt.values():
    sm += sum(v)

if sm != n:
    print("No")
    exit()

print("Yes")
for e in s:
    cnts = cnt[e]
    if cnts[0] > 0:
        print("A")
        cnt[e][0] -= 1
    elif cnts[1] > 0:
        print("B")
        cnt[e][1] -= 1
    else:
        print("C")
        cnt[e][2] -= 1

Submission status: Wrong Answer

Solution code:
import sys
import queue

q = queue.Queue()
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7
S = []
history = []
N, A, B ,C = map(int,input().split())
d = {}
d['A'] = A
d['B'] = B
d['C'] = C

for n in range(N):
    s = input()
    S.append(s)

def select(L,R,x):
    diff = 1
    if L!=x:
        diff *= -1
    d[L] += diff
    d[R] -= diff
    history.append(x)

for n in range(N):
    L, R = S[n]
    if d[L] >= 2:
        select(L,R,R)
    elif d[L] == 0:
        if d[R] == 0:
            print ('No')
            exit()
        else:
            select(L,R,L)
    elif d[L] == 1:
        if d[R] >= 2:
            select(L,R,L)
        elif d[R] == 0:
            select(L,R,R)
        elif d[R] == 1:
            if n!=N-1:
                if S[n+1][0] in S[n]:
                    select(L,R,L)
                else:
                    select(L,R,R)
            else:
                select(L,R,L) # どちらでもOK
print ('Yes')
for h in history:
    print (h)

Submission status: Wrong Answer

Solution code:
import sys
import queue

q = queue.Queue()
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7
S = []
history = []
N, A, B ,C = map(int,input().split())
d = {}
d['A'] = A
d['B'] = B
d['C'] = C

for n in range(N):
    s = input()
    S.append(s)

def select(L,R,x):
    diff = 1
    if L!=x:
        diff *= -1
    d[L] += diff
    d[R] -= diff
    history.append(x)

for n in range(N):
    L, R = S[n]
    if d[L] >= 2:
        select(L,R,R)
    elif d[L] == 0:
        if d[R] == 0:
            print ('No')
            exit()
        else:
            select(L,R,L)
    elif d[L] == 1:
        if d[R] >= 2:
            select(L,R,L)
        elif d[R] == 0:
            select(L,R,R)
        elif d[R] == 1:
            if n!=N-1:
                if S[n+1][0] in S[n]:
                    select(L,R,L)
                else:
                    select(L,R,R)
print ('Yes')
for h in history:
    print (h)

Submission status: Wrong Answer

Solution code:
import sys
import queue

q = queue.Queue()
sys.setrecursionlimit(10**7)
INF = 10**10
MOD = 10**9 + 7
S = []
history = []
N, A, B ,C = map(int,input().split())
d = {}
d['A'] = A
d['B'] = B
d['C'] = C

for n in range(N):
    s = input()
    S.append(s)

def select(L,R,x):
    diff = 1
    if L!=x:
        diff *= -1
    d[L] += diff
    d[R] -= diff
    history.append(x)

for n in range(N):
    L, R = S[n]
    if d[L] >= 2:
        select(L,R,R)
    elif d[L] == 0:
        if d[R] == 0:
            print ('No')
            exit()
        else:
            select(L,R,R)
    elif d[L] == 1:
        if d[R] >= 2:
            select(L,R,L)
        elif d[R] == 0:
            select(L,R,R)
        elif d[R] == 1:
            if n!=N-1:
                if S[n+1][0] in S[n]:
                    select(L,R,L)
                else:
                    select(L,R,R)
print ('Yes')
for h in history:
    print (h)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
cc = []
for i in range(n):
  ff = input().strip()
  cc.append((ff))
  
chk = True
ans = []
for num,q in enumerate(cc):
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if a == 1 and b == 1:
      if num == n-1:
        ans.append('A')
      else:
        if cc[num+1][0] == 'A':
          a += 1
          b -= 1
          ans.append('A')
        else:
          a -= 1
          b += 1
          ans.append('B')       
    elif m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if c == 1 and a == 1:
      if num == n-1:
        ans.append('A')
      else:
        if cc[num+1][0] == 'A':
          a += 1
          c -= 1
          ans.append('A')
        else:
          a -= 1
          c += 1
          ans.append('C')       
    if m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if c == 1 and b == 1:
      if num == n-1:
        ans.append('B')
      else:      
        if cc[num+1][1] == 'C':
          c += 1
          b -= 1
          ans.append('C')
        else:
          c -= 1
          b += 1
          ans.append('B')       
    if m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
cc = []
for i in range(n):
  ff = input().strip()
  cc.append((ff))
  
chk = True
ans = []
for num,q in enumerate(cc):
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if a == b and b == 1 and num < n-1:
      if cc[num+1][0] == 'A':
        a += 1
        b -= 1
        ans.append('A')
      else:
        a -= 1
        b += 1
        ans.append('B')       
    elif m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if a == c and a == 1 and num < n-1:
      if cc[num+1][0] == 'A':
        a += 1
        b -= 1
        ans.append('A')
      else:
        a -= 1
        c += 1
        ans.append('C')       
    if m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if c == b and b == 1 and num < n-1:
      if cc[num+1][1] == 'C':
        c += 1
        b -= 1
        ans.append('C')
      else:
        c -= 1
        b += 1
        ans.append('B')       
    if m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
chk = True
ans = []
for i in range(n):
  q = input().strip()
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 1:
      chk = False
      break
    if m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 1:
      chk = False
      break
    if m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 1:
      chk = False
      break
    if m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n,a,b,c = map(int,input().strip().split())
chk = True
ans = []
for i in range(n):
  q = input().strip()
  if q[0] == 'A' and q[1] == 'B':
    m = max(a,b)
    if m < 0:
      chk = False
      break
    if m == a:
      a -= 1
      b += 1
      ans.append('B')
    else:
      a += 1
      b -= 1
      ans.append('A')      
  if q[0] == 'A' and q[1] == 'C':
    m = max(a,c)
    if m < 0:
      chk = False
      break
    if m == a:
      a -= 1
      c += 1
      ans.append('C')       
    else:
      a += 1
      c -= 1
      ans.append('A')       
  if q[0] == 'B' and q[1] == 'C':
    m = max(b,c)
    if m < 0:
      chk = False
      break
    if m == b:
      b -= 1
      c += 1
      ans.append('C')
    else:
      b += 1
      c -= 1
      ans.append('B')
      
if not chk:
  print('No')
else:
  print('Yes')
  print(*ans,sep='\n')

Submission status: Wrong Answer

Solution code:
n, t1, t2, t3 = map(int, input().split())
a = {'A':t1, 'B':t2, 'C':t3}
s = []
for i in range(n):
  s.append(input())
if a['A']+a['B']+a['C'] < 2 or (a[s[0][0]]==0 and a[s[0][1]]==0):
  print('No')
else:
  print('Yes')
  for i in range(n):
    if a[s[i][1]]==0 and a[s[i][0]]>0:
      a[s[i][1]]+=1
      a[s[i][0]]-=1
      print(s[i][1])
    elif a['A']+a['B']+a['C']==2 and i!=n-1 and a[s[i][0]]==1 and a[s[i][1]]==1:
      if s[i]!=s[i+1] and (s[i+1][0]==s[i][1] or s[i+1][1]==s[i][1]):
        a[s[i][1]]+=1
        a[s[i][0]]-=1
        print(s[i][1])
    elif a[s[i][1]] >= a[s[i][0]]:
      a[s[i][0]]+=1
      a[s[i][1]]-=1
      print(s[i][0])
    else:
      a[s[i][1]]+=1
      a[s[i][0]]-=1
      print(s[i][1])

Submission status: Wrong Answer

Solution code:
n, t1, t2, t3 = map(int, input().split())
a = {'A':t1, 'B':t2, 'C':t3}
s = []
for i in range(n):
  s.append(input())
if a['A']+a['B']+a['C'] < 2 or (a[s[0][0]]==0 and a[s[0][1]]==0):
  print('No')
else:
  print('Yes')
  for i in range(n):
    if a[s[i][1]]==0 and a[s[i][0]]>0:
      a[s[i][1]]+=1
      a[s[i][0]]-=1
      print(s[i][1])
    elif a['A']+a['B']+a['C']==2 and i!=n-1 and a[s[i][0]]==1 and a[s[i][1]]==1:
      if s[i]!=s[i+1] and (s[i+1][0]==s[i][1] or s[i+1][1]==s[i][1]):
        a[s[i][1]]+=1
        a[s[i][0]]-=1
        print(s[i][1])
    else:
      a[s[i][0]]+=1
      a[s[i][1]]-=1
      print(s[i][0])

Submission status: Wrong Answer

Solution code:
n, t1, t2, t3 = map(int, input().split())
a = {'A':t1, 'B':t2, 'C':t3}
s = []
for i in range(n):
  s.append(input())
if a['A']+a['B']+a['C'] < 2 or (a[s[0][0]]==0 and a[s[0][1]]==0):
  print('No')
else:
  print('Yes')
  for i in range(n):
    if a[s[0][1]]==0 and a[s[0][0]]>0:
      a[s[0][1]]+=1
      a[s[0][0]]-=1
      print(s[0][1])
    elif a['A']+a['B']+a['C']==2 and i!=n-1 and a[s[0][0]]==1 and a[s[0][1]]==1:
      if s[i]!=s[i+1] and (s[i+1][0]==s[0][1] or s[i+1][1]==s[0][1]):
        a[s[0][1]]+=1
        a[s[0][0]]-=1
        print(s[0][1])
    else:
      a[s[0][0]]+=1
      a[s[0][1]]-=1
      print(s[0][0])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = []

for i in range(n):
    cmd = input()
    if cmd == 'AC':
        if a > c:
            ans.append('C')
            a -= 1
            c += 1
        else:
            a += 1
            c -= 1
            ans.append('A')
    elif cmd == 'BC':
        if b >= c:
            ans.append('C')
            b -= 1
            c += 1
        else:
            ans.append('B')
            b += 1
            c -= 1
    elif cmd == 'AB':
        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(0)

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

Submission status: Wrong Answer

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

ans = []

for i in range(n):
    cmd = input()
    if cmd == 'AC':
        if a > c:
            ans.append('A')
            a -= 1
            c += 1
        else:
            a += 1
            c -= 1
            ans.append('C')
    elif cmd == 'BC':
        if b >= c:
            ans.append('B')
            b -= 1
            c += 1
        else:
            ans.append('C')
            b += 1
            c -= 1
    elif cmd == 'AB':
        if a >= b:
            ans.append('A')
            a -= 1
            b += 1
        else:
            ans.append('B')
            a += 1
            b -= 1
    if a < 0 or b < 0 or c < 0:
        print('No')
        exit(0)

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

Submission status: Wrong Answer

Solution code:
n,a,b,c = map(int,input().split())
s = [input()for _ in range(n)]
num = {}
num['A'] = a
num['B'] = b
num['C'] = c
ans =[]
if a+b+c == 0:
    print("No")
    exit()
elif a+b+c == 1:
    for i in range(n):
        st = s[i]
        if num[st[0]]==num[st[1]]==0:
            print('No')
            exit()
        else:
            ans.append(st[0] if num[st[0]] else st[1])
            num[st[0]] ^= 1
            num[st[1]] ^= 1
else:
    i=0
    while i < n:
        st = s[i]
        if num[st[0]]==num[st[1]]==0:
            print('No')
            exit()
        elif num[st[0]] == 0:
            num[st[0]] = 1
            num[st[1]] -= 1
            ans.append(st[0])
            i+=1
        elif num[st[1]] == 0:
            num[st[1]] = 1
            num[st[0]] -= 1
            ans.append(st[1])
            i+=1
        else:
            if i == n-1:
                ans.append(st[1])
            else:
                st2 = s[i+1]

                if st == st2:
                    ans.append(st[0])
                    ans.append(st[1])
                elif st[0] == st2[0] or st[0] == st2[1]:
                    ans.append(st[0])
                    num[st[1]]-=1
                    if st[0] == st2[0]:
                        ans.append(st2[1])
                        num[st2[1]]+=1
                    else:
                        ans.append(st2[0])
                        num[st2[0]]+=1
                else:
                    ans.append(st[1])
                    num[st[0]]-=1
                    if st[1] == st2[0]:
                        ans.append(st2[1])
                        num[st2[1]]+=1
                    else:
                        ans.append(st2[0])
                        num[st2[0]]+=1
            i+=2
print('Yes')
for i in range(n):
    print(ans[i])

Submission status: Wrong Answer

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
dic={0:"A",1:"B",2:"C"}
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			elif abc[i2]==[1,0]:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Submission status: Wrong Answer

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==[0,0]:
			ans="No"
		elif ABC==[0,1]:
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if [abc[i1],abc[i2]]==[0,0]:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			ABC=[abc[i1],abc[i2]]
			if ABC==[0,1]:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			elif ABC==[1,0]:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
dic={0:"A",1:"B",2:"C"}
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])

Submission status: Wrong Answer

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=[abc[i1],abc[i2]]
		if ABC==(0,0):
			ans="No"
		elif ABC==[0,1]:
			abc[i1]+=1
			abc[i2]-=1
			res[i]=i1
		else:
			abc[i1]-=1
			abc[i2]+=1
			res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
dic={0:"A",1:"B",2:"C"}
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])
		'''
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")
		'''

Submission status: Wrong Answer

Solution code:
n, a, b, c = map(int, input().split())
_a, _b, _c = a, b, c
r = []


def choice(i, k, s_i, s_k):
    if i > 0 and k == 0:
        return i - 1, k + 1, s_k
    return i + 1, k - 1, s_i


L = []

for _ in range(n):
    i = input()
    L.append(i)

for i, l in enumerate(L):
    nxt = '' if i > n - 2 else L[i + 1]
    s = ''
    if l == 'AB':
        if a == 1 and b == 1:
            if nxt == 'BC':
                b, a, s = choice(b, a, 'B', 'A')
            else:
                a, b, s = choice(a, b, 'A', 'B')
        else:
            a, b, s = choice(a, b, 'A', 'B')
    if l == 'AC':
        if a == 1 and c == 1:
            if nxt == 'BC':
                c, a, s = choice(c, a, 'C', 'A')
            else:
                a, c, s = choice(a, c, 'A', 'C')
        else:
            a, c, s = choice(a, c, 'A', 'C')
    if l == 'BC':
        if b == 1 and c == 1:
            if nxt == 'AC':
                c, b, s = choice(c, b, 'C', 'B')
            else:
                b, c, s = choice(b, c, 'B', 'C')
        else:
            b, c, s = choice(b, c, 'B', 'C')
    r.append(s)

if a >= 0 and b >= 0 and c >= 0:
    print('Yes')
    for i in r:
        print(i)
else:
    print('No')

Submission status: Wrong Answer

Solution code:
n,*abc=map(int,input().split())
ip=[0]*n
for i in range(n):
	tmp=input()
	if tmp=="AB":
		ip[i]=[0,1]
	elif tmp=="BC":
		ip[i]=[1,2]
	else:
		ip[i]=[0,2]
ans="Yes"
res=[0]*n
s=sum(abc)
if s==0:
	ans="No"
elif s==1:
	for i in range(n):
		i1=ip[i][0]
		i2=ip[i][1]
		ABC=(i1,i2)
		if ABC==(0,0):
			ans="No"
		elif abc[i1]+abc[i2]==1:
			if abc[i1]==0:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
			else:
				abc[i1]-=1
				abc[i2]+=1
				res[i]=i2
else:
	i1=ip[0][0]
	i2=ip[0][1]
	if abc[i1]+abc[i2]==0:
		ans="No"
	else:
		for i in range(n):
			i1=ip[i][0]
			i2=ip[i][1]
			if abc[i1]*abc[i2]==0:
				if abc[i1]==0:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			elif i+1<n and ip[i+1]!=ip[i]:
				c=list(set(ip[i])&set(ip[i+1]))[0]
				if i1==c:
					abc[i1]+=1
					abc[i2]-=1
					res[i]=i1
				else:
					abc[i1]-=1
					abc[i2]+=1
					res[i]=i2
			else:
				abc[i1]+=1
				abc[i2]-=1
				res[i]=i1
print(ans)
dic={0:"A",1:"B",2:"C"}
if ans=="Yes":
	for i in range(n):
		print(dic[res[i]])
		'''
		if res[i]==0:
			print("A")
		elif res[i]==1:
			print("B")
		else:
			print("C")
		'''