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,x,y,z=map(int,input().split())
l=[]
cmin=1000000000
for i in range(n):
  l.append(int(input()))
for i in range(4**n):
  l1=[]
  for j in range(n):
    if i>=4:
      l1.append(i%4)
      i=(i//4)
    elif i!=0:
      l1.append(i)
      i=0
    else:
      l1.append(i)
  c1=0
  c2=0
  c3=0
  d1=0
  d2=0
  d3=0
  l1=l1[::-1]
  for j in range(n):
    if l1[j]==0:
      pass
    elif l1[j]==1:
      c1+=l[j]
      d1+=1
    elif l1[j]==2:
      c2+=l[j]
      d2+=1
    else:
      c3+=l[j]
      d3+=1
  if c1*c2*c3!=0:
    c=abs(c1-x)+abs(c2-y)+abs(c3-z)+(d1+d2+d3-3)*10
    if c<=cmin:
      cmin=c
print(cmin)

Solution (Python):
N, A, B, C = map(int, input().split())
l = []

for i in range(N):
    l.append(int(input()))
    
def dfs(syn, n, a, b, c):
    if n == N:
        if a*b*c != 0:
            mp = abs(A-a) + abs(B-b) + abs(C-c)
            mp += syn * 10 - 30
            return mp
        else:
            return 10**9
    else:
        branch1 = dfs(syn+1, n+1, a + l[n], b, c)
        branch2 = dfs(syn+1, n+1, a, b + l[n], c)
        branch3 = dfs(syn+1, n+1, a, b, c + l[n])
        branch4 = dfs(syn, n+1, a, b, c)
        return min(branch1, branch2, branch3, branch4)
        
print(dfs(0, 0, 0, 0, 0))

Solution (Python):
import itertools

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

for i in range(N):
    l.append(int(input()))
    
prod = list(itertools.product(['A','B','C','X'], repeat=N))

mps = []

for prod_e in prod:
    if prod_e.count('A') * prod_e.count('B') * prod_e.count('C') != 0:
        mp = 0
        length = {'A':0, 'B':0, 'C':0, 'X':0}
        for index, prod_e_e in enumerate(prod_e):
            length[prod_e_e] += l[index]
        mp += abs(length['A'] - A) + abs(length['B'] - B) + abs(length['C'] - C)
        mp += (prod_e.count('A')-1) * 10 + (prod_e.count('B')-1) * 10 + (prod_e.count('C')-1) * 10
        mps.append(mp)

print(min(mps))

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

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


def select(next_idx, a=0, b=0, c=0, point=0):
  if next_idx == N:
    if a and b and c:
      tmp = abs(A - a) + abs(B - b) + abs(C - c)
      yield abs(A - a) + abs(B - b) + abs(C - c) + point - 30
  else:
    length = ls[next_idx]
    flag = length > 10
    next_idx += 1

    yield from select(next_idx, a=a, b=b, c=c, point=point)
    point += 10
    if a == 0 or flag: 
      yield from select(next_idx, a=a + length, b=b, c=c, point=point)
    if b == 0 or flag: 
      yield from select(next_idx, a=a, b=b + length, c=c, point=point)
    if c == 0 or flag: 
      yield from select(next_idx, a=a, b=b, c=c + length, point=point)



print(min(select(0)))

Solution (Python):
from itertools import product


def main():
    n, *target = map(int, input().split())
    arr = [int(input()) for i in range(n)]

    tests = product("0123", repeat=n)
    test_arr = [[] for _ in range(3)]
    min_cost = float("inf")

    for test in tests:
        cost = 0

        if "1" not in test or "2" not in test or "3" not in test:
            continue

        for i in range(3):
            test_arr[i] = [arr[x] for x in range(n) if test[x] == str(i + 1)]
            cost += (len(test_arr[i]) - 1) * 10
            cost += abs(sum(test_arr[i]) - target[i])

        if min_cost > cost:
            min_cost = cost

    print(min_cost)


main()

Solution (Python):
N, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(N)]
INF = float('inf')

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

    no_compound = dfs(cursor+1, a, b, c)
    A_compound = dfs(cursor+1, a+L[cursor], b, c) + 10
    B_compound = dfs(cursor+1, a, b+L[cursor], c) + 10
    C_compound = dfs(cursor+1, a, b, c+L[cursor]) + 10
    return min(no_compound, A_compound, B_compound, C_compound)

