Problem:


Score : 300 points

Problem Statement

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?

Constraints

3 \leq N \leq 8

1 \leq C < B < A \leq 1000

1 \leq l_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B C
l_1
l_2
:
l_N

Output

Print the minimum amount of MP needed to achieve the objective.

Sample Input 1

5 100 90 80
98
40
30
21
80

Sample Output 1

23

We are obtaining three bamboos of lengths 100, 90, 80 from five bamboos 98, 40, 30, 21, 80. We already have a bamboo of length 80, and we can obtain bamboos of lengths 100, 90 by using the magics as follows at the total cost of 23 MP, which is optimal.

Use Extension Magic twice on the bamboo of length 98 to obtain a bamboo of length 100. (MP consumed: 2)

Use Composition Magic on the bamboos of lengths 40, 30 to obtain a bamboo of length 70. (MP consumed: 10)

Use Shortening Magic once on the bamboo of length 21 to obtain a bamboo of length 20. (MP consumed: 1)

Use Composition Magic on the bamboo of length 70 obtained in step 2 and the bamboo of length 20 obtained in step 3 to obtain a bamboo of length 90. (MP consumed: 10)

Sample Input 2

8 100 90 80
100
100
90
90
90
80
80
80

Sample Output 2

0

If we already have all bamboos of the desired lengths, the amount of MP needed is 0. As seen here, we do not necessarily need to use all the bamboos.

Sample Input 3

8 1000 800 100
300
333
400
444
500
555
600
666

Sample Output 3

243



Sample input:
5 100 90 80
98
40
30
21
80


Sample output:
23

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>

using namespace std;

/*  テンプレここから */
typedef long long ll;

#define rep(i, n) for(int i = 0; i < n; i++)
#define repi(i, start, end) for(int i = start; i < end; i++)
#define all(x) (x).begin(), (x).end()
#define INF (1e9)
#define add_mod(a, b, x) ( (ll)(a) + (ll)(b) ) % (ll)(x)
#define mul_mod(a, b, x) ( ((ll)(a) % (ll)(x)) * ((ll)(b) % (ll)(x)) ) % (ll)(x)
#define out(x) cout << (x) << endl;
#define outyes(bool_val) cout << (bool_val ? "yes" : "no") << endl;
#define outYes(bool_val) cout << (bool_val ? "Yes" : "No") << endl;
#define outYES(bool_val) cout << (bool_val ? "YES" : "NO") << endl;
#define outPossible(bool_val) cout << (bool_val ? "Possible" : "Impossible") << endl;
#define outPOSSIBLE(bool_val) cout << (bool_val ? "POSSIBLE" : "IMPOSSIBLE") << endl;

ll sub_mod(ll a, ll b, ll x){
    ll tmp = (a - b) % x;
    if(tmp < 0) tmp += x;
    return tmp;
}
ll gcd(ll a, ll b){
    if(b == 0) return a;
    else return gcd(b, a % b);
}

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
/*  テンプレここまで */

