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:
n,a,b,c = map(int,input().split())
ss = [input()for i in range(n)]
ok = True
ans = []
for i in range(n):
    s = ss[i]
    if s == 'AB':
        if a == 1 and b == 1 and c == 0:
            if  i+1 < n and ss[i+1] == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            if ss[i+1] == 'BC':
                ans.append('B')
                b += 1
                a -= 1
                continue
            
            
        if a == 0 and b == 0:
            ok = False
            break
        
        if a > b:
            b +=1
            a -= 1
            ans.append('B')
        else:
            b -= 1
            a += 1
            ans.append('A')
            
    elif s == 'AC':
        if a == 1 and b == 0 and c == 1:
            if  i+1 < n and ss[i+1] == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            if ss[i+1] == 'BC':
                ans.append('C')
                c += 1
                a -= 1
                continue
            
        if a == 0 and c == 0:
            ok = False
            break
        
        if a > c:
            c +=1
            a -= 1
            ans.append('C')
        else:
            c -= 1
            a += 1
            ans.append('A')
            
        
    else:
        if a == 0 and b == 1 and c == 1:
            if i+1 < n and  ss[i+1] == 'AC':
                ans.append('C')
                c += 1
                b -= 1
                continue
            if ss[i+1] == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
        if c == 0 and b == 0:
            ok = False
            break
        
        if c > b:
            b +=1
            c -= 1
            ans.append('B')
        else:
            b -= 1
            c += 1
            ans.append('C')
if ok:
    print('Yes')
    for x in ans:
        print(x)
else:print('No')

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
ss = [input()for i in range(n)]
ok = True
ans = []
for i in range(n):
    s = ss[i]
    if s == 'AB':
        if a == 1 and b == 1 and c == 0:
            if  ss[i+1] == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            if ss[i+1] == 'BC':
                ans.append('B')
                b += 1
                a -= 1
                continue
            
            
        if a == 0 and b == 0:
            ok = False
            break
        
        if a > b:
            b +=1
            a -= 1
            ans.append('B')
        else:
            b -= 1
            a += 1
            ans.append('A')
            
    elif s == 'AC':
        if a == 1 and b == 0 and c == 1:
            if  ss[i+1] == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            if ss[i+1] == 'BC':
                ans.append('C')
                c += 1
                a -= 1
                continue
            
        if a == 0 and c == 0:
            ok = False
            break
        
        if a > c:
            c +=1
            a -= 1
            ans.append('C')
        else:
            c -= 1
            a += 1
            ans.append('A')
            
        
    else:
        if a == 0 and b == 1 and c == 1:
            if  ss[i+1] == 'AC':
                ans.append('C')
                c += 1
                b -= 1
                continue
            if ss[i+1] == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
        if c == 0 and b == 0:
            ok = False
            break
        
        if c > b:
            b +=1
            c -= 1
            ans.append('B')
        else:
            b -= 1
            c += 1
            ans.append('C')
if ok:
    print('Yes')
    for x in ans:
        print(x)
else:print('No')

Submission status: Runtime Error

Solution code:
N,A,B,C=map(int,input().split(' '))
S = [input() for i in range(N)]
ans = []
if A+B+C==0:
    print('No')
else:
    d = {"A":A,"B":B,"C":C}
    for n,i in enumerate(S):
        l,r=list(i)
        L,R=d[l],d[r]
        if L==R==0:
            print('No')
            exit()
        elif L==0:
            ans.append(l)
            d[l] +=1
            d[r] -=1
        elif R==0:
            d[l] -=1
            d[r] +=1
            ans.append(r)
        elif L==R==1 and n != N-1 and i != S[n+1]:
            if l in S[n+1]:
                d[l] +=1
                d[r] -=1
                ans.append(l)
            else:
                d[l] -=1
                d[r] +=1
                ans.apppend(r)
        else:
            if L>=R:
                d[l] -=1
                d[r] +=1
                ans.append(r)
            else:
                d[l] +=1
                d[r] -=1
                ans.append(l)
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 i in range(N)]
ans = []
if A+B+C==0:
    print('No')
