Problem:


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A



Sample input:
2 1 3 0
AB
AC


Sample output:
Yes
A
C

Submission status: Wrong Answer

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    ab = [0]*N
    ac = [0]*N
    bc = [0]*N
    l = []
    for i in range(N):
        s = input()
        ab[i] = ab[i-1]
        ac[i] = ac[i-1]
        bc[i] = bc[i-1]
        if s == 'AB':
            ab[i] += 1
            l.append('AB')
        elif s == 'AC':
            ac[i] += 1
            l.append('AC')
        else:
            bc[i] += 1
            l.append('BC')
    AB = ab[-1]
    BC = bc[-1]
    AC = ac[-1]
    r = []
    for i in range(N):
        if l[i] == 'AB':
            if A == 0 and B == 0:
                print('No')
                return
            elif A == 0:
                B -= 1
                r.append('B')
            elif B == 0:
                A -= 1
                r.append('A')
            else:
                if BC - bc[i] > AC - ac[i]:
                    A -= 1
                    r.append('A')
                else:
                    B -= 1
                    r.append('B')
        if l[i] == 'AC':
            if A == 0 and C == 0:
                print('No')
                return
            elif A == 0:
                C -= 1
                r.append('C')
            elif C == 0:
                A -= 1
                r.append('A')
            else:
                if BC - bc[i] > AB - ab[i]:
                    A -= 1
                    r.append('A')
                else:
                    C -= 1
                    r.append('C')
        if l[i] == 'BC':
            if C == 0 and B == 0:
                print('No')
                return
            elif C == 0:
                B -= 1
                r.append('B')
            elif B == 0:
                C -= 1
                r.append('C')
            else:
                if AB - ab[i] > AC - ac[i]:
                    C -= 1
                    r.append('C')
                else:
                    B -= 1
                    r.append('B')
    print('Yes')
    for i in r:
        print(i)
main()

Submission status: Wrong Answer

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

OK = True
ans = []
tasks = []
A_num = 0
B_num = 0
C_num = 0
for i in range(N):
  s = input()
  tasks.append(s)
  if "A" in s:
    A_num += 1
  if "B" in s:
    B_num += 1
  if "C" in s:
    C_num += 1

for i in tasks:
  if i == "AB":
    A_num -= 1
    B_num -= 1
    if A < B:
      A +=1
      B -=1
      ans.append("A")
    elif A > B:
      A -=1
      B +=1
      ans.append("B")
    elif A_num < B_num:
      A +=1
      B -=1
      ans.append("A")
    else:
      A -=1
      B +=1
      ans.append("B")
  if i == "BC":
    B_num -= 1
    C_num -= 1
    if B < C:
      B +=1
      C -=1
      ans.append("B")
    elif B > C:
      B -=1
      C +=1
      ans.append("C")
    elif B_num < C_num:
      B +=1
      C -=1
      ans.append("B")
    else:
      B -=1
      C +=1
      ans.append("C")
  if i == "AC":
    A_num -= 1
    C_num -= 1
    if A < C:
      A +=1
      C -=1
      ans.append("A")
    elif A > C:
      A -=1
      C +=1
      ans.append("C")
    elif A_num < C_num:
      A +=1
      C -=1
      ans.append("A")
    else:
      A -=1
      C +=1
      ans.append("C")
  if A == -1 or B == -1 or C == -1:
    OK = False
    break

if OK:
  print("Yes")
  for i in ans:
    print(i)
else:
  print("No")

Submission status: Wrong Answer

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

ans = []
for s in S:
    if s=='AB':
        if A==0 and B==0:
            print('No')
            exit()
        if A==0:
            ans.append('A')
            A += 1
            B -= 1
        elif B==0:
            ans.append('B')
            B += 1
            A -= 1
        else:
            if ans and ans[-1]=='A':
                ans.append('B')
                B += 1
                A -= 1
            else:
                ans.append('A')
                A += 1
                B -= 1
    elif s=='AC':
        if A==0 and C==0:
            print('No')
            exit()
        if A==0:
            ans.append('A')
            A += 1
            C -= 1
        elif B==0:
            ans.append('C')
            C += 1
            A -= 1
        else:
            if ans and ans[-1]=='A':
                ans.append('C')
                C += 1
                A -= 1
            else:
                ans.append('A')
                A += 1
                C -= 1
    else:
        if C==0 and B==0:
            print('No')
            exit()
        if C==0:
            ans.append('C')
            C += 1
            B -= 1
        elif B==0:
            ans.append('B')
            B += 1
            C -= 1
        else:
            if ans and ans[-1]=='C':
                ans.append('B')
                B += 1
                C -= 1
            else:
                ans.append('C')
                C += 1
                B -= 1
