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

s = list()
for i in range(n):
    s.append(input())

r = list()

for i in range(n):
    if s[i]=='AB':
        if a>b:
            r.append('B')
            a=a-1
            b=b+1
        elif a<b:
            r.append('A')
            a=a+1
            b=b-1
        else:
            if i==n-1 or 'A' in s[i+1]:
                r.append('A')
                a=a+1
                b=b-1
            else: 
                r.append('B')
                a=a-1
                b=b+1
    elif s[i]=='AC':
        if a>c:
            r.append('C')
            a=a-1
            c=c+1
        elif a<c:
            r.append('A')
            a=a+1
            c=c-1
        else:
            if i==n-1 or 'A' in s[i+1]:
                r.append('A')
                a=a+1
                c=c-1
            else: 
                r.append('C')
                a=a-1
                c=c+1
    else:
        if b>c:
            r.append('C')
            b=b-1
            c=c+1
        elif b<c:
            r.append('B')
            b=b+1
            c=c-1
        else:
            if i==n-1 or 'B' in s[i+1]:
                r.append('B')
                b=b+1
                c=c-1
            else: 
                r.append('C')
                b=b-1
                c=c+1
    if min(a,b,c)<0:
        print('No')
        sys.exit()


print('Yes')
for str in r:
    print(str)

Solution (Python):
n,a,b,c = map(int, input().split())
m = {'A':a, 'B':b, 'C':c}

ss = [input() for _ in range(n)]


ans = []
def _t(s0, s1):
    m[s0] = m[s0] + 1
    m[s1] = m[s1] - 1
    ans.append(s0)


if a+b+c == 0:
    print('No')
    exit(0)


elif a+b+c == 1:
    for k, v in m.items():
        if v == 1:
            o = k

    for s in ss:
        if o not in s:
            print('No')
            exit(0)
        o = s.strip(o)
        ans.append(o)

elif a+b+c == 2:
    for i, s in enumerate(ss):
        if m[s[0]] + m[s[1]] == 0:
            print('No')
            exit(0)
        if m[s[0]] > m[s[1]]:
            _t(s[1], s[0])
        elif m[s[0]] < m[s[1]]:
            _t(s[0], s[1])
        else:
            if i < len(ss)-1 and s[0] in ss[i+1]:
                _t(s[0], s[1])
            else:
                _t(s[1], s[0])

else:
    for s in ss:
        if m[s[0]] + m[s[1]] == 0:
            print('No')
            exit(0)
        if m[s[0]] > m[s[1]]:
            _t(s[1], s[0])
        else:
            _t(s[0], s[1])

print('Yes')
for a in ans:
    print(a)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 5 + 10)

N, A, B, C = [int(i) for i in input().split()]

ans = list()
l = list()

for n in range(N):
    l.append(input())

def dfs(i, a, b, c):
    if i == N:
        return True

    ss = l[i]
    if ss == 'AB':
        if a == 0 and b == 0:
            return False
        if b > 0 and dfs(i + 1, a + 1, b - 1, c):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b + 1, c):
            ans.append('B')
            return True
    if ss == 'AC':
        if a == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a + 1, b, c - 1):
            ans.append('A')
            return True
        if a > 0 and dfs(i + 1, a - 1, b, c + 1):
            ans.append('C')
            return True
    if ss == 'BC':
        if b == 0 and c == 0:
            return False
        if c > 0 and dfs(i + 1, a, b + 1, c - 1):
            ans.append('B')
            return True
        if b > 0 and dfs(i + 1, a, b - 1, c + 1):
            ans.append('C')
            return True
    return False

dfs(0, A, B, C)

if len(ans) > 0:
    print('Yes')
    for x in reversed(ans):
        print(x)
else:
    print('No')

Solution (Python):
from sys import setrecursionlimit
setrecursionlimit(10 ** 6)

*I, = open(0).read().split()
N, A, B, C = map(int, I[:4])
S = I[4:]

Cnt = {"A": A, "B": B, "C": C}

ans = [""] * N
def dfs(i):
    if i == N:
        print("Yes")
        print("\n".join(ans))
        quit()

    for L, R in (S[i], reversed(S[i])):
        if Cnt[R] > 0:
            Cnt[L] += 1
            Cnt[R] -= 1

            ans[i] = L
            dfs(i + 1)

            Cnt[L] -= 1
            Cnt[R] += 1