int search(int n, vector<int> nums, vector<int> bamboo){
    int ans = 0;

    rep(num_i, nums.size()){
        int nor_min_idx = 0;
        int nor_min_val = INF;
        rep(i, n){
            int tmp = abs(nums[num_i] - bamboo[i]);
            if(tmp < nor_min_val){
                nor_min_val = tmp;
                nor_min_idx = i;
            }
        }

        int plus_min_val = INF, plus_min_place = 0, plus_min_bit = 0;
        repi(bin, 1, 1 << n){
            int sum = 0;
            int cp_bin = bin;
            int bit_cnt = 0;
            rep(j, n){
                if(cp_bin & 1 == 1){
                    sum += bamboo[j];
                    ++bit_cnt;
                }
                cp_bin = cp_bin >> 1;
            }
            if(plus_min_val > abs(nums[num_i] - sum) + (plus_min_bit-1) * 10){
                plus_min_val = abs(nums[num_i] - sum);
                plus_min_place = bin;
                plus_min_bit = bit_cnt;
            }
        }

        if(nor_min_val < plus_min_val + (plus_min_bit-1) * 10){
            ans += nor_min_val;
            bamboo[nor_min_idx] = -INF;
        }else{
            ans += plus_min_val + (plus_min_bit-1) * 10;
            rep(j, n){
                if(plus_min_place & 1 == 1) bamboo[j] = -INF;
                plus_min_place = plus_min_place >> 1;
            }
        }
    }

    return ans;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    vector<int> nums(3);
    cin >> n;
    rep(i, 3) cin >> nums[i];

    vector<int> bamboo(n*4);
    rep(i, n){
        cin >> bamboo[i];
        rep(j, nums.size()){
            if(nums[j] == bamboo[i]){
                bamboo.erase(bamboo.begin() + i);
                nums.erase(nums.begin() + j);
            }
        }
    }

    int ans = INF;

    ans = min(ans, search(n, nums, bamboo));
    sort(all(nums));
    ans = min(ans, search(n, nums, bamboo));

    cout << ans << endl;
}

Submission status: Runtime Error

Solution code:
#! /usr/bin/python3

# ABC119 c
import sys

def debug(fmt, *args):
    #print(fmt, *args, file=sys.stderr)
    pass

def minabs(t, ls):
    f
    minc = 10000
    minl = None
    for l in ls:
        m = l - t
        if m < minc:
            minc = m
            minl = l
    return minc, t, minl

def f(t, ls):
    for i, li in enumerate(ls):
        for j, lj in enumerate(ls):
            if j >= i:
                continue
            yield abs(li + lj - t), li, lj

def solve(t, ls):
    def k(x):
        return abs(x)
    cs = sorted((l - t for l in ls), key=k)

    if abs(cs[0]) < 10:
        return cs[0], t, [cs[0]+t]
    if cs[0] > 0:
        return cs[0], t, [cs[0]+t]

    minc, li, lj = min(f(t, ls))
    return minc+10, t, [li, lj]


def main(N, A, B, C, ls):
    """
    A:
    B:
    C:
    """
    count = 0
    #debug(A, B, C) 
    #debug(ls) 
    targets = [A, B, C]

    while targets[:]:
        debug("============================")
        debug("targets", targets) 
        debug("objects", ls) 
        debug("count", count)

        # solve 
        minc, mint, minl = sorted(solve(t, ls) for t in targets)[0]
        if minc < 10:
            count += abs(minc)
            targets.remove(mint)
            debug("solve", mint, "cost", minc, minl)
            for l in minl:
                ls.remove(l)
            continue

        if minc > 0:
            count += minc
            targets.remove(mint)
            debug("solve", mint, "cost", minc, minl)
            for l in minl:
                ls.remove(l)
            continue

        count += 10
        debug("combine", minl)
        ls.remove(minl[0])
        ls.remove(minl[1])
        ls.append(minl[0]+minl[1])
        debug("objects", ls) 

    print(count)


if __name__ == "__main__":
    N, A, B, C = [int(x) for x in input().split()]
    ls = [int(input()) for _ in range(N)]
    main(N, A, B, C, ls)

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().split())
L = [int(input()) for _ in range(n)]
mp = 0
def use(n, d):
  bit = [[0]*n for i in range(2**n)]
  for i in range(2**n):
    for j in range(n):
      if i&(2**j) > 0:
        bit[i][j] = 1
  cl = []
  for i in range(2**n):
    s = 0
    for j in range(n):
      s += L[j] * bit[i][j]
    cl.append(s)
  cmin = 999999
  for i in range(1, 2**n):
    if abs(cl[i] - d) < cmin:
      cmin = abs(cl[i] - d)
      cindex = i
  ng = []
  use = []
  for j in range(n):
    if bit[cindex][j]:
      use.append(L[j])
      if L[j] < 10:
        ng.append(L[j])
  for x in use:
    L.remove(x)
  if sum(bit[cindex]):
     return cmin
  else:
    return sum(bit[cindex]) * 10 - 10 + cmin - 10 * len(ng) + sum(ng)
