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())

S=['']*N
for i in range(N):
    S[i]=input()

result=[]
kekka=True


for i in range(N):
    if (A==0 and B==0 and S[i]=='AB') or (C==0 and B==0 and S[i]=='BC') or (A==0 and C==0 and S[i]=='AC'):
        kekka=False
        break
    else:
        if S[i]=='AB':
            if A==0:
                A += 1
                B -= 1
                result.append('A')
            elif B==0:
                A -=1
                B +=1
                result.append('B')
            elif A==1 and B==1 and C==0:
                if S[i+1]=='AC':
                    A += 1
                    B -= 1
                    result.append('A')
                else:
                    A -=1
                    B +=1
                    result.append('B')
            elif A>B:
                A -=1
                B +=1
                result.append('B')
            else:
                A +=1
                B -=1
                result.append('A')
        elif S[i]=='AC':
            if A==0:
                A += 1
                C -= 1
                result.append('A')
            elif C==0:
                A -=1
                C +=1
                result.append('C')
            elif A==1 and C==1 and B==0:
                if S[i+1]=='AB':
                    A += 1
                    C -= 1
                    result.append('A')
                else:
                    A -=1
                    C +=1
                    result.append('C')
            elif A>C:
                A -=1
                C +=1
                result.append('C')
            else:
                A +=1
                C -=1
                result.append('A') 
        elif S[i]=='BC':
            if B==0:
                B += 1
                C -= 1
                result.append('B')
            elif C==0:
                B -=1
                C +=1
                result.append('C')
            elif B==1 and C==1 and A==0:
                if S[i+1]=='AB':
                    B += 1
                    C -= 1
                    result.append('B')
                else:
                    B -=1
                    C +=1
                    result.append('C')
            elif B>C:
                B -=1
                C +=1
                result.append('C')
            else:
                B +=1
                C -=1
                result.append('B')

if kekka ==False:
    print('No')
else:
    print('Yes')
    for i in range(len(result)):
        print(result[i])

Submission status: Runtime Error

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

S=['']*N
for i in range(N):
    S[i]=input()

result=[]
kekka=True

if A+B+C==0:
    kekka=False

else:
    for i in range(N):
        if (A==0 and B==0 and S[i]=='AB') or (C==0 and B==0 and S[i]=='BC') or (A==0 and C==0 and S[i]=='AC'):
            kekka=False
            break
        else:
            if S[i]=='AB':
                if A==0:
                    A += 1
                    B -= 1
                    result.append('A')
                elif B==0:
                    A -=1
                    B +=1
                    result.append('B')
                elif A==1 and B==1:
                    if S[i+1]=='AC':
                        A += 1
                        B -= 1
                        result.append('A')
                    else:
                        A -=1
                        B +=1
                        result.append('B')
                elif A>B:
                    A -=1
                    B +=1
                    result.append('B')
                else:
                    A +=1
                    B -=1
                    result.append('A')
            elif S[i]=='AC':
                if A==0:
                    A += 1
                    C -= 1
                    result.append('A')
                elif C==0:
                    A -=1
                    C +=1
                    result.append('C')
                elif A==1 and C==1:
                    if S[i+1]=='AB':
                        A += 1
                        C -= 1
                        result.append('A')
                    else:
                        A -=1
                        C +=1
                        result.append('C')
                elif A>C:
                    A -=1
                    C +=1
                    result.append('C')
                else:
                    A +=1
                    C -=1
                    result.append('C') 
            elif S[i]=='BC':
                if B==0:
                    B += 1
                    C -= 1
                    result.append('B')
                elif C==0:
                    B -=1
                    C +=1
                    result.append('C')
                elif B==1 and C==1:
                    if S[i+1]=='AB':
                        B += 1
                        C -= 1
                        result.append('B')
                    else:
                        B -=1
                        C +=1
                        result.append('C')
                elif B>C:
                    B -=1
                    C +=1
                    result.append('C')
                else:
                    B +=1
                    C -=1
                    result.append('B')

if kekka ==False:
    print('No')
else:
    print('Yes')
    for i in range(len(result)):
        print(result[i])

Submission status: Runtime Error

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

def ci(s):
    if s == 'AB':
        return 0,1
    elif s == 'BC':
        return 1,2
    else:
        return 0,2

if A+B+C == 0:
    print('No')
    exit()
