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 numpy as np
import math
n, a, b, c = map(int,input().split())

ans = []
s = input()
f = True
for i in range(n):
    if not i == n-1:
        ns = input()
    if s == "AB": 
        if a == 0 and b == 0:
            print("No")
            f = False
            break
        elif ns == "AC" and c == 0 and a == 1:
            if b == 0:
                print("No")
                f = False
                break
            a += 1
            b -= 1
            ans.append("A")
        elif ns == "BC" and c == 0 and b == 1:
            if a == 0:
                print("No")
                f = False
                break
            b += 1
            a -= 1
            ans.append("B")
        elif a > b:
            a -= 1
            b += 1
            ans.append("B")
        else:
            a += 1
            b -= 1
            ans.append("A")
    if s == "BC": 
        if c == 0 and b == 0:
            print("No")
            f = False
            break
        elif ns == "AC" and a == 0 and c == 1:
            if b == 0:
                print("No")
                f = False
                break
            c += 1
            b -= 1
            ans.append("C")
        elif ns == "AB" and a == 0 and b == 1:
            if c == 0:
                print("No")
                f = False
                break
            b += 1
            c -= 1
            ans.append("B")
        elif c > b:
            c -= 1
            b += 1
            ans.append("B")
        else:
            c += 1
            b -= 1
            ans.append("C")
    if s == "AC": 
        if a == 0 and c == 0:
            print("No")
            f = False
            break
        elif ns == "BC" and b == 0 and c == 1:
            if a == 0:
                print("No")
                f = False
                break
            c += 1
            a -= 1
            ans.append("C")
        elif ns == "AB" and b == 0 and a == 1:
            if c == 0:
                print("No")
                f = False
                break
            a += 1
            c -= 1
            ans.append("A")
        elif c > a:
            c -= 1
            a += 1
            ans.append("A")
        else:
            c += 1
            a -= 1
            ans.append("C")
    s = ns

if f:
    print("Yes")
    for i in ans:
        print(i)

Submission status: Runtime Error

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

ns = [s[0]]
for v in s[1:]:
  if v!=ns[-1]:
    ns.append([v,1])
  else:
    ns[-1][1] += 1

ans = "No"
q = [[] for i in range(N+1)]
q[0] = [(A,B,C)]
q2 = [[] for i in range(N+1)]
q2[0] = [[]]
for i in range(N):
  while q[i]:
    a,b,c = q[i].pop(0)
    trace = q2[i].pop(0)
    if s[i] == "AB":
      if a-1>0 and (a-1,b+1,c) not in q[i+1]:
        q[i+1].append((a-1,b+1,c))
        q2[i+1].append(trace+["B"])
      if b-1>0 and (a+1,b-1,c) not in q[i+1]:
        q[i+1].append((a+1,b-1,c))
        q2[i+1].append(trace+["A"])
    if s[i] == "BC":
      if b-1>0 and (a,b-1,c+1) not in q[i+1]:
        q[i+1].append((a,b-1,c+1))
        q2[i+1].append(trace+["C"])
      if c-1>0 and (a,b+1,c-1) not in q[i+1]:
        q[i+1].append((a,b+1,c-1))
        q2[i+1].append(trace+["B"])
    if s[i] == "AC":
      if a-1>0 and (a-1,b,c+1) not in q[i+1]:
        q[i+1].append((a-1,b,c+1))
        q2[i+1].append(trace+["C"])
      if c-1>0 and (a+1,b,c-1) not in q[i+1]:
        q[i+1].append((a+1,b,c-1))
        q2[i+1].append(trace+["A"])
if q[N]:
  trace = q2[N].pop(0)
  ans = "Yes"


print(ans)
if ans =="Yes":
  for v in trace:
    print(v)

Submission status: Runtime Error

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

