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:
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
n = inpl()
s = []
for i in range(n[0]):
    s.append(input())
A = n[1]
B = n[2]
C = n[3]
def dfs(a,b,c,tmp,step):
    if a < 0 or b < 0 or c < 0:
        return []
    if step == n[0]:
        return tmp
    for j in range(step, len(s)):
        if s[j] == "AB":
            tmp_tmp = dfs(a + 1, b-1, c, tmp + ["A"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a-1, b+1, c, tmp + ["B"], step + 1)
        elif s[j] == "AC":
            tmp_tmp = dfs(a+1, b, c-1, tmp +["A"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a-1, b, c + 1, tmp + ["C"], step + 1)
        else:
            tmp_tmp = dfs(a, b + 1, c-1, tmp + ["B"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a, b - 1, c + 1, tmp + ["C"], step+ 1)
ans = dfs(A,B,C,[],0)
if ans:
    print("Yes")
    for i in range(len(ans)):
        print(ans[i])
else:
    print("No")

Submission status: Runtime Error

Solution code:
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
n = inpl()
s = []
for i in range(n[0]):
    s.append(input())
A = n[1]
B = n[2]
C = n[3]
def dfs(a,b,c,tmp,step):
    if a < 0 or b < 0 or c < 0:
        return []
    if step == n[0]:
        return tmp
    for i in range(step, len(s)):
        if s[i] == "AB":
            tmp_tmp = dfs(a + 1, b-1, c, tmp + ["A"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                tmp_tmp = dfs(a-1, b+1, c, tmp + ["B"], step + 1)
                return tmp_tmp
        elif s[i] == "AC":
            tmp_tmp = dfs(a+1, b, c-1, tmp +["A"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a-1, b, c + 1, tmp + ["C"], step + 1)
        else:
            tmp_tmp = dfs(a, b + 1, c-1, tmp + ["B"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a, b-1, c + 1, tmp + ["C"], step+ 1)
ans = dfs(A,B,C,[],0)
if ans:
    print("Yes")
    for i in range(len(ans)):
        print(ans[i])
else:
    print("No")

Submission status: Runtime Error

Solution code:
import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, raw_input().split())
cmds = []

for i in xrange(n):
    s = raw_input()
    cmds.append(s)

cnt = [a, b, c]
res = []

for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 or cnt[p2] > 1:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
    elif cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif i == n - 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        # print cnt, p1, p2
        assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        if cmd == nxt:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
        else:
            for c in 'ABC':
                if c in cmd and c in nxt:
                    break
            else:
                assert False

            res.append(c)
            if c == cmd[0]:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
    assert min(cnt) >= 0


if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Runtime Error

Solution code:
import sys
def inpl(): return list(map(int, sys.stdin.readline().split()))
n = inpl()
s = []
for i in range(n[0]):
    s.append(input())
A = n[1]
B = n[2]
C = n[3]
def dfs(a,b,c,tmp,step):
    if a < 0 or b < 0 or c < 0:
        return []
    if step == n[0]:
        return tmp
    for i in range(step, len(s)):
        if s[i] == "AB":
            tmp_tmp = dfs(a + 1, b-1, c, tmp + ["A"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                tmp_tmp = dfs(a-1, b+1, c, tmp + ["B"], step + 1)
                return tmp_tmp
        elif s[i] == "AC":
            tmp_tmp = dfs(a+1, b, c-1, tmp +["A"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a-1, b, c + 1, tmp + ["C"], step + 1)
        else:
            tmp_tmp = dfs(a, b + 1, c, tmp + ["B"], step + 1)
            if tmp_tmp:
                return tmp_tmp
            else:
                return dfs(a, b-1, c, tmp + ["C"], step+ 1)


ans = dfs(A,B,C,[],0)
if ans:
    print("Yes")
    for i in range(len(ans)):
        print(ans[i])
else:
    print("No")

Submission status: Runtime Error

Solution code:
import sys
n,a,b,c=map(int,input().split())
slist=[]
for i in range(n):
    slist.append(input())
output=[]
ynflag=True

def abc1(s):
    global a
    global b
    global c
    global output
    global ynflag

    
    if s == "AB":
        if a ==1:
            a -=1
            b +=1
            output.append("B")
            return True
        elif b == 1:
            a +=1
            b -=1
            output.append("A")
            return True
        else:
            return False
    if s == "AC":
        if a ==1:
            a -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            a +=1
            c -=1
            output.append("A")
            return True
        else:
            return False
    if s == "BC":
        if b ==1:
            b -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            b +=1
            c -=1
            output.append("B")
            return True
        else:
            return False

def abc2(i):
    global a
    global b
    global c
    global output
    global slist
    s=slist[i]
    if i == len(slist)-1:
        if s =="AB":
            if a > b:
                output.append("B")
                return True
            elif b >= a:
                output.append("A")
                return True
        elif s =="AC":
            if a > c:
                output.append("C")
                return True
            elif c >= a:
                output.append("A")
                return True
        elif s =="BC":
            if b > c:
                output.append("C")
                return True
            elif c >= b:
                output.append("B")
                return True
    
    if s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if s[i+1] == "BC":
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
        else:
            if a >=b:
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
    elif s == "AC":
        if a + c == 0:
            return False
        elif a == 1 and c == 1 and b == 0:
            if s[i+1] == "BC":
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
        else:
            if a >=c:
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
    else:
        if b + c == 0:
            return False
        elif b == 1 and c == 1 and a == 0:
            if s[i+1] == "AC":
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True
        else:
            if b >=c:
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True

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

else:
    if a+b+c == 1:
        for s in slist:
            ynflag=abc1(s)
            if not ynflag:
                print("No")
                sys.exit()
    else:
        for i in range(n):
            ynflag=abc2(i)
            if not ynflag:
                print("No")
                sys.exit()
    print("Yes")
    for out in output:
        print(out)

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
S =[]
for x in range(N):
    S.append(input())
ans =[]
f =True

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

Submission status: Runtime Error

Solution code:
import sys
n,a,b,c=map(int,input().split())
slist=[]
for i in range(n):
    slist.append(input())
output=[]
ynflag=True

def abc1(s):
    global a
    global b
    global c
    global output
    global ynflag

    
    if s == "AB":
        if a ==1:
            a -=1
            b +=1
            output.append("B")
            return True
        elif b == 1:
            a +=1
            b -=1
            output.append("A")
            return True
        else:
            return False
    if s == "AC":
        if a ==1:
            a -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            a +=1
            c -=1
            output.append("A")
            return True
        else:
            return False
    if s == "BC":
        if b ==1:
            b -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            b +=1
            c -=1
            output.append("B")
            return True
        else:
            return False

def abc2(i):
    global a
    global b
    global c
    global output
    global slist
    s=slist[i]
    if i == len(s)-1:
        if s =="AB":
            if a > b:
                output.append("A")
                return True
            elif b >= a:
                output.append("B")
                return True
        elif s =="AC":
            if a > c:
                output.append("A")
                return True
            elif c >= a:
                output.append("C")
                return True
        elif s =="BC":
            if b > c:
                output.append("B")
                return True
            elif c >= b:
                output.append("C")
                return True
    
    if s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if s[i+1] == "BC":
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
        else:
            if a >=b:
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
    elif s == "AC":
        if a + c == 0:
            return False
        elif a == 1 and c == 1 and b == 0:
            if s[i+1] == "BC":
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
        else:
            if a >=c:
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
    else:
        if b + c == 0:
            return False
        elif b == 1 and c == 1 and a == 0:
            if s[i+1] == "AC":
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True
        else:
            if b >=c:
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True

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

else:
    if a+b+c == 1:
        for s in slist:
            ynflag=abc1(s)
            if not ynflag:
                print("No")
                sys.exit()
    else:
        for i in range(n):
            ynflag=abc2(i)
            if not ynflag:
                print("No")
                sys.exit()
    print("Yes")
    for out in output:
        print(out)

Submission status: Runtime Error

Solution code:
import sys
if sys.subversion[0] == "PyPy":
    import io, atexit
    sys.stdout = io.BytesIO()
    atexit.register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
    
    sys.stdin = io.BytesIO(sys.stdin.read())
    input = lambda: sys.stdin.readline().rstrip()

n, a, b, c = map(int, raw_input().split())
cmds = []

for i in xrange(n):
    s = raw_input()
    cmds.append(s)

cnt = [a, b, c]
res = []

for i in xrange(n):
    cmd = cmds[i]
    p1 = ord(cmd[0]) - ord('A')
    p2 = ord(cmd[1]) - ord('A')

    if cnt[p1] > 1 and cnt[p2] > 1:
        if cnt[p1] > cnt[p2]:
            res.append(cmd[1])
            cnt[p1] -= 1
            cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1
    elif cnt[p1] == 0 and cnt[p2] == 0:
        res = []
        break
    elif cnt[p1] == 1 and cnt[p2] == 0:
        res.append(cmd[1])
        cnt[p1] -= 1
        cnt[p2] += 1
    elif cnt[p1] == 0 and cnt[p2] == 1:
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    elif i == n - 1:
        assert cnt[p1] == 1 and cnt[p2] == 1
        res.append(cmd[0])
        cnt[p1] += 1
        cnt[p2] -= 1
    else:
        assert cnt[p1] == 1 and cnt[p2] == 1
        assert i + 1 < n

        nxt = cmds[i + 1]
        p3 = ord(nxt[0]) - ord('A')
        p4 = ord(nxt[1]) - ord('A')

        if p3 in [p1, p2] and cnt[p4] == 0:
            if p3 == p1:
                res.append(cmd[0])
                cnt[p1] += 1
                cnt[p2] -= 1
            else:
                res.append(cmd[1])
                cnt[p1] -= 1
                cnt[p2] += 1
        else:
            res.append(cmd[0])
            cnt[p1] += 1
            cnt[p2] -= 1


if not res:
    print 'No'
else:
    print 'Yes'
    print '\n'.join(res)

Submission status: Runtime Error

Solution code:
import sys
n,a,b,c=map(int,input().split())
slist=[]
for i in range(n):
    slist.append(input())
output=[]
ynflag=True

def abc1(s):
    global a
    global b
    global c
    global output
    global ynflag

    
    if s == "AB":
        if a ==1:
            a -=1
            b +=1
            output.append("B")
            return True
        elif b == 1:
            a +=1
            b -=1
            output.append("A")
            return True
        else:
            return False
    if s == "AC":
        if a ==1:
            a -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            a +=1
            c -=1
            output.append("A")
            return True
        else:
            return False
    if s == "BC":
        if b ==1:
            b -=1
            c +=1
            output.append("C")
            return True
        elif c == 1:
            b +=1
            c -=1
            output.append("B")
            return True
        else:
            return False

def abc2(i):
    global a
    global b
    global c
    global output
    global slist
    s=slist[i]
    if s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if s[i+1] == "BC":
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
        else:
            if a >=b:
                a -=1
                b +=1
                output.append("B")
                return True
            else:
                a +=1
                b -=1
                output.append("A")
                return True
    elif s == "AC":
        if a + c == 0:
            return False
        elif a == 1 and c == 1 and b == 0:
            if s[i+1] == "BC":
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
        else:
            if a >=c:
                a -=1
                c +=1
                output.append("C")
                return True
            else:
                a +=1
                c -=1
                output.append("A")
                return True
    else:
        if b + c == 0:
            return False
        elif b == 1 and c == 1 and a == 0:
            if s[i+1] == "AC":
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True
        else:
            if b >=c:
                b -=1
                c +=1
                output.append("C")
                return True
            else:
                b +=1
                c -=1
                output.append("B")
                return True

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

else:
    if a+b+c == 1:
        for s in slist:
            ynflag=abc1(s)
            if not ynflag:
                print("No")
                sys.exit()
    else:
        for i in range(n):
            ynflag=abc2(i)
            if not ynflag:
                print("No")
                sys.exit()
    print("Yes")
    for out in output:
        print(out)

Submission status: Runtime Error

Solution code:
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B": b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
    elif num_dic[s[0]] < num_dic[s[1]]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    elif i == n -1:
        ans.append(s[0])
    elif s[0] in s[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
print("Yes")
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B": b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        import IPython;IPython.embed()
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[0])
    elif num_dic[s[0]] < num_dic[s[1]]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[1])
    elif i == n -1:
        ans.append(s[0])
    elif s[0] in s[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
print("Yes")
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B": b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[0])
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
    elif i == n -1:
        ans.append(s[0])
    elif s[0] in s[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-

N,A,B,C=input().split()
D=dict()
D["A"]=int(A)
D["B"]=int(B)
D["C"]=int(C)

S=[]
for i in range(int(N)):
    S.append(input())

ans="Yes"
F=[]
for i,s in enumerate(S):
    if D[s[0]]==0 and D[s[1]]==0:
        ans="No"
        break
    if D[s[0]]==0 and D[s[1]]==1:
        F.append(s[0])
    if D[s[0]]==1 and D[s[1]]==0:
        F.append(s[1])
    if D[s[0]]==1 and D[s[1]]==1:
        if D["A"]+D["B"]+D["C"]==2:
            if i!=int(N)-1:
                s0=S[i+1][0]
                s1=S[i+1][1]
                if s[0]==s0 or s[0]==s1:
                    F.append(s[0])
                if s[1]==s0 or s[1]==s1:
                    F.append(s[1])
    if len(F)!=i+1:
        if D[s[0]]<D[s[1]]:
            F.append(s[0])
        if D[s[0]]>D[s[1]]:
            F.append(s[1])
        if D[s[0]]==D[s[1]]:
            F.append(s[0])

    D[F[-1]]+=1
    if s[0]==F[-1]: D[s[1]]+=1
    if s[1]==F[-1]: D[s[0]]+=1
    print(D)
    print(F)

print(ans)
if ans=="Yes":
    for f in F:
        print(f)

Submission status: Runtime Error

Solution code:
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B":b, "C": c}

for i in range(n):
    s = ss[i]
    if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
        print("No")
        exit()
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[0])
    elif num_dic[s[0]] > num_dic[s[1]]:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
    elif i == n -1:
        ans.append(s[0])
    elif s[0] in S[i+1]:
        num_dic[s[0]] += 1
        num_dic[s[1]] -= 1
        ans.append(s[0])
    else:
        num_dic[s[0]] -= 1
        num_dic[s[1]] += 1
        ans.append(s[1])
for a in ans:
    print(a)

Submission status: Runtime Error

Solution code:
n, a, b, c = list(map(int, input().split()))
ss = []
for i in range(n):
    ss.append(input())
ans = []
num_dic = {"A": a,"B":b, "C": c}
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 0:
    print("No")
    exit()
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 1:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
            else:
                ans.append(s[1])
    print("Yes")
    for a in ans:
        print(a)
if num_dic["A"] + num_dic["B"] + num_dic["C"] == 2:
    for i in range(n-1):
        s = ss[i]
        if num_dic[s[0]] + num_dic[s[1]] == 2:
            if num_dic[s[0]] == 1:
                if ss[i+1].find(s[0]) > -1:
                    num_dic[s[0]] = 2
                    num_dic[s[1]] = 0
                    ans.append(s[0])
                else:
                    num_dic[s[0]] = 0
                    num_dic[s[1]] = 2
                    ans.append(s[1])
            else:
                if num_dic[s[0]] == 0:
                    num_dic[s[0]] = 1
                    num_dic[s[1]] = 1
                    ans.append(s[0])
                else:
                    num_dic[s[0]] = 1
                    num_dic[s[1]] = 1
                    ans.append(s[1])
        elif num_dic[s[0]] + num_dic[s[1]] == 1:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = 0
            else:
                ans.append(s[1])
                num_dic[s[0]] = 0
                num_dic[s[1]] = 1
        elif num_dic[s[0]] + num_dic[s[1]] == 0:
            print("No")
            exit()
        else:
            if num_dic[s[0]] == 0:
                ans.append(s[0])
                num_dic[s[0]] = 1
                num_dic[s[1]] = num_dic[s[1]] - 1
            else:
                ans.append(s[1])
                num_dic[s[0]] = num_dic[s[0]] - 1
                num_dic[s[1]] = num_dic[s[1]] + 1
    if num_dic[s[i+1][0]] + num_dic[s[i+1][1]] == 0:
        print("No")
        exit()
    if num_dic[s[i+1][0]] != 0:
        ans.append(s[i+1][0])
    else:
        ans.append(s[i+1][1])
    print("Yes")
    for a in ans:
        print(a)
else:
    for i in range(n):
        s = ss[i]
        if num_dic[s[0]] == 0 and num_dic[s[1]] == 0:
            print("No")
            exit()
        if num_dic[s[0]] == 0:
            ans.append(s[0])
            
            num_dic[s[0]] = 1
            num_dic[s[1]] = num_dic[s[1]] - 1
        elif num_dic[s[0]] == 0:
            ans.append(s[1])
            num_dic[s[0]] = num_dic[s[0]] - 1
            num_dic[s[1]] = 1
        else:
            if num_dic[s[0]] >= num_dic[s[1]]:
                ans.append(s[1])
                num_dic[s[0]] = num_dic[s[0]] - 1
                num_dic[s[1]] = num_dic[s[1]] + 1
            else:
                ans.append(s[0])
                num_dic[s[0]] = num_dic[s[0]] + 1
                num_dic[s[1]] = num_dic[s[1]] - 1
    print("Yes")
    for a in ans:
        print(a)

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
data = {
  "A":A,
  "B":B,
  "C":C
}
select = []
S = list(input() for _ in range(N))
for i in range(N):
  s = S[i]
  if data[s[0]] == data[s[1]] ==0:
    print("No")
    exit(0)
  elif data[s[0]] > data[s[1]]:
    data[s[0]] -= 1
    data[s[1]] += 1
    select.append(s[1])
  elif data[s[0]] < data[s[1]]:
    data[s[0]] += 1
    data[s[1]] -= 1
    select.append(s[0])
  elif i == N - 2:
    select.append(s[0])
  elif s[0] in S[i+1]:
    data[s[0]] += 1
    data[s[1]] -= 1
    select.append(s[0])
  else:
    data[s[0]] -= 1
    data[s[1]] += 1
    select.append(s[1])
print("Yes")
for i in select:
  print(i)

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
data = {
  "A":A,
  "B":B,
  "C":C
}
select = []
S = list(input() for _ in range(N))
for i in range(N):
  s = S[i]
  if data[s[0]] == data[s[1]] ==0:
    print("No")
    exit(0)
  elif data[s[0]] > data[s[1]]:
    data[s[0]] -= 1
    data[s[1]] += 1
    select.append(s[1])
  elif data[s[0]] < data[s[1]]:
    data[s[0]] += 1
    data[s[1]] -= 1
    select.append(s[0])
  else:
    ss = S[i+1]
  if i == N - 2:
    select.append(s[0])
  elif s[0] in ss:
    data[s[0]] += 1
    data[s[1]] -= 1
    select.append(s[0])
  else:
    data[s[0]] -= 1
    data[s[1]] += 1
    select.append(s[1])

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