elif A+B+C == 1:
    for s in S:
        i,j = ci(s)
        if table[i] == 0 and table[j] == 0:
            print('No')
            exit()
        else:
            if table[i] == 0:
                table[i] = 1
                table[j] = 0
                ans.append(i)
            else:
                table[j] = 1
                table[i] = 0
                ans.append(j)

else:
    for k in range(N):
        i,j= ci(S[k])
        if table[i] > table[j]:
            i,j = j,i
        # print(table,i,j)
        if k + 1 < N:
            i2,j2 = ci(S[k+1])
        if table[i] == 0 and table[j] == 0:
            print('No')
            exit()
        elif table[i] == 0 and table[j] > 0:
            table[i] += 1
            table[j] -= 1
            ans.append(i)
        else:
            table[i] += 1
            table[j] -= 1
            if table[i2] == 0 and table[j2] == 0:
                table[i] -= 2
                table[j] += 2
                ans.append(j)
            else:
                ans.append(i)

print('Yes')
L = ['A','B','C']
for a in ans:
    print(L[a])

Submission status: Runtime Error

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

def ci(s):
    if s == 'AB':
        return 0,1
    elif s == 'BC':
        return 1,2
    else:
        return 0,2

if A+B+C == 0:
    print('No')
elif A+B+C == 1:
    for s in S:
        i,j = ci(s)
        if table[i] == 0 and table[j] == 0:
            print('No')
            exit()
        else:
            if table[i] == 0:
                table[i] = 1
                table[j] = 0
                ans.append(i)
            else:
                table[j] = 1
                table[i] = 0
                ans.append(j)

else:
    for k in range(N):
        i,j= ci(S[k])
        if table[i] > table[j]:
            i,j = j,i
        # print(table,i,j)
        if k + 1 < N:
            i2,j2 = ci(S[k+1])
        if table[i] == 0 and table[j] == 0:
            print('No')
            exit()
        elif table[i] == 0 and table[j] > 0:
            table[i] += 1
            table[j] -= 1
            ans.append(i)
        else:
            table[i] += 1
            table[j] -= 1
            if table[i2] == 0 and table[j2] == 0:
                table[i] -= 2
                table[j] += 2
                ans.append(j)
            else:
                ans.append(i)

print('Yes')
L = ['A','B','C']
for a in ans:
    print(L[a])

Submission status: Runtime Error

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

def ci(s):
    if s == 'AB':
        return 0,1
    elif s == 'BC':
        return 1,2
    else:
        return 0,2

if A+B+C == 0:
    print('No')
elif A+B+C == 1:
    for s in S:
        i,j = ci(s)
        print(table,i,j)
        if table[i] == 0 and table[j] == 0:
            print('No')
            exit()
        else:
            if table[i] == 0:
                table[i] = 1
                table[j] = 0
                ans.append(i)
            else:
                table[j] = 1
                table[i] = 0
                ans.append(j)

else:
    for k in range(N):
        i,j= ci(S[k])
        if table[i] > table[j]:
            i,j = j,i
        # print(table,i,j)
        if k + 1 < N:
            i2,j2 = ci(S[k+1])
        if table[i] == 0 and table[j] == 0:
            print('No')
            exit()
        elif table[i] == 0 and table[j] > 0:
            table[i] += 1
            table[j] -= 1
            ans.append(i)
        else:
            table[i] += 1
            table[j] -= 1
            if table[i2] == 0 and table[j2] == 0:
                table[i] -= 2
                table[j] += 2
                ans.append(j)
            else:
                ans.append(i)

print('Yes')
L = ['A','B','C']
for a in ans:
    print(L[a])

Submission status: Runtime Error

Solution code:
import math
import sys

sys.setrecursionlimit(10**6)

flag = False

def returnDepth(node):
    return math.floor(math.log2(node + 1))

def childNode(node):
    depth = returnDepth(node)
    column = node - 2**depth + 1
    leftChildNode = node + 2**depth + column
    return [leftChildNode, leftChildNode + 1]

def parentNode(node):
    depth = returnDepth(node)
    leftSide = 2**depth - 1
    return int(leftSide - 2**(depth - 1) + math.floor((node - leftSide) / 2))

def sgn(num):
    if num % 2 == 0:
        return 1
    else:
        return -1

