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:
"""
import random
import functools
import copy
import bisect
import array
import re
import collections
import heapq
import fractions
import itertools
import string
import math
from operator import itemgetter as ig
from bisect import bisect_left, bisect_right, insort_left, insort_right
from itertools import permutations, combinations, product, accumulate, groupby
from heapq import heappush, heappop
from collections import deque, defaultdict, Counter
import sys
sys.setrecursionlimit(10 ** 7)
# import numpy as np

inf = 10 ** 20
INF = float("INF")
mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]
ddn9 = ddn + [(0, 0)]
'''for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:'''
"""

from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 7)


def wi(): return list(map(int, sys.stdin.readline().split()))
# WideIntPoint


def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]


def ws(): return sys.stdin.readline().split()


def si(): return int(sys.stdin.readline())  # SingleInt


def ss(): return input()


def hi(n): return [si() for _ in range(n)]


def hs(n): return [ss() for _ in range(n)]  # HeightString


def s_list(): return list(input())


def mi(n): return [wi() for _ in range(n)]  # MatrixInt


def mip(n): return [wip() for _ in range(n)]


def ms(n): return [ws() for _ in range(n)]


def num_grid(n): return [[int(i) for i in sys.stdin.readline().split()[
    0]] for _ in range(n)]  # NumberGrid


def grid(n): return [s_list() for _ in range(n)]


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def round_up(a, b):
    """ a/b の切り上げ """
    c = (a + b - 1) / b
    return c


def lcm(a, b):
    """a,bの最小公倍数"""
    return a * b // gcd(a, b)


def floor(a, b):
    """floor(a/b) 整数値を返す"""
    c = (a - a % b)/b
    return int(c)


def main():
    n, a, b, c = wi()
    s = []
    for i in range(n):
        s.append(input())

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

    elif a + b + c == 2:
        for i in range(n):
            if s[i] == 'AB':
                if a == 1 and b == 1:
                    if i != n-1 and s[i+1] == 'BC':
                        b += 1
                        a -= 1
                        ans.append('B')
                    elif i != n-1 and s[i+1] == 'AC':
                        a += 1
                        b -= 1
                        ans.append('A')
                    elif i != n-1 and s[i+1] == 'AB':
                        a += 1
                        b -= 1
                        ans.append('A')
                    elif i == n-1:
                        a += 1
                        b -= 1
                        ans.append('A')
                elif a == 2 and b == 0:
                    a -= 1
                    b += 1
                    ans.append('B')
                elif a == 0 and b == 2:
                    a += 1
                    b -= 1
                    ans.append('A')

            elif s[i] == 'BC':
                if b == 1 and c == 1:
                    if i != n-1 and s[i+1] == 'BC':
                        b += 1
                        c -= 1
                        ans.append('B')
                    elif i != n-1 and s[i+1] == 'AC':
                        c += 1
                        b -= 1
                        ans.append('C')
                    elif i != n-1 and s[i+1] == 'AB':
                        b += 1
                        c -= 1
                        ans.append('B')
                    elif i == n-1:
                        b += 1
                        c -= 1
                        ans.append('B')
                elif b == 2 and c == 0:
                    b -= 1
                    c += 1
                    ans.append('C')
                elif b == 0 and c == 2:
                    b += 1
                    c -= 1
                    ans.append('B')

            elif s[i] == 'AC':
                if a == 1 and c == 1:
                    if i != n-1 and s[i+1] == 'BC':
                        a -= 1
                        c += 1
                        ans.append('C')
                    elif i != n-1 and s[i+1] == 'AC':
                        c += 1
                        a -= 1
                        ans.append('C')
                    elif i != n-1 and s[i+1] == 'AB':
                        a += 1
                        c -= 1
                        ans.append('A')
                    elif i == n-1:
                        b += 1
                        c -= 1
                        ans.append('B')
                elif a == 2 and c == 0:
                    a -= 1
                    c += 1
                    ans.append('C')
                elif a == 0 and c == 2:
                    a += 1
                    c -= 1
                    ans.append('A')

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

                if idx1 == idx3 and idx2==idx4:
                    ans.append(st[0])
                    ans.append(st[1])
                elif idx1 == idx3 or idx1 == idx4:
                    ans.append(st[0])
                    num[idx2]-=1
                    if idx1 == idx3:
                        ans.append(st2[1])
                        num[idx4]+=1
                    else:
                        ans.append(st2[0])
                        num[idx3]+=1
                else:
                    ans.append(st[1])
                    num[idx1]-=1
                    if idx2 == idx3:
                        ans.append(st2[1])
                        num[idx4]+=1
                    else:
                        ans.append(st2[0])
                        num[idx3]+=1
            i+=2