print(dfs())

Solution (Python):
from collections import deque

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

p = deque()
p.append([-1] * len(L))
while p:
    stats = p.pop()
    stats = list(stats)
    a, b, c = 0, 0, 0
    MP = 0
    cntA = 0; cntB = 0; cntC = 0
    for i in range(N):
        if stats[i] == 1:
            a += L[i]
            cntA += 1
        elif stats[i] == 2:
            b += L[i]
            cntB += 1
        elif stats[i] == 3:
            c += L[i]
            cntC += 1

    if cntA >= 1 and cntB >= 1 and cntC >= 1:
        MP += abs(A - a) + abs(B - b) + abs(C - c) + 10 * (cntA+cntB+cntC-3)
        minMP = min(minMP, MP)

    if not -1 in stats:
        continue
    j = stats.index(-1)
    for k in range(4):
        stats[j] = k
        newStats = tuple(stats)
        p.append(newStats)

print(minMP)

Solution (Python):



import sys

input = sys.stdin.readline

def main():
    n, A, B, C = map(int, input().split())
    bumb = []
    for _ in range(n):
        bumb.append(int(input()))

    min_cost = 100000000
    for i in range(1<<n):
        for j in range(1<<n):
            if i & j:
                continue
            for k in range(1<<n):
                if i & k or j & k:
                    continue
                a = 0
                b = 0
                c = 0
                a_cost = 0
                b_cost = 0
                c_cost = 0
                for idx in range(n):
                    if i & 1<<idx:
                        a_cost += 10 if a != 0 else 0
                        a += bumb[idx]
                    if j & 1<<idx:
                        b_cost += 10 if b != 0 else 0
                        b += bumb[idx]
                    if k & 1<<idx:
                        c_cost += 10 if c != 0 else 0
                        c += bumb[idx]
                if a == 0 or b == 0 or c == 0:
                    continue
                m = abs(a - A) + abs(b - B) + abs(c - C) + a_cost + b_cost + c_cost
                min_cost = min(min_cost, m)
    print(min_cost)
main()

Solution (Python):
inf = 10**9

def dfs(cur, a, b, c):
    if cur==N:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if a > 0 and b>0 and 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)


N, A, B, C = map(int, input().split())
l = [0] * N
for i in range(N):
    l[i] = int(input())
print(dfs(0, 0, 0, 0))

Solution (Python):
N,A,B,C=map(int, input().split())
L=[int(input()) for i in range(N)]
from itertools import product
ans=10**9
for p in product(range(4),repeat=N):
    l=[0]*3
    cost=0
    for i,t in enumerate(p):
        if t<3:
            if l[t]>0:
                cost+=10
            l[t]+=L[i]
    if min(l)>0:
        ans=min(ans,cost+abs(A-l[0])+abs(B-l[1])+abs(C-l[2]))
print(ans)

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

inf=3000

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

    l0=mp(g+1,a+l[g],b,c)+10
    l1=mp(g+1,a,b+l[g],c)+10
    l2=mp(g+1,a,b,c+l[g])+10
    l3=mp(g+1,a,b,c)
    
    return min(l0,l1,l2,l3)

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

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

bit = [0, 0, 0]
ans = 100000000
for bit[0] in range(1, 1<<N):
  for bit[1] in range(1, 1<<N):
    if bit[1] & bit[0]: continue
    for bit[2] in range(1, 1<<N):
      if bit[2] & bit[0]: continue
      if bit[2] & bit[1]: continue

      sums = [0, 0, 0]
      nums = [0, 0, 0]
      for i in range(N):
        for j in range(3):
          if bit[j] & (1<<i):
            sums[j] += l[i]
            nums[j] += 1

      score = 0
      for i in range(3):
        score += abs(sums[i] - As[i])
        score += (nums[i] - 1) * 10

      ans = min(ans, score)

print(ans)

Solution (Python):
n, *goals = list(map(int, input().split()))
bamboos = list(map(int, [input().rstrip() for _ in range(n)]))


