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 = [input() for _ in range(n)]
if s[0] == 'AB'and a == 0 and b == 0:
    print('No')
elif s[0] == 'BC' and b == 0 and c == 0:
    print('No')
elif s[0] == 'AC' and a == 0 and c == 0:
    print('No')
elif a + b + c >= 2:
    exit(1)
    print('Yes')
    ans = []
    for i in range(n):
        if i < n - 1:
            if a == b == 1 and c == 0 and s[i] == 'AB':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    b -= 1
                else:
                    ans.append('B')
                    a -= 1
                    b += 1
                continue
            elif b == c == 1 and a == 0 and s[i] == 'BC':
                if s[i + 1] == 'AB' or s[i + 1] == 'BC':
                    ans.append('B')
                    c -= 1
                    b += 1
                else:
                    ans.append('C')
                    c += 1
                    b -= 1
                continue
            elif a == c == 1 and b == 0 and s[i] == 'AC':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    c -= 1
                else:
                    ans.append('C')
                    a -= 1
                    c += 1
                continue
        if s[i] == 'AB':
            if a <= b:
                ans.append('A')
                a += 1
                b -= 1
            else:
                ans.append('B')
                a -= 1
                b += 1
        elif s[i] == 'BC':
            if b <= c:
                ans.append('B')
                b += 1
                c -= 1
            else:
                ans.append('C')
                b -= 1
                c += 1
        else:
            if a <= c:
                ans.append('A')
                a += 1
                c -= 1
            else:
                ans.append('C')
                a -= 1
                c += 1
    for c in ans:
        print(c)
else:
    ans = []
    if a == 1:
        t = 0
    elif b == 1:
        t = 1
    else:
        t = 2
    for i in range(n):
        if s[i] == 'AB' and t == 2 or s[i] == 'BC' and t == 0 or s[i] == 'AC' and t == 1:
            print('No')
            exit(0)
        elif s[i] == 'AB':
            if t == 0:
                ans.append('B')
            else:
                ans.append('A')
        elif s[i] == 'BC':
            if t == 1:
                ans.append('C')
            else:
                ans.append('B')
        else:
            if t == 0:
                ans.append('C')
            else:
                ans.append('A')
    print('Yes')
    for c in ans:
        print(c)

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().split())
s = [input() for _ in range(n)]
if s[0] == 'AB'and a == 0 and b == 0:
    print('No')
elif s[0] == 'BC' and b == 0 and c == 0:
    print('No')
elif s[0] == 'AC' and a == 0 and c == 0:
    print('No')
elif a + b + c >= 2:
    exit(1)
    print('Yes')
    ans = []
    for i in range(n):
        if i < n - 1:
            if a == b == 1 and c == 0 and s[i] == 'AB':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    b -= 1
                else:
                    ans.append('B')
                    a -= 1
                    b += 1
                continue
            elif b == c == 1 and a == 0 and s[i] == 'BC':
                if s[i + 1] == 'AB' or s[i + 1] == 'BC':
                    ans.append('B')
                    c -= 1
                    b += 1
                else:
                    ans.append('C')
                    c += 1
                    b -= 1
                continue
            elif a == c == 1 and b == 0 and s[i] == 'AC':
                if s[i + 1] == 'AB' or s[i + 1] == 'AC':
                    ans.append('A')
                    a += 1
                    c -= 1
                else:
                    ans.append('C')
                    a -= 1
                    c += 1
                continue
        if s[i] == 'AB':
            if a <= b:
                ans.append('A')
                a += 1
                b -= 1
            else:
                ans.append('B')
                a -= 1
                b += 1
        elif s[i] == 'BC':
            if b <= c:
                ans.append('B')
                b += 1
                c -= 1
            else:
                ans.append('C')
                b -= 1
                c += 1
        else:
            if a <= c:
                ans.append('A')
                a += 1
                c -= 1
            else:
                ans.append('C')
                a -= 1
                c += 1
    for c in ans:
        print(c)