dfs(0)
print("No")

Solution (Python):
from sys import setrecursionlimit
setrecursionlimit(10 ** 6)

stdin = open(0)

N, A, B, C = map(int, stdin.readline().split())
S = stdin.read().split()

ans = [""] * N
def dfs(i, a, b, c):
    if a < 0 or b < 0 or c < 0:
        return

    if i == N:
        print("Yes")
        print("\n".join(ans))
        quit()

    if S[i] == "AB":
        ans[i] = "A"
        dfs(i + 1, a + 1, b - 1, c)

        ans[i] = "B"
        dfs(i + 1, a - 1, b + 1, c)

    elif S[i] == "BC":
        ans[i] = "B"
        dfs(i + 1, a, b + 1, c - 1)

        ans[i] = "C"
        dfs(i + 1, a, b - 1, c + 1)
    else:
        ans[i] = "A"
        dfs(i + 1, a + 1, b, c - 1)

        ans[i] = "C"
        dfs(i + 1, a - 1, b, c + 1)

dfs(0, A, B, C)
print("No")

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
ans=''
def dfs(a,b,c,t):
    global ans
    if a<0 or b<0 or c<0:
        return False
    if t==N:
        return True
    if s[t]=='AB':
        if dfs(a-1,b+1,c,t+1):
            ans+='B'
            return True
        elif dfs(a+1,b-1,c,t+1):
            ans+='A'
            return True
    if s[t]=='BC':
        if dfs(a,b-1,c+1,t+1):
            ans+='C'
            return True
        elif dfs(a,b+1,c-1,t+1):
            ans+='B'
            return True
    if s[t]=='AC':
        if dfs(a+1,b,c-1,t+1):
            ans+='A'
            return True
        if dfs(a-1,b,c+1,t+1):
            ans+='C'
            return True
    return False

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

if dfs(A,B,C,0):
    print('Yes')
    print(*ans[::-1],sep='\n')
else:
    print('No')

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
ans=''
def dfs(a,b,c,t):
    global ans
    if a<0 or b<0 or c<0:
        return False
    if t==N:
        return True
    if s[t]=='AB':
        if dfs(a-1,b+1,c,t+1):
            ans+='B'
            return True
        elif dfs(a+1,b-1,c,t+1):
            ans+='A'
            return True
    if s[t]=='BC':
        if dfs(a,b-1,c+1,t+1):
            ans+='C'
            return True
        elif dfs(a,b+1,c-1,t+1):
            ans+='B'
            return True
    if s[t]=='AC':
        if dfs(a+1,b,c-1,t+1):
            ans+='A'
            return True
        if dfs(a-1,b,c+1,t+1):
            ans+='C'
            return True
    return False

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

if dfs(A,B,C,0):
    print('Yes')
    print(*ans[::-1],sep='\n')
else:
    print('No')

Solution (Python):
n, a, b, c = map(int, input().split())
q = [input() for i in range(n)]
ab, ac, bc = "AB", "AC", "BC"
ans = []
for i in range(n):
  
  if((q[i] == ab and a == b == 0)
  or (q[i] == ac and a == c == 0) 
  or (q[i] == bc and b == c == 0)):
      print("No")
      exit()
      
  elif(q[i] == ab and [a, b, c] == [1, 1, 0] and i != n - 1):
    if q[i+1] == ac: 
      a, b = a+1, b-1
      ans.append("A")
    else:
      a, b = a-1, b+1
      ans.append("B")     
      
  elif(q[i] == ac and [a, b, c] == [1, 0, 1] and i != n - 1):
    if q[i+1] == ab:
      a, c = a+1, c-1
      ans.append("A")
    else:
      a, c = a-1, c+1
      ans.append("C")
      
  elif(q[i] == bc and [a, b, c] == [0, 1, 1] and i != n - 1):
    if q[i+1] == ab:
      b, c = b+1, c-1
      ans.append("B")
    else:
      b, c = b-1, c+1
      ans.append("C")
      
  elif q[i] == ab:
    if a > b:
      a, b = a-1, b+1
      ans.append("B")
    else:
      a, b = a+1, b-1
      ans.append("A")
      
  elif q[i] == ac:
    if a > c:
      a, c = a-1, c+1
      ans.append("C")
    else:
      a, c = a+1, c-1
      ans.append("A")
      
  elif q[i] == bc:
    if b > c:
      b, c = b-1, c+1
      ans.append("C")
    else:
      b, c = b+1, c-1
      ans.append("B")

