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:
n, a, b, c = map(int, input().split())
bamb = [int(input()) for _ in range(n)]
objective = [a, b, c]
ans = float("inf")
for i in range(4 ** n):
    tmp = 0
    material = [[] for _ in range(3)]
    for j in range(n):
        flag = (i >> (2 * j)) & 3
        if flag == 0:
            material[0].append(bamb[j])
        elif flag == 1:
            material[1].append(bamb[j])
        elif flag == 2:
            material[2].append(bamb[j])
    if len(material[0]) > 0 and len(material[1]) > 0 and len(material[2]) > 0:
        for k in range(3):
            tmp += (len(material[k]) - 1) * 10
            tmp += abs(sum(material[k]) - objective[k])
        ans = min(ans, tmp)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

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

INF = 10 ** 9
def f(idx, a, b, c):
    if idx == N:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a, b, c) > 0 else INF

    ret0 = f(idx + 1, a, b, c)
    ret1 = f(idx + 1, a + l[idx], b, c) + 10
    ret2 = f(idx + 1, a, b + l[idx], c) + 10
    ret3 = f(idx + 1, a, b, c + l[idx]) + 10
    return min(ret0, ret1, ret2, ret3)

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

Submission status: Accepted

Solution code:
n,a,b,c = map(int,input().split())
l = []
for i in range(n):
    l0 = int(input())
    l.append(l0)
bitl = []
for i in range(4**n):
    bit = [0 for i in range(n)]
    p = i
    q = 1
    for j in range(n):
        bit[j] = (p//q) % 4
        q *= 4
    bitl.append(bit)
mpl = []
for i in range(4**n):
    bit = bitl[i]
    takel = [[],[],[]]
    for j in range(n):
        if bit[j] > 0:
            takel[bit[j]-1].append(l[j])
    mp = 0
    al = len(takel[0])
    bl = len(takel[1])
    cl = len(takel[2])
    if al > 0:
        take = takel[0][0]
        for j in range(al-1):
            take += takel[0][j+1]
            mp += 10
        mp += abs(a - take)
        if bl > 0:
            take = takel[1][0]
            for j in range(bl-1):
                take += takel[1][j+1]
                mp += 10
            mp += abs(b - take)
            if cl > 0:
                take = takel[2][0]
                for j in range(cl-1):
                    take += takel[2][j+1]
                    mp += 10
                mp += abs(c - take)
                mpl.append(mp)
print(min(mpl))

Submission status: Accepted

Solution code:
INF = 10**10

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

def dfs(num,ai,bi,ci):
  if num==n:
    ret = abs(a-ai) + abs(b-bi) + abs(c-ci) - 30
    return ret if min(ai,bi,ci) >0 else INF
  t0 = dfs(num+1,ai,        bi,        ci)
  ta = dfs(num+1,ai+ll[num],bi,        ci)         +10
  tb = dfs(num+1,ai        ,bi+ll[num],ci)         +10 
  tc = dfs(num+1,ai        ,bi,        ci+ll[num]) +10
  return min(t0,ta,tb,tc)

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

Submission status: Accepted

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

costs = []


def add(xs, cost, index):
    if index < n:
        for i in range(4):
            new_xs = xs.copy()
            new_cost = cost
            if i < 3:
                if new_xs[i] > 0:
                    new_cost += 10
                new_xs[i] += ls[index]
            add(new_xs, new_cost, index + 1)
    else:
        xs.sort()
        if xs[0] > 0:
            cost += abs(c - xs[0]) + abs(b - xs[1]) + abs(a - xs[2])
            costs.append(cost)


add([0] * 3, 0, 0)
print(min(costs))

Submission status: Accepted

Solution code:
from itertools import product

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

def sum_ks(k_1, k_2, k_3):
    return abs(k_1-a) + abs(k_2-b) + abs(k_3-c)

min_ = 999999
for per in product([0, 1, 2, 3], repeat=n):
    if 1 in per and 2 in per and 3 in per:
        count = 0
        k_1 = 0
        k_2 = 0
        k_3 = 0
        for i in range(n):
            if per[i] == 0:
                count += 1
            if per[i] == 1:
                k_1 += l[i]
            if per[i] == 2:
                k_2 += l[i]
            if per[i] == 3:
                k_3 += l[i]
        cost = 10 * (n - 3 - count)
        min_ = min(min_, cost+sum_ks(k_1, k_2, k_3))
print(min_)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
ls = []
for i in range(N):
  l = int(input())
  ls.append(l)
rs = []
for i in range(4**N):
  hA = 0
  hB = 0
  hC = 0
  n = 0
  for j in range(N):
    if i%4 == 1:
      hA += ls[j]
      n += 1
    if i%4 == 2:
      hB += ls[j]
      n += 1
    if i%4 == 3:
      hC += ls[j]
      n += 1
    i = i//4
  if not(hA > 0 and hB > 0 and hC > 0):
    continue
  r = (n-3)*10+abs(hA-A)+abs(hB-B)+abs(hC-C)
  rs.append(r)
r = min(rs)
print(r)

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 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 i in range(n)]
goals = ['A', 'B', 'C', 'N']
from copy import copy
p = []

