Problem p03675 (python)

Fixed submission:
import sys
from collections import deque

from math import *
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    n = int(input_raw[0])
    input_raw = list(read_func().strip().split())
    b_list = deque([])
    is_last_first = False
    for i in range(n):
        is_last_first = not(is_last_first)
        if is_last_first == True:
            b_list.append((input_raw[i]))
        else:
            b_list.appendleft(input_raw[i])

    if is_last_first == True:
        b_list.reverse()
    print " ".join(b_list)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque

from math import *
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n, m] = [int(input_raw[0]), int(input_raw[1])]
    input_raw = list(read_func().strip().split())
    min_cost = sys.maxint
    for i in range(m):
        x = i + 1
        cost = 0
        for j in range(n - 1):
            now = int(input_raw[j])
            target = int(input_raw[j + 1])
            if now < target:
                if x < target:
                    cost += min( (target - now),  1 +(target - x))
                elif x > target:
                    cost += min( (target - now), 1 + (m + target - x))
                else:
                    cost += 1
            elif now > target:
                if x < target:
                    cost += min((m + target - now),  1 +(target - x))
                elif x > target:
                    cost += min((m + target - now), 1 + (m + target - x))
                else:
                    cost += 1
        #print cost
        if cost < min_cost:
            min_cost = cost

    print min_cost


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys
from collections import deque

from math import *
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [n, m] = [int(input_raw[0]), int(input_raw[1])]
    input_raw = list(read_func().strip().split())
    min_cost = sys.maxint
    for i in range(m):
        x = i + 1
        cost = 0
        for j in range(n - 1):
            now = int(input_raw[j])
            target = int(input_raw[j + 1])
            if now < target:
                if x < target:
                    cost += min( (target - now),  1 +(target - x))
                elif x > target:
                    cost += min( (target - now), 1 + (m + target - x))
                else:
                    cost += 1
            elif now > target:
                if x < target:
                    cost += min((m + target - now),  1 +(target - x))
                elif x > target:
                    cost += min((m + target - now), 1 + (m + target - x))
                else:
                    cost += 1
        #print cost
        if cost < min_cost:
            min_cost = cost

    print min_cost


if __name__ == '__main__':
    main()