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:
A, B, Q = [int(a) for a in input().split(" ")]
S = sorted([int(input()) for i in range(A)])
T = sorted([int(input()) for i in range(B)])
X = [int(input()) for i in range(Q)]
INF = float("inf")

def search(arr, x):
    l, r = (0, len(arr))
    while r-l>1:
        med = (l+r)//2
        if arr[med] == x:
            return med
        l, r = (l, med) if x<arr[med] else (med+1, r)
    return l

for x in X:
    s1 = min(search(S, x), len(S)-1)
    t1 = min(search(T, x), len(T)-1)
    ss, ts = None, None
    
    if S[s1]==x:
        ss = (0, 0)
    elif s1==0 and x<S[s1]:
        ss = (INF, abs(S[s1]-x))
    elif s1==len(S)-1 and S[s1]<x:
        ss = (abs(S[s1]-x), INF)
    elif S[s1]<x:
        ss = (abs(S[s1]-x), abs(S[s1+1]-x))
    else:
        ss = (abs(S[s1-1]-x), abs(S[s1]-x))
    
    if T[t1]==x:
        ts = (0, 0)
    elif t1==0 and x<T[t1]:
        ts = (INF, abs(T[t1]-x))
    elif t1==len(T)-1 and T[t1]<x:
        ts = (abs(T[t1]-x), INF)
    elif T[t1]<x:
        ts = (abs(T[t1]-x), abs(T[t1+1]-x))
    else:
        ts = (abs(T[t1-1]-x), abs(T[t1]-x))

    ans = min([max(ss[0], ts[0]), max(ss[1], ts[1]),
               2*ss[0]+ts[1], 2*ss[1]+ts[0], ss[0]+2*ts[1], ss[1]+2*ts[0]])
    print(ans)

Submission status: Runtime Error

Solution code:
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]

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)
    ret2 = dfs(cur+1, a, b+l[cur], c)
    ret3 = dfs(cur+1, a, b, c+l[cur])
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Runtime Error

Solution code:
import collections
from copy import deepcopy

n,a,b,c=map(int,input().split())
arr=[int(input()) for _ in range(n)]
l=collections.deque()
l.append([[],[],[]])
cnt=0

while 1:
  if cnt==8:
    break
  base=l.popleft()
  if len(base[0])==0 and len(base[1])==0 and len(base[2])==0:
    l.append([[arr[cnt]],[],[]])
    l.append([[],[arr[cnt]],[]])
    l.append([[],[],[arr[cnt]]])
    l.append([[],[],[]])
    cnt+=1
  else:
    tmp1=deepcopy(base)
    tmp1[0].append(arr[cnt])
    l.append(tmp1)
    tmp2=deepcopy(base)
    tmp2[1].append(arr[cnt])
    l.append(tmp2)
    tmp3=deepcopy(base)
    tmp3[2].append(arr[cnt])
    l.append(tmp3)
    tmp4=deepcopy(base)
    l.append(tmp4)

mincost=10**9
for bl in l:
  if len(bl[0])==0 or len(bl[1])==0 or len(bl[2])==0:
    continue
  else:
    tmpcost=abs(sum(bl[0])-a)+abs(sum(bl[1])-b)+abs(sum(bl[2])-c)+10*(len(bl[0])-1+len(bl[1])-1+len(bl[2])-1)
    if mincost>tmpcost:
      mincost=tmpcost
print(mincost)

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:
def rec (i,a,b,c):
    if i==N:
        if a==0 and b==0 and c==0:
            return INF
        else:
            return abs(a-A)+abs(b-B)+abs(c-C)
    res = rec(i+1,a,b,c)
    
    res = min(res,rec(i+1,a+L[i],b,c)+(10 if a>0 else 0))
    res = min(res,rec(i+1,a,b+L[i],c)+(10 if b>0 else 0))
    res = min(res,rec(i+1,a,b,c+L[i])+(10 if c>0 else 0))
    
    return res
N,A,B,C = list(map(int,input().split()))
L = [int(input()) for _ in range(N)]
print(rec(0,0,0,0))

Submission status: Runtime Error

Solution code:
import math

def d2n(d, n, p):
    x = '' # Output
    while d > 0:
        x = str(d % n) + x
        d = int(d / n)
    return x.zfill(p)

def divide(l, n):
    x = [[], [], []] # 追加していく
    for i, j in enumerate(n):
        x[int(j)].append(l[i])
    return x

