Problem p02692 (python)

Fixed submission:
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:
        tsugi="DUMMY"
    else:
        tsugi=slist[i+1]
    
    if s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if tsugi == "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 tsugi == "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 tsugi == "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)

Buggy submission (verdict: Runtime Error):
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:
        tsugi="DUMMY"
    else:
        tsugi=slist[i+1]
    
    elif s == "AB":
        if a + b == 0:
            return False
        elif a == 1 and b == 1 and c == 0:
            if tsugi == "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 tsugi == "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 tsugi == "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)

Buggy submission (verdict: Runtime Error):
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
            else:
                output.append("A")
                return True
        elif s =="AC":
            if a > c:
                output.append("C")
                return True
            else:
                output.append("A")
                return True
        else:
            if b > c:
                output.append("C")
                return True
            else:
                output.append("B")
                return True
    
    elif 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)

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
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)

Buggy submission (verdict: Runtime Error):
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)