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:
import itertools
n,*abc=map(int,input().split())
l=[int(input()) for i in range(n)]
inf=100000000000000
mi=inf
for i in list(itertools.product([i for i in range(3)],repeat=n)):
    sub=[[] for j in range(3)]
    for j in range(n):
        sub[i[j]].append(j)
    mi_sub=0
    for j in range(3):
        l_sub=len(sub[j])
        if l_sub==0:
            mi_sub=inf
            break
        mi_subsub=inf
        for k in range(2**l_sub):
            mi_subsubsub=[0,0]
            for l_subsub in range(l_sub):
                if ((k>>l_subsub) &1):
                    mi_subsubsub[0]+=1
                    mi_subsubsub[1]+=l[sub[j][l_subsub]]
            if mi_subsubsub[0]!=0:
                mi_subsub=min(mi_subsub,abs(mi_subsubsub[1]-abc[j])+mi_subsubsub[0]*10-10)
        mi_sub+=mi_subsub
    mi=min(mi,mi_sub)
print(mi)

Submission status: Accepted

Solution code:
import itertools
n,a,b,c=map(int,input().split())
abc=[a,b,c]
l=[int(input()) for i in range(n)]
m=[i for i in range(3)]
mi=100000000000000
for i in list(itertools.product(*[m for _ in range(n)])):
    #print(i)
    #print(0)
    sub=[[] for j in range(3)]
    for j in range(n):
        sub[i[j]].append(j)
    #print(sub)
    mi_sub=0
    for j in range(3):
        subsub=sub[j]
        l_sub=len(subsub)
        if l_sub==0:
            mi_sub=100000000000000
            break
        mi_subsub=100000000000000
        for k in range(2**l_sub):
            mi_subsubsub=[0,0]
            for l_subsub in range(l_sub):
                if ((k>>l_subsub) &1):
                    mi_subsubsub[0]+=1
                    #print(subsub[l_subsub])
                    mi_subsubsub[1]+=l[subsub[l_subsub]]
            if mi_subsubsub[0]!=0:
                mi_subsub=min(mi_subsub,abs(mi_subsubsub[1]-abc[j])+mi_subsubsub[0]*10-10)
        mi_sub+=mi_subsub
    mi=min(mi,mi_sub)
    #print(0)
print(mi)

Submission status: Accepted

Solution code:
n,a,b,c=map(int, input().split())
l=[int(input()) for _ in range(n)]
from itertools import product
X=product([0,1,2,3],repeat=n)

def score(x):
  atmp=0
  btmp=0
  ctmp=0
  score=0
  for i in range(n):
    if x[i]==1:
      if atmp!=0:
        score+=10
      atmp+=l[i]
    elif x[i]==2:
      if btmp!=0:
        score+=10
      btmp+=l[i]
    elif x[i]==3:
      if ctmp!=0:
        score+=10
      ctmp+=l[i]
    else:continue
  if atmp>0 and btmp>0 and ctmp>0:
    score+=(abs(a-atmp)+abs(b-btmp)+abs(c-ctmp))
  else: score=float("inf")
  return score

smin=float('inf')
for x in X:
  smin=min(smin,score(x))
print(smin)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]

class Util():
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Util.round_up(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)

def main():
    N,A,B,C = cin.mapInt()
    L = cin.intColsList(N)

    ans = int(1e09)
    for i in range(1 << N * 2):
        D = [0] * 3
        mp = 0
        for j in range(N):
            n = (i >> (j * 2)) & 3
            if n == 0:
                continue
            if D[n - 1] > 0:
                mp += 10
            D[n - 1] += L[j]
        if 0 in D:
            continue
        mp += abs(A - D[0])
        mp += abs(B - D[1])
        mp += abs(C - D[2])
        ans = min(ans,mp)
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

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


def dfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Submission status: Accepted

Solution code:

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