if (a==0 and  b==1 and c==1) or (a==1 and  b==0 and c==1) or (a==1 and  b==1 and c==0):
    que = deque([[a, b, c, []]])
    for i in range(n):
        if not que:
            print("No")
            exit()
        len_que = len(que)
        for _ in range(len_que):
            a, b, c, ans = que.popleft()
            if s[i] == "AB":
                if a>=1 and b==0:
                    a -= 1
                    b += 1
                    ans.append("B")
                elif b>=1 and a==0:
                    a += 1
                    b -= 1
                    ans.append("A")
                elif a==0 and b==0: continue
                else:
                    ans1 = ans.append("A")
                    ans2 = ans.append("B")
                    que.append([a+1, b-1, c, ans1])
                    que.append([a-1, b+1, c, ans2])
                    break
            elif s[i] == "AC":
                if a>=1 and c==0:
                    a -= 1
                    c += 1
                    ans.append("C")
                elif c>=1and a==0:
                    a += 1
                    c -= 1
                    ans.append("A")
                elif a==0 and c==0: continue
                else:
                    ans1 = ans.append("A")
                    ans2 = ans.append("C")
                    que.append([a+1, b, c-1, ans1])
                    que.append([a-1, b, c+1, ans2])
                    break
            else:
                if b >= 1 and c==0:
                    b -= 1
                    c += 1
                    ans.append("C")
                elif c >= 1and b==0:
                    b += 1
                    c -= 1
                    ans.append("B")
                elif b==0 and c==0: continue  
                else:
                    ans1 = ans.append("B")
                    ans2 = ans.append("C")
                    que.append([a, b+1, c-1, ans1])
                    que.append([a, b-1, c+1, ans2])
                    break
            que.append([a, b, c, ans])
    a, b, c, ans = que.popleft()
    for i in ans:
        print(i)

if a==0 and  b==0:
    if s[0] == "AB":
        print("No")
        exit()
elif a==0 and  c==0:
    if s[0] == "AC":
        print("No")
        exit()
elif b==0 and  c==0:
    if s[0] == "BC":
        print("No")
        exit()

print("Yes")
for i in range(n):
    if s[i] == "AB":
        if a >= b:
            a -= 1
            b += 1
            print("B")
        else:
            a += 1
            b -= 1
            print("A")
    elif s[i] == "AC":
        if a >= c:
            a -= 1
            c += 1
            print("C")
        else:
            a += 1
            c -= 1
            print("A")
    else:
        if b >= c:
            b -= 1
            c += 1
            print("C")
        else:
            b += 1
            c -= 1
            print("B")

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3


def main():
    N = int(input())
    A = [i for i in map(int, input().split())]
    #print (N, A)
    gsize = 10
    numg = (N - 1) // gsize + 1
    maxg = [0] * numg
    ming = [0] * numg
    size = len(A)
    for g in range(0, numg):
        gA = A[gsize*g:min(gsize*(g+1), size)]
        maxg[g] = max(gA)
        ming[g] = min(gA)

    count = 0
    for g1 in range(0, numg):
        for g2 in range(g1, numg):
            #print('checking group', g1, g2)
            gd = abs(g1 - g2)
            if maxg[g1] + maxg[g2] < gsize * (gd - 1):
                continue
            if ming[g1] + ming[g2] > gsize * (gd + 1):
                continue
            for g1i in range(gsize*g1, min(gsize*(g1+1), size)):
                for g2i in range(max(g1i+1, gsize*g2), min(gsize*(g2+1), size)):
                    d = abs(g1i - g2i)
                    if A[g1i] + A[g2i] == d:
                        count += 1
    print(count)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

s = []
for _ in range(n):
    si = sys.stdin.readline().rstrip()
    if si == 'AB':
        s.append(0)
    elif si == 'BC':
        s.append(1)
    else:
        s.append(2)

ddic = [[0,1,2], [1,2,3], [2,0,1]]
ldic = ['A', 'B', 'C']

if a == 0 and b == 0 and c == 0:
    print('No')
    exit()
elif a +b+c == 1:
    log = []
    tani = [a, b, c]
    for x in s:
        i1 = ddic[x][0]
        i2 = ddic[x][1]
        if tani[i1] ==1:
            log.append(ldic[i2])
            tani[i1] -=1
            tani[i2] +=1
        elif tani[i2] == 1:
            log.append(ldic[i1])
            tani[i2] -=1
            tani[i1] +=1
        else:
            print('No')
            exit()
    print('Yes')
    for y in log:
        print(y)
    exit()
