题解 | 点菜问题

点菜问题

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

def fun(price, value, n, c):
    dp = [0] * (c+1)
    for i in range(n):
        for j in range(c, price[i] - 1, -1):
            dp[j] = max(dp[j-price[i]] + value[i], dp[j])
    return dp[c]

while True:
    try:
        c, n = list(map(int, input().split()))
        price = []
        value = []
        for i in range(n):
            a, b = list(map(int, input().split()))
            price.append(a)
            value.append(b)
        res = fun(price, value, n, c)
        print(res)
    except:
        break

全部评论

相关推荐

05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务