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: Accepted

Solution code:
# coding: utf-8
# Your code here!

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

ans = 10000000
for i in range(1,2**n):
    for j in range(1,2**n):
        for k in range(1,2**n):
            cost=0
            if j&k != 0 or i&k != 0 or i&j!=0:
                continue
            L = [l[ii] for ii in range(n) if (2**ii)&k !=0]
            ck = 10*(len(L)-1) + abs(c-sum(L))  
            cost += ck
            LL = [l[ii] for ii in range(n) if (2**ii)&j !=0]
            bk = 10*(len(LL)-1) + abs(b-sum(LL))
            cost += bk
            LLL = [l[ii] for ii in range(n) if (2**ii)&i !=0]
            ak = 10*(len(LLL)-1) + abs(a-sum(LLL))
            cost += ak
#            print(cost,ak,bk,ck,i,j,k,L)
            ans = (ans if ans<cost else cost)

print(ans)

Submission status: Accepted

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

from itertools import combinations

def combi(xs, left):
    for i in range(len(xs) - left):
        for r in combinations(xs, i + 1):
            yield r

def diff(a, b):
    result = a[:]
    for x in b:
        if x in result:
            result.remove(x)
    return result

def calc(xs):
    result = 10**100
    for ra in combi(xs, 2):
        sa = abs(A - sum(ra)) + len(ra) * 10 - 10
        xsa = diff(xs, ra)
        for rb in combi(xsa, 1):
            sb = abs(B - sum(rb)) + len(rb) * 10 - 10
            xsb = diff(xsa, rb)
            for rc in combi(xsb, 0):
                sc = abs(C - sum(rc)) + len(rc) * 10 - 10
                total = sa + sb + sc
                result = min(result, total)
    return result

print(calc(L))

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
D = [A, B, C]
L = [0] * N
for i in range(N):
  L[i] = int(input())
MP = 3000
for i in range(4 ** N):
  E = [0, 0, 0]
  mp = 0
  for j in range(N):
    n = i % 4
    if n < 3:
      mp += 10 if E[n] else 0
      E[n] += L[j]
    i >>= 2
  for i in range(3):
    mp += abs(D[i] - E[i]) if E[i] else 3000
  MP = min(mp, MP)
print(MP)

Submission status: Accepted

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

ans = float("inf")

for i in range(4 ** N):
    temp = i
    mp = -30
    a = 0
    b = 0
    c = 0
    for p in range(N):
        if temp % 4 == 0:
            a += l[p]
            mp += 10
        elif temp % 4 == 1:
            b += l[p]
            mp += 10
        elif temp % 4 == 2:
            c += l[p]
            mp += 10
        
        temp //= 4

    if a == 0 or b == 0 or c == 0:
        continue

    mp += (abs(A - a) + abs(B - b) + abs(C - c))
    if mp < ans:
        ans = mp

print(ans)

Submission status: Accepted

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

import itertools

#n桁のm値のbitを生成
def bit(n, m):
    bit_list = list(itertools.product([i for i in range(m)], repeat=n))
    return bit_list

bit = bit(N, 4)

for b in bit:
    tmp1, tmp2, tmp3, cost = 0, 0, 0, 0
    x, y, z = 0, 0, 0
    for i in range(N):
        if b[i] == 1:
            tmp1 += L[i]
            if x != 0:
                cost += 10
            x += 1
            
        if b[i] == 2:
            tmp2 += L[i]
            if y != 0:
                cost += 10
            y += 1
            
        if b[i] == 3:
            tmp3 += L[i]
            if z != 0:
                cost+= 10
            z += 1
    if x > 0 and y > 0 and z > 0:        
        res = min(abs(A-tmp1)+abs(B-tmp2)+abs(C-tmp3)+cost, res)

print(res)

Submission status: Accepted

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 = (int(i) for i in input().split())
T = []
for i in range(N):
    T.append(int(input()))
res = float("inf")
for i in range(4**N):
    L = [0] * N
    cc = i
    ans = 0
    for j in range(N):
        L[j] = cc//4**(N-1-j)
        cc %= 4**(N-1-j)
    a = b = c = 0
    if 1 not in L or 2 not in L or 3 not in L:
        continue
    for j in range(N):
        if L[j] == 1:
            if a != 0:
                ans += 10
            a += T[j]
        elif L[j] == 2:
            if b != 0:
                ans += 10
            b += T[j]
        elif L[j] == 3:
            if c != 0:
                ans += 10
            c += T[j]
    ans += abs(A-a) + abs(B-b) + abs(C-c)
    res = min(res, ans)