def get_best(li, goal):
    bam_mp_lis = {0: (-10, tuple())}
    li.sort()
    for i, v in enumerate(li):
        tmp = bam_mp_lis.copy()
        for bam, (mp, lis) in tmp.items():
            b0 = bam + v
            m0 = mp + 10
            l0 = lis + (i,)
            if b0 not in bam_mp_lis or m0 < bam_mp_lis[b0][0]:
                bam_mp_lis[b0] = (m0, l0)
    del bam_mp_lis[0]
    best = 10 ** 9
    for bam, (mp, lis) in bam_mp_lis.items():
        cost = mp + abs(goal - bam)
        if cost < best:
            best = cost
            beslis = lis
    return best, beslis


ans = 10 ** 9
from itertools import permutations

for junjo in permutations(goals):
    tmp = 0
    bambooo = bamboos.copy()
    for ju in junjo:
        if len(bambooo) == 0:
            tmp = 10 ** 9
            break
        be, beli = get_best(bambooo, ju)
        beli = sorted(beli, reverse=True)
        for beli0 in beli:
            del bambooo[beli0]
        tmp += be
    if tmp < ans:
        ans = tmp

print(ans)

Solution (Python):

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

INF = float('inf')

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

    
    
    no_compound_pattern = dfs(cursor + 1, a, b, c)
    compound_a_pattern = dfs(cursor + 1, a + L[cursor], b, c) + 10
    compound_b_pattern = dfs(cursor + 1, a, b + L[cursor], c) + 10
    compound_c_pattern = dfs(cursor + 1, a, b, c + L[cursor]) + 10

    
    
    return min(
        no_compound_pattern, compound_a_pattern, compound_b_pattern, compound_c_pattern
    )


print(dfs())

Solution (Python):
n,a,b,c = map(int, input().split())
l = [int(input()) for _ in range(n)]
def dfs(k,p,q,r):
  if k == n:
    if min(p,q,r) > 0:
      return abs(p-a)+abs(q-b)+abs(r-c)-30
    else:
      return 10**7
  t1 = dfs(k+1,p,q,r)
  t2 = dfs(k+1,p+l[k],q,r)+10
  t3 = dfs(k+1,p,q+l[k],r)+10
  t4 = dfs(k+1,p,q,r+l[k])+10
  return min(t1,t2,t3,t4)

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

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

