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:
n,a,b,c=map(int,input().split())
length=[]
for i in range(n):
  k=int(input())
  length.append(k)
length=sorted(length)
x=[0,1,2,3]
ans=[]
if len(length)==3:
    for p in x:
      for q in x:
        for r in x:
            value=0
            a_length=0
            b_length=0
            c_length=0
            line=[p,q,r]
            if line.count(1)==0:
                break
            if line.count(2)==0:
                break
            if line.count(3)==0:
                break
            if line.count(1)>1:
              value+=10*(line.count(1)-1)
            if line.count(2)>1:
              value+=10*(line.count(2)-1)
            if line.count(3)>1:
              value+=10*(line.count(3)-1)
            A=[i for i, y in enumerate(line) if y ==1]
            B=[i for i, y in enumerate(line) if y ==2]
            C=[i for i, y in enumerate(line) if y ==3]
            for i in A:
              a_length+=length[i]
            for i in B:
              b_length+=length[i]
            for i in C:
              c_length+=length[i]
            value+=abs(a-a_length)
            value+=abs(b-b_length)
            value+=abs(c-c_length)
            ans.append(value)
if len(length)==4:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
              value=0
              a_length=0
              b_length=0
              c_length=0
              line=[p,q,r,s]
              if line.count(1)==0:
                  break
              if line.count(2)==0:
                  break
              if line.count(3)==0:
                  break
              if line.count(1)>1:
                value+=10*(line.count(1)-1)
              if line.count(2)>1:
                value+=10*(line.count(2)-1)
              if line.count(3)>1:
                value+=10*(line.count(3)-1)
              A=[i for i, y in enumerate(line) if y ==1]
              B=[i for i, y in enumerate(line) if y ==2]
              C=[i for i, y in enumerate(line) if y ==3]
              for i in A:
                a_length+=length[i]
              for i in B:
                b_length+=length[i]
              for i in C:
                c_length+=length[i]
              value+=abs(a-a_length)
              value+=abs(b-b_length)
              value+=abs(c-c_length)
              ans.append(value)
if len(length)==5:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
                value=0
                a_length=0
                b_length=0
                c_length=0
                line=[p,q,r,s,t]
                if line.count(1)==0:
                    break
                if line.count(2)==0:
                    break
                if line.count(3)==0:
                    break
                if line.count(1)>1:
                  value+=10*(line.count(1)-1)
                if line.count(2)>1:
                  value+=10*(line.count(2)-1)
                if line.count(3)>1:
                  value+=10*(line.count(3)-1)
                A=[i for i, y in enumerate(line) if y ==1]
                B=[i for i, y in enumerate(line) if y ==2]
                C=[i for i, y in enumerate(line) if y ==3]
                for i in A:
                  a_length+=length[i]
                for i in B:
                  b_length+=length[i]
                for i in C:
                  c_length+=length[i]
                value+=abs(a-a_length)
                value+=abs(b-b_length)
                value+=abs(c-c_length)
                ans.append(value)
if len(length)==6:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
              for u in x:
                  value=0
                  a_length=0
                  b_length=0
                  c_length=0
                  line=[p,q,r,s,t,u]
                  if line.count(1)==0:
                      break
                  if line.count(2)==0:
                      break
                  if line.count(3)==0:
                      break
                  if line.count(1)>1:
                    value+=10*(line.count(1)-1)
                  if line.count(2)>1:
                    value+=10*(line.count(2)-1)
                  if line.count(3)>1:
                    value+=10*(line.count(3)-1)
                  A=[i for i, y in enumerate(line) if y ==1]
                  B=[i for i, y in enumerate(line) if y ==2]
                  C=[i for i, y in enumerate(line) if y ==3]
                  for i in A:
                    a_length+=length[i]
                  for i in B:
                    b_length+=length[i]
                  for i in C:
                    c_length+=length[i]
                  value+=abs(a-a_length)
                  value+=abs(b-b_length)
                  value+=abs(c-c_length)
                  ans.append(value)