print(res)

Submission status: Accepted

Solution code:
import itertools

def powerset(seq, m):
	return [ list(u) for u in itertools.product(*([seq]*m) ) ]

def cnt(seq,v):
	return len([x for x in seq if x==v])

def main():
	N,a,b,c = map(int, raw_input().split())
	ls = [input() for _ in range(N)]
	res = 10**9
	for vs in powerset([0,1,2,3], N):
		aa = []
		bb = []
		cc = []
		for i in range(N):
			if vs[i]==0:aa.append(ls[i])
			elif vs[i]==1:bb.append(ls[i])
			elif vs[i]==2:cc.append(ls[i])
			else:
				assert vs[i]==3
		if len(aa)==0 or len(bb)==0 or len(cc)==0:continue
		res=min(res, abs(a-sum(aa))+abs(b-sum(bb))+abs(c-sum(cc))+10*(len(aa)-1 + len(bb)-1 + len(cc)-1)  )
	print res
	
	return 0

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from itertools import product
n,a,b,c = map(int, input().split())
ll = [int(input()) for _ in range(n)]

res = 10**10
for t in product([0,1,2,3], repeat=n):
    x = [0,0,0]
    y = [0,0,0]
    for i in range(len(t)):
        if t[i]==3:
            continue
        x[t[i]] += ll[i]
        y[t[i]] += 1
    if min(x)==0:
        continue
    for i in range(3):
        if y[i]>=2:
            y[i] -= 1
        elif y[i]==1:
            y[i] = 0

    x.sort()
    tmp = abs(a-x[2])+abs(b-x[1])+abs(c-x[0])+10*sum(y)
    res = min(res, tmp)

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import itertools
def solve(n, a, b, c, l):
    answer = float('inf')
    for p in itertools.product(range(4), repeat=n):
        sum_l = [ 0, 0, 0, 0 ]
        cnt = [ 0, 0, 0, 0 ]
        for i in range(n):
            sum_l[p[i]] += l[i]
            cnt[p[i]] += 1
        if cnt[0] and cnt[1] and cnt[2]:
            cost = 10 * (cnt[0] + cnt[1] + cnt[2] - 3) + abs(sum_l[0] - a) + abs(sum_l[1] - b) + abs(sum_l[2] - c)
            answer = min(answer, cost)
    return answer

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

Submission status: Accepted

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

ptns = [L]

ans = float('inf')

for k in range(N-2):
    _ptns = []
    for ptn in ptns:
        for comb in itertools.combinations(ptn,3):
            comb = list(comb)
            comb.sort()
            tmp = abs(comb[0] - C)
            tmp += abs(comb[1] - B)
            tmp += abs(comb[2] - A)
            ans = min(ans, tmp + k*10)
        for i,j in itertools.combinations(range(len(ptn)),2):
            tmps = [ptn[i] + ptn[j]]
            for l,a in enumerate(ptn):
                if l==i or l==j: continue
                tmps.append(a)
            _ptns.append(tmps)
    ptns = _ptns
print(ans)

Submission status: Accepted

Solution code:
#! usr/bin/env python
# -*- coding: utf-8 -*-

from collections import deque
import heapq
import math
import bisect


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

    ans = 10 ** 9
    for i in range(4**N):
        l = [0, 0, 0]
        cur = 0
        for j in range(N):
            idx = i // (4**j) % 4 - 1
            if idx >= 0:
                if l[idx]:
                    cur += 10
                l[idx] += L[j]

        for j in range(3):
            if l[j] == 0:
                cur += 10 ** 9
            cur += abs(T[j]-l[j])

        ans = min(ans, cur)

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
l = [int(input()) for i in range(n)]

