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

Solution (Python):

import sys

YES = "Yes"  
NO = "No"  


class Impossible(Exception):
    pass

def f(q, qq, state):
    '''
    >>> f((0, 1), None, [2, 1, 0])
    1
    >>> f((0, 1), None, [1, 2, 0])
    0
    >>> f((0, 1), None, [0, 1, 0])
    0
    >>> f((1, 2), None, [0, 1, 0])
    2
    >>> f((0, 2), None, [1, 0, 0])
    2
    >>> f((0, 1), (0, 3), [1, 1, 0])
    0
    >>> f((0, 1), (0, 3), [1, 2, 0])
    0
    >>> f((0, 1), [], [1, 1, 0])
    1
    >>> f((0, 1), None, [0, 0, 0])
    -1
    >>> f((1, 2), None, [0, 0, 0])
    -1
    >>> f((0, 1), (0, 3), [1, 0, 0])
    1
    '''
    n, m = q
    if state[n] == state[m] == 0:
        return -1
    if state[n] == state[m] == 1:
        if n in qq:
            return n
        return m
    if state[n] > state[m]:
        return m
    return n
        

def solve(N: int, A: int, B: int, C: int, s: "List[str]"):
    D = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
    E = 'ABC'
    Q = tuple(map(lambda x: D[x], s))
    state = [A, B, C]
    for q, qq in zip(Q, Q[1:] + (tuple(),)):
        r = f(q, qq, state)
        if r == -1:
            raise Impossible
        yield E[r]
        state[r] += 1
        state[q[q.index(r)^1]] -= 1



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = int(next(tokens))  
    B = int(next(tokens))  
    C = int(next(tokens))  
    s = [next(tokens) for _ in range(N)]  
    try:
        ans = list(solve(N, A, B, C, s))
        print(YES)
        print(*ans, sep='\n')
    except Impossible:
        print(NO)

def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    test()
    main()

Solution (Python):
N, A, B, C = map(int, input().split())
if A + B + C == 0:
    print('No')
elif A + B + C == 1:
    res = []
    for _ in range(N):
        s = input()
        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0 and C == 0:
                print('No')
                break
            elif B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
    else:
        print('Yes')
        print('\n'.join(res))
else:
    res = []
    s = input()
    if (s == 'AB' and A == 0 and B == 0) or (s == 'AC' and A == 0 and C == 0) or (s == 'BC' and B == 0 and C == 0):
        print('No')
    else:
        for _ in range(N - 1):
            next = input()
            if s == 'AB':
                if A == 0:
                    B -= 1
                    A += 1
                    res.append('A')
                elif B == 0:
                    B += 1
                    A -= 1
                    res.append('B')
                else:
                    if next == 'AB':
                        A += 1
                        B -= 1
                        res.append('A')
                    elif next == 'AC':
                        A += 1
                        B -= 1
                        res.append('A')
                    else:
                        A -= 1
                        B += 1
                        res.append('B')
            elif s == 'AC':
                if A == 0:
                    C -= 1
                    A += 1
                    res.append('A')
                elif C == 0:
                    C += 1
                    A -= 1
                    res.append('C')
                else:
                    if next == 'AC':
                        A += 1
                        C -= 1
                        res.append('A')
                    elif next == 'AB':
                        A += 1
                        C -= 1
                        res.append('A')
                    else:
                        A -= 1
                        C += 1
                        res.append('C')
            else:
                if C == 0:
                    B -= 1
                    C += 1
                    res.append('C')
                elif B == 0:
                    B += 1
                    C -= 1
                    res.append('B')
                else:
                    if next == 'CB':
                        C += 1
                        B -= 1
                        res.append('C')
                    elif next == 'AC':
                        C += 1
                        B -= 1
                        res.append('C')
                    else:
                        C -= 1
                        B += 1
                        res.append('B')
            s = next
        if s == 'AB':
            if A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
        print('Yes')
        print('\n'.join(res))

Solution (Python):

import sys
from collections import defaultdict

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

L=[]    

