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:
from itertools import product
n,a,b,c=map(int,input().split())
abc=[a,b,c]
l=tuple(int(input()) for _ in range(n))

ans=10**18
for a in product(range(4), repeat=n):
    MP=0
    take=[[] for _ in range(4)]

    for i in range(n):
        take[a[i]].append(l[i])
    
    if take[0] and take[1] and take[2]:
        for i,t in enumerate(take[:-1]):
            MP+=max(0, 10*(len(t)-1))
            MP+=abs(abc[i] - sum(t))
    else:
        continue

    ans=min(ans, MP) 

print(ans)

Submission status: Accepted

Solution code:
from itertools import product
n, *A = map(int, input().split())
L = [int(input()) for _ in range(n)]
m_ = sum(abs(A[i]-L[i]) for i in range(3))
for P in product(range(4), repeat = n):
    C = [[] for _ in range(4)]
    m = 0
    for i in range(n):
        C[P[i]].append(L[i])
    C = C[1:]
    if [] in C: continue
    for i in range(3):
        m += (len(C[i])-1)*10+abs(sum(C[i])-A[i])
    m_ = min(m_, m)
print(m_)

Submission status: Accepted

Solution code:
def makelist(n, m):
    return [[0 for _ in range(m)] for _ in range(n)]

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


ans = int(1e18)
for n in range(1 << N * 2):
    use = [0]*(N+1)
    for i in range(1, N+1):
        use[i] = (n >> ((i-1)*2)) % 4

    hoge = [[], [], [], []]
    for i in range(1, N+1):
        hoge[use[i]].append(i)

    flag = False
    for e in hoge[:3]:
        if len(e) == 0:
            flag = True
    if flag:
        continue

    tmp = 0
    fuga = [A, B, C]
    for i in range(3):
        now = hoge[i]
        tmp += 10 * (len(now) - 1)
        tmp += abs(fuga[i] - sum(map(lambda x: l[x], now)))

    ans = min(ans, tmp)

print(ans)

Submission status: Accepted

Solution code:
from itertools import product, combinations

n, *abc = list(map(int, input().split()))
ll = [int(input()) for _ in range(n)]

# 合成してから短縮/延長すればよい
# 合成方法を全探索する

ans = float("inf")
for n_use in range(3, n+1) : # 何本使うか
    for l_use in combinations(ll, n_use) : # どの竹を使うか
        for dist in product(*[range(3) for _ in range(n_use)]) : #竹をどう合成するか
            if len(set(dist)) != 3 :
                continue

            labc = [0, 0, 0]
            for i, l in zip(dist, l_use) :
                labc[i] += l
    
            diff = [abs(labc[i]-abc[i]) for i in range(3)]
            ans = min(ans, 10*(n_use-3)+sum(diff))

print(ans)

Submission status: Accepted

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

    cur = dict()
    cur[(0, 0, 0)] = 0
    for i in range(N):
        temp = dict()
        for t in cur:
            for j in range(3):
                cost = cur[t]
                lv = list(t)
                merge = False
                if lv[j] != 0:
                    merge = True
                lv[j] += L[i]
                newt = tuple(lv)
                if newt not in temp:
                    temp[newt] = cost + 10 * (merge == True)
                else:
                    temp[newt] = min(temp[newt], cost + 10 * (merge == True))
        for t in temp:
            if t not in cur:
                cur[t] = temp[t]
            else:
                cur[t] = min(cur[t], temp[t])

    ans = float('inf')
    for t in cur:
        lv = list(t)
        if lv[0] > 0 and lv[1] > 0 and lv[2] > 0:
            v = abs(lv[0]-A) + abs(lv[1]-B) + abs(lv[2]-C) + cur[t]
            ans = min(ans, v)

    return ans

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

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
INF = 10**9
def count(a_sum, b_sum, c_sum, cur):
  if cur == n:
    return (abs(a_sum -a) + abs(b_sum -b) +abs(c_sum -c)) -30 if min(a_sum, b_sum, c_sum) > 0 else INF
  else:
    ans1 = count(a_sum, b_sum, c_sum, cur+1)
    ans2 = count(a_sum+l[cur], b_sum, c_sum, cur+1) + 10 
    ans3 = count(a_sum, b_sum+l[cur], c_sum, cur+1) + 10
    ans4 = count(a_sum, b_sum, c_sum+l[cur], cur+1) + 10
    return min(ans1, ans2, ans3, ans4)

