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:
#!/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]"):
    if A ==0 and B ==0 and C ==0:
        print(NO)
        return 

    def yes():
        tmp = [A,B,C]
        print(YES)
        for i in range(N):
            a = tmp[0]
            b = tmp[1]
            c = tmp[2]
            if s[i] == "AB":
                if a<=b:
                    a += 1
                    b -= 1
                    print("A")
                else:
                    a -=1
                    b += 1
                    print("B")
            elif s[i] == "AC":
                if a<=c:
                    a += 1
                    c -= 1
                    print('A')
                else:
                    a -=1
                    c += 1
                    print('C')
            else:
                if b<=c:
                    b += 1
                    c -= 1
                    print('B')

                else:
                    b -=1
                    c += 1
                    print('C')
            tmp = [a,b,c]
        return 

    if A+B+C>2 and [A,B,C].count(0) <= 1:
        yes()
    else:
        if A+B+C>2:
            if C>0:
                if s[0] == "AB":
                    print(NO)
                else:
                    yes()
            elif A>0:
                if s[0] == "BC":
                    print(NO)
                else:
                    yes()
            else: 
                if s[0] == "AC":
                    print(NO)
                else:
                    yes()
        else:
            if [A,B,C].count(0) == 2:
                tmp = [A,B,C]
                for i in range(N):
                    a = tmp[0]
                    b = tmp[1]
                    c = tmp[2]
                    if s[i] == "AB":
                        if a<=b:
                            a += 1
                            b -= 1
                        else:
                            a -=1
                            b += 1
                    elif s[i] == "AC":
                        if a<=c:
                            a += 1
                            c -= 1
                        else:
                            a -=1
                            c += 1
                    else:
                        if b<=c:
                            b += 1
                            c -= 1
                        else:
                            b -=1
                            c += 1
                    if a < 0 or b<0 or c<0:
                        print(NO)
                        return
                    tmp = [a,b,c]
                yes()
            elif [A,B,C].count(0) == 1:
                raise Exception
                tmp = [A,B,C]
                print(YES)

                for i in range(N):
                    a = tmp[0]
                    b = tmp[1]
                    c = tmp[2]
                    if s[i] == "AB" and a==1 and b == 1:
                        if i+1<N:
                            if s[i+1] == "BC":
                                b += 1
                                a -= 1
                                print("B")
                            else:
                                b -= 1
                                a += 1
                                print("A")
                            tmp = [a,b,c]
                            continue

                    if s[i] == "BC" and b==1 and c == 1:
                        if i+1<N:
                            if s[i+1] == "AC":
                                b -= 1
                                c += 1
                                print("C")
                            else:
                                b += 1
                                c -= 1
                                print("B")
                            tmp = [a,b,c]
                            continue
                    if s[i] == "AC" and a==1 and c == 1:
                        if i+1<N:
                            if s[i+1] == "BC":
                                c += 1
                                a -= 1
                                print("C")
                            else:
                                c -= 1
                                a += 1
                                print("A")     
                            tmp = [a,b,c]
                            continue               

                    if s[i] == "AB":
                        if a<=b:
                            a += 1
                            b -= 1
                            print("A")
                        else:
                            a -=1
                            b += 1
                            print("B")
                    elif s[i] == "AC":
                        if a<=c:
                            a += 1
                            c -= 1
                            print('A')
                        else:
                            a -=1
                            c += 1
                            print('C')
                    else:
                        if b<=c:
                            b += 1
                            c -= 1
                            print('B')

                        else:
                            b -=1
                            c += 1
                            print('C')
                    tmp = [a,b,c]
        return 
    return


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: Runtime Error

Solution code:
import numpy as np
N, *number = map(int, input().split())
s_list = [input() for i in range(N)]


def solve(i, n_list, s):
    if i == N:
        print("Yes")
        for a in s:
            print(a)
        exit()
    if s_list[i] == "AB":
        if n_list[0] - 1 > 0:
            solve(i + 1, n_list + np.array([-1, 1, 0]), s + ["B"])
        if n_list[1] - 1 > 0:
            solve(i + 1, n_list + np.array([1, -1, 0]), s + ["A"])
    elif s_list[i] == "AC":
        if n_list[0] - 1 > 0:
            solve(i + 1, n_list + np.array([-1, 0, 1]), s + ["C"])
        if n_list[2] - 1 > 0:
            solve(i + 1, n_list + np.array([1, 0, -1]), s + ["A"])
    else:
        if n_list[1] - 1 > 0:
            solve(i + 1, n_list + np.array([0, -1, 1]), s + ["C"])
        if n_list[2] - 1 > 0:
            solve(i + 1, n_list + np.array([0, 1, -1]), s + ["B"])


