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: Runtime Error

Solution code:
from collections import Counter
n,a,b,c = list(map(int, input().split()))
s = [[0]*2 for _ in range(n)]
S = []
for i in range(n):
  X = input()
  S += X
  cnt = 0
  for x in X:
    if x == 'A':
      s[i][cnt] = 0
    elif x == 'B':
      s[i][cnt] = 1
    else:
      s[i][cnt] = 2
    cnt += 1

if (a + b + c) == 0:exit(print('No'))
abc = [a,b,c]
x = s[0][0]
y = s[0][1]
if abc[x] == 0 and abc[y] == 0:
  exit(print('No'))


def con(x):
  if x == 0:
    return 'A'
  elif x == 1:
    return 'B'
  else:
    return 'C'

def sel(i):
  x = s[i][0]
  y = s[i][1]
  if abc[x] == 0:
    print(con(x))
    abc[x] += 1
    abc[y] -= 1
  elif abc[y] == 0:
    print(con(y))
    abc[y] += 1
    abc[x] -= 1
  else:
    nx = s[i+1][0]
    ny = s[i+1][1]
    if abc[nx] == 0:
      if nx == x:
        print(con(x))
        abc[x] += 1
        abc[y] -= 1
      elif nx == y:
        print(con(y))
        abc[y] += 1
        abc[x] -= 1 
    elif abc[ny] == 0:
      if ny == x:
        print(con(x))
        abc[x] += 1
        abc[y] -= 1
      elif ny == y:
        print(con(y))
        abc[y] += 1
        abc[x] -= 1              
      else:
        print(con(x))
        abc[x] += 1
        abc[y] -= 1          

print('Yes')
for i in range(n):
  sel(i)

Submission status: Runtime Error

Solution code:
# -*- coding: Shift-JIS -*-
import sys 
from collections import Counter
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
Q, A, B, C = map(int, readline().split())
N = A + B + C
SS = []
for _ in range(Q):
    S = readline().decode().rstrip()
    a = 0 if S == 'AB' else 1 if S == 'AC' else 2 
    SS.append(a)
ans = [] 
if N == 0:
    print('No')
    sys.exit()
elif N == 1:
    nums = [A,B,C]
    for i in range(Q):
        num = SS[i]
        i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)  
        if nums[i] == nums[j]:
            print('No')
            sys.exit()
        if nums[i] > nums[j]:
            i,j = j,i
        nums[i] += 1
        nums[j] -= 1
else:
    nums = [A,B,C]
    num = SS[0]
    i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2) 
    if nums[i] == 0 and nums[j] == 0:
        print('No')
        sys.exit()
    if N == 2:
        for i in range(Q):
            num = SS[i]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] == 1 and nums[j] == 1:
                if SS[i] != SS[i+1]:
                    ni,nj = (0,1) if SS[i+1] == 1 else (0,2) if SS[i+1] == 2 else (1,2)
                    a = Counter([i,j,ni,nj])
                    b = a.most_common()[0][0] ## 2つあるの探す   
                    if j == b:
                        i,j = j,i   
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
    else:
        for i in range(Q):
            num = SS[i]
            i,j = (0,1) if num == 0 else (0,2) if num == 1 else (1,2)
            if nums[i] > nums[j]:
                i,j = j,i
            nums[i] += 1
            nums[j] -= 1
            ans.append(i)
        
print('Yes')
ABC = 'ABC'
answer = list(ABC[i] for i in ans)
print('\n'.join(answer))

Submission status: Runtime Error

Solution code:
def main():
    N, A, B, C = list(map(int, input().split(' ')))
    S = [input() for _ in range(N)]
    D = {
        'AB': A + B,
        'BC': B + C,
        'AC': A + C
    }
    ans = list()
    for i, s in enumerate(S):
        if D[s] == 0:
            print('No')
            return
        if s == 'AB':
            if (D['BC'] < D['AC']) or \
                (i < N and S[i + 1] == 'BC' and D['BC'] == D['AC'] == 1):
                ans.append('B')
                D['BC'] += 1
                D['AC'] -= 1
            else:
                ans.append('A')
                D['BC'] -= 1
                D['AC'] += 1
        elif s == 'BC':
            if (D['AB'] < D['AC']) or \
                (i < N and S[i + 1] == 'AB' and D['AB'] == D['AC'] == 1):
                ans.append('B')
                D['AB'] += 1
                D['AC'] -= 1
            else:
                ans.append('C')
                D['AB'] -= 1
                D['AC'] += 1
        else:  # s == 'AC'
            if (D['AB'] < D['BC']) or \
                (i < N and S[i + 1] == 'AB' and D['AB'] == D['BC'] == 1):
                ans.append('A')
                D['AB'] += 1
                D['BC'] -= 1
            else:
                ans.append('C')
                D['AB'] -= 1
                D['BC'] += 1
    print('Yes')
    for a in ans:
        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#import numpy as np