print(count(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)]
INF=1e9

def dfs(i,a,b,c):
  if i==n:
    if min(a,b,c)>0:
      return abs(A-a)+abs(B-b)+abs(C-c)-30
    else:
      return INF
  ret0 = dfs(i+1,a,b,c)
  ret1 = dfs(i+1,a+l[i],b,c)+10
  ret2 = dfs(i+1,a,b+l[i],c)+10
  ret3 = dfs(i+1,a,b,c+l[i])+10
  return min(ret0,ret1,ret2,ret3)
 
print(dfs(0,0,0,0))

Submission status: Accepted

Solution code:
import itertools
n, a, b, c = map(int, input().split())
abc = sorted([a, b, c])
ll = [int(input()) for _ in range(n)]
ls = [0, 1, 2, 3]
m_cost = 10 ** 9
for i in itertools.product(ls, repeat=n):
    t = [[0, 0] for _ in range(4)]
    if 1 in i and 2 in i and 3 in i:
        for j in range(n):
            t[i[j]][0] += ll[j]
            t[i[j]][1] += 1
        st = sorted([t[1][0], t[2][0], t[3][0]])
        c = (t[1][1] + t[2][1] + t[3][1] - 3) * 10
        c += abs(abc[0] - st[0]) + abs(abc[1] - st[1]) + abs(abc[2] - st[2])
        m_cost = min(m_cost, c)
print(m_cost)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

INF = 10**9


def dfs(cur, ia, ib, ic):
    if(cur == N):
        return abs(ia-A) + abs(ib-B) + abs(ic-C) - 30 if min(ia,ib,ic) > 0 else INF

    ret0 = dfs(cur+1, ia, ib, ic)
    ret1 = dfs(cur+1, ia+l[cur], ib, ic) + 10
    ret2 = dfs(cur+1, ia, ib+l[cur], ic) + 10
    ret3 = dfs(cur+1, ia, ib, ic+l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    print(dfs(0,0,0,0))

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    global N, A, B, C, l
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    l = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A, B, C, l)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys

INF = 10**9


def dfs(cur, l, ia, ib, ic):
    if(cur == N):
        return abs(ia-A) + abs(ib-B) + abs(ic-C) - 30 if min(ia,ib,ic) > 0 else INF

    ret0 = dfs(cur+1, l, ia, ib, ic)
    ret1 = dfs(cur+1, l, ia+l[cur], ib, ic) + 10
    ret2 = dfs(cur+1, l, ia, ib+l[cur], ic) + 10
    ret3 = dfs(cur+1, l, ia, ib, ic+l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)


def solve(N: int, A: int, B: int, C: int, l: "List[int]"):
    print(dfs(0,l,0,0,0))

    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    global N, A, B, C, l
    N = int(next(tokens))  # type: int
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    C = int(next(tokens))  # type: int
    l = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, A, B, C, l)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#### import ####
import sys
import math
from collections import defaultdict

#### 設定 ####
sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
    read_all = [tuple(map(int, input().split())) for _ in range(N)]
    return map(list,zip(*read_all))

#################

N,A,B,C = II()
l = [I() for _ in range(N)]
inf = float('inf')