solve(0, np.array(number), [])
print("No")

Submission status: Runtime Error

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


n, a, b, c = LI()
ans = []
D = {'A':a, 'B':b, 'C':c}
sx = SR(n)
for i in range(n):
    s = sx[i]
    s1 = s[0]
    s2 = s[1]
    if D[s1] == D[s2] == 0:
        print('No')
        exit()
    elif D[s[0]] == D[s[1]] == 1 and i + 1 < n:
        if s1 in sx[i + 1]:
            a = s1
            b = s2
        else:
            D[s2] += 1
            D[s1] -= 1
            ans += [s2]
    else:
        if D[s1] > D[s2]:
            a = s2
            b = s1
        else:
            a = s1
            b = s2
    D[a] += 1
    D[b] -= 1
    ans += [s1]
print('Yes')
print(*ans, sep='\n')

Submission status: Runtime Error

Solution code:
import sys
def main():
    input = sys.stdin.readline
    N,A,B,C=map(int, input().split())
    S=[input().rstrip() for _ in range(N)]

    MX=3
    dp=[[[[None]*MX+1 for _ in range(MX+1)] for __ in range(MX+1)] for ___ in range(N+4)]
    dp[0][min(MX,A)][min(MX,B)][min(MX,C)]=1
    for i,s in enumerate(S):
        ni=i+1
        for a in range(MX+1):
            for b in range(MX+1):
                for c in range(MX+1):
                    for j in range(2):
                        na,nb,nc=a,b,c
                        sel=None
                        if s=='AB':
                            if j==0:
                                if b==0: continue
                                na,nb=min(MX,na+1),nb-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nb=na-1,min(MX,nb+1)
                                sel='B'
                        elif s=='AC':
                            if j==0:
                                if c==0: continue
                                na,nc=min(MX,na+1),nc-1
                                sel='A'
                            else:
                                if a==0: continue
                                na,nc=na-1,min(MX,nc+1)
                                sel='C'
                        elif s=='BC':
                            if j==0:
                                if c==0: continue
                                nb,nc=min(MX,nb+1),nc-1
                                sel='B'
                            else:
                                if b==0: continue
                                nb,nc=nb-1,min(MX,nc+1)
                                sel='C'
                        if dp[i][a][b][c]: dp[ni][na][nb][nc]=(a,b,c,sel)

    flag=False
    ga,gb,gc=0,0,0
    for a in range(MX+1):
        for b in range(MX+1):
            for c in range(MX+1):
                if dp[N][a][b][c]:
                    flag=True
                    ga,gb,gc=a,b,c
    if flag==False:
        print('No')
        return
    print('Yes')
    ans=[]
    for i in range(N,0,-1):
        ga,gb,gc,sel=dp[i][ga][gb][gc]
        ans.append(sel)
    for a in reversed(ans):
        print(a)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys

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

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

ans = []
for i in range(n):

	if d[s[i][0]] <= 0 and d[s[i][1]] <= 0:
		print('No')
		sys.exit()
	if d[s[i][0]] < d[s[i][1]]:
		d[s[i][1]] -= 1
		d[s[i][0]] += 1
		ans.append(s[i][0])
	elif d[s[i][0]] > d[s[i][1]]:
		d[s[i][0]] -= 1
		d[s[i][1]] += 1
		ans.append(s[i][1])
	else:
		if i < n-1 and d[s[i+1].strip(s[i])] == 0:
			d[s[i+1].strip(s[i+1].strip(s[i]))] += 1
			ans.append(s[i+1].strip(s[i+1].strip(s[i])))
		else:
			d[s[i][0]] += 1
			ans.append(s[i][0])
print('Yes')
for i in range(len(ans)):
	print(ans[i])

Submission status: Runtime Error

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

DP = [[0] * 3 for _ in range(N + 1)]
DP[0] = [A, B, C]

