hihocoder 完全背包

小Ho现在手上有M张奖券,而奖品区有N种奖品,分别标号为1到N,其中第i种奖品需要need(i)张奖券进行兑换,并且可以兑换无数次,为了使得辛苦得到的奖券不白白浪费,小Ho给每件奖品都评了分,其中第i件奖品的评分值为value(i),表示他对这件奖品的喜好值。现在他想知道,凭借他手上的这些奖券,可以换到哪些奖品,使得这些奖品的喜好值之和能够最大。

#include <iostream>
#include <memory.h>
#include <algorithm>
#define INF 0x3f3f3f3f
#define NUM 505
using namespace std;

int need[NUM];
int value[NUM];
int best[505][100005];
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>need[i]>>value[i];
    }
    for(int i=1;i<=n;i++)
    {
        for(int x=1;x<=m;x++)
        {
            if(need[i]>x)
            {
                best[i][x]=best[i-1][x];
            }
            else
            {
                best[i][x]=max(best[i-1][x],best[i][x-need[i]]+value[i]);
            }
        }
    }
    cout<<best[n][m]<<endl;

}
发布了164 篇原创文章 · 获赞 69 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/wqy20140101/article/details/71082687
今日推荐