def calc(m):
    s=[0,0,0,0]
    sy=-3
    for i in range(N):
        s[m%4]+=L[i]
        if m%4!=0:
            sy+=1
        m=m//4
    for i in range(1,4):
        if s[i]==0:
            return 10**9
    return abs(A-s[1])+abs(B-s[2])+abs(C-s[3])+sy*10
    
ans=10**9
for i in range(pow(4,N)):
    ans=min(ans,calc(i))
print(ans)

Submission status: Accepted

Solution code:
def dfs(i, a, b, c, t):
    global ans
    if a*b*c > 0:
        ans = min( ans, abs(A-a)+abs(B-b)+abs(C-c)+10*(t-3) )
    if i > N - 1: return 0
    dfs(i+1,a,b,c,t)
    dfs(i+1, a + L[i], b, c, t+1)
    dfs(i+1, a, b + L[i], c, t+1)
    dfs(i+1, a, b, c + L[i], t+1)
    return ans
N, A, B, C = map(int,input().split())
L = [int(input()) for i in range(N)]
ans = 10**9
print(dfs(0,0,0,0,0))

Submission status: Accepted

Solution code:
def solve(current, x, y, z):
    if current == n:
        if min(x, y, z) > 0:
            return abs(a - x) + abs(b - y) + abs(c - z) - 30
        else:
            return 10 ** 9 + 7
    use_for_a = solve(current + 1, x + l[current], y, z) + 10
    use_for_b = solve(current + 1, x, y + l[current], z) + 10
    use_for_c = solve(current + 1, x, y, z + l[current]) + 10
    not_use = solve(current + 1, x, y, z)

    return min(use_for_a, use_for_b, use_for_c, not_use)

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