elif a >0 and b>0 and c>0:
    print('Yes')
    tani = [1, 1, 1]
    for x in s:
        i1 = ddic[x][0]
        i2 = ddic[x][1]
        if tani[i1]==0 or tani[i2] ==2:
            print(ldic[i1])
            tani[i1]+=1
            tani[i2]-=1
        else:
            print(ldic[i2])
            tani[i2]+=1
            tani[i1]-=1
    exit()
else:
    log = []
    count = 0
    tani = [a, b, c]
    for dis in ddic:
        i1 = dis[0]
        i2 = dis[1]
        if tani[i1] == 0 and tani[i2] == 0:
            x = s[0]
            if ddic[x][0] == i1 and ddic[x][1] == i2:
                print('No')
                exit()
            if tani[ddic[x][0]] > 0:
                log.append(ldic[ddic[x][1]])
                tani[ddic[x][0]] -=1
                tani[ddic[x][1]] +=1
            else:
                log.append(ldic[ddic[x][0]])
                tani[ddic[x][1]] -=1
                tani[ddic[x][0]] +=1
            count+=1
            break
    while True:
        i1 = ddic[s[count]][0]
        i2 = ddic[s[count]][1]
        i3 = ddic[s[count]][2]
        if tani[i3] == 0 and tani[i1] == 1 and tani[i2] == 1 and count < len(s)-1:
            if s[count+1] == s[count]:
                log.append(ldic[i1])
                log.append(ldic[i2])
                count+=1
            else:
                if ddic[s[count+1]][0] ==i1 or ddic[s[count+1]][1] ==i1:
                    log.append(ldic[i1])
                    tani[i1] +=1
                    tani[i2] -=1
                else:
                    log.append(ldic[i2])
                    tani[i2] +=1
                    tani[i1] -=1
        else:
            if tani[i1] > tani[i2]:
                log.append(ldic[i2])
                tani[i1] -=1
                tani[i2] +=1
            else:
                log.append(ldic[i1])
                tani[i2] -=1
                tani[i1] +=1
        count+=1
        if count >= len(s):
            break
    print('Yes')
    for y in log:
        print(y)
    exit()

Submission status: Runtime Error

Solution code:
import numpy as np
import math
n, a, b, c = map(int,input().split())
s = a+b+c

ans = []
s = input()
f = True
for i in range(n):
    if not i == n-1:
        ns = input()
    if s == "AB": 
        if a == 0 and b == 0:
            print("No")
            f = False
            break
        elif ns == "AC" and c == 0 and a == 1:
            if b == 0:
                print("No")
                f = False
                break
            a += 1
            b -= 1
            ans.append("A")
        elif ns == "BC" and c == 0 and b == 1:
            if a == 0:
                print("No")
                f = False
                break
            b += 1
            a -= 1
            ans.append("B")
        elif a > b:
            a -= 1
            b += 1
            ans.append("B")
        else:
            a += 1
            b -= 1
            ans.append("A")
    if s == "BC": 
        if c == 0 and b == 0:
            print("No")
            f = False
            break
        elif ns == "AC" and a == 0 and c == 1:
            if b == 0:
                print("No")
                f = False
                break
            c += 1
            b -= 1
            ans.append("C")
        elif ns == "AB" and a == 0 and b == 1:
            if c == 0:
                print("No")
                f = False
                break
            b += 1
            c -= 1
            ans.append("B")
        elif c > b:
            c -= 1
            b += 1
            ans.append("B")
        else:
            c += 1
            b -= 1
            ans.append("C")
    if s == "AC": 
        if a == 0 and c == 0:
            print("No")
            f = False
            break
        elif ns == "BC" and b == 0 and c == 1:
            if a == 0:
                print("No")
                f = False
                break
            c += 1
            a -= 1
            ans.append("C")
        elif ns == "AB" and b == 0 and a == 1:
            if c == 0:
                print("No")
                f = False
                break
            a += 1
            c -= 1
            ans.append("A")
        elif c > b:
            c -= 1
            a += 1
            ans.append("A")
        else:
            c += 1
            a -= 1
            ans.append("C")
    s = ns

if f:
    print("Yes")
    for i in ans:
        print(i)

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
S = [input() for _ in range(N)]
a = min(A,10**5)
b = min(B,10**5)
c = min(C,10**5)

res = ['0']*N