print("Yes")
for i in range(n):
  print(ans[i])

Solution (Python):
n,A,B,C=map(int,input().split())
r=[A,B,C]
g=list()
for i in range(n):
    g.append(str(input()))
    
def re(string):
    if string=="AB":
        return(0,1)
    elif string=="AC":
        return(0,2)
    else:
        return(1,2)
    
def al(num):
    if num==0:
        return "A"
    elif num==1:
        return "B"
    else:
        return "C"
    
flag=0
result=list()
for i in range(n):
    a,b=re(g[i])
    if r[a]==0 and r[b]==0:
        print("No")
        flag=1
        break
    elif r[a]>r[b]:
        r[a]-=1
        r[b]+=1
        result.append(al(b))
    elif r[a]<r[b]:
        r[a]+=1
        r[b]-=1
        result.append(al(a))
    else:
        if i+1==n:
            r[a]+=1
            r[b]-=1
            result.append(al(a))
            break
        x,y=re(g[i+1])
        if (x,y)==(a,b):
            r[a]+=1
            r[b]-=1
            result.append(al(a))
        else:
            if x in (a,b):
                r[x]+=2
                r[a]-=1;r[b]-=1
                result.append(al(x))
            else:
                r[y]+=2
                r[a]-=1;r[b]-=1
                result.append(al(y))
        
if flag==0:
    print("Yes")
    for item in result:
        print(item)

Solution (Python):
import sys


def main():
    N,A,B,C = map(int,input().split())
    use = [A,B,C]
    conv = ["A","B","C"]
    
    l = []
    for _ in range(N):
        s = input()
        if s == "AB":
            s = 0
        elif s == "BC":
            s = 1
        else:
            s = 2
        l.append(s)
        
    ans = []
        
    for i in range(N):
        num = l[i]
        if use[num]+use[(num+1)%3] == 0:
            print("No")
            exit()
        elif use[num] == use[(num+1)%3] == 1:
            if i == N-1:
                ans.append(conv[num])
                use[num] += 1
                use[(num+1)%3] -= 1
            else:
                _num = l[i+1]
                if num == _num:
                    ans.append(conv[num])
                    use[num] += 1
                    use[(num+1)%3] -= 1
                else:
                    if (_num+3-num)%3 == 1:
                        ans.append(conv[_num])
                        use[_num] += 1
                        use[(_num-1)%3] -= 1
                    else:
                        ans.append(conv[num])
                        use[num] += 1
                        use[(num+1)%3] -= 1
        else:
            if use[num] >= use[(num+1)%3]:
                ans.append(conv[(num+1)%3])
                use[num] -= 1
                use[(num+1)%3] += 1
            else:
                ans.append(conv[num])
                use[num] += 1
                use[(num+1)%3] -= 1

    print("Yes")
    for st in ans:
        print(st)
    
if __name__ == "__main__":
	main()

Solution (Python):
def update_vrs(vrs, idx, s):
    if idx == 0:
        vrs[s[0]] += 1
        vrs[s[1]] -= 1
    elif idx == 1:
        vrs[s[0]] -= 1
        vrs[s[1]] += 1
    else:
        raise ValueError()


def process1(vrs, lst_s):
    selects = []
    for s in lst_s:
        if vrs[s[0]] == 0 and vrs[s[1]] == 0:
            return False, None
        elif vrs[s[0]] == 0:
            selects.append(s[0])
            update_vrs(vrs, 0, s)
        else:  
            selects.append(s[1])
            update_vrs(vrs, 1, s)
    return True, selects


def process2(vrs, lst_s):
    selects = []
    for i, s in enumerate(lst_s):
        if vrs[s[0]] == 0 and vrs[s[1]] == 0:
            return False, None
        elif vrs[s[0]] == 0:
            selects.append(s[0])
            update_vrs(vrs, 0, s)
        elif vrs[s[1]] == 0:
            selects.append(s[1])
            update_vrs(vrs, 1, s)
        else:
            if i == len(lst_s) - 1:
                selects.append(s[0])
            else:
                next_s = lst_s[i + 1]
                if s[0] in next_s:
                    selects.append(s[0])
                    update_vrs(vrs, 0, s)
                else:
                    selects.append(s[1])
                    update_vrs(vrs, 1, s)
    return True, selects


