Problem:
You have N bamboos. The lengths (in centimeters) of these are l_1, l_2, ..., l_N, respectively.  Your objective is to use some of these bamboos (possibly all) to obtain three bamboos of length A, B, C. For that, you can use the following three kinds of magics any number:    * Extension Magic: Consumes 1 _MP_ (magic point). Choose one bamboo and increase its length by 1.   * Shortening Magic: Consumes 1 MP. Choose one bamboo of length at least 2 and decrease its length by 1.   * Composition Magic: Consumes 10 MP. Choose two bamboos and combine them into one bamboo. The length of this new bamboo is equal to the sum of the lengths of the two bamboos combined. (Afterwards, further magics can be used on this bamboo.)  At least how much MP is needed to achieve the objective?

Incorrect submission (verdict: Wrong Answer):
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):
            take = list(take)
            cnt = 0
            i = 3
            while abs(take[0] - a) > 10:
                if i >= n:
                    break
                take[0] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[0] - a)

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

            while abs(take[2] - c) > 10:
                if i >= n:
                    break
                take[2] += take[i]
                i += 1
                cnt += 10
            cnt += abs(take[2] - c)

            ans = min(ans, cnt)
        print(ans)
    except EOFError:
        exit()


Fixed, accepted 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)
            
            #    print(take[0], take[1], take[2])
        print(ans)
    except EOFError:
        exit()