# 明らかに可能
if a+b+c >= 6:
    print('Yes')
    for i in range(N):
        if S[i]=='AC':
            if a<c:
                res[i] = 'A'
                a += 1
                c -= 1
            else:
                res[i] = 'C'
                c += 1
                a -= 1
        elif S[i]=='AB':
            if a<b:
                res[i] = 'A'
                a += 1
                b -= 1
            else:
                res[i] = 'B'
                b += 1
                a -= 1
        else:
            if b<c:
                res[i] = 'B'
                b += 1
                c -= 1
            else:
                res[i] = 'C'
                c += 1
                b -= 1
    for val in res:
        print(val)
elif a+b+c==0:
    print('No')
elif a+b+c==1:
    now = 'a'
    if b==1:
        now = 'b'
    elif c==1:
        now = 'c'
    for i in range(N):
        if S[i] == 'AC':
            if now=='b':
                print('No')
                exit()
            elif now=='a':
                res[i] = 'C'
                now = 'c'
            else:
                res[i] = 'A'
                now = 'a'
        if S[i] == 'BC':
            if now=='a':
                print('No')
                exit()
            elif now=='b':
                res[i] = 'C'
                now = 'c'
            else:
                res[i] = 'B'
                now = 'b'
        if S[i] == 'AB':
            if now=='c':
                print('No')
                exit()
            elif now=='a':
                res[i] = 'B'
                now = 'b'
            else:
                res[i] = 'A'
                now = 'a'
    print('Yes')
    for val in res:
        print(val)
elif a+b+c==2:
    assert()
    available = [(a,b,c)]
    next_available = []
    for s in S:
        for aa,bb,cc in available:
            if s=='AC':
                if aa==2:
                    next_available.append((1,0,1))
                elif aa==1 and cc==1:
                    next_available.append((2,0,0))
                    next_available.append((0,0,2))
                elif aa==1:
                    next_available.append((0,1,1))
                else:
                    next_available.append((1,0,1))
                    
            if s=='AB':
                if aa==2:
                    next_available.append((1,1,0))
                elif aa==1 and bb==1:
                    next_available.append((2,0,0))
                    next_available.append((0,2,0))
                elif aa==1:
                    next_available.append((0,1,1))
                else:
                    next_available.append((1,1,0))
                    
            if s=='BC':
                if bb==2:
                    next_available.append((0,1,1))
                elif bb==1 and cc==1:
                    next_available.append((0,2,0))
                    next_available.append((0,0,2))
                elif bb==1:
                    next_available.append((1,0,1))
                else:
                    next_available.append((0,1,1))
            if next_available==[]:
                print('No')
                exit()
else:
    assert()

Submission status: Runtime Error

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

def main():
    N,A,B,C=map(int,input().split())
    n=[A,B,C]
    d={'AB':0,'BC':1,'AC':2}
    c=[deque() for _ in range(3)]
    ans=[-1]*N
    sousa=[]
    for i in range(N):
        s=input()
        sousa.append(d[s])
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            raise Exception
            print('No')
            exit()
        elif n[d[s]]==0:
            ans[i]=d[s]
            n[d[s]]+=1
            n[(d[s]+1)%3]-=1
        elif n[(d[s]+1)%3]==0:
            ans[i]=(d[s]+1)%3
            n[d[s]]-=1
            n[(d[s]+1)%3]+=1
        else:
            c[d[s]].append(i)
        if len(c[d[s]])>=2:
            if n[(d[s]+2)%3]>=1:
                if n[d[s]]>=1:
                    k=c[d[s]].popleft()
                    ans[k]=(d[s]+1)%3
                    k=c[d[s]].popleft()
                    ans[k]=d[s]%3
                else:
                    k=c[d[s]].popleft()
                    ans[k]=d[s]%3
                    k=c[d[s]].popleft()
                    ans[k]=(d[s]+1)%3
        if len(c[0])>=1 and len(c[1])>=1 and len(c[2])>=1:
            for j in range(3):
                if n[j]>=1:
                    start=j
                    break
            for j in range(start,3+start):
                j%=3
                k=c[j].popleft()
                ans[k]=(j+1)%3
    n=[A,B,C]
    print('Yes')
    d2=['A','B','C']
    for x,y in zip(ans,sousa):
        if x!=-1:
            n[x]+=1
            if x==y:
                n[(x+1)%3]-=1
            else:
                n[(x-1)%3]-=1
            print(d2[x])
        else:
            if n[y]>=n[(y+1)%3]:
                n[y]-=1
                n[(y+1)%3]+=1
                print(d2[(y+1)%3])
            elif n[(y+1)%3]>=1:
                n[y]+=1
                n[(y+1)%3]-=1
                print(d2[y])
            else:
                raise Exception
                
                    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