def toq(N):
    ret = ""
    t = 8
    while 4**t > 1:
        ret += str(N//(4**t))
        N -= (4**t)*(N//(4**t))
        t -= 1
    ret += str(N%4)
    return ret[::-1]

ans = float("inf")
for k in range(4**N):
    a, b, c = 0, 0, 0
    e = 0
    q = toq(k)
    for d in range(N):
        if q[d] == "1":
            a += L[d]
            e += 1
        if q[d] == "2":
            b += L[d]
            e += 1
        if q[d] == "3":
            c += L[d]
            e += 1
    if a*b*c > 0:
        ans = min(ans, abs(A-a)+abs(B-b)+abs(C-c) + 10*(e-3))
print(ans)

Solution (Python):
def main():
    N, A, B, C = map(int, input().split())
    len_banboo = []
    for i in range(N):
        len_banboo.append(int(input()))

    list_mp = []
    for i in range(4 ** N):
        materials = [0] * N
        tmp = i
        zero_cnt = 0

        for j in range(N):
            materials[(N-1)-j] = tmp % 4
            tmp //= 4
            if materials[(N-1)-j] == 0:
                zero_cnt += 1

        if (1 in materials and 2 in materials and 3 in materials) == False:
            continue

        a_mat, b_mat, c_mat = 0, 0, 0
        cost_mp = 0
        for k in range(len(len_banboo)):
            if materials[k] == 0:
                continue
            elif materials[k] == 1:
                if a_mat > 0:
                    cost_mp += 10
                a_mat += len_banboo[k]
            elif materials[k] == 2:
                if b_mat > 0:
                    cost_mp += 10
                b_mat += len_banboo[k]
            elif materials[k] == 3:
                if c_mat > 0:
                    cost_mp += 10
                c_mat += len_banboo[k]

        cost_mp += (abs(A - a_mat) + abs(B - b_mat) + abs(C - c_mat))
        list_mp.append(cost_mp)

    print(min(list_mp))


if __name__ == "__main__":
    main()

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



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

  rate_add_Not = dfs(n+1,a,b,c)
  rate_add_A   = dfs(n+1,a+l[n],b,c) + 10
  rate_add_B   = dfs(n+1,a,b+l[n],c) + 10
  rate_add_C   = dfs(n+1,a,b,c+l[n]) + 10
  return min(rate_add_Not,rate_add_A,rate_add_B,rate_add_C)

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

Solution (Python):
n,A,B,C=map(int,input().split())
l=[]
for _ in range(n):
    l.append(int(input()))
ans=10**9+7
for i in range(4**n):
    k=i
    a=0
    b=0
    c=0
    cnt=[0,0,0]
    for j in range(n):
        if k%4==1:
            a+=l[j]
            cnt[0]+=1
        elif k%4==2:
            b+=l[j]
            cnt[1]+=1
        elif k%4==3:
            c+=l[j]
            cnt[2]+=1
        k=k//4
    if cnt[0]>0 and cnt[1]>0 and cnt[2]:
        mp=abs(A-a)+abs(B-b)+abs(C-c)+max(cnt[0]*10-10,0)+max(cnt[1]*10-10,0)+max(cnt[2]*10-10,0)
        if mp<ans:
            ans=mp
print(ans)

Solution (Python):
from itertools import product
N,*ABC=map(int,input().split())
L=list(int(input()) for _ in range(N))
mp=10**9
for i in product([0,1,2,3] ,repeat=N):
    if {0,1,2}<=set(i):
        m=[0,0,0]
        for x,y in zip(i,L):
            if x!=3:
                m[x]+=y
        t=sum(abs(p-q) for p,q in zip(ABC,m))
        r=sum(1 if k!=3 else 0 for k in i)-3
        mp=min(mp,t+r*10)
print(mp)

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline

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) - 30 
        else:
            return 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))

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline

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) - 30
        else:
            return 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))

Solution (Python):
N ,A, B, C = [int(i) for i in input().split()]
L = []
for _ in range(N):
    L.append(int(input()))
minMP = 100000
for Ai in range(2**N):
    bin_Ai = format(Ai, "08b")
    bin_Ai = list(bin_Ai)
    bin_Ai.reverse()
    bambooA = []
    bambooBC = []
    for ni in range(N):
        if bin_Ai[ni] == "1":
            bambooA.append(L[ni])
        else:
            bambooBC.append(L[ni])
    if bambooA == []:
        continue
    k = len(bambooBC)
    for Bi in range(2**k):
        bin_Bi = format(Bi,"08b")
        bin_Bi = list(bin_Bi)
        bin_Bi.reverse()
        bambooB = []
        bambooCD = []
        for ki in range(k):
            if bin_Bi[ki] == "1":
                bambooB.append(bambooBC[ki])
            else:
                bambooCD.append(bambooBC[ki])
        if bambooB == []:
            continue
        m = len(bambooCD)
        for Ci in range(2**m):
            bin_Ci = format(Ci, "08b")
            bin_Ci = list(bin_Ci)
            bin_Ci.reverse()
            bambooC = []
            for mi in range(m):
                if bin_Ci[mi] == "1":
                    bambooC.append(bambooCD[mi])
            if bambooC == []:
                continue
            MP = 10*(len(bambooA)-1)+abs(sum(bambooA)-A)+10*(len(bambooB)-1)+abs(sum(bambooB)-B)+10*(len(bambooC)-1)+abs(sum(bambooC)-C)
            if MP < minMP:
                minMP = MP
print(minMP)

Solution (Python):
N, A, B, C = map(int, input().split())
lLst = [0] * N
for i in range(N):
    lLst[i] = int(input())
maxM = 10000

def dfs(nCnt, atake, btake, ctake):
    if nCnt == N:
        if min(atake, btake, ctake) > 0:
            return abs(A - atake) + abs(B - btake) + abs(C - ctake) - 30
        else:
            return maxM

    ret0 = dfs(nCnt + 1, atake, btake, ctake)
    ret1 = dfs(nCnt + 1, atake + lLst[nCnt], btake, ctake) + 10
    ret2 = dfs(nCnt + 1, atake, btake + lLst[nCnt], ctake) + 10
    ret3 = dfs(nCnt + 1, atake, btake, ctake + lLst[nCnt]) + 10
    return min(maxM, ret0, ret1, ret2, ret3)

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