def DFS(node):
    global flag
    if flag:
        return 0
    childs = childNode(node)
    depth = returnDepth(node)
    parent = parentNode(node)
    if node > 0:
        route[node] = route[parent]
        if route[parent] != '':
            route[node] += '\n'
        if choice[depth - 1] == 'AB':
            tree[node] = [tree[parent][0] + sgn(node), tree[parent][1] - sgn(node), tree[parent][2]]
            if node % 2 == 0:
                route[node] += 'A'
            else:
                route[node] += 'B'
        elif choice[depth - 1] == 'BC':
            tree[node] = [tree[parent][0], tree[parent][1] + sgn(node), tree[parent][2] - sgn(node)]
            if node % 2 == 0:
                route[node] += 'B'
            else:
                route[node] += 'C'
        elif choice[depth - 1] == 'AC':
            tree[node] = [tree[parent][0] + sgn(node), tree[parent][1], tree[parent][2] - sgn(node)]
            if node % 2 == 0:
                route[node] += 'A'
            else:
                route[node] += 'C'

    if tree[node][0] >= 0 and tree[node][1] >= 0 and tree[node][2] >= 0:
        if childs[0] < len(tree):
            DFS(childs[0])
            DFS(childs[1])
        else:
            print('Yes')
            print(route[node])
            flag = True
    else:
        tree[node] = []

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

tree = [[]] * (2**(n + 1) - 1)
route = [''] * (2**(n + 1) - 1)

tree[0] = [a,b,c]

DFS(0)

if not flag:
    print('No')

Submission status: Runtime Error

Solution code:
def end():
    print("No")
    exit()



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

for i in range(n):
    if s[i] == "AB":
        # コーナーケース
        if a == 1 and b == 1:
            # aが重複 最後はどっちでもいいので
            if i == n-1 or s[i+1] == "AB" or s[i+1] == 'AC':
                a += 1
                b -= 1
                log.append("A")
            # Bが重複
            else:# "BC"
                a -= 1
                b += 1
                log.append("B")
        elif a == 0 and b == 0:
            end()
        else:
            if a>=b:
                a -= 1
                b += 1
                log.append("B")
            else:
                a += 1
                b -= 1
                log.append("A")

    elif s[i] == "AC":
        # コーナーケース
        if a == 1 and c == 1:
            # aが重複
            if i == n-1 or s[i+1] == "AC" or s[i+1] == 'AB':
                a += 1
                c -= 1
                log.append("A")
            # Cが重複
            else:# "BC"
                c += 1
                a -= 1
                log.append("C")
        elif a == 0 and c == 0:
            end()
        else:
            if a>=c:
                a -= 1
                c += 1
                log.append("C")
            else:
                a += 1
                c -= 1
                log.append("A")
    else:#BC
        # コーナーケース
        if c == 1 and b == 1:
            # Bが重複
            if i == n-1 or  s[i+1] == "BC" or s[i+1] == 'AB':
                c -= 1
                b += 1
                log.append("B")
            # Cが重複
            else:# "AC"
                c += 1
                b -= 1
                log.append("C")
        elif a == 0 and c == 0:
            end()
        else:
            if b>=c:
                b -= 1
                c += 1
                log.append("C")
            else:
                b += 1
                c -= 1
                log.append("B")
    else:
        print("Yes")
        print(*log, sep='\n')

Submission status: Runtime Error

Solution code:
import math

flag = False

def returnDepth(node):
    return math.floor(math.log2(node + 1))

def childNode(node):
    depth = returnDepth(node)
    column = node - 2**depth + 1
    leftChildNode = node + 2**depth + column
    return [leftChildNode, leftChildNode + 1]

def parentNode(node):
    depth = returnDepth(node)
    leftSide = 2**depth - 1
    return int(leftSide - 2**(depth - 1) + math.floor((node - leftSide) / 2))

def sgn(num):
    if num % 2 == 0:
        return 1
    else:
        return -1

def DFS(node):
    global flag
    if flag:
        return 0
    childs = childNode(node)
    depth = returnDepth(node)
    parent = parentNode(node)

    if node > 0:
        route[node] = route[parent]
        if route[parent] != '':
            route[node] += '\n'
        if choice[depth - 1] == 'AB':
            tree[node] = [tree[parent][0] + sgn(node), tree[parent][1] - sgn(node), tree[parent][2]]
            if node % 2 == 0:
                route[node] += 'A'
            else:
                route[node] += 'B'
        elif choice[depth - 1] == 'BC':
            tree[node] = [tree[parent][0], tree[parent][1] + sgn(node), tree[parent][2] - sgn(node)]
            if node % 2 == 0:
                route[node] += 'B'
            else:
                route[node] += 'C'
        elif choice[depth - 1] == 'AC':
            tree[node] = [tree[parent][0] + sgn(node), tree[parent][1], tree[parent][2] - sgn(node)]
            if node % 2 == 0:
                route[node] += 'A'
            else:
                route[node] += 'C'

    if tree[node][0] >= 0 and tree[node][1] >= 0 and tree[node][2] >= 0:
        if childs[0] < len(tree):
            DFS(childs[0])
            DFS(childs[1])
        else:
            print(route[node])
            flag = True
    else:
        tree[node] = []

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