mp += use(n, c)
n = len(L)
mp += use(n, b)
n = len(L)
mp += use(n, a)
print(mp)

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np
import copy


ansl = []
N, A, B, C = list(map(int, input().split()))

a = []
for i in range(N):
    a.append(int(input()))

def get_ptn(la):
    ptn = []
    for i in range(1, len(la) + 1):
        ptn.append(list(itertools.combinations(la, i)))
    return ptn

takes = list(itertools.permutations([A, B, C]))


for take in takes:
    # print(take)
    # print(a)
    ta = copy.deepcopy(a)
    ans = 0
    for k in range(3):

        # print(a)
        ptn = get_ptn(ta)

        # print(ptn)
        cost = []
        tmp = []

        for i in range(len(ptn)):
            tmp.append([abs(take[k] - sum(j)) for j in ptn[i]])
            cost.append(min(tmp[i]) + 10 * i)
        # print(cost)
        ans += min(cost)
        ac = np.argmin(cost)
        # print(np.argmin(cost))
        # print(ptn[ac])
        # print(tmp[ac])
        at = np.argmin(tmp[ac])
        # print(ptn[ac][at])

        for _ in ptn[ac][at]:
            ta.remove(_)

    ansl.append(ans)

print(min(ansl))

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().split())
L = [int(input()) for _ in range(n)]
mp = 0
def use(n, d):
  bit = [[0]*n for i in range(2**n)]
  for i in range(2**n):
    for j in range(n):
      if i&(2**j) > 0:
        bit[i][j] = 1
  cl = []
  for i in range(2**n):
    s = 0
    for j in range(n):
      s += L[j] * bit[i][j]
    cl.append(s)
  cmin = 999999
  for i in range(1, 2**n):
    if abs(cl[i] - d) < cmin:
      cmin = abs(cl[i] - d)
      cindex = i
  ng = []
  use = []
  for j in range(n):
    if bit[cindex][j]:
      use.append(L[j])
      if L[j] < 10:
        ng.append(L[j])
  for x in use:
    L.remove(x)
  if bit[cindex]:
     return cmin
  else:
    return sum(bit[cindex]) * 10 - 10 + cmin - 10 * len(ng) + sum(ng)
mp += use(n, c)
n = len(L)
mp += use(n, b)
n = len(L)
mp += use(n, a)
print(mp)

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().split())
L = [int(input()) for _ in range(n)]
mp = 0
def use(n, d):
  bit = [[0]*n for i in range(2**n)]
  for i in range(2**n):
    for j in range(n):
      if i&(2**j) > 0:
        bit[i][j] = 1
  cl = []
  for i in range(2**n):
    s = 0
    for j in range(n):
      s += L[j] * bit[i][j]
    cl.append(s)
  cmin = 999999
  for i in range(1, 2**n):
    if abs(cl[i] - d) < cmin:
      cmin = abs(cl[i] - d)
      cindex = i
  ng = []
  use = []
  for j in range(n):
    if bit[cindex][j]:
      use.append(L[j])
      if L[j] < 10:
        ng.append(L[j])
  for x in use:
    L.remove(x)
  return sum(bit[cindex]) * 10 - 10 + cmin - 10 * len(ng) + sum(ng)
mp += use(n, c)
n = len(L)
mp += use(n, b)
n = len(L)
mp += use(n, a)
print(mp)

Submission status: Runtime Error

Solution code:
from itertools import*
n,a,b,c,*l=map(int,open(0).read().split())
want=[a,b,c]
ans=0
#print(n,want,l)
for s in range(3):
  if want[s] in l:
    l.remove(want[s])
    want[s]=0
#print(want,l)
want=list(filter(lambda x:x!=0,want))
for s in range(3):
  for t in l:
    if abs(want[s]-t)<10:
      ans+=abs(want[s]-t)
      l.remove(t)
      want[s]=0