else:
    exit(1)
    ans = []
    if a == 1:
        t = 0
    elif b == 1:
        t = 1
    else:
        t = 2
    for i in range(n):
        if s[i] == 'AB' and t == 2 or s[i] == 'BC' and t == 0 or s[i] == 'AC' and t == 1:
            print('No')
            exit(0)
        elif s[i] == 'AB':
            if t == 0:
                ans.append('B')
            else:
                ans.append('A')
        elif s[i] == 'BC':
            if t == 1:
                ans.append('C')
            else:
                ans.append('B')
        else:
            if t == 0:
                ans.append('C')
            else:
                ans.append('A')
    print('Yes')
    for c in ans:
        print(c)

Submission status: Runtime Error

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

ss = []
for i in range(n):
    ss.append(input())

ans = []
tower = [a,b,c]
s2num = {"AB":[0,1],"BC":[1,2],"AC":[0,2]}
num2s = {0:"A",1:"B",2:"C"}
flag =True

def check_large(l,r,t):
    if t[l]>t[r]:
        return l,r,False
    elif t[l]<t[r]:
        return r,l,False
    else:
        return l,r,True

for i in range(n):
    s = ss[i]
    l,r = s2num[s]
    big,small,f = check_large(l,r,tower)
    if f:
        if i == n-1:
            pass
        else:
            nex = s2num[ss[i+1]]
            if big in nex:
                small,big = big,small
            else:
                continue
            
    if tower[big] <= 0:
        flag = False
        break
    else:
        tower[big]-=1
        tower[small]+=1
        ans.append(num2s[small])

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

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    def ng():
        print("No")
        exit()

    n,a,b,c=MI()
    ss=[SI() for _ in range(n)]
    ans=[]
    for i in range(n):
        s=ss[i]
        if s=="AB":
            if (a,b)==(0,0):ng()
            elif (a,b)==(0,1):
                ans.append("A")
                a,b=a+1,b-1
            elif (a,b)==(1,0):
                ans.append("B")
                a,b=a-1,b+1
            elif (a,b,c)==(1,1,0):
                if i==n-1:
                    ans.append("A")
                else:
                    if s[i+1]=="AC":
                        ans.append("A")
                        a,b=a+1,b-1
                    else:
                        ans.append("B")
                        a, b = a - 1, b + 1
            else:
                if a<b:
                    ans.append("A")
                    a, b = a + 1, b - 1
                else:
                    ans.append("B")
                    a, b = a - 1, b + 1
        elif s=="BC":
            if (b,c)==(0,0):ng()
            elif (b,c)==(0,1):
                ans.append("B")
                b,c=b+1,c-1
            elif (b,c)==(1,0):
                ans.append("C")
                b,c=b-1,c+1
            elif (a,b,c)==(0,1,1):
                if i==n-1:
                    ans.append("B")
                else:
                    if s[i+1]=="AC":
                        ans.append("C")
                        b, c = b - 1, c + 1
                    else:
                        ans.append("B")
                        b, c = b + 1, c - 1
            else:
                if c<b:
                    ans.append("C")
                    b, c = b - 1, c + 1
                else:
                    ans.append("B")
                    b, c = b + 1, c - 1
        else:
            if (a,c)==(0,0):ng()
            elif (a,c)==(0,1):
                ans.append("A")
                a,c=a+1,c-1
            elif (a,c)==(1,0):
                ans.append("C")
                a,c=a-1,c+1
            elif (a,b,c)==(1,0,1):
                if i==n-1:
                    ans.append("A")
                else:
                    if s[i+1]=="AB":
                        ans.append("A")
                        a, c = a + 1, c - 1
                    else:
                        ans.append("C")
                        a, c = a - 1, c + 1
            else:
                if a<c:
                    ans.append("A")
                    a, c = a + 1, c - 1
                else:
                    ans.append("C")
                    a, c = a - 1, c + 1
    print("Yes")
    for s in ans:
        print(s)

main()

Submission status: Runtime Error

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

n,a,b,c = map(int,input().split())
ans = []
abc = [a,b,c]

d = {'AB\n':(0,1),'AC\n':(0,2),'BC\n':(1,2)}