ans = 100000000000000
for i1 in range(4):
  for i2 in range(4):
    for i3 in range(4):
      for i4 in range(4):
        for i5 in range(4):
          for i6 in range(4):
            for i7 in range(4):
              for i8 in range(4):
                tmpa = 0
                tmpb = 0
                tmpc = 0
                counta=0
                countb=0
                countc=0
                if len(l)>=1:
                  if i1 ==0:
                    tmpa += l[0]
                    counta +=1
                  if i1 ==1:
                    tmpb += l[0]
                    countb +=1
                  if i1 ==2:
                    tmpc += l[0]
                    countc +=1
                if(len(l)>=2) :                 
                  if i2 ==0:
                    tmpa += l[1]
                    counta +=1
                  if i2 ==1:
                    tmpb += l[1]
                    countb +=1
                  if i2 ==2:
                    tmpc += l[1]
                    countc +=1
                if(len(l)>=3) :                 
                  if i3 ==0:
                    tmpa += l[2]
                    counta +=1
                  if i3 ==1:
                    tmpb += l[2]
                    countb +=1
                  if i3 ==2:
                    tmpc += l[2]
                    countc +=1
                if(len(l)>=4):               
                  if i4 ==0:
                    tmpa += l[3]
                    counta +=1
                  if i4 ==1:
                    tmpb += l[3]
                    countb +=1
                  if i4 ==2:
                    tmpc += l[3]  
                    countc +=1
                if(len(l)>=5) :                 
                  if i5 ==0:
                    tmpa += l[4]
                    counta +=1
                  if i5 ==1:
                    tmpb += l[4]
                    countb +=1
                  if i5 ==2:
                    tmpc += l[4]
                    countc +=1
                if(len(l)>=6):                  
                  if i6 ==0:
                    tmpa += l[5]
                    counta +=1
                  if i6 ==1:
                    tmpb += l[5]
                    countb +=1
                  if i6 ==2:
                    tmpc += l[5]
                    countc +=1
                if(len(l)>=7) :                 
                  if i7 ==0:
                    tmpa += l[6]
                    counta +=1
                  if i7 ==1:
                    tmpb += l[6]
                    countb +=1
                  if i7 ==2:
                    tmpc += l[6]
                    countc +=1
                if(len(l)>=8):                  
                  if i8 ==0:
                    tmpa += l[7]
                    counta +=1
                  if i8 ==1:
                    tmpb += l[7]
                    countb +=1
                  if i8 ==2:
                    tmpc += l[7]
                    countc +=1
                if counta>=1 and countb>=1 and countc>=1:
                   ans = min(ans,(counta-1)*(10)+(countb-1)*(10)+(countc-1)*(10)+abs(tmpa-a)+abs(tmpb-b)+abs(tmpc-c))
print(ans)

Submission status: Accepted

Solution code:
import sys
from itertools import combinations


fin = sys.stdin.readline
N, A, B, C = [int(elem) for elem in fin().split()]
l_list = sorted([int(fin()) for _ in range(N)], reverse=True)

min_mp = float('inf')
for num_conc_A in range(1, N - 1):
    # mp = 0
    rest_indices = set(range(N))
    for A_indices in combinations(rest_indices, num_conc_A):
        mp_A = 10 * (num_conc_A - 1) + abs(sum(l_list[i] for i in A_indices) - A)
        rest_indices -= set(A_indices)
        for num_conc_B in range(1, N - num_conc_A):
            for B_indices in combinations(rest_indices, num_conc_B):
                mp_B = 10 * (num_conc_B - 1) + abs(sum(l_list[i] for i in B_indices) - B)
                rest_indices -= set(B_indices)
                for num_conc_C in range(1, N - num_conc_A - num_conc_B + 1):
                    for C_indices in combinations(rest_indices, num_conc_C):
                        mp_C = 10 * (num_conc_C - 1) + abs(sum(l_list[i] for i in C_indices) - C)
                        min_mp = min(min_mp, mp_A + mp_B + mp_C)
                rest_indices.update(set(B_indices))
        rest_indices.update(set(A_indices))
print(min_mp)

Submission status: Accepted

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

def rec(Y):
    if len(Y) == 0:
        return [(0, 0, 0, 0)]
    
    y = Y.pop()
    tmp = rec(Y)
    return [(t[0]+y, t[1], t[2], t[3]+(10 if t[0]>0 else 0)) for t in tmp] + [(t[0], t[1]+y, t[2], t[3]+(10 if t[1]>0 else 0)) for t in tmp] + [(t[0], t[1], t[2]+y, t[3]+(10 if t[2]>0 else 0)) for t in tmp] + [(t[0], t[1], t[2], t[3]) for t in tmp]


X = rec([int(input()) for i in range(N)])
mi = 10**20
for a, b, c, mp in X:
    if a>0 and b>0 and c>0:
        mi = min(mi, abs(A-a) + abs(B-b) + abs(C-c) + mp)
    
