candidates = [10, 20, 20, 30, 40]
target = 45
target1 = 10
from functools import cache
@
cachedef dp(idx, count, min_element, max_element):
if idx == len(candidates):
________if count > target and count - min_element < target and count - max_element <= target - target1:
____________print(count, min_element, max_element)
____________return 1
________return 0
____if not count - min_element < target:
________return 0
____ele = candidates[idx]
____pick = dp(idx + 1, count + ele, min(ele, min_element), max(ele, max_element))
____not_pick = dp(idx + 1, count, min_element, max_element)
____return pick + not_pick
print(dp(0, 0, 4e18, 0))
O(n^3 * target) 只有方案数, 具体方案是什么你自己改一下.