tree = [[]] * (2**(n + 1) - 1)
route = [''] * (2**(n + 1) - 1)

tree[0] = [a,b,c]

DFS(0)

if not flag:
    print('No')

Submission status: Runtime Error

Solution code:
from bisect import bisect_left, bisect_right

INF = float("inf")
n, *s = map(int, input().split())
toidx = {"AB": (0, 1), "BC": (1, 2), "AC": (0, 2)}
toalp = lambda x: chr(ord("A") + x)
m = sum(s)
q = [toidx[input()] for _ in range(n)]

def solve():
    res = []
    if m == 0 or (not s[q[0][0]] and not s[q[0][1]]):
        print("No")
        return
    elif m == 1:
        for i, j in q:
            if not s[i] and not s[j]:
                print("No")
                return
            elif s[i]:
                s[j] += 1; s[i] -= 1
                res.append(toalp(j))
            elif s[j]:
                s[i] += 1; s[j] -= 1
                res.append(toalp(i))
    elif m == 2:
        d = {(0, 1): [], (0, 2): [], (1, 2): []}
        for i in range(n): d[q[i]].append(i)
        for key in d.keys(): d[key].append(INF)
        for idx, (i, j) in enumerate(q):
            if s[i] and s[j]:
                k = [x for x in range(3) if i != x and j != x][0]
                nik = d[(i, k)][bisect_left(d[(i, k)], idx)]
                njk = d[(j, k)][bisect_left(d[(j, k)], idx)]
                if nik <= njk:
                    s[i] += 1; s[j] -= 1
                    res.append(toalp(i))
                else: 
                    s[j] += 1; s[i] -= 1
                    res.append(toalp(j))
            elif s[i]:
                s[j] += 1; s[i] -= 1
                res.append(toalp(j))
            elif s[j]:
                s[i] += 1; s[j] -= 1
                res.append(toalp(i))
    else:
        for i, j in q:
            if s[i] >= s[j]:
                s[j] += 1; s[i] -= 1
                res.append(toalp(j))
            else:
                s[i] += 1; s[j] -= 1
                res.append(toalp(i))
    print("Yes")
    print(*res, sep="\n")

solve()

Submission status: Runtime Error

Solution code:
import sys

f = open("C:/Users/naoki/Desktop/Atcoder/input.txt")

 #入力
N, A, B, C = map(int, f.readline().split())
stock = [A, B, C]

s = [f.readline().rstrip('\n') for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])
if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] in target[i+1] and stock[target[i][1]] >= 1:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            elif stock[target[i][0]] >= 1:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
            else:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Runtime Error

Solution code:
import sys


N, A, B, C = map(int, inpute().split())
stock = [A, B, C]

s = [input() for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]

target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])

if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append(target[i][1])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append(target[i][0])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] in target[i+1] and stock[target[i][1]] >= 1:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            else:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Runtime Error

Solution code:
import sys
N, A, B, C = map(int, input().split())
stock = [A, B, C]

s = [input() for _ in range(N)]
sum_num = sum(list([A, B, C]))
to_char = ["A","B","C"]


target = []
for i in range(N):
    if s[i] == "AB":
        target.append([0,1])
    elif s[i] == "BC":
        target.append([1,2])
    else:
        target.append([0,2])

if sum_num == 0:
    print("No")
    sys.exit()
elif sum_num == 1:
    answer = []
    for i in range(N):
        if stock[target[i][0]] == 1:
            stock[target[i][0]] -= 1
            stock[target[i][1]] += 1
            answer.append([target[i][1]])
        elif stock[target[i][1]] == 1:
            stock[target[i][1]] -= 1
            stock[target[i][0]] += 1
            answer.append([target[i][0]])
        else :
            print("No")
            sys.exit()
    print("Yes")
    for i in range(N):
        to_char[answer[i]]