print('Yes')
print(*ans, sep='\n')

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def Int():
    return int(input())
def Ints():
    return map(int,input().split())
def IntList():
    return list(Ints())
def IntMat(N):
    return [IntList() for i in range(N)]

import sys
sys.setrecursionlimit(4100000)
input = sys.stdin.readline

N,A,B,C = Ints()
            
QList = []
for i in range(N-1):
    QList.append(input()[:-1])
QList.append(input())
#print(QList)
#print(QList[0]=='AB')
    
Ans = []
Wh = 1
now = 0

while now < N:
    q = QList[now]
    #print(A,B,C)
    if not (A>=0 and B>= 0 and C>=0):
        Wh = 0
        break
    if q=='AB':
        if A==0 and B==0:
            print('No')
            sys.exit()
        elif A==1 and B==1:
            if now==N-1:
                Ans.append('A')
                A += 1
                B -= 1
            else:
                nexq = QList[now+1]
                if nexq==q:
                    Ans.append('A')
                    A += 1
                    B -= 1
                elif nexq == 'AC':
                    Ans.append('A')
                    A += 1
                    B -= 1
                elif nexq == 'BC':
                    Ans.append('B')
                    B += 1
                    A -= 1
        else:
            if A<B:
                Ans.append('A')
                A += 1
                B -= 1
            else:
                Ans.append('B')
                B += 1
                A -= 1
    if q=='AC':
        if A==0 and C==0:
            print('No')
            sys.exit()
        elif A==1 and C==1:
            if now==N-1:
                Ans.append('A')
                A += 1
                C -= 1
            else:
                nexq = QList[now+1]
                if nexq==q:
                    Ans.append('A')
                    A += 1
                    C -= 1
                elif nexq == 'BC':
                    Ans.append('C')
                    C += 1
                    A -= 1
                elif nexq == 'AB':
                    Ans.append('A')
                    A += 1
                    C -= 1
        else:
            if A<C:
                Ans.append('A')
                A += 1
                C -= 1
            else:
                Ans.append('C')
                C += 1
                A -= 1
    if q=='BC':
        if B==0 and C==0:
            print('No')
            sys.exit()
        elif B==1 and C==1:
            if now==N-1:
                Ans.append('B')
                B += 1
                C -= 1
            else:
                nexq = QList[now+1]
                if nexq==q:
                    Ans.append('B')
                    B += 1
                    C -= 1
                elif nexq == 'AC':
                    Ans.append('C')
                    C += 1
                    B -= 1
                elif nexq == 'AB':
                    Ans.append('B')
                    B += 1
                    C -= 1
        else:
            if B<C:
                Ans.append('B')
                B += 1
                C -= 1
            else:
                Ans.append('C')
                C += 1
                B -= 1
    now += 1
                
if Wh==0:
    print('No')
else:
    print('Yes')
    for i in Ans:
        print(i)

Submission status: Wrong Answer

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

OK = True
ans = []
tasks = []
A_num = 0
B_num = 0
C_num = 0
for i in range(N):
  s = input()
  tasks.append(s)
  if "A" in s:
    A_num += 1
  if "B" in s:
    B_num += 1
  if "C" in s:
    C_num += 1

for i in tasks:
  if i == "AB":
    if A < B:
      A +=1
      B -=1
      ans.append("A")
    elif A > B:
      A -=1
      B +=1
      ans.append("B")
    elif A_num < B_num:
      A +=1
      B -=1
      ans.append("A")
    else:
      A -=1
      B +=1
      ans.append("B")
  if i == "BC":
    if B < C:
      B +=1
      C -=1
      ans.append("B")
    elif B > C:
      B -=1
      C +=1
      ans.append("C")
    elif B_num < C_num:
      B +=1
      C -=1
      ans.append("B")
    else:
      B -=1
      C +=1
      ans.append("C")
  if i == "AC":
    if A < C:
      A +=1
      C -=1
      ans.append("A")
    elif A > C:
      A -=1
      C +=1
      ans.append("C")
    elif A_num < C_num:
      A +=1
      C -=1
      ans.append("A")
    else:
      A -=1
      C +=1
      ans.append("C")
  if A == -1 or B == -1 or C == -1:
    OK = False
    break