Solution (Python):
N, A, B, C, *l = map(int, open(0).read().split())

INF = 1e9

def dfs(cur, a, b, c):
    if cur == N:
        return abs(A - a) + abs(B - b) + abs(C - c) - 30 if a * b * c != 0 else INF
    
    v1 = dfs(cur + 1, a, b, c)
    v2 = dfs(cur + 1, a + l[cur], b, c) + 10
    v3 = dfs(cur + 1, a, b + l[cur], c) + 10
    v4 = dfs(cur + 1, a, b, c + l[cur]) + 10
    
    return min(v1, v2, v3, v4)

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

Solution (Python):
n,a,b,c=map(int,input().split())
l=sorted([int(input()) for i in range(n)])
def kad(w,x,y,z):
    if w==n:
        if min(x,y,z)>0:
            return abs(x-a)+abs(y-b)+abs(z-c)-30
        else:
            return 10e4
        
    c0=kad(w+1,x,y,z)
    c1=kad(w+1,x+l[w],y,z)+10
    c2=kad(w+1,x,y+l[w],z)+10
    c3=kad(w+1,x,y,z+l[w])+10
    return min(c0,c1,c2,c3)

print(kad(0,0,0,0))

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

def solve(w,x,y,z):
    if w==n:
        if min(x,y,z)>0:
            return abs(x-a)+abs(y-b)+abs(z-c)-30
        else:
            return 10e4
    can0=solve(w+1,x,y,z)
    can1=solve(w+1,x+l[w],y,z)+10
    can2=solve(w+1,x,y+l[w],z)+10
    can3=solve(w+1,x,y,z+l[w])+10
    return min(can0,can1,can2,can3)

print(solve(0,0,0,0))

Solution (Python):
N, *P = map(int, input().split())
L = [int(input()) for _ in range(N)]

mp = sum(P) - 30

minmp = 1e10
Pcnt = [0] * 3
Ks = [-1] * N

while True:

    i = N-1
    while i >= 0:
        pre = cur = Ks[i]
        cur += 1
        if cur >= 3:
            cur = -1
        Ks[i] = cur

        for d, x in ((-1, pre), (1, cur)):

            if x >= 0:
                Pcnt[x] += d
                mp -= abs(P[x])
                P[x] -= d * L[i]
                mp += abs(P[x]) + d * 10

        if cur >= 0:
            break
        i -= 1
    else:
        break

    if min(Pcnt) > 0:
        minmp = min(minmp, mp)

print(minmp)

Solution (Python):
N, *P = map(int, input().split())
L = [int(input()) for _ in range(N)]

mp = sum(P) - 30

minmp = 1e10
Pcnt = [0] * 3
Ks = [-1] * N

while True:

    i = N-1
    while i >= 0:
        pre = cur = Ks[i]
        cur += 1
        if cur >= 3:
            cur = -1
        Ks[i] = cur

        for d, x in ((-1, pre), (1, cur)):

            if x >= 0:
                Pcnt[x] += d
                mp -= abs(P[x])
                P[x] -= d * L[i]
                mp += abs(P[x]) + d * 10

        if cur >= 0:
            break
        i -= 1
    else:
        break

    if min(Pcnt) > 0:
        minmp = min(minmp, mp)

print(minmp)

Solution (Python):
from itertools import product
n, a, b, c = map(int, input().split())
parts = [int(input()) for _ in range(n)]

usage = [['a', 'b', 'c', '_'] for _ in range(n)]

ans = 1e9
for cmb in product(*usage):
    if not ('a' in cmb and 'b' in cmb and 'c' in cmb):
        continue
    cost_a, cost_b, cost_c = 0, 0, 0
    len_a, len_b, len_c = 0, 0, 0
    for i in range(n):
        if cmb[i] == 'a':
            cost_a += 10 if len_a > 0 else 0
            len_a += parts[i]
        elif cmb[i] == 'b':
            cost_b += 10 if len_b > 0 else 0
            len_b += parts[i]
        elif cmb[i] == 'c':
            cost_c += 10 if len_c > 0 else 0
            len_c += parts[i]
    cost_a += abs(a - len_a)
    cost_b += abs(b - len_b)
    cost_c += abs(c - len_c)
    ans = min(ans, sum((cost_a, cost_b, cost_c)))