def search(i, goal, pre_abc):
  abc = copy(pre_abc)
  if goal == 'A':
    abc[0] += l[i]
  elif goal == 'B':
    abc[1] += l[i]
  elif goal == 'C':
    abc[2] += l[i]
  else:
    abc[3] += 1
  if i == len(l) - 1:
    if abc[0]!=0 and abc[1]!=0 and abc[2]!=0:
      p.append(abc)
  else:
    for goal in goals:
      search(i+1, goal, abc)
      
def mp(abc):
  return abs(abc[0]-a) + abs(abc[1]-b) + abs(abc[2]-c) + 10 * (n-3-abc[3])
      
def solve():
  for goal in goals:
    search(0, goal, [0,0,0,0])
  print(min(list(map(mp, p))))
  
solve()

Submission status: Accepted

Solution code:
N,A,B,C=[int(s) for s in input().split()]
ls=[int(input()) for i in range(N)]
def four(n):
  val_n=n
  val_ls=[0 for s in range(N)]
  for i in range(N):
    val_ls[i]=val_n%4
    val_n=val_n//4
  return val_ls
def mp(ls,n):
  if ls[0]==0:
    return 10000
  else:
    return 10*(ls[0]-1)+abs(ls[1]-n)
ans=30000
for i in range(4**N):
  take_ls=[[0,0],[0,0],[0,0],[0,0]] #A,B,C,None
  go_ls=four(i)
  for j in range(N):
    take_ls[go_ls[j]][0]+=1
    take_ls[go_ls[j]][1]+=ls[j]
  mp_use=mp(take_ls[0],A)+mp(take_ls[1],B)+mp(take_ls[2],C)
  if mp_use<ans:
    ans=mp_use
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 5 + 5)

INF = 10 ** 9

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


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

    r0 = dfs(current + 1, a, b, c)
    r1 = dfs(current + 1, a + L[current], b, c) + 10
    r2 = dfs(current + 1, a, b + L[current], c) + 10
    r3 = dfs(current + 1, a, b, c + L[current]) + 10

    return min(r0, r1, r2, r3)


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

Submission status: Accepted

Solution code:
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 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)


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

Submission status: Accepted

Solution code:
n, a, b, c = map(int, input().split(' '))
inf = 10**9
l = [int(input()) for _ in range(n)]
result = []

def dfs(counter, A, B, C):
    if counter == len(l):
        return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(A,B,C) > 0 else inf
    p_1 = dfs(counter+1, A+l[counter], B, C) + 10
    p_2 = dfs(counter+1, A, B+l[counter], C) + 10
    p_3 = dfs(counter+1, A, B, C+l[counter]) + 10
    p_4 = dfs(counter+1, A, B, C)
    return min(p_1,p_2,p_3,p_4)

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(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 #Aに使用する竹にaを追加
    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)]
MP = []
for i in range(4**n):
    i_bit = bin(i)[2:].zfill(2*n)
    co_a = 0
    co_b = 0
    co_c = 0
    p_a = 0
    p_b = 0
    p_c = 0
    for j in range(n):
        if i_bit[-1-2*j:-3-2*j:-1] == '00':
            co_a += L[j]
            p_a += 1
        elif i_bit[-1-2*j:-3-2*j:-1] == '01':
            co_b += L[j]
            p_b += 1
        elif i_bit[-1-2*j:-3-2*j:-1] == '10':
            co_c += L[j]
            p_c += 1
        else:
            continue
    if co_a*co_b*co_c!=0 and p_a<=a and p_b<=b and p_c<=c:
        MP += [10*(p_a+p_b+p_c-3)+abs(a-co_a)+abs(b-co_b)+abs(c-co_c)]
print(min(MP))