want=list(filter(lambda x:x!=0,want))
#print(want,l,ans)
x=[]
y=[]
z=[]
for s in range(1,len(l)+1):
  x.extend(list(combinations(l,s)))
#print(x)
for s in want:
  for t in range(len(x)):
    y.append(abs(s-sum(x[t]))+(len(x[t])-1)*10)
    z.append(x[t])
  ans+=min(y)
#  print(x[y.count(min(y))])
  x[y.count(min(y))]=(999999,9999999)
print(ans)

Submission status: Runtime Error

Solution code:
import itertools

N,A,B,C=map(int,input().split())
L=[int(input()) for i in range(N)]

def score(x,NOW):
    L=len(NOW)
    ITE=list(itertools.product([0,1],repeat=L))

    ANS=10**15
    CANDI=[]

    for ite in ITE:
        if max(ite)==0:
            continue
        SUM=sum([NOW[i] for i in range(L) if ite[i]==1])        
        MAGIC=(ite.count(1)-1)*10
        

        if ANS>MAGIC+abs(x-SUM):
            ANS=MAGIC+abs(x-SUM)
            CANDI=ite

    return ANS,[NOW[i] for i in range(L) if CANDI[i]==0]

X=[[A,B,C],[A,C,B],[B,A,C],[B,C,A],[C,A,B],[C,B,A]]
ANS=10**15

for x in X:
    ANS1,L1=score(x[0],L)
    ANS2,L2=score(x[1],L1)
    ANS3,L3=score(x[2],L2)

    #print(ANS,ANS1,ANS2,ANS3,L1,L2,L3,A,B,C)

    if ANS>ANS1+ANS2+ANS3:
        ANS=ANS1+ANS2+ANS3

print(ANS)

Submission status: Runtime Error

Solution code:
import itertools
N,A,B,C=map(int,input().split())
l=list()
for i in range(N):
    l.append(int(input()))


def solve(x,ans):
    global N,A,B,C,l
    c=[10000,-1]
    for i in  range(0,len(l)):
        if(abs(l[i]-x)<=10 and abs(l[i]-x)<=c[0]):
            c[0]=abs(l[i]-x)
            c[1]=i
    
    if(c[1]!=-1):
        tmp=l[c[1]]
        l.pop(c[1])
        return(ans+abs(tmp-x))
    else:
        mins=100000
        minstmp=[0,0]
        for i in range(0,len(l)):
            if(mins<abs(x-l[i])):
                pass
            else:
                minstmp=[i,i]
                mins=abs(x-l[i])-10
        check=False
    
        for i in itertools.combinations(l,2):
            tmp=i[0]+i[1]
            if(abs(x-tmp)<mins):
                mins=abs(x-tmp)
                minstmp=[i[0],i[1]]
                check=True
        if(check):
            ans+=10
            l.append(minstmp[0]+minstmp[1])
            l.remove(minstmp[0])
            l.remove(minstmp[1])
            return solve(x,ans)
        else:
            l.pop(minstmp[0])
            return(ans+mins+10)



totalAns=1000000000
import copy
L=copy.deepcopy(l)