L = [""] * N
R = {}
R[0] = "A"
R[1] = "B"
R[2] = "C"
A = []
B = []
for i in range(N):
  t = s[i]
  if t == "AB":
    x = 0
    y = 1
  if t == "BC":
    x = 1
    y = 2
  if t == "AC":
    x = 0
    y = 2
  if DP[i][y] != 0:
    DP[i + 1] = DP[i]
    DP[i + 1][x] += 1
    DP[i + 1][y] -= 1
    L[i] = R[x]
    if x == 0:
      A.append([i, y])
    else:
      B.append([i, y])
  elif DP[i][x] != 0:
    DP[i + 1] = DP[i]
    DP[i + 1][x] -= 1
    DP[i + 1][y] += 1
    L[i] = R[y]
  else:
    if x == 0:
      if len(B) == 0:
        print("No")
        exit()
      z = 1
      k, n = B.pop()
    if x == 1:
      if len(A) == 0:
        print("No")
        exit()
      z = 0
      k, n = A.pop()
    DP[i + 1] = DP[i]
    DP[i + 1][z] -= 2
    DP[i + 1][n] += 2
    if n == x:
      x, y = y, x
    DP[i + 1][x] += 1
    DP[i + 1][y] -= 1
    if DP[i + 1][z] < 0:
      print("No")
      exit()
    L[i] = R[x]
    L[t] = R[y]

print("Yes")
for i in L:
  print(i)

Submission status: Runtime Error

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    import math
    #from math import gcd

    #inf = 10**17
    #mod = 10**9 + 7

    n,a,b,c = map(int, input().split())
    l = []
    for i in range(n):
        s = input().rstrip()
        l.append(s)
    if a==0 and b==0 and c==0:
        print('No')
        exit()
    if a==0 and b==0:
        if l[0] == 'AB':
            print('No')
            exit()
    if c==0 and b==0:
        if l[0] == 'BC':
            print('No')
            exit()
    if a==0 and c==0:
        if l[0] == 'AC':
            print('No')
            exit()
    print('Yes')
    res = []
    boll = 0
    for i in range(n):
        if i == 0:
            if s[i] == 'AB':
                if a==0:
                    b -= 1
                    a += 1
                    boll = 0
                    res.append('A')
                else:
                    a -= 1
                    b += 1
                    boll = 1
                    res.append('B')
            if s[i] == 'BC':
                if c==0:
                    b -= 1
                    c += 1
                    boll = 2
                    res.append('C')
                else:
                    c -= 1
                    b += 1
                    boll = 1
                    res.append('B')
            if s[i] == 'AC':
                if a==0:
                    c -= 1
                    a += 1
                    boll = 0
                    res.append('A')
                else:
                    a -= 1
                    c += 1
                    boll = 2
                    res.append('C')
        else:
            if s[i] == 'AB':
                if boll==1:
                    b -= 1
                    a += 1
                    boll = 0
                    res.append('A')
                else:
                    a -= 1
                    b += 1
                    boll = 1
                    res.append('B')
            if s[i] == 'BC':
                if boll==1:
                    b -= 1
                    c += 1
                    boll = 2
                    res.append('C')
                else:
                    c -= 1
                    b += 1
                    boll = 1
                    res.append('B')
            if s[i] == 'AC':
                if boll==2:
                    c -= 1
                    a += 1
                    boll = 0
                    res.append('A')
                else:
                    a -= 1
                    c += 1
                    boll = 2
                    res.append('C')
    for i in res:
        print(i)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import Counter

def resolve():
    n,a,b,c = map(int, input().split())
    abc = [a,b,c]
    s = [input() for i in range(n)]
    d = {'AB': [0,1,2], 'AC': [0,2,1], 'BC': [2,1,0]}
    dd = {'AB': ['AC', 'BC'], 'AC': ['AB', 'BC'], 'BC': ['AC', 'AB']}
    ans = []
    q = ['A', 'B', 'C']
    select = [0,0,0]

    for c in s:
        x = [abc[i] for i in d[c]]
        if x[0] == 0 and x[1] == 0:
            print('No')
            return
        if select[0] > select[1]:
            non = d[c][0]
            idx = d[c][1]
        elif select[0] < select[1]:
            non = d[c][1]
            idx = d[c][0]
        elif x[0] > x[1]:
            non = d[c][0]
            idx = d[c][1]
        elif x[0] > x[1]:
            non = d[c][1]
            idx = d[c][0]

        ans.append(q[idx])
        select[idx] += 1
        select[non] -= 1
        abc[idx] += 1
        abc[non] -= 1

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