Submission status: Accepted

Solution code:
def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

N, A, B, C = map(int,input().split())
l = [int(input()) for i in range(N)]
com = []
ans = float('inf')

for i in range(4**N):
    x = Base_10_to_n(i, 4)
    if '1' in x and '2' in x and '3' in x:
        x = x.zfill(N)
        idx = 0
        a = []
        b = []
        c = []
        for i in x:
            i = int(i)
            if i == 1:
                a.append(l[idx])
            elif i == 2:
                b.append(l[idx])
            elif i == 3:
                c.append(l[idx])
            idx += 1
        MP = (len(a)+len(b)+len(c)-3) * 10
        MP += abs(sum(a)-A) + abs(sum(b)-B) + abs(sum(c)-C)
        ans = min(ans, MP)
print(ans)

Submission status: Accepted

Solution code:
def slove():
    import sys, itertools
    input = sys.stdin.readline
    n, a, b, c = list(map(int, input().rstrip('\n').split()))
    l = [int(input().rstrip('\n')) for _ in range(n)]
    ls = [0, 1, 2, 3]
    m_cost = 10 ** 10

    for it in itertools.product(ls, repeat=n):
        if 1 in it and 2 in it and 3 in it:
            lst = [0] * 4
            cost = 0
            for i, v in enumerate(it):
                lst[v] += l[i]
                if v == 1 or v == 2 or v == 3:
                    cost += 10
            cost -= 30
            cost += (abs(lst[1] - a) + abs(lst[2] - b) + abs(lst[3] - c))
            m_cost = min(m_cost, cost)
    print(m_cost)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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


x=10**9
#print(list(itertools.product([0,1,2,3], repeat=n)))
for i in list(itertools.product([0,1,2,3], repeat=n)):
    l2=[0]*3
    for j in range(n):
        if i[j]!=3:
            l2[i[j]]+=l1[j]
    l2=sorted(l2)
    if l2[0]!=0:
        l3=(len([_ for _ in i if _<3])-3)*10+abs(c-l2[0])+abs(b-l2[1])+abs(a-l2[2])
        if l3<x:
            x=l3

print(x)

Submission status: Accepted

Solution code:
import itertools

A = [0, 0, 0, 0]
N,A[0],A[1],A[2] = map(int,input().split())
bamboo_list = [int(input()) for n in range(N)]
ans = 10**15

for seq in itertools.product(range(4),repeat=N): ## seq:手元の竹を、どのように使うかのList
  X = [0, 0, 0] ## それぞれのターゲットの竹に使った竹の合計長さ
  Y = [0, 0, 0] ## それぞれのターゲットの竹に利用した竹の本数
  
  for n in range(N):
    if seq[n] != 3:
      X[seq[n]] += bamboo_list[n]
      Y[seq[n]] += 1
  
  ## 竹を一本も使わない、みたいな組み合わせがあった場合はスルーする
  if Y[0]*Y[1]*Y[2] == 0:
    continue
  
  ## このループでの消費MPを計算する
  MP = (sum(Y)-3)*10
  for i in range(3):
    MP += abs(X[i]-A[i])
  
  
  ## 消費MPが最小だった場合、それを答えとして格納する
  if MP < ans:
    ans = MP

print(ans)

Submission status: Accepted

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

# 0, 1, 2, 3(使わない) で前通り試す
from itertools import product

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

print(answer)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline

def main():
    N,A,B,C = map(int, input().split())
    L = []
    for _ in range(N):
        L.append(int(input()))
    mi = 1000*N
    for i in range(4**N):
        mp = 0
        a = b = c = 0
        x = 0
#        print(bin(i) + " ", end="")
        while i > 0:
            if i%4 == 1:
                if a > 0:
                    mp += 10
                a += L[x]
            if i%4 == 2:
                if b > 0:
                    mp += 10
                b += L[x]
            if i%4 == 3:
                if c > 0:
                    mp += 10
                c += L[x]
            i >>= 2
            x += 1
        if a > 0 and b > 0 and c > 0:
            mi = min(mi, mp + abs(A-a) + abs(B-b) + abs(C-c))
#            print(a,b,c,mi)
    print(mi)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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 10**9
  ret0 = dfs(cur+1, a+ls[cur], b, c)+10
  ret1 = dfs(cur+1, a, b+ls[cur], c)+10
  ret2 = dfs(cur+1, a, b, c+ls[cur])+10
  ret3 = dfs(cur+1, a, b, c)
  return min(ret0, ret1, ret2, ret3)