if(a+b+c >= 3)|(a+b+c==1):
    for _ in range(n):
        s = input()
        x,y = d[s]
        if(abc[x]==0)&(abc[y]==0):
            print('No')
            exit()
        if(abc[x] < abc[y]):
            x,y = y,x
        abc[x] -= 1
        abc[y] += 1
        ans.append(y)
else:
    ss = (input() for _ in range(n))
    if(max(abc)==2):
        #(2,0,0)
        case = True
    else:
        case = False

    for i in range(n):
        s = ss[i]
        x,y = d[s]
        if(case)|(i==n-1):
            if(abc[x]==0)&(abc[y]==0):
                print('No')
                exit()
            if(abc[x] < abc[y]):
                x,y = y,x
            abc[x] -= 1
            abc[y] += 1
            case = False
        else:
            if(abc[x]!=abc[y]):
                if(abc[x] < abc[y]):
                    x,y = y,x
                abc[x] -= 1
                abc[y] += 1
            else:
                sn = ss[i+1]
                if(x in d[sn]):
                    x,y = y,x
                abc[x] -= 1
                abc[y] += 1
            case = True
        ans.append(y)

print('Yes')
for i in ans:
    print(chr(i+ord('A')))

Submission status: Runtime Error

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
D = {"AB": 2, "AC": 1, "BC": 0}
N, a, b, c = map(int, input().split())
X = [a, b, c]
S = [D[input()] for _ in range(N)]
ANS = []
if max(X) == 0:
    print("No")
    exit()

if sum(X) == 1:
    t = 0 if a else 1 if b else 2
    for s in S:
        if X[s]:
            print("No")
            exit()
        t ^= s ^ 3
        ANS.append(t)
        X[s-1] ^= 1
        X[s-2] ^= 1
    print("Yes")
    print("\n".join([chr(t+65) for t in ANS]))
    exit()

s = S[0]
if X[s-1] + X[s-2] == 0:
    print("No")
    exit()

