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=(input().split())
a,b,q=int(a),int(b),int(q)
s=[int(input()) for i in range(a)]
t=[int(input()) for i in range(b)]
x=[int(input()) for i in range(q)]
dist=['' for i in range(8)]
dist1=['' for i in range(2)]
dist2=['' for i in range(4)]
sp1=['' for i in range(2)]
tp1=['' for i in range(4)]

import bisect
for k in range(q):

    p1=bisect.bisect_left(s,x[k])

    if 0 < p1 < a:
        dist1[0]=x[k]-s[p1-1]
        dist1[1]=s[p1]-x[k]
        sp1[0]=s[p1-1]
        sp1[1]=s[p1]
    elif p1==0:
        dist1[0]=s[0]-x[k]
        dist1[1]=s[0]-x[k]
        sp1[0]=s[0]
        sp1[1]=s[0]        
    elif p1==a:
        dist1[0]=x[k]-s[a-1]
        dist1[1]=x[k]-s[a-1]
        sp1[0]=s[a-1]
        sp1[1]=s[a-1]
        
    for i in range(2):     
        p11=bisect.bisect_left(t,sp1[i])

        if 0 < p11 < b:
            dist2[0+2*i]=sp1[i]-t[p11-1]
            dist2[1+2*i]=t[p11]-sp1[i]
        if p11==0:
            dist2[0+2*i]=t[0]-sp1[i]
            dist2[1+2*i]=t[0]-sp1[i]
        elif p11==b:
            dist2[0+2*i]=sp1[i]-t[b-1]
            dist2[1+2*i]=sp1[i]-t[b-1]
        
    dist[0]=dist1[0]+dist2[0]
    dist[1]=dist1[0]+dist2[1]
    dist[2]=dist1[1]+dist2[2]
    dist[3]=dist1[1]+dist2[3]
                
    p1=bisect.bisect_left(t,x[k])

    if 0 < p1 < b:
        sp1[0]=t[p1-1]
        sp1[1]=t[p1]
        dist1[0]=x[k]-t[p1-1]
        dist1[1]=t[p1]-x[k]
    elif p1==0:
        sp1[0]=t[0]
        sp1[1]=t[0]
        dist1[0]=t[0]-x[k]
        dist1[1]=t[0]-x[k]
    elif p1==b:
        sp1[0]=t[b-1]
        sp1[1]=t[b-1]
        dist1[0]=x[k]-t[b-1]
        dist1[1]=x[k]-t[b-1]
        
    for i in range(2):    
        p11=bisect.bisect_left(s,sp1[i])

        if 0 < p11 < a:
            dist2[0+2*i]=sp1[i]-s[p11-1]
            dist2[1+2*i]=s[p11]-sp1[i]
        elif p11==0:
            dist2[0+2*i]=s[0]-sp1[i]
            dist2[1+2*i]=s[0]-sp1[i]
        elif p11==a:
            dist2[0+2*i]=sp1[i]-s[a-1]
            dist2[1+2*i]=sp1[i]-s[a-1]

    dist[4]=dist1[0]+dist2[0]
    dist[5]=dist1[0]+dist2[1]
    dist[6]=dist1[1]+dist2[2]
    dist[7]=dist1[1]+dist2[3]
    
    ans=min(dist[0],dist[1],dist[2],dist[3],dist[4],dist[5],dist[6],dist[7])

    print(ans)

Submission status: Runtime Error

Solution code:
# N A B C
# l1
# l2
# ...

(N, A, B, C) = int(input().split())
L = []
for i in range(N):
    l = int(input())
    L.append(l)
# N = 5
# A = 100
# B = 90
# C = 80
# L = [98, 40, 30, 21, 80]

def recur_function(a, b, c, n):
    if n == N:
        return abs(a - A) + abs(b - B) + abs(c - C) if min(a, b, c) > 0 else 10**9
    ret0 = recur_function(a + L[n], b, c, n + 1) + 10 if a > 0 else recur_function(a + L[n], b, c, n + 1)
    ret1 = recur_function(a, b + L[n], c, n + 1) + 10 if b > 0 else recur_function(a, b + L[n], c, n + 1)
    ret2 = recur_function(a, b, c + L[n], n + 1) + 10 if c > 0 else recur_function(a, b, c + L[n], n + 1)
    ret3 = recur_function(a, b, c, n + 1)
    return min(ret0, ret1, ret2, ret3)

