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: Runtime Error

Solution code:
from itertools import product 
n,a,b,c = map(int,input().split())
l = [int(input()) for _ in range(n)]
tar = [a,b,c]
ans = float("inf")
for p in product(range(4),repeat=8):
    mp = 0
    length = [0]*3
    for i,j in enumerate(p):
        if j == 3: continue
        if length[j] > 0: mp += 10
        length[j] += l[i]
    for i in range(3):
        if length[i] == 0: break
        mp += abs(length[i]-tar[i])
    else:
        ans = min(ans, mp)
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import permutations
from copy import deepcopy
from collections import Counter

n,a,b,c = map(int,input().split())
take = []
for _ in range(n):
	take.append(int(input()))
tmp = deepcopy(take)
ans_list = []

for abc in permutations([a,b,c],3):
	take = deepcopy(tmp)
	sum_take = sum(take)
	ans = 0
	l = []
	l2 = []
	s = Counter(take)
	#print(s)
	for x in abc:
		res = []
		l2 = []
		take = []
		for i,num in s.items():
			for j in range(num):
				take.append(i)
		#take = list(s)
		for i in range(2**n):
			l = []
			for j in range(len(take)):
					if i & (1<<j):
						l.append(take[j])
			if len(l) == 0:
				continue
			res.append(10*(len(l)-1) + abs(sum(l) - x))
			l2.append(l)
		minimum = sum_take
		for i in range(len(res)):
			if minimum > res[i]:
				minimum = res[i]
				s2 = l2[i]
		for i in range(len(s2)):
			s[s2[i]] -= 1
			if s[s2[i]] == 0:
				s.pop(s2[i])
		#s = s - s2
		ans += minimum
		#print(res,s,minimum)
	ans_list.append(ans)

print(min(ans_list))

Submission status: Runtime Error

Solution code:
from itertools import permutations
from copy import deepcopy

n,a,b,c = map(int,input().split())
take = []
for _ in range(n):
	take.append(int(input()))
tmp = deepcopy(take)
ans_list = []
for abc in permutations([a,b,c],3):
	take = deepcopy(tmp)
	sum_take = sum(take)
	ans = 0
	l = []
	l2 = []
	s = set(take)
	#print(s)
	for x in abc:
		res = []
		l2 = []
		take = list(s)
		for i in range(2**n):
			l = []
			for j in range(len(take)):
					if i & (1<<j):
						l.append(take[j])
			if len(l) == 0:
				continue
			res.append(10*(len(l)-1) + abs(sum(l) - x))
			l2.append(set(l))
		minimum = sum_take
		for i in range(len(res)):
			if minimum > res[i]:
				minimum = res[i]
				s2 = l2[i]
		s = s - s2
		ans += minimum
		#print(res,s,minimum)
	ans_list.append(ans)

print(min(ans_list))

Submission status: Runtime Error

Solution code:
from itertools import permutations
from copy import deepcopy

n,a,b,c = map(int,input().split())
take = []
for _ in range(n):
	take.append(int(input()))
tmp = deepcopy(take)
ans_list = []
for abc in permutations([a,b,c],3):
	sum_take = sum(take)
	ans = 0
	l = []
	l2 = []
	s = set(tmp)
	#print(s)
	for x in abc:
		res = []
		l2 = []
		take = list(s)
		for i in range(2**n):
			l = []
			for j in range(len(take)):
					if i & (1<<j):
						l.append(take[j])
			if len(l) == 0:
				continue
			res.append(10*(len(l)-1) + abs(sum(l) - x))
			l2.append(set(l))
		minimum = sum_take
		for i in range(len(res)):
			if minimum > res[i]:
				minimum = res[i]
				s2 = l2[i]
		s = s - s2
		ans += minimum
		#print(res,s,minimum)
	ans_list.append(ans)


print(min(ans_list))

Submission status: Runtime Error

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)
  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: Runtime Error

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)
  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: Runtime Error

Solution code:
@n, @a, @b, @c = gets.chomp.split(" ").map(&:to_i)

@l = []
@n.times do
  @l << gets.chomp.to_i
end
INF = 10**9

def dfs(cur, a, b, c)
  if cur == @n
    if [a, b, c].min > 0
      return (a - @a).abs + (b - @b).abs + (c - @c).abs - 30
    else
      return INF
    end
  end
  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 [ret0, ret1, ret2, ret3].min
end

puts dfs(0, 0, 0, 0)

Submission status: Runtime Error

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: Runtime Error

Solution code:
import sys
from functools import lru_cache
inf = float('inf')

n, A, B, C, *l = map(int, sys.stdin.read().split())