print(mi)

Submission status: Accepted

Solution code:
A = [0,0,0]
N, A[0], A[1], A[2] = map(int, input().split())
l = []
for i in range(N):
    l.append(int(input()))
ans = 9999
for i in range(4 ** N):
    d = [[] for i in range(4)]
    j = i
    for k in range(N):
        d[j % 4].append(k)
        j //= 4
    mp = 0
    for j in range(3):
        if len(d[j]) == 0:
            break
        mp += abs(sum([l[k] for k in d[j]]) - A[j]) + 10 * (len(d[j]) - 1)
    else:
        ans = min(ans, mp)
print(ans)

Submission status: Accepted

Solution code:
N, A, B, C = list(map(int, input().split()))
l = []
for i in range(N):
  l.append(int(input()))
  
Ans = 1e15
for i in range(pow(4,N)):
  L = [0 for j in range(N)]
  t = i
  for j in range(N):
    L[j] = t % 4
    t //= 4
  if 1 not in L:
    continue
  if 2 not in L:
    continue
  if 3 not in L:
    continue

  a = 0
  b = 0
  c = 0
  Num = 0
  for j in range(N):
    if L[j] == 1:
      a += l[j]
      Num += 1
    elif L[j] == 2:
      b += l[j]
      Num += 1
    elif L[j] == 3:
      c += l[j]
      Num += 1

  k = abs(A - a) + abs(B - b) + abs(C - c) + (Num - 3) * 10
  if k < Ans:
    Ans = k

print(Ans)

Submission status: Accepted

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

def dfs(idx, a, b, c):

    if idx == N:
        if a == 0 or b == 0 or c == 0:
            return 1e9
        
        return abs(A - a) + abs(B - b) + abs(C - c)
    
    x = dfs(idx + 1, a, b, c)
    y = dfs(idx + 1, a + L[idx], b, c) + 10*(a != 0)
    z = dfs(idx + 1, a, b + L[idx], c) + 10*(b != 0)
    w = dfs(idx + 1, a, b, c + L[idx]) + 10*(c != 0)
    return min(x, y, z, w);

ans = dfs(0, 0, 0, 0);
print(ans)

Submission status: Accepted

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

