from functools import lru_cache
class Solution:
def profitableSchemes(self, G: int, P: int, group: List[int], profit: List[int]) -> int:
val = [(g, p) for g, p in zip(group, profit)]
val.sort(key = lambda x : x[0], reverse=True)
# 背包大小为n,选前k种物品,总收益大于等于p的方法数
@lru_cache(typed=False, maxsize=12800000000)
def dp(n, k, p):
if p > 0 and n < val[k][0]:
return 0
if n == 0:
return 1 if p == 0 else 0
if k == 0:
ans = 0
if p == 0:
ans += 1
if n >= val[0][0] and val[0][1] >= p:
ans += 1
else:
ans = dp(n, k-1, p)
if n >= val[k][0]:
ans += dp(n-val[k][0], k-1, max(0, p - val[k][1]))
return ans % 1000000007
return dp(G, len(group)-1, P)