else:
    d = {"A":A,"B":B,"C":C}
    for n,i in enumerate(S):
        l,r=list(i)
        L,R=d[l],d[r]
        if L==R==0:
            print('No')
            print(ans)
            exit()
        elif L==0:
            ans.append(l)
            d[l] +=1
            d[r] -=1
        elif R==0:
            d[l] -=1
            d[r] +=1
            ans.append(r)
        elif L==R==1 and n != N-1 and i != S[n+1]:
            if l in S[n+1]:
                d[l] +=1
                d[r] -=1
                ans.append(l)
            else:
                d[l] -=1
                d[r] +=1
                ans.apppend(r)
        else:
            if L>=R:
                d[l] -=1
                d[r] +=1
                ans.append(r)
            else:
                d[l] +=1
                d[r] -=1
                ans.append(l)
print('Yes')
for i in ans:
    print(i)

Submission status: Runtime Error

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

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for i in range(n):
    s = sl[i]
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if n == i - 1:
                ansl.append("A")
                a += 1
                b -= 1
            elif sl[i+1] == "AC":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if n == i - 1:
                ansl.append("B")
                b += 1
                c -= 1
            elif  sl[i+1] == "AB":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if n == i - 1:
                ansl.append("C")
                c += 1
                a -= 1
            elif sl[i+1] == "BC":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
ss = [input()for i in range(n)]
ok = True
ans = []
for i in range(n):
    s = ss[i]
    if s == 'AB':
        if a == 1 and b == 1 and c == 0:
            if  ss[i+1] == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            elif ss[i+1] == 'BC':
                ans.append('B')
                b += 1
                a -= 1
                continue
            
            
        if a == 0 and b == 0:
            ok = False
            break
        
        if a > b:
            b +=1
            a -= 1
            ans.append('B')
        else:
            b -= 1
            a += 1
            ans.append('A')
            
    elif s == 'AC':
        if a == 1 and b == 0 and c == 1:
            if  ss[i+1] == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            elif ss[i+1] == 'BC':
                ans.append('C')
                c += 1
                a -= 1
                continue
            
        if a == 0 and c == 0:
            ok = False
            break
        
        if a > c:
            c +=1
            a -= 1
            ans.append('C')
        else:
            c -= 1
            a += 1
            ans.append('A')
            
        
    else:
        if a == 0 and b == 1 and c == 1:
            if  ss[i+1] == 'AC':
                ans.append('C')
                c += 1
                b -= 1
                continue
            elif ss[i+1] == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
        if c == 0 and b == 0:
            ok = False
            break
        
        if c > b:
            b +=1
            c -= 1
            ans.append('B')
        else:
            b -= 1
            c += 1
            ans.append('C')
if ok:
    print('Yes')
    for x in ans:
        print(x)
else:print('No')

Submission status: Runtime Error

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