def solve(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 = solve(cur+1,a,b,c)
    ret1 = solve(cur+1,a+l[cur],b,c)+10
    ret2 = solve(cur+1,a,b+l[cur],c)+10
    ret3 = solve(cur+1,a,b,c+l[cur])+10
    return min(ret0,ret1,ret2,ret3)

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

Submission status: Accepted

Solution code:
#コピペAC
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
INF = 10 ** 9
def dfs(cur, a, b, c):
  if cur == N:
    return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
  ret0 = dfs(cur + 1, a, b, c)
  ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
  ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
  ret3 = dfs(cur + 1, a, b, c + l[cur]) + 10
  return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

Solution code:
INF=10**9
n,A,B,C=map(int,input().split())
l=[int(input()) for i in range(n)]
ans=INF
a=[0,0,0,0]
def yonsin(X):
    if(int(X/4)):
        return yonsin(int(X/4))+str(X%4)
    return str(X%4)

#a[1]はAにいれる　a[2]はBにいれる　a[0]は使わない
for i in range(4**n):
  s=yonsin(i)
  if len(s)<n:
    s='0'*(n-len(s))+s
  d=list(s)
  cost=0
  a=[0,0,0,0]
  if set(d)!={'1','2','3'} and set(d)!={'0','1','2','3'}:
    cost=INF
  else:  
    for j in range(n):          #j番目の竹に関する処理 4進数がABCどれにいれるか使わないか表す
      a[int(s[j])]+=l[j]
      if int(s[j])!=0:
        cost+=10
    cost+=abs(a[1]-A)+abs(a[2]-B)+abs(a[3]-C)-30
    

  ans=min(ans,cost)

print(ans)

Submission status: Accepted

Solution code:
import itertools

N, A, B, C = map(int, input().split())
l = [0] * N
for i in range(N):
  l[i] = int(input())
mplist = []
for r in itertools.product([0, 1, 2, 3], repeat = N):
  mp = 0
  use = 0
  le = [0] * 4
  for i in range(N):
    le[r[i]] += l[i]
    if r[i] >= 1:
      use += 1
  if le[1] == 0 or le[2] == 0 or le[3] == 0:
    continue
  mp = (use - 3) * 10 + abs(le[1] - A) + abs(le[2] - B) + abs(le[3] - C)
  mplist.append(mp)
print(min(mplist))

Submission status: Accepted

Solution code:
# お約束
INF = float("inf")
MOD = int(1e9 + 7)
def int1(n):
    return int(n) - 1
def parse(args, *params):
    return tuple(p(v) for p, v in zip(params, next(args).split()))

# エントリーポイント
def main(args):
    N, A, B, C = parse(args, int, int, int, int)
    L = tuple(int(next(args)) for _ in range(N))

    def solve(n, l, a, b, c, cost):
        if n == 0:
            if not all((a, b, c)): return INF
            return abs(A - a) + abs(B - b) + abs(C - c) + cost - 30
        r = []
        r += [solve(n - 1, l, a, b, c, cost)]
        r += [solve(n - 1, l, a + l[n - 1], b, c, cost + 10)]
        r += [solve(n - 1, l, a, b + l[n - 1], c, cost + 10)]
        r += [solve(n - 1, l, a, b, c + l[n - 1], cost + 10)]
        return min(r)

    print(solve(N, L, 0, 0, 0, 0))

import sys
if __name__ == '__main__':
    main(iter(sys.stdin.read().split("\n")))

Submission status: Accepted

Solution code:
#n 全探索問題
n, A, B, C = map(int, input().split())
L = [int(input()) for _ in range(n)]
INF = float('inf')
mixMP = 10

def rec(idx, a, b, c):
  if idx == n:
    return abs(A-a) + abs(B-b) + abs(C-c)  - mixMP*3 if min(a,b,c)>0 else INF
  res1 = rec(idx+1,a,b,c) #使わない #1本目のノード(dfs)
  res2 = rec(idx+1,a+L[idx],b,c) + mixMP #Aで使う #2本目のノード(dfs)
  res3 = rec(idx+1,a,b+L[idx],c) + mixMP #Bで使う #3本目のノード(dfs)
  res4 = rec(idx+1,a,b,c+L[idx]) + mixMP #Cで使う #4本目のノード(dfs)
  return min(res1, res2, res3, res4)

print(rec(0,0,0,0))

Submission status: Accepted

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

b4 = ["0"]*(4**n)
def B4(X):
  if X == "0":
    return "0"
  elif b4[X] != "0":
    return b4[X]
  if (int(X/4)):
    b4[X] = B4(int(X/4))+str(X%4)
    return B4(int(X/4))+str(X%4)
  else:
    b4[X] = str(X%4)
  return str(X%4)
def f(x):
  return [int(j) for j in ["0"]*(n-len(list(x)))+list(x)]

m = float('inf')
for i in range(4**n):
  t = f(B4(i))
  cost = 0
  for j in range(3):
    cost += 10*(max(t.count(j)-1,0))
  length = [0,0,0,0]
  for j in range(n):
    length[t[j]] += l[j]
  if min(length[0:3]) == 0:
    continue
  cost += abs(a-length[0])+abs(b-length[1])+abs(c-length[2])
  #print(t)
  #print(cost)
  m = min(m,cost)
print(m)

Submission status: Accepted

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:
    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)
  # A に使う場合
  ret1 = dfs(cur + 1, a + l[cur], b, c) + 10
  # B に使う場合
  ret2 = dfs(cur + 1, a, b + l[cur], c) + 10
  # C に使う場合
  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:
inf=10**9
n,A,B,C=map(int,input().split())
l=[int(input())  for i in range(n)]
def dfs(s,a,b,c):
    if n==s:
        return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c) >0 else inf
    ret0=dfs(s+1,a,b,c)
    ret1=dfs(s+1,a+l[s],b,c)+10
    ret2=dfs(s+1,a,b+l[s],c)+10
    ret3=dfs(s+1,a,b,c+l[s])+10
    return min((ret0,ret1,ret2,ret3))
print(dfs(0,0,0,0))

Submission status: Accepted

Solution code:
import itertools

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

case = list(itertools.product(range(4), repeat=N))
ans = 10 ** 7

for i in range(len(case)):
    if case[i].count(1) != 0 and case[i].count(2) != 0 and case[i].count(3) != 0:
        listA = []
        listB = []
        listC = []    
        for j in range(N):
            if case[i][j] == 1:
                listA.append(l[j])
            elif case[i][j] == 2:
                listB.append(l[j])
            elif case[i][j] == 3:
                listC.append(l[j])
        ansA = abs(sum(listA) - A) + 10 * (len(listA) - 1)
        ansB = abs(sum(listB) - B) + 10 * (len(listB) - 1)
        ansC = abs(sum(listC) - C) + 10 * (len(listC) - 1)
#        print(listA, listB, listC)
#        print(ansA, ansB, ansC)
        anstmp = ansA + ansB + ansC
#        print(anstmp)
        if anstmp < ans :
            ans = anstmp
    else:
        pass

print(ans)

Submission status: Accepted

Solution code:
N,A,B,C=map(int,input().split())
l=[int(input()) for i in range(N)]
inf=10**9
def dfs(cur,a,b,c):
  if cur==N:
    return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else inf
  ret0 = dfs(cur+1,a,b,c)
  ret1 = dfs(cur+1,a+l[cur],b,c)+10
  ret2 = dfs(cur+1,a,b+l[cur],c)+10
  ret3 = dfs(cur+1,a,b,c+l[cur])+10
  return min(ret0,ret1,ret2,ret3)
print(dfs(0,0,0,0))

Submission status: Accepted

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

answer = 3000
container = [0]*n
for _ in range(4**n):
    abcx = [0,0,0,0]
    for i in range(n):
        container[i] += 1
        if container[i] == 4:
            container[i] = 0
        else:
            break
    for i in range(n):
        for j in range(4):
            if container[i] == j:
                if j == 3:
                    abcx[j] += 1
                else:
                    abcx[j] += l[i]
    if abcx[0] == 0 or abcx[1] == 0 or abcx[2] == 0:
        continue
    else:
        need_mp = (n-3-abcx[3])*10 + abs(abcx[0]-a) + abs(abcx[1]-b) + abs(abcx[2]-c)
        if need_mp < answer:
            answer = need_mp

print(answer)

Submission status: Accepted

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

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

    x = dfs(cur+1, a, b, c)
    y = dfs(cur+1, a+l[cur], b, c)+10
    z = dfs(cur+1, a, b+l[cur], c)+10
    w = dfs(cur+1, a, b, c+l[cur])+10
    return min(x,y,z,w)

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

Submission status: Accepted

Solution code:
import sys

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

x=[int(input()) for _ in range(n)]
inf=10**9

def dfs(cur,a,b,c):
    if cur==n:
        #print("abs",abs(a-A),abs(b-B),abs(c-C))
        #print("abs...",abs(a-A)+abs(b-B)+abs(c-C)-30)
        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+x[cur],b,c) + 10
    ret2 = dfs(cur+1,a,b+x[cur],c) + 10
    ret3 = dfs(cur+1,a,b,c+x[cur]) + 10
    #print(ret0,ret1,ret2,ret3)
    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 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())