if len(length)==7:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
              for u in x:
                for v in x:
                    value=0
                    a_length=0
                    b_length=0
                    c_length=0
                    line=[p,q,r,s,t,u,v]
                    if line.count(1)==0:
                        break
                    if line.count(2)==0:
                        break
                    if line.count(3)==0:
                        break
                    if line.count(1)>1:
                      value+=10*(line.count(1)-1)
                    if line.count(2)>1:
                      value+=10*(line.count(2)-1)
                    if line.count(3)>1:
                      value+=10*(line.count(3)-1)
                    A=[i for i, y in enumerate(line) if y ==1]
                    B=[i for i, y in enumerate(line) if y ==2]
                    C=[i for i, y in enumerate(line) if y ==3]
                    for i in A:
                      a_length+=length[i]
                    for i in B:
                      b_length+=length[i]
                    for i in C:
                      c_length+=length[i]
                    value+=abs(a-a_length)
                    value+=abs(b-b_length)
                    value+=abs(c-c_length)
                    ans.append(value)
if len(length)==8:
    for p in x:
      for q in x:
        for r in x:
          for s in x:
            for t in x:
              for u in x:
                for v in x:
                  for w in x:
                    value=0
                    a_length=0
                    b_length=0
                    c_length=0
                    line=[p,q,r,s,t,u,v,w]
                    if line.count(1)==0:
                        break
                    if line.count(2)==0:
                        break
                    if line.count(3)==0:
                        break
                    if line.count(1)>1:
                      value+=10*(line.count(1)-1)
                    if line.count(2)>1:
                      value+=10*(line.count(2)-1)
                    if line.count(3)>1:
                      value+=10*(line.count(3)-1)
                    A=[i for i, y in enumerate(line) if y ==1]
                    B=[i for i, y in enumerate(line) if y ==2]
                    C=[i for i, y in enumerate(line) if y ==3]
                    for i in A:
                      a_length+=length[i]
                    for i in B:
                      b_length+=length[i]
                    for i in C:
                      c_length+=length[i]
                    value+=abs(a-a_length)
                    value+=abs(b-b_length)
                    value+=abs(c-c_length)
                    ans.append(value)
print(min(ans))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n, a, b, c = map(int, input().split(" "))
take = [a, b, c]
l = []
for i in range(n):
    l.append(int(input()))
take.sort()
l.sort()
mp = 0
get = []
for i in take:
    if i in l:
        get.append(i)
for i in get:
    l.remove(i)
    take.remove(i)

get2 = []
get3 = [[],[],[]]
for i in take:
    mini = [0, 0, 10]
    mini2 = [0, 0, 10]
    mini3 = [0, 0, 10]
    for j in l:
        d = abs(i - j)
        if d <= mini[2]:
            if mini2[0] == 0 and mini[0] != 0:
                mini2 = mini
            elif mini2[0] != 0 and mini[0] != 0:
                mini3 = mini2
                mini2 = mini
            mini = [j, i, d]
    get2.append([mini, mini2, mini3])
for i in get2:
    for ii in i:
        if ii[0] == 0:
            break
        else:
            get3[0].append(ii[0])
            get3[1].append(ii[1])
            get3[2].append(ii[2])
ind = []
mark = []
for i in get3[2]:
    ii = get3[2].index(min(get3[2]))
    if get3[1][ii] not in mark:
        s = get3[0].pop(ii)
        t = get3[1].pop(ii)
        u = get3[2].pop(ii)
        ind.append([s,t,u])
        mark.append(t)
    if len(ind) == 3:
        break
for i in ind:
    l.remove(i[0])
    take.remove(i[1])
    mp += i[2]