for i in range(n):
    print(ans[i])

Submission status: Runtime Error

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"
		'''
		if abc[i1]+abc[i2]==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")
		'''

Submission status: Runtime Error

Solution code:
N,A,B,C=map(int,input().split())
r=[]
for i in range(N):
  R=input()
  r.append([str(c) for c in R])
flag=0
ans=[]
if A+B+C==0:
  flag=1
elif A+B+C==1:
  for i in range(N):
    if A==1:
      if "A" in r[i]:
        if r[i][0]=="A":
          ans.append(r[i][1])
          if r[i][1]=="B":
            B+=1
          else:
            C+=1
          A-=1
        else:
          ans.append(r[i][0])
          if r[i][0]=="B":
            B+=1
          else:
            C+=1
          A-=1
      else:
        flag=1
        break
    elif B==1:
      if "B" in r[i]:
        if r[i][0]=="B":
          ans.append(r[i][1])
          if r[i][1]=="A":
            A+=1
          else:
            C+=1
          B-=1
        else:
          ans.append(r[i][0])
          if r[i][0]=="A":
            A+=1
          else:
            C+=1
          B-=1
      else:
        flag=1
        break
    else:
      if "C" in r[i]:
        if r[i][0]=="C":
          ans.append(r[i][1])
          if r[i][1]=="B":
            B+=1
          else:
            A+=1
          C-=1
        else:
          ans.append(r[i][0])
          if r[i][1]=="B":
            B+=1
          else:
            A+=1
          C-=1
      else:
        flag=1
        break
else:
  for i in range(N):
    if A+B==0 and "A" in r[i] and "B" in r[i]:
      flag=1
    elif A+C==0 and "A" in r[i] and "C" in r[i]:
      flag=1
    elif C+B==0 and "C" in r[i] and "B" in r[i]:
      flag=1
    elif i!=N-1 and (r[i][0] not in r[i+1] or r[i][1] not in r[i+1])::
      if "A" in r[i] and "B" in r[i]:
        if "A" in r[i+1]:
          ans.append("A")
          A+=1
          B-=1
        else:
          ans.append("B")
          B+=1
          A-=1
      elif "A" in r[i] and "C" in r[i]:
        if "A" in r[i+1]:
          ans.append("A")
          A+=1
          B-=1
        else:
          ans.append("C")
          B+=1
          A-=1
      else:
        if "C" in r[i+1]:
          ans.append("C")
          C+=1
          B-=1
        else:
          ans.append("B")
          B+=1
          C-=1
    else:
      if "A" in r[i] and "B" in r[i]:
        if A<B:
          ans.append("A")
          A+=1
          B-=1
        else:
          ans.append("B")
          B+=1
          A-=1
      elif "A" in r[i] and "C" in r[i]:
        if A<C:
          ans.append("A")
          A+=1
          C-=1
        else:
          ans.append("C")
          C+=1
          A-=1
      else:
        if C<B:
          ans.append("C")
          C+=1
          B-=1
        else:
          ans.append("B")
          B+=1
          C-=1
if flag==1:
  print("No")