if OK:
  print("Yes")
  for i in ans:
    print(i)
else:
  print("No")

Submission status: Wrong Answer

Solution code:
FAST_IO = 0
if FAST_IO:
    import io, sys, atexit
    rr = iter(sys.stdin.read().splitlines()).next
    sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
    @atexit.register
    def write():
        sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
    rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())

####
from collections import Counter

def solve2(N, A, B, C, SA):
    def canonical(a, b, c):
        return min(4, a), min(4, b), min(4, c)
    
    seen = [None] * (N+1)
    seen[0] = {canonical(A, B, C)}
    for i, x in enumerate(SA, 1):
        seen[i] = set()
        for a, b, c in seen[i-1]:
            if x == 0:  # BC
                na = a; nb = b + 1; nc = c - 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
                na = a; nb = b - 1; nc = c + 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
            if x == 1:  # AC
                na = a + 1; nb = b; nc = c - 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
                na = a - 1; nb = b; nc = c + 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
            if x == 2:  # AB
                na = a + 1; nb = b - 1; nc = c
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
                na = a - 1; nb = b + 1; nc = c
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
        if not seen[i]: return
    ans = []
    a,b,c = seen[-1].pop()
    for i in xrange(N-1, -1, -1):
        x = SA[i]
        if x == 0:  # BC
            na = a; nb = b + 1; nc = c - 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('C')
                a,b,c = nei
                continue
            na = a; nb = b - 1; nc = c + 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('B')
                a,b,c = nei
                continue
            return
        if x == 1:  # AC
            na = a + 1; nb = b; nc = c - 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('C')
                a,b,c = nei
                continue
            na = a - 1; nb = b; nc = c + 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('A')
                a,b,c = nei
                continue
            return
        if x == 2:  # AB
            na = a + 1; nb = b - 1; nc = c
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('B')
                a,b,c = nei
                continue
            na = a - 1; nb = b + 1; nc = c
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('A')
                a,b,c = nei
                continue
            return 
    return ans[::-1]
    
def solve(N, A, B, C, SA, flag=False):
    if flag is False and min(A, B, C) == 0:
        return solve2(N, A, B, C, SA)
    ans = []
    for x in SA:
        if x == 0:  # BC
            if B >= C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
        elif x == 1: #AC
            if A >= C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')
        else:  # AB
            if A >= B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
        if A<0 or B<0 or C<0: return
    return ans
    
N, A, B, C = rrm()
SA = []
for _ in xrange(N):
    s = rr()
    if s == 'AB':
        SA.append(2)
    elif s == 'BC':
        SA.append(0)
    else:
        SA.append(1)
ans = solve(N, A, B, C, SA)
if ans:
    print "Yes"
    for x in ans: print x
else:
    print "No"

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,A,B,C = map(int,input().split())
S = [input() for _ in range(N)]
li = []

def add(x,y,a,b):
    if x<=y:
        x+=1
        y-=1
        li.append(a)
    else:
        y+=1
        x-=1
        li.append(b)
    return x,y

ans = []
for s in S:
    if s=="AB":
        A,B = add(A,B,"A","B")
    elif s=="BC":
        B,C = add(B,C,"B","C")
    elif s=="AC":
        A,C = add(A,C,"A","C")
    if A<0 or B<0 or C<0:
        print("No")
        break
else:
    print("Yes")
    print(*li,sep="\n")

Submission status: Wrong Answer

Solution code:
n, *L = map(int, input().split())
t = "ABC"
def f(s):
    if s == "AB":
        u, v, w = 0, 1, 2
    elif s == "BC":
        u, v, w = 1, 2, 0
    else:
        u, v, w = 0, 2, 1
    return u, v, w
