Problem p03111 (python)

Fixed submission:
from itertools import permutations

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

while(True):
    try:
        n, a, b, c = get_int_list()
        l = [get_int() for _ in range(n)]
        ans = 1000000000000
        for take in permutations(l, n):
            #print(take)
            take = list(take)
            cnt = 0
            i = 3
            while True:
                if i >= n:
                    break
                if abs(take[2] - a) < 10:
                    break
                if take[2] > a:
                    break
                if abs(take[2] + take[i] - a) >= abs(take[2] - a):
                    break
                if take[i] <= 10:
                    break
                take[2] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[2] - a)
            #print(take[2])

            while True:
                if i >= n:
                    break
                if abs(take[1] - b) < 10:
                    break
                if take[1] > b:
                    break
                if abs(take[1] + take[i] - b) >= abs(take[1] - b):
                    break
                if take[i] <= 10:
                    break
                take[1] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[1] - b)
            #print(take[1])

            while True:
                if i >= n:
                    break
                if abs(take[0] - c) < 10:
                    break
                if take[0] > c:
                    break
                if abs(take[0] + take[i] - c) >= abs(take[0] - c):
                    break
                if take[i] <= 10:
                    break
                take[0] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[0] - c)
            #print(take[0])

            ans = min(ans, cnt)
            #if(ans == 253):
            #    print(take[0], take[1], take[2])
        print(ans)
    except EOFError:
        exit()

Buggy submission (verdict: Runtime Error):
import os
import math
from itertools import permutations
import functools as func
INF, MOD = float("inf"), 1e9 + 7
MAX, MIN = -INF, INF
dx1, dy1, dx2, dy2 = [-1, 0, 1, 0], [0, -1, 0, 1], [-1, 0, 1, -1, 1, -1, 0, 1], [-1, -1, -1, 0, 0, 1, 1, 1]

def get_int():
    return int(input())

def get_int_list():
    return list(map(int, input().split()))

while(True):
    try:
        n, a, b, c = get_int_list()
        l = [get_int() for _ in range(n)]
        ans = 1000000000000
        for take in permutations(l, n):
            #print(take)
            take = list(take)
            cnt = 0
            i = 3
            while abs(take[0] - c) > 10 and take[0] < c and abs(take[0] + take[i] - c) < abs(take[0] - c):
                if i >= n:
                    break
                take[0] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[0] - c)
            #print(take[0])

            while abs(take[1] - b) > 10 and take[1] < b and abs(take[1] + take[i] - b) < abs(take[1] - b):
                if i >= n:
                    break
                take[1] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[1] - b)
            #print(take[1])

            while abs(take[2] - a) > 10 and take[2] < a and abs(take[2] + take[i] - a) < abs(take[2] - a):
                if i >= n:
                    break
                take[2] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[2] - a)
            #print(take[2])

            ans = min(ans, cnt)
            #if(ans == 253):
            #    print(take[0], take[1], take[2])
        print(ans)
    except EOFError:
        exit()