if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
from collections import Counter

def resolve():
    n,a,b,c = map(int, input().split())
    abc = [a,b,c]
    s = [input() for i in range(n)]
    d = {'AB': [0,1,2], 'AC': [0,2,1], 'BC': [2,1,0]}
    dd = {'AB': ['AC', 'BC'], 'AC': ['AB', 'BC'], 'BC': ['AC', 'AB']}
    ans = []
    q = ['A', 'B', 'C']
    select = [0,0,0]

    for c in s:
        x = [abc[i] for i in d[c]]
        if x[0] == 0 and x[1] == 0:
            print('No')
            return
        if select[0] > select[1]:
            non = d[c][0]
            idx = d[c][1]
        elif select[0] > select[1]:
            non = d[c][1]
            idx = d[c][0]
        if x[0] > x[1]:
            non = d[c][0]
            idx = d[c][1]
        elif x[0] > x[1]:
            non = d[c][1]
            idx = d[c][0]

        ans.append(q[idx])
        select[idx] += 1
        select[non] -= 1
        abc[idx] += 1
        abc[non] -= 1

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

if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

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

DP = [[0] * 3 for _ in range(N + 1)]
DP[0] = [A, B, C]

L = [""] * N
R = {}
R[0] = "A"
R[1] = "B"
R[2] = "C"
A = []
B = []
  for i in range(N):
    t = s[i]
    if t == "AB":
      x = 0
      y = 1
    if t == "BC":
      x = 1
      y = 2
    if t == "AC":
      x = 0
      y = 2
    if DP[i][y] != 0:
      DP[i + 1] = DP[i]
      DP[i + 1][x] += 1
      DP[i + 1][y] -= 1
      L[i] = R[x]
      if x == 0:
        A.append([i, y])
      else:
        B.append([i, y])
    elif DP[i][x] != 0:
      DP[i + 1] = DP[i]
      DP[i + 1][x] -= 1
      DP[i + 1][y] += 1
      L[i] = R[y]
    else:
      if x == 0:
        if len(B) == 0:
          print("No")
          exit()
        z = 1
        k, n = pop(B)
      if x == 1:
        if len(A) == 0:
          print("No")
          exit()
        z = 0
        k, n = pop(A)
      DP[i + 1] = DP[i]
      DP[i + 1][z] -= 2
      DP[i + 1][n] += 2
      if n == x:
        x, y = y, x
      DP[i + 1][x] += 1
      DP[i + 1][y] -= 1
      if DP[i + 1][z] < 0:
        print("No")
        exit()
      L[i] = R[x]
      L[t] = R[y]

  print("Yes")
  for i in L:
    print(i)

Submission status: Runtime Error

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

ab = 0
ac = 0
bc = 0

ans = []
ng = False
for i in range(n):
    n = input()
    if n == "AB":
        if a != 0:
            a -= 1
            b += 1
            ans.append("B")
            ab = i
        elif b != 0:
            a += 1
            b -= 1
            ab = i
            ans.append("A")
        elif ac > 0 and c > 0:
            a += 2
            c -= 2
            ans[ac] = "A"
        elif bc > 0 and c > 0:
            b += 2
            c -= 2
            ans[bc] = "B"
        else:
            ng = True
            break

    elif n == "AC":
        if b != 0:
            b -= 1
            c += 1
            ans.append("C")
            ac = i
        elif c != 0:
            b += 1
            c -= 1
            ac = i
            ans.append("B")
        elif ab > 0 and a > 0:
            b += 2
            a -= 2
            ans[ab] = "B"
        elif ac > 0 and a > 0:
            c += 2
            a -= 2
            ans[bc] = "C"
        else:
            ng = True
            break

    else:
        if a != 0:
            a -= 1
            b += 1
            ans.append("B")
            ab = i
        elif b != 0:
            a += 1
            b -= 1
            ab = i
            ans.append("A")
        elif ac > 0 and c > 0:
            a += 2
            c -= 2
            ans[ac] = "A"
        elif bc > 0 and c > 0:
            b += 2
            c -= 2
            ans[bc] = "B"
        else:
            ng = True
            break