for i,s in enumerate(S): 
    if s=="AB":
        if A<B:
            A+=1
            B-=1
            L.append("A")
        elif B<A:
            A-=1
            B+=1
            L.append("B")
        elif A==B==0:
            print "No"
            quit()
        elif A==B:
            if i<N-1: 
                next_s=S[i+1]
                if next_s=="AB":    
                    A+=1
                    B-=1
                    L.append("A")
                elif next_s=="AC":    
                    A+=1
                    B-=1
                    L.append("A")
                elif next_s=="BC":    
                    A-=1
                    B+=1
                    L.append("B")
            else: 
                A+=1
                B-=1
                L.append("A")
    elif s=="AC":
        if A<C:
            A+=1
            C-=1
            L.append("A")
        elif C<A:
            A-=1
            C+=1
            L.append("C")
        elif A==C==0:
            print "No"
            quit()
        elif A==C:
            if i<N-1: 
                next_s=S[i+1]
                if next_s=="AC":    
                    A+=1
                    C-=1
                    L.append("A")
                elif next_s=="AB":    
                    A+=1
                    C-=1
                    L.append("A")
                elif next_s=="BC":    
                    A-=1
                    C+=1
                    L.append("C")
            else: 
                A+=1
                C-=1
                L.append("A")
    elif s=="BC":
        if B<C:
            C-=1
            B+=1
            L.append("B")
        elif C<B:
            B-=1
            C+=1
            L.append("C")
        elif B==C==0:
            print "No"
            quit()
        elif B==C:
            if i<N-1: 
                next_s=S[i+1]
                if next_s=="BC":    
                    B+=1
                    C-=1
                    L.append("B")
                elif next_s=="AB":    
                    B+=1
                    C-=1
                    L.append("B")
                elif next_s=="AC":    
                    B-=1
                    C+=1
                    L.append("C")
            else: 
                A+=1
                C-=1
                L.append("A")
print "Yes"
for x in L:
    print x

Solution (Python):

import sys
from collections import defaultdict

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

L=[]    

for i,s in enumerate(S): 
    if s=="AB":
        if A<B:
            A+=1
            B-=1
            L.append("A")
        elif B<A:
            A-=1
            B+=1
            L.append("B")
        elif A==B==0:
            print "No"
            quit()
        elif A==B:
            try: 
                next_s=S[i+1]
                if next_s=="AB":    
                    A+=1
                    B-=1
                    L.append("A")
                elif next_s=="AC":    
                    A+=1
                    B-=1
                    L.append("A")
                elif next_s=="BC":    
                    A-=1
                    B+=1
                    L.append("B")
            except: 
                A+=1
                B-=1
                L.append("A")
    elif s=="AC":
        if A<C:
            A+=1
            C-=1
            L.append("A")
        elif C<A:
            A-=1
            C+=1
            L.append("C")
        elif A==C==0:
            print "No"
            quit()
        elif A==C:
            try: 
                next_s=S[i+1]
                if next_s=="AC":    
                    A+=1
                    C-=1
                    L.append("A")
                elif next_s=="AB":    
                    A+=1
                    C-=1
                    L.append("A")
                elif next_s=="BC":    
                    A-=1
                    C+=1
                    L.append("C")
            except: 
                A+=1
                C-=1
                L.append("A")
    elif s=="BC":
        if B<C:
            C-=1
            B+=1
            L.append("B")
        elif C<B:
            B-=1
            C+=1
            L.append("C")
        elif B==C==0:
            print "No"
            quit()
        elif B==C:
            try: 
                next_s=S[i+1]
                if next_s=="BC":    
                    B+=1
                    C-=1
                    L.append("B")
                elif next_s=="AB":    
                    B+=1
                    C-=1
                    L.append("B")
                elif next_s=="AC":    
                    B-=1
                    C+=1
                    L.append("C")
            except: 
                A+=1
                C-=1
                L.append("A")