S = [input() for _ in range(n)]
A = []
for i, s in enumerate(S):
    u, v, w = f(s)
    if L[u] == 0 and L[v] == 0:
        print("No")
        exit()
    if L[u] == 0:
        A.append(t[u])
        L[u] += 1
        L[v] -= 1
    elif L[v] == 0:
        A.append(t[v])
        L[v] += 1
        L[u] -= 1
    elif L[u] >= 2:
        A.append(t[v])
        L[v] += 1
        L[u] -= 1
    elif L[v] >= 2:
        A.append(t[u])
        L[u] += 1
        L[v] -= 1
    else:
        if i == n-1:
            break
        ns = S[i+1]
        for j, l in enumerate(t):
            if l in s and l in ns:
                A.append(l)
                L[j] += 1
                if u == j:
                    L[v] -= 1
                else:
                    L[u] -= 1
print("Yes")
print("\n".join(A))

Submission status: Wrong Answer

Solution code:

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,A,B,C=MI()
    NA=0
    NB=0
    NC=0
    S=[]
    for i in range(N):
        s=input()
        S.append(s)
        NA+=s.count("A")
        NB+=s.count("B")
        NC+=s.count("C")
    
    ans=[]
    flag=1
    for i in range(N):
        s=S[i]
        if "A" in s:
            if NA>A:
                A+=1
                ans.append("A")
                if s=="AB":
                    B-=1
                else:
                    C-=1
                    
            else:
                A-=1
                if s=="AB":
                    B+=1
                    ans.append("B")
                else:
                    C+=1
                    ans.append("C")
            NA-=1
        
        elif "B" in s:
            if NB>B:
                B+=1
                ans.append("B")
                if s=="AB":
                    A-=1
                else:
                    C-=1
                    
            else:
                B-=1
                if s=="AB":
                    A+=1
                    ans.append("A")
                else:
                    C+=1
                    ans.append("C")
            NB-=1
        
        if A<0 or B<0 or C<0:
            flag=-1
            break
        
    if flag==-1:
        print("No")
    else:
        print("Yes")
        for i in range(N):
            print(ans[i])
            
            
        
    
    

main()

Submission status: Wrong Answer

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

choice = []

for _ in range(n):
  s = input()
  if s == "AB":
    if a < b:
      a += 1
      b -= 1
      choice.append("A")
    else:
      a -= 1
      b += 1
      choice.append("B")
  elif s == "AC":
    if a < c:
      a += 1
      c -= 1
      choice.append("A")
    else:
      a -= 1
      c += 1
      choice.append("C")
  else:
    if b < c:
      b += 1
      c -= 1
      choice.append("B")
    else:
      b -= 1
      c += 1
      choice.append("C")
  if min(a, b, c) == -1:
    print("No")
    exit()

print("Yes")

for c in choice:
  print(c)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
YES = "Yes"  # type: str
NO = "No"  # type: str

def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    S = {}
    S[(A,B,C)] = ""
    for i in range(N):
        N = {}
        d = s[i]
        for a,b,c in S:
            path = S[(a,b,c)]
            if d == "AB":
                if b > 0:
                    # A+1,B-1
                    N[(a+1,b-1,c)] = path + "A"
                if a > 0:
                    N[(a-1,b+1,c)] = path + "B"
            elif d == "BC":
                if c > 0:
                    # b +1,c -1
                    N[(a, b + 1, c-1)] = path + "B"
                if b > 0:
                    N[(a, b -1, c+1)] = path + "C"
            else:
                if c > 0:
                    N[(a+1,b,c-1)] = path + "A"
                if a > 0:
                    N[(a-1,b,c+1)] = path + "C"
        if len(N) == 0:
            print(NO)
            exit()
        S = N
    for a,b,c in S:
        p = S[(a,b,c)]
        for i in p:
            print(i)
        break

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    s = [next(tokens) for _ in range(N)]  # type: "List[str]"
    solve(N, A, B, C, s)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
FAST_IO = 0
if FAST_IO:
    import io, sys, atexit
    rr = iter(sys.stdin.read().splitlines()).next
    sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
    @atexit.register
    def write():
        sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
    rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())

####
from collections import Counter