v = [A, B, C]
id_d = {'A':0, 'B':1, 'C':2}
ans = [0] * N
for i, s in enumerate(S):
  s1, s2 = s
  id1, id2 = id_d[s1], id_d[s2]
  v1, v2 = v[id1], v[id2]
  if v1 == v2 == 0:
    if i == 0:
      print('No')
      quit()
    else:
      pre_s1, pre_s2 = S[i-1]
      pre_id1, pre_id2 = id_d[pre_s1], id_d[pre_s2]
      if pre_s1 in [s1, s2]:
        v[pre_id1] += 2
        v[pre_id2] -= 2
        ans[i-1] = pre_s1
        if v[pre_id2] < 0:
          print('No')
          quit()
      else: # pre_s2 in [s1, s2]
        v[pre_id2] += 2
        v[pre_id1] -= 2
        ans[i-1] = pre_s2
        if v[pre_id1] < 0:
          print('No')
          quit()
  v1, v2 = v[id1], v[1d2]
  if v1 >= v2:
    ans[i] = s2
    v[id1] -= 1
    v[id2] += 1
  else:
    ans[i] = s1
    v[id1] += 1
    v[id2] -= 1

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

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
ss = [input()for i in range(n)]
ok = True
ans = []
for i in range(n):
    s = ss[i]
    if s == 'AB':
        if a == 1 and b == 1 and c == 0:
            if  ss[i+1] == 'AC':
                ans.append('A')
                a += 1
                b -= 1
                continue
            elif ss[i+1] == 'BC':
                ans.append('B')
                b += 1
                a -= 1
                continue
            
            
        if a == 0 and b == 0:
            ok = False
            break
        
        if a > b:
            b +=1
            a -= 1
            ans.append('B')
        else:
            b -= 1
            a += 1
            ans.append('A')
            
    elif s == 'AC':
         if a == 1 and b == 0 and c == 1:
            if  ss[i+1] == 'AB':
                ans.append('A')
                a += 1
                c -= 1
                continue
            elif ss[i+1] == 'BC':
                ans.append('C')
                c += 1
                a -= 1
                continue
            
        if a == 0 and c == 0:
            ok = False
            break
        
        if a > c:
            c +=1
            a -= 1
            ans.append('C')
        else:
            c -= 1
            a += 1
            ans.append('A')
            
        
    else:
        if a == 0 and b == 1 and c == 1:
            if  ss[i+1] == 'AC':
                ans.append('C')
                c += 1
                b -= 1
                continue
            elif ss[i+1] == 'AB':
                ans.append('B')
                b += 1
                c -= 1
                continue
        if c == 0 and b == 0:
            ok = False
            break
        
        if c > b:
            b +=1
            c -= 1
            ans.append('B')
        else:
            b -= 1
            c += 1
            ans.append('C')
if ok:
    print('Yes')
    for x in ans:
        print(x)
else:print('No')

Submission status: Runtime Error

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

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for i in range(n):
    s = sl[i]
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if i - 1 != n and sl[i+1] == "AC":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if i - 1 != n and sl[i+1] == "AB":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if i - 1 != n and sl[i+1] == "BC":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)

Submission status: Runtime Error

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

sl = []
for _ in range(n):
    s = input()
    sl.append(s)

yusen = []
before = sl[0]
co = 0
for s in sl:
    if before == s:
        co += 1
    else:
        target = list(set(list(s)) & set(list(before)))[0]
        for i in range(co):
            yusen.append(target)
        before = s
        co = 1
else:
    for i in range(co):
        yusen.append("C")




ansl = []
for i in range(n):
    s = sl[i]
    if s == "AB":
        if a > b:
            ansl.append("B")
            a -= 1
            b += 1
        elif a < b:
            ansl.append("A")
            a += 1
            b -= 1
        else:
            if sl[i+1] == "AC":
                ansl.append("A")
                a += 1
                b -= 1
            else:
                ansl.append("B")
                a -= 1
                b += 1
    if s == "BC":
        if b > c:
            ansl.append("C")
            b -= 1
            c += 1
        elif b < c:
            ansl.append("B")
            b += 1
            c -= 1
        else:
            if sl[i+1] == "AB":
                ansl.append("B")
                b += 1
                c -= 1
            else:
                ansl.append("C")
                b -= 1
                c += 1
    if s == "AC":
        if c > a:
            ansl.append("A")
            c -= 1
            a += 1
        elif c < a:
            ansl.append("C")
            c += 1
            a -= 1
        else:
            if sl[i+1] == "BC":
                ansl.append("C")
                c += 1
                a -= 1
            else:
                ansl.append("A")
                c -= 1
                a += 1
    if a < 0 or b < 0 or c < 0:
        print("No")
        exit()