print "Yes"
for x in L:
    print x

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
n, *abc = map(int, input().split())
s = [input().rstrip()for _ in range(n)]
cond = {'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}
legend = ('A', 'B', 'C')
ans = []


def dfs(i):
    if -1 in abc:
        return 0
    elif i == n:
        print('Yes')
        print(*ans, sep='\n')
        exit()
        
    else:
        idx1, idx2 = cond[s[i]]
        abc[idx1] += 1
        abc[idx2] -= 1
        ans.append(legend[idx1])
        dfs(i+1)
        ans.pop()
        abc[idx1] -= 1
        abc[idx2] += 1

        abc[idx1] -= 1
        abc[idx2] += 1
        ans.append(legend[idx2])
        dfs(i+1)
        ans.pop()
        abc[idx1] += 1
        abc[idx2] -= 1


dfs(0)
print('No')

Solution (Python):


n, a, b, c = map(int, input().split())
s = [input() for i in range(n)]

def decode(c):
    return ord(c) - ord('A')

state = [a, b, c]
choice = []
for i in range(n):
    
    j, k = map(decode, s[i])

    
    if state[j] == state[k] == 0:
        print('No')
        break

    
    if state[j] > state[k]:
        j, k = k, j

    
    if state[j] == state[k] == 1 and i < n - 1:
        xj, xk = map(decode, s[i + 1])
        if state[xj] + state[xk] == 1 and k in {xj, xk}:
            j, k = k, j

    
    state[j] += 1
    state[k] -= 1
    choice.append('ABC'[j])
else:
    print('Yes')
    for c in choice:
        print(c)

Solution (Python):
N, A, B, C = map(int, input().split())
if A + B + C == 0:
    print('No')
elif A + B + C == 1:
    res = []
    for _ in range(N):
        s = input()
        if s == 'AB':
            if A == 0 and B == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0 and C == 0:
                print('No')
                break
            elif A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0 and C == 0:
                print('No')
                break
            elif B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
    else:
        print('Yes')
        print('\n'.join(res))
else:
    res = []
    s = input()
    if (s == 'AB' and A == 0 and B == 0) or (s == 'AC' and A == 0 and C == 0) or (s == 'BC' and B == 0 and C == 0):
        print('No')
    else:
        for _ in range(N - 1):
            next = input()
            if s == 'AB':
                if A == 0:
                    B -= 1
                    A += 1
                    res.append('A')
                elif B == 0:
                    B += 1
                    A -= 1
                    res.append('B')
                else:
                    if next == 'AB':
                        A += 1
                        B -= 1
                        res.append('A')
                    elif next == 'AC':
                        A += 1
                        B -= 1
                        res.append('A')
                    else:
                        A -= 1
                        B += 1
                        res.append('B')
            elif s == 'AC':
                if A == 0:
                    C -= 1
                    A += 1
                    res.append('A')
                elif C == 0:
                    C += 1
                    A -= 1
                    res.append('C')
                else:
                    if next == 'AC':
                        A += 1
                        C -= 1
                        res.append('A')
                    elif next == 'AB':
                        A += 1
                        C -= 1
                        res.append('A')
                    else:
                        A -= 1
                        C += 1
                        res.append('C')
            else:
                if C == 0:
                    B -= 1
                    C += 1
                    res.append('C')
                elif B == 0:
                    B += 1
                    C -= 1
                    res.append('B')
                else:
                    if next == 'CB':
                        C += 1
                        B -= 1
                        res.append('C')
                    elif next == 'AC':
                        C += 1
                        B -= 1
                        res.append('C')
                    else:
                        C -= 1
                        B += 1
                        res.append('B')
            s = next
        if s == 'AB':
            if A == 0:
                A += 1
                B -= 1
                res.append('A')
            else:
                A -= 1
                B += 1
                res.append('B')
        elif s == 'AC':
            if A == 0:
                A += 1
                C -= 1
                res.append('A')
            else:
                A -= 1
                C += 1
                res.append('C')
        else:
            if B == 0:
                B += 1
                C -= 1
                res.append('B')
            else:
                B -= 1
                C += 1
                res.append('C')
        print('Yes')
        print('\n'.join(res))

Solution (Python):

abc = ["A","B","C"]

def calculate(x,y, q1,q2):
    flag = 0
    ans = ""
    if x == 1 and y == 1:
        if q1 == q2:
            y -= 1
            x += 1
            ans = abc[q1[0]]

        elif q2 != "":
            t = set(q1) & set(q2)
            s = set(q1) - t
            
            
            if int(list(t)[0]) < int(list(s)[0]):
                y -= 1
                x += 1
                ans = abc[q1[0]]
            else:
                x -= 1
                y += 1
                ans = abc[q1[1]]
        else:
            y -= 1
            x += 1
            ans = abc[q1[0]]

    elif x <= y:
        y -= 1
        x += 1
        
        if y < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[0]]
    else:
        x -= 1
        y += 1
        ans = ""
        
        if x < 0:
            print("No")
            flag = 1
            return(x, y, ans, flag)
        ans = abc[q1[1]]
    return (x, y, ans, flag)

def main():
    N, A, B, C  =  [int(i) for i in input().split()]
    s = []
    for i in range(N):
        t = input()
        if t == "AB":
            s.append([0,1])
        if t == "AC":
            s.append([0,2])
        if t == "BC":
            s.append([1,2])
    
    answers  = []
    for i, q1 in enumerate(s):
        ans = ""
        f = 0
        q2 = ""
        if i+1 != N:
            q2 = s[i+1]

        if q1 == [0,1]:
            
            A, B, ans, f = calculate(A,B,q1,q2)
        if q1 == [1,2]:
            
            B, C, ans,f  = calculate(B,C,q1,q2)
        if q1 == [0,2]:
            
            A, C, ans,f  = calculate(A,C,q1,q2)
        answers.append(ans)
        if f == 1:
            return
    print("Yes")

    for a in answers:
        print(a)