print(recur_function(0, 0, 0, 0))

Submission status: Runtime Error

Solution code:
N,A,B,C = map(int, input().split())
 
bms = [int(input()) for _ in Range(N)]
 
def dfs(cur,a,b,c):
    if cur == N:
        s = abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c) > 0 else 10**9
        return s
    s_0 = dfs(cur+1,a,b,c)
    s_a = dfs(cur+1,a+bms[cur],b,c) + 10
    s_b = dfs(cur+1,a,b+bms[cur],c) + 10
    s_c = dfs(cur+1,a,b,c+bms[cur]) + 10
    return min(s_0,s_a,s_b,s_c)

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

Submission status: Runtime Error

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

bms = [int(input()) for _ in Range(N)]

def dfs(cur,a,b,c):
    if cur == N:
        s = abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c) > 0 else 10**9
        return s
    s_0 = dfs(cur+1,a,b,c)
    s_a = dfs(cur+1,a+bms[cur],b,c) + 10
    s_b = dfs(cur+1,a,b+bms[cur],c) + 10
    s_c = dfs(cur+1,a,b,c+bms[cur]) + 10
    return min(s_0,s_a,s_b,s_c)

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

Submission status: Runtime Error

Solution code:
import itertools

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

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

todo_list = [0,1,2,3]

todo_comb = itertools.product(todo_list, repeat=5)

ABCNo = [[],[],[],[]]

MP = float('inf')

for todo in todo_comb:
    for i in range(N):
        ABCNo[todo[i]].append(l_list[i])

    if ABCNo[0] == [] or ABCNo[1] == [] or ABCNo[2] == []:
        continue

    AA = sum(ABCNo[0])
    BB = sum(ABCNo[1])
    CC = sum(ABCNo[2])

    tmp = abs(AA-A) + abs(BB-B) + abs(CC-C)
    tmp += (len(ABCNo[0]) + len(ABCNo[1]) + len(ABCNo[2]))*10

    if tmp < MP:
        MP = tmp

print(tmp)

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:
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 100 90 80\n"
     ]
    }
   ],
   "source": [
    "N,A,B,C = map(int, input().split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "98\n",
      "40\n",
      "30\n",
      "21\n",
      "80\n"
     ]
    }
   ],
   "source": [
    "l = []\n",
    "for i in range(n):\n",
    "    l.append(int(input()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23\n"
     ]
    }
   ],
   "source": [
    "INF = 10 ** 9\n",
    "def dfs(cur, a, b, c):\n",
    "    if cur == n:\n",
    "        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a,b,c) > 0 else INF\n",
    "    ret0 = dfs(cur + 1, a, b, c)\n",
    "    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10\n",
    "    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10\n",
    "    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10\n",
    "    return min(ret0, ret1, ret2, ret3)\n",
    "\n",
    "print(dfs(0, 0, 0, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}

Submission status: Runtime Error

Solution code:
import numpy as np

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

# goals = [1000,800,100]
# ls = [300,333,400,444,500,555,600,666]
# goals = [100, 90, 80]
# ls = [98, 40,30,21,80]

def count(x,ls):
    try:
        x.index(0)
        x.index(1)
        x.index(2)        
    except:
        return False
    A = np.zeros(len(goals))
    for i,l in enumerate(ls):
        add_ind = x[i]
        if add_ind==3:
            pass
        else:
            A[add_ind] += l
    combine_MP = 0
    for i in range(3):
        combine_MP += 10*(x.count(i)-1)
    return A, combine_MP
    
    
score_min = 100000
for N in range(4**8):
    x = []
    for j in range(len(ls)):
        x.append(N % 4)
        N = N // 4 
    try:
        A, combine_MP = count(x,ls)
        score = np.abs(A-np.array(goals)).sum() + combine_MP
        if score_min >= score:
            score_min = score
    except:
        pass
print(score_min)

Submission status: Runtime Error

Solution code:
import numpy as np

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

def count(x,ls):
    try:
        x.index(0)
        x.index(1)
        x.index(2)        
    except:
        return False
    A = np.zeros(len(goals))
    for i,l in enumerate(ls):
        add_ind = x[i]
        if add_ind==3:
            pass
        else:
            A[add_ind] += l
    combine_MP = 0
    for i in range(3):
        combine_MP += 10*(x.count(i)-1)
    return A, combine_MP
    
    
score_min = 100000
for N in range(4**8):
    x = []
    for j in range(len(ls)):
        x.append(N % 4)
        N = N // 4 
    try:
        A, combine_MP = count(x,ls)
        score = np.abs(A-np.array(goals)).sum() + combine_MP
        if score_min >= score:
            score_min = score
    except:
        pass
print(score_min)

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(cursor, a, b, c):  # cursor:カーソル a,b,c:現在の竹の長さ
    if cursor == N:  # cursorが最後まで行ったら終了する。
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    # abs(a - A) + abs(b - B) + abs(c - C) - 30 でなぜ30を減じているのかというと、
    # dfs(0,0,0)
    # dfs(0,0,0,0)で始まる以上、最初に選ばれるa,b,cを決定する際にもコストが10増加してしまうからである。
    # また、全探索を行う中で、a,b,cの初期値が0,0,0である以上、a,b,cのどれかが0のまま終了する場合が存在する。
    # この場合はa,b,cに竹が対応してないと言えるため、解にはならない、そこで三項演算子を利用して
    # その場合についてコストをINFとしている

    # 以下は4**Nで展開される再起処理となる。
    # カーソルの当たっている竹に対して、(A or B or Cに合成する) or (合成しない)の場合に分ける
    no_compound_pattern = dfs(cursor, a, b, c)
    compound_a_pattern = dfs(cursor, a + L[cursor], b, c)
    compound_b_pattern = dfs(cursor, a, b + L[cursor], c)
    compound_c_pattern = dfs(cursor, a, b, c + L[cursor])

    # 結果的に以下の値が返るのはそれぞれのパターンのコストが決定されてからなので
    # 以下のコードは最終的なコストの最小値
    return min(no_compound_pattern, compound_a_pattern, compound_b_pattern, compound_c_pattern)


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))
#example answer