print(dfs(0,0,0,0))

Submission status: Accepted

Solution code:
# 竹1, 2, 3, 使わない の4種類
# 4 ** 8 = 65536

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

INF = 10 ** 18
answer = INF

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

Submission status: Accepted

Solution code:
n, a, b, c = list(map(int, input().split()))
l = []
for i in range(n):
    l.append(int(input()))
min = 10**20
def dfs(depth, v):
    if depth >= n:
        if 0 in v and 1 in v and 2 in v:
            calc(v)
    else:
        for i in range(4):
            v[depth] = i
            dfs(depth + 1, v)

def calc(array):
    A = 0
    B = 0
    C = 0
    count = -3
    global min
    for i in range(n):
        if array[i] == 0:
            A += l[i]
            count += 1
        elif array[i] == 1:
            B += l[i]
            count += 1
        elif array[i] == 2:
            C += l[i]
            count += 1
    if min > abs(A - a) + abs(B - b) + abs(C - c) + count * 10 :
        min = abs(A - a) + abs(B - b) + abs(C - c) + count * 10

v = [0 for i in range(n)]
dfs(0,v)
print(min)

Submission status: Accepted

Solution code:
N, A, B, C = map(int, input().split())
l = [int(input()) for i in range(N)]
mp = []
syn = [str(i) for i in range(4)]
temp = []
for k in range(N - 1):
    for i in syn:
        for j in range(4):
            temp.append(i + str(j))
    syn.clear()
    syn.extend(temp)
    temp.clear()

syn_list = [i for i in syn if '1' in i and '2' in i and '3' in i]

for i in syn_list:
    A_l = 0
    B_l = 0
    C_l = 0
    for j in range(N):
        if i[j] == '1':
            A_l += l[j]
        elif i[j] == '2':
            B_l += l[j]
        elif i[j] == '3':
            C_l += l[j]
        else:
            continue
    mp.append(abs(A_l - A) + abs(B_l - B) + abs(C_l - C) + (i.count('1') + i.count('2') + i.count('3') - 3) * 10)
print(min(mp))

Submission status: Accepted

Solution code:
def main():
    import itertools
    import sys
    input = sys.stdin.readline
    N, A, B, C = map(int, input().split())
    D = [A,B,C]
    L = [int(input()) for i in range(N)]
    L = sorted(L)
    X = [[],[],[],[]]
    l = [0,1,2,3]
    p = itertools.product(l, repeat = N)
    q = itertools.product()
    ans = float("inf")
    for v in p:
        tmp = 0
        X = [[],[],[],[]]
        for i in range(len(v)):
            X[v[i]].append(L[i])
        if X[0] != [] and X[1] != [] and X[2] != []:
            for j in itertools.permutations(D,3):
                for k in range(3):
                    tmp += 10* (len(X[k])-1) + abs(j[k] - sum(X[k]))
                if tmp < ans:
                    ans = tmp
    print(ans)
    
    
main()

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


NABC=list(map(int, input().split()))
l = [int(input()) for _ in range(NABC[0])]
l.sort(reverse=True)
done = [0]*NABC[0]

ans = 1e+8
for i in range(4**NABC[0]):
    A=0
    B=0
    C=0
    A_count=-1
    B_count=-1
    C_count=-1
    i_tmp = i
    for j in range(NABC[0]):
        bit = i_tmp%4
        i_tmp = i_tmp//4
        if bit == 0: 
            A += l[j]
            A_count += 1
        if bit == 1:
            B += l[j]
            B_count += 1
        if bit == 2:
            C += l[j]
            C_count += 1
    if A_count==-1 or B_count==-1 or C_count==-1: continue
    tmp = abs(A-NABC[1])+abs(B-NABC[2])+abs(C-NABC[3])+max(0,A_count*10)+max(0,B_count*10)+max(0,C_count*10)
    if tmp < ans:
        ans = tmp
print(ans)

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

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

Submission status: Accepted