print("Yes")
for a in ansl:
    print(a)

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}
    before=[-1,-1,-1]
    ans=[-1]*N
    for i in range(N):
        s=input()
        if n[d[s]]==0 and n[(d[s]+1)%3]==0:
            if before[d[s]]!=-1:
                n[d[s]]+=1
                n[(d[s]-1)%3]-=1
                before[d[s]]=-1
                ans[before[d[s]]]=(d[s]-1)%3
            print('No')
            exit()
        if 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:
            ans[i]=d[s]
            before[(d[s]+1)%3]=i
    print('Yes')
    d2=['A','B','C']
    for a,b in ans:
        print(d2[a])
                
                    

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:

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

def swap(ABC, f, t):
    ABC[f] -= 1
    ABC[t] += 1
    return itos[t]


def main():
    N, A, B, C = map(int, input().split())
    S = [input() for i in range(N)]
    ABC = [A, B, C]
    StoP = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
    itos = ["A", "B", "C"]
    L = []
    for i in range(N):
        x, y = StoP[S[i]]

        if ABC[x] == 0 and ABC[y] == 0:
            print("No")
            return
        if ABC[x] == 1 and ABC[y] == 1 and i < N - 1 and S[i] != S[i+1]:
            x2, y2 = StoP(S[i+1])
            if x in [x2, y2]:
                L.append(swap(ABC, y, x))
            else:
                L.append(swap(ABC, x, y))
        elif ABC[x] < ABC[y]:
            L.append(swap(ABC, y, x))
        else:
            L.append(swap(ABC, x, y))

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

Submission status: Runtime Error

Solution code:
def f(A, i, j, k, nex,res):
    if A[i] == A[j] == 0:
        print("No")
        exit()
    elif A[i] == A[j] == 1 and A[k] == 0:
        if i in nex:
            A[i] += 1
            A[j] -= 1
            return "ABC"[i]
        else:
            A[i] -= 1
            A[j] += 1
            return "ABC"[j]
    else:
        A[i] += 1
        A[j] -= 1
        return "ABC"[i]
    

def main():
    N, A, B, C = [int(_n) for _n in input().split()]
    S = []
    for i in range(N):
        s = input()
        S.append(s)
    if [A,B,C].count(0) == 3:
        print("No")
        return
    res = ""
    arr = [A,B,C]
    S.append("")
    for i,s in enumerate(S[:-1]):
        if S[i+1] == "AB":
            n = [0,1]
        if S[i+1] == "BC":
            n = [2,1]
        if S[i+1] == "AC":
            n = [0,2]
        if s == "AB":
            if arr[0] > arr[1]:
                res += f(arr, 1, 0, 2, n,res)
            else:
                res += f(arr,0,1,2,n,res)
        elif s == "BC":
            if arr[1] > arr[2]:
                res += f(arr, 2, 1, 0, n,res)
            else:
                res += f(arr,1,2,0,n,res)
        elif s == "AC":
            if arr[0] > arr[2]:
                res += f(arr, 2,0, 1, n,res)
            else:
                res += f(arr,0,2,1,n,res)
    print("Yes")
    for s in res:
        print(s)
main()

Submission status: Runtime Error

Solution code:

n, a, b, c = map(int, input().split())
tempa =0
tempc =0
li_a = []
li_ab = []
li_bc = []
li_c = []
for i in range(n):
    s = input()
    if s == 'AB':
        if b >= 1:
            a+=1
            tempa += 1
            b-=1
            li_a.append(i)
        else:
            if a >= 1:
                a-=1
                b+=1
                li_ab.append(i)
            elif tempc >= 1:
                if c >= 2:
                    tempc -=1
                    c -=2
                    b += 2
                    a+=1
                    b-=1
                    tempa+=1
                    li_a.append(i)
                    k = li_c.pop()
                    li_bc.append(k)
                else:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()
    else:
        if b >= 1:
            c+=1
            tempc += 1
            b-=1
            li_c.append(i)
        else:
            if c >= 1:
                c-=1
                b+=1
                li_bc.append(i)
            elif tempa>=1:
                if a >= 2:
                    tempa -= 1
                    a-=2
                    b+=2
                    c+=1
                    b-=1
                    tempc+=1
                    li_c.append(i)
                    k = li_a.pop()
                    li_ab.append(k)
                else:
                    print('No')
                    sys.exit()
            else:
                print('No')
                sys.exit()