print(ans)

Solution (Python):
N, A, B, C = map(int, raw_input().split())
target = [A, B, C]
memo = [[[0, 0, 0], 0]]
for i in range(0, N):
	t = int(raw_input())
	m = len(memo)
	for j in range(0, m):
		now = memo[j]
		trees = now[0]
		mp = now[1]
		for k in range(0, 3):
			addmp = 0
			if trees[k] != 0: addmp = 10
			newmemo = [[trees[0], trees[1], trees[2]], mp+addmp]
			newmemo[0][k] += t
			memo.append(newmemo)
min = 1000000007
for i in range(0, len(memo)):
	if 0 not in memo[i][0]:
		sum = memo[i][1]
		for j in range(0, 3):
			sum += abs(target[j]-memo[i][0][j])
		if sum < min: min = sum
print min

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

K = 4

minmp = 1e8
for x in range(pow(4, N)):
    v = x
    c = [0] * K
    s = [0] * K
    i = 0
    while v > 0:
        m = v % K
        c[m] += 1
        s[m] += L[i]
        i += 1
        v //= K

    if c[1] > 0 and c[2] > 0 and c[3] > 0:
        minmp = min(minmp, (c[1] + c[2] + c[3] - 3) * 10 + abs(s[1] - A) + abs(s[2] - B) + abs(s[3] - C))

print(minmp)

Solution (Python):
N, A, B, C = [int(e) for e in input().split()]
Ls = [int(input()) for _ in range(N)]

INF = 10 **9
def optimal_value(a_step, a_cumA, a_cumB, a_cumC):
    if a_step == N:
        if min(a_cumA, a_cumB, a_cumC) == 0:
            return INF
        return abs(A - a_cumA) + abs(B - a_cumB) + abs(C-a_cumC)
     
    dA = optimal_value(a_step + 1, a_cumA + Ls[a_step], a_cumB, a_cumC)
    if a_cumA > 0:
        dA += 10
    
    dB = optimal_value(a_step + 1, a_cumA, a_cumB + Ls[a_step], a_cumC)
    if a_cumB > 0:
        dB += 10
   
    dC = optimal_value(a_step + 1, a_cumA, a_cumB, a_cumC + Ls[a_step])
    if a_cumC > 0:
        dC += 10
    
    dE = optimal_value(a_step + 1, a_cumA, a_cumB, a_cumC)
    
    return min(dA, dB, dC, dE)

print(optimal_value(0,0,0,0))

Solution (Python):
n, A, B, C = map(int, input().split())

l = [0 for _ in range(n)]

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


def dfs(count, a, b, c):
    if count == n:
        if a == 0 or b == 0 or c == 0:
            return 10000000
        return abs(A - a) + abs(B - b) + abs(C - c) - 30
    else:
        return min(dfs(count + 1, a, b, c),
                   dfs(count + 1, a + l[count], b, c) + 10,
                   dfs(count + 1, a, b + l[count], c) + 10,
                   dfs(count + 1, a, b, c + l[count]) + 10,
                   )


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

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(100000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b



def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)



def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def dfs(i, a, b, c, N, A, B, C, L):
    if i >= N:
        if a == 0 or b == 0 or c == 0:
            return INF
        return abs(A - a) + abs(B - b) + abs(C - c)

    ret = INF
    ret = min(ret, dfs(i + 1, a + L[i], b, c, N, A, B, C, L) + (10 if a > 0 else 0))
    ret = min(ret, dfs(i + 1, a, b + L[i], c, N, A, B, C, L) + (10 if b > 0 else 0))
    ret = min(ret, dfs(i + 1, a, b, c + L[i], N, A, B, C, L) + (10 if c > 0 else 0))
    ret = min(ret, dfs(i + 1, a, b, c, N, A, B, C, L))

    return ret


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

    print(dfs(0, 0, 0, 0, N, A, B, C, L))


