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

Solution (Python):



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

            ans = (ans if ans<cost else cost)

print(ans)

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

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

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

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

import itertools


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)

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

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

Solution (Python):

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)

Solution (Python):

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))

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

Solution (Python):



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

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

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

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

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

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

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

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



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)

Solution (Python):



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)

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

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

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

Solution (Python):
import itertools
A=[0,0,0]
N,A[0],A[1],A[2]=map(int,input().split())
l=[int(input()) for i in range(N)]
ans=10**15
for seq in itertools.product(range(4),repeat=N):
    X=[0,0,0]
    Y=[0,0,0]
    for i in range(N):
        if seq[i]!=3:
            X[seq[i]]+=l[i]
            Y[seq[i]]+=1
    if Y[0]*Y[1]*Y[2]==0:
        continue
    MP=(sum(Y)-3)*10
    for i in range(3):
        MP+=abs(X[i]-A[i])
    if MP<ans:
        ans=MP
print(ans)

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