Submission status: Runtime Error

Solution code:
def main():
    minmp = 10000000
    def dfs(depth, pt):

        if depth==n:
            mp_tmp = 0
            all_used = True
            for k in range(3):
                if pt.count(k) == 0:
                    all_used = False
                    break

                sum_tmp = 0
                mp_tmp += (pt.count(k) - 1)*10
                for l in range(n):
                    if pt[l] == k:
                        sum_tmp += moto[l]
                
                mp_tmp += abs(sum_tmp-take[k])

            if all_used == True:
                global minmp
                minmp = min(minmp,mp_tmp)
                return
            else:
                return

        for i in range(4):
            pt[depth] = i
            dfs(depth+1, pt)

        return

    #入力をとる
    n,a,b,c = map(int,input().split())
    moto = [int(input()) for i in range(n)]


    take = []
    take.append(a)
    take.append(b)
    take.append(c)

    pt  = [3]*n
    for i in range(n):
        dfs(i,pt)

    print(minmp)

main()

Submission status: Runtime Error

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

takes = takes+[0,0,0,0,0,0,0]
takes = takes[:8]
g = [A, B, C]
combis = []
for i in range(4):
    for j in range(4):
        for k in range(4):
            for l in range(4):
                for m in range(4):
                    for n in range(4):
                        for o in range(4):
                            for p in range(4):
                                combis.append([i,j,k,l,m,n,o,p])
min_diff = 10**10
for combi in combis:
    A,B,C = 0,0,0
    for idx, el in enumerate(combi):
        if el == 0:
            A += takes[idx]
        elif el == 1:
            B += takes[idx]
        elif el == 2:
            C += takes[idx]

        if A == 0 or B == 0 or C == 0:
            continue

        A_diff = abs(g[0] - A) + (len([_ for _ in combi if _ == 0]) - 1)*10
        B_diff = abs(g[1] - B) + (len([_ for _ in combi if _ == 1]) - 1)*10
        C_diff = abs(g[2] - C) + (len([_ for _ in combi if _ == 2]) - 1)*10
        diff = A_diff + B_diff + C_diff
        min_diff = min(diff, min_diff)

print(min_diff)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
x = list(map(int, input().split()))
mintime = 10**20
for i in range(n-k+1):
    left = x[i]
    right = x[i+k-1]
    abss = (abs(left), abs(right))
    if left * right >= 0:
        mintime = min(mintime, max(abss))
    else:
        mintime = min(mintime, min(abss) * 2 + max(abss))
print(mintime)

Submission status: Runtime Error