else:
  print("Yes")
  for i in range(len(ans)):
    print(ans[i])

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().split())
abc = list((a, b, c))
ans = [0] * n
dic = {'A': 0, 'B': 1, 'C': 2}
flag = True
s = [input() for i in range(n)]
for i in range(n):
    if abc[dic[s[i][0]]] == 0 and abc[dic[s[i][1]]] == 0:
        flag = False
        break
    elif sum(abc) == 2 and abc[dic[s[i][0]]] == 1 and abc[dic[s[i][1]]] == 1:
        ps = (set(s[i]) & set(s[i + 1])).pop()
        ms = (set(s[i]) - set(ps)).pop()
        abc[dic[ps]] += 1
        abc[dic[ms]] -= 1
        ans[i] = ps
    elif abc[dic[s[i][0]]] <= abc[dic[s[i][1]]]:
        abc[dic[s[i][0]]] += 1
        abc[dic[s[i][1]]] -= 1
        ans[i] = s[i][0]
    else:
        abc[dic[s[i][0]]] -= 1
        abc[dic[s[i][1]]] += 1
        ans[i] = s[i][1]
if flag:
    print('Yes')
    print(*ans, sep='\n')
else:
    print('No')

Submission status: Runtime Error

Solution code:
"""
Ai + Aj = abs(i - j)より、
=>  Ai - i = -Aj - j
=>  -(i - Ai) = -(Aj + j)
=>  i - Ai = j + Aj
それぞれは独立なので、これらが一致するxを見つけてその個数を数えれば良い。
"""

n = int(input())
l = list(map(int, input().split()))

d = [0] * n

cnt = 0
for i in range(len(l)):
    if i + 1 + l[i] < n:
        d[i + l[i]] += 1
    if i + 1 - l[i] > 0:
        cnt += d[i - l[i]]
print(cnt)

Submission status: Runtime Error

Solution code:
N,A,B,C=map(int,input().split())
r=[]
for i in range(N):
  R=input()
  r.append([str(c) for c in R])
flag=0
ans=[]
if A+B+C==0:
  flag=1
elif A+B+C==1:
  for i in range(N):
    if A==1:
      if "A" in r[i]:
        if r[i][0]=="A":
          ans.append(r[i][1])
          if r[i][1]=="B":
            B+=1
          else:
            C+=1
          A-=1
        else:
          ans.append(r[i][0])
          if r[i][0]=="B":
            B+=1
          else:
            C+=1
          A-=1
      else:
        flag=1
        break
    elif B==1:
      if "B" in r[i]:
        if r[i][0]=="B":
          ans.append(r[i][1])
          if r[i][1]=="A":
            A+=1
          else:
            C+=1
          B-=1
        else:
          ans.append(r[i][0])
          if r[i][0]=="A":
            A+=1
          else:
            C+=1
          B-=1
      else:
        flag=1
        break
    else:
      if "C" in r[i]:
        if r[i][0]=="C":
          ans.append(r[i][1])
          if r[i][1]=="B":
            B+=1
          else:
            A+=1
          C-=1
        else:
          ans.append(r[i][0])
          if r[i][1]=="B":
            B+=1
          else:
            A+=1
          C-=1
      else:
        flag=1
        break
else:
  for i in range(N):
    if A+B==0 and "A" in r and "B" in r:
      flag=1
    elif if A+C==0 and "A" in r and "C" in r:
      flag=1
    elif C+B==0 and "C" in r and "B" in r:
      flag=1
    elif A+B+C==2 and i!=N-1 and (A==1 or B==1 or C==1):
      if "A" in r[i] and "B" in r[i]:
        if "A" in r[i+1]:
          ans.append("A")
          A+=1
          B-=1
        else:
          ans.append("B")
          B+=1
          A-=1
      elif "A" in r[i] and "C" in r[i]:
        if "A" in r[i+1]:
          ans.append("A")
          A+=1
          B-=1
        else:
          ans.append("C")
          B+=1
          A-=1
      else:
        if "C" in r[i+1]:
          ans.append("C")
          C+=1
          B-=1
        else:
          ans.append("B")
          B+=1
          C-=1
    else:
      if "A" in r[i] and "B" in r[i]:
        if A<B:
          ans.append("A")
          A+=1
          B-=1
        else:
          ans.append("B")
          B+=1
          A-=1
      elif "A" in r[i] and "C" in r[i]:
        if A<C:
          ans.append("A")
          A+=1
          C-=1
        else:
          ans.append("C")
          C+=1
          A-=1
      else:
        if C<B:
          ans.append("C")
          C+=1
          B-=1
        else:
          ans.append("B")
          B+=1
          C-=1
if flag=1:
  print("No")