@lru_cache
def cost(a, b, c, cnt, i):
    if i == n:
        if not (a and b and c): return inf
        return abs(A - a) + abs(B - b) + abs(C - c) + 10 * (cnt - 3)
    return min(cost(a, b, c, cnt, i+1), 
               cost(a+l[i], b, c, cnt+1, i+1),
               cost(a, b+l[i], c, cnt+1, i+1),
               cost(a, b, c+l[i], cnt+1, i+1))

def main():
    print(cost(0, 0, 0, 0, 0))

if __name__ ==  '__main__':
    main()

Submission status: Runtime Error

Solution code:
from math import gcd
k = int(input())
ans = 0
for i in range(1,k+1):
    for j in range(1,k+1):
        for l in range(1,k+1):
            f = gcd(i, j)
            ans += gcd(f, l)
print(ans)

Submission status: Runtime Error

Solution code:
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(A-x[0])
  MP += abs(B-x[1])
  MP += abs(C-x[2])
  if answer > MP:
    answer = MP
print(answer

Submission status: Runtime Error

Solution code:
def Base_10_to_n(x,n):
  tmp=x
  out=''
  while tmp>0:
    out=str(tmp%n)+out
    tmp=int(tmp/n)
  return out
n,a,b,c=map(int,input().split())
l=[int(input()) for i in range(n)]
ans=10**18
for i in range(4**n):
  cost=0
  d=[[] for i in range(4)]
  s=Base_10_to_n(i,4).zfill(n)
  for j in range(n):
    k=int(s[j])
    d[k].append(l[j])
  if len(d[1])*len(d[2])*len(d[3])==0:
    continue
  for i in range(1,4):
    cost+=(len(d[k])-1)*10
  d1=sum(d[1])
  d2=sum(d[2])
  d3=sum(d[3])
  l=[abs(d1-a)+abs(d2-b)+abs(d3-c),abs(d1-a)+abs(d2-c)+abs(d3-b),abs(d1-b)+abs(d2-a)+abs(d3-c),abs(d1-b)+abs(d2-c)+abs(d3-a),abs(d1-c)+abs(d2-a)+abs(d3-b),abs(d1-c)+abs(d2-b)+abs(d3-a)]
  cost=min(l)
  ans=min(ans,cost)
print(ans)

Submission status: Runtime Error

Solution code:
n,a,b,c = map(int,input().split())
l = [int(input()) for _ in range(n)]
ans = 10000
for i in range(2 ** (2*n)):
  v = [0,0,0,0]
  assign = [(i>>(2*j)) & 3 for j in range(n)]
  cost = 0
  for j in range(n):
    is v[assign[j]] > 0:
      cost += 10
    v[assign[j]] += l[j]
  if v[1] == 0 or v[2] == 0 or v[3] == 0:
    continue
  ans = min(ans, cost + abs(v[1]-a) + abs(v[2]-b) + abs(v[3]-c))
print(ans)

Submission status: Runtime Error

Solution code:
N, A, B, C, *L = map(int, open('0').read().split())
ans = float('inf')
for i in range(4**N):
  a = 0
  b = 0
  c = 0
  m = 0
  for j in range(N):
    if i%4==0:
      if a!=0:
        m += 10
      a += L[j]
    elif i%4==1:
      if b!=0:
        m += 10
      b += L[j]
    elif i%4==2:
      if c!=0:
        m += 10
      c += L[j]
    i //= 4
  if a==0 or b==0 or c==0:
    continue
  ans = min(ans, m+abs(A-a)+abs(B-b)+abs(C-c))
print(ans)

Submission status: Runtime Error

Solution code:
N, A, B, C=map(int,input().split())2l=[int(input())foriinrange(N)]3INF=10**945defdfs(cur, a, b, c):6ifcur==N:7returnabs(a-A)+abs(b-B)+abs(c-C)-30ifmin(a, b, c)>0elseINF8ret0=dfs(cur+1, a, b, c)9ret1=dfs(cur+1, a+l[cur], b, c)+1010ret2=dfs(cur+1, a, b+l[cur], c)+1011ret3=dfs(cur+1, a, b, c+l[cur])+1012returnmin(ret0, ret1, ret2, ret3)1314print(dfs(0,0,0,0))

Submission status: Runtime Error

Solution code:
l=[]

def detain(n,a,b,c,l):
    
    n,a,b,c=map(int,input().split())
    for i in range(n):
        li=int(input())
        l.append(li)
    return


detain(n,a,b,c,l)
d=[a,b,c]
d.sort()

mpmin=sum(l)*4+len(l)*10
for i in range(4**n):
    k=[]
    kj=i%4
    k.append(kj)
    b=int((i-kj)/4)
    l0=[]
    l1=[]
    l2=[]
    l3=[]
    if kj==0:
        l0.append(l[0])
    elif kj==1:
        l1.append(l[0])
    elif kj==2:
        l2.append(l[0])
    elif kj==3:
        l3.append(l[0])
        
    for j in range(n-1):
        kj=b%4
        k.append(kj)
        b=int((b-kj)/4)
        if kj==0:
            l0.append(l[j+1])
        elif kj==1:
            l1.append(l[j+1])
        elif kj==2:
            l2.append(l[j+1])
        elif kj==3:
            l3.append(l[j+1])
    mp=0
    if len(l0)>=1:
        mp=mp+(len(l0)-1)*10
    if len(l1)>=1:
        mp=mp+(len(l1)-1)*10
    if len(l2)>=1:
        mp=mp+(len(l2)-1)*10
    ls=[]
    ls=[sum(l0),sum(l1),sum(l2)]
    ls.sort()
    mp=mp+abs(d[0]-ls[0])+abs(d[1]-ls[1])+abs(d[2]-ls[2])    
    if mpmin>mp and len(l0)>=1 and len(l1)>=1 and len(l2)>=1:
        mpmin=mp
print(mpmin)

Submission status: Runtime Error

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

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

ans=10**18
def cost_calc(pp,TAKEs):
    if TAKEs==[]:
        return 10**8
    
    else:
        ans=0
        ans+=(len(TAKEs)-1)*10
        ans+=abs(pp-sum(TAKEs))
        return ans

assign=[]#[A,B,C,none]

for pattern in product([0,1,2,3],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: Runtime Error

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: Runtime Error

Solution code:
# coding: utf-8

list = [int(i) for i in input().rstrip().split(" ")]

matsu_count = list[0]
require = sorted(list[1:])

matsu_list = sorted([int(input().rstrip()) for i in range(matsu_count)])

def get_mp(matsu:list):
    ans = 0
    for i in range(3):
        ans += abs(matsu[i] - require[i])
    return ans
mp = {}
def get_ans(matsu:list):
    if len(matsu) == 3:
        return get_mp(matsu)
    key = "#".join(matsu)
    if key in mp:
        return mp[key]
    ans = 80000
    for i in range(len(matsu) - 3 + 1):
        ans = min(ans, get_mp(matsu[i:]))
    for i in range(len(matsu)):
        for j in range(i + 1, len(matsu)):
            sub_matsu = []
            for k in range(len(matsu)):
                if k == i or k == j:
                    continue
                else:
                    sub_matsu.append(matsu[k])
            sub_matsu.append(matsu[i] + matsu[j])
            ans = min(ans, get_ans(sorted(sub_matsu)) + 10)

    mp[key] = ans
    return ans

ans = get_ans(matsu_list)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit("10**7")

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: Runtime Error

Solution code:
import sys
sys.setrecursionlimit("10**7")

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: Runtime Error

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: Runtime Error

Solution code:
N,A,B,C = map(int,input().split())
ABC = [A,B,C]
l = [int(input()) for _ in range(N)]
ans = 0
for i in range(N):
    if l[i]-10 <= A <= l[i]+10:
        ans += abs(l[i]-A)
        del l[i]
        del ABC[0]
        N -= 1
    elif l[i]-10 <= B <= l[i]+10:
        ans += abs(l[i]-B)
        N -= 1
        del ABC[1]
        del l[i]
    elif l[i]-10 <= C <= l[i]+10:
        ans += abs(l[i]-C)
        del l[i]
        N -= 1
        del ABC[2]

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

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

#print(L)

mp = 0
for i in [C, B, A]:
	same = False
	for l in L:
		if l==i:
			L.remove(l)
			same = True
	if same:
		continue
	elif min(L) >= i:
		#print('min',i,min(L)-i)
		mp += min(L)-i
		L.remove(min(L))
	elif max(L) <= i and i - max(L) <= 10:
		#print('max',i,i-max(L))
		mp += i-max(L)
		L.remove(max(L))
	else:
		dif = float('INF')
		dif_idx = []
		for j in range(2**len(L)):
			tmp = []
			for k in range(len(L)):
				if (j>>k & 1):
					tmp.append(len(L)-k-1)
			tmpsum = sum([L[t] for t in tmp])
			if abs(i-tmpsum)<dif:
				dif = abs(i-tmpsum)
				dif_idx=tmp
		#print(dif_idx, [L[t] for t in dif_idx])
		#print('dif',dif_idx, [L[t] for t in dif_idx],abs(sum([L[t] for t in dif_idx])-i))
		mp += abs(sum([L[t] for t in dif_idx])-i) + (len(dif_idx)-1)*10
		for t in dif_idx:
			L.pop(t)

print(mp)
#print(L)

Submission status: Runtime Error

Solution code:
from sys import setrecursionlimit
setrecursionlimit(1000000)

def permute(a, idx):
    if idx == len(a):
        permutations.append(a[::])
    for i in range(idx, len(a)):
        a[i], a[idx] = a[idx], a[i]
        permute(a, idx + 1)
        a[i], a[idx] = a[idx], a[i]

def doit(a, b, c, idx):
    if (a, b, c, idx) in memo:
        return memo[a, b, c, idx]
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    memo[a, b, c, idx] = min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )
    return memo[a, b, c, idx]

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    permutations = []
    permute(abc, 0)
    for p in permutations:
      	memo = {}
        ret = min(ret, doit(p[0], p[1], p[2], 0))
print(ret)

Submission status: Runtime Error

Solution code:
from sys import setrecursionlimit
setrecursionlimit(1000000)

def permute(a, idx):
    if idx == len(a):
        permutations.append(a[::])
    for i in range(idx, len(a)):
        a[i], a[idx] = a[idx], a[i]
        permute(a, idx + 1)
        a[i], a[idx] = a[idx], a[i]

def doit(a, b, c, idx):
    if (a, b, c, idx) in memo:
        return memo[a, b, c, idx]
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    memo[a, b, c, idx] = min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )
    return memo[a, b, c, idx]

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
memo = {}
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    permutations = []
    permute(abc, 0)
    for p in permutations:
        ret = min(ret, doit(*p, 0))