F=[]
ans=100000000
for i in range(4**n):
    ii=i
    f=""
    for j in range(n-1,-1,-1):
        f+=str(ii//(4**j))
        ii%=(4**j)
    F.append(f)

#print(F)

for i in range(len(F)):
    p=F[i].count("1")
    q=F[i].count("2")
    r=F[i].count("3")
    if p*q*r>0 :
        aa=[0,0,0,0]
        for j in range(n):
            aa[int(F[i][j])]+=L[j]
        ans2=abs(aa[1]-a)+abs(aa[2]-b)+abs(aa[3]-c)+10*(p+q+r-3)
        ans=min(ans,ans2)
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
import sys

N, A, B, C = map(int, input().split())
Ls = [None] * N
for n in range(N):
    Ls[n] = int(input())

ans = 1e9
group = [0, 1, 2, 3]
for comb in product(group, repeat=N):
    c = Counter()
    tmp = 0
    for i, group_idx in enumerate(comb):
        if group_idx == 3:
            continue
        if c[group_idx] != 0:
            tmp += 10
        c[group_idx] += Ls[i]
    if c[0] == 0 or c[1] == 0 or c[2] == 0:
        continue
    tmp += abs(c[0] - A)
    tmp += abs(c[1] - B)
    tmp += abs(c[2] - C)
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

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

ans = 10 ** 9
for i in range(4 ** n):
    ta, tb, tc = 0, 0, 0
    tmp = 0
    for j in range(n):
        k = (i >> (2 * j)) & 3
        if k == 1:
            if ta > 0:
                tmp += 10
            ta += lll[j]
        elif k == 2:
            if tb > 0:
                tmp += 10
            tb += lll[j]
        elif k == 3:
            if tc > 0:
                tmp += 10
            tc += lll[j]
    if ta == 0 or tb == 0 or tc == 0:
        continue
    tmp += abs(a - ta) + abs(b - tb) + abs(c - tc)
    ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
N,A,B,C = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 1e9
a = list()
b = list()
c = list()
for i in range(1<<N):
	for j in range(1<<N):
		res = 0
		a.clear()
		b.clear()
		c.clear()
		for k in range(N):
			if i&(1<<k):
				if j&(1<<k):
					a.append(L[k])
				else:
					b.append(L[k])
			elif j&(1<<k):
				c.append(L[k])
		if len(a)==0 or len(b)==0 or len(c)==0:
			continue
		res += 10*(len(a)-1)+abs(A-sum(a))+10*(len(b)-1)+abs(B-sum(b))+10*(len(c)-1)+abs(C-sum(c))
		ans = min(ans,res)

print(ans)

Submission status: Accepted

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

def cost(X, x):
  t = 0
  c = 0
  for i in range(N):
    if x>>i&1:
      t += l[i]
      c += 1
  return (c-1)*10 + abs(X-t)

ans = 10000
for a in range(1, 1<<N):
  ac = cost(A, a)
  for b in range(1, 1<<N):
    if (a&b)==0:
      bc = cost(B, b)
      for c in range(1, 1<<N):
        if ((a|b)&c)==0:
          cc = cost(C, c)
          ans = min(ans, ac+bc+cc)
print ans

Submission status: Accepted

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

T = [0, 1, 2, 3]
E = [0]*N
def dfs(i):
    if i == N:
        return solve()
    r = 10**18
    for k in T:
        E[i] = k
        r = min(r, dfs(i+1))
    return r
def solve():
    a = b = c = 0
    p = q = r = 0
    for i in range(N):
        if E[i] == 1:
            a += L[i]
            p += 1
        elif E[i] == 2:
            b += L[i]
            q += 1
        elif E[i] == 3:
            c += L[i]
            r += 1
    if not p or not q or not r:
        return 10**18
    return (p+q+r-3)*10 + abs(A - a) + abs(B - b) + abs(C - c)
print(dfs(0))

Submission status: Runtime Error

Solution code:
N,A,B,C=map(int,input().split())
L=[int(input()) for i in range(N)]
R=[0]*N
ans=10**9
def myadd(i):
    if R[i]==3:
        R[i]=0
        myadd(i+1)
    else:
        R[i]+=1
for i in range(N**4):
    myadd(0)
    if 1 in R and 2 in R and 3 in R:
        tmp=-6
        t1,t2,t3=0,0,0
        for j in range(N):
            if R[j]==0:
                continue
            if R[j]==1:
                t1+=L[j]
            elif R[j]==2:
                t2+=L[j]
            elif R[j]==3:
                t3+=L[j]
            tmp+=2
        tmp+=abs(A-t1)+abs(B-t2)+abs(C-t3)
        ans=min(tmp,ans)
print(ans)

Submission status: Runtime Error

Solution code:
#全探索をA,B,C一度で分けた
# TLE   ac 

# 初期入力
import heapq
from itertools import combinations
import sys
input = sys.stdin.readline  #文字列では使わない
N,A,B,C = (int(i) for i in input().split())
l =[0]*N
for i in range(N):
    l[i] = int(input())

# A,B,Cどの竹を選ぶかcombination
mp=[]
for na in range(1,N-1):
    for ja in combinations(l,na):

        l_b =list(set(l) -set(ja))
        for nb in range(1,N):
            for jb in combinations(l_b ,nb):

                l_c =list(set(l_b) -set(jb) )
                for nc in range(1,N+1):
                    for jc in combinations(l_c ,nc):
                        
                        # MPの消費量＝（求める長さー今回選んだ竹の長さ合計）+10*（合成魔法の回数)
                        AA =abs(A-sum(ja)) +10*(na-1)
                        BB =abs(B-sum(jb)) +10*(nb-1)
                        CC =abs(C-sum(jc)) +10*(nc-1)
                        mp.append(AA +BB +CC)


print(min(mp))

Submission status: Runtime Error

Solution code:
n,a,b,c=map(int,input().split())
l=[int(input())for i in range(n)]
def dfs(i,x,y,z):
 if i==n:return abs(a-x)+abs(b-y)+abs(c-z)-30if a+b+c>=3else 10**9+7
 A,B,C,D=dfs(i+1,x,y,z),dfs(i+1,x+l[i],y,z)+10,dfs(i+1,x,y+l[i],z)+10,dfs(i+1,x,y,z+l[i])+10
 return min(A,B,C,D)
print(dfs(0,0,0,0)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**9)
Input = open('input.txt')
def input(): return Input.readline()
def mapint(): return map(int, input().split())

N, A, B, C = mapint()
Ls = [int(input()) for _ in range(N)]
ans = 10**18
def dfs(a, b, c, idx, magic):
    global ans
    if idx==N:
        if a==0 or b==0 or c==0:
            return
        magic -= 30
        magic += abs(A-a)
        magic += abs(B-b)
        magic += abs(C-c)
        ans = min(ans, magic)
        return
    nx = Ls[idx]
    dfs(a+nx, b, c, idx+1, magic+10)
    dfs(a, b+nx, c, idx+1, magic+10)
    dfs(a, b, c+nx, idx+1, magic+10)
    dfs(a, b, c, idx+1, magic)

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

Submission status: Runtime Error

Solution code:
#bit全探索の亜種(4種全探索)
#itertools.product([0,1,2,3],repeat=N)

import itertools
N,A,B,C = map(int,input().split())
l = [int(input()) for i in range(0,N,1)]

ans = 10**10
for ABCD in itertools.product([0,1,2,3],repeat=N):
    tmp = [[] for i in range(4)]
    for i in range(0,N,1):
        tmp[ABCD[i]].append(l[i])
    score = 0
    owaowari = False
    for i in range(3):#Dはくっ付ける必要なし
        if len(tmp[i])==0:
            owaowari = True
            break
        score+=10*max(len(tmp[i])-1,0)
    if owaowari: continue
    score+=abs(A-sum(tmp[0]))
    score+=abs(B-sum(tmp[1]))
    score+=abs(C-sum(tmp[2]))
    ans = min(ans,score)
    if score == 120:

print(ans)

Submission status: Runtime Error

Solution code:
# C MP消費を抑えるにはABCとの差が小さけれは良い
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]
INF = 10**9


