Problem p03276 (python)

Fixed submission:
str = raw_input()
str1 = raw_input()
array = str.split()
array = map(int, array)
N = array[0]
k = array[1]
array = str1.split()
array = map(int, array)
left_index = 0
right_index = 0
if array[0]*array[-1]>=0:
    if array[0] >= 0:
        answer = array[k-1]
    else:
        answer = abs(array[-k])
else:
    result = []
    x = min([abs(i) for i in array])
    if x == 0:
        index = array.index(x)
        if index < k - 1:
            left_index = 0
        else:
            left_index = index - k + 2
            result.append(abs(array[index-k+1]))
        if len(array) - 1 - index < k - 1:
            right_index = len(array)
        else:
            right_index = index + k - 1
            result.append(array[index+k-1])

    else:
        if x in array:
            index = array.index(x)
            middle_left_index = index - 1
            middle_right_index = index
        else:
            index = array.index(-x)
            middle_left_index = index
            middle_right_index = index + 1
        if middle_left_index < k - 1:
            left_index = 0
        else:
            left_index = middle_left_index - k + 2
            result.append(abs(array[middle_left_index - k + 1]))
        if len(array) - 1 - middle_right_index < k - 1:
            right_index = len(array)
        else:
            right_index = middle_right_index + k - 1
            result.append(array[middle_right_index+k-1])
    new_array = array[left_index:right_index]
    if len(new_array) >= k:
        min_route = max(abs(new_array[0]), abs(new_array[-1]))**2
        for i in range(len(new_array)):
            if i+k-1 <= len(new_array) - 1:
                left_abs = abs(new_array[i])
                right_abs = abs(new_array[i+k-1])
                current_min_route = min(left_abs, right_abs) + left_abs + right_abs
                if min_route > current_min_route:
                    min_route = current_min_route
        result.append(min_route)
    answer = min(result)
print answer

Buggy submission (verdict: Runtime Error):
N = input()
k = input()
array = []
for i in range(N):
    x = input()
    array.append(x)
left_index = 0
right_index = 0
if array[0]*array[-1]>=0:
    if array[0] >= 0:
        answer = array[k-1]
    else:
        answer = abs(array[-k])
else:
    result = []
    x = min([abs(i) for i in array])
    if x == 0:
        index = array.index(x)
        if index < k - 1:
            left_index = 0
        else:
            left_index = index - k + 2
            result.append(abs(array[index-k+1]))
        if len(array) - 1 - index < k - 1:
            right_index = len(array)
        else:
            right_index = index + k - 1
            result.append(array[index+k-1])

    else:
        if x in array:
            index = array.index(x)
            middle_left_index = index - 1
            middle_right_index = index
        else:
            index = array.index(-x)
            middle_left_index = index
            middle_right_index = index + 1
        if middle_left_index < k - 1:
            left_index = 0
        else:
            left_index = middle_left_index - k + 2
            result.append(abs(array[middle_left_index - k + 1]))
        if len(array) - 1 - middle_right_index < k - 1:
            right_index = len(array)
        else:
            right_index = middle_right_index + k - 1
            result.append(array[middle_right_index+k-1])
    new_array = array[left_index:right_index]
    if len(new_array) >= k:
        min_route = max(abs(new_array[0]), abs(new_array[-1]))**2
        for i in range(len(new_array)):
            if i+k-1 <= len(new_array) - 1:
                left_abs = abs(new_array[i])
                right_abs = abs(new_array[i+k-1])
                current_min_route = min(left_abs, right_abs) + left_abs + right_abs
                if min_route > current_min_route:
                    min_route = current_min_route
        result.append(min_route)
    answer = min(result)
print answer

Buggy submission (verdict: Runtime Error):
import time
N = input()
k = input()
array = []
for i in range(N):
    x = input()
    array.append(x)

begin = time.time()
print begin
left_index = 0
right_index = 0
if array[0]*array[-1]>=0:
    if array[0] >= 0:
        answer = array[k-1]
    else:
        answer = abs(array[-k])
else:
    result = []
    x = min([abs(i) for i in array])
    if x == 0:
        index = array.index(x)
        if index < k - 1:
            left_index = 0
        else:
            left_index = index - k + 2
            result.append(abs(array[index-k+1]))
        if len(array) - 1 - index < k - 1:
            right_index = len(array)
        else:
            right_index = index + k - 1
            result.append(array[index+k-1])

    else:
        if x in array:
            index = array.index(x)
            middle_left_index = index - 1
            middle_right_index = index
        else:
            index = array.index(-x)
            middle_left_index = index
            middle_right_index = index + 1
        if middle_left_index < k - 1:
            left_index = 0
        else:
            left_index = middle_left_index - k + 2
            result.append(abs(array[middle_left_index - k + 1]))
        if len(array) - 1 - middle_right_index < k - 1:
            right_index = len(array)
        else:
            right_index = middle_right_index + k - 1
            result.append(array[middle_right_index+k-1])
    new_array = array[left_index:right_index]
    if len(new_array) >= k:
        min_route = max(abs(new_array[0]), abs(new_array[-1]))**2
        for i in range(len(new_array)):
            if i+k-1 <= len(new_array) - 1:
                left_abs = abs(new_array[i])
                right_abs = abs(new_array[i+k-1])
                current_min_route = min(left_abs, right_abs) + left_abs + right_abs
                print left_abs, right_abs
                if min_route > current_min_route:
                    min_route = current_min_route
        result.append(min_route)
    print result
    answer = min(result)

print answer
end = time.time()
print end
print "Processing time is: %f seconds" % (end-begin)