if __name__ == '__main__':
    main()

Solution (Python):
import itertools


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

mint = 1e9
for comb in itertools.product((0, 1, 2, 3), repeat=len(ls)):
    fa = [ls[i] for i in range(len(ls)) if comb[i] == 1]
    fb = [ls[i] for i in range(len(ls)) if comb[i] == 2]
    fc = [ls[i] for i in range(len(ls)) if comb[i] == 3]

    if len(fa) == 0 or len(fb) == 0 or len(fc) == 0:
        continue

    mint = min(mint, (len(fa) + len(fb) + len(fc) - 3) * 10 +
               abs(sum(fa) - a) + abs(sum(fb) - b) + abs(sum(fc) - c))


print(mint)

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

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

def dfs(i, a_len, b_len, c_len):
  if i == N:
    if a_len == 0 or b_len == 0 or c_len == 0:
      return float('inf')
    else:
      return abs(a_len - A) + abs(b_len - B) + abs(c_len - C) - 30
  
  br1 = dfs(i + 1, a_len + L[i], b_len, c_len) + 10
  br2 = dfs(i + 1, a_len, b_len + L[i], c_len) + 10
  br3 = dfs(i + 1, a_len, b_len, c_len + L[i]) + 10
  br4 = dfs(i + 1, a_len, b_len, c_len)
  
  return min(br1, br2, br3, br4)

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

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


def dfs(a, b, c, i):
    if i == N:
        if a == 0 or b == 0 or c == 0:
            return 1e9

        return abs(A - a) + abs(B - b) + abs(C - c)

    dfs_not_used = dfs(a, b, c, i+1)
    dfs_A = dfs(a+l[i], b, c, i+1) + + 10*(a != 0)
    dfs_B = dfs(a, b+l[i], c, i+1) + 10*(b != 0)
    dfs_C = dfs(a, b, c+l[i], i+1) + 10*(c != 0)

    return min(dfs_not_used, dfs_A, dfs_B, dfs_C)


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

Solution (Python):
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)-30 if min(a,b,c)>0 else inf
    no_merge = dfs(cur+1, a, b, c)
    merge_a = dfs(cur+1, a+L[cur], b, c) + 10
    merge_b = dfs(cur+1, a, b+L[cur], c) + 10
    merge_c = dfs(cur+1, a, b, c+L[cur]) + 10
    return min(no_merge, merge_a, merge_b, merge_c)

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

Solution (Python):
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)-30 if min(a,b,c)>0 else inf
    non_marge = dfs(cur+1, a, b, c)
    marge_a = dfs(cur+1, a+L[cur], b, c) + 10
    marge_b = dfs(cur+1, a, b+L[cur], c) + 10
    marge_c = dfs(cur+1, a, b, c+L[cur]) + 10
    return min(non_marge, marge_a, marge_b, marge_c)

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

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

mina = 1001
minb = 1001
minc = 1001

ans = 999999
for i in range(4 ** N):
    tmpA = []
    tmpB = []
    tmpC = []
    mp = 0
    for j in reversed(range(0, N * 2, 2)):

            
            if (i & (1 << j)) and (i & (1 << (j + 1))):
                pass
            
            elif (i & (1 << j)) and not (i & (1 << (j + 1))):
                tmpC.append(bam[int(j / 2)])
            
            elif not (i & (1 << j)) and (i & (1 << (j + 1))):
                tmpB.append(bam[int(j / 2)])
            
            elif not (i & (1 << j)) and not (i & (1 << (j + 1))):
                tmpA.append(bam[int(j / 2)])
            
            
    if not tmpA or not tmpB or not tmpC:
        continue
    
    else:
        mp = (len(tmpA) - 1) * 10 + (len(tmpB) - 1) * 10 + (len(tmpC) - 1) * 10
        tmpA = sum(tmpA)
        tmpB = sum(tmpB)
        tmpC = sum(tmpC)
    mp += abs(A- tmpA) + abs(B - tmpB) + abs(C - tmpC)
    ans = min(ans, mp)

print(ans)

Solution (Python):
N, A, B, C = [int(x) for x in input().split()]
l = []
for i in range(N):
  l.append(int(input()))
 
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 10000000
  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))