def dfs(now, a, b, c):
    if now == n:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    # 方法１　１つ飛ばす
    way1 = dfs(now + 1, a, b, c)
    # 方法２　合成魔法(MP10)
    way2 = dfs(now + 1, a + l[now], b, c)+10
    way3 = dfs(now + 1, a, b + l[now], c)+10
    way4 = dfs(now + 1, a, b, c + l[now])+10
    return min(way1, way2, way3, way4)


print(dfs(0, 0, 0, 0))
# C MP消費を抑えるにはABCとの差が小さけれは良い
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]
INF = 10**9


def dfs(now, a, b, c):
    if now == n:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    # 方法１　１つ飛ばす
    way1 = dfs(now + 1, a, b, c)
    # 方法２　合成魔法(MP10)
    way2 = dfs(now + 1, a + l[now], b, c)+10
    way3 = dfs(now + 1, a, b + l[now], c)+10
    way4 = dfs(now + 1, a, b, c + l[now])+10
    return min(way1, way2, way3, way4)


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

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from operator import itemgetter

n, a, b, c, *L = map(int, open(0).read().split())
L.sort()
A = [a, b, c]
dicts = [defaultdict(lambda: 10**5) for _ in range(3)]
for i in range(n-2):
    for x in combinations(enumerate(L), i+1):
        k = tuple(i for i, l in x)
        v = sum(l for i, l in x)
        for d, x in zip(dicts, A):
            d[k] = min(d[k], abs(x-v) + 10*i)
scores = [sorted(d.items(), key=itemgetter(1)) for d in dicts]

ans = 10**5
for p1, p2, p3 in permutations(range(3)):
    k1, v1 = scores[p1][0]
    k1 = set(k1)
    for k2, v2 in scores[p2]:
        k2 = set(k2)
        if len(k1 & k2) == 0:
            break
    for k3, v3 in scores[p3]:
        k3 = set(k3)
        if len(k1 & k3) + len(k2 & k3) == 0:
            break
    ans = min(ans, sum([v1, v2, v3]))
print(ans)

Submission status: Runtime Error

Solution code:
n,a,b,c = int(input().split())
mp = 0
la = 0
lb = 0
lc = 0
for i in range(n):
    l = int(input().split())
    if abs(l[i] - a) < la:
        la = l[i]
    if abs(l[i] - b) < lb:
        lb = l[i]
    if abs(l[i] - c) < lc:
        lc = l[i]
af=0
bf=0
cf=0
for i in range(n):
    for j in (i,n):
        if abs(l[i]+l[j]-a) < la:
            la - l[i]+l[j]
            if af == 0:
                mp = mp+10
        if abs(l[i]+l[j]-b) < lb:
            lb = l[i]+l[j]
            if bf == 0:
                mp = mp+10
        if abs(l[i]+l[j]-c) < lc:
            lc = l[i]+l[j]
            if cf == 0:
                mp = mp+10