elif sum_num >= 2:
    if stock[target[0][0]] == 0 and stock[target[0][1]] == 0:
        print("No")
        sys.exit()
    else:
        print("Yes")
        for i in range(N-1):
            if target[i][0] in target[i+1]:
                stock[target[i][0]] += 1
                stock[target[i][1]] -= 1
                print(to_char[target[i][0]])
            else:
                stock[target[i][1]] += 1
                stock[target[i][0]] -= 1
                print(to_char[target[i][1]])
        if stock[target[-1][0]] == 0:
            print(to_char[target[-1][0]])
        else:
            print(to_char[target[-1][1]])

Submission status: Runtime Error

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

def end():
    print("No")
    exit()

def under_zero(x):
    if x < 0:
        return True
    else:
        return False

def solve(string,a,b,c):

    if string == "AB":
        if a>b:
            a -= 1
            b += 1
            log.append("B")
            if under_zero(a):
                return end()
        else:
            a += 1
            b -= 1
            log.append("A")
            if under_zero(b):
                return end()
    elif string == "AC":
        if a>c:
            a -= 1
            c += 1
            log.append("C")
            if under_zero(a):
                return end()
        else:
            a += 1
            c -= 1
            log.append("A")
            if under_zero(c):
                return end()
    else:
        if b>c:
            b -= 1
            c += 1
            log.append("C")
            if under_zero(b):
                return end()
        else:
            b += 1
            c -= 1
            log.append("B")
            if under_zero(c):
                return end()

def check(i,a,b,c):
    now = list(s[i])
    next = list(s[i+1])

    for i in now:
        if i in next:
            d = i

    #print(d)

    # aが重複
    if 'A' == d and now[1] == 'B':
        a += 1
        b -= 1
        log.append("A")

    elif 'A' == d and now[1] == 'C':
        a += 1
        c -= 1
        log.append("A")

    # bが重複
    if 'B' == d and now[0] == 'A':
        a -= 1
        b += 1
        log.append("B")

    elif 'B' == d and now[1] == 'C':
        b += 1
        c -= 1
        log.append("B")

    # cが重複
    if 'C' == d and now[0] == 'A':
        c += 1
        a -= 1
        log.append("C")

    elif 'C' == d and now[0] == 'B':
        c += 1
        b -= 1
        log.append("C")

    return

for i in range(n):
    if (a + b + c) == 2 and i != n-1 and (a == 1 and b == 1) or (a == 1 and c == 1) or (c == 1 and b == 1):
        check(i,a,b,c)
    else:
        solve(s[i],a,b,c)
else:
    print("Yes")
    print(*log, sep='\n')

Submission status: Runtime Error

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

ans='Yes'
L_ans=['']*(N+1)
i=0
while i<N:
    if S[i]=='AB':
        if A>B:
            B+=1
            A-=1
            L_ans[i]='B'
        elif A<B:
            A+=1
            B-=1
            L_ans[i]='A'
        elif A==B and S[i+1]=='BC':
            B+=1
            A-=1
            L_ans[i]='B'
        else:
            A+=1
            B-=1
            L_ans[i]='A'
    elif S[i]=='AC':
        if A>C:
            C+=1
            A-=1
            L_ans[i]='C'
        elif A<C:
            A+=1
            C-=1
            L_ans[i]='A'
        elif A==C and S[i+1]=='BC':
            C+=1
            A-=1
            L_ans[i]='C'
        else:
            A+=1
            C-=1
            L_ans[i]='A'
    elif S[i]=='BC':
        if C>B:
            B+=1
            C-=1
            L_ans[i]='B'
        elif C<B:
            C+=1
            B-=1
            L_ans[i]='C'
        elif B==C and S[i+1]=='AB':
            B+=1
            C-=1
            L_ans[i]='B'
        else:
            C+=1
            B-=1
            L_ans[i]='C'
    i+=1
    if any([A<0,B<0,C<0]):
        ans='No'
        break

print(ans)
if ans=='Yes':
    j=0
    while j<N:
        print(L_ans[j])
        j+=1

Submission status: Runtime Error

Solution code:
N,A,B,C=map(int,input().split())
*S,=open(0).read().split()