#import math
#from decimal import *
#from numba import njit

#@njit
def main():
    N,A,B,C = map(int, input().split())
    array = [A,B,C]
    string = ['A','B','C']
    si = [] # AB:0,BC:1,AC:2
    if A+B+C <= 0:
        print('No')
        return
    for _ in range(N):
        s = input()
        si += (0 if s == 'AB' else 1 if s == 'BC' else 2),

    ans = []
    for i in range(len(si)):
        s = si[i]
        index = -3 if s == 0 else -2 if s == 1 else -1
        if array[index] == 0:
            array[index] += 1
            array[index+1] -= 1
            ans += string[index],
        elif array[index+1] == 0:
            array[index] -= 1
            array[index+1] += 1
            ans += string[index+1],
        elif array[index] == 1 and array[index+1] == 1:
            if i+1 >= len(si):
                array[index] += 1
                array[index+1] -= 1
                ans += string[index],
            else:
                if s[i+1] == 1:
                    array[index] += 1
                    array[index+1] -= 1
                    ans += string[index],
                else:
                    array[index] -= 1
                    array[index+1] += 1
                    ans += string[index+1],
        elif array[index] == 1:
            array[index] += 1
            array[index+1] -= 1
            ans += string[index],
        else:
            array[index] -= 1
            array[index+1] += 1
            ans += string[index+1],

        if array[0] < 0 or array[1] < 0 or array[2] < 0:
            print('No')
            return
        
    print('Yes')
    for s in ans:
        print(s)

main()

Submission status: Runtime Error