def add_take():
    global take, l, mp
    get2 = []
    for i in take:
        added = 0
        mp2 = -10
        mini = [[], 0, 10, 0]
        for j in l:
            mp2 += 10
            added += j
            mini[0].append(j)
            d = abs(i - added)
            if d <= mini[2]:
                mini[1] = i
                mini[2] = d
                mini[3] = mp2
                if added > i:
                    for k in l:
                        added -= k
                        d = abs(i - added)
                        if d <= mini[2]:
                            mp2 -= 10
                            mini[0].remove(k)
                            mini[1] = i
                            mini[2] = d
                            mini[3] = mp2
                        else:
                            break
            elif added > i + 10:
                break
        get2.append(mini)
    get3 = [[],[],[],[]]
    for ii in get2:
        if ii[1] == 0:
            break
        else:
            get3[0].append(ii[0])
            get3[1].append(ii[1])
            get3[2].append(ii[2])
            get3[3].append(ii[3])
    index = get3[2].index(min(get3[2]))
    for i in get3[0][index]:
        l.remove(i)
    take.remove(get3[1][index])
    mp += get3[2][index] + get3[3][index]

if len(take) != 0:
    add_take()

print("{}".format(mp))

Submission status: Runtime Error

Solution code:
N,A,B,C=[int(i) for i in input().split()]
l=[int(input()) for _ in range(n)]
INF=10**18

def dfs(cur, a, b, c):
    if cur==N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 
    ret0 = dfs(cur+1,a,b,c)
    ret1 = dfs(cur+1,a+l[cur],b,c)+10
    ret2 = dfs(cur+1,a,b+l[cur],c)+10    
    ret3 = dfs(cur+1,a,b,c+l[cur])+10    
    return min(ret0,ret1,ret2,ret3)

print(dfs(0,0,0,0))

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())
length=[]
for i in range(n):
  k=int(input())
  length.append(k)
length=sorted(length)
x=[0,1,2,3]
ans=[]
for p in x:
  for q in x:
    for r in x:
      for s in x:
        for t in x:
          for u in x:
            for v in x:
              for w in x:
                value=0
                a_length=0
                b_length=0
                c_length=0
                line=[p,q,r,s,t,u,v,w]
                line.count(1)>1:
                  value+=10*line.count(1)
                line.count(2)>1:
                  value+=10*line.count(1)
                line.count(3)>1:
                  value+=10*line.count(1)
                A=[i for i, y in enumerate(length) if y ==1]
                B=[i for i, y in enumerate(length) if y ==2]
                C=[i for i, y in enumerate(length) if y ==3]
                for i in A:
                  a_length+=length[i]
                for i in B:
                  b_length+=length[i]
                for i in C:
                  c_length+=length[i]
                value+=abs(a-a_length)
                value+=abs(b-b_length)
                value+=abs(c-c_length)
                ans.append(value)
print(min(ans))

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
l=[int(input()) for i in range(n)]
answer = 10*100
for i in range(1,2**(2*n)):
    a_u = []
    b_u = []
    c_u = []
    ans = 0
    j = i
    counte = 0
    while counte <= n:
        if j%4 == 0:
            a_u.append(l[counte])
        if j%4 == 1:
            b_u.append(l[counte])
        if j%4 == 2:
            c_u.append(l[counte])
        j = j//4
        counte += 1
    if len(a_u)*len(b_u)*len(c_u) > 0:
        ans += abs(sum(a_u)-a) + 10*(len(a_u)-1)
        ans += abs(sum(b_u)-b) + 10*(len(b_u)-1)
        ans += abs(sum(c_u)-c) + 10*(len(c_u)-1)
        answer = min(ans,answer)

print(answer)

Submission status: Runtime Error

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

def f(cur,a,b,c) :
    if cur == N :
        if min(a,b,c) > 0 :
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
        else :
            return float("inf")
    ret0 = f(cur+1,a,b,c)
    ret1 = f(cur+1,a+L[cur],b,c) + 10
    ret2 = f(cur+1,a,b+L[cur],c) + 10
    ret3 = f(cur+1,a,b,c+L[cur]) + 10
    return min(ret0,ret1,ret2,ret3)
    