if __name__ == "__main__":
    main()

Solution (Python):
MOD = 10 ** 9 + 7
INF = 10 ** 11
import sys
sys.setrecursionlimit(100000000)

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

    ans = []
    for i in range(N):
        s = S[i]
        if s == 'AB':
            if A > 1 and B > 1:
                if A > B:
                    A -= 1
                    B += 1
                    ans.append('B')
                else:
                    B -= 1
                    A += 1
                    ans.append('A')
            elif A == 1 and B > 1:
                B -= 1
                A += 1
                ans.append('A')
            elif A > 1  and B == 1:
                A -= 1
                B += 1
                ans.append('B')
            elif A == 1 and B == 1:
                if i + 1 < N:
                    if S[i + 1] == 'AB':
                        A += 1
                        B -= 1
                        ans.append('A')
                    elif S[i + 1] == 'BC':
                        B += 1
                        A -= 1
                        ans.append('B')
                    else:
                        A += 1
                        B -= 1
                        ans.append('A')
                else:
                    ans.append('A')
            elif A == 0 and B == 0:
                print('No')
                return
            elif A == 0:
                A += 1
                B -= 1
                ans.append('A')
            elif B == 0:
                B += 1
                A -= 1
                ans.append('B')
        
        elif s == 'BC':
            if C > 1 and B > 1:
                if C > B:
                    C -= 1
                    B += 1
                    ans.append('B')
                else:
                    B -= 1
                    C += 1
                    ans.append('C')
            elif C == 1 and B > 1:
                B -= 1
                C += 1
                ans.append('C')
            elif C > 1  and B == 1:
                C -= 1
                B += 1
                ans.append('B')
            elif C == 1 and B == 1:
                if i + 1 < N:
                    if S[i + 1] == 'AB':
                        C -= 1
                        B += 1
                        ans.append('B')
                    elif S[i + 1] == 'BC':
                        B += 1
                        C -= 1
                        ans.append('B')
                    else:
                        C += 1
                        B -= 1
                        ans.append('C')
                else:
                    ans.append('B')
            elif C == 0 and B == 0:
                print('No')
                return
            elif C == 0:
                C += 1
                B -= 1
                ans.append('C')
            elif B == 0:
                B += 1
                C -= 1
                ans.append('B')
        
        elif s == 'AC':
            if A > 1 and C > 1:
                if A > C:
                    A -= 1
                    C += 1
                    ans.append('C')
                else:
                    C -= 1
                    A += 1
                    ans.append('A')
            elif A == 1 and C > 1:
                C -= 1
                A += 1
                ans.append('A')
            elif A > 1  and C == 1:
                A -= 1
                C += 1
                ans.append('C')
            elif A == 1 and C == 1:
                if i + 1 < N:
                    if S[i + 1] == 'AB':
                        A += 1
                        C -= 1
                        ans.append('A')
                    elif S[i + 1] == 'BC':
                        C += 1
                        A -= 1
                        ans.append('C')
                    else:
                        A += 1
                        C -= 1
                        ans.append('A')
                else:
                    ans.append('A')
            elif A == 0 and C == 0:
                print('No')
                return
            elif A == 0:
                A += 1
                C -= 1
                ans.append('A')
            elif C == 0:
                C += 1
                A -= 1
                ans.append('C')

    print('Yes')
    print(*ans,sep = '\n')
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N, A, B, C = [int(i) for i in input().split()]

    if A == B == C == 0:
        print("No")
        return

    S = [input() for _ in range(N)]
    d = {"A": A, "B": B, "C": C}
    order = []

    for i, s in enumerate(S):
        s1, s2 = s

        if d[s1] == d[s2] == 0:
            print("No")
            return
        elif d[s1] == 0:
            d[s1] += 1
            d[s2] -= 1
            order.append(s1)
        elif d[s2] == 0:
            d[s2] += 1
            d[s1] -= 1
            order.append(s2)
        elif d[s1] == d[s2] == 1:
            try:
                if s1 in S[i + 1]:
                    d[s1] += 1
                    d[s2] -= 1
                    order.append(s1)
                else:
                    d[s2] += 1
                    d[s1] -= 1
                    order.append(s2)
            except:
                order.append(s1)
        else:
            if d[s1] > d[s2]:
                d[s1] -= 1
                d[s2] += 1
                order.append(s2)
            else:
                d[s2] -= 1
                d[s1] += 1
                order.append(s1)
    
    print("Yes")
    for o in order:
        print(o)


main()