Problem:
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.

Incorrect submission (verdict: Wrong Answer):
def g_solve(N, A, B, C):
  seq = []
  for _ in range(N):
    s = input()
    if s == 'AB':
      if A > B:
        A -= 1
        B += 1
        seq.append('B')
      else:
        A += 1
        B -= 1
        seq.append('A')
    elif s == 'AC':
      if A > C:
        A -= 1
        C += 1
        seq.append('C')
      else:
        A += 1
        C -= 1
        seq.append('A')
    elif s == 'BC':
      if B > C:
        B -= 1
        C += 1
        seq.append('C')
      else:
        B += 1
        C -= 1
        seq.append('B')
    if A < 0 or B < 0 or C < 0:
      print('No')
      return
  print('Yes')
  print('\n'.join(seq))    


def dp_solve(N, a,b,c):
  state = {}
  state[(a, b, c)] = []
  for _ in range(N):
    s = input()
    next_state = {}
    for a, b, c in state:
      if s == 'AB':
        if b-1 >= 0:
            next_state[(a+1, b-1, c)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b+1, c)] = ['B', state[(a,b,c)]]
      if s == 'AC':
        if c-1 >= 0:
          next_state[(a+1, b, c-1)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b, c+1)] = ['C', state[(a,b,c)]]
      if s == 'BC':
        if c-1 >= 0:
          next_state[(a, b+1, c-1)] = ['B', state[(a,b,c)]]
        if b-1 >= 0:
          next_state[(a, b-1, c+1)] = ['C', state[(a,b,c)]]
    state = next_state
    if not state:
      print('No')
      return
    
  print('Yes')
  seq = list(state.values())[0]
  while seq:
    print(seq[0])
    seq = seq[1]
  
    
  
  
N, A, B, C = [int(x) for x in input().split(' ')]
if A + B + C < 6:
  dp_solve(N, A, B, C)
else:
  g_solve(N, A, B, C)


Fixed, accepted submission:
def g_solve(N, A, B, C):
  seq = []
  for _ in range(N):
    s = input()
    if s == 'AB':
      if A > B:
        A -= 1
        B += 1
        seq.append('B')
      else:
        A += 1
        B -= 1
        seq.append('A')
    elif s == 'AC':
      if A > C:
        A -= 1
        C += 1
        seq.append('C')
      else:
        A += 1
        C -= 1
        seq.append('A')
    elif s == 'BC':
      if B > C:
        B -= 1
        C += 1
        seq.append('C')
      else:
        B += 1
        C -= 1
        seq.append('B')
    if A < 0 or B < 0 or C < 0:
      print('No')
      return
  print('Yes')
  print('\n'.join(seq))    


def dp_solve(N, a,b,c):
  state = {}
  state[(a, b, c)] = []
  for _ in range(N):
    s = input()
    next_state = {}
    for a, b, c in state:
      if s == 'AB':
        if b-1 >= 0:
            next_state[(a+1, b-1, c)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b+1, c)] = ['B', state[(a,b,c)]]
      if s == 'AC':
        if c-1 >= 0:
          next_state[(a+1, b, c-1)] = ['A', state[(a,b,c)]]
        if a-1 >= 0:
          next_state[(a-1, b, c+1)] = ['C', state[(a,b,c)]]
      if s == 'BC':
        if c-1 >= 0:
          next_state[(a, b+1, c-1)] = ['B', state[(a,b,c)]]
        if b-1 >= 0:
          next_state[(a, b-1, c+1)] = ['C', state[(a,b,c)]]
    state = next_state
    if not state:
      print('No')
      return
    
  print('Yes')
  ans = []
  seq = list(state.values())[0]
  while seq:
    ans.append(seq[0])
    
    seq = seq[1]
  ans.reverse()
  for a in ans:
    print(a)
    
  
  
N, A, B, C = [int(x) for x in input().split(' ')]
if A + B + C < 20:
  dp_solve(N, A, B, C)
else:
  g_solve(N, A, B, C)