Solution code:
def main():
    N, A, B, C = map(int, input().split())
    l = [int(input()) for i in range(N)]
    
    ans = int(1e10)
    for i in range(4**N):
        tmp = [0, 0, 0, 0]# 0:a, 1:b, 2:c, 3:使用しない
        count = [0, 0, 0, 0]
        for j in range(N):
            tmp[i&3] += l[j]
            count[i&3] += 1
            i >>= 2
        if tmp[0] > 0 and tmp[1] > 0 and tmp[2] > 0:# a, b,c 用の竹が各一本以上ある
            mp = abs(tmp[0] - A) + abs(tmp[1] - B) + abs(tmp[2] - C) + (count[0]+count[1]+count[2]-3) * 10
            # print(mp)
            ans = min(ans, mp)
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def dfs(depth):
    if depth == N:
        ans.append(cal(v))
        return
    for i in range(4):
        v[depth] = i
        dfs(depth+1) 

def cal(v):
    tmp = [0] * 3 
    for i in range(N):
        if v[i] == 0:
            tmp[0] += l[i]
        elif v[i] == 1:
            tmp[1] += l[i]
        elif v[i] == 2:
            tmp[2] += l[i]
    cnt = 0
    if v.count(0) > 1:
        cnt += v.count(0) - 1
    if v.count(1) > 1:
        cnt += v.count(1) - 1
    if v.count(2) > 1:
        cnt += v.count(2) - 1

    if v.count(0) == 0 or v.count(1) == 0 or v.count(2) == 0:
        return float('inf')
    diff = cnt * 10
    for i in range(3):
        diff += abs(A[i]-tmp[i])
    
    return diff
        
    
    

N, *A= map(int, input().split())
l = [int(input()) for _ in range(N)]
v = [-1] * N

ans = []

dfs(0)
print(min(ans))

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################


# https://atcoder.jp/contests/abc119/tasks/abc119_c


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:
import sys
import math
from fractions import gcd
# import queue
# from collections import Counter
# from itertools import accumulate
# from functools import reduce

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

def combination_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def permutations_count(n, r):
    return math.factorial(n) // math.factorial(n - r)

big_prime = 1000000007

"""
# 標準入力取得
## 文字列
 =  sys.stdin.readline().rstrip()
 =  list(sys.stdin.readline().rstrip())

## 数値
 =  int(sys.stdin.readline())
 =  map(int, sys.stdin.readline().split())
 =  list(map(int, sys.stdin.readline().split()))
 =  [list(map(int,list(sys.stdin.readline().split()))) for i in range(N)]
"""

N, A, B, C =  map(int, sys.stdin.readline().split())
l = [int(sys.stdin.readline()) for i in range(N)]
INF = 10 ** 9

def dfs(idx, a, b, c):
    if idx >= N:
        return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else INF
    n_t = dfs(idx + 1, a, b, c)
    a_t = dfs(idx + 1, a + l[idx], b, c) + 10
    b_t = dfs(idx + 1, a, b + l[idx], c) + 10
    c_t = dfs(idx + 1, a, b, c + l[idx]) + 10
    return min(n_t, a_t, b_t, c_t)

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

Submission status: Accepted

Solution code:
N, A, B, C = map(int,input().split())
ABC = [0, A,B,C]
L = [int(input()) for _ in range(N)]
import itertools
ans = 100000000
for flags in itertools.product(range(4), repeat=N):
    tmp = [[0, 0] for _ in range(4)]
    for i in range(N):
        tmp[flags[i]][0] += L[i]
        tmp[flags[i]][1] += 1
    mp = 0
    for i in range(1, 4):
        if tmp[i][1] == 0:
            mp = 100000000
            break
        mp += abs(ABC[i] - tmp[i][0])
        mp += (tmp[i][1] - 1) * 10
    if mp < ans:
        ans = mp
print(ans)

Submission status: Accepted

Solution code:
import math
import copy
import sys
import fractions
import numpy as np
from functools import reduce

# ===FUNCTION===

def getInputNum():
    inputNum = int(input())
    return inputNum

def getInputListInt():
    outputoData = []
    inputData = input().split()
    outputData = [int(n) for n in inputData]

    return outputData

def getSomeInputInt(n):
    outputDataList = []
    for i in range(n):
        inputData = int(input())
        outputDataList.append(inputData)

    return outputDataList

def getSomeInputListInt(n):
    inputDataList = []
    outputDataList = []
    for i in range(n):
        inputData = input().split()
        inputDataList = [int(n) for n in inputData]
        outputDataList.append(inputDataList)

    return outputDataList

# ===CODE===

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

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

    nothing = dfs(cnt+1, a, b, c)
    ap = dfs(cnt+1, a+data[cnt], b, c) + 10
    bp = dfs(cnt+1, a, b+data[cnt], c) + 10
    cp = dfs(cnt+1, a, b, c + data[cnt]) + 10

    return min(nothing, ap, bp, cp)

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, A, B, C = MAP()
l = [INT() for _ in range(N)]

