Charm Bracelet

Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi ≤ 400), a 'desirability' factor Di (1 ≤ Di ≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M (1 ≤ M ≤ 12,880).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.

Input

  • Line 1: Two space-separated integers: N and M
  • Lines 2..N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di

Output

  • Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints

Sample Input

4 6

1 4

2 6

3 12

2 7

Sample Output

23

不多说, 就是01背包模板题, 啥变化都没有

#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAXN 13000
using namespace std;
int n, m;
int w[MAXN], v[MAXN], dp[MAXN];

int main() {
    while(~scanf("%d%d", &n, &m)) {
        for (int i = 0; i < n; i++) {
            scanf("%d%d", &w[i], &v[i]);
        }
        memset(dp, 0, sizeof dp);
        for(int i = 0; i < n; i++) {
            for(int j = m; j >= w[i]; j--) {
                dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
            }
        }
        printf("%d\n", dp[m]);
    }

    return 0;
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 现在,写文章的是日渐多了,很多人在网上大讲写作经验,猛一看,真是写作天才了。 作品拿过来,一水的鸡汤文,这个价值观...
    陈清伟阅读 184评论 0 1
  • 电视剧《我的前半生》热播以来,掀起了一阵主妇离家出走找工作之风,也有很多职场人因为不满意工作现状企图跳槽。究其原因...
    有实阅读 426评论 0 1
  • 我对写作颇有兴趣,但我不太喜欢用手机写作,会觉得那样没有乐趣,相比起用笔写作没有体验感,没有那种实在的感觉,也...
    儒逸馨阅读 285评论 0 1
  • 一 方法的实现 二 使用
    青椒辣不辣阅读 190评论 0 0
  • 时光荏苒,岁月如梭,蓦然回首中,不免惊叹日子就在我们或不知不觉,或患得患失中溜走......细数之下,我在目前的公...
    品希公主阅读 868评论 14 2