先骂一句:煞笔洛谷、煞笔出题人
菜品价格 a i a_i ai 可能有多行,存在如下的奇葩样例:
22 20
2 3 3 4 5 4 5 5 10 23 4 5
10 10 10 10 10 10 17 18 19 20
导致使用 costs = list(map(int, input().split()))
读不到完整数据,结果出错。
煞笔出题人浪费我时间。
下面是正解:
def main():
m, n = map(int, input().split())
costs = []
while len(costs) < m:
costs.extend(map(int, input().split()))
dp = [0] * (n + 1)
dp[0] = 1
for c in costs:
for j in range(n, c - 1, -1):
dp[j] += dp[j - c]
print(dp[-1])
main()