Problem p02692 (python)

Fixed submission:
# coding: utf-8
import sys
sysread = sys.stdin.readline
sys.setrecursionlimit(10**7)
def run():
    pri = []
    N, A, B, C = map(int, sysread().split())
    vals = {'A':A, 'B':B, 'C':C}
    S = [sysread() for _ in range(N)]

    pri = []
    for idx, s in enumerate(S):
        if vals[s[0]] == vals[s[1]] and vals[s[0]] == 1 and idx !=len(S)-1:
            if s[0] in S[idx+1]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])
        else:
            if vals[s[0]] <= vals[s[1]]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])

        if not check_vals(vals):
            print('No')
            return None
    print('Yes')
    for st in pri:
        print(st)

def check_vals(vals):
    for v in vals.values():
        if v < 0:
            return False
    return True

if __name__ == "__main__":
    run()

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
from itertools import combinations
def run():
    pri = []
    N, A, B, C = map(int, sysread().split())
    vals = {'A':A, 'B':B, 'C':C}
    S = [sysread() for _ in range(N)]

    pri = []
    for idx, s in enumerate(S):
        if vals[s[0]] == vals[s[1]] and vals[s[0]] == 1:
            if s[0] in S[idx+1]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])
        else:
            if vals[s[0]] <= vals[s[1]]:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[0]] -= 1
                vals[s[1]] += 1
                pri.append(s[1])

        if not check_vals(vals):
            print('No')
            return None
    print('Yes')
    for st in pri:
        print(st)

def check_vals(vals):
    for v in vals.values():
        if v < 0:
            return False
    return True

if __name__ == "__main__":
    run()

Buggy submission (verdict: Runtime Error):
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.readline
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
from itertools import combinations
def run():
    pri = []
    N, A, B, C = map(int, sysread().split())
    vals = {'A':A, 'B':B, 'C':C}
    S = [sysread() for _ in range(N)]
    if A + B + C == 0:
        print('No')
        return None
    elif A + B + C == 1:
        if set([A,B,C]) != set([-1,1,1]):
            print('No')
            return None
        if -1 in (vals[S[0][0]], vals[S[0][1]]):
            if vals[S[0][0]] == -1:
                vals[S[0][0]] += 1
                vals[S[0][1]] -= 1
                pri.append(S[0][0])
            else:
                vals[S[0][1]] += 1
                vals[S[0][0]] -= 1
                print(S[0][1])
        else:
            print('No')
            return None
        for s in S[1:]:
            if vals[s[0]] == 0 and vals[s[1]] == 1:
                pri.append(s[0])
                vals[s[0]] += 1
                vals[s[1]] -= 1
            elif vals[s[0]] == 1 and vals[s[1]] == 0:
                pri.append(s[1])
                vals[s[1]] += 1
                vals[s[0]] -= 1
            else:
                print('No')
                return None
    else:
        tmp = S[0]
        done1 = False
        vals[tmp[0]] += 1
        vals[tmp[1]] -= 1
        if check_vals(vals):
            done1 = True
            pri.append(tmp[0])
        else:
            vals[tmp[0]] -= 1
            vals[tmp[1]] += 1

        if not done1:
            vals[tmp[1]] += 1
            vals[tmp[0]] -= 1
            if check_vals(vals):
                done1 = True
                pri.append(tmp[1])
        if not done1:
            print('No')
            return None

        tmp = S[1]
        done2 = False
        vals[tmp[0]] += 1
        vals[tmp[1]] -= 1
        if check_vals(vals):
            done2 = True
            pri.append(tmp[0])
        else:
            vals[tmp[0]] -= 1
            vals[tmp[1]] += 1

        if not done2:
            vals[tmp[1]] += 1
            vals[tmp[0]] -= 1
            if check_vals(vals):
                done2 = True
                pri.append(tmp[1])

        if not done2:
            print('No')
            return None


        for s in S[2:]:
            if vals[s[0]] == 0:
                vals[s[0]] += 1
                vals[s[1]] -= 1
                pri.append(s[0])
            else:
                vals[s[1]] += 1
                vals[s[0]] -= 1
                pri.append(s[1])

    print('Yes')
    for sss in pri:
        print(sss)

def check_vals(vals):
    cnt = 0
    over = 0
    for v in vals.values():
        if v > 1:
            over += 1
        elif v == 0:
            cnt += 1
        elif v < 0:
            return False
    if cnt > 1:
        if over == 0:
            return False
        else:
            return True
    else:
        return True

if __name__ == "__main__":
    run()