N, A, B, C = map(int, readline().split())
S = [readline().strip() for i in range(N)]

AB, AC, BC = 'AB', 'AC', 'BC'

n0 = [A, B, C].count(0)

F = [A, B, C]
H = ['A', 'B', 'C']
G = {AB: (0, 1), BC:(1,2), AC:(0, 2)}

if n0 == 3:
    print('No')
elif n0 == 2 and sum([A, B, C]) == 1:
    path = []
    for com in S:
        a,b = G[com]
        if F[a] == F[b] == 0:
            print('No')
            exit(0)
        else:
            if F[a] == 1:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
    print('Yes')
    for p in path:
        print(p)
else:
    if (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')
        exit(0)

    path = []
    for i,com in enumerate(S):
        a,b = G[com]
        if F[a] == 0 or F[b] == 0:
            if F[b] == 0:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
        else:
            if i < len(S) - 1 and H[a] == H[b]:
                if b in G[S[i + 1]]:
                    a, b = b, a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
            else:
                if F[a] > F[b]:
                    a,b = b,a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
        assert all(v >= 0 for v in F)
    print('Yes')
    for p in path:
        print(p)

Submission status: Runtime Error

Solution code:
def solve(data,siji):
    global henkan
    ans=[]
    for s in siji:
        hiku,tasu=henkan[s]
        if(data[hiku]<data[tasu]):
            temp=hiku
            hiku=tasu
            tasu=temp
        #print(hiku,tasu,data)
        data[hiku]-=1
        data[tasu]+=1
        ans.append(tasu)
        if(data[hiku]<0):
            return [False,[]]
    return [True,ans]

n,a,b,c=map(int,input().split())
siji=[]
henkan=[(0,1),(0,2),(1,2)]
for i in range(n):
    s=input()
    if(s=="AB"):
        siji.append(0)
    elif(s=="AC"):
        siji.append(1)
    else:
        siji.append(2)



data=[a,b,c]
zeros=0
gokei=sum(data)
for i in range(3):
    if(data[i]==0):
        zeros+=1

if(zeros==3):
    koate=solve([0,0,0],siji)
elif(zeros==0):
    kotae=solve([1,1,1],siji)
    #print(kotae)
elif(gokei>=3):
    kotae=solve(data,siji)
elif(gokei==1):
    kotae=solve(data,siji)
elif(gokei==2):
    koate=solve(data,siji)
else:
    kotae=solve(data,siji)




if(not kotae[0]):
    print("No")
else:
    print("Yes")
    alpha=["A","B","C"]
    for k  in kotae[1]:
        print(alpha[k])

Submission status: Runtime Error

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

if (a==0 and  b==1 and c==1) or (a==1 and  b==0 and c==1) or (a==1 and  b==1 and c==0):
    que = deque([[a, b, c, []]])
    for i in range(n):
        if not que:
            print("No")
            exit()
        len_que = len(que)
        for _ in range(len_que):
            a, b, c, ans = que.popleft()
            if s[i] == "AB":
                if a>=1 and b==0:
                    a -= 1
                    b += 1
                    ans.append("B")
                elif b>=1 and a==0:
                    a += 1
                    b -= 1
                    ans.append("A")
                elif a==0 and b==0: continue
                else:
                    ans1 = ans.append("A")
                    ans2 = ans.append("B")
                    que.append([a+1, b-1, c, ans1])
                    que.append([a-1, b+1, c, ans2])
            elif s[i] == "AC":
                if a>=1 and c==0:
                    a -= 1
                    c += 1
                    ans.append("C")
                elif c>=1and a==0:
                    a += 1
                    c -= 1
                    ans.append("A")
                elif a==0 and c==0: continue
                else:
                    ans1 = ans.append("A")
                    ans2 = ans.append("C")
                    que.append([a+1, b, c-1, ans1])
                    que.append([a-1, b, c+1, ans2])
            else:
                if b >= 1 and c==0:
                    b -= 1
                    c += 1
                    ans.append("C")
                elif c >= 1and b==0:
                    b += 1
                    c -= 1
                    ans.append("B")
                elif b==0 and c==0: continue  
                else:
                    ans1 = ans.append("B")
                    ans2 = ans.append("C")
                    que.append([a, b+1, c-1, ans1])
                    que.append([a, b-1, c+1, ans2])
            que.append([a, b, c, ans])
    a, b, c, ans = que.popleft()
    for i in ans:
        print(i)

if a==0 and  b==0:
    if s[0] == "AB":
        print("No")
        exit()
elif a==0 and  c==0:
    if s[0] == "AC":
        print("No")
        exit()
elif b==0 and  c==0:
    if s[0] == "BC":
        print("No")
        exit()

print("Yes")
for i in range(n):
    if s[i] == "AB":
        if a >= b:
            a -= 1
            b += 1
            print("B")
        else:
            a += 1
            b -= 1
            print("A")
    elif s[i] == "AC":
        if a >= c:
            a -= 1
            c += 1
            print("C")
        else:
            a += 1
            c -= 1
            print("A")
    else:
        if b >= c:
            b -= 1
            c += 1
            print("C")
        else:
            b += 1
            c -= 1
            print("B")

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 = []

a_ = a
b_ = b
c_ = c

def print_res(cnts, res):
    rem = [a_, b_, c_]
    p = [0, 0, 0]
    for i in xrange(3):
        p[i] = cnts[i] + res[i]
        assert p[i] % 2 == 0
        p[i] /= 2

    print 'Yes'
    for s in cmds:
        nr = {
            'AB': 0,
            'AC': 1,
            'BC': 2
        }[s]

        x, y = ord(s[0]) - ord('A'), ord(s[1]) - ord('A')

        if p[nr] > 0:
            p[nr] -= 1
            print s[0]
            rem[x] += 1
            rem[y] -= 1
        else:
            print s[1]
            rem[y] += 1
            rem[x] -= 1

    assert min(rem) >= 0


cnts = [0, 0, 0]

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

    nr = {
        'AB': 0,
        'AC': 1,
        'BC': 2
    }[s]

    cnts[nr] += 1


for i in xrange(-cnts[0], cnts[0] + 1, 2):
    aa = a + i
    bb = b - i
    cc = c

    res = [i, -1, -1]

    delta = cnts[1]
    da = (delta - aa + 1) / 2
    dc = delta - da
    res[1] = da - dc
    assert da + dc == delta
    aa = aa + da - dc
    cc = cc + dc - da

    delta = cnts[2]
    db = (delta - bb + 1) / 2
    dc = delta - db
    assert db + dc == delta
    bb = bb + db - dc
    cc = cc + dc - db
    res[2] = db - dc

    if aa >= 0 and bb >= 0 and cc >= 0:
        print_res(cnts, res)
        break
else:
    print 'No'

Submission status: Runtime Error

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

N, A, B, C = map(int, readline().split())
S = [readline().strip() for i in range(N)]

AB, AC, BC = 'AB', 'AC', 'BC'

n0 = [A, B, C].count(0)

F = [A, B, C]
H = ['A', 'B', 'C']
G = {AB: (0, 1), BC:(1,2), AC:(0, 2)}

if n0 == 3:
    print('No')
elif n0 == 2 and sum([A, B, C]) == 1:
    path = []
    for com in S:
        a,b = G[com]
        if F[a] == F[b] == 0:
            print('No')
            exit(0)
        else:
            if F[a] == 1:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
    print('Yes')
    for p in path:
        print(p)
else:
    if (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')
        exit(0)

    path = []
    for i,com in enumerate(S):
        a,b = G[com]
        if a == 0 or b == 0:
            if b == 0:
                a,b = b,a
            path.append(H[a])
            F[a] += 1
            F[b] -= 1
        else:
            if i < len(S) - 1 and H[a] == H[b]:
                if b in G[S[i + 1]]:
                    a, b = b, a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
            else:
                if F[a] > F[b]:
                    a,b = b,a
                path.append(H[a])
                F[a] += 1
                F[b] -= 1
        assert all(v >= 0 for v in F)
    print('Yes')
    for p in path:
        print(p)