Solution code:
import sys
from bisect import bisect_right
input = sys.stdin.readline
A, B, Q = map(int, input().split())
INF = 10 ** 18
s = [-INF] + [int(input()) for i in range(A)] + [INF]
t = [-INF] + [int(input()) for i in range(B)] + [INF]
xs = [int(input()) for i in range(Q)]
for x in xs:
    b, d = bisect_right(s, x), bisect_right(t, x)
    res = INF
    for S in [s[b - 1], s[b]]:
        for T in [t[d - 1], t[d]]:
            d1, d2 = abs(S - x) + abs(T - S), abs(T - x) + abs(S - T)
            res = min(res, d1, d2)
    print(res)

Submission status: Runtime Error

Solution code:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <climits>
#include <numeric>
#include<stdio.h>
#include <queue>
#include <iomanip>
#include <float.h>
#include <set>
#include<functional>
#include <stack>
#include <time.h>
#include <climits>
#include <bitset>
#include <cmath>
using namespace std;
long long gcd(long long a, long long b) {
	if (a < b) gcd(b, a);
	long long  r;
	while ((r = a%b)) {
		a = b;
		b = r;
	}
	return b;
}
long long lcm(long long a, long long b) {
	return a / gcd(a, b) * b;
}
long long modpow(long long a, long long n, long long mod) {
	long long res = 1;
	while (n > 0) {
		if (n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res;
}

long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while (b) {
		long long t = a / b;
		a -= t*b; swap(a, b);
		u -= t*v; swap(u, v);
	}
	u %= mod;
	if (u < 0) u += mod;
	return u;
}
inline long long mod(long long a, long long m) {
	return (a % m + m) % m;
}

// 拡張 Euclid の互除法
// ap + bq = gcd(a, b) となる (p, q) を求め、d = gcd(a, b) をリターンします
long long extGcd(long long a, long long b, long long &p, long long &q) {
	if (b == 0) { p = 1; q = 0; return a; }
	long long d = extGcd(b, a%b, q, p);
	q -= a / b * p;
	return d;
}

// 中国剰余定理
// リターン値を (r, m) とすると解は x ≡ r (mod. m)
// 解なしの場合は (0, -1) をリターン
//amari,mod,amari,mod
pair<long long, long long> ChineseRem(long long b1, long long m1, long long b2, long long m2) {
	long long p, q;
	long long d = extGcd(m1, m2, p, q); // p is inv of m1/d (mod. m2/d)
	if ((b2 - b1) % d != 0) return make_pair(0, -1);
	long long m = m1 * (m2 / d); // lcm of (m1, m2)
	long long tmp = (b2 - b1) / d * p % (m2 / d);
	long long r = mod(b1 + m1 * tmp, m);
	return make_pair(r, m);
}
typedef pair<int, int>p;
int n, a, b, c;
int l[10];
int num(int x) {
	int ans = 0;
	while (x > 1) {
		ans += x / 2;
		x = (x + 1) / 2;
	}
	return ans;
}
int dfs(int x, int y, int z, int now,int numx,int numy,int numz) {
	if (now == n) {
		if (numx == 0 || numy == 0 || numz == 0)return INT_MAX;
		return abs(x - a) + abs(y - b) + abs(z - c) + num(numx)*10 + num(numy)*10+num(numz)*10;
	}
	else {
		return min(min(dfs(x + l[now], y, z, now + 1,numx+1,numy,numz), dfs(x, y + l[now], z, now + 1, numx, numy+1, numz)), min(dfs(x, y, z + l[now],now+1, numx, numy, numz+1), dfs(x, y, z, now + 1,numx,numy,numz)));
	}
}
signed main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);
	cin >> n >> a >> b >> c;
	for (int i = 0; i < n; i++) {
		cin >> l[i];
	}
	cout << dfs(0, 0, 0, 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()))
INF=100000

def mp(j,a,b,c):
    if j==N:
      return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else INF
    ret0=mp(j+1,a,b,c)
    ret1=mp(j+1,a+l[j],b,c)+10
    ret2=mp(j+1,a,b+l[j],c)+10
    ret3=mp(j+1,a,b,c+l[j])+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())
ans = [[0, 0, 0, 0]]
for i in range(N): ans = [[p[j] + int(input()) * (i == j) for j in [0, 1, 2]] + [p[3]+(i < 3)] for p in ans for i in range(4)]
print(min(abs(p[0] - A) + abs(p[1] - B) + abs(p[2] - C) + p[3] * 10 - 30 for p in ans if min(p[:3]) > 0))

Submission status: Runtime Error