Solution code:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    def conv(s):
        ret = []
        for i in s:
            if i == 'A':
                ret.append(0)
            elif i == 'B':
                ret.append(1)
            else:
                ret.append(2)
        return ret
    
    def inv(i):
        if i == 0:
            return 'A'
        elif i == 1:
            return 'B'
        else:
            return 'C'

    def answer(ans):
        print('Yes')
        for a in ans:
            print(a)
        return -1
    
    n, *s = map(int, input().split())
    idx_list = []
    for i in range(n):
        idx_list.append(conv(input()))
    
    ans = []
    if sum(s) == 0:
        print('No')
        exit(0)
    elif sum(s) == 1:
        for x in idx_list:
            if s[x[0]] == s[x[1]]:
                print('No')
                exit(0)

            elif s[x[0]] > s[x[1]]:
                s[x[0]] -= 1
                s[x[1]] += 1
                ans.append(inv(x[1]))
            else:
                s[x[0]] += 1
                s[x[1]] -= 1
                ans.append(inv(x[0]))
        answer(ans)
    else:
        for i, x in enumerate(idx_list):
            if s[x[0]] == 0 and s[x[1]] == 0:
                print('No')
                exit(0)
            if i != n-1 and s[x[0]] == 1 and s[x[1]] == 1:
                nx = idx_list[i+1]
                if x != nx:
                    if x[0] in nx:
                        s[x[0]] += 1
                        s[x[1]] -=1
                        ans.append(inv(x[0]))
                    else:
                        s[x[0]] -= 1
                        s[x[1]] += 1
                        ans.append(inv[x[1]])
            else:
                if s[x[0]] > s[x[1]]:
                    s[x[0]] -= 1
                    s[x[1]] += 1
                    ans.append(inv(x[1]))
                else:
                    s[x[0]] += 1
                    s[x[1]] -= 1
                    ans.append(inv(x[0]))
        answer(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

n, a, b, c = map(int, input().split())
ans = []
ss = []
for _ in range(n):
    ss.append(input().strip())
ss.append('')

def ab():
    global a
    global b
    global c
    ans.append('B')
    a -= 1
    b += 1
def ba():
    global a
    global b
    global c
    ans.append('A')
    a += 1
    b -= 1
def ac():
    global a
    global b
    global c
    ans.append('C')
    a -= 1
    c += 1
def ca():
    global a
    global b
    global c
    ans.append('A')
    a += 1
    c -= 1
def bc():
    global a
    global b
    global c
    ans.append('C')
    b -= 1
    c += 1
def cb():
    global a
    global b
    global c
    ans.append('B')
    b += 1
    c -= 1


for i, s in enumerate(ss):
    if i == n:
        break

    if s == 'AB':
        if a == 0 and b == 0:
            print('No')
            exit()
        elif a == 1 and b == 1:
            nx = s[i + 1]
            if nx == 'BC':
                ab()
            else:
                ba()
        elif a >= b:
            ab()
        else:
            ba()
    elif s == 'BC':
        if b == 0 and c == 0:
            print('No')
            exit()
        elif b == 1 and c == 1:
            nx = s[i + 1]
            if nx == 'AC':
                bc()
            else:
                cb()
        elif b >= c:
            bc()
        else:
            cb()
    elif s == 'AC':
        if a == 0 and c == 0:
            print('No')
            exit()
        elif a == 1 and c == 1:
            nx = s[i + 1]
            if nx == 'AB':
                ca()
            else:
                ac()
        elif a >= c:
            ac()
        else:
            ca()

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

Submission status: Runtime Error

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

n, a, b, c = map(int, input().split())
ans = []
ss = []
for _ in range(n):
    ss.append(input().strip())

def ab():
    global a
    global b
    global c
    ans.append('B')
    a -= 1
    b += 1
def ba():
    global a
    global b
    global c
    ans.append('A')
    a += 1
    b -= 1
def ac():
    global a
    global b
    global c
    ans.append('C')
    a -= 1
    c += 1
def ca():
    global a
    global b
    global c
    ans.append('A')
    a += 1
    c -= 1
def bc():
    global a
    global b
    global c
    ans.append('C')
    b -= 1
    c += 1
def cb():
    global a
    global b
    global c
    ans.append('B')
    b += 1
    c -= 1


for i, s in enumerate(ss):
    if s == 'AB':
        if a == 0 and b == 0:
            print('No')
            exit()
        elif a == 1 and b == 1:
            nx = s[i + 1]
            if nx == 'BC':
                ab()
            else:
                ba()
        elif a >= b:
            ab()
        else:
            ba()
    elif s == 'BC':
        if b == 0 and c == 0:
            print('No')
            exit()
        elif b == 1 and c == 1:
            nx = s[i + 1]
            if nx == 'AC':
                bc()
            else:
                cb()
        elif b >= c:
            bc()
        else:
            cb()
    elif s == 'AC':
        if a == 0 and c == 0:
            print('No')
            exit()
        elif a == 1 and c == 1:
            nx = s[i + 1]
            if nx == 'AB':
                ca()
            else:
                ac()
        elif a >= c:
            ac()
        else:
            ca()

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

Submission status: Runtime Error

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

d = {'A':a,'B':b,'C':c}

ans = ['Yes']

for i in range(n):
    if d[s[i][0]] == 0 and d[s[i][1]] == 0:
        print('No')
        exit()
    if d[s[i][0]] == 1 and d[s[i][1]] == 1 and n > 1:
        d[s[i][0]] = 2
        d[s[i][1]] = 0
        if d[s[i+1][0]] == 0 and d[s[i+1][1]] ==0:
            d[s[i][0]] = 0
            d[s[i][1]] = 2
            ans.append(s[i][1])
        else:
            ans.append(s[i][0])
    elif d[s[i][0]] >= d[s[i][1]]:
        d[s[i][0]] = d[s[i][0]] - 1
        d[s[i][1]] = d[s[i][1]] + 1
        ans.append(s[i][1])
    else:
        d[s[i][0]] = d[s[i][0]] + 1
        d[s[i][1]] = d[s[i][1]] - 1
        ans.append(s[i][0])

for i in range(n+1):
    print(ans[i])

Submission status: Runtime Error

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

d = {'A':a,'B':b,'C':c}

ans = ['Yes']

for i in range(n):
    if d[s[i][0]] == 0 and d[s[i][1]] == 0:
        print('No')
        exit()
    if d[s[i][0]] == 1 and d[s[i][1]] == 1:
        d[s[i][0]] = 2
        d[s[i][1]] = 0
        if d[s[i+1][0]] == 0 and d[s[i+1][1]] ==0:
            d[s[i][0]] = 0
            d[s[i][1]] = 2
            ans.append(s[i][1])
        else:
            ans.append(s[i][0])
    elif d[s[i][0]] >= d[s[i][1]]:
        d[s[i][0]] = d[s[i][0]] - 1
        d[s[i][1]] = d[s[i][1]] + 1
        ans.append(s[i][1])
    else:
        d[s[i][0]] = d[s[i][0]] + 1
        d[s[i][1]] = d[s[i][1]] - 1
        ans.append(s[i][0])

for i in range(n+1):
    print(ans[i])

Submission status: Runtime Error

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:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
            print(*ans, sep='\n')


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

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 for num in ABC < 0):
                    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:
                    ABC[idx0] += 1
                    ABC[idx1] -= 1
                    ans.append(si[0])
            print(*ans, sep='\n')


if __name__ == '__main__':
    solve()

Submission status: Runtime Error

Solution code:
import sys

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

if a+b+c==0:
    print('No')