for i in range(n):
    if i in li_a:
        print('A')
    elif i in li_ab:
        print('B')
    elif i in li_bc:
        print('B')
    elif i in li_c:
        print('C')
    else:
        print('No')

Submission status: Runtime Error

Solution code:
import sys
d = 'ABC'
N, *v = map(int, input().split())
e = [tuple(map(d.index, input())) for _ in range(N)]
p = []
def f(i):
	if i == len(e):
		print('Yes')
		print('\n'.join(p))
		raise SystemExit
	k, j = e[i]
	for a, s in ((k, j), (j, k)):
		if v[s] == 0: continue
		v[a] += 1
		v[s] -= 1
		p.append(d[a])
		f(i+1)
		v[a] -= 1
		v[s] += 1
		p.pop()
f(0)
print('No')

Submission status: Runtime Error

Solution code:
def f(A, i, j, k, nex,res):
    if A[i] == A[j] == 0:
        print("No")
        exit()
    elif A[i] == A[j] == 1 and A[k] == 0:
        if i in nex:
            A[i] += 1
            A[j] -= 1
            return "ABC"[i]
        else:
            A[i] -= 1
            A[j] += 1
            return "ABC"[j]
    else:
        A[i] += 1
        A[j] -= 1
        return "ABC"[i]
    

def main():
    N, A, B, C = [int(_n) for _n in input().split()]
    S = []
    for i in range(N):
        s = input()
        S.append(s)
    if [A,B,C].count(0) == 3:
        print("No")
        return
    res = ""
    arr = [A,B,C]
    S.append("")
    for i,s in enumerate(S[:-1]):
        if S[i+1] == "AB":
            n = [0,1]
        if S[i+1] == "BC":
            n = [2,1]
        if S[i+1] == "AC":
            n = [0,2]
        if s == "AB":
            if arr[0] > arr[1]:
                res += f(arr, 1, 0, 2, n,res)
            else:
                res += f(arr,0,1,2,n,res)
        elif s == "BC":
            if arr[1] > arr[2]:
                res += f(arr, 2, 1, 0, n,res)
            else:
                res += f(arr,1,2,0,n,res)
        elif s == "AC":
            if arr[0] > arr[2]:
                res += f(arr, 2,0, 1, n,res)
            else:
                res += f(arr,0,2,1,n,res)
    for s in res:
        print(s)
main()

Submission status: Runtime Error

Solution code:
def f(A, i, j, k, next,res):
    if A[i] == A[j] == 0:
        print("No")
        exit()
    elif A[i] == A[j] == 1 and A[k] == 0:
        if i in next:
            A[i] += 1
            A[j] -= 1
            return "ABC"[i]
        else:
            A[i] -= 1
            A[j] += 1
            return "ABC"[j]
    else:
        A[i] += 1
        A[j] -= 1
        return "ABC"[i]
    

def main():
    N, A, B, C = [int(_n) for _n in input().split()]
    S = []
    for i in range(N):
        s = input()
        S.append(s)
    if [A,B,C].count(0) == 3:
        print("No")
        return
    res = ""
    arr = [A,B,C]
    S.append("")
    for i,s in enumerate(S[:-1]):
        if S[i+1] == "AB":
            n = [0,1]
        if S[i+1] == "BC":
            n = [2,1]
        if S[i+1] == "AC":
            n = [0,2]
        if s == "AB":
            if arr[0] > arr[1]:
                res += f(arr, 1, 0, 2, n,res)
            else:
                res += f(arr,0,1,2,n,res)
        elif s == "BC":
            if arr[1] > arr[2]:
                res += f(arr, 2, 1, 0, n,res)
            else:
                res += f(arr,1,2,0,n,res)
        elif s == "AC":
            if arr[0] > arr[2]:
                res += f(arr, 0, 2, 1, n,res)
            else:
                res += f(arr,2,0,1,n,res)
    for s in res:
        print(s)