def main():
    vrs = {}
    n, vrs['A'], vrs['B'], vrs['C'] = [int(v) for v in input().split(' ')]
    lst_s = []
    for _ in range(n):
        lst_s.append(input().strip())

    sum_abc = vrs['A'] + vrs['B'] + vrs['C']

    if sum_abc == 0:
        ans = False
    elif sum_abc == 1:
        ans, selects = process1(vrs, lst_s)
    else:
        ans, selects = process2(vrs, lst_s)

    if ans is True:
        print('Yes')
        for s in selects:
            print(s)
    else:
        print('No')


if __name__ == '__main__':
    main()

Solution (Python):
def update_vrs(vrs, idx, s):
    if idx == 0:
        vrs[s[0]] += 1
        vrs[s[1]] -= 1
    elif idx == 1:
        vrs[s[0]] -= 1
        vrs[s[1]] += 1
    else:
        raise ValueError()


def process1(vrs, lst_s):
    selects = []
    for s in lst_s:
        if vrs[s[0]] == 0 and vrs[s[1]] == 0:
            return False, None
        elif vrs[s[0]] == 0:
            selects.append(s[0])
            update_vrs(vrs, 0, s)
        else:  
            selects.append(s[1])
            update_vrs(vrs, 1, s)
    return True, selects


def process2(vrs, lst_s):
    selects = []
    for i, s in enumerate(lst_s):
        if vrs[s[0]] == 0 and vrs[s[1]] == 0:
            return False, None
        elif vrs[s[0]] == 0:
            selects.append(s[0])
            update_vrs(vrs, 0, s)
        elif vrs[s[1]] == 0:
            selects.append(s[1])
            update_vrs(vrs, 1, s)
        else:
            if i == len(lst_s) - 1:
                selects.append(s[0])
            else:
                next_s = lst_s[i + 1]
                if s[0] in next_s:
                    selects.append(s[0])
                    update_vrs(vrs, 0, s)
                else:
                    selects.append(s[1])
                    update_vrs(vrs, 1, s)
    return True, selects


def main():
    vrs = {}
    n, vrs['A'], vrs['B'], vrs['C'] = [int(v) for v in input().split(' ')]
    lst_s = []
    for _ in range(n):
        lst_s.append(input().strip())

    sum_abc = vrs['A'] + vrs['B'] + vrs['C']

    if sum_abc == 0:
        ans = False
    elif sum_abc == 1:
        ans, selects = process1(vrs, lst_s)
    else:
        ans, selects = process2(vrs, lst_s)

    if ans is True:
        print('Yes')
        for s in selects:
            print(s)
    else:
        print('No')


if __name__ == '__main__':
    main()

Solution (Python):
n, a, b, c = list(map(int, input().split()))
s = [list(input()) for _ in range(n)]
s.append(["A", "B"])
d = {"A": a, "B": b, "C": c}
ans = []

def calc(add, sub):
    d[add] += 1
    d[sub] -= 1
    ans.append(add)

for i, [j, k] in enumerate(s):
    if i == n:
        print('Yes')
        print('\n'.join(ans))
        break
    if d[j] == 0 and d[k] == 0:
        print("No")
        break
    elif j == "A" and k == "B":
        
        if d[j] < d[k]:
            calc(j, k)
        elif d[j] > d[k]:
            calc(k, j)
        else:
            if s[i + 1][0] == "A":
                calc(j, k)
            else:
                calc(k, j)
    elif j == "A" and k == "C":

        if d[j] < d[k]:
            calc(j, k)
        elif d[j] > d[k]:
            calc(k, j)
        else:
            if s[i + 1][0] == "A":
                calc(j, k)
            else:
                calc(k, j)
    elif j == "B" and k == "C":

        if d[j] < d[k]:
            calc(j, k)
        elif d[j] > d[k]:
            calc(k, j)
        else:
            if s[i + 1][0] == "A" and s[i + 1][1] == "C":
                calc(k, j)
            else:
                calc(j, k)