Submission status: Accepted

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:
        if min(a, b, c) > 0:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
        else:
            return float("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: Accepted

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

ans=10**18

for pattern in product(range(4),repeat=N):
    MP=0
    take=[0]*3
    for index,length in zip(pattern,L):
        if index==3:
            continue
        if take[index]!=0:
            MP+=10
        take[index]+=length
    if take[0]!=0 and take[1]!=0 and take[2]!=0:
        MP+=abs(A-take[0])
        MP+=abs(B-take[1])
        MP+=abs(C-take[2])
        ans=min(MP,ans)
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n, A, B, C = MAP()

l = [INT() for i in range(n)]

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 10 ** 9
    
    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: Accepted

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

orgPetternList =[]
def petternMake(cnt,s):
  if cnt == n+1:
    orgPetternList.append(s)
    return
  for i in range(4):
    petternMake(cnt+1,s+str(i))
  

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

#全部一度全パターンで足しきる場合の回す元リスト作成
petternMake(1,'')

min = 999999999
#全パターンループして、最低MP算出
for ptn in orgPetternList:

  if ptn.find('1') == -1 or ptn.find('2') == -1  or ptn.find('3') == -1 :
    continue
    
  ptnList = list(ptn)
  sumMp = nowa = nowb = nowc = acnt = bcnt = ccnt= 0
  for cnt in range(len(ptnList)):
    
    if ptnList[cnt] == '1':
      nowa += banboo[cnt]
      acnt+=1
      if acnt>1:
        sumMp +=10
    elif ptnList[cnt]=='2':
      nowb += banboo[cnt]
      bcnt+=1
      if bcnt>1:
        sumMp +=10
    elif ptnList[cnt]=='3':
      nowc += banboo[cnt]
      ccnt+=1
      if ccnt>1:
        sumMp +=10
      
  sumMp = sumMp + abs(a-nowa) + abs(b-nowb) +abs(c-nowc)

  if sumMp < min:
    min = sumMp

print(min)

Submission status: Accepted

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


def dfs(i, a, b, c):
    if n == i:
        if 1 > a or 1 > b or 1 > c:
            return 10 ** 18
        return abs(A-a) + abs(B-b) + abs(C-c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Submission status: Accepted

Solution code:
#A

"""
s = input().split("/")
if int(s[1]) <= 4:
    print("Heisei")
else:
    print("TBD")
"""
#B
"""
n = int(input())
ans = 0
for i in range(n):
    a = input().split()
    if a[1] == "JPY":
        ans += int(a[0])
    else:
        ans +=  float(a[0]) * 380000
print(ans)
"""

#C
import itertools

n, a, b, c = map(int, input().split())
abc = [a, b, c]
ans = float("INF")
x = [int(input()) for i in range(n)]
nums = list(itertools.product([0, 1, 2, 3], repeat = n))
#print(nums)
for i in nums:
    tmp = 0
    aa = 0
    ba = 0
    ca = 0
    for number, k in enumerate(i):
        if k == 1:
            if aa != 0:
                tmp += 10
            aa += x[number]
        if k == 2:
            if ba != 0:
                tmp += 10
            ba += x[number]
        if k == 3:
            if ca != 0:
                tmp += 10
            ca += x[number]
    if aa == 0 or ba == 0 or ca == 0:
        tmp = float("INF")
    else:
        tmp += abs(aa - a)
        tmp += abs(ba - b)
        tmp += abs(ca - c)
    ans = min(tmp, ans)
print(ans)
        
        
            


#D

"""
a, b, q = map(int, input().split())
s = [int(input()) for i in range(a)]
t = [int(input()) for i in range(b)]
s.sort()
t.sort()

import bisect


for i in range(q):
    x = int(input())
    s_l = bisect.bisect_left(s, x) - 1
    t_l = bisect.bisect_left(t, x) - 1
    s_r = s_l + 1
    t_r = t_l + 1
    if s_l == -1:
        s_l = 0
    if t_l == -1:
        t_l = 0
    if s_l == a :
        s_l = a - 1
        s_r = a - 1
    if s_r == a:
        s_r = a - 1
    if t_l == b:
        t_l = b - 1
        t_r = b - 1
    if t_r == b:
        t_r = b - 1
    ans = min(abs(t[t_l] - s[s_r]) + min(abs(t[t_l] - x), abs(s[s_r] - x)),
    abs(s[s_l] - t[t_r]) + min(abs(s[s_l] - x), abs(t[t_r] - x)),
    abs(s[s_l] - t[t_l]) + min(abs(s[s_l] - x), abs(t[t_l] - x)),
    abs(s[s_r] - t[t_r]) + min(abs(s[s_r] - x), abs(t[t_r] - x)),
    )
    
    print(ans)
"""

Submission status: Accepted

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


def bfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = bfs(i + 1, a, b, c)
    res = min([res, bfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, bfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, bfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


print(bfs(0, 0, 0, 0))

Submission status: Accepted

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


def dfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)

    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Submission status: Accepted

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


def dfs(i, a, b, c):
    if n == i:
        if a < 1 or b < 1 or c < 1:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)

    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

N, A, B, C = [int(x) for x in input().split()]

L = [int(input()) for _ in range(N)]
INF = 10 ** 12

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

Solution code:
import sys
sys.setrecursionlimit(10 ** 6)

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

L = [int(input()) for _ in range(N)]
INF = 10 ** 12


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

Solution code:
def calc(T, bam):
    mix = len(bam) - 1
    total = sum(bam)
    return mix * 10 + abs(T - total)


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


var = [[] for _ in range(N + 1)]
var[0].append('')

for i in range(N):
    for j in var[i]:
        for c in '0abc':
            var[i + 1].append(j + c)

V = var[N]

ans = 10 ** 6
for v in V:
    a = []
    b = []
    c = []
    cost = 0
    for i, m in enumerate(v):
        if m == 'a':
            a.append(L[i])
        elif m == 'b':
            b.append(L[i])
        elif m == 'c':
            c.append(L[i])
    if len(a) and len(b) and len(c):
        CA = calc(A, a)
        CB = calc(B, b)
        CC = calc(C, c)
        cost = CA + CB + CC
        if cost == 5:
            print('hello')
        ans = min(ans, cost)

print(ans)

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

from itertools import permutations
from itertools import combinations

n, a, b, c = na()
l = [ni() for i in range(n)]
inf = 1 << 60

def dfs(i, x, y, z):
    if i == n:
        return abs(x - a) + abs(y - b) + abs(z - c) - 30 if min(x, y, z) > 0 else inf
    q = dfs(i + 1, x, y, z)
    w = dfs(i + 1, x + l[i], y, z) + 10
    e = dfs(i + 1, x, y + l[i], z) + 10
    r = dfs(i + 1, x, y, z + l[i]) + 10
    return min(q, w, e, r)

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

Submission status: Accepted

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


def dfs(i, a, b, c):
    if i == n:
        if a < 1 or b < 1 or c < 1:
            return inf
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Submission status: Accepted

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


def dfs(i, a, b, c):
    if i == n:
        if 1 > a or 1 > b or 1 > c:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Submission status: Accepted

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


def dfs(i, a, b, c):
    if n == i:
        if 1 > a or 1 > b or 1 > c:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)

    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a != 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b != 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c != 0 else 0)])
    return res


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