def solve2(N, A, B, C, SA):
    def canonical(a, b, c):
        return min(2, a), min(2, b), min(2, c)
    
    seen = [None] * (N+1)
    seen[0] = {canonical(A, B, C)}
    for i, x in enumerate(SA, 1):
        seen[i] = set()
        for a, b, c in seen[i-1]:
            if x == 0:  # BC
                na = a; nb = b + 1; nc = c - 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
                na = a; nb = b - 1; nc = c + 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
            if x == 1:  # AC
                na = a + 1; nb = b; nc = c - 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
                na = a - 1; nb = b; nc = c + 1
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
            if x == 2:  # AB
                na = a + 1; nb = b - 1; nc = c
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
                na = a - 1; nb = b + 1; nc = c
                if na >= 0 and nb >= 0 and nc >= 0:
                    seen[i].add(canonical(na, nb, nc))
        if not seen[i]: return
    ans = []
    a,b,c = seen[-1].pop()
    for i in xrange(N-1, -1, -1):
        x = SA[i]
        if x == 0:  # BC
            na = a; nb = b + 1; nc = c - 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('C')
                a,b,c = nei
                continue
            na = a; nb = b - 1; nc = c + 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('B')
                a,b,c = nei
                continue
            return
        if x == 1:  # AC
            na = a + 1; nb = b; nc = c - 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('C')
                a,b,c = nei
                continue
            na = a - 1; nb = b; nc = c + 1
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('A')
                a,b,c = nei
                continue
            return
        if x == 2:  # AB
            na = a + 1; nb = b - 1; nc = c
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('B')
                a,b,c = nei
                continue
            na = a - 1; nb = b + 1; nc = c
            nei = canonical(na, nb, nc)
            if nei in seen[i]:
                ans.append('A')
                a,b,c = nei
                continue
            return 
    return ans[::-1]
    
def solve(N, A, B, C, SA, flag=False):
    if flag is False and min(A, B, C) == 0:
        return solve2(N, A, B, C, SA)
    ans = []
    for x in SA:
        if x == 0:  # BC
            if B >= C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
        elif x == 1: #AC
            if A >= C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')
        else:  # AB
            if A >= B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
        if A<0 or B<0 or C<0: return
    return ans
    
N, A, B, C = rrm()
SA = []
for _ in xrange(N):
    s = rr()
    if s == 'AB':
        SA.append(2)
    elif s == 'BC':
        SA.append(0)
    else:
        SA.append(1)
ans = solve(N, A, B, C, SA)
if ans:
    print "Yes"
    for x in ans: print x
else:
    print "No"

Submission status: Wrong Answer

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    N, A, B, C = map(int, input().split())
    s = [input().rstrip() for _ in range(N)]

    ans = []
    for op in s:
        # print(op)
        if op == 'AB':
            if A == B == 0:
                print('No')
                return

            if A > B:
                A -= 1
                B += 1
                ans.append('B')
            else:
                A += 1
                B -= 1
                ans.append('A')
        elif op == 'AC':
            if A == C == 0:
                print('No')
                return

            if A > C:
                A -= 1
                C += 1
                ans.append('C')
            else:
                A += 1
                C -= 1
                ans.append('A')
        else:
            if B == C == 0:
                print('No')
                return

            if B > C:
                B -= 1
                C += 1
                ans.append('C')
            else:
                B += 1
                C -= 1
                ans.append('B')
        # print(A, B, C)

    print('Yes')
    print(*ans, sep='\n')


if __name__ == '__main__':
    main()

# import sys
# input = sys.stdin.readline
# 
# sys.setrecursionlimit(10 ** 7)
# 
# (int(x)-1 for x in input().split())
# rstrip()
#
# def binary_search(*, ok, ng, func):
#     while abs(ok - ng) > 1:
#         mid = (ok + ng) // 2
#         if func(mid):
#             ok = mid
#         else:
#             ng = mid
#     return ok

Submission status: Wrong Answer

Solution code:
n, A, B, C = map(int, input().split())
S = [input() for _ in range(n)]
ins = []
for s in S:
  if s == 'AB':
    if A > B:
      A -= 1
      B += 1
      ins.append('B')
    else:
      A += 1
      B -= 1
      ins.append('A')
  elif s == 'BC':
    if B > C:
      B -= 1
      C += 1
      ins.append('C')
    else:
      B += 1
      C -= 1
      ins.append('B')
  else:
    if A >= C:
      A -= 1
      C += 1
      ins.append('C')
    else:
      A += 1
      C -= 1
      ins.append('A')
  if A < 0 or B < 0 or C < 0:
    print('No')
    break
else:
  print('Yes')
  for i in ins:
    print(i)

Submission status: Wrong Answer

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