Solution code:
# g = [1000, 800, 100]
#takes = [300, 333, 400, 444, 500, 555, 600, 666]
#g = [100, 90, 80]
#takes = [100, 100, 90,90,90,80,80,80]
#  g = [100,90,80]
#  takes = [98, 40, 30, 21, 80, 0,0,0]
N,A,B,C = map(int, input().split())
g=[A, B, C]
takes =[]
for i in range(N):
    takes.append(int(input())
combis = []
for i in range(4):
    for j in rang e(4):
        for k in range(4):
            for l in range(4):
                for m in range(4):
                    for n in range(4):
                        for o in range(4):
                            for p in range(4):
                                #  print(i,j,k,l,m,n,o,p)
                                combis.append([i,j,k,l,m,n,o,p])
#  print(combis)
min_diff = 10**10
for combi in combis:
    A,B,C = 0,0,0
    for idx, el in enumerate(combi):
        if el == 0:
            A += takes[idx]
        elif el == 1:
            B += takes[idx]
        elif el == 2:
            C += takes[idx]

        if A == 0 or B == 0 or C == 0:
            continue

        A_diff = abs(g[0] - A) + (len([_ for _ in combi if _ == 0]) - 1)*10
        B_diff = abs(g[1] - B) + (len([_ for _ in combi if _ == 1]) - 1)*10
        C_diff = abs(g[2] - C) + (len([_ for _ in combi if _ == 2]) - 1)*10
        diff = A_diff + B_diff + C_diff
        min_diff = min(diff, min_diff)

print(minq_diff)

Submission status: Runtime Error

Solution code:
# g = [1000, 800, 100]
#takes = [300, 333, 400, 444, 500, 555, 600, 666]
#g = [100, 90, 80]
#takes = [100, 100, 90,90,90,80,80,80]
#  g = [100,90,80]
#  takes = [98, 40, 30, 21, 80, 0,0,0]
N,A,B,C = [int(input()) for i in range(0,4)]
g=[A, B, C]
takes =[]
for i in range(N):
    takes.append(int(input())
combis = []
for i in range(4):
    for j in range(4):
        for k in range(4):
            for l in range(4):
                for m in range(4):
                    for n in range(4):
                        for o in range(4):
                            for p in range(4):
                                #  print(i,j,k,l,m,n,o,p)
                                combis.append([i,j,k,l,m,n,o,p])
#  print(combis)
min_diff = 10**10
for combi in combis:
    A,B,C = 0,0,0
    for idx, el in enumerate(combi):
        if el == 0:
            A += takes[idx]
        elif el == 1:
            B += takes[idx]
        elif el == 2:
            C += takes[idx]

        if A == 0 or B == 0 or C == 0:
            continue

        A_diff = abs(g[0] - A) + (len([_ for _ in combi if _ == 0]) - 1)*10
        B_diff = abs(g[1] - B) + (len([_ for _ in combi if _ == 1]) - 1)*10
        C_diff = abs(g[2] - C) + (len([_ for _ in combi if _ == 2]) - 1)*10
        diff = A_diff + B_diff + C_diff
        min_diff = min(diff, min_diff)

print(minq_diff)

Submission status: Runtime Error

Solution code:
from sys import stdin

n, a, b, c = [int(x) for x in stdin.readline().rstrip().split()]
l = []
for _ in range(n):
    l.append(int(stdin.readline().rstrip()))
add = n - 3
ans = float('inf')
for i in range(3, n + 1):
    for i_a in range(1, i - 1):
        for i_b in range(1, i -  i_a):
            for i_c in range(1, i - i_a - i_b):
                for j in range(2 ** n):
                    j = bin(j)
                    if srt(j).count('1') == i_a:
                        lu = [l[x] if (j and (1 << x)) for x in range(n)]
                        c_a = (i_a - 1) * 10 + abs(sum(lu) - a)
                        for k in range(2 ** n):
                            k = bin(k)
                            if str(k).count('1') == i_b and str((k and j)).count('1') == 0:
                                lu = [l[x] if (k and (1 << x)) for x in range(n)]
                                c_b = (i_b - 1) * 10 + abs(sum(lu) - b)
                                for m in range(2 ** n):
                                    m = bin(m)
                                    if str(m).count('1') == i_c and str((k and j and m)).count('1') == 0:
                                        lu = [l[x] if (m and (1 << x)) for x in range(n)]
                                        c_c = (i_c - 1) * 10 + abs(sum(lu) - c)
                                        cost = a_c + b_c + c_c
                                        if ans > cost:
                                            ans = cost
print(int(ans))

Submission status: Runtime Error

Solution code:
def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

n,a,b,c=map(int,input().split())
L=[]
for _ in range(n):
    L.append(int(input()))

ans=99999
for i in range(4**len(L)+1):
    i_4_list=list(Base_10_to_n(i, 4).zfill(len(L)))
    at,bt,ct=[],[],[]
    for num,i_4 in enumerate(i_4_list):
        if i_4=='0':
            at.append(L[num])
        elif i_4=='1':
            bt.append(L[num])
        elif i_4=='2':
            ct.append(L[num])
        else:
            continue
    if min(len(at),len(bt),len(ct))>0:
        al=0
        al+=(len(at)+len(bt)+len(ct)-3)*10
        al+=(abs(sum(at)-a)+abs(sum(bt)-b)+abs(sum(ct)-c))
        if al < ans:
            ans=al
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) if min(a,b,c) > 0 else return 10**8
  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 math

N, A, B, C = map(int, input().split())  # 複数数値入力
L = [0] * N
for i in range(N):
    L[i] = int(input())


def dfs(depth, a, b, c):    # 深さは何本目の竹まで場合分けしたかを示す  a,b,cにはA,B,Cに割り当てられている竹の長さの和が入っている　戻り値はMP
    if depth == N:
        # ABCのどれかが1本も割り当てられていない場合はだめなのでINFを返す
        return abs(a-A)+abs(b-B)+abs(c-C) - 30 if min(a, b, c) > 0 else math.inf

    ret0 = dfs(depth + 1, a, b, c)  # depth本目の竹を使わない
    # depth本目の竹をAを作るのに使用する    コストを足して、長さには使用する竹の長さを足してやる
    ret1 = dfs(depth + 1, a + L[depth], b, c) + 10
    ret2 = dfs(depth + 1, a, b + L[depth], c) + 10  # depth本目の竹をBを作るのに使用する
    ret3 = dfs(depth + 1, a, b, c + L[depth]) + 10  # depth本目の竹をCを作るのに使用する

    return min(ret0, ret1, ret2, ret3)  # 一番小さいMPを返す


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

Submission status: Runtime Error

Solution code:
from sys import stdin
import itertools
N,A,B,C=[int(x) for x in stdin.readline().rstrip().split()]
data=[]

for i in range(N):
    data.append(int(input()))
target=[A,B,C]

ans=0

if A in data:
    data.remove(A)
    target.remove(A)

if B in data:
    data.remove(B)
    target.remove(B)
    
if C in data:
    data.remove(C)
    target.remove(C)

if target==[]:
    print(ans)

else:
    for k in sorted(target):
        sa=float("inf")
        for l in range(1,len(data)):
            for j in itertools.combinations(data,l):
                if abs(k-sum(j)) < sa:
                    sa= abs(k-sum(j))
                    ansj=j
        if len(ansj)==1:
            ans+=sa
        else:
            ans=ans+sa+10*(len(ansj)-1)
        for m in ansj:
            data.remove(m)
    print(ans)

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().spilt())

ko = [int(input()) for i in range(n)]
INF = 10**9

 def dfs(q,w,e,r):
 	if q == n:
 		 return abs(a-w) + abs(b-e) + abs(c-r) - 30 if min(a,b,c) > 0 else INF
 	re0 = dfs(q+1,w,e,r)
 	re1 = dfs(q+1,w+ko[q],e,r) + 10
 	re2 = dfs(q+1,w,e+ko[ko],r) + 10
 	re3 = dfs(q+1,w,e,r+ko[q]) + 10
 	return min(re0,re1,re2,re3)

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

Submission status: Runtime Error

Solution code:
n,a,b,c = int(input().spilt())

ko = [int(input()) for i in range(n)]
INF = 10**9

 def dfs(q,w,e,r):
 	if q == n:
 		 return abs(a-w) + abs(b-e) + abs(c-r) - 30 if min(a,b,c) > 0 else INF
 	re0 = dfs(q+1,w,e,r)
 	re1 = dfs(q+1,w+ko[q],e,r) + 10
 	re2 = dfs(q+1,w,e+ko[ko],r) + 10
 	re3 = dfs(q+1,w,e,r+ko[q]) + 10
 	return min(re0,re1,re2,re3)

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