Submission status: Accepted

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


def dfs(i, a, b, c):
    if i == n:
        if a == 0 or b == 0 or c == 0:
            return 10 ** 18
        return abs(A - a) + abs(B - b) + abs(C - c)
    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Submission status: Accepted

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

inf = 10 ** 18


def dfs(i, a, b, c):
    if n == i:
        if 0 == a or 0 == b or 0 == c:
            return inf
        return abs(A - a) + abs(B - b) + abs(C - c)

    res = dfs(i + 1, a, b, c)
    res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
    res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
    res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
    return res


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

Submission status: Accepted

Solution code:
n,A,B,C=map(int,input().split())
bam=[]
for i in range(n):
    bam.append(int(input()))
INF = 10 ** 9

def dfs(cur, a, b, c):
    if cur == n: #curは０スタートだから、cur=nのときにはｎ本の竹の使い道が決まっている。
        #print(str(cur)+" "+str(a)+" "+str(b)+" "+str(c))
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF 
        #-30する理由は、２本の竹を使うときに20mpで合体してる。本当は、10mpでいいのに。
        #min(a, b, c) > 0 この条件は、A,B,Cどれにも最低一本は木を使わないといけないから
    ret0 = dfs(cur + 1, a, b, c)
    ret1 = dfs(cur + 1, a + bam[cur], b, c) + 10 #Aにcur番目の竹を使う　使うときに10mp使っておく。
    ret2 = dfs(cur + 1, a, b + bam[cur], c) + 10
    ret3 = dfs(cur + 1, a, b, c + bam[cur]) + 10
    #print(min(ret0, ret1, ret2, ret3))
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
#from scipy.misc import comb

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

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

ans = 10**9

for i in range(1<<(2*N)):
    a = b = c = 0
    tmp = 0
    for j in range(N):
        flg = 0
        if i&(1<<(2*j)):
            flg += 1
        if i&(1<<(2*j+1)):
            flg += 2

        if flg == 1:
            if a:
                tmp += 10
            a += L[j]
        elif flg == 2:
            if b:
                tmp += 10
            b += L[j]
        elif flg == 3:
            if c:
                tmp += 10
            c += L[j]

    if a*b*c==0:
        continue

    tmp += abs(A-a)+abs(B-b)+abs(C-c)
    ans = min(ans,tmp)

print(ans)

Submission status: Accepted

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

for i in range(N):
    L.append(int(input()))

def search(l,a,b,c,d):
    if l == N:
        if a == 0 or b == 0 or c == 0:
            return float('inf')

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

    if a == 0:
        mp1 = search(l+1,a+L[l],b,c,d)
    else:
        mp1 = search(l+1,a+L[l],b,c,d+1)

    if b == 0:
        mp2 = search(l+1,a,b+L[l],c,d)
    else:
        mp2 = search(l+1,a,b+L[l],c,d+1)

    if c == 0:
        mp3 = search(l+1,a,b,c+L[l],d)
    else:
        mp3 = search(l+1,a,b,c+L[l],d+1)

    mp4 = search(l+1,a,b,c,d)

    return min([mp1,mp2,mp3,mp4])