Solution (Python):
def solve(A, B, C, S):
    N = len(S)
    abc = [A, B, C]
    path = []
    for si in range(N - 1):
        i, j = S[si]
        if abc[i] == abc[j] == 0:
            print('No')
            return
        if abc[i] == 0:
            path.append('ABC'[i])
            abc[i] += 1
            abc[j] -= 1
            continue
        elif abc[j] == 0:
            path.append('ABC'[j])
            abc[j] += 1
            abc[i] -= 1
            continue

        i2, j2 = S[si + 1]
        if not (abc[j] - 1 == 0 and abc[3 - i - j] == 0 and {i2, j2} == {3-i-j, j}):
            path.append('ABC'[i])
            abc[i] += 1
            abc[j] -= 1
            continue
        if not (abc[i] - 1 == 0 and abc[3 - i - j] == 0 and {i2, j2} == {3-i-j, i}):
            path.append('ABC'[j])
            abc[j] += 1
            abc[i] -= 1
            continue

        if abc[i] <= abc[j]:
            path.append('ABC'[i])
            abc[i] += 1
            abc[j] -=1
        else:
            path.append('ABC'[j])
            abc[j] += 1
            abc[i] -=1

    i, j = S[N - 1]
    if abc[i] == abc[j] == 0:
        print('No')
        return
    if abc[i] == 0:
        path.append('ABC'[i])
    else:
        path.append('ABC'[j])

    print('Yes')
    for c in path:
        print(c)


N, A, B, C = map(int, input().split())
S = []
for _ in range(N):
    s = input()
    S.append({'AB': (0, 1), 'AC': (0, 2), 'BC': (1, 2)}[s])
solve(A, B, C, S)

Solution (Python):


def check(s):
    if dict_x[s[0]] == 0 and dict_x[s[1]] == 0:
        return "NOT"
    elif  dict_x[s[0]] > 0 and dict_x[s[1]] == 0:
        return s[1]
    elif  dict_x[s[0]] == 0 and dict_x[s[1]] > 0:
        return s[0]
    elif dict_x[s[0]] == 1 and dict_x[s[1]] == 1:
        return "EVEN"
    else:
        if dict_x[s[0]] > dict_x[s[1]]:
            return s[1]
        else:
            return s[0] 
def update(s, mozi):
    if s[1] == mozi:
        dict_x[s[0]] -= 1
        dict_x[s[1]] += 1
    else:
        dict_x[s[0]] += 1
        dict_x[s[1]] -= 1

n, a, b, c = map(int, input().split())
dict_x = {"A": a, "B": b, "C": c}
s_list = []
ans_list = []
flag = 1
for i in range(0, n):
    s = input()
    s_list.append(s)
if a + b + c == 0:
    flag = 0
elif a + b + c == 1:
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        else:
            ans_list.append(x)
            update(s, x)
elif a + b + c == 2:
    i = 0
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        elif x == "EVEN":
            if i == len(s_list) - 1:
                ans_list.append(s[0])
                update(s, x)
            elif s == s_list[i + 1]:
                ans_list.append(s[0])
                update(s, x)
            else:
                if s[0] in s_list[i + 1]:
                    ans_list.append(s[0])
                    update(s, s[0])
                else:
                    ans_list.append(s[1])
                    update(s, s[1])
        else:
            ans_list.append(x)
            update(s, x)
        i += 1
else:
    for s in s_list:
        x = check(s)
        if x == "NOT":
            flag = 0
            break
        elif x == "EVEN":
            ans_list.append(s[0])
            update(s, s[0])
        else:
            ans_list.append(x)
            update(s, x)

if flag == 1:
    print("Yes")
    for ans in ans_list:
        print(ans)
else:
    print("No")

Solution (Python):
import sys
readline = sys.stdin.readline

def main():
  N, a, b, c = map(int, readline().split())
  A = {"A":a, "B":b, "C":c}
  S = []
  for i in range(N):
    l = readline().strip()
    S.append([l[0], l[1]])

  ans = ""
  ret = False
  for i in range(N):
    if A[S[i][0]] == 0 and A[S[i][1]] == 0:
      break
    if i == N-1:
      if A[S[i][0]] > 0:
        ans += S[i][1]
      else:
        ans += S[i][0]
      ret = True
      break
    if A["A"] + A["B"] + A["C"] == 1:
      if A[S[i][0]] == 1:
        if S[i][0] in S[i+1] and S[i][1] not in S[i+1]:
          break
        else:
          A[S[i][0]] -= 1
          A[S[i][1]] += 1
          ans += S[i][1]
          continue
      else:
        if S[i][1] in S[i+1] and S[i][0] not in S[i+1]:
          break
        else:
          A[S[i][0]] += 1
          A[S[i][1]] -= 1
          ans += S[i][0]
          continue
    if A[S[i][0]] == 0:
      A[S[i][0]] += 1
      A[S[i][1]] -= 1
      ans += S[i][0]
      continue
    elif A[S[i][1]] == 0:
      A[S[i][0]] -= 1
      A[S[i][1]] += 1
      ans += S[i][1]
      continue
    elif S[i][0] in S[i+1]:
      A[S[i][0]] += 1
      A[S[i][1]] -= 1
      ans += S[i][0]
      continue
    else:
      A[S[i][0]] -= 1
      A[S[i][1]] += 1
      ans += S[i][1]
      continue

  if ret:
    print("Yes")
    for i in range(N):
      print(ans[i])
  else:
    print("No")