def minimum(l, ABC):
    mp = math.inf
    c = 1
    while c < 2 ** len(l):
        n = d2n(c, 2, len(l)) # 選ぶ竹を集める
        m = []
        for i, j in enumerate(n):
            if j == "1":
                m.append(l[i])
        if abs(ABC - sum(m)) + (len(m) - 1) * 10 < mp:
            mp = abs(ABC - sum(m)) + (len(m) - 1) * 10
        c += 1 
    return mp

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

l = []
for _ in range(N):
    l.append(int(input()))

c = 0 
mp = math.inf
while c < 3 ** N:
    n = d2n(c, 3, N) # グループわけ
    l1, l2, l3 = divide(l, n)
    if len(l1) != 0 and len(l2) != 0 and len(l3) != 0: # 各リストの要素を使った最小値を計算
        a = minimum(l1, A) + minimum(l2, B) + minimum(l3, C)
        if a < mp:
            mp = a    
    c += 1

print(mp)

Submission status: Runtime Error

Solution code:
a=a

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:
N, A, B, C = map(int, input().split(' '))
L = [int(input()) for i in range(N)]
# 上限
MAX = 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 MAX
  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
  print(ret0,ret1,ret2,ret3)
  return min(ret0,ret1,ret2,ret3)
print(dfs(0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)
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:
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:
read = input
read = Read(False, """


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

""").read
tmp = read()
tmp = list(map(int, tmp.split(' ')))
N,A,B,C = tmp
opt = list(map(int, map(lambda x: read(), range(N))))
def dfs(n,a,b,c):
    ret = 10**7
    if(n == N):
        if(min(a,b,c) == 0):
            return ret
        return abs(A - a) + abs(B - b) + abs(C - c) - 30
    cur = opt[n]
    ret = min(ret, dfs(n+1, a + cur, b, c) + 10)
    ret = min(ret, dfs(n+1, a, b + cur, c) + 10)
    ret = min(ret, dfs(n+1, a, b ,c + cur) + 10)
    ret = min(ret, dfs(n+1, a, b ,c ) + 0)
    return ret
print(dfs(0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
many, A, B, C = map(int, input().split())
bamboos = [int(input()) for i in range(many)]

# point: 訳が分からなくなるものは自分でルールづけをする
# 合成魔法を全て最初に行う、というルールづけ

# 解法: DFS or 4進数(4patternsで2の累乗だからできる)
# PS: DFS(深さ優先探索), BFS(幅優先探索)

infinity = 1e10

def dfs(depth, a, b, c):
    if depth == many:
        # 謎の場合分け
        if min(a, b, c) > 0:
            # 竹が何もない状態でも各竹に+10がかかっていることから-30する必要がある。
            # また、+10が一個はないとベースとなる竹がないことになるので
            # a,b,c = 0はコストを無限にすることで避ける。
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
    else:
        ret0 = dfs(depth+1, a, b, c)
        ret1 = dfs(depth+1, a+bamboos[depth], b, c) + 10
        ret2 = dfs(depth+1, a, b+bamboos[depth], c) + 10
        ret3 = dfs(depth+1, a, b, c+bamboos[depth]) + 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:
N,A,B,C = map(int, input().split())
l = [int(input()) for _ 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) > 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 bisect

ans = 10**20

N, K = list(map(int, input().split()))
x = [-10**20] + list(map(int, input().split())) + [10**20]

zero = bisect.bisect_left(x, 0)

left = x[:zero] + [0] 
right = [0] + x[zero:]
left.reverse()
l = len(left)
r = len(right)

idxs = [[K-i, i] for i in range(K+1) if K-i < l and i < r]

for l, r in idxs:
    n = abs(left[l]) + right[r] + min(abs(left[l]), right[r])
    ans = min(ans, n)
print(ans)

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())
l=[]
for i in range(n):
  l.append(int(input()))
list2=[]
s=0
for i in range(4):
  list=[0,0,0,0]
  list[i]+=l[0]
  if i!=3:
    s+=10
  for j in range(4):
    list[j]+=l[1]
    if j!=3:
      s+=10
      for o in range(4):
        list[o]+=l[2]
        if o!=3:
          s+=10
          for p in range(4):
            list[p]+=l[3]
            if p!=3:
              s+=10
              for u in range(4):
                list[u]+=l[4]
                if u!=3:
                  s+=10                 
                  for t in range(4):
                    list[t]+=l[5]
                    if t!=3:
                      s+=10
                      for r in range(4):
                        list[r]+=l[6]
                        if r!=3:
                          s+=10
                          for q in range(4):
                            list[q]+=l[7]
                            if q!=3:
                              s+=10
                            if list[0]==0 or list[1]==0 or list[2]==0:
                              pass
                            else:
                              list2.append(abs(a-list[0])+abs(b-list[1])+abs(c-list[2]))
print(min(list2))

Submission status: Runtime Error

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

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

INF=3000

def mp(co,a,b,c):
    if co==N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else INF

    ret0=mp(co+1,a,b,c)
    ret1=mp(co+1,a+1,b,c)+10
    ret2=mp(co+1,a,b+1,c)+10
    ret3=mp(co+1,a,b,c+1)+10

    return min(ret0,ret1,ret2,ret3)

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

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

INF=3000

def mp(co,a,b,c):
    if co==N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else INF

    ret0=mp(co+1,a,b,c)
    ret1=mp(co+1,a+1,b,c)+10
    ret2=mp(co+1,a,b+1,c)+10
    ret3=mp(co+1,a,b,c+1)+10

    return min(ret0,ret1,ret2,ret3)

print(mp(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=3000

def mp(c,a,b,c):
    if c==N:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else INF

    ret0=mp(c+1,a,b,c)
    ret1=mp(c+1,a+1,b,c)+10
    ret2=mp(c+1,a,b+1,c)+10
    ret3=mp(c+1,a,b,c+1)+10

    return min(ret0,ret1,ret2,ret3)

print(mp(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=3000

def mp(c,a,b,c):
    return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else INF

    ret0=mp(c+1,a,b,c)
    ret1=mp(c+1,a+1,b,c)+10
    ret2=mp(c+1,a,b+1,c)+10
    ret3=mp(c+1,a,b,c+1)+10

    return min(ret0,ret1,ret2,ret3)

print(mp(0,0,0,0))

Submission status: Runtime Error

Solution code:
from math import inf
from collections import deque

N, A, B, C = map(int, input().split())
ls = [0]
for _ in range(N):
    ls.append(int(input()))
ls.sort()

q = deque()
q.append((0, -30, 0, 0, 0))
minmp = inf
while len(q):
    n, mp, a, b, c = q.pop()
    if n == N:
        if min(a, b, c) == 0:
            continue
        minmp = min(minmp, mp + abs(a - A) + abs(b - B) + abs(c - C))
    else:
        q.append((n + 1, mp, a, b, c))
        q.append((n + 1, mp + 10, a + ls[n + 1], b, c))
        q.append((n + 1, mp + 10, a, b + ls[n + 1], c))
        q.append((n + 1, mp + 10, a, b, c + ls[n + 1]))

print(minmp)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <numeric>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define INF (int)(1123456789)
#define LINF (long long int)(123456789012345678)


unsigned GetDigit(unsigned num) {
    return std::to_string(num).length();
}


int main(void){
	int N, A, B, C; cin >> N >> A >> B >> C;
  int ans = INF;
  vector<int> end;  // 目的とする長さのリスト
  end.push_back(A);
  end.push_back(B);
  end.push_back(C);
  int l[N];
  rep(i, N) cin >> l[i];

  // N桁の4進数について考える
  int max_cnt = std::pow(4, N);
  
  // 1から4**Nまで
  rep(i, max_cnt)  {
    
    // 長さが4のリストassignに、A,B,C,未使用の竹の長さを割り当てる
    vector<vector<int> > assign;
    assign.resize(4);
    
    int cnt = i;
    rep(j, N) {
      // 4進数で表した時の一番下の桁を見る
      assign[cnt%4].push_back(l[j]);
      // 一番下の桁を抜く
      cnt = cnt/4;
    }
    if (assign[0].size()!=0 && assign[1].size()!=0 && assign[2].size()!=0)  {
      int cost = 0; // 操作に必要なコスト
      rep(i, 3) {
        cost += (assign[i].size()-1) * 10;  // mergeの操作
        cost += abs(end[i] - std::accumulate(assign[i].begin(), assign[i].end(), 0)); // 長さの調整
      }
      ans = min(ans, cost); // コストの小さい方が答え
    }
  }
  cout << ans << endl;
	return 0;
}

Submission status: Runtime Error

Solution code:
import math

N,A,B,C = map(int,input().split())
target = [A,B,C]
l = []
#ans = math.inf

for _ in range(N):
    l.append(int(input()))


def calc2(v):
    res = 0
    for i in range(3):
        if len(v[i])==0:
            return math.inf
        res += 10*(len(v[i])-1) + abs(sum(v[i]) - target[i])
    return res

v=[[],[],[],[]]
def dfs2(depth=0):
    #global ans
    if depth == N:
        return calc2(v)
    #else:
    ans = math.inf
    for i in range(4):
        #next_v = v.copy()
        #print(i, v, depth, l)
        v[i].append(l[depth])
        ans = min(ans,dfs2(depth+1))
        del v[i][-1]

    return ans
print(dfs2())

Submission status: Runtime Error

Solution code:
def nsin(X,y):
    if(int(X/y)):
        return nsin(int(X/y),y)+str(X%y)
    return str(X%y)

cost=10000
for i in range(3**n):
    g=list(nsin(i,3).zfill(5))
    if len(set(g))!=3:
        continue
    la=[]
    lb=[]
    lc=[]
    for j in range(n):        
        if g[j]=="0":
            la.append(l[j])
        elif g[j]=="1":
            lb.append(l[j])
        elif g[j]=="2":
            lc.append(l[j])
    costa=10000
    costb=10000
    costc=10000
    for j in range(1,2**len(la)):
        ga=list(nsin(j,2).zfill(len(la)))
        A=0
        cont=-1
        for k in range(len(la)):
            if ga[k]=="1":
                A+=la[k]
                cont+=1
        tmp=cont*10+abs(A-a)
        if tmp<costa:
            costa=tmp
    for j in range(1,2**len(lb)):
        gb=list(nsin(j,2).zfill(len(lb)))
        B=0
        cont=-1
        for k in range(len(lb)):
            if gb[k]=="1":
                B+=lb[k]
                cont+=1
        tmp=cont*10+abs(B-b)
        if tmp<costb:
            costb=tmp
    for j in range(1,2**len(lc)):
        gc=list(nsin(j,2).zfill(len(lc)))
        C=0
        cont=-1
        for k in range(len(lc)):
            if gc[k]=="1":
                C+=lc[k]
                cont+=1
        tmp=cont*10+abs(C-c)
        if tmp<costc:
            costc=tmp
    if cost>(costa+costb+costc):
        cost=costa+costb+costc
print(cost)

Submission status: Runtime Error

Solution code:
def dfs(a, b, c, k)
  
  if k == N
    nil if [a, b, c].min == 0
    return (A-a).abs + (B-b).abs + (C-c).abs - 10 * 3
  end
  
  x = dfs(a+L[k], b, c, k+1) + 10
  y = dfs(a, b+L[k], c, k+1) + 10
  z = dfs(a, b, c+L[k], k+1) + 10
  w = dfs(a, b, c, k+1)
  
  [x, y, z, w].compact.min
end

N, A, B, C = gets.split.map(&:to_i)

L = Array.new(N)
N.times{ |i| L[i] = gets.to_i }

p 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:
    if min(a, b, c) > 0:
      return abs(a - A) + abs(b - B) + abs(c - C) 
    else:
      return inf
  else:  
  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())
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:
#coding:utf-8
import sys

BAMB_NUM = 0
BAMB_A = 0
BAMB_B = 0
BAMB_C = 0
BAMB_LIST = []

def calcMP(int idx, int lenA, int lenB, int lenC):
    # classified all bamboos
    if (idx == BAMB_NUM):
        # exists a zero length bamboo
        if (lenA <= 0 or lenB <= 0 or lenC <= 0):
            return sys.maxsize
        return abs(lenA - BAMB_A) + abs(lenB - BAMB_B) + abs(lenc - BAMB_C)
    
    # don't use idx-th bamboo
    mp = calcMP(idx+1, lenA, lenB, lenC)

    # use idx-th bamboo
    mp = min(mp, calcMP(lenA + BAMB_LIST[idx], lenB, lenC) + (BAMB_A > 0 ? 10 : 0))
    mp = min(mp, calcMP(lenA, lenB + BAMB_LIST[idx], lenC) + (BAMB_B > 0 ? 10 : 0))
    mp = min(mp, calcMP(lenA, lenB, lenC + BAMB_LIST[idx]) + (BAMB_C > 0 ? 10 : 0))

    return mp

if __name__ == "__main__":
    """
    http://drken1215.hatenablog.com/entry/2019/02/24/224100
    """
    # requested bamboo lengths
    bamb_num, bambA, bambB, bambC = map(lambda x: int(x), input().split())
    # available bamboo lengths
    bamb_list = []
    for bamb in range(bamb_num):
        bamb_list.append(int(input()))

    mp = calcMP(0, 0, 0, 0)
    print(mp)

Submission status: Runtime Error

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    ls = [int(input()) for _ in range(N)]

    tmp = []
    from itertools import permutations
    for a, b, c in permutations([A, B, C]):
        x = f(N, a, b, c, ls)
        tmp.append(x)

    ans = min(tmp)
    print(ans)


def f(N, A, B, C, ls):
    """
    A,B,Cそれぞれが存在していたら終了
    存在しない場合
    diff をとる
        diffと合成するしないでの最善
    ABCの順に依存するが、3つしかないので 3P3=6通り
    N_max=8 よりゴリ押し
    """
    ls = ls[:]
    abc = [A, B, C]

    ans = 0
    for x in abc[:]:
        if x in ls:
            abc.remove(x)
            ls.remove(x)

    while abc:
        x = abc[0]
        diffs = [l - x for l in ls]
        # print(abc, ans, ls, diffs)
        # 完全一致
        if x in ls:
            ls.remove(x)
            abc.remove(x)
            continue

        # 合成コスト以下, sortedで試してたけどmin のkey指定でよかった
        i, diff = min(enumerate(diffs), key=lambda a: abs(a[0]))
        min_cost = abs(diff)
        if min_cost <= 10:
            ans += min_cost
            del ls[i]
            del abc[0]
            continue

        # 合成できない
        if len(ls) < 2:
            ans += abs(ls[0] - x)
            abc.remove(x)
            del ls[0]
            continue

        # 合成
        comb = []
        n_ls = len(ls)
        for i in range(n_ls):
            for j in range(i+1, n_ls):
                comb.append((i, j, ls[i] + ls[j]))

        # 合成前後のコスト比較
        i, j, new_l = min(comb, key=lambda b: abs(b[2] - x))
        # print("\t", [c[2] for c in comb])
        # print("\t", i, j, new_l, min_cost)
        cost = abs(x - new_l)
        if cost >= min_cost:
            del_idx = min(i, j, key=lambda idx: abs(ls[idx]) - x)
            ans += min_cost
            del ls[del_idx]
            del abc[0]
        else:
            ls[i] = new_l
            del ls[j]
            ans += 10

    return ans


if __name__ == '__main__':
    main()

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(d,a,b,c):
    if d==n:#合成完了
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else inf #無からは作れない#0に合成するのにmpは使わないので-30
    ret0=dfs(d+1,a,b,c)#l[d]を使わない
    ret1=dfs(d+1,a+l[d],b,c)+10#l[d]をAに
    ret2=dfs(d+1,a,b+l[d],c)+10#l[d]をBに
    ret3=dfs(d+1,a,b,c+l[d])+10#l[d]をCに
    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(d,a,b,c):
    if d==n:#合成完了
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else inf #無からは作れない
    ret0=dfs(d+1,a,b,c)#l[d]を使わない
    ret1=dfs(d+1,a+l[d],b,c)+10#l[d]をAに
    ret2=dfs(d+1,a,b+l[d],c)+10#l[d]をBに
    ret3=dfs(d+1,a,b,c+l[d])+10#l[d]をCに
    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(d,a,b,c):
    if d==n:#合成完了
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else inf
    ret0=def(d+1,a,b,c)#l[d]を使わない
    ret1=def(d+1,a+l[d],b,c)+10#l[d]をAに
    ret2=def(d+1,a,b+l[d],c)+10#l[d]をBに
    ret3=def(d+1,a,b,c+l[d])+10#l[d]をCに
    return min(ret0,ret1,ret2,ret3)
print(def(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)
    ret2 = dfs(cur + 1, a, b + l[cur], c)
    ret3 = dfs(cur + 1, a, b, c + l[cur])
    return min(ret0, ret1, ret2, ret3)

print(dfs(ret0, ret1, ret2, ret3))

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)
    ret2 = dfs(cur+1,a+l[cur],b,c)+10
    ret3 = dfs(cur+1,a,b+l[cur],c)+10
    ret4 = 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:
data = input().split()
N = int(data[0])
A = int(data[1])
B = int(data[2])
C = int(data[3])

i = 0
kd = []
while i < N:
  kd.append(int(input()))
  i += 1


cur = 0 
sc = 0
def ans(n,a,b,c):
  if cur == N:
    return abs(a-A) + abs(b-B)+ abs(c-C) - 30 if min(a,b,c) > 0 else 1000000
  ret0 = ans(cur+1,a,b,c)
  ret1 =  ans(cur+1,a+kd[cur],b,c) + 10
  ret2 = ans(cur + 1,a,b+kd[cur],c) +10 
  ret3 = ans(cur+1,a,b,c+kd[cur])+ 10
  return min(ret0,ret1,ret2,ret3)

print(ans(0,0,0,0))

Submission status: Runtime Error

Solution code:
n, A, B, C = (int(i) for i in input().split())  #a=1, b=3, c=5, d=7
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:
n,A,B,C = map(int, input().split())
l = []
for i in range(n):
  l.append(int(input()))

def dfs(cur, a, b, c):
  #再帰の枝が末尾まで行った時の処理
  if cur == n:
    #末尾まで行ったらその時点で全ての竹に伸縮魔法使った時のmpを算出（もともと長さが0の場合を除く）
    return abs(A-a)+abs(B-b)+abs(C-c)-30　if min(a,b,c) > 0 else 10**9
  
  #次の竹(cur番目の竹)をどの竹にも使わない場合
  ret0 = dfs(cur+1,a,b,c)
  #次の竹(cur番目の竹)をaの竹に使う場合
  ret1 = dfs(cur+1,a+l[cur],b,c) +10
  
  ret2 = dfs(cur+1,a,l[cur]+b,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 = []
for i in range(n):
  l.append(int(input()))

def dfs(cur, a, b, c):
  #再帰の枝が末尾まで行った時の処理
  if cur == n:
    #末尾まで行ったらその時点で全ての竹に伸縮魔法使った時のmpを算出（もともと長さが0の場合を除く）
    return abs(A-a)+abs(B-b)+abs(C-c)-30　if min(a,b,c) > 0 else 10**9
  
  #次の竹(cur番目の竹)をどの竹にも使わない場合
  ret0 = dfs(cur+1,a,b,c)
  #次の竹(cur番目の竹)をaの竹に使う場合
  ret1 = dfs(cur+1,a+l[cur],b,c) +10
  
  ret2 = dfs(cur+1,a,l[cur]+b,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 = []
for i in range(n):
  l.append(int(input()))

def dfs(cur, a, b, c):
  #再帰の枝が末尾まで行った時の処理
  if cur == n:
    #末尾まで行ったらその時点で全ての竹に伸縮魔法使った時のmpを算出（もともと長さが0の場合を除く）
    return abs(A-a)+abs(B-b)+abs(C-c)-30　if min(a,b,c) > 0 else 10**9
  
  #次の竹(cur番目の竹)をどの竹にも使わない場合
  ret0 = dfs(cur+1,a,b,c)
  #次の竹(cur番目の竹)をaの竹に使う場合
  ret1 = dfs(cur+1,a+l[cur],b,c) +10
  
  ret2 = dfs(cur+1,a,l[cur]+b,c) +10
  
  ret3 = dfs(cur+1,a,b,c+l[cur]) +10
  
  return min(ret0, ret1, ret2, ret3)

print(0,0,0,0)

Submission status: Runtime Error

Solution code:
N, A, B, C = map(int, input().split())
2 l = [int(input()) for i in range(N)]
3 INF = 10 ** 9
4
5 def dfs(cur, a, b, c, MP):
6 if cur == N:
7 return abs(a - A) + abs(b - B) + abs(c - C) + MP if min(a, b, c) > 0 else INF
8 ret0 = dfs(cur + 1, a, b, c, MP)
9 ret1 = dfs(cur + 1, a + l[cur], b, c, MP + 10)
10 ret2 = dfs(cur + 1, a, b + l[cur], c, MP + 10)
11 ret3 = dfs(cur + 1, a, b, c + l[cur], MP + 10)
12 return min(ret0, ret1, ret2, ret3)
13
14 print(dfs(0, 0, 0, 0))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int, input().split())
l = [int(i) for i in input()]
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:
N,A,B,C = map(int,input().split())
l = [input() for i in range(N)]
INF = 10 ** 9

def ans(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 = ans(cur + 1,a,b,c) 
    ret1 = ans(cur + 1,a + l[cur],b,c) + 10
    ret2 = ans(cur + 1,a,b + l[cur],c) + 10
    ret3 = ans(cur + 1,a,b,c + l[cur]) + 10

    return min(ret0,ret1,ret2,ret3)

print(ans(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))