def dfs(cur, a, b, c):
    if cur == N: # 最初の1つを入れた分の10*3を引く
        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  # Aに加える
    ret2 = dfs(cur+1, a, b+l[cur], c) + 10  # Bに加える
    ret3 = dfs(cur+1, a, b, c+l[cur]) + 10  # Cに加える
    return min(ret0, ret1, ret2, ret3)
print(dfs(0, 0, 0, 0))

Submission status: 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:
        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))

Submission status: Accepted

Solution code:
def calc_cost( data_list, target ):
    num = len(data_list)
    if num == 0:
        return -1
    length = sum( data_list )
    cost = (num - 1) * 10 
    cost += abs( target - length)
    return cost 

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

for _ in range(N):
    data.append(int(input()))

ans = -1
for n in range(4**N):
    pat = n 
    data_a = []
    data_b = []
    data_c = []

    for i in range( N ):
        l = pat % 4
        if l == 0:
            pass
        elif l == 1:
            data_a.append(data[i])
        elif l == 2:
            data_b.append(data[i])
        elif l == 3:
            data_c.append(data[i])
        pat //= 4

    cost_a = calc_cost( data_a, A)
    cost_b = calc_cost( data_b, B)
    cost_c = calc_cost( data_c, C)

    if cost_a != -1 and cost_b != -1 and cost_c != -1:
        cost = cost_a+cost_b+cost_c
        if ans == -1:
            ans = cost
        else:
            if ans > cost:
                ans = cost

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
sys.setrecursionlimit(200000)
input = sys.stdin.readline

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

ans = 10 ** 18
def dfs(index, z, al):
    global ans
    if len(al) == N:
        if len(set(al) - {0}) < 3:
            return

        m = {
            1: 0,
            2: 0,
            3: 0,
        }
        mp = 0
        for i in range(N):
            v = al[i]
            if v == 0:
                continue
            if m[v] != 0:
                mp += 10
            m[v] += L[i]
        ans = min(ans, mp + abs(A - m[1]) + abs(B - m[2]) + abs(C - m[3]))
        return

    dfs(index + 1, 0, al + [0])
    dfs(index + 1, 1, al + [1])
    dfs(index + 1, 2, al + [2])
    dfs(index + 1, 3, al + [3])
    return

dfs(0, 0, [])

print(ans)

Submission status: Accepted

Solution code:
from itertools import product
N, a, b, c = map(int, input().split())
L = []
for i in range(N):
  L.append(int(input()))

abc = [0,a,b,c]
ch = [0,1,2,3]
cal = []

for i in product(ch,repeat=N):
  calc = 0
  su = [0,0,0,0]
  ct = [0,0,0,0]
  temp = list(i)
  for j in range(N):
    su[temp[j]] += L[j]
    ct[temp[j]] += 1
  if (ct[1]==0)|(ct[2]==0)|(ct[3]==0):
    continue
  for k in range(1,4):
    calc += abs(abc[k]-su[k])
    calc += max((ct[k]-1),0)*10
  cal.append(calc)
    
print(min(cal))

Submission status: Accepted

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


def ten_to_n(X, n):
    if (int(X / n)):
        return ten_to_n(int(X / n), n) + str(X % n)
    return str(X % n)

ans = float("inf")
for i in range(4**N):
    I = ten_to_n(i, 4)
    a, b, c = 0, 0, 0
    temp = 0
    for j in range(min(len(I), N)):
        J = I[-j-1]
        if J == "1":
            a += L[j]
            temp += 10
        elif J == "2":
            b += L[j]
            temp += 10
        elif J == "3":
            c += L[j]
            temp += 10
    if a == 0 or b == 0 or c == 0:
        continue
    temp += abs(A - a) + abs(B - b) + abs(C - c) - 30
    ans = min(ans, temp)
print(ans)

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,x):
    if i==N:
        return(abs(A-a)+abs(B-b)+abs(C-c)+10*(x-3))if a*b*c!=0 else 10**9
    ret1=dfs(i+1,a,b,c,x)
    ret2=dfs(i+1,a+l[i],b,c,x+1)
    ret3=dfs(i+1,a,b+l[i],c,x+1)
    ret4=dfs(i+1,a,b,c+l[i],x+1)
    return(min(ret1,ret2,ret3,ret4))
print(dfs(0,0,0,0,0))