print(1 // 0)
for i, s in enumerate(S):
    if X[s-1] > X[s-2]:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    elif X[s-1] < X[s-2]:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1
    elif i == N - 1 or S[i+1] == s-1:
        ANS.append((s-2) % 3)
        X[s-1] -= 1
        X[s-2] += 1
    else:
        ANS.append((s-1) % 3)
        X[s-1] += 1
        X[s-2] -= 1

print("Yes")
print("\n".join([chr(t+65) for t in ANS]))

Submission status: Runtime Error

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

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

ans = []
def solve(idx, x, y, z, cur):
    if x < 0 or y < 0 or z < 0:
        return
    if idx == n:
        if x >= 0 and y >= 0 and z >= 0:
            print('Yes')
            for i in range(n):
                print(cur[i])
            exit()
        #     ans.append(cur)
        # return

    if s[idx] == 'AB':
        solve(idx + 1, x + 1, y - 1, z, cur + ['A'])
        solve(idx + 1, x - 1, y + 1, z, cur + ['B'])
    elif s[idx] == 'AC':
        solve(idx + 1, x + 1, y, z - 1, cur + ['A'])
        solve(idx + 1, x - 1, y, z + 1, cur + ['C'])
    elif s[idx] == 'BC':
        solve(idx + 1, x, y + 1, z - 1, cur + ['B'])
        solve(idx + 1, x, y - 1, z + 1, cur + ['C'])

solve(0, a, b, c, [])

print(ans)

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
S = [input() for _ in range(N)]
a = min(A,10**5)
b = min(B,10**5)
c = min(C,10**5)

res = ['0']*N

# 明らかに可能
if a+b+c >= 3:
    print('Yes')
    for i in range(N):
        if S[i]=='AC':
            if a<c:
                res[i] = 'A'
                a += 1
                c -= 1
            else:
                res[i] = 'C'
                c += 1
                a -= 1
        elif S[i]=='AB':
            if a<b:
                res[i] = 'A'
                a += 1
                b -= 1
            else:
                res[i] = 'B'
                b += 1
                a -= 1
        else:
            if b<c:
                res[i] = 'B'
                b += 1
                c -= 1
            else:
                res[i] = 'C'
                c += 1
                b -= 1
    for val in res:
        print(val)
else:
    assert()

Submission status: Runtime Error

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

ans = [0]*N
for i,s in enumerate(S):
    if s=="AB":
        if A==B==0:
            print("No")
            break
        elif A==B==1:
            if s[(i+1)%N]=="BC":
                ans[i] = "B"
                B += 1
                A -= 1
            else:
                ans[i] = "A"
                A += 1
                B -= 1
        elif A>=B:
            ans[i] = "B"
            A -= 1
            B += 1
        else:
            ans[i] = "A"
            A += 1
            B -= 1
    elif s=="BC":
        if B==C==0:
            print("No")
            break
        elif B==C==1:
            if s[(i+1)%N]=="AC":
                ans[i] = "C"
                C += 1
                B -= 1
            else:
                ans[i] = "B"
                B += 1
                C -= 1
        elif B>=C:
            ans[i] = "C"
            B -= 1
            C += 1
        else:
            ans[i] = "B"
            B += 1
            C -= 1
    elif s=="AC":
        if A==C==0:
            print("No")
            break
        elif A==C==1:
            if s[(i+1)%N]=="BC":
                ans[i] = "C"
                C += 1
                A -= 1
            else:
                ans[i] = "A"
                A += 1
                C -= 1
        elif A>=C:
            ans[i] = "C"
            A -= 1
            C += 1
        else:
            ans[i] = "A"
            A += 1
            C -= 1
else:
    print("Yes")
    for out in ans:
        print(out)

Submission status: Runtime Error

Solution code:
s = [0,0,0]
n,s[0],s[1],s[2] = map(int,input().split())
T = [0]*n
ABC = 'ABC'
for i in range(n):
    t = input()
    if t == 'AB':T[i] = 2
    if t == 'BC':T[i] = 0
    if t == 'AC':T[i] = 1
ans = ['' for _ in range(n)]
if s.count(0) <= 1 and sum(s) >= 3:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
    print('Yes')
    print('\n'.join(ans))
elif s.count(0) == 1:
    now = s.index(0)
    i = 0
    while i < n-1:
        if T[i] == T[i+1]:
            if T[i] == now:
                ans[i] = ABC[(now-1)%3]
                ans[i+1] = ABC[(now-2)%3]
            else:
                for j in range(3):
                    if j not in [T[i],now]:
                        ans[i+1] = ABC[j]
                ans[i] = now
            i += 2
        elif T[i] == now:
            for j in range(3):
                if j not in [T[i+1],now]:ans[i] = ABC[j]
            ans[i+1] = ABC[T[i]]
            now = T[i+1]
            i += 2
        else:
            ans[i] = ABC[now]
            for j in range(3):
                if j not in [T[i],now]:
                    now = j
                    break
            i += 1
    if i == n-1:
        ans[i] = ABC[(T[i]-1)%3]
    print('Yes')
    print('\n'.join(ans))
else:
    for i,t in enumerate(T):
        nxt = [0,1,2]
        nxt.remove(t)
        if s[nxt[0]] < s[nxt[1]]:
            ans[i] = ABC[nxt[0]]
            s[nxt[0]] += 1
            s[nxt[1]] -= 1
        else:
            ans[i] = ABC[nxt[1]]
            s[nxt[0]] -= 1
            s[nxt[1]] += 1
        if -1 in s:
            print('No')
            break
    else:
        print('Yes')
        print('\n'.join(ans))

Submission status: Runtime Error

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

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

if A+B+C==0:
    print("No")
elif A+B+C==1:
    ans = []
    flag = True
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                flag = False
                break
            else:
                if A==0:
                    ans.append("A")
                else:
                    ans.append("B")
                A,B = B,A
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    ans.append("B")
                else:
                    ans.append("C")
                B,C = C,B
        elif S[i]=="AC":
            if A==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if A==0:
                    ans.append("A")
                else:
                    ans.append("C")
                A,C = C,A
    if flag:
        print("Yes")
        for a in ans:
            print(a)
elif A+B+C>=2:
    ans = []
    flag = True
    for i in range(N):
        if S[i]=="AB":
            if A==0 and B==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    B += 1
                    A -= 1
                    ans.append("B")
                elif A==0:
                    B -= 1
                    A += 1
                    ans.append("A")
                elif A==1 and B==1:
                    if i<N:
                        if S[i+1]=="AB":
                            A+=1
                            B-=1
                            ans.append("A")
                        elif S[i+1]=="BC":
                            A-=1
                            B+=1
                            ans.append("B")
                        elif S[i+1]=="AC":
                            A+=1
                            B-=1
                            ans.append("A")
                    else:
                        A+=1
                        B-=1
                        ans.append("A")
                else:
                    if A<=B:
                        A+=1
                        B-=1
                        ans.append("A")
                    else:
                        A-=1
                        B+=1
                        ans.append("B")
        elif S[i]=="BC":
            if B==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if B==0:
                    B += 1
                    C -= 1
                    ans.append("B")
                elif C==0:
                    B -= 1
                    C += 1
                    ans.append("C")
                elif B==1 and C==1:
                    if i<N:
                        if S[i+1]=="AB":
                            C-=1
                            B+=1
                            ans.append("B")
                        elif S[i+1]=="BC":
                            B+=1
                            C-=1
                            ans.append("B")
                        elif S[i+1]=="AC":
                            B-=1
                            C+=1
                            ans.append("C")
                    else:
                        B+=1
                        C-=1
                        ans.append("B")
                else:
                    if B<=C:
                        B+=1
                        C-=1
                        ans.append("B")
                    else:
                        B-=1
                        C+=1
                        ans.append("C")
        if S[i]=="AC":
            if A==0 and C==0:
                print("No")
                flag = False
                break
            else:
                if C==0:
                    C += 1
                    A -= 1
                    ans.append("C")
                elif A==0:
                    C -= 1
                    A += 1
                    ans.append("A")
                elif A==1 and C==1:
                    if i<N:
                        if S[i+1]=="AB":
                            A+=1
                            C-=1
                            ans.append("A")
                        elif S[i+1]=="BC":
                            A-=1
                            C+=1
                            ans.append("C")
                        elif S[i+1]=="AC":
                            A+=1
                            C-=1
                            ans.append("A")
                    else:
                        A+=1
                        C-=1
                        ans.append("A")
                else:
                    if A<=C:
                        A+=1
                        C-=1
                        ans.append("A")
                    else:
                        A-=1
                        C+=1
                        ans.append("C")
    if flag:
        print("Yes")
        for a in ans:
            print(a)

Submission status: Runtime Error

Solution code:
def game(cmds,A,B,C):
    print(cmds,A,B,C)
    if len(cmds)==0:
        if A>=0 and B>=0 and C>=0:
            return True,[]
        else:
            return False,[]
    cmd=cmds.pop(0)
    if cmd=="AB":
        if A<B:
            suc,his= game(cmds,A+1,B-1,C)
            if suc:
                return suc,his+["A"]
            else:
                return False,[]
        elif A>B:
            suc,his= game(cmds,A-1,B+1,C)
            if suc:
                return suc,his+["B"]
            else:
                return False,[]
        else:
            if A==0 and B==0:
                return False,[]
            g1= game(cmds,A-1,B+1,C)
            if g1[0]:
                return g1[0],g1[1]+["B"]
            g2=game(cmds,A+1,B-1,C)
            if g2[0]:
                return g2[0],g2[1]+["A"]
            else:
                return False,[]

    elif cmd=="AC":
        if A<C:
            suc,his= game(cmds,A+1,B,C-1)
            if suc:
                return suc,his+["A"]
            else:
                return False,[]
        elif A>C:
            suc,his= game(cmds,A-1,B,C+1)
            if suc:
                return suc,his+["C"]
            else:
                return False,[]
        else:
            if A==0 and C==0:
                return False,[]
            g1= game(cmds,A-1,B,C+1)
            if g1[0]:
                return g1[0],g1[1]+["C"]
            g2=game(cmds,A+1,B,C-1)
            if g2[0]:
                return g2[0],g2[1]+["A"]
            else:
                return False,[]
    else:
        if C<B:
            suc,his= game(cmds,A,B-1,C+1)
            if suc:
                return suc,his+["C"]
            else:
                return False,[]
        elif C>B:
            suc,his= game(cmds,A,B+1,C-1)
            if suc:
                return suc,his+["B"]
            else:
                return False,[]
        else:
            if C==0 and B==0:
                return False,[]
            g1= game(cmds,A,B+1,C-1)
            if g1[0]:
                return g1[0],g1[1]+["B"]
            g2=game(cmds,A,B-1,C+1)
            if g2[0]:
                return g2[0],g2[1]+["C"]
            else:
                return False,[]


N,A,B,C=[int(i) for i in input().split(" ")]
cmds=[]
for i in range(N):
    cmds.append(input())

suc,his=game(cmds,A,B,C)
if suc:
    print("Yes")
    for h in his[::-1]:
        print(h)
else:
    print("No")

Submission status: Runtime Error

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

data = {
    'A': A,
    'B': B,
    'C': C
}
ans = []

def bt( i ) :
    if i >= N :
        return True
    if data[commands[i][0]] == 0 and data[commands[i][0]] == 0 :
        return False

    for x, y in [ commands[i], commands[i][::-1] ] :
        if data[x] == 0 :
            continue
        data[x] -= 1
        data[y] += 1
        ans.append(y)
        if bt(i+1 ) :
            return True
        ans.pop() 
        data[x] += 1
        data[y] -= 1
    
    return False

if bt(0) :
    print("Yes")
    for c in ans :
        print(c)
else :
    print("No")

Submission status: Runtime Error

Solution code:
def peaks(lookup, hs, n):
    ans = 0
    for k in lookup.keys():
        for kk in lookup[k]:
            if hs[k-1] <= hs[kk-1]:
                ans -= 1
                break
        ans += 1
    print(ans + n - len(lookup.keys()))

if __name__ == "__main__":
    import sys
    from collections import defaultdict as DD
    lookup = DD(dict)
    n, m = [int(_) for _ in input().split()]
    hs = [int(_) for _ in input().split()]
    for i in range(m):
        a, b = [int(_) for _ in input().split()]
        lookup[a][b] = lookup[b][a] = True
    peaks(lookup, hs, n)

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
li = []
f = True
for i in range(N):
    s = input()
    if s == 'AB':
        if A==B==0:
            f = False
            break
        elif A == 0:
            A += 1
            B -= 1
            li.append('A')
        elif B == 0:
            A -= 1
            B += 1
            li.append('B')
        else:
            if A >= B:
                A -= 1
                B += 1
                li.append('B')
            else:
                A += 1
                B -= 1
                li.append('A')
    if s == 'AC':
        if A==C==0:
            f = False
            break
        elif A == 0:
            A += 1
            BC -= 1
            li.append('A')
        elif C == 0:
            A -= 1
            C += 1
            li.append('C')
        else:
            if A >= C:
                A -= 1
                C += 1
                li.append('C')
            else:
                A += 1
                C -= 1
                li.append('A')
    if s == 'BC':
        if C==B==0:
            f = False
            break
        elif C == 0:
            C += 1
            B -= 1
            li.append('C')
        elif B == 0:
            C -= 1
            B += 1
            li.append('B')
        else:
            if C >= B:
                C -= 1
                B += 1
                li.append('B')
            else:
                C += 1
                B -= 1
                li.append('C')
if f:
    print('Yes')
    for x in li:
        print(x)
else:
    print('No')

Submission status: Runtime Error

Solution code:
def solve():
    ans = []
    for i, (x, y) in enumerate(s):
        if num[x]>num[y]: x, y = y, x
        if num[x]==num[y] and i < n-1:
            if y == s[i+1][0] or y == s[i+1][1]:
                x, y = y, x
        num[x]+=1
        num[y]-=1
        ans.append(x)
        if num[y]<0: return False
    return True
from collections import defaultdict
import sys
n, a, b, c = map(int, input().split())
num = defaultdict(int)
s = [input() for _ in range(n)]
num['A'] = a
num['B'] = b
num['C'] = c
id = ['A', 'B', 'C']
ans = solve()
if ans:
    print('Yes')
    print(*ans, sep = '\n')
else:
    print('No')