l=[int(input()) for _ in range(N)]
INF=10**4

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
    else:
        cost0 = dfs(cur+1,a,b,c)
        cost1 = dfs(cur+1,a+l[cur],b,c)+10
        cost2 = dfs(cur+1,a,b+l[cur],c)+10
        cost3 = dfs(cur+1,a,b,c+l[cur])+10
        return min(cost0,cost1,cost2,cost3)

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

Submission status: Accepted

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

def kado(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 10**9
  cal1=kado(cur+1,a,b,c)
  cal2=kado(cur+1,a+lis[cur],b,c)+10
  cal3=kado(cur+1,a,b+lis[cur],c)+10
  cal4=kado(cur+1,a,b,c+lis[cur])+10
  return min(cal1,cal2,cal3,cal4)
print(kado(0,0,0,0))

Submission status: Accepted

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

MP = []
#それぞれの竹を、４進数によって、「使用しない(0)」「Aに使用(1)」「Bに使用(2)」「Cに使用(3)」
#の４つに分類したい
start = 0 #最初は　00000000
end = (4**N)*1 #最後は　33333333
for n in range(start, end):
  n4 = ""
  while n > 0:
    mod = n % 4
    n4 = str(mod) + n4
    n //= 4
  if ("1" not in n4) or ("2" not in n4) or ("3" not in n4):
    continue
  if len(n4) < N:
    n4 = "0"*(N-len(n4)) + n4
  #print(n4)
  
  IA = [i for i, x in enumerate(n4) if x == "1"]
  IB = [j for j, y in enumerate(n4) if y == "2"]
  IC = [k for k, z in enumerate(n4) if z == "3"]
  
  LA = 0
  mp_A = -10
  for i in IA:
    LA += L[i]
    mp_A += 10
  mp_A += abs(A - LA)
  
  LB = 0
  mp_B = -10
  for i in IB:
    LB += L[i]
    mp_B += 10
  mp_B += abs(B - LB)
  
  LC = 0
  mp_C = -10
  for i in IC:
    LC += L[i]
    mp_C += 10
  mp_C += abs(C - LC)
  
  mp = mp_A + mp_B + mp_C
  MP.append(mp)
  #print(mp)

ans = min(MP)
print(ans)

Submission status: Accepted

Solution code:
N,A,B,C = map(int,input().split())
l = list(int(input())for i in range(N))
ans = float('inf')

def dfs(x,a,b,c):
    if x == N:
        return (abs(a-A) + abs(b-B) + abs(c-C) -30) if min(a,b,c)> 0 else float('inf')
    ret0 = dfs(x+1,a,b,c)
    ret1 = dfs(x+1,a+l[x],b,c)+10
    ret2 = dfs(x+1,a,b+l[x],c)+10
    ret3 = dfs(x+1,a,b,c+l[x])+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 i in range(n)]
inf = 10 ** 9