elif a+b+c==1:
    ans=[]
    for i in range(n):
        s=str(input())
        if s=='AB':
            if a==b==0:
                print('No')
                sys.exit()
            elif a==1:
                a-=1
                b+=1
                ans.append('B')
            else:
                a+=1
                b-=1
                ans.append('A')
        elif s=='BC':
            if b==c==0:
                print('No')
                sys.exit()
            elif b==1:
                b-=1
                c+=1
                ans.append('C')
            else:
                b+=1
                c-=1
                ans.append('B')
        else:
            if c==a==0:
                print('No')
                sys.exit()
            elif c==1:
                c-=1
                a+=1
                ans.append('A')
            else:
                a-=1
                c+=1
                ans.append('C')
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans=[]
    S=[]
    for i in range(n):
        S.append(str(input()))
    for i in range(n):
        s=S[i]
        if s=='AB':
            if a==b==0:
                print('No')
                sys.exit()
            elif a==0:
                a+=1
                b-=1
                ans.append('A')
            elif b==0:
                b+=1
                a-=1
                ans.append('B')
            elif a==b==1 and i!=n-1:
                if s!=S[i+1]:
                    if S[i+1]=='BC':
                        b+=1
                        a-=1
                        ans.append('B')
                    else:
                        a+=1
                        b-=1
                        ans.append('A')
            else:
                a+=1
                b-=1
                ans.append('A')
        elif s=='BC':
            if b==c==0:
                print('No')
                sys.exit()
            elif b==0:
                b+=1
                c-=1
                ans.append('B')
            elif c==0:
                c+=1
                b-=1
                ans.append('C')
            elif b==c==1 and i!=n-1:
                if s!=S[i+1]:
                    if S[i+1]=='AC':
                        c+=1
                        b-=1
                        ans.append('C')
                    else:
                        b+=1
                        c-=1
                        ans.append('B')
            else:
                b+=1
                c-=1
                ans.append('B')
        else:
            if c==a==0:
                print('No')
                sys.exit()
            elif c==0:
                c+=1
                a-=1
                ans.append('C')
            elif a==0:
                a+=1
                c-=1
                ans.append('A')
            elif c==a==1 and i!=n-1:
                if s!=S[i+1]:
                    if S[i+1]=='AB':
                        a+=1
                        c-=1
                        ans.append('A')
                    else:
                        c+=1
                        a-=1
                        ans.append('C')
            else:
                c+=1
                a-=1
                ans.append('C')
    print('Yes')
    for i in range(n):
        print(ans[i])

Submission status: Runtime Error

Solution code:
import sys

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

if a+b+c==0:
    print('No')
elif a+b+c==1:
    ans=[]
    for i in range(n):
        s=str(input())
        if s=='AB':
            if a==b==0:
                print('No')
                sys.exit()
            elif a==1:
                a-=1
                b+=1
                ans.append('B')
            else:
                a+=1
                b-=1
                ans.append('A')
        elif s=='BC':
            if b==c==0:
                print('No')
                sys.exit()
            elif b==1:
                b-=1
                c+=1
                ans.append('C')
            else:
                b+=1
                c-=1
                ans.append('B')
        else:
            if c==a==0:
                print('No')
            elif c==1:
                c-=1
                a+=1
                ans.append('A')
            else:
                a-=1
                c+=1
                ans.append('C')
    print('Yes')
    for i in range(n):
        print(ans[i])
else:
    ans=[]
    S=[]
    for i in range(n):
        S.append(str(input()))
    for i in range(n):
        s=S[i]
        if s=='AB':
            if a==b==0:
                print('No')
                sys.exit()
            elif a==0:
                a+=1
                b-=1
                ans.append('A')
            elif b==0:
                b+=1
                a-=1
                ans.append('B')
            elif a==b==1 and i!=n-1:
                if s!=S[i+1]:
                    if S[i+1]=='BC':
                        b+=1
                        a-=1
                        ans.append('B')
                    else:
                        a+=1
                        b-=1
                        ans.append('A')
            else:
                a+=1
                b-=1
                ans.append('A')
        elif s=='BC':
            if b==c==0:
                print('No')
                sys.exit()
            elif b==0:
                b+=1
                c-=1
                ans.append('B')
            elif c==0:
                c+=1
                b-=1
                ans.append('C')
            elif b==c==1 and i!=n-1:
                if s!=S[i+1]:
                    if S[i+1]=='AC':
                        c+=1
                        b-=1
                        ans.append('C')
                    else:
                        b+=1
                        c-=1
                        ans.append('B')
            else:
                b+=1
                c-=1
                ans.append('B')
        else:
            if c==a==0:
                print('No')
                sys.exit()
            elif c==0:
                c+=1
                a-=1
                ans.append('C')
            elif a==0:
                a+=1
                c-=1
                ans.append('A')
            elif c==a==1 and i!=n-1:
                if s!=S[i+1]:
                    if S[i+1]=='AB':
                        a+=1
                        c-=1
                        ans.append('A')
                    else:
                        c+=1
                        a-=1
                        ans.append('C')
            else:
                c+=1
                a-=1
                ans.append('C')
    print('Yes')
    for i in range(n):
        print(ans[i])