题解 | #购物单#

购物单

https://www.nowcoder.com/practice/f9c6f980eeec43ef85be20755ddbeaf4

total_money, sum = map(int, input().split())
goods_dic = {}
for i in range(1, sum+1):
    v, p, q = map(int, input().split())
    v //= 10
    if q == 0:
        if i not in goods_dic:
            goods_dic[i] = [[v, v*p]]
        else:
            for lst in goods_dic[i]:
                lst[0] += v
                lst[1] += v*p
            goods_dic[i].append([v, v*p])
    else:
        if q not in goods_dic:
            goods_dic[q] = [[v, v*p]]
        else:
            le = len(goods_dic[q])
            for i in range(le):
                x, y = goods_dic[q][i]
                goods_dic[q].append([x+v, y+v*p])


goods_lst = []
for key, value in goods_dic.items():
    goods_lst.append(value)

le = len(goods_lst)
dp = [[0 for _ in range(total_money//10+1)] for _ in range(le+1)]

for i in range(1, le+1):
    for j in range(1, total_money//10+1):
        for lst in goods_lst[i-1]:
            v, w = lst
            if v == j:
                dp[i][j] = max(dp[i-1][j], w, dp[i][j])
            if v < j:
                dp[i][j] = max(dp[i-1][j], w + dp[i-1][j-v], dp[i][j])
            if v > j:
                dp[i][j] = max(dp[i-1][j], dp[i][j])

print(dp[-1][-1]*10)

全部评论

相关推荐

11-28 17:48
中山大学 C++
点赞 评论 收藏
分享
三年之期已到我的offer快到碗里来:9硕都比不上9本
点赞 评论 收藏
分享
无情咸鱼王的秋招日记之薛定谔的Offer:好拒信,偷了,希望有机会用到
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务