def dfs(i,a,b,c):
    if i == n:
        if min(a,b,c) > 0:
            return abs(a-A) + abs(b-B) + abs(c-C) - 30
        else:
            return inf
    ret0 = dfs(i+1, a, b, c)
    ret1 = dfs(i+1, a + l[i], b, c) + 10
    ret2 = dfs(i+1, a, b + l[i], c) + 10
    ret3 = dfs(i+1, a, b, c + l[i]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

Solution code:
def Base_10_to_n(X, n):
    X_dumy = X
    out = ''
    while X_dumy>0:
        out = str(X_dumy%n)+out
        X_dumy = int(X_dumy/n)
    return out

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

ans = float("inf")
for i in range(4**n):
    cnt = Base_10_to_n(i,4)
    cnt = cnt.rjust(n,"0")

    num_a = cnt.count("1")
    num_b = cnt.count("2")
    num_c = cnt.count("3")

    if num_a >= 1 and num_b >= 1 and num_c >= 1:
        bb_a = [l[j] for j in range(n) if cnt[j] == "1"]
        bb_b = [l[j] for j in range(n) if cnt[j] == "2"]
        bb_c = [l[j] for j in range(n) if cnt[j] == "3"]

        temp = 10 * (len(bb_a) + len(bb_b) + len(bb_c) -3) + abs(a-sum(bb_a)) + abs(b-sum(bb_b)) + abs(c-sum(bb_c))

        ans = min([ans, temp])

print(ans)

Submission status: Accepted

Solution code:

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

xs=[int(input()) for _ in range(n)]

inf = 10**9

def solve(aa, bb, cc, xs):
    if not xs:
        if min(aa,bb,cc) == 0:
           return inf
        else:
            return abs(a-aa) + abs(b-bb) + abs(c-cc) - 30

    x, *rest = xs
    res0 = solve(aa, bb, cc,     rest)
    res1 = solve(aa + x, bb, cc, rest) + 10
    res2 = solve(aa, bb + x, cc, rest) + 10
    res3 = solve(aa, bb, cc + x, rest) + 10

    return min(res0, res1, res2, res3)

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

Submission status: Accepted

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

def dfs(cur,a,b,c):
    if cur == n:
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
    ret0 = dfs(cur+1,a,b,c)
    ret1 = dfs(cur+1,a+l[cur],b,c) + 10
    ret2 = dfs(cur+1,a,b+l[cur],c) + 10
    ret3 = dfs(cur+1,a,b,c+l[cur]) + 10
    return min(ret0,ret1,ret2,ret3)

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

Submission status: Accepted

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

def dfs(used, a, b, c):
    if used == n: # n本の使い道を決め終わった
        return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF # -30は、合成してないときでも＋１０してるからそれの調整
    
    ret0 = dfs(used+1, a, b, c) # 竹iをどの材料にもしない場合
    ret1 = dfs(used+1, a + l[used], b, c) + 10 # 竹iをAの材料にする場合
    ret2 = dfs(used+1, a, b + l[used], c) + 10 # 竹iをBの材料にする場合
    ret3 = dfs(used+1, a, b, c + l[used]) + 10 # 竹iをCの材料にする場合
    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)]                                                   
                                                                                       
def base10to(x, y):  # xをy進数に                                                          
    if int(x/y):                                                                       
        return base10to(int(x/y), y)+str(x%y)                                          
    return str(x%y)                                                                    
                                                                                       
ans = float("inf")                                                                     
                                                                                       
for i in range(4**n):                                                                  
    i_base4 = base10to(i, 4)                                                           
    while len(i_base4)<n:                                                              
        i_base4 = "0"+i_base4                                                          
    if ("0" not in i_base4) or ("1" not in i_base4) or ("2" not in i_base4):           
        continue                                                                       
    memo = [0, 0, 0]  # [A, B, C]                                                      
    fusion = -3  # 合成回数                                                                
    for j in range(n):                                                                 
        if i_base4[j]=="3":                                                            
            continue                                                                   
        fusion += 1                                                                    
        memo[int(i_base4[j])] += l[j]                                                  
    mp = abs(memo[0]-a) + abs(memo[1]-b) + abs(memo[2]-c) + fusion*10                  
    ans = min(ans, mp)                                                                 
                                                                                       
print(ans)

Submission status: Accepted

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

def dfs(a,b,c,cnt):
#     print(a,b,c,cnt)
    if cnt >= len(l):
        if a==0 or b==0 or c==0:
            return 10**10
        return abs(a-A)+abs(b-B)+abs(c-C)
    
    res0 = dfs(a,b,c,cnt+1)
    if a>0:
        res1 = dfs(a+l[cnt],        b, c       ,cnt+1) + 10
    else:
        res1 = dfs(a+l[cnt],        b, c       ,cnt+1)
    
    if b>0:
        res2 = dfs(       a, b+l[cnt], c       ,cnt+1) + 10
    else:
        res2 = dfs(       a, b+l[cnt], c       ,cnt+1)

    if c>0:
        res3 = dfs(       a,        b, c+l[cnt],cnt+1) + 10
    else:
        res3 = dfs(       a,        b, c+l[cnt],cnt+1)
    
    return min(res0,res1,res2,res3)

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(a,b,c,cnt):
#     print(a,b,c,cnt)
    if cnt >= len(l):
        if a==0 or b==0 or c==0:
            return 10**10
        return abs(a-A)+abs(b-B)+abs(c-C)
    
    res0 = dfs(a,b,c,cnt+1)
    res1 = dfs(a+l[cnt],        b, c       ,cnt+1) + 10
    res2 = dfs(       a, b+l[cnt], c       ,cnt+1) + 10
    res3 = dfs(       a,        b, c+l[cnt],cnt+1) + 10
    
    return min(res0,res1,res2,res3)

