"""
https://www.lanqiao.cn/problems/3505/learning/?page=1&first_category_id=1&problem_id=3505
"""
# 对于每个瓜都有三种可能: 买一整个, 买一半, 不买
def dfs(depth, weight, cnt):
"""
weight: 买瓜的总重量
cnt: 切瓜的个数
"""
if weight > m:
return
if weight == m:
global ans
ans = min(ans, cnt)
if depth == n:
return
dfs(depth + 1, weight + a[depth], cnt) # 买整个
dfs(depth + 1, weight + a[depth] // 2, cnt + 1) # 买一半
dfs(depth + 1, weight, cnt) # 不买
n, m = map(int, input().split())
a = list(map(int, input().split()))
m *= 2
a = [x * 2 for x in a]
ans = n + 1
dfs(0, 0, 0)
if ans == n + 1:
ans = -1
print(ans)
蓝桥杯-买瓜
最新推荐文章于 2024-08-15 11:54:48 发布