print(ret)

Submission status: Runtime Error

Solution code:
from functools import lru_cache
from sys import setrecursionlimit
setrecursionlimit(1000000)

def permute(a, idx):
    if idx == len(a):
        permutations.append(a[::])
    for i in range(idx, len(a)):
        a[i], a[idx] = a[idx], a[i]
        permute(a, idx + 1)
        a[i], a[idx] = a[idx], a[i]

@lru_cache(None)
def doit(a, b, c, idx):
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    return min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    permutations = []
    permute(abc, 0)
    for p in permutations:
        ret = min(ret, doit(*p, 0))
print(ret)

Submission status: Runtime Error

Solution code:
from itertools import permutations
from functools import lru_cache
from sys import setrecursionlimit
setrecursionlimit(1000000)

#@lru_cache(None)
def doit(a, b, c, idx):
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    return min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    for p in permutations(abc):
        ret = min(ret, doit(*p, 0))
print(ret)

Submission status: Runtime Error

Solution code:
from itertools import permutations
from functools import lru_cache
from sys import setrecursionlimit

setrecursionlimit(1000000)

@lru_cache(None)
def doit(a, b, c, idx):
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    return min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    for p in permutations(abc):
        ret = min(ret, doit(*p, 0))
print(ret)

Submission status: Runtime Error