if __name__ == '__main__':
  main()

Solution (Python):
import sys
readline = sys.stdin.readline


def solve(writer):
    N, A, B, C = map(int, input().split())

    variables = {
        'A': A,
        'B': B,
        'C': C
    }

    queries = (list(readline().strip()) for _ in range(N))

    next_q = next(queries)

    for i in range(N):

        let_1, let_2 = next_q
        next_q = next(queries) if i < N - 1 else ()

        if variables[let_1] == variables[let_2] == 0:
            return False

        if variables[let_1] == variables[let_2] == 1 and let_2 in next_q \
                or variables[let_1] > variables[let_2]:
            let_1, let_2 = let_2, let_1

        variables[let_1] += 1
        variables[let_2] -= 1

        writer.append(let_1)

    return True


def main():
    
    writer = list()
    ok = solve(writer)
    
    if ok:
        print('Yes')
        for w in writer:
            print(w)
    else:
        print('No')


if __name__ == "__main__":
    main()

Solution (Python):
import sys
readline = sys.stdin.readline


def solve(writer):
    N, A, B, C = map(int, input().split())

    variables = {
        'A': A,
        'B': B,
        'C': C
    }

    queries = (list(readline().strip()) for _ in range(N))

    next_q = next(queries)

    for i in range(N):

        let_1, let_2 = next_q
        next_q = next(queries) if i != N - 1 else ()

        if variables[let_1] == variables[let_2] == 0:
            return False

        if variables[let_1] == variables[let_2] == 1 and let_2 in next_q \
                or variables[let_1] > variables[let_2]:
            let_1, let_2 = let_2, let_1

        variables[let_1] += 1
        variables[let_2] -= 1

        writer.append(let_1)

    return True


def main():
    writer = ['Yes']
    ok = solve(writer)
    if ok:
        for w in writer:
            print(w)
    else:
        print('No')


if __name__ == "__main__":
    main()

Solution (Python):
def solve(writer):
    N, A, B, C = map(int, input().split())
    variables = {
        'A': A,
        'B': B,
        'C': C
    }

    queries = (list(input()) for _ in range(N))

    buffer = list()

    for i in range(N):

        if not buffer:
            t1, t2 = next(queries)
        else:
            b = buffer.pop()
            if b == 'pass':
                continue
            else:
                t1, t2 = list(b)

        v1 = variables[t1]
        v2 = variables[t2]

        if v1 == v2 == 0:
            return False

        elif v1 == v2 == 1:
            if i == N-1:
                writer.append(t1)
                return True

            nt1, nt2 = next(queries)
            if len({t1, t2, nt1, nt2}) == 2:
                buffer.append('pass')
                writer.append(t1)
                writer.append(t2)
                continue

            if t1 in {nt1, nt2}:
                variables[t1] += 1
                variables[t2] -= 1
                writer.append(t1)
            else:
                variables[t1] -= 1
                variables[t2] += 1
                writer.append(t2)

            buffer.append(''.join([nt1, nt2]))

        elif v1 > v2:
            variables[t1] -= 1
            variables[t2] += 1
            writer.append(t2)
        elif v2 > v1:
            variables[t1] += 1
            variables[t2] -= 1
            writer.append(t1)
        else:
            variables[t1] += 1
            variables[t2] -= 1
            writer.append(t1)

    return True


def main():
    writer = list()
    ok = solve(writer)
    if ok:
        print('Yes')
        for w in writer:
            print(w)
    else:
        print('No')


if __name__ == "__main__":
    main()

Solution (Python):
import sys
n, a, b, c = map(int, input().split())
s = []
for _ in range(n):
    s.append(input())
    