print(dfs(0,0,0,0)-30)

Submission status: Accepted

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

def f(n, a, b, c):
    if n == N:
        if a == 0 or b == 0 or c == 0:
            return 1000000
        else:
            return abs(a - A) + abs(b - B) + abs(c - C) - 30
    x = f(n + 1, a, b, c)
    y = f(n + 1, a + l[n], b, c) + 10
    z = f(n + 1, a, b + l[n], c) + 10
    w = f(n + 1, a, b, c + l[n]) +10
    return min(x, y, z, w)

print(f(0, 0, 0, 0))

Submission status: Accepted

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

def dfs(num,a,b,c):
  if num == N:
    return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else INF
  ret0 = dfs(num+1,a,b,c)
  ret1 = dfs(num+1,a+take[num],b,c) + 10
  ret2 = dfs(num+1,a,b+take[num],c) + 10
  ret3 = dfs(num+1,a,b,c+take[num]) + 10
  return min(ret0,ret1,ret2,ret3)

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

Submission status: Accepted

Solution code:
n, a, b, c = [ int(v) for v in input().split() ]
num_list = [ int(input()) for i in range(n) ]
n_2 = 2 * n
def magic(inlist,barlength):
    return abs( barlength - sum(inlist) ) + ( 10 * ( len(inlist) - 1 ) )

ans = 10**9
for i in range(1,4**n):
    distribute_list = [ [] for i in range(3) ]
    s = bin(i)[2:].zfill(n_2)

    for j in range(0,n_2,2):
        p = int(s[j:j+2],2)
        if p != 0:
            distribute_list[p-1].append(num_list[j//2])
    if all(map(lambda x: x != [],distribute_list)):
        bar_sum = magic(distribute_list[0],a) + magic(distribute_list[1],b) + magic(distribute_list[2],c)
        ans = min(bar_sum,ans)
print(ans)

Submission status: Accepted

Solution code:
str_in = input().split()
num   = int(str_in[0])
tgt_a = int(str_in[1])
tgt_b = int(str_in[2])
tgt_c = int(str_in[3])

source = [int(input()) for i in range(num)]

mp = float('inf')

for i in range(1,2**num):

  sum_a = 0
  len_a = 0
  
  bin_i = format(i,'08b')  
  for t in range(1,num+1):
    sum_a += int(bin_i[-t])
    len_a += int(bin_i[-t]) * source[t-1]
    
  for j in range(1,2**num):
 
    sum_b = 0
    len_b = 0

    if (i & j) > 0:
      continue
    
    bin_j = format(j,'08b')
    for t in range(1,num+1):
      sum_b += int(bin_j[-t])
      len_b += int(bin_j[-t]) * source[t-1]

    for k in range(1,2**num):

      sum_c = 0
      len_c = 0
      
      if (i & k) > 0:
        continue
      elif(j & k) > 0:
        continue
        
      bin_k = format(k,'08b')        
      
      for t in range(1,num+1):
        sum_c += int(bin_k[-t])
        len_c += int(bin_k[-t]) * source[t-1]
        
      mp_try = abs(len_a - tgt_a) + abs(len_b - tgt_b) + abs(len_c - tgt_c) + (sum_a + sum_b + sum_c - 3) * 10 
      if(mp > mp_try):
        mp = mp_try

print (mp)

Submission status: Accepted

Solution code:
import itertools

N,A,B,C = (int(x) for x in input().split())
l_arr = []
for _ in range(N):
    l_arr.append(int(input()))

ans = 100000000
for ns in itertools.product([0,1,2,3], repeat=N):
    t_arr = [0,0,0,0]
    cost = -30
    for n,l in zip(ns, l_arr):
        t_arr[n] += l
        if n < 3:
            cost += 10
    if t_arr[0] == 0 or t_arr[1] == 0 or t_arr[2] == 0:
        continue
    cost += abs(A-t_arr[0]) + abs(B-t_arr[1]) + abs(C-t_arr[2])
    ans = min(ans, cost)

print(ans)