else:
  print("Yes")
  for i in range(len(ans)):
    print(ans[i])

Submission status: Runtime Error

Solution code:
n,A,B,C = map(int, input().split())

if n == 2:
  print("No)

Submission status: Runtime Error

Solution code:
n,A,B,C = map(int,input().split())
s = [[*input()] for _ in range(n)]
a = {"A":A, "B":B, "C":C}
ans = ["Yes"]+[0]*n
def dfs(i):
    if i==n:
        print("\n".join(ans))
        exit()
    for p,m in [s[i],s[i][::-1]]:
        if a[m]:
            a[p]+=1; a[m]-=1; ans[i+1]=p
            dfs(i+1)
            #一つ戻る
            a[p]-=1; a[m]+=1
dfs(0)
print("No")

Submission status: Runtime Error

Solution code:
import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, raw_input().split())
cmds = []

def getidx(c):
    return 'ABC'.index(c)

g = [ [], [], [] ]
for i in xrange(n):
    s = raw_input()
    cmds.append(s)
    g[getidx(s[0])].append(i)
    g[getidx(s[1])].append(i)

def getnext(c, idx):
    v = g[getidx(c)]
    l, r = 0, len(v) - 1
    while l <= r:
        m = (l + r) / 2
        if v[m] < idx:
            l = m + 1
        else:
            r = m - 1
    if l >= len(v):
        return n
    return v[l]

cnt = [a, b, c]
res = []


for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1 or cnt[p1] == 0 or cnt[p2] == 0:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        if min(cnt) < 0:
            res = []
            break
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        idx1 = getnext(cmd[0], i)
        idx2 = getnext(cmd[1], i)
        assert min(idx1, idx2) > i

        if idx1 < idx2:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
    assert min(cnt) >= 0

if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Runtime Error

Solution code:
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
n = inpl()
s = []
for i in range(n[0]):
    word = input()
    s.append(word)
