题解 | #购物单#
购物单
https://www.nowcoder.com/practice/f9c6f980eeec43ef85be20755ddbeaf4
n, m = map(int,input().split()) primary, annex = {}, {} for i in range(1,m+1): x, y, z = map(int, input().split()) if z==0: primary[i] = [x, y] else: if z in annex: annex[z].append([x, y]) else: annex[z] = [[x,y]] dp = [0]*(n+1) for key in primary: w, v= [], [] w.append(primary[key][0])#1、主件 v.append(primary[key][0]*primary[key][1]) if key in annex:#存在附件 w.append(w[0]+annex[key][0][0])#2、主件+附件1 v.append(v[0]+annex[key][0][0]*annex[key][0][1]) if len(annex[key])>1:#附件个数为2 w.append(w[0]+annex[key][1][0])#3、主件+附件2 v.append(v[0]+annex[key][1][0]*annex[key][1][1]) w.append(w[0]+annex[key][0][0]+annex[key][1][0])#4、主件+附件1+附件2 v.append(v[0]+annex[key][0][0]*annex[key][0][1]+annex[key][1][0]*annex[key][1][1]) for j in range(n,-1,-10):#物品的价格是10的整数倍 for k in range(len(w)): if j-w[k]>=0: dp[j] = max(dp[j], dp[j-w[k]]+v[k]) print(dp[n])
dp[j] = max(dp[j], dp[j-w[k]]+v[k]) #、dp[j-w[k]]表示剩下的钱查历史记录还能获得的满意度,v[k]表示当前循环商品获得的满意度