ans=0
ans+=solve(A,0)
ans+=solve(B,0)
ans+=solve(C,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(A,0)
ans+=solve(C,0)
ans+=solve(B,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(B,0)
ans+=solve(A,0)
ans+=solve(C,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(B,0)
ans+=solve(C,0)
ans+=solve(A,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(C,0)
ans+=solve(A,0)
ans+=solve(B,0)
if(ans<totalAns):
    totalAns=ans

l=copy.deepcopy(L)
ans=0
ans+=solve(C,0)
ans+=solve(B,0)
ans+=solve(A,0)
if(ans<totalAns):
    totalAns=ans

print(totalAns)

Submission status: Runtime Error

Solution code:
8 1000 800 100
300
333
400
444
500
555
600
666

Submission status: Runtime Error

Solution code:
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()

Submission status: Runtime Error

Solution code:
import itertools
from tqdm import tqdm
N,A,B,C=[int(i) for i in input().split(" ")]
bamboo=[]
bamboo=sorted(bamboo,reverse=True)
for i in range(N):
    bamboo.append(int(input()))

def costWithMagic1and2(bamboo,A,B,C):
    retval=10**10
    for i in itertools.combinations([j for j in range(len(bamboo))],3):
        a,b,c=bamboo[i[0]],bamboo[i[1]],bamboo[i[2]]
        retval=min(retval,(abs(A-a)+abs(B-b)+abs(C-c)))
    return retval

ans=10**10

for i in itertools.product([0,1],repeat=N-1):
        _bamboo=[]
        tmp=bamboo[0]
        for j,m in enumerate(i):
            if m==0:
                _bamboo.append(tmp)
                tmp=bamboo[j+1]
            else:
                tmp+=bamboo[j+1]
        _bamboo.append(tmp)
        ans=min(ans,costWithMagic1and2(_bamboo,A,B,C)+(N-len(_bamboo))*10)
print(ans)

Submission status: Runtime Error

Solution code:
n, a, b, c = list(map(int, input().split()))
goals = [a, b, c]
bamboos = []
for _ in range(n):
    bamboos.append(int(input()))

def get_best(li, goal):
    bam_mp_lis = {0: (-10, tuple())}
    li.sort()
    for i, v in enumerate(li):
        tmp = bam_mp_lis.copy()
        for bam, (mp, lis) in tmp.items():
            b0 = bam + v
            m0 = mp + 10
            l0 = lis + (i,)
            if b0 not in bam_mp_lis or m0 < bam_mp_lis[b0][0]:
                bam_mp_lis[b0] = (m0, l0)
    del bam_mp_lis[0]
    best = 10 ** 9
    for bam, (mp, lis) in bam_mp_lis.items():
        cost = mp + abs(goal - bam)
        if cost < best:
            best = cost
            beslis = lis
    return best, beslis

ans = 10 ** 9
from itertools import permutations
for junjo in permutations(goals):
    tmp = 0
    bambooo = bamboos.copy()
    for ju in junjo:
        be, beli = get_best(bambooo, ju)
        beli = sorted(beli, reverse=True)
        for beli0 in beli:
            del bambooo[beli0]
        tmp += be
    if tmp < ans:
        ans = tmp

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

n, a, b, c = map(int, input().split(' '))
ls = []
for _ in range(n):
    ls.append(int(input()))
n = 0
def solve(obj):
  c = len(ls)
  dp = np.zeros((obj+1, c+1), dtype=int)
  dp[1:, 0] = np.inf
  for j in range(1, c+1):
    dp[0, j] = min(ls[:j])
  for i in range(1, obj+1):
    for j in range(1, c+1):
      dp[i, j] = min(dp[i-1, j] + 1, dp[i, j-1], dp[i-ls[j-1], j-1] + 10, abs(ls[j-1]) - i)
  return dp[obj, c]

for obj in [a, b, c]:
  if obj in ls:
    continue
  else:
    n += solve(obj)
print(n)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <cstring>
#include <string>    
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
ll n, a, b, c, l[10], gl[10], buf[4];

int main(){
	cin >> n >> a >> b >> c;
	for(ll i = 1; i <= n; i++){cin >> l[i];}
	
	ll ans = a+b+c;
	for(ll bit = 0; bit < (1<<2*n); bit++){
		for(ll i = 1; i <= n; i++){
			if(bit&(1<<2*i-2) && bit&(1<<2*i-1)){gl[i] = 3;}
			if(bit&(1<<2*i-2) && !(bit&(1<<2*i-1))){gl[i] = 1;}
			if(!(bit&(1<<2*i-2)) && bit&(1<<2*i-1)){gl[i] = 2;}
			if(!(bit&(1<<2*i-2)) && !(bit&(1<<2*i-1))){gl[i] = 0;}
		}
		
		ll mp = -30;
		ll check = 0;
		memset(buf, 0, sizeof(buf));
		for(ll i = 1; i <= n; i++){
			buf[gl[i]] += l[i];
			if(gl[i] <= 2){
				mp += 10;
				check |= (1<<gl[i]);
			}
		
		}
		sort(buf, buf+3);
		
		mp += abs(c-buf[0]);
		mp += abs(b-buf[1]);
		mp += abs(a-buf[2]);
		
		if(check == (1<<3)-1){ans = min(mp, ans);}
		//cout << gl[1] << gl[2] << gl[3] << " " << bit << endl;
	}
	
	cout << ans << endl;
	return 0;
}

Submission status: Runtime Error

Solution code:
#coding: utf-8
import numpy as np

N, A, B, C = map(int, input().split())
bumboo = np.array([int(input()) for _ in range(N)])
magic = 0
for i in [A, B, C]:
    bumboo_diff = bumboo - i
    bumboo_diff = np.where(bumboo_diff >= 0)
    if np.sum(bumboo_diff) >= 2:
        magic += 10
    else:
        magic += np.min(bumboo_diff)
print(magic)

Submission status: Runtime Error

Solution code:
#coding: utf-8
import numpy as np

N, A, B, C = map(int, input().split())
bumboo = np.array([int(input()) for _ in range(N)])
magic = 0
for i in [A, B, C]:
    bumboo_diff = bumboo - i
    bumboo_diff = np.where(bumboo_diff >= 0)
    if np.sum(bumboo_diff) == 2:
        magic += 10
    else:
        magic += np.min(bumboo_diff)
print(magic)

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split(" "))
L = []
for i in range(N):
    L.append(int(input()))
#print(L)
D = [A,B,C]
L = sorted(L)
MP = 0
flag = []
for i in range(3):
    for j in range(N-len(flag)):
        if D[i] == L[j]:
            MP += 0
            L.remove(D[i])
            flag.append(i)
            break
        else:
            pass
#print(flag)
if len(flag) == 3:
    print(0)
else:
    for i in range(len(flag)):
        D.remove(D[flag[i]])
    #print(D)
    #print(L)

    for i in range(len(D)):
        DIF = []
        for j in range(len(L)):
            DIF.append(abs(D[i] - L[j]))
        #print(DIF)
        #print(min(DIF))
        if min(DIF) <= 10:
            MP += min(DIF)
    print(MP)

Submission status: Runtime Error

Solution code:
import itertools
N, a, b, c = [int(_) for _ in input().split()]
L = [int(input()) for _ in range(N)]
ans = 10**10
for K in itertools.product(range(4), repeat=N):
    A = [[] for _ in range(N)]
    for i in range(N):
        A[K[i]] += [L[i]]
    if len(A[1]) and len(A[2]) and len(A[3]):
        cost = 10 * (N - len(A[0]) - 3)
        cost += abs(a - sum(A[1]))
        cost += abs(b - sum(A[2]))
        cost += abs(c - sum(A[3]))
        ans = min(cost, ans)
print(ans)

Submission status: Runtime Error

Solution code:
import itertools
N, a, b, c = [int(_) for _ in input().split()]
L = [int(input()) for _ in range(N)]
ans = float('inf')
for K in itertools.product(range(4), repeat=N):
    A = [[] for _ in range(N)]
    for i in range(N):
        k = K[i]
        A[k] += [L[i]]
    if len(A[1]) and len(A[2]) and len(A[3]):
        cost = 10 * (max(len(A[1]) - 1, 0) + max(len(A[2]) - 1, 0) + max(
            len(A[3]) - 1, 0))
        cost += abs(a - sum(A[1]))
        cost += abs(b - sum(A[2]))
        cost += abs(c - sum(A[3]))
        ans = min(cost, ans)
print(ans)