main()

Submission status: Runtime Error

Solution code:
def f(A, i, j, k, next,res):
    if A[i] == A[j] == 0:
        print("No")
        exit()
    elif A[i] == A[j] == 1 and A[k] == 0:
        if i in next:
            A[i] += 1
            A[j] -= 1
            return "ABC"[i]
        else:
            A[i] -= 1
            A[j] += 1
            return "ABC"[j]
    else:
        A[i] += 1
        A[j] -= 1
        return "ABC"[i]
    

def main():
    N, A, B, C = [int(_n) for _n in input().split()]
    S = []
    for i in range(N):
        s = input()
        S.append(s)
    if [A,B,C].count(0) == 3:
        print("No")
        return
    res = ""
    arr = [A,B,C]
    S.append("")
    for i,s in enumerate(S):
        if s == "AB":
            if arr[0] > arr[1]:
                res += f(arr, 1, 0, 2, S[i+1],res)
            else:
                res += f(arr,0,1,2,S[i+1],res)
        elif s == "BC":
            if arr[1] > arr[2]:
                res += f(arr, 2, 1, 0, S[i+1],res)
            else:
                res += f(arr,1,2,0,S[i+1],res)
        elif s == "AC":
            if arr[0] > arr[2]:
                res += f(arr, 0, 2, 1, S[i+1],res)
            else:
                res += f(arr,2,0,1,S[i+1],res)
    for s in res:
        print(s)
main()

Submission status: Runtime Error

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

n, a, b, c = map(int, input().split())
count = {
    'A': a,
    'B': b,
    'C': c
}
queries = []
for _ in range(n):
    query = tuple(input())
    queries.append(query)


def solve(plus, minus):
    count[plus] += 1
    count[minus] += 1
    ans.append(plus)


ans = []
for idx, (x, y) in enumerate(queries):
    if count[x] == count[y] == 0:
        print('No')
        exit()

    if count[x] == count[y] == 1:
        if idx == n-1:
            solve(x, y)
            break

        nex = queries[idx+1]
        if x in nex:
            solve(x, y)
        else:
            solve(y, x)
    
    elif count[x] >= count[y]:
        solve(x, y)
        
    else:
        solve(y, x)

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

Submission status: Runtime Error

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()
    S=[]
    SS=A+B+C
    for i in range(N):
        s=input()
        S.append(s)
    
    S.append("AB")
    S.append("AB")#適当に入れとけ
    
    ans=[]
    flag=1
    if SS<1:
        flag=-1
    elif SS>=3 or SS==1:
        for i in range(N):
            s=S[i]
            if s=="AB":
                if A<B:
                    A+=1
                    B-=1
                    ans.append("A")
                elif B>A:
                    A-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="AC":
                        A+=1
                        B-=1
                        ans.append("A")
                    else:
                        A-=1
                        B+=1
                        ans.append("B")
                        
                    
            elif s=="AC":
                if A<C:
                    A+=1
                    C-=1
                    ans.append("A")
                elif A>C:
                    A-=1
                    C+=1
                    ans.append("C")
                else:#同数
                    s2=S[i+1]
                    if s2=="AB":
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
                    
            else:#BC
                if C<B:
                    C+=1
                    B-=1
                    ans.append("C")
                elif C>B:
                    C-=1
                    B+=1
                    ans.append("B")
                else:#同数
                    s2=S[i+1]
                    if s2=="AC":
                        C+=1
                        B-=1
                        ans.append("C")
                    else:
                        C-=1
                        B+=1
                        ans.append("B")
                
   
        
                
            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()