ans =[]
for i in range(n):
    if s[i] == "AB":
        if a == b == 1 and i < n-1:
            if s[i+1] == "AC":
                a += 1
                b -= 1
                ans.append("A")
            else:
                a -= 1
                b+= 1
                ans.append("B")
        else:
            if min(a, b) == a:
                a += 1
                b -= 1
                ans.append("A")
            else:
                a -= 1
                b+= 1
                ans.append("B")
    elif s[i] == "BC":
        if c == b == 1 and i < n-1:
            if s[i+1] == "AB":
                b += 1
                c -= 1
                ans.append("B")
            else:
                b -= 1
                c+= 1
                ans.append("C")
        else:
            if min(c, b) ==b:
                b += 1
                c -= 1
                ans.append("B")
            else:
                b -= 1
                c+= 1
                ans.append("C")
    else:
        if c == a == 1 and i < n-1:
            if s[i+1] == "BC":
                c += 1
                a -= 1
                ans.append("C")
            else:
                a += 1
                c-= 1
                ans.append("A")
        else:
            if min(c, a) == c:
                c += 1
                a -= 1
                ans.append("C")
            else:
                c -= 1
                a += 1
                ans.append("A")
    if min(a, b, c) < 0:
        print("No")
        sys.exit()

print("Yes")
print("\n".join(ans))

Solution (Python):
n,a,b,c=map(int,input().split())
s=[0]*(n+1)
for i in range(n):
    s[i]=input()
l=[0]*(n+1)
for i in range(n):
    if s[i]=="AB":
        if a==b==0:
            print("No")
            exit()
        if a+b+c==2:
            if a==b==1:
                if s[i+1]=="AC":
                    a+=1
                    b-=1
                    l[i]="A"
                    continue
                elif s[i+1]=="BC":
                    a-=1
                    b+=1
                    l[i]="B"
                    continue
        if a>=b:
            a-=1
            b+=1
            l[i]="B"
        else:
            a+=1
            b-=1
            l[i]="A"
    if s[i]=="BC":
        if c==b==0:
            print("No")
            exit()
        if a+b+c==2:
            if b==c==1:
                if s[i+1]=="AB":
                    c-=1
                    b+=1
                    l[i]="B"
                    continue
                elif s[i+1]=="AC":
                    b-=1
                    c+=1
                    l[i]="C"
                    continue
        if b>=c:
            b-=1
            c+=1
            l[i]="C"
        else:
            b+=1
            c-=1
            l[i]="B"
    if s[i]=="AC":
        if a==c==0:
            print("No")
            exit()
        if a+b+c==2:
            if a==c==1:
                if s[i+1]=="AB":
                    c-=1
                    a+=1
                    l[i]="A"
                    continue
                elif s[i+1]=="BC":
                    a-=1
                    c+=1
                    l[i]="C"
                    continue
        if a>=c:
            a-=1
            c+=1
            l[i]="C"
        else:
            a+=1
            c-=1
            l[i]="A"
if l[-2]!=0:
    print("Yes")
    for i in range(n):
        print(l[i])

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

si = [input() for i in range(n)]

if a == 0 and b == 0 and c== 0:
    print("No")
    exit()

s1 = 0
s2 = 0

abc = [a,b,c]
moji = ['A','B','C']

def checkmoji(s):
    if s == "AB":
        s1 = 0
        s2 = 1
    elif s == 'AC':
        s1 = 0
        s2 = 2
    elif s == 'BC':
        s1 = 1
        s2 = 2
    return s1,s2

ans = []

for i in range(n):
    s1,s2 = checkmoji(si[i])
    if abc[s1] == 0 and abc[s2] == 0:
        print('No')
        exit()
    if abc[s2] == 0:
        abc[s1] -= 1
        abc[s2] += 1
        ans.append(moji[s2])
    elif abc[s1] == 0:
        abc[s1] += 1
        abc[s2] -= 1
        ans.append(moji[s1])
    else:
        
        if i == n-1:
            ans.append(moji[s1])
            break
        
        s1_a,s2_a = checkmoji(si[i+1])
        if s1 == s1_a or s1 == s2_a:
            abc[s1] += 1
            abc[s2] -= 1
            ans.append(moji[s1])
        else:
            abc[s1] -= 1
            abc[s2] += 1
            ans.append(moji[s2])
print('Yes')
for i in range(len(ans)):
    print(ans[i])