print(search(0,0,0,0,0))

Submission status: Accepted

Solution code:
import itertools

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

li = [0,1,2,3]

ans = 10**9
for elm in  itertools.product(li,repeat=n):
    #1か2か3がなければ次へ
    if not(1 in elm) or not(2 in elm) or not(3 in elm):
        continue

    whch = [[],[],[]]
    ans_mm = 0
    for j in range(len(elm)):
        if elm[j] == 1:
            whch[0].append(l[j])
        elif elm[j] == 2:
            whch[1].append(l[j])
        elif elm[j] == 3:
            whch[2].append(l[j])

    ans_mm += ((len(whch[0])-1) + (len(whch[1])-1) + (len(whch[2])-1)) * 10
    ans_mm += abs(sum(whch[0])-a)
    ans_mm += abs(sum(whch[1])-b)
    ans_mm += abs(sum(whch[2])-c)

    ans = min(ans,ans_mm)

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n,a,b,c = inpl()
l = inpln(n)
x = [a,b,c]
res = INF

for flag in itertools.product(range(4), repeat=n):
    cnt = 0
    xx = [[]for i in range(4)]
    for i,bit in enumerate(flag):
        xx[bit].append(i)
    for i in range(3):
        if xx[i] == []:
            break
        cnt += (len(xx[i]) - 1)*10
        sum = 0
        for j in xx[i]:
            sum += l[j]
        cnt += abs(sum-x[i])
    else:
        res = min(res,cnt)
print(res)

Submission status: Accepted

Solution code:
import sys
from itertools import product

inf = float('inf')

n, *target = map(int, sys.stdin.readline().split())
target = [None] + target
*l,  = map(int, sys.stdin.read().split())

def main():
    costs = []
    for p in product(range(4), repeat=n):
        tmp = [[] for _ in range(4)]
        p = list(p)
        for i in range(n):
            tmp[p[i]].append(l[i])

        cost = [0] * 4
        for i in range(1, 4):
            if not tmp[i]:
                break
            cost[i] += 10 * (len(tmp[i]) - 1)
            cost[i] += abs(target[i] - sum(tmp[i]))
        else:
            costs.append(sum(cost))

    return min(costs)

if __name__ == '__main__':
    ans = main()
    print(ans)

Submission status: Accepted

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

ans = 10**18
for pattern in product(range(4), repeat=N):
  MP = 0
  take = [0] * 3 # A, B, Cの3本
  for index, length in zip(pattern, l):
    if index == 3:
      continue
    if take[index] != 0: # 最初の1本は合成MP不要
      MP += 10
    take[index] += length
  
  if take[0] != 0 and take[1] != 0 and take[2] != 0:
    MP += abs(A - take[0])
    MP += abs(B - take[1])
    MP += abs(C - take[2])
    ans = min(MP, ans)

print(ans)

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split())
l=[int(input()) for i in range(N)]
ans=10**9
for i in range(4**N):
  tmp=[0]*N
  s=[0,0,0]
  for j in range(N):
    k = (i // (4**j))%4
    tmp[j]=k
    if k>0:
      s[k-1]+=1
  if s[0]==0 or s[1]==0 or s[2]==0:
    continue
  else:
    ans_tmp=0
    ans_tmp=10*(max(s[0]-1,0)+max(s[1]-1,0) +max(s[2]-1,0))
    s[0],s[1],s[2]=0,0,0
    for j in range(N):
      if tmp[j]==0:
        continue
      s[tmp[j]-1]+=l[j]
    ans_tmp+=abs(s[0]-A)+abs(s[1]-B)+abs(s[2]-C)
    ans=min(ans,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)]
r=10**5
def dsf(i,a,b,c,p):
    global r
    if i==n:
        if a!=0 and b!=0 and c!=0:
            p+=abs(A-a)+abs(B-b)+abs(C-c)-30
            r=min(r,p)
        return
    dsf(i+1,a+L[i],b,c,p+10)
    dsf(i+1,a,b+L[i],c,p+10)
    dsf(i+1,a,b,c+L[i],p+10)
    dsf(i+1,a,b,c,p)