print(f(0,

Submission status: Runtime Error

Solution code:
from itertools import combinations 
N, A, B, C = map(int, input().split())
l_list = []
for i in range(N):
    l_list.append(int(input()))
a_flg, b_flg, c_flg = True, True, True
#print(N, A, B, C)

def dff(wanted, point):
    #print("dff: {}, {}, {}".format(l_list, wanted, point))
    mp1_diff = 10000
    mp1_candidate = -1
    mp2_diff = 10000
    l1, l2 = -1, -1 
    for l in l_list:
        if l == wanted: #欲しい竹だったら
            l_list.remove(l)
            return point 
        diff = abs(wanted - l)
        if mp1_diff > diff:
            mp1_diff = diff
            mp1_candidate = l

    
    for candidate in combinations(l_list, 2): 
        comb_diff = abs(wanted - sum(candidate))
        if comb_diff < mp2_diff:
            mp2_diff = comb_diff
            l1, l2 = candidate[0], candidate[1]

    if mp1_diff < mp2_diff or mp1_diff <=10:
        l_list.remove(mp1_candidate)
        l_list.append(wanted)
        return dff(wanted, point + mp1_diff)
    else:
        l_list.remove(l1)
        l_list.remove(l2)
        l_list.append(l1+l2)
        return dff(wanted, point + 10)
            
if A in l_list:
    l_list.remove(A)
    a_flg = False
if B in l_list:
    b_flg = False
    l_list.remove(C)
if C in l_list:
    c_flg = False
    l_list.remove(C)
rtn = 0
if a_flg:
    rtn += dff(A, 0)
if b_flg:
    rtn += dff(B, 0)
if c_flg:
    rtn += dff(C, 0)
print(rtn)

Submission status: Runtime Error

Solution code:
from itertools import combinations 
N, A, B, C = map(int, input().split())
l_list = []
for i in range(N):
    l_list.append(int(input()))
a_flg, b_flg, c_flg = True, True, True
#print(N, A, B, C)

def dff(wanted, point):
    print("dff: {}, {}, {}".format(l_list, wanted, point))
    mp1_diff = 10000
    mp1_candidate = -1
    mp2_diff = 10000
    l1, l2 = -1, -1 
    for l in l_list:
        if l == wanted: #欲しい竹だったら
            l_list.remove(l)
            return point 
        diff = abs(wanted - l)
        if mp1_diff > diff:
            mp1_diff = diff
            mp1_candidate = l

    
    for candidate in combinations(l_list, 2): 
        comb_diff = abs(wanted - sum(candidate))
        if comb_diff < mp2_diff:
            mp2_diff = comb_diff
            l1, l2 = candidate[0], candidate[1]

    if mp1_diff < mp2_diff or mp1_diff <=10:
        l_list.remove(mp1_candidate)
        l_list.append(wanted)
        return dff(wanted, point + mp1_diff)
    else:
        l_list.remove(l1)
        l_list.remove(l2)
        l_list.append(l1+l2)
        return dff(wanted, point + 10)
            
if A in l_list:
    l_list.remove(A)
    a_flg = False
if B in l_list:
    b_flg = False
    l_list.remove(C)
if C in l_list:
    c_flg = False
    l_list.remove(C)
rtn = 0
if a_flg:
    rtn += dff(A, 0)
if b_flg:
    rtn += dff(B, 0)
if c_flg:
    rtn += dff(C, 0)
print(rtn)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import math

# 解説見ながら記述
# 延長魔法は、合成前・合成後で実施しても等価。
# 合成魔法が使えないパターンにのみ延長魔法を使うようにして全探索するみたい

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

def rec(cur, a, b, c):
    """
    cur: 現在のlsのindex
    a: 合成中の1本目の長さ
    b: 合成中の1本目の長さ
    c: 合成中の1本目の長さ
    """
    if cur == N:
        if min(a, b, c) > 0:
            # 合成1回目：1本目は選ぶだけで合成する必要ないので10*3を引いておく
            return abs(A-a) + abs(B-b) + abs(C-c) - 30
        else:
            # 1回も合成していない場合は無効
            return math.inf
    else:
        r0 = rec(cur+1, a, b, c)    # ls[cur]に対して何もしない
        r1 = rec(cur+1, a+ls[cur], b, c) + 10    # ls[cur]をaに合成
        r2 = rec(cur+1, a, b+ls[cur], c) + 10    # ls[cur]をbに合成
        r3 = rec(cur+1, a, b, c+ls[cur]) + 10    # ls[cur]をcに合成
        return min(r0, r1, r2, r3)

r = rec(0, 0, 0, 0)

print(r)

Submission status: Runtime Error

Solution code:
import sys,collections,math,itertools,copy;sys.setrecursionlimit(10**7)
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def Ss(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

n, a, b, c = Is()
lis = []
ps = list(itertools.permutations([c,b,a]))
ans = []
for i in range(n):
    lis.append(I())

for p in ps:
    clis = copy.deepcopy(lis)
    tan = 0
    for ep in p:
        min_s = 10**10
        com = 0
        for i in range(1,len(clis)+1):
            t = list(itertools.combinations(clis,i))
            for e in t:
                s = (len(e)-1)*10 + abs(sum(e) - ep)
                if s < min_s:
                    min_s = s
                    com = e
        for e in com:
            clis.remove(e)
        tan += min_s
    ans.append(tan)
print(min(ans))

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
l = [int(input()) for i in range(N)]
INF = 10**9

def dfs(cur,a,b,c):
    if(cur == N):
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a,b,c)>0 else INF
    ret0 = dfs(cur+1, a, b, c)
    re1 = dfs(cur + 1, a + l[cur], b, c) + 10
    ret2 = dfs(cur + 1, a, b+l[cur],c)+10
    ret3 = dfs(cur + 1,a,b,c+l[cur])+10
    return min(ret0,ret1,ret2,ret3)
print(dfs(0,0,0,0))

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
def dfs(cur, a, b, c):
	if cur == N:
	return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
	ret0 = dfs(cur + 1, a, b, c)
	ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
	ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
	ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
	return min(ret0, ret1, ret2, ret3)
print(dfs(0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np

N, A, B, C = map(int, input().split(' '))
target = [A, B, C]
length = []
for i in range(N):
    length.append(int(input()))
    
mana = 0
for i in target:
    if(i in length):
        target.remove(i)
        length.remove(i)
        
for i in target:
    if(i < min(length)):
        mana += min(length) - i
        target.remove(i)
        length.remove(min(length))

target_comb = list(itertools.permutations(target, len(target)))
mana_cost = []
for target in target_comb:
    for target_num in target:
        """
        length = sorted(length)        
        for num in range(1, len(length)+1):
            if(sum(length[:num]) > target_num):
                break
        """
        
        combs = []
        for i in range(1, len(length)+1):
            combs = combs + list(itertools.combinations(length, i))
        
        #combs = [flatten for inner in combs for flatten in inner]
        cost = np.zeros(len(combs))
        for col, comb_num in enumerate(combs):
            cost[col] = abs(target_num - sum(comb_num)) + (len(comb_num)-1)*10
        
        arg_min = cost.argmin()
        mana += cost[arg_min]
        for i in combs[arg_min]:
            length.remove(i)
        #print(cost[arg_min])
        #print(combs[arg_min])
    mana_cost.append(mana)
print(min(mana_cost))

Submission status: Runtime Error

Solution code:
from itertools import combinations
N, A, B, C = [int(i) for i in input().split()]
l_ls = list()
for i in range(N):
    l_ls.append(int(input()))
dis_ls = [(i,[i]) for i in l_ls]
for i in range(2,7):
    dis_ls += [(sum(i),i) for i in combinations(l_ls, i)]
best = 1e9
for hoge in [[A, B, C], [A, C, B],[B, A, C],[B, C, A], [C,B,A],[C,A,B]]:
    _sum = 0
    for target in hoge:
        min = 1e9
        for dis in dis_ls:
            c_sa = abs(dis[0] - target) + 10 * (len(dis[1]) - 1)
            if sa < min:
                min = sa
                min_combi = dis[1]
        _sum += min
        dis_ls = [i for i in dis_ls if len(set(min_combi).intersection(set(i[1]))) == 0]
    if _sum < best:
        best = _sum
print(best)

Submission status: Runtime Error

Solution code:
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
cnt = 0
for k in [a,b,c]:
    ans = 0
    for i in range(1, 257):
        s = 0
        for j in range(n):
            if i & 2**j:
                if s > 0:
                    s += 10
                s += l[j]
        v = abs(k - s)
        if v < ans:
            ans = v
            x = i
    cnt += ans
    for j in range(n):
        if x & 2**j:
            l[j] = 0
print(cnt)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
n, a, b, c = map(int, input().split(" "))
take = [a, b, c]
l = []
for i in range(n):
    l.append(int(input()))
take.sort()
l.sort()
mp = 0
get = []
for i in take:
    if i in l:
        get.append(i)
for i in get:
    l.remove(i)
    take.remove(i)

get2 = get3 = []
for i in take:
    mini = [0, 0, 10]
    mini2 = [0, 0, 10]
    mini3 = [0, 0, 10]
    for j in l:
        d = abs(i - j)
        if d <= mini[2]:
            if mini2[0] == 0 and mini[0] != 0:
                mini2 = mini
            elif mini2[0] != 0 and mini[0] != 0:
                mini3 = mini2
                mini2 = mini
            mini = [j, i, d]
    get2.append([mini, mini2, mini3])
for i in get2:
    for ii in i:
        if ii[0] == 0:
            break
        else:
            get3[0].append(ii[0])
            get3[1].append(ii[1])
            get3[2].append(ii[2])
ind = []
for i in get[3]:
    ind.append(i.index(min(get3[2])))
    get[3].remove(i)
    if len(ind) == 3:
        break
for i in ind:
    l.remove(get3[0][i])
    take.remove(get3[1][i])
    mp += get3[2][i]

for i in take:
    added = 0
    mp2 = 0
    mini = [[], 0, 10]
    for j in l:
        mp2 += 10
        added += j
        mini[0].append(j)
        d = abs(i - added)
        if d <= mini[2]:
            mini[1] = i
            mini[2] = d
            if added > i:
                for k in l:
                    added -= k
                    d = abs(i - added)
                    if d <= mini[2]:
                        mp2 -= 10
                        mini[0].remove(k)
                        mini[1] = i
                        mini[2] = d
                    else:
                        break
        elif added > i + 10:
            break
    for m in mini[0]:
        l.remove(m)
    take.remove(mini[1])
    mp += mini[2] + mp2

print("{}".format(mp))

Submission status: Runtime Error

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

def get_nearest_idx(l, i):
    idx = np.searchsorted(np.array(l), i, side='right')

    return idx

n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
l.sort()

c -= l.pop(get_nearest_idx(l, c))
b -= l.pop(get_nearest_idx(l, b))
a -= l.pop(get_nearest_idx(l, a))

mp = 0
for i in [c, b, a]:
    while abs(i) < 10:
        idx = get_nearest_idx(l, i)
        nearest = l[idx]
        if abs(i) < abs(i - nearest):
            break
        else:
            i -= l.pop(idx)
    mp += i

print(mp)

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())
length=[]
point=0
for i in range(n):
  k=int(input())
  length.append(k)
length=sorted(length)
def mp_1(k):
  length_1=0
  ans=11
  for i in length:
    if k-10<=i<=k+10:
      if abs(k-i)<ans:
        length=i
    if length_1 in length:
      length.remove(length_1)
    return ans
def mp_2(k):
  count=0
  for i in length:
    if k>length:
      count=1
      break
    else:
      pass
  if count!=0:
    return 0
  else:
    return 1
def mp_3(k):
  map=[]
  for i in length:
    if k>length:
      map.append(length)
    else:
      pass
    return map
for i in [c,b,a]:
  if mp_1(i)!=11:
    point+=mp_1
  elif mp_2(i)==1:
    point+=abs(k-length.pop(0))
  else:
    for i in mp_3(i):
      for m in mp_3(i).pop(index(i)):
        if i+m==k:
          point+=10
print(point)

Submission status: Runtime Error

Solution code:
num, a, b, c = input().split()
num = int(num)

targetList = ([int(a), int(b), int(c)])
list = list()

mp = 0

for i in range(num) :
    list.append(int(input()))

for target in targetList :
    for elem in list :
        if elem == target :
            list.remove(elem)
            targetList.remove(target)

for target in targetList :
    for elem in list :
        if elem - target >= -10 and elem - target < 0:
            mp += target - elem
            list.remove(elem)
            targetList.remove(target)
        elif elem - target <= 10 and elem - target > 0:
            mp += elem - target
            list.remove(elem)
            targetList.remove(target)

for target in targetList :
    min = 100
    savedMin = 100
    buf1 = 0
    buf2 = 0
    buf3 = 0
    while True :
        for elem in list :
            for elem2 in list :
                if elem + elem2 - target < min and elem + elem2 - target > - min:
                    min = elem + elem2 - target
                    buf1 = elem
                    buf2 = elem2
        if savedMin == min :
            break
        list.append(buf1+buf2)
        list.remove(buf1)
        targetList.remove(buf2)
        buf3 = buf1 + buf2
        mp += 10
        if min >= -10 and min <= 10 :
            break
        savedMin = min
    if buf3 - target < 0 :
        mp+= buf3 - target
        list.remove(buf3)
        targetList.remove(target)
    if buf3 - target > 0 :
        mp+= target - buf3
        list.remove(buf3)
        targetList.remove(target)
    if buf3 - target == 0 :
        list.remove(buf3)
        targetList.remove(target)




print(mp)

Submission status: Runtime Error

Solution code:
N = list(map(int,input().split()))
l = [int(input()) for i in range(N[0])]

if N[1] in l:
    l.remove(N[1])
    del N[1]
if N[2] in l:
    l.remove(N[2])
    del N[2]
if N[3] in l:
    l.remove(N[3])
    del N[3]

MP=0

for i in N[1:]:
    P1 = min([abs(x-i) for x in l])
    P2 = min([abs(x+y-i) for x in l for y in l if l.index(x)!=l.index(y)])
    if P1>=P2:
        MP+=P1
    else:
        MP+=P2

print(MP)

Submission status: Runtime Error

Solution code:
def able(lens):
    ret = [0]
    for i in lens:
        temp = []
        for j in ret:
            temp.append(i + j)
        
        for k in temp:
            ret.append(k)
    ret.remove(0)
    return ret

def choice(ls, tar):
    mind = max(ls)
    theni = len(ls)
    #tar = 1000
    for i in range (len(ls)-1):
        if abs(ls[i] - tar) < mind:
            mind = abs(ls[i] - tar)
            theni = i
            if mind == 0:
                break
    #print(theni, mind)
    #print(theni, mind)
    #print(ls)
    return [theni, mind]

def conv(n):
    t_ = list(map(int, list(str(bin(n))[2:])))
    t_.reverse()
    return t_

ins = list(map(int, input().split()))
n = ins[0]
targs = (ins[1:]).sort()
lengths = [int(input()) for i in range(n)]


def solve(ls,ts):
    point = 0
    for tar in ts:
        com = choice(able(ls), tar)
        point += com[1]
        target = conv(com[0] + 1)
        point += (sum(target) - 1) * 10
        
        k = 0
        for i in range(len(target)):
            
            if (target[i] == 1) and (len(ls)-1 > i-k):
                #print(i-k)
                ls.pop(i - k)
                k += 1
        print(lengths, point)
        
    return point
    
print(solve(lengths, targs))

Submission status: Runtime Error

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

ans = 10000000

A = [0,1,2,3]

import itertools
for a in itertools.product(A, repeat=N):
    ind_1 = [i for i, x in enumerate(a) if x == 1]
    sum_1 = 0
    for i in ind_1:
        sum_1 += l[i]
    ans_1 = 10*(len(ind_1)-1) + abs(sum_1-A)

    ind_2 = [i for i, x in enumerate(a) if x == 2]
    sum_2 = 0
    for i in ind_2:
        sum_2 += l[i]
    ans_2 = 10*(len(ind_2)-1) + abs(sum_2-B)
    
    ind_3 = [i for i, x in enumerate(a) if x == 3]
    sum_3 = 0
    for i in ind_3:
        sum_3 += l[i]
    ans_3 = 10*(len(ind_3)-1) + abs(sum_3-C)
    
    ans = min(ans, ans_1 + ans_2 + ans_3)
    
print(ans)