Solution code:
from itertools import permutations
from functools import lru_cache

@lru_cache(None)
def doit(a, b, c, idx):
    if idx == len(unused):
        return abs(a - needA) + abs(b - needB) + abs(c - needC)
    return min(
        doit(a, b, c, idx + 1),
        doit(a + unused[idx], b, c, idx + 1) + 10,
        doit(a, b + unused[idx], c, idx + 1) + 10,
        doit(a, b, c + unused[idx], idx + 1) + 10
    )

n, needA, needB, needC = map(int, input().split())
a = [int(input()) for _ in range(n)]
ret = float('inf')
for mask in range(1 << n):
    if bin(mask).count('1') != 3:
        continue
    abc = [a[i] for i in range(n) if (mask >> i) & 1]
    unused = [a[i] for i in range(n) if not (mask >> i) & 1]
    for p in permutations(abc):
        ret = min(ret, doit(*p, 0))
print(ret)

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
ans=0
while all(a%2==0 for a in A):
  for i in range(n):
    A[i]=A[i]/2
    ans+=1
print(ans)

Submission status: Runtime Error

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: Runtime Error

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: Runtime Error

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: Runtime Error

Solution code:
import numpy as np
import copy
N, A, B, C = map(int,input().split())
t = sorted([A,B,C])
A = t[0]
B = t[1]
C = t[2]
l = []
for i in range(N):
    l.append(int(input()))
dic = {}