while la != a:
    if la < a:
        la = la+1
        mp = mp+1
    else:
        if la > 2:
            la = la-1
            mp = mp+1
while lb != b:
    if lb < b:
        lb = lb+1
        mp = mp+1
    else:
        if lb > 2:
            lb = lb-1
            mp = mp+1
while lc != c:
    if lc < c:
        lc = lc+1
        mp = mp+1
    else:
        if lc > 2:
            lc = lc-1
            mp = mp+1
print(mp)

Submission status: Runtime Error

Solution code:
n,a,b,c = int(input().split())
mp = 0
la = 0
lb = 0
lc = 0
for i in range(n):
    l = input().split()
    if abs(l[i] - a) < la:
        la = l[i]
    if abs(l[i] - b) < lb:
        lb = l[i]
    if abs(l[i] - c) < lc:
        lc = l[i]
af=0
bf=0
cf=0
for i in range(n):
    for j in (i,n):
        if abs(l[i]+l[j]-a) < la:
            la - l[i]+l[j]
            if af == 0:
                mp = mp+10
        if abs(l[i]+l[j]-b) < lb:
            lb = l[i]+l[j]
            if bf == 0:
                mp = mp+10
        if abs(l[i]+l[j]-c) < lc:
            lc = l[i]+l[j]
            if cf == 0:
                mp = mp+10

while la != a:
    if la < a:
        la = la+1
        mp = mp+1
    else:
        if la > 2:
            la = la-1
            mp = mp+1
while lb != b:
    if lb < b:
        lb = lb+1
        mp = mp+1
    else:
        if lb > 2:
            lb = lb-1
            mp = mp+1
while lc != c:
    if lc < c:
        lc = lc+1
        mp = mp+1
    else:
        if lc > 2:
            lc = lc-1
            mp = mp+1
print(mp)

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:
import itertools
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
ans=100000
for p in itertools.product(['A','B','C','Z'],repeat=n):
  if list(p).count('A')>=1 and list(p).count('B')>=1 and list(p).count('C')>=1:
    for i in range(n):
      la=0
      lb=0
      lc=0
      if p[i]=='A':
        la+=p[i]
      elif p[i]=='B':
        lb+=p[i]
      elif p[i]=='C':
        lc+=p[i]
      ans=min(ans,abs(la-a)+abs(lb-b)+abs(lc-c))
print(ans)

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:
        if min(a, b, c) > 0:
            return abs(a-A)+abs(b-B)+abs(c-C)-30
        else:
            return INF
    ret1 = 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(ret1 + ret2 + ret3 + ret4)

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

Submission status: Runtime Error

Solution code:
import itertools
A,B,C,n=map(int,input(),split())
l=[int(input()) for i in range(n)]
ll=[]
for p in itertools.product(['A','B','C','Z'].repeat=n):
  ans=0
  a=0
  b=0
  c=0
  for i in range(n):
    if p[i]=='A':
      a+=l[i]
    elif p[i]=='B':
      b+=l[i]
    elif p[i]=='C':
      c+=l[i]
  ans+=(abs(a-A)+abs(B-b)+abs(C-c))
  ll.append(ans)
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(cut, 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#-30は最初に選んだもの
    ret0 = dfs(cur + 1, a, b, c)#何も使わない
    ret1 = dfs(cur + 1, a + l[cur], b, c) + 10#Aに使うために合成するとして
    ret2 = dfs(cur + 1, a, b + l[cur], c) + 10#Bに使うために合成するとして
    ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10#Cに使うために合成するとして
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Runtime Error

Solution code:
import itertools

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

ans = float('inf')
for Lp in itertools.permutations(L):
    Lac = tuple(itertools.accumulate((0, *Lp)))
    for a in range(1, N - 1):
        for b in range(a + 1, N):
            for c in range(b + 1, N + 1):
                A_MP = 10 * (a - 1) + abs(Lac[a] - Lac[0] - A)
                B_MP = 10 * (b - a - 1) + abs(Lac[b] - Lac[a] - B)
                C_MP = 10 * (c - b - 1) + abs(Lac[c] - Lac[b] - C)
                ans = min(ans, A_MP + B_MP + C_MP)
print(ans)