Buggy python submission (verdict: Time Limit Exceeded):
import time

N, A, B, C = map(int, input().split())

l = [int(input()) for i in range(N)]

INF = 10 ** 8

stack = []
depth, a, b, c, mp = 0, 0, 0, 0, 0
stack.append([depth, a, b, c, mp])
flg = []

MP = INF

ct = 0
while stack:
    x = stack.pop(-1)
    if x[0] == N:
        a, b, c = x[1], x[2], x[3]
        mp = x[4] + abs(a-A) + abs(b-B) + abs(c-C) - 30
        if min(a, b, c) > 0 and mp < MP:
            MP = mp
    elif x[0] > N:
        pass
    else:
        tmp = tuple(x)
        for i in range(4):
            if i == 0:
                x[0] += 1
            else:
                x[0] += 1
                x[i] += l[x[0]-1]
                x[4] += 10
            if x not in flg:
                stack.append(x)
                flg.append(tuple(x))
            x = list(tmp)
print(MP)


Fixed submission:
N, A, B, C = map(int, input().split())

l = [int(input()) for i in range(N)]


def convert(n):
    list_q = []
    while True:
        a = n % 4
        n //= 4
        list_q.insert(0, a)
        if n == 0:
            break
    list_q = [str(i) for i in list_q]
    return list_q


MP = -1
for i in range(21, 4**N):
    mp_a = 0
    mp_b = 0
    mp_c = 0
    use = [int(j) for j in ''.join(convert(i)).zfill(N)]
    a_n = use.count(1)
    b_n = use.count(2)
    c_n = use.count(3)
    if a_n == 0 or b_n == 0 or c_n == 0:
        pass
    else:
        mp_a += 10 * (a_n - 1)
        mp_b += 10 * (b_n - 1)
        mp_c += 10 * (c_n - 1)
        b_a = 0
        b_b = 0
        b_c = 0
        idx = 0
        for i in use:
            if i == 1:
                b_a += l[idx]
            elif i == 2:
                b_b += l[idx]
            elif i == 3:
                b_c += l[idx]
            idx += 1
        mp_a += abs(A-b_a)
        mp_b += abs(B-b_b)
        mp_c += abs(C-b_c)
        mp = mp_a + mp_b + mp_c
        if MP == -1 or mp < MP:
            MP = mp
print(MP)

Buggy python submission (verdict: Time Limit Exceeded):
N, A, B, C = map(int, input().split())

l = [int(input()) for i in range(N)]

stack = []
stack.append((0, (0, 0, 0), 0))
flg = []
mp = []

while stack:
    x = stack.pop(-1)
    if x[0] == N:
        a = x[1][0]
        b = x[1][1]
        c = x[1][2]
        if min(a, b, c) > 0:
            sum_mp = x[2] + abs(a-A)+abs(b-B)+abs(c-C)
            mp.append(sum_mp)
    else:
        depth = x[0]
        bamboo = x[1]
        MP = x[2]
        for i in range(4):
            bb = list(bamboo)
            if i < 3:
                if bb[i] != 0 and bb[i] < [A, B, C][i]:
                    mm = MP + 10
                    bb[i] += l[depth]
                    if (depth+1, bb, mm) not in flg:
                        stack.append((depth+1, bb, mm))
                        flg.append((depth+1, bb, mm))
                elif bb[i] == 0:
                    mm = MP
                    bb[i] += l[depth]
                    if (depth+1, bb, mm) not in flg:
                        stack.append((depth+1, bb, mm))
                        flg.append((depth+1, bb, mm))
            else:
                mm = MP
                if (depth+1, bb, mm) not in flg:
                    stack.append((depth+1, bb, mm))
                    flg.append((depth+1, bb, mm))

print(min(mp))


Fixed submission:
N, A, B, C = map(int, input().split())

l = [int(input()) for i in range(N)]


def convert(n):
    list_q = []
    while True:
        a = n % 4
        n //= 4
        list_q.insert(0, a)
        if n == 0:
            break
    list_q = [str(i) for i in list_q]
    return list_q


MP = -1
for i in range(21, 4**N):
    mp_a = 0
    mp_b = 0
    mp_c = 0
    use = [int(j) for j in ''.join(convert(i)).zfill(N)]
    a_n = use.count(1)
    b_n = use.count(2)
    c_n = use.count(3)
    if a_n == 0 or b_n == 0 or c_n == 0:
        pass
    else:
        mp_a += 10 * (a_n - 1)
        mp_b += 10 * (b_n - 1)
        mp_c += 10 * (c_n - 1)
        b_a = 0
        b_b = 0
        b_c = 0
        idx = 0
        for i in use:
            if i == 1:
                b_a += l[idx]
            elif i == 2:
                b_b += l[idx]
            elif i == 3:
                b_c += l[idx]
            idx += 1
        mp_a += abs(A-b_a)
        mp_b += abs(B-b_b)
        mp_c += abs(C-b_c)
        mp = mp_a + mp_b + mp_c
        if MP == -1 or mp < MP:
            MP = mp
print(MP)