Problem p02727 (python)

Fixed submission:
def merge(l):
    t = [-1]*len(l)
    if(len(l) == 2):
        if(l[0]<l[1]):
            t[1] = l[0]
            t[0] = l[1]
        else:
            t=l
    elif(len(l) > 2):
        x = merge(l[:len(l)//2])
        y = merge(l[len(l)//2:])
        p,q = 0,0
        for i in range(len(l)):
            if(p<len(x) and q<len(y)):
                if(x[p] > y[q]):
                    t[i] = x[p]
                    p += 1
                else:
                    t[i] = y[q]
                    q += 1
            elif(p == len(x)):
                t[i] = y[q]
                q += 1
            else:
                t[i] = x[p]
                p += 1
    else:
        t = l
    return t
x,y,a,b,c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
a = [x,y,0]
P = merge(p)
Q = merge(q)
R = merge(r)
X = P[:x]
Y = Q[:y]
for i in range(x+y):
    if(a[2] == c):
        break
    elif(a[0] == 0):
        if(Y[a[1]-1] < R[a[2]]):
                a[1] -= 1
                a[2] += 1
        else:
            break
    elif(a[1] == 0):
        if(X[a[0]-1] < R[a[2]]):
                a[0] -= 1
                a[2] += 1
        else:
            break
    else:
        if(X[a[0]-1] > Y[a[1]-1]):
            if(Y[a[1]-1] < R[a[2]]):
                a[1] -= 1
                a[2] += 1
            else:
                break
        else:
            if(X[a[0]-1] < R[a[2]]):
                a[0] -= 1
                a[2] += 1
            else:
                break
m = 0
for i in range(a[0]):
    m += P[i]
for i in range(a[1]):
    m += Q[i]
for i in range(a[2]):
    m += R[i]
print(m)

Buggy submission (verdict: Runtime Error):
def merge(l):
    t = [-1]*len(l)
    if(len(l) == 2):
        if(l[0]<l[1]):
            t[1] = l[0]
            t[0] = l[1]
        else:
            t=l
    elif(len(l) > 2):
        x = merge(l[:len(l)//2])
        y = merge(l[len(l)//2:])
        p,q = 0,0
        for i in range(len(l)):
            if(p<len(x) and q<len(y)):
                if(x[p] > y[q]):
                    t[i] = x[p]
                    p += 1
                else:
                    t[i] = y[q]
                    q += 1
            elif(p == len(x)):
                t[i] = y[q]
                q += 1
            else:
                t[i] = x[p]
                p += 1
    else:
        t = l
    return t
x,y,a,b,c = map(int, input().split())
p = list(map(int, input().split()))
q = list(map(int, input().split()))
r = list(map(int, input().split()))
a = [x,y,0]
P = merge(p)
Q = merge(q)
R = merge(r)
X = P[:x]
Y = Q[:y]
for i in range(x+y):
    if(a[2] == c):
        break
    elif(a[0] == 0):
        if(Y[a[1]-1] < R[a[2]]):
                a[1] -= 1
                a[2] += 1
            else:
                break
    elif(a[1] == 0):
        if(X[a[0]-1] < R[a[2]]):
                a[0] -= 1
                a[2] += 1
            else:
                break
    else:
        if(X[a[0]-1] > Y[a[1]-1]):
            if(Y[a[1]-1] < R[a[2]]):
                a[1] -= 1
                a[2] += 1
            else:
                break
        else:
            if(X[a[0]-1] < R[a[2]]):
                a[0] -= 1
                a[2] += 1
            else:
                break
m = 0
for i in range(a[0]):
    m += P[i]
for i in range(a[1]):
    m += Q[i]
for i in range(a[2]):
    m += R[i]
print(m)