ans='Yes'
L_ans=['']*N
i=0
while i<N:
    if S[i]=='AB':
        if A>=B:
            B+=1
            A-=1
            L_ans[i]='B'
        else:
            A+=1
            B-=1
            L_ans[i]='A'
    elif S[i]=='AC':
        if A>=C:
            C+=1
            A-=1
            L_ans[i]='C'
        else:
            A+=1
            C-=1
            L_ans[i]='A'
    elif S[i]=='BC':
        if C>=B:
            B+=1
            C-=1
            L_ans[i]='B'
        else:
            C+=1
            B-=1
            L_ans[i]='C'
    i+=1
    if any([A<0,B<0,C<0]):
        ans='No'
        break

print(ans)
if ans=='Yes':
    for j in L_ans:
        print(j)

Submission status: Runtime Error

Solution code:
x = [int(a) for a in input().split()]
N, A, B, C = x
v = [A, B, C]

if sum(v) >= 3:
  # greedy works
  decisions = []
  flg = True
  for i in range(N):
    s1, s2 = input()
    i1 = ord(s1)-ord("A")
    i2 = ord(s2)-ord("A")
    if v[i1] == 0 and v[i2] == 0:
      flg = False
      break
    if v[i1] > v[i2]:
      decisions.append(chr(i2+65))
      v[i1] -= 1
      v[i2] += 1
    else:
      decisions.append(chr(i1+65))
      v[i1] += 1
      v[i2] -= 1
  if flg:
    print("Yes")
    print("\n".join(decisions))
  else:
    print("No")
else:
  v = tuple(v)
  i1s = []
  i2s = []
  for i in range(N):
    s1, s2 = input()
    i1s.append(ord(s1)-ord("A"))
    i2s.append(ord(s2)-ord("A"))
  f = []
  for i in range(N):
    f.append({})

  def get(i, s):
    if s in f[i]:
      return f[i][s]
    if i == N-1:
      i1 = i1s[i]
      i2 = i2s[i]
      if s[i1] == 0 and s[i2] == 0:
        f[i][s] = False
      else:
        f[i][s] = True
      return f[i][s]
    i1 = i1s[i]
    i2 = i2s[i]
    if s[i1] == 0 and s[i2] == 0:
      f[i][s] = False
      return False
    s1 = list(s)
    s1[i1] -= 1
    s1[i2] += 1
    s1 = tuple(s1)

    s2 = list(s)
    s2[i2] -= 1
    s2[i1] += 1
    s2 = tuple(s2)
    
    f1 = get(i+1, s1) if s1[i1] >= 0 else False
    f2 = get(i+1, s2) if s2[i2] >= 0 else False
    if f1 or f2:
      f[i][s] = True
      return True
    else:
      f[i][s] = False
      return False
  ans = get(0, v)
  if ans:
    print("Yes")
    s = tuple(v)
    for i in range(N):
      #print(s)
      i1 = i1s[i]
      i2 = i2s[i]
      if i == N-1:
        if s[i1] > 0:
          print(chr(i1+65))
        else:
          print(chr(i2+65))
        break
      s1 = list(s)
      s1[i1] -= 1
      s1[i2] += 1
      s1 = tuple(s1)
      s2 = list(s)
      s2[i2] -= 1
      s2[i1] += 1
      s2 = tuple(s2)
      f1 = get(i+1, s1)
      f2 = get(i+1, s2)
      if f1:
        print(chr(i1+65))
        s = s1
      else:
        print(chr(i2+65))
        s = s2
  else:
    print("No")

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
# a+i と i-aのとりうる値の個数をカウントするが
# 一致するとしても[0, n)の範囲だけなのでDの大きさはnで十分
D = [0]*n
for i, a in enumerate(A):
    if i+a < n:
        D[i+a] += 1
ans = 0
# i-aに対応するD[i-a]を加えるだけで答えが求まる
for i, a in enumerate(A):
    if i-a >= 0:
        ans += D[i-a]
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
arr=list(map(int,input().split()))
d=[0]*n
for i,num in enumerate(arr):
  if (i+num)<n:
    d[i+num]+=1
res=0
for i,num in enumerate(arr):
  if (i-num)>=0:
    res+=d[i-num]
print(res)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
d=[0]*n
for i,num in enumerate(a):
  if (i+num)<n:
    d[i+num]+=1
res=0
for i,num in enumerate(a):
  if (i-num)>=0:
    res+=d[i-num]
print(res)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
d=[0]*n
for i,num in enumerate(a):
  if (i+a)<n:
    d[i+a]+=1
res=0
for i,num in enumerate(a):
  if (i-a)>=0:
    res+=d[i-a]
print(res)