A = n[1]
B = n[2]
C = n[3]
def dfs(a,b,c,tmp,step):
    if a < 0 or b < 0 or c < 0:
        return []
    if step == n[0]:
        return tmp
    for j in range(step, len(s)):
        if s[j] == "AB":
            tmp_tmp = dfs(a + 1, b-1, c, tmp + ["A"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a-1, b+1, c, tmp + ["B"], step + 1)
        elif s[j] == "AC":
            tmp_tmp = dfs(a+1, b, c-1, tmp +["A"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a-1, b, c + 1, tmp + ["C"], step + 1)
        else:
            tmp_tmp = dfs(a, b + 1, c-1, tmp + ["B"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a, b - 1, c + 1, tmp + ["C"], step+ 1)
ans = dfs(A,B,C,[],0)
if ans:
    print("Yes")
    for i in range(len(ans)):
        print(ans[i])
else:
    print("No")

Submission status: Runtime Error

Solution code:
#F
N,A,B,C = map(int,input().split())
S =[]
for x in range(N):
    S.append(input())
ans =[]
f =True

for x in range(N):
    
    if A==0 and B==0 and C ==0:
        f =False
        break
    
    elif A+B+C ==1:
        if S[x] =="AB" and A==1:
            ans.append("B")
            A -=1
            B +=1
        elif S[x] =="AB" and A==1:
            ans.append("A")
            A +=1
            B -=1
        elif S[x] =="AC" and A==1:
            ans.append("C")
            A -=1
            C +=1
        elif S[x] =="AC" and C==1:
            ans.append("A")
            A +=1
            C -=1
        elif S[x] =="BC" and B==1:
            ans.append("C")
            B -=1
            C +=1
        elif S[x] =="BC" and C==1:
            ans.append("B")
            B +=1
            C -=1
        else:
            f =False
            break
    elif A +B+C >= 2:
        if A==0 or B ==0 or C==0:
            if S[x]=="AB" and A ==0 and B == 0:
                f =False
                break
            if S[x]=="AC" and A ==0 and C == 0:
                f =False
                break
            if S[x]=="BC" and C ==0 and B == 0:
                f =False
                break
        if A+B+C ==2 and x <N-1 and S[X]!=S[X+1]:
            if S[x+1] =="AC":
                ans.append("A")
                A +=1
                B -=1
            elif S[x+1] =="BC":
                ans.append("B")
                A -=1
                B +=1
            elif S[x+1] =="BC":
                ans.append("C")
                A -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("A")
                A +=1
                C -=1
            elif S[x+1] =="AC":
                ans.append("C")
                B -=1
                C +=1
            elif S[x+1] =="AB":
                ans.append("B")
                B +=1
                C -=1
                
        else:
            if S[x] =="AB" and B == 0:
                ans.append("B")
                A -=1
                B +=1
            elif S[x] =="AB":
                ans.append("A")
                A +=1
                B =1
            elif S[x] =="AC" and C ==0:
                ans.append("C")
                A -=1
                C +=1
            elif S[x] =="AC":
                ans.append("A")
                A +=1
                C -=1    
            elif S[x] =="BC" and C ==0:
                ans.append("C")
                B -=1
                C +=1
            elif S[x] =="BC":
                ans.append("B")
                B +=1
                C -=1

            
    
if f ==True:
    print("Yes")
    for x in ans:
        print(x)
else:
    print("No")

Submission status: Runtime Error

Solution code:
X = [0, 0, 0]
N, X[0], X[1], X[2] = map(int, input().split())
Q = []
for _ in range(N):
    s = input()
    if s == 'AB':
        Q.append([0, 1])
    elif s == 'AC':
        Q.append([0, 2])
    else:
        Q.append([1, 2])
ans = True
ret = ''
for i in range(N):
    a = Q[i][0]
    b = Q[i][1]
    if X[a] == X[b] == 0:
        ans = False
        break
    elif X[a] < X[b]:
        ret += chr(ord(str(a)) + 17)
        X[a] += 1
        X[b] -= 1
    elif X[a] > X[b] or i == N - 1:
        ret += chr(ord(str(b)) + 17)
        X[a] -= 1
        X[b] += 1
    else:
        s = Q[i + 1][0]
        t = Q[i + 1][1]
        if a == s or a == t:
            ret += chr(ord(str(a)) + 17)
            X[a] += 1
            X[b] -= 1
        else:
            ret += chr(ord(str(b)) + 17)
            X[a] -= 1
            X[b] += 1
    print(X, ret)
    if X[a] < 0 or X[b] < 0:
        ans = False
        break
if ans:
    print('Yes')
    print('\n'.join(ret))
else:
    print('No')

Submission status: Runtime Error

Solution code:
import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, raw_input().split())
cmds = []

for i in xrange(n):
    s = raw_input()
    cmds.append(s)

cnt = [a, b, c]
res = []

for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1 or cnt[p1] == 0 or cnt[p2]] == 0:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        # assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        if cmd == nxt:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            for c in 'ABC':
                if c in cmd and c in nxt:
                    break
            else:
                assert False

            res.append(c)
            if c == cmd[0]:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
    if min(cnt) < 0:
        res = []
        break

if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Runtime Error

Solution code:
X = [0, 0, 0]
N, X[0], X[1], X[2] = map(int, input().split())
Q = []
for _ in range(N):
    s = input()
    if s == 'AB':
        Q.append([0, 1])
    elif s == 'AC':
        Q.append([0, 2])
    else:
        Q.append([1, 2])
ans = True
ret = ''
for i in range(N):
    a = Q[i][0]
    b = Q[i][1]
    if X[a] == X[b] == 0:
        ans = False
        break
    elif i == N - 1 or X[a] < X[b]:
        ret += chr(ord(str(a)) + 17)
        X[a] += 1
        X[b] -= 1
    elif X[a] > X[b]:
        ret += chr(ord(str(b)) + 17)
        X[a] -= 1
        X[b] += 1
    else:
        s = Q[i + 1][0]
        t = Q[i + 1][1]
        if a == s or a == t:
            ret += chr(ord(str(a)) + 17)
            X[a] += 1
            X[b] -= 1
        else:
            ret += chr(ord(str(b)) + 17)
            X[a] -= 1
            X[b] += 1
    if X[i] < 0 or X[j] < 0:
        ans = False
        break
if ans:
    print('Yes')
    print('\n'.join(ret))
else:
    print('No')