if ng and (a > 0 or b > 0 or c > 0):
    print("Yes")
    for i in range(len(ans)):
        print(ans[i])
else:
    print("No")

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

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

Lis = [None, None]
i = 0
if a != 0:
    Lis[i] = "A"
    i +=1
if b != 0:
    Lis[i] = "B"

if i ==2:
    print("Yes")
    for j in range(n):
        print("AB")
else:
    if c != 0:
        Lis[i] ="C
        if Lis[i] == "A":
            print("Yes")
            for j in range(n):
                print("AC")
        elif Lis[i] == "B":
            print("Yes")
            for j in range(n):
                print("BC")
    else:
        print("NO")

Submission status: Runtime Error

Solution code:
N,A,B,C=map(int,input().split())
ans={"A":A,"B":B,"C":C}
ans1=[]
s=[]
flag=0
flag1=[0,0,0,"D","D"]
for _ in range(N):
    s=input()
    if  ans[s[0]]==ans[s[1]]:
        flag1[0]=1
        flag1[1]=s[0]
        flag1[2]=s[1]
    if ans[s[0]]>=ans[s[1]]:
        ans[s[0]]-=1
        ans[s[1]]+=1
        ans1.append(s[1])
        flag[3]=2
        flag[4]=1
    else:
        ans[s[0]]+=1
        ans[s[1]]-=1
        ans1.append(s[0])
        flag[3]=1
        flag[4]=2
    if ans[s[0]] <=-1 or ans[s[1]]<= -1:
        if flag1[0]==0:
            flag=1
        else:
            i=flag2[3]
            ans[i]-=1
            ans[lag2[4]]+=1
            if ans[s[0]] <=-1 or ans[s[1]]<= -1:
                flag=1
        
if flag==1:
    print("No")
    exit()
print("Yes")
for a in ans1:
    print(a)

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
n,a,b,c=map(int,input().split())
s=[input() for i in range(n)]
dp=[dict() for i in range(n+1)]
dp[0][""]=[a,b,c]
for i in range(n):
    if len(dp[i])==0:
        print("No")
        break
    if s[i]=="AB":
        for j in dp[i]:
            a,b,c=dp[i][j]
            if dp[i][j][0]>0:
                dp[i+1][j+"A"]=[a-1,b+1,c]
            if dp[i][j][1]>0:
                dp[i+1][j+"B"]=[a+1,b-1,c]
    elif s[i]=="AC":
        for j in dp[i]:
            a,b,c=dp[i][j]
            if dp[i][j][0]>0:
                dp[i+1][j+"A"]=[a-1,b,c+1]
            if dp[i][j][2]>0:
                dp[i+1][j+"C"]=[a+1,b,c-1]
    else:
        for j in dp[i]:
            a,b,c=dp[i][j]
            if dp[i][j][1]>0:
                dp[i+1][j+"B"]=[a,b-1,c+1]
            if dp[i][j][2]>0:
                dp[i+1][j+"C"]=[a,b+1,c-1]
else:
    print("Yes")
    z=list(dp[-1])
    #print(z)
    for i in range(n):
        print(z[-1][i])

Submission status: Runtime Error

Solution code:
import sys

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

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

ans = []
for i in range(n):

	if d[s[i][0]] <= 0 and d[s[i][1]] <= 0:
		print('No')
		sys.exit()
	if d[s[i][0]] < d[s[i][1]]:
		d[s[i][1]] -= 1
		d[s[i][0]] += 1
		ans.append(s[i][0])
	elif d[s[i][0]] > d[s[i][1]]:
		d[s[i][0]] -= 1
		d[s[i][1]] += 1
		ans.append(s[i][1])
	else:
		if i < n-1 and d[s[i+1].strip(s[i])] == 0:
			d[s[i+1].strip(s[i+1].strip(s[i]))] += 1
			ans.append(s[i+1].strip(s[i+1].strip(s[i])))
print('Yes')
for i in range(len(ans)):
	print(ans[i])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# 整数の入力
n = int(input())
h_list = input().split()
h_list = [int(h) for h in h_list]

count = 0

for i in range(n):
    a_height = h_list[i]
    if i + 21 < n:
        n_aa = i + 21
    else:
        n_aa = n
    for k in range(i + 1 + a_height, n_aa):
        b_height = h_list[k]
        if k - i == a_height + b_height:
            count += 1

print(count)