def calc(ls):
    dic[tuple(ls)] = 0
    #print([np.abs(i-A) for i in ls])
    Asublist = [np.abs(i-A) for i in ls]
    #Asublist = list(sorted([np.abs(i-A) for i in ls]))[0:2]
    Bsublist = list(sorted([np.abs(i-B) for i in ls]))[0:2]
    Csublist = list(sorted([np.abs(i-C) for i in ls]))[0:2]
    res = min(Asublist)
    idx = Asublist.index(res)
    ls.remove(ls[idx])

    Bsublist = [np.abs(i-B) for i in ls]
    tmp = min(Bsublist)
    res += tmp
    idx = Bsublist.index(tmp)
    ls.remove(ls[idx])

    Csublist = [np.abs(i-C) for i in ls]
    tmp = min(Csublist)
    res += tmp
    idx = Csublist.index(tmp)
    ls.remove(ls[idx])

    #dic[tuple(ls)] = res
    return res + 10*(N - (len(ls)+3))

def rec(ls, num, maxcomb, minval):
    if num == 1:
        minval = calc(copy.deepcopy(ls))
    count = 0
    for i in range(0, len(ls)-1):
        for j in range(i+1, len(ls)):
            tmp = [ls[i]+ls[j]]
            tmp = tmp + [ls[k] for k in range(len(ls)) if k!=i and k!=j]
            #print(tmp)
            if tuple(tmp) in dic.keys():
                continue
            tmp2 = calc(copy.deepcopy(tmp))
            #print([minval, tmp2])
            if minval > tmp2:
                minval = tmp2
            if num < maxcomb:
                #print([i,j,num])
                #print(tmp)
                tmp3 = rec(copy.deepcopy(tmp), num+1, maxcomb, minval)
                if minval > tmp3:
                    minval = tmp3
    return minval
minval = rec(l, 1, N-3, np.inf)
print(minval)

Submission status: Runtime Error

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

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

print(magic(ret0, ret1, ret2, ret3)

Submission status: Runtime Error

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 + [cur]) + 10
    return min(ret0, ret1, ret2, ret3)


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

Submission status: Runtime Error

Solution code:
from itertools import product


N, A, B, C = map(int, input().split())
l = []
for i in range(N):
    l.append(int(input()))
ans = min(sum(
    abs(sum(x for k, x in zip(p, 1) if k == r)-y) + 10 *(p.count(r) - 1)
    for r, y in enumerate((A, B, C), 1)
    )
    for p in product(range(4), repeat=N)
    if {1, 2, 3} <= set(p)
)
print(ans)

Submission status: Runtime Error

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(rest0, ret1, ret2, ret3)

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

Submission status: Runtime Error

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(rest0, ret1, ret2, ret3)

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

Submission status: Runtime Error

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

def func(dst, src, mp):
    if len(dst) == 0: # dst が空になったら終了
        return mp
    for s in src: # 長さが一致していたら何もせず除外
        if s in dst:
            src.remove(s)
            dst.remove(s)
#            print("removed", s, src, dst, mp)
    for i in dst: # 持っているどの竹よりも短い dst には、短縮魔法を使うしかない
        if i < min(src):
            tmp = min(src)
            mp += min(src) - i
            src.remove(tmp)
            dst.remove(i)
#            print("shorten", tmp, src, dst, mp)
    # 持っている竹のうち、mp 消費が10未満で処理できるものがあれば、その中で最小のものを延長
    mi = 10
    dd = None
    ss = None
    for s in src:
        for d in dst:
            tmp = mi
            mi = min(mi, abs(d-s))
            if mi != tmp:
                dd = d
                ss = s
    if mi < 10:
        mp += dd - ss
        src.remove(ss)
        dst.remove(dd)
#        print("extend", ss, src, dst, mp)
    # 結合した時に、目標に近くなる 2 本を結合
    mi = 1000
    for s in src:
        for d in dst:
            mi = min(mi, abs(s-d))
    s1 = s2 = None
    for i in range(len(src)):
        for j in range(i+1,len(src)):
            for d in dst:
                tmp = mi
                mi = min(mi, abs(d - (src[i] + src[j])))
                if mi != tmp:
                    s1 = src[i]
                    s2 = src[j]
    if s1 != None and s2 != None:
        src.remove(s1)
        src.remove(s2)
        mp += 10
#        print("synthesize", s1, s2, src, dst,mp)
        src.append(s1+s2)
    else:
        # 結合しても意味が無い場合、最も差が小さいものを変更
        mi = 1000
        ss = None
        dd = None
        for s in src:
            for d in dst:
                tmp = mi
                mi = min(mi, abs(s-d))
                if tmp != mi:
                    ss = s
                    dd = d
        if ss != None and dd != None:
            mp += abs(s-d)
            src.remove(s)
            dst.remove(d)
    return func(dst, src, mp)

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

if __name__ == '__main__':
    main()