input stringlengths 20 127k | target stringlengths 20 119k | problem_id stringlengths 6 6 |
|---|---|---|
import itertools
n,*abc=list(map(int,input().split()))
l=[int(eval(input())) for i in range(n)]
inf=100000000000000
mi=inf
for i in list(itertools.product([i for i in range(3)],repeat=n)):#(1)↓
sub=[[] for j in range(3)]
for j in range(n):
sub[i[j]].append(j)
mi_sub=0
for j in range(3):
l_sub=len(sub[j])
if l_sub==0:#(2)
mi_sub=inf
break
mi_subsub=inf
for k in range(2**l_sub):#(3)↓
mi_subsubsub=[0,0]
for l_subsub in range(l_sub):#(5)↓
if ((k>>l_subsub) &1):
mi_subsubsub[0]+=1
mi_subsubsub[1]+=l[sub[j][l_subsub]]
if mi_subsubsub[0]!=0:#(4)
mi_subsub=min(mi_subsub,abs(mi_subsubsub[1]-abc[j])+mi_subsubsub[0]*10-10)
mi_sub+=mi_subsub
mi=min(mi,mi_sub)
print(mi) | N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for i in range(N)]
INF = 10 ** 9
def dfs(cur, a, b, c):
'''
深さがNだったら使用MPを評価
N本の竹を使うときに必要な合成回数はN-1のため,最後に-1*10*3(?)
1本も竹を使用していなかったら棄却→MPを無限に使うこととしている
'''
if cur == N:
return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
'''
cur番目の竹に対して
ret0:ABCどれにも使わない
ret1:Aに使う(合成:MP+10)
ret2:Bに使う(合成:MP+10)
ret3:Cに使う(合成:MP+10)
'''
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))) | p03111 |
N,A,B,C=list(map(int,input().split()))
l=[int(eval(input())) for i in range(N)]
INF=3000
def mp(co,a,b,c):
if co==N:
return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else INF
ret0=mp(co+1,a,b,c)
ret1=mp(co+1,a+l[co],b,c)+10
ret2=mp(co+1,a,b+l[co],c)+10
ret3=mp(co+1,a,b,c+l[co])+10
return min(ret0,ret1,ret2,ret3)
print((mp(0,0,0,0)))
| N,A,B,C=list(map(int,input().split()))
l=[int(eval(input())) for i in range(N)]
inf=3000
def mp(g,a,b,c,):
if g==N:
return abs(A-a)+abs(B-b)+abs(C-c)-30 if min(a,b,c)>0 else inf
l0=mp(g+1,a+l[g],b,c)+10
l1=mp(g+1,a,b+l[g],c)+10
l2=mp(g+1,a,b,c+l[g])+10
l3=mp(g+1,a,b,c)
return min(l0,l1,l2,l3)
print((mp(0,0,0,0)))
| p03111 |
import itertools
import math
import copy
N, A, B, C = list(map(int, input().rstrip().split()))
pattern = [list(itertools.combinations(list(range(x)), 3)) for x in range(3, N+1)]
def stretch_shortening(sticks, A, B, C):
min_cost = 1e10
for i1, i2, i3 in pattern[len(sticks) - 3]:
cost = abs(sticks[i1] - A) + abs(sticks[i2] - B) + abs(sticks[i3] - C)
if min_cost > cost:
min_cost = cost
return min_cost
def connect(sticks, A, B, C):
min_cost = stretch_shortening(sticks, A, B, C) + (N - len(sticks)) * 10
if len(sticks) <= 3:
return min_cost
for i1, i2 in itertools.combinations(list(range(len(sticks))), 2):
if sticks[i1] + sticks[i2] > sticks[-1] and sticks[-1] >= C:
continue
if sticks[i1] <= 10 or sticks[i2] <= 10:
continue
'''
copied_sticks = copy.copy(sticks)
copied_sticks[i1] += copied_sticks[i2]
copied_sticks.pop(i2)
copied_sticks = sorted(copied_sticks)
'''
i1_value = sticks[i1]
i2_value = sticks[i2]
total_i1_i2 = i1_value + i2_value
flag = False
for i, v in enumerate(sticks):
if total_i1_i2 < v:
insert_index = i
sticks.insert(i, total_i1_i2)
sticks.pop(i2)
sticks.pop(i1)
flag = True
#print(0, sticks)
break
if flag == False:
insert_index = -1
sticks.insert(len(sticks), total_i1_i2)
sticks.pop(i2)
sticks.pop(i1)
#print(1, sticks[-1], sticks)
min_cost_connect = connect(sticks, A, B, C)
sticks.insert(i1, i1_value)
sticks.insert(i2, i2_value)
sticks.pop(insert_index)
if min_cost_connect < min_cost:
min_cost = min_cost_connect
return min_cost
'''
def connect(sticks, A, B, C, min_cost):
new_min_cost = stretch_shortening(sticks, A, B, C) + (N - len(sticks)) * 10
if len(sticks) <= 3:
return min_cost
if len(sticks) <= 3:
if min_cost > new_min_cost:
return new_min_cost
else:
return min_cost
if min_cost < new_min_cost:
return min_cost
else:
min_cost = new_min_cost
min_copied_sticks = None
for i1, i2 in itertools.combinations(range(len(sticks)), 2):
if sticks[i1] + sticks[i2] > sticks[-1] and sticks[-1] > C:
continue
copied_sticks = copy.copy(sticks)
copied_sticks[i1] += copied_sticks[i2]
copied_sticks.pop(i2)
copied_sticks = sorted(copied_sticks)
#min_cost_connect = connect(copied_sticks, A, B, C, min_cost)
min_cost_connect = stretch_shortening(copied_sticks, A, B, C) + (N - len(copied_sticks)) * 10
if min_cost_connect < min_cost:
min_cost = min_cost_connect
min_copied_sticks = copied_sticks
if min_copied_sticks is not None:
min_cost_connect = connect(min_copied_sticks, A, B, C, min_cost)
if min_cost_connect < min_cost:
min_cost = min_cost_connect
return min_cost
'''
sticks = []
for _ in range(N):
sticks.append(int(eval(input())))
sticks = sorted(sticks)
min_cost = connect(sticks, C, B, A) #, 1e10)
#min_cost = stretch_shortening(sticks, A, B, C)
#print(pattern)
print(min_cost)
|
import itertools
import math
import copy
N, A, B, C = list(map(int, input().rstrip().split()))
pattern = [list(itertools.combinations(list(range(x)), 3)) for x in range(3, N+1)]
def stretch_shortening(sticks, A, B, C):
min_cost = 1e10
for i1, i2, i3 in pattern[len(sticks) - 3]:
cost = abs(sticks[i1] - A) + abs(sticks[i2] - B) + abs(sticks[i3] - C)
if min_cost > cost:
min_cost = cost
return min_cost
temp_set = set()
def connect(sticks, A, B, C):
min_cost = stretch_shortening(sticks, A, B, C) + (N - len(sticks)) * 10
if len(sticks) <= 3:
return min_cost
for i1, i2 in itertools.combinations(list(range(len(sticks))), 2):
if sticks[i1] + sticks[i2] > sticks[-1] and sticks[-1] >= C:
continue
if sticks[i1] <= 10 or sticks[i2] <= 10:
continue
copied_sticks = copy.copy(sticks)
copied_sticks[i1] += copied_sticks[i2]
copied_sticks.pop(i2)
copied_sticks = sorted(copied_sticks)
len_set = len(temp_set)
temp_set.add(''.join(["{:05d}".format(x) for x in copied_sticks]))
if len_set == len(temp_set):
continue
min_cost_connect = connect(copied_sticks, A, B, C)
if min_cost_connect < min_cost:
min_cost = min_cost_connect
return min_cost
sticks = []
for _ in range(N):
sticks.append(int(eval(input())))
sticks = sorted(sticks)
min_cost = connect(sticks, C, B, A) #, 1e10)
#min_cost = stretch_shortening(sticks, A, B, C)
#print(pattern)
print(min_cost)
| p03111 |
N,A,B,C=list(map(int,input().split()))
l=[int(eval(input())) for _ in range(N)]
ABC=[A,B,C]
def base2n(Num,n):
if Num<n:
return str(Num)
else:
return base2n(Num//n,n)+str(Num%n)
ans=10**6
for i in range(4**N):
i_4=("0"*N+base2n(i,4))[-N:]
Group=[[] for i in range(4)]
for j in range(N):
Group[int(i_4[j])-1].append(l[j])
if len(Group[0])*len(Group[1])*len(Group[2])!=0:
ans=min(ans,(N-3-len(Group[3]))*10+sum([abs(ABC[j] - sum(Group[j])) for j in range(3)]))
print(ans) | N,A,B,C=list(map(int,input().split()))
l=[int(eval(input())) for _ in range(N)]
inf=10**6
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
else:
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))) | p03111 |
n, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
ans = 3000
for bits in range(4**n):
xa = xb = xc = 0
tmp_ans = 0
for i in range(n):
f = (bits//(4**i)) % 4
if f == 1:
if xa != 0:
tmp_ans += 10
xa += l[i]
elif f == 2:
if xb != 0:
tmp_ans += 10
xb += l[i]
elif f == 3:
if xc != 0:
tmp_ans += 10
xc += l[i]
if xa == 0 or xb == 0 or xc == 0:
continue
tmp_ans += abs(a - xa) + abs(b - xb) + abs(c - xc)
ans = min(ans, tmp_ans)
print(ans)
| n, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
INF = 10**9
def dfs(index, x, y, z):
if index == n:
return abs(a-x) + abs(b-y) + abs(c-z) - 30 if min(x, y, z) > 0 else INF
cand1 = dfs(index+1, x+l[index], y, z) + 10
cand2 = dfs(index+1, x, y+l[index], z) + 10
cand3 = dfs(index+1, x, y, z+l[index]) + 10
cand4 = dfs(index+1, x, y, z)
return min(cand1, cand2, cand3, cand4)
print((dfs(0, 0, 0, 0)))
| p03111 |
#!/usr/bin/env python
# coding: utf-8
import itertools
def main():
N, A, B, C = list(map(int, input().split()))
ll = []
for _ in range(N):
ll.append(int(eval(input())))
cand = []
ids = set(range(N))
for a in range(1, N-1):
for b in range(1, N-a):
for c in range(1, N-a-b+1):
cand.append((a, b, c))
best_cost = -1
for a, b, c in cand:
la = itertools.combinations(ids, a)
for a_ in la:
lb = itertools.combinations(ids-set(a_), b)
for b_ in lb:
lc = itertools.combinations(ids-set(a_)-set(b_), c)
for c_ in lc:
cost = (len(a_)-1)*10 + abs(sum(ll[e] for e in a_)-A)
cost += (len(b_)-1)*10 + abs(sum(ll[e] for e in b_)-B)
cost += (len(c_)-1)*10 + abs(sum(ll[e] for e in c_)-C)
if best_cost == -1 or cost < best_cost:
best_cost = cost
print(best_cost)
if __name__ == '__main__':
main()
| #!/usr/bin/env python
# coding: utf-8
N, A, B, C = list(map(int, input().split()))
ll = []
for _ in range(N):
ll.append(int(eval(input())))
def dfs(n, a, b, c):
if n == N:
if a == 0 or b == 0 or c == 0: return float('inf')
return abs(A-a) + abs(B-b) + abs(C-c)
ca = dfs(n+1, a+ll[n], b, c)
if a > 0: ca += 10
cb = dfs(n+1, a, b+ll[n], c)
if b > 0: cb += 10
cc = dfs(n+1, a, b, c+ll[n])
if c > 0: cc += 10
cn = dfs(n+1, a, b, c)
return min(ca, cb, cc, cn)
def main():
print((dfs(0, 0, 0, 0)))
if __name__ == '__main__':
main()
| p03111 |
# XをN進数に変換
def base_number(X:int, N:int):
res = ""
while True:
q, mod = divmod(X,N)
res += str(mod)
X = q
if q == 0:
break
return res[::-1]
N,*B = list(map(int,input().split()))
L = [int(eval(input())) for _ in range(N)]
ans = float("inf")
for i in range(4 ** N):
state = list(base_number(i,4).rjust(N, "0"))
if not set(["0","1","2"]) <= set(state):
continue
make = [[] for _ in range(3)]
for j,x in enumerate(state):
if x == "3":
continue
make[int(x)].append(L[j])
MP = 0
for j,m in enumerate(make):
if len(m) == 1:
MP += abs(B[j] - m[0]) # 延長or短縮魔法
else:
MP += (len(m) - 1) * 10 # 合成魔法
MP += abs(B[j] - sum(m))
ans = min(ans, MP)
print(ans) | import sys
sys.setrecursionlimit(10 ** 7)
from functools import lru_cache
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for i in range(N)]
INF = 10 ** 9
@lru_cache(None)
def dfs(cur, a, b, c):
if cur == N:
return abs(a - A) + abs(b - B) + abs(c - C) if min(a, b, c) > 0 else INF
# (10 if _ else 0): 最初の1本目は合成魔法の必要がない
ret0 = dfs(cur + 1, a, b, c)
ret1 = dfs(cur + 1, a + l[cur], b, c) + (10 if a else 0)
ret2 = dfs(cur + 1, a, b + l[cur], c) + (10 if b else 0)
ret3 = dfs(cur + 1, a, b, c + l[cur]) + (10 if c else 0)
return min(ret0, ret1, ret2, ret3)
print((dfs(0, 0, 0, 0))) | p03111 |
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
l = list(sorted(l))
tar = []
for x in C, B, A:
if x in l:
l.remove(x)
else:
tar.append(x)
if not tar:
print((0))
exit()
ans = float('inf')
for j in range((len(tar) + 1) ** len(l)):
vvv = [[] for _ in range(len(tar) + 1)]
for i in sorted(list(range(len(l))), reverse=True):
idx, j = divmod(j, (len(tar) + 1) ** i)
vvv[idx].append(l[i])
if any(len(vvvv) == 0 for vvvv in vvv[:-1]):
continue
else:
bbb = 0
# print(tar, vvv)
for k in range(len(tar)):
bbb += 10 * (len(vvv[k]) - 1)
bbb += abs(sum(vvv[k]) - tar[k])
ans = min(ans, bbb)
print(ans)
| N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
INF = float('inf')
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
# A,B,Cに使用する材料を合成し、目的の長さの差を加算(必要なMP)
# ret1-3で一つ目の材料にも合成魔法のMP10を要求しているので、A,B,Cの初回加算合わせて3回分のMP30を減算
# a,b,cのいずれかが0のとき、材料が1つも宛がわれていない竹が存在するため、不適(必要MPを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)
# l[cur]について、使用しないか、(A,B,C)のいずれかに使用の4分岐
print((dfs(0, 0, 0, 0)))
| p03111 |
# 4進数
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
INF = float('inf')
ans = INF
for ptn in range(1 << (2 * N)):
abc = [0 for _ in range(4)]
v = [None for _ in range(N)]
# lの各idxをどの竹用に割り当てるか, ABC無 = 0123
cnt = 0
for j in range(N):
v[j] = (ptn >> (2 * j)) & 3
abc[v[j]] += l[j]
if 0 <= v[j] < 3:
cnt += 1
if 0 in abc[:3]: continue
ans = min(ans, sum(abs(x - t) for x, t in zip(abc[:3], [A, B, C])) + (cnt - 3) * 10)
print(ans)
| # dfs
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
INF = float('inf')
def calc(mask):
abc = [0 for _ in range(4)]
cnt = 0
for j in range(N):
idx = (mask >> (2 * j)) & 3
abc[idx] += l[j]
if 0 <= idx < 3:
cnt += 1
if 0 in abc[:3]:
return INF
return sum(abs(x - t) for x, t in zip(abc, [A, B, C])) + (cnt - 3) * 10
def dfs(cur, mask):
if cur == N:
return calc(mask)
res = INF
for i in range(4):
res = min(res, dfs(cur + 1, mask | i << (2 * cur)))
return res
print((dfs(0, 0)))
| p03111 |
inf = float('inf')
n, *goal = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
ans = inf
for q in range(4 ** n):
m = [[] for _ in range(4)]
lm = 0
for j in range(n):
idx = (q // (4 ** j)) % 4
m[idx].append(l[j])
if any(len(m[idx]) == 0 for idx in range(3)):
continue
cost = 0
for idx in range(3):
merge_time = len(m[idx]) - 1
cost += merge_time * 10
cost += abs(sum(m[idx]) - goal[idx])
ans = min(ans, cost)
print(ans)
| inf = float('inf')
n, *goal = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
ans = inf
for q in range(4 ** n):
cost = -30
t = [0] * 3
for j in range(n):
idx = (q // (4 ** j)) % 4
if idx == 3: continue
t[idx] += l[j]
cost += 10
if any(tt == 0 for tt in t):
continue
for idx in range(3):
cost += abs(t[idx] - goal[idx])
ans = min(ans, cost)
print(ans)
| p03111 |
inf = float('inf')
n, *goal = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
ans = inf
for q in range(4 ** n):
cost = -30
t = [0] * 3
for j in range(n):
idx = (q // (4 ** j)) % 4
if idx == 3: continue
t[idx] += l[j]
cost += 10
if any(tt == 0 for tt in t):
continue
for idx in range(3):
cost += abs(t[idx] - goal[idx])
ans = min(ans, cost)
print(ans)
| inf = float('inf')
def calc(mask):
cost = -30
t = [0] * 3
for j in range(n):
idx = (mask >> (2 * j)) & 3
if idx == 3: continue
t[idx] += l[j]
cost += 10
if 0 in t[:3]:
return inf
cost += sum(abs(tt - gg) for tt, gg in zip(t, goal))
return cost
def dfs(cur, mask):
# l[cur]をa,b,c,未使用のいずれかにあてがう
# base case
if cur == n:
return calc(mask)
res = inf
for i in range(4):
# i == 0,1,2 : a,b,cにあてがう / i == 3 : 未使用
res = min(res, dfs(cur + 1, mask | i << (2 * cur)))
return res
n, *goal = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
print((dfs(0, 0)))
| p03111 |
inf = float('inf')
def calc(mask):
cost = -30
t = [0] * 3
for j in range(n):
idx = (mask >> (2 * j)) & 3
if idx == 3: continue
t[idx] += l[j]
cost += 10
if 0 in t[:3]:
return inf
cost += sum(abs(tt - gg) for tt, gg in zip(t, goal))
return cost
def dfs(cur, mask):
# l[cur]をa,b,c,未使用のいずれかにあてがう
# base case
if cur == n:
return calc(mask)
res = inf
for i in range(4):
# i == 0,1,2 : a,b,cにあてがう / i == 3 : 未使用
res = min(res, dfs(cur + 1, mask | i << (2 * cur)))
return res
n, *goal = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
print((dfs(0, 0)))
| inf = float('inf')
def calc(mask):
t = [0] * 3
cnt = 0
for j in range(n):
idx = (mask >> (2 * j)) & 3
if idx == 3:
cnt += 1
continue
t[idx] += l[j]
if 0 in t:
return inf
return sum(abs(tt - gg) for tt, gg in zip(t, goal)) + (n - cnt - 3) * 10
def dfs(cur, mask):
# l[cur]をa,b,c,未使用のいずれかにあてがう
# base case
if cur == n:
return calc(mask)
res = inf
for i in range(4):
# i == 0,1,2 : a,b,cにあてがう / i == 3 : 未使用
res = min(res, dfs(cur + 1, mask | i << (2 * cur)))
return res
n, *goal = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
print((dfs(0, 0)))
| p03111 |
l=[]
n,a,b,c=list(map(int,input().split()))
for i in range(n):
li=int(eval(input()))
l.append(li)
d=[a,b,c]
d.sort()
mpmin=sum(l)*4+len(l)*10+sum(d)
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)
|
n,a,b,c=list(map(int,input().split()))
l=[0]*n
for i in range(n):
l[i]=int(eval(input()))
import itertools
mpmin=a+b+c+1
for i in itertools.product([0,1,2,3], repeat=n):
aa=0
bb=0
cc=0
mpa=0
mpb=0
mpc=0
ia=0
ib=0
ic=0
for ii in range(n):
if i[ii]==1:
ia=1
if aa>0:
mpa+=10
aa+=l[ii]
elif i[ii]==2:
ib=1
if bb>0:
mpb+=10
bb+=l[ii]
elif i[ii]==3:
ic=1
if cc>0:
mpc+=10
cc+=l[ii]
if ia==1 and ib==1 and ic==1:
mp=mpa+abs(a-aa)+mpb+abs(b-bb)+mpc+abs(c-cc)
# if mp<mpmin:
# print(mp,mpa,mpb,mpc,i)
mpmin=min(mp,mpmin)
print(mpmin)
| p03111 |
n,a,b,c=list(map(int,input().split()))
l=[0]*n
for i in range(n):
l[i]=int(eval(input()))
import itertools
mpmin=a+b+c+1
for i in itertools.product([0,1,2,3], repeat=n):
aa=0
bb=0
cc=0
mpa=0
mpb=0
mpc=0
ia=0
ib=0
ic=0
for ii in range(n):
if i[ii]==1:
ia=1
if aa>0:
mpa+=10
aa+=l[ii]
elif i[ii]==2:
ib=1
if bb>0:
mpb+=10
bb+=l[ii]
elif i[ii]==3:
ic=1
if cc>0:
mpc+=10
cc+=l[ii]
if ia==1 and ib==1 and ic==1:
mp=mpa+abs(a-aa)+mpb+abs(b-bb)+mpc+abs(c-cc)
# if mp<mpmin:
# print(mp,mpa,mpb,mpc,i)
mpmin=min(mp,mpmin)
print(mpmin)
|
n,a,b,c=list(map(int,input().split()))
l=[0]*n
for i in range(n):
l[i]=int(eval(input()))
inf=10**9
def dfs(cur,aa,bb,cc):
if cur==n:
if min(aa,bb,cc)>0:
return abs(a-aa)+abs(b-bb)+abs(c-cc)-30
else:
return inf
ret0=dfs(cur+1,aa,bb,cc)
ret1=dfs(cur+1,aa+l[cur],bb,cc)+10
ret2=dfs(cur+1,aa,bb+l[cur],cc)+10
ret3=dfs(cur+1,aa,bb,cc+l[cur])+10
# print(cur,l[cur],aa,bb,cc,ret0,ret1,ret2,ret3)
return min(ret0,ret1,ret2,ret3)
print((dfs(0,0,0,0)))
| p03111 |
# coding: utf-8
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 = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
ans = float("inf")
for i in range(4**N):
bits = Base_10_to_n(i, 4).zfill(N)
a, b, c = 0, 0, 0
cost = 0
for j in range(len(bits)):
if bits[j] == "0":
pass
elif bits[j] == "1":
a += L[j]
cost += 10
elif bits[j] == "2":
b += L[j]
cost += 10
else:
c += L[j]
cost += 10
if min(a, b, c) > 0:
ans = min(ans, cost + abs(a-A)+abs(b-B)+abs(c-C) - 10*3)
print(ans) | # coding: utf-8
import sys
sys.setrecursionlimit(10**9)
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 = list(map(int, input().split()))
L = []
for i in range(N):
L.append(int(eval(input())))
# L.sort()
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 10**9
r0 = dfs(cnt+1, a, b, c)
r1 = dfs(cnt+1, a+L[cnt], b, c) + 10
r2 = dfs(cnt+1, a, b+L[cnt], c) + 10
r3 = dfs(cnt+1, a, b, c+L[cnt]) + 10
return min(r0, r1, r2, r3)
print((dfs(0,0,0,0))) | p03111 |
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/2/24
Solved on 2019/2/24
@author: shinjisu
"""
# ABC 119
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n
def getIntLines(n): return [int(input()) for i in range(n)]
def getIntMat(n):
mat = []
for i in range(n):
mat.append(getIntList())
return mat
def zeros2(n, m): return [zeros(m)]*n
ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
def dmp(x, cmt=''):
global debug
if debug:
if cmt != '':
print(cmt, ': ', end='')
print(x)
return x
def gen2Bits(n): # n個の0/1のリストを生成 [0]から変化
bits = zeros(n)
for i in range(4**n):
for k in range(n):
bits[k] = i % 4
i //= 4
yield bits
def probC():
N, A, B, C = getIntList()
L = []
for i in range(N):
L.append(getInt())
dmp((N, A, B, C ))
dmp(L)
d = zeros(3)
d[0] = A
d[1] = B
d[2] = C
dmp(d)
minMP = 99999999999999999999
for f in gen2Bits(N):
dmp(f)
mp = 0
lg = zeros(4)
for i in range(N):
if lg[f[i]] > 0 and f[i] < 3:
mp += 10
lg[f[i]] += L[i]
dmp((i,lg,mp))
#dmp(lg)
allUsed = True
for i in range(3): # 4番目は使用しない竹
if lg[i] == 0:
allUsed = False
break
mp += abs(lg[i]-d[i])
if not allUsed:
continue
dmp((minMP, mp))
minMP = min(minMP, mp)
return minMP
debug = False # True False
print(probC())
def gcd(x, y): # 最大公約数
m = max(x, y)
n = min(x, y)
while m % n != 0:
w = m % n
m = n
n = w
return n
def probD():
N, M = getIntList()
A = getIntList()
dmp((N, M))
dmp(A)
return num
def probA():
S = input()
dmp(S)
y = int(S[0:4])
m = int(S[5:7])
d = int(S[8:10])
dmp((y,m,d))
HEI = 'Heisei'
if y <= 2018:
return HEI
if m <= 4:
return HEI
else:
return 'TBD'
def probB():
N = getInt()
dmp(N)
sm = 0.0
for i in range(N):
x, u = [a for a in input().split()]
dmp((x,u))
if u == 'JPY':
x = int(x)
else:
x = float(x) * 380000.0
sm += x
return sm
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 2019/2/24
Solved on 2019/2/24
@author: shinjisu
"""
# ABC 119
def getInt(): return int(input())
def getIntList(): return [int(x) for x in input().split()]
def zeros(n): return [0]*n
def getIntLines(n): return [int(input()) for i in range(n)]
def getIntMat(n):
mat = []
for i in range(n):
mat.append(getIntList())
return mat
def zeros2(n, m): return [zeros(m)]*n
ALPHABET = [chr(i+ord('a')) for i in range(26)]
DIGIT = [chr(i+ord('0')) for i in range(10)]
N1097 = 10**9 + 7
INF = 10**10
def dmp(x, cmt=''):
global debug
if debug:
if cmt != '':
print(cmt, ': ', end='')
print(x)
return x
def gen2Bits(n): # n個の0/1のリストを生成 [0]から変化
bits = zeros(n)
for i in range(4**n):
for k in range(n):
bits[k] = i % 4
i //= 4
yield bits
# 多重ループを再帰で実現
def loop(level, a, b, c, mp):
global N, L, A, B, C, minMP
if level == N:
if min(a, b, c) == 0:
return INF
mp += abs(A-a) + abs(B-b) + abs(C-c) - 30 # 一本目にはMP不要
dmp((minMP, mp), 'min, MP')
minMP = min(minMP,mp)
return minMP
else:
mp0 = loop(level+1, a, b, c, mp)
mp1 = loop(level+1, a+L[level], b, c, mp+10)
mp2 = loop(level+1, a, b+L[level], c, mp+10)
mp3 = loop(level+1, a, b, c+L[level], mp+10)
dmp((mp0, mp1, mp2, mp3))
return min(mp0, mp1, mp2, mp3)
def probC():
global N, L, A, B, C, minMP
N, A, B, C = getIntList()
L = []
for i in range(N):
L.append(getInt())
dmp((N, A, B, C ))
dmp(L)
d = zeros(3)
d = A, B, C
dmp(d)
minMP = INF
return loop(0, 0, 0, 0, 0)
def probC_v2():
N, A, B, C = getIntList()
L = []
for i in range(N):
L.append(getInt())
dmp((N, A, B, C ))
dmp(L)
d = zeros(3)
d = A, B, C
dmp(d)
minMP = INF
for f in gen2Bits(N):
dmp(f)
mp = 0
lg = zeros(4)
for i in range(N):
if lg[f[i]] > 0 and f[i] < 3:
mp += 10
lg[f[i]] += L[i]
#dmp((i,lg,mp))
#dmp(lg)
if min(lg[:3]) == 0: # 4番目は使用しない竹
continue
for i in range(3):
mp += abs(lg[i]-d[i])
dmp((minMP, mp), 'min, MP')
minMP = min(minMP, mp)
return minMP
def probC_v1():
N, A, B, C = getIntList()
L = []
for i in range(N):
L.append(getInt())
dmp((N, A, B, C ))
dmp(L)
d = zeros(3)
d[0] = A
d[1] = B
d[2] = C
dmp(d)
minMP = INF
for f in gen2Bits(N):
dmp(f)
mp = 0
lg = zeros(4)
for i in range(N):
if lg[f[i]] > 0 and f[i] < 3:
mp += 10
lg[f[i]] += L[i]
#dmp((i,lg,mp))
#dmp(lg)
allUsed = True
for i in range(3): # 4番目は使用しない竹
if lg[i] == 0:
allUsed = False
break
mp += abs(lg[i]-d[i])
if not allUsed:
continue
dmp((minMP, mp))
minMP = min(minMP, mp)
return minMP
debug = False # True False
print(probC())
def gcd(x, y): # 最大公約数
m = max(x, y)
n = min(x, y)
while m % n != 0:
w = m % n
m = n
n = w
return n
def probD():
N, M = getIntList()
A = getIntList()
dmp((N, M))
dmp(A)
return num
def probA():
S = input()
dmp(S)
y = int(S[0:4])
m = int(S[5:7])
d = int(S[8:10])
dmp((y,m,d))
HEI = 'Heisei'
if y <= 2018:
return HEI
if m <= 4:
return HEI
else:
return 'TBD'
def probB():
N = getInt()
dmp(N)
sm = 0.0
for i in range(N):
x, u = [a for a in input().split()]
dmp((x,u))
if u == 'JPY':
x = int(x)
else:
x = float(x) * 380000.0
sm += x
return sm
| p03111 |
# coding: utf-8
import sys
import itertools
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
# N本の竹それぞれA, B, C, 使わないか
N, A, B, C = lr()
bamboo = [ir() for _ in range(N)]
answer = 10 ** 5
for pattern in itertools.product(list(range(4)), repeat=N):
mp = 0
Alist = []; Blist = []; Clist = []
for i, p in enumerate(pattern):
if p == 0:
Alist.append(bamboo[i])
elif p == 1:
Blist.append(bamboo[i])
elif p == 2:
Clist.append(bamboo[i])
if len(Alist) == 0 or len(Blist) == 0 or len(Clist) == 0:
continue
mp += 10 * (len(Alist) + len(Blist) + len(Clist) - 3)
mp += abs(A - sum(Alist)) + abs(B - sum(Blist)) + abs(C - sum(Clist))
if mp < answer:
answer = mp
print(answer)
# 04 | # coding: utf-8
import sys
import itertools
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
# Nは最大8まで、Aに使う竹はどれ?
N, A, B, C = lr()
bamboo = [ir() for _ in range(N)]
answer = 10 ** 5
for pattern in itertools.product(list(range(4)), repeat=N):
use = [0] * 3
mp = 0
for p, bam in zip(pattern, bamboo):
if p == 3:
continue
if use[p] != 0:
mp += 10
use[p] += bam
if use.count(0) != 0:
continue
mp += abs(A-use[0]) + abs(B-use[1]) + abs(C-use[2])
if mp < answer:
answer = mp
print(answer)
# 23 | p03111 |
from itertools import product, permutations
N, A, B, C = list(map(int, input().split(' ')))
L = [int(eval(input())) for _ in range(N)]
ans = 10 ** 9
for groups in product(list(range(4)), repeat=N):
items = []
group_a = []
group_b = []
group_c = []
for group, length in zip(groups, L):
if group == 0:
items.append((length, 0))
elif group == 1:
group_a.append(length)
elif group == 2:
group_b.append(length)
elif group == 3:
group_c.append(length)
if group_a:
items.append((sum(group_a), (len(group_a) - 1) * 10))
if group_b:
items.append((sum(group_b), (len(group_b) - 1) * 10))
if group_c:
items.append((sum(group_c), (len(group_c) - 1) * 10))
if len(items) < 3:
continue
for one, two, three in permutations((A, B, C), r=3):
cost = 0
_items = list(items)
_items = sorted(_items, key=lambda x: (abs(x[0] - one), x[1]))
cost += abs(_items[0][0] - one) + _items[0][1]
_items.pop(0)
_items = sorted(_items, key=lambda x: (abs(x[0] - two), x[1]))
cost += abs(_items[0][0] - two) + _items[0][1]
_items.pop(0)
_items = sorted(_items, key=lambda x: (abs(x[0] - three), x[1]))
cost += abs(_items[0][0] - three) + _items[0][1]
ans = min(ans, cost)
print(ans)
| from itertools import product
N, A, B, C = list(map(int, input().split(' ')))
L = [int(eval(input())) for _ in range(N)]
ans = 10 ** 9
for groups in product(list(range(4)), repeat=N):
length_a = 0
length_b = 0
length_c = 0
cost_a = -10
cost_b = -10
cost_c = -10
for group, length in zip(groups, L):
if group == 1:
length_a += length
cost_a += 10
elif group == 2:
length_b += length
cost_b += 10
elif group == 3:
length_c += length
cost_c += 10
if 0 in {length_a, length_b, length_c}:
continue
ans = min(ans, sum([
abs(A - length_a),
abs(B - length_b),
abs(C - length_c),
cost_a,
cost_b,
cost_c,
]))
print(ans)
| p03111 |
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
min_cost = 10 ** 18
def func(n, select):
global min_cost
if n == 0:
if len(set(select) - {0}) < 3:
return
#print(select)
cost = 0
a, b, c = 0, 0, 0
for i, t in enumerate(select):
if t == 1:
if a != 0:
cost += 10
a += L[i]
if t == 2:
if b != 0:
cost += 10
b += L[i]
if t == 3:
if c != 0:
cost += 10
c += L[i]
cost += abs(A - a)
cost += abs(B - b)
cost += abs(C - c)
min_cost = min(min_cost, cost)
return
for i in range(4):
func(n - 1, select + [i])
func(N, [])
print(min_cost)
| N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
INF = 10 ** 12
def solve(n, cost, a, b, c):
if n == N:
if 0 == min(a, b, c):
return INF
return abs(a - A) + abs(b - B) + abs(c - C) + cost - 30
ret1 = solve(n + 1, cost, a, b, c)
ret2 = solve(n + 1, cost + 10, a + L[n], b, c)
ret3 = solve(n + 1, cost + 10, a, b + L[n], c)
ret4 = solve(n + 1, cost + 10, a, b, c + L[n])
return min(ret1, ret2, ret3, ret4)
print((solve(0, 0, 0, 0, 0)))
| p03111 |
n,a,b,c = list(map(int,input().split()))
takelist = [a,b,c]
li = [int(eval(input())) for _ in range(n)]
li2 = [0 for _ in range(n)]
min = 10**100
def movelist(li,n):
for i in range(n):
if li[i] != 3:
li[i] += 1
break
else:
li[i] = 0
for i in range(4**n):
li3 = [0,0,0,0]
sum = 0
for j in range(n):
if li3[li2[j]] != 0 and li2[j] != 3:
sum += 10
li3[li2[j]] += li[j]
if li3[0] != 0 and li3[1] != 0 and li3[2] != 0:
for k in range(3):
sum += abs(li3[k] - takelist[k])
if sum < min:
min = sum
movelist(li2,n)
print(min)
| n,A,B,C = list(map(int,input().split()))
li = [int(eval(input())) for _ in range(n)]
def searchans(count,a,b,c):
if count == n:
if not 0 in [a,b,c]:
return abs(A-a) + abs(B-b) + abs(C-c) - 30
else:
return 10**100
else:
ret0 = searchans(count+1,a,b,c)
ret1 = searchans(count+1,a+li[count],b,c) + 10
ret2 = searchans(count+1,a,b+li[count],c) + 10
ret3 = searchans(count+1,a,b,c+li[count]) + 10
return min([ret0,ret1,ret2,ret3])
print((searchans(0,0,0,0)))
| p03111 |
import itertools
n_bamboo, b1, b2, b3 = [int(i) for i in input().split()]
bamboos = [int(eval(input())) for _ in range(n_bamboo)]
ans = float("inf")
# すべての竹について、b1, b2, b3のどの竹に使うかを列挙
# 0のときはどの竹にも使わない
for bit in itertools.product((0, 1, 2, 3), repeat=n_bamboo):
l1, l2, l3 = 0, 0, 0
c1, c2, c3 = 0, 0, 0
# 選択した竹を足す(2つ目以降はMP10消費)
for b, bamboo in zip(bit, bamboos):
if b == 1:
if l1 > 0:
c1 += 10
l1 += bamboo
elif b == 2:
if l2 > 0:
c2 += 10
l2 += bamboo
elif b == 3:
if l3 > 0:
c3 += 10
l3 += bamboo
# 1つも構成する竹がない竹がある場合はスキップ
if l1 * l2 * l3 == 0:
continue
# 延長、短縮のコスト
c1 += abs(b1 - l1)
c2 += abs(b2 - l2)
c3 += abs(b3 - l3)
# 合計コストが今までより低ければ更新
ans = min(ans, c1 + c2 + c3)
print(ans)
| def dfs(cursor, a, b, c):
if cursor == N:
return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else float("inf")
ret0 = dfs(cursor + 1, a, b, c)
ret1 = dfs(cursor + 1, a + l[cursor], b, c) + 10
ret2 = dfs(cursor + 1, a, b + l[cursor], c) + 10
ret3 = dfs(cursor + 1, a, b, c + l[cursor]) + 10
return min(ret0, ret1, ret2, ret3)
N, A, B, C = [int(i) for i in input().split()]
l = [int(eval(input())) for _ in range(N)]
print((dfs(0, 0, 0, 0)))
| p03111 |
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**5)
def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
import copy
def choose3(bamboos: list, a: int, b: int, c: int):
bamboos.sort(reverse=True)
l = len(bamboos)
minmp = float('inf')
for idx in range(l):
for jdx in range(idx+1,l):
for kdx in range(jdx+1,l):
minmp = min(minmp,
abs(bamboos[idx]-a)\
+ abs(bamboos[jdx]-b)\
+ abs(bamboos[kdx]-c))
return minmp
def dfs(bamboos: list, a: int, b: int, c: int, mp: int):
l = len(bamboos)
if l == 3:
return mp + choose3(bamboos, a, b, c)
else:
minmp = mp + choose3(bamboos, a, b, c)
for p1 in range(l):
for p2 in range(p1+1,l):
newbamboos = copy.deepcopy(bamboos)
temp = bamboos[p1] + bamboos[p2]
newbamboos.remove(bamboos[p1])
newbamboos.remove(bamboos[p2])
newbamboos.append(temp)
minmp = min(minmp, dfs(newbamboos, a, b, c, mp+10))
return minmp
n,a,b,c = li()
laaa = [ni() for _ in range(n)]
print((dfs(laaa, a, b, c, 0))) | import sys
stdin = sys.stdin
sys.setrecursionlimit(10**5)
def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
def choose3(bamboos: list, a: int, b: int, c: int):
bamboos.sort(reverse=True)
l = len(bamboos)
minmp = float('inf')
for idx in range(l):
for jdx in range(idx+1,l):
for kdx in range(jdx+1,l):
minmp = min(minmp,
abs(bamboos[idx]-a)\
+ abs(bamboos[jdx]-b)\
+ abs(bamboos[kdx]-c))
return minmp
def dfs(bamboos: list, a: int, b: int, c: int, mp: int):
l = len(bamboos)
if l == 3:
return mp + choose3(bamboos, a, b, c)
else:
minmp = mp + choose3(bamboos, a, b, c)
for p1 in range(l):
for p2 in range(p1+1,l):
newbamboos = [bamboos[p1] + bamboos[p2]]
for p in range(l):
if p == p1 or p == p2:
continue
else:
newbamboos.append(bamboos[p])
minmp = min(minmp, dfs(newbamboos, a, b, c, mp+10))
del newbamboos
return minmp
n,a,b,c = li()
laaa = [ni() for _ in range(n)]
print((dfs(laaa, a, b, c, 0))) | p03111 |
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**5)
def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
def choose3(bamboos: list, a: int, b: int, c: int):
bamboos.sort(reverse=True)
l = len(bamboos)
minmp = float('inf')
for idx in range(l):
for jdx in range(idx+1,l):
for kdx in range(jdx+1,l):
minmp = min(minmp,
abs(bamboos[idx]-a)\
+ abs(bamboos[jdx]-b)\
+ abs(bamboos[kdx]-c))
return minmp
def dfs(bamboos: list, a: int, b: int, c: int, mp: int):
l = len(bamboos)
if l == 3:
return mp + choose3(bamboos, a, b, c)
else:
minmp = mp + choose3(bamboos, a, b, c)
for p1 in range(l):
for p2 in range(p1+1,l):
newbamboos = [bamboos[p1] + bamboos[p2]]
for p in range(l):
if p == p1 or p == p2:
continue
else:
newbamboos.append(bamboos[p])
minmp = min(minmp, dfs(newbamboos, a, b, c, mp+10))
del newbamboos
return minmp
n,a,b,c = li()
laaa = [ni() for _ in range(n)]
print((dfs(laaa, a, b, c, 0))) | import sys
stdin = sys.stdin
sys.setrecursionlimit(10**2)
def li(): return list(map(int, stdin.readline().split()))
def li_(): return [int(x)-1 for x in stdin.readline().split()]
def lf(): return list(map(float, stdin.readline().split()))
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())
n,a,b,c = li()
l = [ni() for _ in range(n)]
ans = 10**18
for i in range(4**n):
cur = i
cnt = {i: [] for i in range(4)}
for bi in l:
cnt[cur%4].append(bi)
cur //= 4
if len(cnt[1]) * len(cnt[2]) * len(cnt[3]) == 0:
continue
ans = min(ans, abs(a-sum(cnt[1]))\
+ abs(b-sum(cnt[2]))\
+ abs(c-sum(cnt[3]))\
+ 10*(n - len(cnt[0]) -3))
print(ans) | p03111 |
def examC():
N, A, B, C = LI()
L = [I() for _ in range(N)]
loop = 4**N; ans = 10**9
for i in range(loop):
cur = 0
curA = 0; curB = 0; curC = 0
for j in range(N):
judge = (i//(4**j))%4
if judge==1:
if curA>0:
cur +=10
curA += L[j]
elif judge==2:
if curB>0:
cur +=10
curB += L[j]
elif judge==3:
if curC>0:
cur +=10
curC += L[j]
if curA==0 or curB==0 or curC==0:
continue
cur += abs(curA-A)
cur += abs(curB-B)
cur += abs(curC-C)
ans = min(cur,ans)
print(ans)
import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')
if __name__ == '__main__':
examC()
| def examA():
N = DI()/dec(7)
ans = N
print(N)
return
def examB():
ans = 0
print(ans)
return
def examC():
N, A, B, C = LI()
L = [I()for _ in range(N)]
loop = (1<<(2*N))
ans = inf
for l in range(loop):
cost = 0
bamboo_A = 0
bamboo_B = 0
bamboo_C = 0
for j in range(N):
if l&(1<<(2*j))>0:
if l&(1<<(2*j+1))>0:
cost += 10
bamboo_A += L[j]
else:
cost += 10
bamboo_B += L[j]
else:
if l&(1<<(2*j+1))>0:
cost += 10
bamboo_C += L[j]
if bamboo_A==0 or bamboo_B==0 or bamboo_C==0:
continue
cost += abs(A-bamboo_A) + abs(B-bamboo_B) + abs(C-bamboo_C) - 30
ans = min(ans,cost)
print(ans)
return
def examD():
N = I()
P = LI()
O_ = [-1]*N
start = P[0]-1
for i in range(N):
O_[P[i]-1] = i
#print(O_,start)
A = [1]*N
B = [1]*N
for i in range(start+1,N):
cur = O_[i]-O_[i-1]
if cur>0:
A[i] = A[i-1] + (cur+1)
B[i] = B[i-1] - 1
else:
A[i] = A[i-1] + 1
B[i] = B[i-1] - (-cur+1)
for i in range(start)[::-1]:
cur = O_[i]-O_[i+1]
if cur>0:
B[i] = B[i+1] + (cur+1)
A[i] = A[i+1] - 1
else:
B[i] = B[i+1] + 1
A[i] = A[i+1] - (-cur+1)
if A[0]<1:
add = (1-A[0])
for i in range(N):
A[i] += add
if B[-1]<1:
add = (1-B[-1])
for i in range(N):
B[i] += add
print((" ".join(map(str,A))))
print((" ".join(map(str,B))))
return
def examE():
return
def examF():
ans = 0
print(ans)
return
from decimal import getcontext,Decimal as dec
import sys,bisect,itertools,heapq,math,random
from copy import deepcopy
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def I(): return int(eval(input()))
def LI(): return list(map(int,sys.stdin.readline().split()))
def DI(): return dec(eval(input()))
def LDI(): return list(map(dec,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,_ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = dec("0.000000000001")
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}
getcontext().prec = 28
sys.setrecursionlimit(10**7)
if __name__ == '__main__':
examC()
"""
9
7 3 9 1 8 4 5 6 2
""" | p03111 |
import itertools
N, A, B, C = list(map(int, input().split()))
l = []
for i in range(N):
l.append(int(eval(input())))
prod = list(itertools.product(['A','B','C','X'], repeat=N))
mps = []
for prod_e in prod:
if prod_e.count('A') * prod_e.count('B') * prod_e.count('C') != 0:
mp = 0
length = {'A':0, 'B':0, 'C':0, 'X':0}
for index, prod_e_e in enumerate(prod_e):
length[prod_e_e] += l[index]
mp += abs(length['A'] - A) + abs(length['B'] - B) + abs(length['C'] - C)
mp += (prod_e.count('A')-1) * 10 + (prod_e.count('B')-1) * 10 + (prod_e.count('C')-1) * 10
mps.append(mp)
print((min(mps))) | N, A, B, C = list(map(int, input().split()))
l = []
for i in range(N):
l.append(int(eval(input())))
def dfs(syn, n, a, b, c):
if n == N:
if a*b*c != 0:
mp = abs(A-a) + abs(B-b) + abs(C-c)
mp += syn * 10 - 30
return mp
else:
return 10**9
else:
branch1 = dfs(syn+1, n+1, a + l[n], b, c)
branch2 = dfs(syn+1, n+1, a, b + l[n], c)
branch3 = dfs(syn+1, n+1, a, b, c + l[n])
branch4 = dfs(syn, n+1, a, b, c)
return min(branch1, branch2, branch3, branch4)
print((dfs(0, 0, 0, 0, 0))) | p03111 |
#https://atcoder.jp/contests/abc119/tasks/abc119_c
#2019-02-24
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
ans = float("inf")
# 00 なら使わない、 01 ならAをつくる、10 ならBをつくる、11ならCをつくる
for i in range(1 << 2*N):
cost = float("inf")
A_hon = 0
B_hon = 0
C_hon = 0
A_zai = 0
B_zai = 0
C_zai = 0
for k in range(N):
if i & (1<<(2*k)) and i & (1<< (2*k)+1):
C_hon += 1
C_zai += l[k]
elif i & (1<<(2*k)+1):
B_hon += 1
B_zai += l[k]
elif i & (1<<(2*k)):
A_hon += 1
A_zai += l[k]
if A_hon * B_hon * C_hon >0:
cost = 10*(A_hon - 1)+ abs(A_zai - A)+10*(B_hon - 1)+ abs(B_zai - B)+ 10*(C_hon - 1)+ abs(C_zai - C)
ans = min(cost, ans)
print(ans) | #https://atcoder.jp/contests/abc119/tasks/abc119_c
#2019-02-24
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
ans = []
# 00 なら使わない、 01 ならAをつくる、10 ならBをつくる、11ならCをつくる
for i in range(1 << 2*N):
A_hon = 0
B_hon = 0
C_hon = 0
A_zai = 0
B_zai = 0
C_zai = 0
for k in range(N):
if i & (1<<(2*k)) and i & (1<< (2*k)+1):
C_hon += 1
C_zai += l[k]
elif i & (1<<(2*k)+1):
B_hon += 1
B_zai += l[k]
elif i & (1<<(2*k)):
A_hon += 1
A_zai += l[k]
if A_hon * B_hon * C_hon >0:
ans += [10*(A_hon - 1)+ abs(A_zai - A)+10*(B_hon - 1)+ abs(B_zai - B)+ 10*(C_hon - 1)+ abs(C_zai - C)]
print((min(ans))) | p03111 |
#https://atcoder.jp/contests/abc119/tasks/abc119_c
#2019-02-24
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
ans = []
# 00 なら使わない、 01 ならAをつくる、10 ならBをつくる、11ならCをつくる
for i in range(1 << 2*N):
A_hon = 0
B_hon = 0
C_hon = 0
A_zai = 0
B_zai = 0
C_zai = 0
for k in range(N):
if i & (1<<(2*k)) and i & (1<< (2*k)+1):
C_hon += 1
C_zai += l[k]
elif i & (1<<(2*k)+1):
B_hon += 1
B_zai += l[k]
elif i & (1<<(2*k)):
A_hon += 1
A_zai += l[k]
if A_hon * B_hon * C_hon >0:
ans += [10*(A_hon - 1)+ abs(A_zai - A)+10*(B_hon - 1)+ abs(B_zai - B)+ 10*(C_hon - 1)+ abs(C_zai - C)]
print((min(ans))) | import itertools
N, *A = list(map(int,input().split()))
l = [int(eval(input())) for i in range(N)]
li = itertools.product(list(range(4)), repeat=N)
ans = float("inf")
for i in li:
cnt = [0]*3
length = [0]*3
for k in range(N):
if i[k] == 3:
continue
cnt[i[k]] += 1
length[i[k]] += l[k]
if min(cnt) > 0:
cost = sum([abs(length[i]-A[i]) +10*cnt[i] for i in range(3)]) -30
ans = min(ans, cost)
print(ans) | p03111 |
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
import itertools
m = 4
n = N
bit_list = list(itertools.product([i for i in range(m)], repeat=n))
res = 10 ** 18
for b in bit_list:
tmp1 = 0
tmp2 = 0
tmp3 = 0
check1 = False
check2 = False
check3 = False
cost = 0
for i in range(n):
if b[i] == 1:
tmp1 += L[i]
if check1:
cost += 10
check1 = True
if b[i] == 2:
tmp2 += L[i]
if check2:
cost += 10
check2 = True
if b[i] == 3:
tmp3 += L[i]
if check3:
cost += 10
check3 = True
if min(tmp1, tmp2, tmp3) != 0:
res = min(res, abs(tmp1 - A)+abs(tmp2 - B)+abs(tmp3 - C)+cost)
print(res) | N, A, B, C = list(map(int, input().split()))
L = []
for i in range(N):
L.append(int(eval(input())))
import itertools
m = 4
bit_list = list(itertools.product([i for i in range(m)], repeat=N))
ans = 10 ** 18
for bit in bit_list:
retA = 0
retB = 0
retC = 0
checkA = False
checkB = False
checkC = False
cost = 0
for i,j in enumerate(bit):
if j == 1:
retA += L[i]
if checkA:
cost += 10
checkA = True
if j == 2:
retB += L[i]
if checkB:
cost += 10
checkB = True
if j == 3:
retC += L[i]
if checkC:
cost += 10
checkC = True
if min(retA,retB,retC) != 0:
ans = min(ans, abs(A-retA)+abs(B-retB)+abs(C-retC)+cost)
print(ans) | p03111 |
from itertools import groupby, accumulate, product, permutations, combinations
def calc(x,X):
n = len(x)
ans = 10**10
for q in product([0,1],repeat=n):
point = 0
cnt = 0
for i in range(n):
if q[i]==1:
if cnt>0:
point += 10
cnt += x[i]
if cnt>0:
point += abs(cnt-X)
else:
point = 10**10
ans = min(point,ans)
return ans
def solve():
ans = 10**10
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
for p in product(['A','B','C','D'],repeat=N):
a,b,c = [],[],[]
cnt = 0
for i in range(N):
if p[i]=='A':
a.append(L[i])
elif p[i]=='B':
b.append(L[i])
elif p[i]=='C':
c.append(L[i])
cnt += calc(a,A)+calc(b,B)+calc(c,C)
ans = min(ans,cnt)
return ans
print((solve())) | from itertools import groupby, accumulate, product, permutations, combinations
def calc(x,X):
n = len(x)
if n==0:
return 10**10
ans = abs(sum(x)-X)+(n-1)*10
return ans
def solve():
ans = 10**10
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
for p in product(['A','B','C','D'],repeat=N):
a,b,c = [],[],[]
cnt = 0
for i in range(N):
if p[i]=='A':
a.append(L[i])
elif p[i]=='B':
b.append(L[i])
elif p[i]=='C':
c.append(L[i])
cnt += calc(a,A)+calc(b,B)+calc(c,C)
ans = min(ans,cnt)
return ans
print((solve())) | p03111 |
n, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
from itertools import product
ptns = list(product([0,1,2,-1], repeat = n))
ans = 10**9
for i in range(len(ptns)):
if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
continue
abc = [0, 0, 0, -30]
for j in range(n):
tmp = ptns[i][j]
if tmp >= 0:
abc[tmp] += l[j]
abc[3] += 10
ans = min(ans, abc[3] + abs(abc[0] - a) + abs(abc[1] - b) + abs(abc[2] - c))
print(ans) | n, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
from itertools import product
ptns = list(product([0,1,2,-1], repeat = n))
anss = []
for i in range(len(ptns)):
if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
continue
abc = [0, 0, 0, -30]
ans = 10**9
for j in range(n):
tmp = ptns[i][j]
if tmp >= 0:
abc[tmp] += l[j]
abc[3] += 10
anss.append(abc[3] + abs(abc[0] - a) + abs(abc[1] - b) + abs(abc[2] - c))
print((min(anss))) | p03111 |
# DFS, パターン列挙_各竹の使用パターンを列挙
n, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
stack = [[0],[1],[2],[-1]] # 初期化_ここでは竹の使用パターン4つを設置
ptns = [] # パターン候補の器_初期化
while stack: # stackが空になるまでループ
tmp = stack.pop() # パターンの候補を pop
if len(tmp) == n: # 条件に合えば append
ptns.append(tmp)
elif len(tmp) < n: # 条件に合わなければ...
w = tmp + [0] # パターンの候補を作成して...
x = tmp + [1] # 〃
y = tmp + [2] # 〃
z = tmp + [-1]
stack += [w, x, y, z] # 積む
ans = 10**9
for i in range(len(ptns)):
if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
continue
abc = [0, 0, 0, -30]
for j in range(n):
tmp = ptns[i][j]
if tmp >= 0:
abc[tmp] += l[j]
abc[3] += 10
mp = abc[3]
mp += abs(abc[0] - a)
mp += abs(abc[1] - b)
mp += abs(abc[2] - c)
ans = min(ans, mp)
print(ans)
| # DFS, パターン列挙_各竹の使用パターンを列挙
n, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
stack = [[0],[1],[2],[-1]]
ans = 10**9
while stack:
tmp = stack.pop()
if len(tmp) == n:
if 0 not in tmp or 1 not in tmp or 2 not in tmp:
continue
abc = [0, 0, 0, -30]
for j in range(n):
if tmp[j] >= 0:
abc[tmp[j]] += l[j]
abc[3] += 10
mp = abc[3]
mp += abs(abc[0] - a)
mp += abs(abc[1] - b)
mp += abs(abc[2] - c)
ans = min(ans, mp)
elif len(tmp) < n:
w = tmp + [0]
x = tmp + [1]
y = tmp + [2]
z = tmp + [-1]
stack += [w, x, y, z]
print(ans)
| p03111 |
# DFS, パターン列挙_竹'n'本の使用パターン
n, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
stack = [[0],[1],[2],[-1]] # 初期化_ここでは竹の使用パターン4つを設置
ptns = [] # パターン候補の器_初期化
while stack: # stackが空になるまでループ
tmp = stack.pop() # パターンの候補を pop
if len(tmp) == n: # 条件に合えば append
ptns.append(tmp)
elif len(tmp) < n: # 条件に合わなければ...
w = tmp + [0] # パターンの候補を作成して...
x = tmp + [1] # 〃
y = tmp + [2] # 〃
z = tmp + [-1]
stack += [w, x, y, z] # 積む
#print(ptns) # 竹の使用パターン
ans = 10**9
for i in range(len(ptns)):
if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
continue
abc = [0, 0, 0, -30]
for j in range(n):
tmp = ptns[i][j]
if tmp >= 0:
abc[tmp] += l[j]
abc[3] += 10
mp = abc[3]
mp += abs(abc[0] - a)
mp += abs(abc[1] - b)
mp += abs(abc[2] - c)
ans = min(ans, mp)
print(ans)
'''
# パターン列挙と探索を並行して処理
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
stack = [[0],[1],[2],[-1]]
ans = 10**9
while stack:
tmp = stack.pop()
if len(tmp) == n:
if 0 not in tmp or 1 not in tmp or 2 not in tmp:
continue
abc = [0, 0, 0, -30]
for j in range(n):
if tmp[j] >= 0:
abc[tmp[j]] += l[j]
abc[3] += 10
mp = abc[3]
mp += abs(abc[0] - a)
mp += abs(abc[1] - b)
mp += abs(abc[2] - c)
ans = min(ans, mp)
elif len(tmp) < n:
w = tmp + [0]
x = tmp + [1]
y = tmp + [2]
z = tmp + [-1]
stack += [w, x, y, z]
print(ans)
''' | # DFS, パターン列挙_竹'n'本の使用パターン
'''
from collections import deque
n, a, b, c = map(int, input().split())
l = [int(input()) for _ in range(n)]
queue = deque([[0],[1],[2],[-1]]) # 初期化_ここでは竹の使用パターン4つを設置
ptns = [] # パターン候補の器_初期化
while queue: # stackが空になるまでループ
tmp = queue.pop() # パターンの候補を pop
if len(tmp) == n: # 条件に合えば append
ptns.append(tmp)
elif len(tmp) < n: # 条件に合わなければ...
w = tmp + [0] # パターンの候補を作成して...
x = tmp + [1] # 〃
y = tmp + [2] # 〃
z = tmp + [-1]
queue += [w, x, y, z] # 積む
#print(ptns) # 竹の使用パターン
ans = 10**9
for i in range(len(ptns)):
if 0 not in ptns[i] or 1 not in ptns[i] or 2 not in ptns[i]:
continue
abc = [0, 0, 0, -30]
for j in range(n):
tmp = ptns[i][j]
if tmp >= 0:
abc[tmp] += l[j]
abc[3] += 10
mp = abc[3]
mp += abs(abc[0] - a)
mp += abs(abc[1] - b)
mp += abs(abc[2] - c)
ans = min(ans, mp)
print(ans)
'''
# パターン列挙と探索を並行して処理
from collections import deque
n, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
queue = deque([[0],[1],[2],[-1]])
ans = 10**9
while queue:
tmp = queue.pop()
if len(tmp) == n:
if 0 not in tmp or 1 not in tmp or 2 not in tmp:
continue
abc = [0, 0, 0, -30]
for j in range(n):
if tmp[j] >= 0:
abc[tmp[j]] += l[j]
abc[3] += 10
mp = abc[3]
mp += abs(abc[0] - a)
mp += abs(abc[1] - b)
mp += abs(abc[2] - c)
ans = min(ans, mp)
elif len(tmp) < n:
w = tmp + [0]
x = tmp + [1]
y = tmp + [2]
z = tmp + [-1]
queue += [w, x, y, z]
print(ans)
| p03111 |
"""
できなかった
- 操作 = 四則演算 or 関数
- 交換法則などが成り立つかどうか確認する必要がある
- 再帰的(dfs)に実装する場合、何を初期値として終了条件とするかの理解が必要
- 前提として全探索であること => 1の解に収束する必要がない
全ての葉を訪れたときに最小・最大値・解への到達が判定できれば良い
- dfsの実装: いまどの高さにいるかを引数として渡す = 何を高さとして定義するか
- 階層のインデックスは選択可能な配列のインデックス
- 同階層のノードは選択肢
- ノードが持つ値は、その選択をした時の合計の値
- 終了条件A, B, Cを満たす
- 求める長さA, B, Cの竹 -> 選択肢
"""
(N, A, B, C) = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
def dfs(index, a, b, c):
if N == index:
if 0 < min(a, b, c):
return abs(a - A) + abs(b - B) + abs(c - C) - 30
else:
return 10 ** 9
costs = []
costs.append(dfs(index + 1, a, b, c))
costs.append(dfs(index + 1, a + l[index], b, c) + 10)
costs.append(dfs(index + 1, a, b + l[index], c) + 10)
costs.append(dfs(index + 1, a, b, c + l[index]) + 10)
return min(costs)
print((dfs(0, 0, 0, 0)))
| n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
inf = 10 ** 18
def dfs(i, a, b, c):
if n == i:
if 0 == a or 0 == b or 0 == c:
return inf
return abs(A - a) + abs(B - b) + abs(C - c)
res = dfs(i + 1, a, b, c)
res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
return res
print((dfs(0, 0, 0, 0)))
| p03111 |
n, A, B, C = list(map(int, input().split()))
l = [int(eval(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)))
| n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if n == i:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A - a) + abs(B - b) + abs(C - c)
res = dfs(i + 1, a, b, c)
res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
return res
print((dfs(0, 0, 0, 0)))
| p03111 |
n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if n == i:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A - a) + abs(B - b) + abs(C - c)
res = dfs(i + 1, a, b, c)
res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
return res
print((dfs(0, 0, 0, 0)))
| n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if i == n:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A - a) + abs(B - b) + abs(C - c)
res = dfs(i + 1, a, b, c)
res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
return res
print((dfs(0, 0, 0, 0)))
| p03111 |
n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if i == n:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A - a) + abs(B - b) + abs(C - c)
res = dfs(i + 1, a, b, c)
res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
return res
print((dfs(0, 0, 0, 0)))
| n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def bfs(i, a, b, c):
if i == n:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A - a) + abs(B - b) + abs(C - c)
res = bfs(i + 1, a, b, c)
res = min([res, bfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
res = min([res, bfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
res = min([res, bfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
return res
print((bfs(0, 0, 0, 0)))
| p03111 |
n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if n == i:
if 1 > a or 1 > b or 1 > c:
return 10 ** 18
return abs(A-a) + abs(B-b) + abs(C-c)
res = dfs(i + 1, a, b, c)
res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
return res
print((dfs(0, 0, 0, 0)))
| n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if i == n:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A - a) + abs(B - b) + abs(C - c)
res = dfs(i + 1, a, b, c)
res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
return res
print((dfs(0, 0, 0, 0)))
| p03111 |
n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if i == n:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A-a) + abs(B-b) + abs(C-c)
res = dfs(i + 1, a, b, c)
res = min([res, dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0)])
res = min([res, dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0)])
res = min([res, dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
return res
print((dfs(0, 0, 0, 0)))
| n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if i == n:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A - a) + abs(B - b) + abs(C - c)
return min([
dfs(i + 1, a, b, c),
dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
print((dfs(0, 0, 0, 0)))
| p03111 |
n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if i == n:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A - a) + abs(B - b) + abs(C - c)
return min([
dfs(i + 1, a, b, c),
dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)])
print((dfs(0, 0, 0, 0)))
| n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
def dfs(i, a, b, c):
if n == i:
if a < 1 or b < 1 or c < 1:
return 10 ** 18
return abs(A-a) + abs(B-b) + abs(C-c)
return min([
dfs(i + 1, a, b, c),
dfs(i + 1, a + l[i], b, c) + (10 if a > 0 else 0),
dfs(i + 1, a, b + l[i], c) + (10 if b > 0 else 0),
dfs(i + 1, a, b, c + l[i]) + (10 if c > 0 else 0)
])
print((dfs(0, 0, 0, 0)))
| p03111 |
N,*T = list(map(int,input().split()))
L = [int(eval(input())) for i in range(N)]
ans = 3000
for x in range(4**N):
l = [0]*4
t = [0]*4
n = 4
i = 0
for i in range(N):
l[x%n] += L[i]
t[x%n] += 1
x//=n
if 0 in l[:3]:
continue
cost = 0
for i in range(3):
cost += (t[i]-1)*10
cost += abs(l[i]-T[i])
ans = min(ans, cost)
print(ans)
| from itertools import product
N,*T = list(map(int,input().split()))
L = [int(eval(input())) for i in range(N)]
ans = 3000
for x in product([0,1,2,3], repeat=N):
cost = 0
l = [0]*4
for i,v in enumerate(x):
l[v] += L[i]
if v<3:
cost += 10
if 0 in l[:3]:
continue
cost -= 30
for i in range(3):
cost += abs(l[i]-T[i])
ans = min(ans, cost)
print(ans)
| p03111 |
import sys
import itertools
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
return sys.stdin.readline().rstrip()
def main():
N,A,B,C=list(map(int,input().split()))
l=[int(eval(input())) for _ in range(N)]
ps=itertools.permutations(l)
ans=INF
for p in ps:
for i in range(N-2):
for j in range(N-1-(i+1)):
for k in range(N-(i+1)-(j+1)):
tmp=0
a=sum(p[:i+1])
b=sum(p[i+1:i+1+j+1])
c=sum(p[i+1+j+1:i+1+j+1+k+1])
tmp=(i+j+k)*10+abs(A-a)+abs(B-b)+abs(C-c)
ans=min(ans,tmp)
print(ans)
if __name__ == '__main__':
main()
| import sys
import itertools
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
return sys.stdin.readline().rstrip()
def main():
N,A,B,C=list(map(int,input().split()))
l=[int(eval(input())) for _ in range(N)]
ps=itertools.permutations(l)
ans=INF
for p in ps:
S=[0]*(N+1)
for i in range(N):
S[i+1]=S[i]+p[i]
for i in range(N-2):
for j in range(N-1-(i+1)):
for k in range(N-(i+1)-(j+1)):
tmp=0
a=S[i+1]
b=S[i+1+j+1]-S[i+1]
c=S[i+1+j+1+k+1]-S[i+1+j+1]
tmp=(i+j+k)*10+abs(A-a)+abs(B-b)+abs(C-c)
ans=min(ans,tmp)
print(ans)
if __name__ == '__main__':
main()
| p03111 |
def calc(T, bam):
mix = len(bam) - 1
total = sum(bam)
return mix * 10 + abs(T - total)
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
var = [[] for _ in range(N + 1)]
var[0].append('')
for i in range(N):
for j in var[i]:
for c in '0abc':
var[i + 1].append(j + c)
V = var[N]
ans = 10 ** 6
for v in V:
a = []
b = []
c = []
cost = 0
for i, m in enumerate(v):
if m == 'a':
a.append(L[i])
elif m == 'b':
b.append(L[i])
elif m == 'c':
c.append(L[i])
if len(a) and len(b) and len(c):
CA = calc(A, a)
CB = calc(B, b)
CC = calc(C, c)
cost = CA + CB + CC
if cost == 5:
print('hello')
ans = min(ans, cost)
print(ans)
| import sys
sys.setrecursionlimit(10 ** 6)
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
INF = 10 ** 12
def dfs(cur, a, b, c):
if cur == N:
return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
ret0 = dfs(cur + 1, a, b, c)
ret1 = dfs(cur + 1, a + L[cur], b, c) + 10
ret2 = dfs(cur + 1, a, b + L[cur], c) + 10
ret3 = dfs(cur + 1, a, b, c + L[cur]) + 10
return min(ret0, ret1, ret2, ret3)
print((dfs(0, 0, 0, 0)))
| p03111 |
import bisect
INF=10**18
n,*a=list(map(int,input().split()))
l=[int(eval(input())) for _ in range(n)]
ans=INF
for i in range(4**n):
x=[[] for _ in range(3)]
for j in range(n):
if (i>>2*j)&1 and not (i>>2*j+1)&1:x[0].append(l[j])
if not (i>>2*j)&1 and (i>>2*j+1)&1:x[1].append(l[j])
if (i>>2*j)&1 and (i>>2*j+1)&1:x[2].append(l[j])
cnt=0
for j in range(3):
if len(x[j])==0:cnt=INF
cnt+=(len(x[j])-1)*10+abs(sum(x[j])-a[j])
ans=min(ans,cnt)
print(ans) | n,a,b,c=list(map(int,input().split()))
l=[int(eval(input())) for _ in range(n)]
ans=10**18
for i in range(4**n):
la,lb,lc,ca,cb,cc=0,0,0,0,0,0
for j in range(n):
if (i>>2*j)&1 and not (i>>2*j+1)&1:
ca+=1
la+=l[j]
if not (i>>2*j)&1 and (i>>2*j+1)&1:
cb+=1
lb+=l[j]
if (i>>2*j)&1 and (i>>2*j+1)&1:
cc+=1
lc+=l[j]
if ca*cb*cc:ans=min((ca+cb+cc-3)*10+abs(la-a)+abs(lb-b)+abs(lc-c),ans)
print(ans) | p03111 |
N, A, B, C = list(map(int, input().split()))
L_list = [int(eval(input())) for i in range(N)]
import itertools
Target = itertools.product([0, 1, 2, 3], repeat=N)
INF = 10 ** 9
#Target = [(1,2,2,2,3)]
ans = INF
for T in Target:
A_sum = 0
B_sum = 0
C_sum = 0
A_cost = 0
B_cost = 0
C_cost = 0
for i in range(N):
if T[i] == 0: continue
if T[i] == 1:
if A_sum == 0:
A_sum += L_list[i]
else:
A_sum += L_list[i]
A_cost += 10
if T[i] == 2:
if B_sum == 0:
B_sum += L_list[i]
else:
B_sum += L_list[i]
B_cost += 10
if T[i] == 3:
if C_sum == 0:
C_sum += L_list[i]
else:
C_sum += L_list[i]
C_cost += 10
if min(A_sum, B_sum, C_sum) == 0: continue
ans = min(ans, abs(A - A_sum)+A_cost + abs(B - B_sum)+B_cost + abs(C - C_sum)+C_cost)
print(ans)
| N, A, B, C = list(map(int, input().split()))
L_list = [int(eval(input())) for i in range(N)]
INF = 10 ** 9
def dfs(a, b, c, n):
if n == N:
return abs(A - a) + abs(B - b) + abs(C - c) - 30 if min(a, b, c) > 0 else INF
ret1 = dfs(a, b, c, n + 1)
ret2 = dfs(a + L_list[n], b, c, n + 1) + 10
ret3 = dfs(a, b + L_list[n], c, n + 1) + 10
ret4 = dfs(a, b, c + L_list[n], n + 1) + 10
return min(ret1, ret2, ret3, ret4)
ans = dfs(0, 0, 0, 0)
print(ans)
'''
8 100 90 80
100
100
90
90
90
80
80
80
0
8 1000 800 100
300
333
400
444
500
555
600
666
243
''' | p03111 |
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))
import itertools
N, *A = mi()
L = [ii() for _ in range(N)]
L_ = [L[i] for i in range(N)]
N_ = N
A = sorted(A)
#sum_l = sum(L)
tmp = [0] * 3
ans = float('inf')
for k in itertools.product([0,1], repeat=N_):
N = sum(k)
if N < 3:
continue
L = []
for x in range(N_):
if k[x]:
L.append(L_[x]*k[x])
#L = [L_[x]*k[x] for x in range(N_)]
sum_l = sum(L)
for i in range(1, N+1):
for koho_i in range(1, 2**i):
for koho_j in range(1, 2**i):
j = i
#print(koho_i, koho_j)
if koho_i & koho_j:
continue
#tmp[0] = [L[x]*koho_i[x] for x in range(i)]
tmp[0] = sum([L[x]*min((koho_i & 1<<x), 1) for x in range(i)])
#tmp[1] = [L[x]*koho_j[x] for x in range(j)]
tmp[1] = sum([L[x]*min((koho_j & 1<<x), 1) for x in range(j)])
tmp[2] = sum_l - tmp[0] - tmp[1]
#print(tmp)
if 0 in tmp:
continue
tmp = sorted(tmp)
cost = (N-3) * 10 + sum([abs(tmp[x]-A[x]) for x in range(3)])
#if cost < ans:
#print(tmp, cost, N)
#if tmp[0] == 80 and tmp[1] == 91 and tmp[2] == 98:
#print(cost)
ans = min(ans, cost)
print(ans)
| import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))
import itertools
N, *A = mi()
L = [ii() for _ in range(N)]
A = sorted(A)
sum_a = sum(A)
def dfs(i, a, b, c, use):
if (0 not in [a, b, c]) and use > 2:
global ans
abc = sorted([a, b, c])
ans = min(ans, (use-3)*10 + sum([abs(abc[i]-A[i]) for i in range(3)]))
if i > N-1:
return
dfs(i+1, a+L[i], b, c, use+1)
dfs(i+1, a, b+L[i], c, use+1)
dfs(i+1, a, b, c+L[i], use+1)
dfs(i+1, a, b, c, use)
ans = float('inf')
dfs(0, 0, 0, 0, 0)
print(ans) | p03111 |
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))
import itertools
N, *A = mi()
L = [ii() for _ in range(N)]
A = sorted(A)
sum_a = sum(A)
def dfs(i, a, b, c, use):
if (0 not in [a, b, c]) and use > 2:
global ans
abc = sorted([a, b, c])
ans = min(ans, (use-3)*10 + sum([abs(abc[i]-A[i]) for i in range(3)]))
if i > N-1:
return
dfs(i+1, a+L[i], b, c, use+1)
dfs(i+1, a, b+L[i], c, use+1)
dfs(i+1, a, b, c+L[i], use+1)
dfs(i+1, a, b, c, use)
ans = float('inf')
dfs(0, 0, 0, 0, 0)
print(ans) | import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return list(map(int, sys.stdin.readline().split()))
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))
import itertools
N, *A = mi()
L = [ii() for _ in range(N)]
#A = sorted(A)
sum_a = sum(A)
def dfs(i, a, b, c, use):
if (0 not in [a, b, c]) and use > 2:
global ans
#abc = sorted([a, b, c])
#ans = min(ans, (use-3)*10 + sum([abs(abc[i]-A[i]) for i in range(3)]))
ans = min(ans, (use-3)*10 + abs(a-A[0]) + abs(b-A[1]) + abs(c-A[2]))
if i > N-1:
return
dfs(i+1, a+L[i], b, c, use+1)
dfs(i+1, a, b+L[i], c, use+1)
dfs(i+1, a, b, c+L[i], use+1)
dfs(i+1, a, b, c, use)
ans = float('inf')
dfs(0, 0, 0, 0, 0)
print(ans)
| p03111 |
# coding:utf-8
import sys
import itertools
INF = float('inf')
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return eval(input())
n, a, b, c = LI()
A = [a, b, c]
K = [II() for _ in range(n)]
ans = INF
for x in itertools.product([0, 1, 2, 3], repeat=n):
s = set(x)
if 0 not in s or 1 not in s or 2 not in s:
continue
T = [0] * 3
cnt = 0
for i, n in enumerate(x):
if n == 3:
continue
if T[n]:
cnt += 1
T[n] += K[i]
# print(T, cnt)
tmp = cnt * 10
for i in range(3):
tmp += abs(T[i] - A[i])
ans = min(ans, tmp)
print(ans)
| # coding:utf-8
import sys
INF = float('inf')
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return eval(input())
n, a, b, c = LI()
A = [a, b, c]
K = [II() for _ in range(n)]
def DFS(cur, x, y, z):
if cur == n:
return abs(x - a) + abs(y - b) + abs(z - c) - 30 if min(x, y, z) > 0 else INF
ret0 = DFS(cur + 1, x, y, z)
ret1 = DFS(cur + 1, x + K[cur], y, z) + 10
ret2 = DFS(cur + 1, x, y + K[cur], z) + 10
ret3 = DFS(cur + 1, x, y, z + K[cur]) + 10
return min(ret0, ret1, ret2, ret3)
print((DFS(0, 0, 0, 0)))
| p03111 |
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for i in range(N)]
def dfs(d, a, b, c):
if d == N:
res = abs(a - A) + abs(b - B) + abs(c - C) - 30
return 1e9 if min(a, b, c) == 0 else res
ret = [0] * 4
ret[0] = dfs(d + 1, a, b, c)
ret[1] = dfs(d + 1, a + l[d], b, c) + 10
ret[2] = dfs(d + 1, a, b + l[d], c) + 10
ret[3] = dfs(d + 1, a, b, c + l[d]) + 10
return min(ret)
print((dfs(0, 0, 0, 0)))
| N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
def rec(itr, a, b, c, mp):
if itr == N:
if a == 0 or b == 0 or c == 0:
return 10 ** 9
return abs(a - A) + abs(b - B) + abs(c - C) + mp - 30
res = rec(itr + 1, a, b, c, mp)
res = min(res, rec(itr + 1, a + L[itr], b, c, mp) + 10)
res = min(res, rec(itr + 1, a, b + L[itr], c, mp) + 10)
res = min(res, rec(itr + 1, a, b, c + L[itr], mp) + 10)
return res
print((rec(0, 0, 0, 0, 0)))
| p03111 |
import itertools
def diff(a,b,c):
return abs(A-a)+abs(B-b)+abs(C-c)
N,A,B,C,*l=list(map(int,open(0).read().split()))
_min=10**6
for i in itertools.product([0,1,2,3],repeat=N):
cnt=0
abc=[0,0,0]
for j in range(N):
if i[j] !=3:
abc[i[j]]+=l[j]
cnt+=1
if 0 in abc:
continue
_min=min(_min,10*cnt-30+diff(abc[0],abc[1],abc[2]))
print(_min)
| def dfs(cnt,a,b,c):
if cnt == N:
_sum = abs(A-a) + abs(B-b) + abs(C-c) - 30
return _sum if min(a,b,c) > 0 else 10**9
ret0 = dfs(cnt+1, a, b, c)
ret1 = dfs(cnt+1, a+l[cnt] , b, c) + 10
ret2 = dfs(cnt+1, a, b+l[cnt], c) + 10
ret3 = dfs(cnt+1, a, b, c+l[cnt]) + 10
return min(ret0,ret1,ret2,ret3)
N,A,B,C,*l = list(map(int,open(0).read().split()))
print((dfs(0,0,0,0))) | p03111 |
from itertools import product
n,*B=list(map(int,input().split()))
A=[int(eval(input())) for i in range(n)]
s=10**9
for P in product([0,1,2,3],repeat=n):
if {0,1,2}<=set(P):
m=[0,0,0]
for x,y in zip(P,A):
if x!=3:
m[x]+=y
t=sum(abs(m-y) for m,y in zip(m,B))
l=sum(1 for j in P if j!=3)-3
s=min(s,t+l*10)
print(s) | from itertools import *
N,A,B,C = list(map(int, input().split()))
L = [int(eval(input())) for n in range(N)]
ans = []
for i in product(list(range(4)),repeat=N):
a = 0
b = 0
c = 0
d = 0
for j in range(N):
if i[j]:
d+=1
if i[j]==1:
a+=L[j]
elif i[j]==2:
b+=L[j]
else:
c+=L[j]
if a*b*c:
ans+=[abs(A-a)+abs(B-b)+abs(C-c)+10*(d-3)]
print((min(ans))) | p03111 |
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= list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
v = [-1] * N
ans = []
dfs(0)
print((min(ans))) | from itertools import product
def main():
N, A, B, C = list(map(int, input().split()))
L = list(int(eval(input())) for _ in range(N))
ans = 0
# A => 0
# B => 1
# C => 2
ans = float('inf')
for item in product(list(range(4)), repeat=N):
length_A = 0
cnt_A = 0
score_A = 0
length_B = 0
cnt_B = 0
score_B = 0
length_C = 0
cnt_C = 0
score_C = 0
for i,j in enumerate(item):
if j == 0:
length_A += L[i]
cnt_A += 1
if j == 1:
length_B += L[i]
cnt_B += 1
if j == 2:
length_C += L[i]
cnt_C += 1
if cnt_A == 0 or cnt_B == 0 or cnt_C == 0:
continue
score_A += abs(A - length_A) + 10 * (cnt_A - 1)
score_B += abs(B - length_B) + 10 * (cnt_B - 1)
score_C += abs(C - length_C) + 10 * (cnt_C - 1)
ans = min(ans, score_A + score_B + score_C)
print(ans)
if __name__ == "__main__":
main() | p03111 |
def dfs(i, A, B, C):
global ans
if i == n:
if A and B and C:
s = 0
ABC = [A, B, C]
for i in range(3):
s += (len(ABC[i]) - 1) * 10 + abs(sum(ABC[i]) - abc[i])
ans = min(ans, s)
else:
dfs(i + 1, A + [l[i]], B, C)
dfs(i + 1, A, B + [l[i]], C)
dfs(i + 1, A, B, C + [l[i]])
dfs(i + 1, A, B, C)
n, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for i in range(n)]
abc = [a, b, c]
ans = float("inf")
dfs(0, [], [], [])
print(ans) | def dfs(i, A, B, C):
global ans
if i == n:
if A and B and C:
ans = min(ans, abs(a - sum(A)) + abs(b - sum(B)) + abs(c - sum(C)) + (len(A) + len(B) + len(C) - 3) * 10)
else:
# a の肉焼き器に乗せるか、 b の肉焼き器に乗せるか
dfs(i + 1, A, B, C)
dfs(i + 1, A + [L[i]], B, C)
dfs(i + 1, A, B + [L[i]], C)
dfs(i + 1, A, B, C + [L[i]])
n, a, b, c = list(map(int, input().split()))
L = [int(eval(input())) for i in range(n)]
ans = float("inf")
dfs(0, [], [], [])
print(ans) | p03111 |
def dfs(i, A, B, C):
global ans
if i == n:
if A and B and C:
ans = min(ans, abs(a - sum(A)) + abs(b - sum(B)) + abs(c - sum(C)) + (len(A) + len(B) + len(C) - 3) * 10)
else:
dfs(i + 1, A, B, C)
dfs(i + 1, A + [L[i]], B, C)
dfs(i + 1, A, B + [L[i]], C)
dfs(i + 1, A, B, C + [L[i]])
n, a, b, c = list(map(int, input().split()))
L = [int(eval(input())) for i in range(n)]
ans = float("inf")
dfs(0, [], [], [])
print(ans) | def dfs(i, a, b, c, plus):
global ans
if i == n:
if a and b and c:
mp = abs(a - A) + abs(b - B) + abs(c - C) + (plus - 3) * 10
ans = min(ans, mp)
else:
dfs(i + 1, a, b, c, plus)
dfs(i + 1, a + l[i], b, c, plus + 1)
dfs(i + 1, a, b + l[i], c, plus + 1)
dfs(i + 1, a, b, c + l[i], plus + 1)
n, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for i in range(n)]
ans = float("inf")
dfs(0, 0, 0, 0, 0)
print(ans) | p03111 |
n, a, b, c = list(map(int, input().split()))
take = []
for i in range(n):
t = int(eval(input()))
take.append(t)
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
def cal_cost(li, a):
if li == []:
return 10**12
co = 0
if len(li) == 1:
co += abs(li[0] - a)
else:
co += abs(sum(li) - a)
co += (len(li) - 1) * 10
return co
min_cost = 10**12
for i in range(4**n):
cost = 0
num = Base_10_to_n(i, 4).zfill(n)
for_A = []
for_B = []
for_C = []
for j in range(n):
if num[j] == '1':
for_A.append(take[j])
elif num[j] == '2':
for_B.append(take[j])
elif num[j] == '3':
for_C.append(take[j])
cost += cal_cost(for_A, a)
cost += cal_cost(for_B, b)
cost += cal_cost(for_C, c)
if cost < min_cost:
min_cost = cost
print(min_cost)
| n, a, b, c = list(map(int, input().split()))
bamboos = []
for i in range(n):
bamboo = int(eval(input()))
bamboos.append(bamboo)
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
def calc_score(group, target):
score = 0
if len(group) > 1:
score += 10 * (len(group) - 1)
length = sum(group)
score += abs(length - target)
return score
min_score = 10**12
for i in range(4**n):
num = Base_10_to_n(i, 4).zfill(n)
score = 0
group_A = []
group_B = []
group_C = []
for j in range(n):
if num[j] == '1':
group_A.append(bamboos[j])
elif num[j] == '2':
group_B.append(bamboos[j])
elif num[j] == '3':
group_C.append(bamboos[j])
if len(group_A) == 0 or len(group_B) == 0 or len(group_C) == 0:
continue
else:
score += calc_score(group_A, a)
score += calc_score(group_B, b)
score += calc_score(group_C, c)
if score < min_score:
min_score = score
print(min_score) | p03111 |
import sys
input=sys.stdin.readline
n,a,b,c=list(map(int,input().split()))
L=[]
A=[a,b,c]
for i in range(n):
L.append(int(eval(input())))
L.sort()
Ans=10**9
for i in range(4**n):
F=[[] for i in range(3)] #竹格納:[A,B,C]
now=i
for j in range(n):
r=now%(4**(j+1))
chk=r//(4**(j))
if chk!=3:
F[chk].append(L[j])
now-=r
if F[0] and F[1] and F[2]: #竹が1本以上あるとき
ans=0
for k in range(3):
f=F[k] #竹一覧
Len=len(f)
mp=10**9
for l in range(1,2**Len):
t=0
cnt=0
for q in range(Len):
if (l>>q)&1:
cnt+=1
t+=f[q]
mp=min(abs(t-A[k])+10*(max(0,cnt-1)),mp)
ans+=mp
Ans=min(ans,Ans)
print(Ans) | import sys
input=sys.stdin.readline
n,a,b,c=list(map(int,input().split()))
L=[]
A=[a,b,c]
for i in range(n):
L.append(int(eval(input())))
L.sort()
ans=10**9
for i in range(4**n):
F=[[] for i in range(3)] #竹格納:[A,B,C]
now=i
for j in range(n):
r=now%(4**(j+1))
chk=r//(4**(j))
if chk!=3:
F[chk].append(L[j])
now-=r
if F[0] and F[1] and F[2]: #竹が1本以上あるとき
mp=0
for k in range(3):
f=F[k]
mp+=abs(sum(f)-A[k])+10*(max(0,len(f)-1))
ans=min(mp,ans)
print(ans) | p03111 |
# -*- coding: utf-8 -*-
from itertools import product
def inpl(): return list(map(int, input().split()))
N, A, B, C = inpl()
L = [int(eval(input())) for _ in range(N)]
ans = 1e9
def calc(Q, D):
S = sum(Q)
res = 10*(len(Q)-1) + abs(D-S)
return res
for X in product(list(range(4)), repeat=N):
P = [[] for _ in range(4)]
for i, x in enumerate(X):
P[x].append(L[i])
if min(list(map(len, P[1:]))) == 0:
continue
tmp = 0
for Q, D in zip(P[1:], [A, B, C]):
tmp += calc(Q, D)
if tmp < ans:
ans = min(tmp, ans)
print(ans) | # -*- coding: utf-8 -*-
from itertools import product
def inpl(): return list(map(int, input().split()))
N, A, B, C = inpl()
L = [int(eval(input())) for _ in range(N)]
ans = 1e9
for X in product(list(range(4)), repeat=N):
P = [[] for _ in range(4)]
for i, x in enumerate(X):
P[x].append(L[i])
if min(list(map(len, P[1:]))) == 0:
continue
tmp = 0
for Q, D in zip(P[1:], [A, B, C]):
tmp += 10*(len(Q)-1) + abs(D-sum(Q))
if tmp < ans:
ans = min(tmp, ans)
print(ans)
| p03111 |
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
#L1~LNはL[0]~L[N-1]で取り出す。よってこれから竹1~Nのことを竹0~N-1と呼ぶ。
#作られるべきA,B,Cが自らの材料として選んだ竹を、集合memA, memB, memCに格納?
#メンバーの選び方[0]~[255]の「[合計初期サイズ, 本数]」を格納するリスト
initialSum = []
for i in range(2**N): #全てのフラグパターンについて計算する
nowsum = 0
nowbamboocnt = 0
for j in range(N): #2^(j+1)乗で割った余りが2^j以上なら、フラグjがTrue
if i % (2**(j+1)) >= 2**j:
nowbamboocnt += 1
nowsum += L[j] #j(0-N-1)本目の竹について見ているので、L[j]が今見ている竹の長さ
initialSum.append([nowsum, nowbamboocnt])
#テスト用→ print(initialSum)
#A,B,Cが竹を選んだ場合の所要MPを出し、最低値とその時の組み合わせをminに格納する
bambooset = {i for i in range(N)} #存在する竹の組み合わせ
min = 3000 #最低値の初期化
lestbamboo = bambooset.copy() #残っている竹
for i, initSumA in enumerate(initialSum): #i:0~255。initsumA[0]は初期合計サイズ, initsumA[1]は本数
#Aはこのfor文で既に選ばれていると考えればいい。
if initSumA[1] ==0 or N - initSumA[1] < 2: continue #竹が0とか、残り竹が0,1なら飛ばす
lestbamboo = bambooset.copy() #再初期化
for j in range(N): #使った竹はlestbambooからremove
if i % (2**(j+1)) >= 2**j: #j(0-N-1)本目の竹について考えている
lestbamboo.remove(j)
necesMPforA = abs(A - initSumA[0]) + (initSumA[1]-1)*10 #abs(目標-合計)-合成必要MP
#次はBを計算する。Bが取らなかったものの中からCが取る
for b in range(2**len(lestbamboo)): #残った竹それぞれにつき取るか取らないか選ぶ
#2*残り本数通りある。ex.3本残っているとして、b(000とか101とか)で、Bの選び方を示す
lestbambooList = list(lestbamboo) #リスト化する [0]~[本数-1]。値は竹番号[0-N]
#lestbambooListは自動的に昇順になっている
bamboocntB = 0 #Bの本数
nowsumB = 0
lestbambooforC = lestbamboo.copy() #Cのための残り竹を入れる
for k in range(len(lestbambooList)): #2^(k+1)乗で割った余りが2^k以上なら、フラグkがTrue
if b % (2**(k+1)) >= 2**k: #Bがとる場合
bamboocntB += 1
nowsumB += L[lestbambooList[k]] #竹番号はlestbambooList[k]に入っている
lestbambooforC.remove(lestbambooList[k])
if bamboocntB == 0: continue #0本だと駄目なので飛ばす
for m in range(2**len(lestbambooforC)): #残った竹それぞれにつき取るか取らないか
#このfor文終了1回につき、ABCそれぞれの組み合わせが1つ終了!
# →このループの終了時にminの判定を行う
lestbambooforCList = list(lestbambooforC) #やっぱりリスト化
bamboocntC = 0
nowsumC = 0
for n in range(len(lestbambooforCList)):
if m % (2**(n+1)) >= 2**n: #Cがとる場合
bamboocntC += 1
nowsumC += L[lestbambooforCList[n]] #竹番号はlestbambooListforC[n]に入っている
if bamboocntC == 0: continue #0本だと駄目なので飛ばす
nowTotalSum = necesMPforA + abs(B - nowsumB) + (bamboocntB-1)*10 + abs(C - nowsumC) + (bamboocntC-1)*10
#minの判定!
if nowTotalSum < min: min = nowTotalSum
print(min) | N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
#L1~LNはL[0]~L[N-1]で取り出す。よってこれから竹1~Nのことを竹0~N-1と呼ぶ。
#メンバーの選び方[0]~[255]の「[合計初期サイズ, 本数]」を格納するリスト
initialSum = []
for i in range(2**N): #全てのフラグパターンについて計算する
nowsum = 0
nowbamboocnt = 0
for j in range(N): #2^(j+1)乗で割った余りが2^j以上なら、フラグjがTrue
if i % (2**(j+1)) >= 2**j:
nowbamboocnt += 1
nowsum += L[j] #j(0-N-1)本目の竹について見ているので、L[j]が今見ている竹の長さ
initialSum.append([nowsum, nowbamboocnt])
#A,B,Cが竹を選んだ場合の所要MPを出し、最低値をminに格納する
bambooset = {i for i in range(N)} #存在する竹の組み合わせ
min = 3000 #最低値の初期化
lestbamboo = bambooset.copy() #残っている竹
for i, initSumA in enumerate(initialSum): #i:0~255。initsumA[0]は初期合計サイズ, initsumA[1]は本数
#Aはこのfor文で既に選ばれていると考えればいい。
if initSumA[1] ==0 or N - initSumA[1] < 2: continue #竹が0とか、残り竹が0,1なら飛ばす
lestbamboo = bambooset.copy() #再初期化
for j in range(N): #使った竹はlestbambooからremove
if i % (2**(j+1)) >= 2**j: #j(0-N-1)本目の竹について考えている
lestbamboo.remove(j)
necesMPforA = abs(A - initSumA[0]) + (initSumA[1]-1)*10 #abs(目標-合計)-合成必要MP
#次はBを計算する。Bが取らなかったものの中からCが取る
for b in range(2**len(lestbamboo)): #残った竹それぞれにつき取るか取らないか選ぶ
#2*残り本数通りある。ex.3本残っているとして、b(000とか101とか)で、Bの選び方を示す
lestbambooList = list(lestbamboo) #リスト化する [0]~[本数-1]。値は竹番号[0-N]
#lestbambooListは自動的に昇順になっている
bamboocntB = 0 #Bの本数
nowsumB = 0
lestbambooforC = lestbamboo.copy() #Cのための残り竹を入れる
for k in range(len(lestbambooList)): #2^(k+1)乗で割った余りが2^k以上なら、フラグkがTrue
if b % (2**(k+1)) >= 2**k: #Bがとる場合
bamboocntB += 1
nowsumB += L[lestbambooList[k]] #竹番号はlestbambooList[k]に入っている
lestbambooforC.remove(lestbambooList[k])
if bamboocntB == 0: continue #0本だと駄目なので飛ばす
for m in range(2**len(lestbambooforC)): #残った竹それぞれにつき取るか取らないか
#このfor文終了1回につき、ABCそれぞれの組み合わせが1つ終了!
# →このループの終了時にminの判定を行う
lestbambooforCList = list(lestbambooforC) #やっぱりリスト化
bamboocntC = 0
nowsumC = 0
for n in range(len(lestbambooforCList)):
if m % (2**(n+1)) >= 2**n: #Cがとる場合
bamboocntC += 1
nowsumC += L[lestbambooforCList[n]] #竹番号はlestbambooListforC[n]に入っている
if bamboocntC == 0: continue #0本だと駄目なので飛ばす
nowTotalSum = necesMPforA + abs(B - nowsumB) + (bamboocntB-1)*10 + abs(C - nowsumC) + (bamboocntC-1)*10
#minの判定!
if nowTotalSum < min: min = nowTotalSum
print(min) | p03111 |
import itertools
n, a, b, c = list(map(int, input().split()))
_l = [int(eval(input())) for i in range(n)]
cl = ('A', 'B', 'C', 'N')
result = 1e10
for pattern in itertools.product(*[cl]*n):
wands = {'A': 0, 'B': 0, 'C': 0}
point = 0
for i, char in enumerate(pattern):
if char != 'N':
if wands[char] != 0:
point += 10
wands[char] += _l[i]
if min(wands.values()) > 0:
point += abs(wands['A'] - a) + abs(wands['B'] - b) + abs(wands['C'] - c)
if point < result:
result = point
print(result)
| n, a, b, c = list(map(int, input().split()))
_l = [int(eval(input())) for i in range(n)]
inf = 10**9
# DP
def f(i, _a, _b, _c):
# A,B,Cにしようとしている枝の長さが現在_a,_b,_cのとき,i番目以降の材料を使って達成される点数の最小値
if i == n:
return abs(a - _a) + abs(b - _b) + abs(c - _c) if min(_a, _b, _c) > 0 else inf
# i番目を使わないとき
val1 = f(i + 1, _a, _b, _c)
# i番目をAに使うとき
val2 = f(i + 1, _a + _l[i], _b, _c) + (0 if _a == 0 else 10)
val3 = f(i + 1, _a, _b + _l[i], _c) + (0 if _b == 0 else 10)
val4 = f(i + 1, _a, _b, _c + _l[i]) + (0 if _c == 0 else 10)
return min(val1, val2, val3, val4)
print((f(0, 0, 0, 0)))
| p03111 |
from functools import lru_cache
n, a, b, c = list(map(int, input().split()))
_l = [int(eval(input())) for i in range(n)]
inf = 10**9
# DP
@lru_cache(maxsize=10000)
def f(i, _a, _b, _c):
# A,B,Cにしようとしている枝の長さが現在_a,_b,_cのとき,i番目以降の材料を使って達成される点数の最小値
if i == n:
return abs(a - _a) + abs(b - _b) + abs(c - _c) if min(_a, _b, _c) > 0 else inf
# i番目を使わないとき
val1 = f(i + 1, _a, _b, _c)
# i番目をAに使うとき
val2 = f(i + 1, _a + _l[i], _b, _c) + (0 if _a == 0 else 10)
val3 = f(i + 1, _a, _b + _l[i], _c) + (0 if _b == 0 else 10)
val4 = f(i + 1, _a, _b, _c + _l[i]) + (0 if _c == 0 else 10)
return min(val1, val2, val3, val4)
print((f(0, 0, 0, 0)))
| from functools import lru_cache
n, a, b, c = list(map(int, input().split()))
_l = [int(eval(input())) for i in range(n)]
inf = 10**9
# DP
@lru_cache(maxsize=1000)
def f(i, _a, _b, _c):
# A,B,Cにしようとしている枝の長さが現在_a,_b,_cのとき,i番目以降の材料を使って達成される点数の最小値
if i == n:
return abs(a - _a) + abs(b - _b) + abs(c - _c) if min(_a, _b, _c) > 0 else inf
# i番目を使わないとき
val1 = f(i + 1, _a, _b, _c)
# i番目をAに使うとき
val2 = f(i + 1, _a + _l[i], _b, _c) + (0 if _a == 0 else 10)
val3 = f(i + 1, _a, _b + _l[i], _c) + (0 if _b == 0 else 10)
val4 = f(i + 1, _a, _b, _c + _l[i]) + (0 if _c == 0 else 10)
return min(val1, val2, val3, val4)
print((f(0, 0, 0, 0)))
| p03111 |
import math
N, A, B, C = 5, 100, 90, 80
ARR = [
98,
40,
30,
21,
80
]
# N,A,B,C = 8, 100, 90, 80
# ARR = [
# 100,
# 100,
# 90,
# 90,
# 90,
# 80,
# 80,
# 80,
# ]
# N,A,B,C,= 8, 1000, 800, 100
# ARR = [
# 300,
# 333,
# 400,
# 444,
# 500,
# 555,
# 600,
# 666,
# ]
N,A,B,C = list(map(int,input().split()))
ARR = []
for i in range(N):
ARR.append(int(eval(input())))
def calculate(n, arr, a, b, c):
result = []
for i in range(4 ** n):
materialA = set()
materialB = set()
materialC = set()
materialNone = set()
for j in range(n):
s = (i >> 2 * j & 1)
t = (i >> (2 * j + 1) & 1)
if (s == 0) and (t == 0):
materialA.add(j)
if (s == 1) and (t == 0):
materialB.add(j)
if (s == 0) and (t == 1):
materialC.add(j)
if (s == 1) and (t == 1):
materialNone.add(j)
ok, mp = judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone)
if ok:
result.append(mp)
print((int(min(result))))
def judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone):
if materialNone == n:
return False, -1
if len(materialA & materialB) > 0:
return False, -1
if len(materialA & materialC) > 0:
return False, -1
if len(materialB & materialC) > 0:
return False, -1
if len(materialA) == 0:
return False, -1
if len(materialB) == 0:
return False, -1
if len(materialC) == 0:
return False, -1
mpA = 0
a = a - calculateResult(arr,materialA)
mpA += math.fabs(a)
if len(materialA) > 1:
mpA += 10 * (len(materialA) - 1)
mpB = 0
b = b - calculateResult(arr,materialB)
mpB += math.fabs(b)
if len(materialB) > 1:
mpB += 10 * (len(materialB) - 1)
mpC = 0
c = c - calculateResult(arr,materialC)
mpC += math.fabs(c)
if len(materialC) > 1:
mpC += 10 * (len(materialC) - 1)
return True,mpA+mpB+mpC
def calculateResult(arr,material):
result = 0
for m in material:
result = result + arr[m]
return result
calculate(N, ARR, A, B, C)
| import math
N, A, B, C = 5, 100, 90, 80
ARR = [
98,
40,
30,
21,
80
]
# N,A,B,C = 8, 100, 90, 80
# ARR = [
# 100,
# 100,
# 90,
# 90,
# 90,
# 80,
# 80,
# 80,
# ]
# N,A,B,C,= 8, 1000, 800, 100
# ARR = [
# 300,
# 333,
# 400,
# 444,
# 500,
# 555,
# 600,
# 666,
# ]
N,A,B,C = list(map(int,input().split()))
ARR = []
for i in range(N):
ARR.append(int(eval(input())))
def calculate(n, arr, a, b, c):
result = []
for i in range(4 ** n):
materialA = set()
materialB = set()
materialC = set()
materialNone = set()
for j in range(n):
s = (i >> 2 * j & 1)
t = (i >> (2 * j + 1) & 1)
if (s == 0) and (t == 0):
materialA.add(j)
if (s == 1) and (t == 0):
materialB.add(j)
if (s == 0) and (t == 1):
materialC.add(j)
if (s == 1) and (t == 1):
materialNone.add(j)
ok, mp = judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone)
if ok:
result.append(mp)
print((int(min(result))))
def judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone):
if materialNone == n:
return False, -1
if len(materialA) == 0:
return False, -1
if len(materialB) == 0:
return False, -1
if len(materialC) == 0:
return False, -1
mpA = 0
a = a - calculateResult(arr,materialA)
mpA += math.fabs(a)
if len(materialA) > 1:
mpA += 10 * (len(materialA) - 1)
mpB = 0
b = b - calculateResult(arr,materialB)
mpB += math.fabs(b)
if len(materialB) > 1:
mpB += 10 * (len(materialB) - 1)
mpC = 0
c = c - calculateResult(arr,materialC)
mpC += math.fabs(c)
if len(materialC) > 1:
mpC += 10 * (len(materialC) - 1)
return True,mpA+mpB+mpC
def calculateResult(arr,material):
result = 0
for m in material:
result = result + arr[m]
return result
calculate(N, ARR, A, B, C)
| p03111 |
import math
N, A, B, C = 5, 100, 90, 80
ARR = [
98,
40,
30,
21,
80
]
# N,A,B,C = 8, 100, 90, 80
# ARR = [
# 100,
# 100,
# 90,
# 90,
# 90,
# 80,
# 80,
# 80,
# ]
# N,A,B,C,= 8, 1000, 800, 100
# ARR = [
# 300,
# 333,
# 400,
# 444,
# 500,
# 555,
# 600,
# 666,
# ]
N,A,B,C = list(map(int,input().split()))
ARR = []
for i in range(N):
ARR.append(int(eval(input())))
def calculate(n, arr, a, b, c):
result = []
for i in range(4 ** n):
materialA = set()
materialB = set()
materialC = set()
materialNone = set()
for j in range(n):
s = (i >> 2 * j & 1)
t = (i >> (2 * j + 1) & 1)
if (s == 0) and (t == 0):
materialA.add(j)
if (s == 1) and (t == 0):
materialB.add(j)
if (s == 0) and (t == 1):
materialC.add(j)
if (s == 1) and (t == 1):
materialNone.add(j)
ok, mp = judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone)
if ok:
result.append(mp)
print((int(min(result))))
def judge(n, arr, a, b, c, materialA, materialB, materialC, materialNone):
if materialNone == n:
return False, -1
if len(materialA) == 0:
return False, -1
if len(materialB) == 0:
return False, -1
if len(materialC) == 0:
return False, -1
mpA = 0
a = a - calculateResult(arr,materialA)
mpA += math.fabs(a)
if len(materialA) > 1:
mpA += 10 * (len(materialA) - 1)
mpB = 0
b = b - calculateResult(arr,materialB)
mpB += math.fabs(b)
if len(materialB) > 1:
mpB += 10 * (len(materialB) - 1)
mpC = 0
c = c - calculateResult(arr,materialC)
mpC += math.fabs(c)
if len(materialC) > 1:
mpC += 10 * (len(materialC) - 1)
return True,mpA+mpB+mpC
def calculateResult(arr,material):
result = 0
for m in material:
result = result + arr[m]
return result
calculate(N, ARR, A, B, C)
| import math
# N, A, B, C = 7, 100, 90, 80
# ARR = [
# 98,
# 40,
# 30,
# 21,
# 80,
# 25,
# 100
# ]
N, A, B, C = list(map(int, input().split()))
ARR = []
for i in range(N):
ARR.append(int(eval(input())))
result = []
def dfs(deep, crr):
if deep == N:
ok, res = calculate(crr)
if ok:
result.append(int(res))
else:
for i in range(4):
crr[deep] = i
dfs(deep + 1, crr)
def calculate(crr):
sA = []
sB = []
sC = []
sN = []
for index, cr in enumerate(crr):
if cr == 0:
sN.append(ARR[index])
if cr == 1:
sA.append(ARR[index])
if cr == 2:
sB.append(ARR[index])
if cr == 3:
sC.append(ARR[index])
if len(sA) == 0:
return False, -1
if len(sB) == 0:
return False, -1
if len(sC) == 0:
return False, -1
s1 = (len(sA) - 1) * 10 + math.fabs(sum(sA) - A)
s2 = (len(sB) - 1) * 10 + math.fabs(sum(sB) - B)
s3 = (len(sC) - 1) * 10 + math.fabs(sum(sC) - C)
return True, s1 + s2 + s3
dfs(0, [0 for i in range(N)])
print((min(result))) | p03111 |
from collections import Counter
n, a, b, c = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(n)]
def base_n(x, n, zero_padding=8):
retval = [[], ['0']][x == 0]
while x > 0:
x, r = divmod(x, n)
retval.append(str(r))
cnt = zero_padding - len(retval)
pad = ['', '0'*cnt][cnt > 0]
return pad+''.join(retval[::-1])
ans = 10**8
for i in range(4**n):
base_4 = base_n(i, 4, n)
n_bamboo = n - base_4.count("0")
ctr = Counter(base_4)
if n_bamboo < 3 or any(k not in list(ctr.keys()) for k in "123"):
continue
cost = 0
for x, y in zip([a, b, c], "123"):
cost += abs(x - sum(L[idx]for idx, v in enumerate(base_4)
if v == y))+(ctr[y]-1)*10
if ans > cost:
ans = cost
print(ans) | n, a, b, c = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(n)]
INF = 10**8
def dfs(current_index, sum_a, sum_b, sum_c):
if current_index == n:
if min(sum_a, sum_b, sum_c) > 0:
return sum(abs(x-y) for x, y in
zip([a, b, c], [sum_a, sum_b, sum_c]))-30
else:
return INF
cost_none = dfs(current_index+1, sum_a, sum_b, sum_c)
cost_a = dfs(current_index+1, sum_a+L[current_index], sum_b, sum_c)+10
cost_b = dfs(current_index+1, sum_a, sum_b+L[current_index], sum_c)+10
cost_c = dfs(current_index+1, sum_a, sum_b, sum_c+L[current_index])+10
return min(cost_none, cost_a, cost_b, cost_c)
print((dfs(0, 0, 0, 0))) | p03111 |
from itertools import combinations
from itertools import permutations
def f(x, lst, rem):
d = abs(x - min(lst))
min_lst = min(lst)
tpl = None
for v in range(len(lst) - rem + 1):
for t in combinations(lst, v + 1):
if abs(x - sum(t)) + 10 * v < d:
d = abs(x - sum(t)) + 10 * v
tpl = t
if tpl is None:
lst.remove(min_lst)
else:
for v in tpl:
lst.remove(v)
return d, lst
def main():
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for i in range(N)]
ans = int(1e9+7)
for ABC in permutations((A, B, C), 3):
lst = L[:]
accum = 0
for i, v in enumerate(ABC):
d, lst = f(v, lst, 3 - i)
accum += d
ans = min(ans, accum)
print(ans)
main()
| from itertools import combinations
from itertools import permutations
def f(x, lst, rem):
d = int(1e9+7)
tpl = ()
for v in range(len(lst) - rem + 1):
for t in combinations(lst, v + 1):
if abs(x - sum(t)) + 10 * v < d:
d = abs(x - sum(t)) + 10 * v
tpl = t
for v in tpl:
lst.remove(v)
return d, lst
def main():
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for i in range(N)]
ans = int(1e9+7)
for ABC in permutations((A, B, C), 3):
lst = L[:]
accum = 0
for i, v in enumerate(ABC):
d, lst = f(v, lst, 3 - i)
accum += d
ans = min(ans, accum)
print(ans)
main()
| p03111 |
from itertools import product
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for i in range(N)]
INF = 10 ** 9
t1 = set([0,1,2,3])
t2= set([1,2,3])
def calc(t):
t3 = set(t)
if not (t3 == t1 or t3 == t2): return INF
a, b, c = 0, 0, 0
cnt = 0
for i, v in enumerate(t):
if v != 0: cnt += 1
if v == 1: a += l[i]
elif v == 2: b += l[i]
elif v == 3: c += l[i]
return abs(a-A) + abs(b-B) + abs(c-C) + (cnt-3) * 10
ans = 10 ** 9
# 4進数の配列を生成する
# 0:未使用,1:aに使用,2:bに使用,3:cに使用
for p in product(list(range(4)), repeat=N):
ans = min(ans, calc(p))
print(ans)
| from itertools import product
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for i in range(N)]
INF = 10 ** 9
ans = INF
def calc(a, b, c):
la, lb, lc = len(a), len(b), len(c)
if la + lb + lc < 3 or 0 in (la, lb, lc): return INF
sa, sb, sc = sum(a), sum(b), sum(c)
t = 0
return abs(sa-A) + abs(sb-B) + abs(sc-C) + (la+lb+lc-3) * 10
def dfs(n, a, b, c):
global ans
if n == N:
ans = min(ans, calc(a, b, c))
return
dfs(n+1, a, b, c)
dfs(n+1, a+[l[n]], b, c)
dfs(n+1, a, b+[l[n]], c)
dfs(n+1, a, b, c+[l[n]])
dfs(0, [], [], [])
print(ans)
| p03111 |
#%%
import itertools
#%%
N, A, B, C = list(map(int, input().split()))
materials = [int(eval(input())) for _ in range(N)]
iterobj = itertools.product(list(range(4)),repeat=N)
ans = 10**8
for use in iterobj:
if 1 in use and 2 in use and 3 in use:
synth = [0,0,0,0]
for m, u in zip(materials, use):
synth[u] += m
mp = 10 * (use.count(1) + use.count(2) + use.count(3) - 3)
mp += abs(A-synth[1]) + abs(B-synth[2]) + abs(C-synth[3])
ans = min(ans, mp)
print(ans)
| #%%
import sys
INPUT = sys.stdin.readline
def SINGLE_INT(): return int(INPUT())
def MULTIPLE_INT_LIST(): return list(map(int, INPUT().split()))
def MULTIPLE_INT_MAP(): return list(map(int, INPUT().split()))
def SINGLE_STRING(): return INPUT()
def MULTIPLE_STRING(): return INPUT().split()
#%%
N, A, B, C = MULTIPLE_INT_MAP()
materials = [int(eval(input())) for _ in range(N)]
INF = 10 ** 9
#%%
def dfs(cursor, a, b, c): # cursor:カーソル a,b,c:現在の竹の長さ
if cursor == N: # cursorが最後まで行ったら終了する。
return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) != 0 else INF
# なぜ30を減じているのかというと、
# dfs(0,0,0,0)で始まる以上、最初に選ばれるa,b,cを決定する際にもコストが10増加してしまうからである。
# a,b,cのいずれかに1本も竹を利用していない場合は不適であるため、三項演算子を利用してその場合のコストをINFとする。
# 再帰(4**N)
# カーソルの当たっている竹に対して、(A or B or Cに合成する) or (合成しない)の場合に分ける
no_compound = dfs(cursor+1, a, b, c)
compound_to_a = dfs(cursor+1, a + materials[cursor], b, c) + 10
compound_to_b = dfs(cursor+1, a, b + materials[cursor], c) + 10
compound_to_c = dfs(cursor+1, a, b, c + materials[cursor]) + 10
return min(no_compound, compound_to_a, compound_to_b, compound_to_c)
print((dfs(0, 0, 0, 0))) | p03111 |
def main():
n, a, b, c = list(map(int, input().split()))
arr = [int(eval(input())) for i in range(n)]
min_cost = float("inf")
for i in range(1, 2 ** n):
bin_i = "{:08b}".format(i)[::-1]
arr_i = [arr[x] for x in range(n) if bin_i[x] == "1"]
cost_i = (len(arr_i) - 1) * 10
sum_i = sum(arr_i)
for j in range(1, 2 ** n):
bin_j = "{:08b}".format(j)[::-1]
if any([x == "1" and y == "1" for x, y in zip(bin_i, bin_j)]):
continue
arr_j = [arr[x] for x in range(n) if bin_j[x] == "1"]
cost_j = (len(arr_j) - 1) * 10
sum_j = sum(arr_j)
for k in range(1, 2 ** n):
bin_k = "{:08b}".format(k)[::-1]
if any([x == "1" and y == "1" for x, y in zip(bin_i, bin_k)]):
continue
if any([x == "1" and y == "1" for x, y in zip(bin_j, bin_k)]):
continue
arr_k = [arr[x] for x in range(n) if bin_k[x] == "1"]
cost_k = (len(arr_k) - 1) * 10
sum_k = sum(arr_k)
cost = abs(sum_i - a) + abs(sum_j - b) + abs(sum_k - c) + cost_i + cost_j + cost_k
if min_cost > cost:
min_cost = cost
print(min_cost)
main()
| from itertools import product
def main():
n, *target = list(map(int, input().split()))
arr = [int(eval(input())) for i in range(n)]
tests = product("0123", repeat=n)
test_arr = [[] for _ in range(3)]
min_cost = float("inf")
for test in tests:
cost = 0
if "1" not in test or "2" not in test or "3" not in test:
continue
for i in range(3):
test_arr[i] = [arr[x] for x in range(n) if test[x] == str(i + 1)]
cost += (len(test_arr[i]) - 1) * 10
cost += abs(sum(test_arr[i]) - target[i])
if min_cost > cost:
min_cost = cost
print(min_cost)
main()
| p03111 |
N, a, b, c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(N)]
used = [0]*N
global pattern
pattern = []
global ans
ans = float('inf')
def dfs(A):
global pattern
global ans
if len(A) == N:
pattern.append(A)
ans = min(ans, MP(A))
return
for i in range(4):
A.append(i)
dfs(A)
A.pop()
return
def MP(A):
group1 = []
group2 = []
group3 = []
for i in range(N):
if A[i] == 1:
group1 += [l[i]]
elif A[i] == 2:
group2 += [l[i]]
elif A[i] == 3:
group3 += [l[i]]
if len(group1) == 0 or len(group2) == 0 or len(group3) == 0:
return float('inf')
return calc(group1, a)+calc(group2, b)+calc(group3, c)
def calc(group, length):
res = float('inf')
for i in range(2**len(group)):
bag = []
for j in range(len(group)):
if (i>>j) & 1:
bag += [group[j]]
if len(bag) == 0:
continue
elif len(bag) == 1:
res = min(res, abs(length - bag[0]))
else:
res = min(res, abs(length - sum(group)) + 10*(len(group)-1))
return res
dfs([])
print(ans)
|
N, A, B, C = list(map(int, input().split()))
l = [int(eval(input())) for _ 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 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))) | p03111 |
import itertools
N,A,B,C=list(map(int,input().split()))
l=[int(eval(input())) for _ in range(N)]
d=list(itertools.product([0,1,2,3], repeat=N))
MIN=10**10
for i in range(len(d)):
ans=0
a_num=0
b_num=0
c_num=0
a_kadomatu=A
b_kadomatu=B
c_kadomatu=C
if {0,1,2} <= set(d[i]):
for g in range(len(d[i])):
if d[i][g]==0:
a_num+=1
a_kadomatu-=l[g]
elif d[i][g]==1:
b_num+=1
b_kadomatu-=l[g]
elif d[i][g]==2:
c_num+=1
c_kadomatu-=l[g]
ans=abs(a_kadomatu)+abs(b_kadomatu)+abs(c_kadomatu)+10*(a_num-1)+10*(b_num-1)+10*(c_num-1)
MIN=min(MIN,ans)
print(MIN) | def dfs(ka, kb, kc, cnta, cntb, cntc, phase):
global ans
if phase == n:
if 0 < cnta and 0 < cntb and 0 < cntc:
ans = min(ans, abs(a-ka) + abs(b-kb) + abs(c-kc) + 10*(cnta+cntb+cntc-3))
return
dfs(ka+l[phase], kb, kc, cnta+1, cntb, cntc, phase+1)
dfs(ka, kb+l[phase], kc, cnta, cntb+1, cntc, phase+1)
dfs(ka, kb, kc+l[phase], cnta, cntb, cntc+1, phase+1)
dfs(ka, kb, kc, cnta, cntb, cntc, phase+1)
n,a,b,c = list(map(int, input().split()))
l = [int(eval(input())) for _ in range(n)]
ans = 10**9
dfs(0,0,0,0,0,0,0)
print(ans) | p03111 |
from itertools import product
N,a,b,c = list(map(int,input().split()))
L = list(int(eval(input())) for i in range(N))
ans = float("inf")
for prod in product(list(range(4)), repeat=N) :
A = []
B = []
C = []
D = []
for i in range(len(prod)) :
if prod[i] == 1 :
A.append(L[i])
elif prod[i] == 2 :
B.append(L[i])
elif prod[i] == 3 :
C.append(L[i])
else :
D.append(L[i])
if len(A) * len(B) * len(C) == 0 :
continue
cost = 0
for li in [A,B,C] :
cost += (len(li)-1)*10
cost += abs(a-sum(A)) + abs(b-sum(B)) + abs(c-sum(C))
ans = min(ans, cost)
print(ans)
| from itertools import product
N,a,b,c = list(map(int,input().split()))
L = list(int(eval(input())) for i in range(N))
ans = float("inf")
for prod in product(list(range(4)), repeat=N) :
A = []
B = []
C = []
for i in range(len(prod)) :
if prod[i] == 1 :
A.append(L[i])
elif prod[i] == 2 :
B.append(L[i])
elif prod[i] == 3 :
C.append(L[i])
if len(A) * len(B) * len(C) == 0 :
continue
cost = 0
for li in [A,B,C] :
cost += (len(li)-1)*10
cost += abs(a-sum(A)) + abs(b-sum(B)) + abs(c-sum(C))
ans = min(ans,cost)
print(ans)
| p03111 |
class ans:
def __init__(self):
self.N, self.A, self.B, self.C = list(map(int,input().split()))
self.L = [int(eval(input())) for _ in range(self.N)]
print((self.dfs(0, 0, 0, 0)))
exit()
def dfs(self, x, a, b, c):
if(x == self.N):
if(min(a, b, c) > 0):
return abs(a-self.A)+abs(b-self.B)+abs(c-self.C) - 30
else:
return 1e9+7
ret0 = self.dfs(x+1, a, b, c)
ret1 = self.dfs(x+1, a+self.L[x], b, c) + 10
ret2 = self.dfs(x+1, a, b+self.L[x], c) + 10
ret3 = self.dfs(x+1, a, b, c+self.L[x]) + 10
return min(ret0, ret1, ret2, ret3)
ans() | N, A, B, C = list(map(int,input().split()))
L = [int(eval(input())) for _ in range(N)]
def dfs(x, a, b, c):
if(x == N):
if(min(a, b, c) > 0):
return abs(a-A)+abs(b-B)+abs(c-C)-30
else:
return 1e9+7
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))) | p03111 |
n, *li = list(map(int, input().split()))
L = list(map(int, (int(eval(input())) for _ in range(n))))
ans = int(1e9)
def f(A, num, cnt):
if num == n:
if 0 in A:
return
global ans
ans = min(ans, abs(A[0] - li[0]) + abs(A[1] - li[1]) + abs(A[2] - li[2]) + cnt)
return
for i in range(3):
p = 0 if A[i] == 0 else 10
B = A[::]
B[i] += L[num]
f(B, num + 1, cnt + p)
f(A, num + 1, cnt)
f([0, 0, 0], 0, 0)
print(ans)
| INF = float("inf")
n, A, B, C = list(map(int, input().split()))
L = tuple(map(int, (int(eval(input())) for _ in range(n))))
def f(a, b, c, num):
if num == n:
if min(a, b, c) > 0:
return abs(A - a) + abs(B - b) + abs(C - c)
else:
return INF
return min(
f(a + L[num], b, c, num + 1) + (10 if a else 0),
f(a, b + L[num], c, num + 1) + (10 if b else 0),
f(a, b, c + L[num], num + 1) + (10 if c else 0),
f(a, b, c, num + 1)
)
print((f(0, 0, 0, 0)))
| p03111 |
INF = float("inf")
n, A, B, C = list(map(int, input().split()))
L = tuple(map(int, (int(eval(input())) for _ in range(n))))
def f(a, b, c, num):
if num == n:
if min(a, b, c) > 0:
return abs(A - a) + abs(B - b) + abs(C - c)
else:
return INF
return min(
f(a + L[num], b, c, num + 1) + (10 if a else 0),
f(a, b + L[num], c, num + 1) + (10 if b else 0),
f(a, b, c + L[num], num + 1) + (10 if c else 0),
f(a, b, c, num + 1)
)
print((f(0, 0, 0, 0)))
| INF = float("inf")
n, A, B, C = list(map(int, input().split()))
L = tuple(map(int, (int(eval(input())) for _ in range(n))))
def f(a, b, c, num):
if num == n:
return abs(A - a) + abs(B - b) + abs(C - c) if min(a, b, c) > 0 else INF
return min(
f(a + L[num], b, c, num + 1) + (10 if a else 0),
f(a, b + L[num], c, num + 1) + (10 if b else 0),
f(a, b, c + L[num], num + 1) + (10 if c else 0),
f(a, b, c, num + 1)
)
print((f(0, 0, 0, 0)))
| p03111 |
INF = float("inf")
n, A, B, C = list(map(int, input().split()))
L = tuple(map(int, (int(eval(input())) for _ in range(n))))
def f(a, b, c, num):
if num == n:
return abs(A - a) + abs(B - b) + abs(C - c) if min(a, b, c) > 0 else INF
return min(
f(a + L[num], b, c, num + 1) + (10 if a else 0),
f(a, b + L[num], c, num + 1) + (10 if b else 0),
f(a, b, c + L[num], num + 1) + (10 if c else 0),
f(a, b, c, num + 1)
)
print((f(0, 0, 0, 0)))
| INF = int(1e9)
n, A, B, C = list(map(int, input().split()))
L = tuple(map(int, (int(eval(input())) for _ in range(n))))
def f(a, b, c, num):
if num == n:
return abs(A - a) + abs(B - b) + abs(C - c) if min(a, b, c) > 0 else INF
return min(
f(a + L[num], b, c, num + 1) + (10 if a else 0),
f(a, b + L[num], c, num + 1) + (10 if b else 0),
f(a, b, c + L[num], num + 1) + (10 if c else 0),
f(a, b, c, num + 1)
)
print((f(0, 0, 0, 0)))
| p03111 |
import itertools
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for i in range(N)]
def min_cost(target, l_list):
if target in l_list:
return 0
else:
res = 1000
for l in l_list:
res = min(res, abs(target - l))
for i, l in enumerate(l_list):
res = min(res, min_cost(target-l, l_list[:i]+l_list[i+1:]) + 10)
return res
ans = 1000 * 3
combs = list(itertools.product([0, 1, 2], repeat=N))
for comb in combs:
labc = [[], [], []]
for i, idx in enumerate(comb):
labc[idx].append(L[i])
la, lb, lc = labc
if len(la)*len(lb)*len(lc):
ans = min(ans, min_cost(A, la)+min_cost(B, lb)+min_cost(C, lc))
print(ans) | import itertools
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for i in range(N)]
def min_cost(target, l_list):
return 10*(len(l_list) - 1) + abs(target - sum(l_list))
ans = 1000 * 3
combs = list(itertools.product([0, 1, 2, 3], repeat=N))
for comb in combs:
labcn = [[], [], [], []]
for i, idx in enumerate(comb):
labcn[idx].append(L[i])
la, lb, lc, ln = labcn
if len(la)*len(lb)*len(lc):
ans = min(ans, min_cost(A, la)+min_cost(B, lb)+min_cost(C, lc))
print(ans) | p03111 |
import itertools
n,a,b,c=list(map(int,input().split()))
x=[a,b,c]
l=[int(eval(input())) for i in range(n)]
ans=1000000
loop='012'
for i in range(2**n):
use=[]
for j in range(n):
if((i>>j)&1):
use.append(l[j])
if(len(use)>=3):
for j in (itertools.product(loop,repeat=len(use))):
A=[[] for i in range(3)]
for k in range(len(use)):
A[int(j[k])].append(use[k])
if(len(A[0])!=0 and len(A[1])!=0 and len(A[2])!=0):
res=0
for i in range(3):
res+=abs(sum(A[i])-x[i])+(len(A[i])-1)*10
ans=min(res,ans)
print(ans) | def check(s,e,t):
global ans
if(not s or not e or not t):
return
ans=min(ans,(len(s)-1)*10+abs(A-sum(s))+(len(e)-1)*10+abs(B-sum(e))+(len(t)-1)*10+abs(C-sum(t)))
def dfs(s,e,t,i):
if(i==N):
check(s,e,t)
return
dfs(s,e,t,i+1)
dfs(s+[l[i]],e,t,i+1)
dfs(s,e+[l[i]],t,i+1)
dfs(s,e,t+[l[i]],i+1)
N,A,B,C=list(map(int,input().split()))
l=[int(eval(input())) for i in range(N)]
ans=10**18
dfs([],[],[],0)
print(ans) | p03111 |
N,A,B,C = list(map(int,input().split()))
L = []
for i in range(N):
L.append(int(eval(input())))
MP = float('inf')
n = 4
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)
for X in range(4**N):
STR = Base_10_to_n(X,n).zfill(N)
nU = []
uA = []
uB = []
uC = []
SUBMP = 0
for l in range(N):
if STR[l] == '0':
nU.append(L[l])
elif STR[l] == '1':
uA.append(L[l])
elif STR[l] == '2':
uB.append(L[l])
else:
uC.append(L[l])
if len(uA) != 0 and len(uB) != 0 and len(uC) != 0:
SUBMP = (len(uA)+len(uB)+len(uC)-3)*10+abs(sum(uA)-A)+abs(sum(uB)-B)+abs(sum(uC)-C)
if SUBMP <= MP:
MP = SUBMP
print(MP) | N,A,B,C = list(map(int,input().split()))
L = []
for i in range(N):
L.append(int(eval(input())))
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 = 4
ans = float('inf')
for X in range(n**N):
STR = Base_10_to_n(X,n).zfill(N)
cur = 0
TAKEA = 0
TAKEB = 0
TAKEC = 0
for i in range(len(STR)):
if STR[i] == '1':
TAKEA += L[i]
cur += 10
elif STR[i] == '2':
TAKEB += L[i]
cur += 10
elif STR[i] == '3':
TAKEC += L[i]
cur += 10
if TAKEA != 0 and TAKEB != 0 and TAKEC != 0:
cur += abs(TAKEA-A)+abs(TAKEB-B)+abs(TAKEC-C)
cur -= 30
if cur <= ans:
ans = cur
print(ans) | p03111 |
N,A,B,C=list(map(int,input().split()))
L=[0]*N
for i in range(N):
L[i]=int(eval(input()))
# ある竹を,Aに使う、Bに使う、Cに使う、どれにも使わない の4パターンで全探索
# A,B,C それぞれに対して2つ目以降は10Pかかる
stack=[]
# Aの長さ、Bの長さ、Cの長さ、処理した竹の本数、消費MP
stack.append([0,0,0,0,0])
ans=1000*10
while stack:
a,b,c,num,mp=stack.pop()
if num==N:
if a==0 or b==0 or c==0:
continue
mp+=abs(A-a)+abs(B-b)+abs(C-c)
if mp<ans:
ans=mp
continue
for i in range(4):
if i==0:
stack.append([a+L[num],b,c,num+1,mp+(a!=0)*10])
if i==1:
stack.append([a,b+L[num],c,num+1,mp+(b!=0)*10])
if i==2:
stack.append([a,b,c+L[num],num+1,mp+(c!=0)*10])
if i==3:
stack.append([a,b,c,num+1,mp])
print(ans) | N,A,B,C=list(map(int,input().split()))
L=[0]*N
for i in range(N):
L[i]=int(eval(input()))
# ある竹を,Aに使う、Bに使う、Cに使う、どれにも使わない の4パターンで全探索
# A,B,C それぞれに対して2つ目以降は10Pかかる
stack=[]
# Aの長さ、Bの長さ、Cの長さ、処理した竹の本数、消費MP
stack.append([0,0,0,0,0])
ans=1000*10
while stack:
a,b,c,num,mp=stack.pop()
if num==N:
if a==0 or b==0 or c==0:
continue
mp+=abs(A-a)+abs(B-b)+abs(C-c)
if mp<ans:
ans=mp
continue
stack.append([a+L[num],b,c,num+1,mp+(a!=0)*10])
stack.append([a,b+L[num],c,num+1,mp+(b!=0)*10])
stack.append([a,b,c+L[num],num+1,mp+(c!=0)*10])
stack.append([a,b,c,num+1,mp])
print(ans) | p03111 |
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy
sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())
# def main():
# # main()
# print(main())
n,a,b,c=LI()
l=[I() for _ in range(n)]
answer=inf
def f(lst,ind):
global answer
if ind==n:
ac=lst.count(1)
bc=lst.count(2)
cc=lst.count(3)
if ac==0 or bc==0 or cc==0:
return min(answer,inf)
_a=_b=_c=0
for i,x in enumerate(lst):
if x==1:
_a+=l[i]
elif x==2:
_b+=l[i]
elif x==3:
_c+=l[i]
# 合わなかった原因。短縮できることを忘れてた
# if _a>a or _b>b or _c>c:
# return min(answer,inf)
sm=0
sm+=max(0,ac-1)*10
sm+=max(0,bc-1)*10
sm+=max(0,cc-1)*10
sm+=abs(a-_a)
sm+=abs(b-_b)
sm+=abs(c-_c)
return min(answer,sm)
else:
for i in range(4):
lst[ind]=i
answer=min(answer,f(lst,ind+1))
return answer
lst=[-1]*n
print((f(lst,0)))
| import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy
sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
# def LF(): return [float(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return eval(input())
n,a,b,c=LI()
l=[I() for _ in range(n)]
def dfs(cnt,aa,bb,cc):
if cnt==n:
if aa==0 or bb==0 or cc==0:
return inf
return abs(aa-a)+abs(bb-b)+abs(cc-c)-30
aaa=dfs(cnt+1,aa+l[cnt],bb,cc)+10
bbb=dfs(cnt+1,aa,bb+l[cnt],cc)+10
ccc=dfs(cnt+1,aa,bb,cc+l[cnt])+10
ddd=dfs(cnt+1,aa,bb,cc)
return min(aaa,bbb,ccc,ddd)
print((dfs(0,0,0,0)))
| p03111 |
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 = list(map(int, input().split()))
a = [int(eval(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) | from itertools import permutations
from sys import setrecursionlimit
setrecursionlimit(1000000)
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 = list(map(int, input().split()))
a = [int(eval(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):
memo = {}
ret = min(ret, doit(p[0], p[1], p[2], 0))
print(ret) | p03111 |
import sys
from itertools import product
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, *A = list(map(int, readline().split()))
(*L,) = list(map(int, read().split()))
ans = INF
for p in product(list(range(4)), repeat=N):
num = [0] * 3
length = [0] * 3
for i, j in enumerate(p):
if j < 3:
num[j] += 1
length[j] += L[i]
res = 0
for j in range(3):
if num[j] == 0:
res = INF
break
else:
res += (num[j] - 1) * 10 + abs(length[j] - A[j])
if ans > res:
ans = res
print(ans)
return
if __name__ == '__main__':
main()
| import sys
from itertools import product
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007
def main():
N, A, B, C = list(map(int, readline().split()))
(*L,) = list(map(int, read().split()))
def rec(i, a, b, c):
if i == N:
return abs(a - A) + abs(b - B) + abs(c - C) - 30 if min(a, b, c) > 0 else INF
res0 = rec(i + 1, a, b, c)
res1 = rec(i + 1, a + L[i], b, c) + 10
res2 = rec(i + 1, a, b + L[i], c) + 10
res3 = rec(i + 1, a, b, c + L[i]) + 10
return min(res0, res1, res2, res3)
print((rec(0, 0, 0, 0)))
return
if __name__ == '__main__':
main()
| p03111 |
n,A,B,C,=list(map(int,input().split()))
l=[int(eval(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))) | N,A,B,C=list(map(int,input().split()))
l=[int(eval(input())) for _ in range(N)]
inf=10**18
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
res0 = dfs(cur+1,a,b,c)
res1 = dfs(cur+1,a+l[cur],b,c)+10
res2 = dfs(cur+1,a,b+l[cur],c)+10
res3 = dfs(cur+1,a,b,c+l[cur])+10
return min(res0,res1,res2,res3)
print((dfs(0,0,0,0))) | p03111 |
import itertools
N,A,B,C = list(map(int,input().split()))
l = [int(eval(input())) for i in range(N)]
inf = float('inf')
ans = inf
for pair in itertools.product(list(range(4)),repeat=N):
mA = 0 ; mB = 0; mC = 0; cnt = 0
for i in range(N):
if pair[i] == 1:
mA += l[i]
cnt += 1
elif pair[i] == 2:
mB += l[i]
cnt += 1
elif pair[i] == 3:
mC += l[i]
cnt += 1
if mA*mB*mC == 0:continue
cost = abs(mA-A) + abs(mB-B) + abs(mC-C) + (cnt-3)*10
if cost < ans:
ans = cost
print(ans) | N,A,B,C = list(map(int,input().split()))
l = [int(eval(input())) for i in range(N)]
inf = float('inf')
def dfs(depth=0, a=0, b=0, c=0):
if depth == N:
if a*b*c != 0:
return abs(a-A) + abs(b-B) + abs(c-C) - 30
return inf
res0 = dfs(depth+1,a,b,c)
res1 = dfs(depth+1,a+l[depth],b,c) + 10
res2 = dfs(depth+1,a,b+l[depth],c) + 10
res3 = dfs(depth+1,a,b,c+l[depth]) + 10
return min([res0,res1,res2,res3])
print((dfs())) | p03111 |
N,A,B,C = list(map(int,input().split()))
l = [int(eval(input())) for i in range(N)]
inf = float('inf')
def dfs(depth=0, a=0, b=0, c=0):
if depth == N:
if a*b*c != 0:
return abs(a-A) + abs(b-B) + abs(c-C) - 30
return inf
res0 = dfs(depth+1,a,b,c)
res1 = dfs(depth+1,a+l[depth],b,c) + 10
res2 = dfs(depth+1,a,b+l[depth],c) + 10
res3 = dfs(depth+1,a,b,c+l[depth]) + 10
return min([res0,res1,res2,res3])
print((dfs())) | N,A,B,C = list(map(int,input().split()))
l = [int(eval(input())) for _ in range(N)]
ans = float('inf')
def dfs(d=0,a=0,b=0,c=0,mp=0):
if d == N:
mp = mp + abs(A-a) + abs(B-b) + abs(C-c) - 30
global ans
if a*b*c > 0 and mp < ans:
ans = mp
return
dfs(d+1,a,b,c,mp)
dfs(d+1,a+l[d],b,c,mp+10)
dfs(d+1,a,b+l[d],c,mp+10)
dfs(d+1,a,b,c+l[d],mp+10)
dfs()
print(ans) | p03111 |
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
inf = 10 ** 9
def dfs(cur, a, b, c):
if cur == N:
return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else inf
non_marge = dfs(cur+1, a, b, c)
marge_a = dfs(cur+1, a+L[cur], b, c) + 10
marge_b = dfs(cur+1, a, b+L[cur], c) + 10
marge_c = dfs(cur+1, a, b, c+L[cur]) + 10
return min(non_marge, marge_a, marge_b, marge_c)
print((dfs(0, 0, 0, 0))) | N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
inf = 10**9
def dfs(cur, a, b, c):
if cur == N:
return abs(a-A)+abs(b-B)+abs(c-C)-30 if min(a,b,c)>0 else inf
no_merge = dfs(cur+1, a, b, c)
merge_a = dfs(cur+1, a+L[cur], b, c) + 10
merge_b = dfs(cur+1, a, b+L[cur], c) + 10
merge_c = dfs(cur+1, a, b, c+L[cur]) + 10
return min(no_merge, merge_a, merge_b, merge_c)
print((dfs(0, 0, 0, 0))) | p03111 |
from itertools import product
n,a,b,c=list(map(int,input().split()))
abc=[a,b,c]
l=tuple(int(eval(input())) for _ in range(n))
ans=10**18
for a in product(list(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)
| from itertools import product
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
N, A, B, C = list(map(int, input().split()))
L = [int(eval(input())) for _ in range(N)]
ans = 10 ** 18
for alloc in product(list(range(4)), repeat=N):
La = 0
Lb = 0
Lc = 0
use_a = 0
use_b = 0
use_c = 0
for i, f in enumerate(alloc):
if f == 0:
La += L[i]
use_a += 1
elif f == 1:
Lb += L[i]
use_b += 1
elif f == 2:
Lc += L[i]
use_c += 1
else:
pass
if not all([use_a, use_b, use_c]):
continue
score = 0
score += 10 * (use_a - 1)
score += 10 * (use_b - 1)
score += 10 * (use_c - 1)
score += abs(A - La)
score += abs(B - Lb)
score += abs(C - Lc)
ans = min(ans, score)
print(ans) | p03111 |
INF = 10 ** 18
N, A, B, C = list(map(int,input().split()))
ls = [int(eval(input())) for i in range(N)]
def func(pos, a, b, c):return abs(a - A) + abs(b - B) + abs(c - C) - 30 + INF * max(0, 1 - a * b * c) if pos == N else min(func(pos + 1, a + ls[pos] * (i == 0), b + ls[pos] * (i == 1), c + ls[pos] * (i == 2)) + 10 * (i < 3) for i in range(4))
print((func(0, 0, 0, 0))) | N, A, B, C = list(map(int,input().split()))
ls = [int(eval(input())) for i in range(N)]
def func(pos, a, b, c):return abs(a - A) + abs(b - B) + abs(c - C) - 30 + 10000 * max(0, 1 - a * b * c) if pos == N else min(func(pos + 1, a + ls[pos] * (i == 0), b + ls[pos] * (i == 1), c + ls[pos] * (i == 2)) + 10 * (i < 3) for i in range(4))
print((func(0, 0, 0, 0))) | p03111 |
N, A, B, C = list(map(int,input().split()))
ans = [[0, 0, 0, 0]]
for i in range(N):
l = int(eval(input()))
ans = [[p[j] + l * (i == j) for j in [0, 1, 2]] + [p[3]+(i < 3)] for p in ans for i in range(4)]
print((min(abs(p[0] - A) + abs(p[1] - B) + abs(p[2] - C) + p[3] * 10 - 30 for p in ans if min(p[:3]) > 0))) | N, A, B, C = list(map(int,input().split()))
ans = [[0, 0, 0, -30]]
for i in range(N):
l = int(eval(input()))
ans = [[p[j] + l * (i == j) for j in [0, 1, 2]] + [p[3] + (i < 3) * 10] for p in ans for i in range(4)]
print((min(abs(p[0] - A) + abs(p[1] - B) + abs(p[2] - C) + p[3] for p in ans if min(p[:3]) > 0))) | p03111 |
N, A, B, C = list(map(int,input().split()))
ans = [[0, 0, 0, -30]]
for i in range(N):
l = int(eval(input()))
ans = [[p[j] + l * (i == j) for j in [0, 1, 2]] + [p[3] + (i < 3) * 10] for p in ans for i in range(4)]
print((min(abs(p[0] - A) + abs(p[1] - B) + abs(p[2] - C) + p[3] for p in ans if min(p[:3]) > 0))) | N,A,B,C=list(map(int,input().split()));a=[[0,0,0,-30]]
for i in range(N):l=int(eval(input()));a=[[p[j]+l*(i==j)for j in[0,1,2]]+[p[3]+(i<3)*10]for i in range(4)for p in a]
print((min(abs(p[0]-A)+abs(p[1]-B)+abs(p[2]-C)+p[3]for p in a if min(p[:3])>0))) | p03111 |
N,*A=list(map(int,input().split()));a=[[0]*4];r=[0,1,2]
for i in range(N):l=int(eval(input()));a=[[p[j]+l*(i==j)for j in r]+[p[3]+(i<3)*10]for i in r+[3]for p in a]
print((min(sum(abs(p[i]-A[i])for i in r)-30+p[3]for p in a if min(p[:3])>0))) | N,*A=list(map(int,input().split()));a=[[0]*4];r=[0,1,2]
for i in range(N):l=int(eval(input()));a+=[[p[j]+l*(i==j)for j in r]+[p[3]+10]for i in r for p in a]
print((min(sum(abs(p[i]-A[i])for i in r)-30+p[3]for p in a if min(p[:3])>0))) | p03111 |
import sys
check_ary = [[0 for i in range(3)]for i in range(2)]
num_ary = [[0 for i in range(3)]for i in range(3)]
check_minus = False
for i in range(3):
num_ary[i] = list(map(int,input().split()))
for i in range(num_ary[0][0]+1):
for x in range(3):
check_ary[1][x] = num_ary[0][x] - i
for a in range(3):
check_ary[0][a] = num_ary[a][0] - check_ary[1][0]
for x in range(3):
for y in range(3):
if not check_ary[0][x] + check_ary[1][y] == num_ary[x][y]:
break
else:
continue
break
else :
print("Yes")
sys.exit()
print("No") | ary = [list(map(int,input().split())) for _ in range(3)]
for i in range(ary[0][0]+1):
a = [ary[0][0]-i,ary[0][1]-i,ary[0][2]-i]
a2 = ary[1][0] - a[0]
a3 = ary[2][0] - a[0]
b = [a2+a[0],a2+a[1],a2+a[2]]
c = [a3+a[0],a3+a[1],a3+a[2]]
if ary == [a,b,c]:print("Yes");exit()
else:print("No") | p03435 |
lis1 = []
lis2 = []
lis3 = []
count = 0
for num in input().split():
lis1.append(int(num))
for num in input().split():
lis2.append(int(num))
for num in input().split():
lis3.append(int(num))
for a1 in range(101):
for a2 in range(101):
for a3 in range(101):
count = 0
if lis1[0] - a1 == lis2[0] - a2 == lis3[0] - a3:
count += 1
if lis1[1] - a1 == lis2[1] - a2 == lis3[1] - a3:
count += 1
if lis1[2] - a1 == lis2[2] - a2 == lis3[2] - a3:
count += 1
if count == 3:
print("Yes")
break
if count == 3:
break
if count == 3:
break
if a1 == 100 and count != 3:
print("No") | lis1 = []
lis2 = []
lis3 = []
count = 0
flag = 0
for num in input().split():
lis1.append(int(num))
for num in input().split():
lis2.append(int(num))
for num in input().split():
lis3.append(int(num))
for a1 in range(101):
for a2 in range(101):
for a3 in range(101):
if lis1[0] - a1 == lis2[0] - a2 == lis3[0] - a3:
if lis1[1] - a1 == lis2[1] - a2 == lis3[1] - a3:
if lis1[2] - a1 == lis2[2] - a2 == lis3[2] - a3:
print("Yes")
flag = 1
break
if flag == 1:
break
if flag == 1:
break
if a1 == 100 and flag == 0:
print("No") | p03435 |
cij =[]
for i in range(3):
cij.append(list(map(int, input().split(" "))))
bi_list = []
for b1 in range(-500000, 500000):
b2 = b1 - (cij[0][0] - cij[0][1])
b3 = b1 - (cij[0][0] - cij[0][2])
a1 = cij[0][0]+cij[0][1]+cij[0][2] -b1-b2-b3
a2 = cij[1][0]+cij[1][1]+cij[1][2] -b1-b2-b3
a3 = cij[2][0]+cij[2][1]+cij[2][2] -b1-b2-b3
if a1/3+b1 ==cij[0][0] and a1/3+b2 ==cij[0][1] and a1/3+b3 ==cij[0][2] and a2/3+b1 ==cij[1][0] and a2/3+b2 ==cij[1][1] and a2/3+b3 ==cij[1][2] and a3/3+b1 ==cij[2][0] and a3/3+b2 ==cij[2][1] and a3/3+b3 ==cij[2][2]:
print("Yes")
exit()
print("No") | cij =[]
for i in range(3):
cij.append(list(map(int, input().split(" "))))
bi_list = []
for b1 in range(-50000, 50000):
b2 = b1 - (cij[0][0] - cij[0][1])
b3 = b1 - (cij[0][0] - cij[0][2])
a1 = cij[0][0]+cij[0][1]+cij[0][2] -b1-b2-b3
a2 = cij[1][0]+cij[1][1]+cij[1][2] -b1-b2-b3
a3 = cij[2][0]+cij[2][1]+cij[2][2] -b1-b2-b3
if a1/3+b1 ==cij[0][0] and a1/3+b2 ==cij[0][1] and a1/3+b3 ==cij[0][2] and a2/3+b1 ==cij[1][0] and a2/3+b2 ==cij[1][1] and a2/3+b3 ==cij[1][2] and a3/3+b1 ==cij[2][0] and a3/3+b2 ==cij[2][1] and a3/3+b3 ==cij[2][2]:
print("Yes")
exit()
print("No") | p03435 |
c=[list(map(int,input().split())) for i in range(3)]
x=c[0][2]-c[0][1]
y=c[0][1]-c[0][0]
for i in range(1,3):
if c[i][1]-c[i][0]!=y:
print("No")
break
if c[i][2]-c[i][1]!=x:
print("No")
break
else:
print("Yes") | c = [list(map(int, input().split())) for i in range(3)]
x = c[0][2] - c[0][1]
y = c[0][1] - c[0][0]
ans = 'Yes'
for i in range(1, 3):
if c[i][2] - c[i][1] != x or c[i][1] - c[i][0] != y:
ans = "No"
break
print(ans)
| p03435 |
# ABC088C - Takahashi's Information
def main():
arr = [list(map(int, input().rstrip().split())) for _ in range(3)]
memo = []
for i, j, k in arr:
memo += [(i - j, j - k)]
print(("Yes" if len(set(memo)) == 1 else "No"))
if __name__ == "__main__":
main() | # ABC088C - Takahashi's Information
def main():
*C, = list(map(int, open(0).read().split()))
memo = {(i - j, j - k) for i, j, k in zip(*[iter(C)] * 3)}
flg = len(memo) == 1
print(("Yes" if flg else "No"))
if __name__ == "__main__":
main() | p03435 |
def calc(a1,c):
As=[a1]
Bs=[]
Bs.append(c[0][0]-As[0])
As.append(c[1][0]-Bs[0])
As.append(c[2][0]-Bs[0])
Bs.append(c[0][1]-As[0])
Bs.append(c[0][2]-As[0])
for a in range(3):
for b in range(3):
if As[a]+Bs[b]!=c[a][b]: return False
return True
c=[]
for i in range(3):
c.append(list(map(int,input().split())))
flag=False
for i in range(-int(1e4),100):
if (calc(i,c)):
print('Yes')
flag=True
break
if not flag: print('No') | def calc(a1,c):
As=[a1]
Bs=[]
Bs.append(c[0][0]-As[0])
As.append(c[1][0]-Bs[0])
As.append(c[2][0]-Bs[0])
Bs.append(c[0][1]-As[0])
Bs.append(c[0][2]-As[0])
for a in range(3):
for b in range(3):
if As[a]+Bs[b]!=c[a][b]: return False
return True
c=[]
for i in range(3):
c.append(list(map(int,input().split())))
flag=False
if (calc(0,c)):
print('Yes')
else: print('No') | p03435 |
def confirm(a, b, c):
for i in range(3):
for j in range(3):
if a[i] + b[j] != c[i][j]:
return False
return True
def main():
c = [list(map(int, input().split())) for i in range(3)]
for a1 in range(101):
for a2 in range(101):
for a3 in range(101):
a = (a1, a2, a3)
b = (c[0][0] - a1, c[0][1] - a1, c[0][2] - a1)
if confirm(a, b, c):
return 'Yes'
return 'No'
if __name__ == '__main__':
ans = main()
print(ans)
| def main():
c = [list(map(int, input().split())) for i in range(3)]
for x in range(101):
b = (c[0][0] - x, c[0][1] - x, c[0][2] - x)
a = (x, c[1][0] - b[0], c[2][0] - b[0])
flag = True
for i in (1, 2):
for j in (1, 2):
if a[i] + b[j] != c[i][j]:
flag = False
if flag:
return 'Yes'
return 'No'
if __name__ == "__main__":
print((main()))
| p03435 |
c=[[int(i) for i in input().split()]for i in range(3)]
a=[0]*3
b=[0]*3
for i in range(3):
for j in range(3):
b[j]=c[i][j]-a[0]
for i in range(3):
for j in range(3):
a[j]=c[j][0]-b[0]
judge=True
for i in range(3):
for j in range(3):
if a[j]+b[i]!=c[j][i]:
judge=False
if judge:
print("Yes")
else:
print("No")
| s=[list(map(int,input().split()))for i in range(3)]
a=[0]*3
b=[0]*3
for i in range(3):
b[i]=s[0][i]-a[0]
for i in range(3):
a[i]=s[i][0]-b[0]
for i in range(3):
for j in range(3):
if a[i]+b[j]!=s[i][j]:
print("No")
exit()
print("Yes") | p03435 |
#import collections
#aa = collections.Counter(a) # list to list
#mod = 10**9 + 7
def readInts():
return list(map(int,input().split()))
def main():
C = [readInts() for _ in range(3)]
a = [0] * 3
b = [0] * 3
# a[0] = 0としとく
# 1つ適当に決めることでほかのものが決まる
# それでちょいちょいやればよーい!
for i in range(3):
b[i] = C[0][i] - a[0]
for i in range(1,3):
a[i] = C[i][0] - b[0]
for i in range(3):
for j in range(3):
if C[j][i] == a[j] + b[i]:
pass
else:
print('No')
exit()
print('Yes')
if __name__ == '__main__':
main()
| c = [list(map(int,input().split())) for _ in range(3)]
#print(LIST)
# a1 = 0 とする
a,b= [0,0,0],[0,0,0]
for i in range(3):
b[i] = c[0][i] - a[0]
for j in range(1,3):
a[j] = c[j][0] - b[0]
for i in range(1,3):
for j in range(1,3):
#print(a,b)
if c[j][i] == a[j] + b[i]:
pass
else:
print('No')
exit()
print('Yes')
| p03435 |
from itertools import product
c = [tuple(map(int, input().split())) for _ in range(3)]
maxNum = max(max(c))
for i in product(list(range(maxNum+1)),repeat=3):
if max(i) > maxNum: continue
for x in range(maxNum+1):
row1 = (i[0]+x,i[1]+x,i[2]+x)
if row1 == c[0]:
for y in range(maxNum+1):
row2 = (i[0]+y,i[1]+y,i[2]+y)
if row2 == c[1]:
for z in range(maxNum+1):
row3 = (i[0]+z,i[1]+z,i[2]+z)
if row3 == c[2]:
print("Yes")
exit()
print("No") | c = [list(map(int, input().split())) for _ in range(3)]
# 斜め同士を足し合わせ比較をする
if c[0][0] + c[1][1] == c[0][1] + c[1][0]:
if c[0][1] + c[1][2] == c[0][2] + c[1][1]:
if c[1][0] + c[2][1] == c[1][1] + c[2][0]:
if c[1][1] + c[2][2] == c[1][2] + c[2][1]:
print("Yes")
exit()
print("No") | p03435 |
def main():
A = [[int(i) for i in input().split()] for j in range(3)]
from itertools import product
for a1, a2, a3 in product(range(101), repeat=3):
c1 = A[0][0] - a1 == A[1][0] - a2 == A[2][0] - a3
c2 = A[0][1] - a1 == A[1][1] - a2 == A[2][1] - a3
c3 = A[0][2] - a1 == A[1][2] - a2 == A[2][2] - a3
if c1 and c2 and c3:
return print("Yes")
print("No")
if __name__ == '__main__':
main()
| def main():
A = [[int(i) for i in input().split()] for j in range(3)]
for i in range(1, 3):
for j in range(3):
A[i][j] -= A[0][j]
if (A[1][0] == A[1][1] == A[1][2]) and (A[2][0] == A[2][1] == A[2][2]):
print("Yes")
else:
print("No")
if __name__ == '__main__':
main()
| p03435 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.