dsf(0,0,0,0,0)
print(r)

Submission status: Accepted

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

ans = 10**10

for pat in range(4**N):
    a = b = c = 0
    mp = 0
    for i in range(N):
        k = (pat >> (2*i)) & 0b11
        if k == 0:
            pass            
        if k == 1:
            a += l[i]
            mp += 10
        elif k == 2:
            b += l[i]
            mp += 10
        elif k == 3:
            c += l[i]
            mp += 10
    if a != 0 and b != 0 and c != 0:
        mp += -30 + 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 = list(map(int, input().split()))
l = [int(input().strip()) for i in range(N)]
INF = 10 ** 9


def dfs(pointer, a, b, c):
    if pointer == N:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(min(a, b), c) > 0 else INF
    ret_0 = dfs(pointer + 1, a, b, c)
    ret_1 = dfs(pointer + 1, a + l[pointer], b, c) + 10
    ret_2 = dfs(pointer + 1, a, b + l[pointer], c) + 10
    ret_3 = dfs(pointer + 1, a, b, c + l[pointer]) + 10
    return min(ret_0, ret_1, ret_2, ret_3)


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

Submission status: Accepted

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


ans=0

from operator import mul
from functools import reduce
import itertools
def cmb(n,r):
    if n<r: return 0
    r = min(n-r,r)
    if r == 0: return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1,r + 1))
    return over // under

#各竹への竹の割り振り方は10000通り弱。
#各割り振りでの最小コストを求めればよい。（使わない竹がある場合も含め）

def mp(x,y,z): #割り振りをx,y,zと決めた場合の最小MP…
  ans=float("inf")
  rng=list(range(n))
  for ptn in itertools.combinations(rng,x):
    cost=0
    cost+=10*(x-1) #まず接続コストを追加
    wa=0
    copy=rng[:]
    for i in ptn:
      wa+=l[i]
      copy.remove(i)
    #print(wa)
    cost+=abs(wa-a) #出来た長さをaに調整する分の費用追加
    for ptn2 in itertools.combinations(copy,y):
      #print(ptn2)
      cost1=cost+10*(y-1) #まず接続コストを追加
      wa=0
      copy2=copy[:]
      for i in ptn2:
        wa+=l[i]
        copy2.remove(i)
      cost1+=abs(wa-b)
      for ptn3 in itertools.combinations(copy2,z):
        cost2=cost1+10*(z-1)
        wa=0
        for i in ptn3:
          wa+=l[i]
        cost2+=abs(wa-c)
        ans=min(ans,cost2)
  return(ans)

final=float("inf")
for i in range(1,n-1):
  for j in range(1,n-i):
    for k in range(1,n+1-i-j):
      final=min(final,mp(i,j,k))
print(final)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, A, B, C = map(int, input().split(' '))
L = [int(input()) for _ in range(N)]

costs = []
min_cost = float('inf')

def get_candidates(n, buf):
    if n == N:
        cands_A = [l for i, l in zip(buf, L) if i == 0]
        cands_B = [l for i, l in zip(buf, L) if i == 1]
        cands_C = [l for i, l in zip(buf, L) if i == 2]

        if len(cands_A) > 0 and len(cands_B) > 0 and len(cands_C) > 0:
            cost = 0
            cost += (len(cands_A) - 1) * 10
            cost += abs(sum(cands_A) - A)

            cost += (len(cands_B) - 1) * 10
            cost += abs(sum(cands_B) - B)

            cost += (len(cands_C) - 1) * 10
            cost += abs(sum(cands_C) - C)

            costs.append(cost)
            # if cost < 300:
            #     print(cost)


    else:
        for i in range(4):
            buf.append(i)
            get_candidates(n+1, list(buf))
            buf.pop()

get_candidates(0, [])
# cost = min(list())
print(min(costs))