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

C++版本一

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>using namespace std;
int n,m;
struct node{int d,w;}b[4000];
bool cmp(node a,node b){if(a.w==b.w)return a.d>a.d;return a.w>b.w;}
int dp[13005];
int main()
{scanf("%d%d",&n,&m);for(int i=0;i<n;i++){scanf("%d%d",&b[i].w,&b[i].d);}memset(dp,0,sizeof(dp));sort(b,b+n,cmp);for(int i=0;i<n;i++){//dp[i]=b[i].d;for(int j=m;j>=b[i].w;j--){dp[j]=max(dp[j],dp[j-b[i].w]+b[i].d);}}cout << dp[m] << endl;//cout << "Hello world!" << endl;return 0;
}

C++版本二

///2014.4.10
///poj3624#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;int N,M;
int w[3500],c[3500];
int f[13500];int main()
{// freopen("in","r",stdin);// freopen("out","w",stdout);cin>>N>>M;for(int i=1 ; i<=N ; i++){cin>>c[i]>>w[i];}memset(f,0,sizeof(f) );for(int i=1 ; i<=N ; i++){for(int j=M ; j>=1 ; j--){int a;if( j-c[i]>=0 )a = f[j-c[i]]+w[i];elsea = 0;f[j] = f[j]>a? f[j]:a;}}cout<<f[M]<<endl;return 0;
}

C++版本三

#include <iostream>
using namespace std;  const int MAX_N = 3405;
const int MAX_M = 12885;  int dp[MAX_M];
int w[MAX_N];
int d[MAX_N];  int max(int a, int b) {  return a > b ? a : b;
}  int main()
{  int n, m;  cin >> n >> m;  for(int i = 1; i <= n; i++) {  cin >> w >> d;  }  for(int i = 1; i <= n; i++) {  for(int j = m; j >= w; j--) {  dp[j] = max(dp[j-w] + d, dp[j]);  }  }  cout << dp[m] << endl;  return 0;
}

Charm Bracelet相关推荐

  1. AC日记——[USACO07DEC]手链Charm Bracelet 洛谷 P2871

    题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like t ...

  2. Charm Bracelet(0-1)

    Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to fil ...

  3. 洛谷——P2871 [USACO07DEC]手链Charm Bracelet

    https://www.luogu.org/problem/show?pid=2871 题目描述 Bessie has gone to the mall's jewelry store and spi ...

  4. 【POJ - 3624 】Charm Bracelet (dp,0-1背包裸题)

    题干: Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like to ...

  5. Charm Bracelet(POJ-3624)

    Problem Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of cours ...

  6. 百练 01 Charm Bracelet

    百练 01 Charm Bracelet 总时间限制: 内存限制: 1000ms 65536kB 描述 Bessie has gone to the mall's jewelry store and ...

  7. 背包问题——01背包问题——Charm Bracelet

    Charm Bracelet Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, sh ...

  8. POJ-3426-0-1背包Charm Bracelet

    Charm Bracelet Description Bessie has gone to the mall's jewelry store and spies a charm bracelet. O ...

  9. 【Charm Bracelet】题目解析~~

    题目: [Charm Bracelet] 有n个物品,编号为i的物品的重量为w[i],价值为c[i],现在要从这些物品中选一些物品装到一个容量为m的背包中,使得背包内物体在总重量不超过m的前提下价值尽 ...

最新文章

  1. 【转】结合ashx来在DataGrid中显示从数据库中读出的图片
  2. Python游戏开发,pygame模块,Python实现愤怒的小鸟【附带源码】
  3. 不关闭seLinux解决vsftpd服务本地用户不能登录问题(500 OOPS: cannot change directory:/home/***
  4. Qt::AutoConnection 信号从不同于接收者的thread发出时是queued 方式触发
  5. 百度网盘API调用二
  6. 关于语音发送/接收 方法和装置发明专利正式发布
  7. Android之popWindows底部弹出挡住了华为虚拟键盘问题
  8. 关于图片预加载的思考
  9. java list 获取索引_java – 获取arrayList中元素的索引
  10. 如果NATv6 是个笑话,那么 IPv6 是什么?
  11. 图画日记怎么画_期末到了,孩子不会复习,可以试试画思维导图的方法
  12. MySql修改默认端口
  13. iOS性能优化之Leaks动态分析
  14. 企业微信有定位跟踪吗?企业微信会跟踪员工轨迹记录吗?
  15. 2022年茶艺师(中级)考试题模拟考试题库模拟考试平台操作
  16. NDK/C++ 耗时统计类TimeUtils
  17. 70年代生人的80年代
  18. vs2017+ivf+abaqus
  19. office 2016安装包下载
  20. 财务数据分析工具的选择:Excel还是大数据BI?

热门文章

  1. html社交模板,Olympus – 社交网HTML静态模板
  2. mysql数据库5.7配置文件_MySQL 5.7配置文件参考
  3. ajax form表单提交_LayUI提交表单,监听select,分页组件
  4. graphpad如何做x轴在上方的图_R-ggridges包:山峰叠峦图
  5. android SharedPreferences设置初始密码,并修改密码
  6. 【机器学习算法专题(蓄力计划)】二十、实操代码MNIST 数据集
  7. 一、学爬虫前,你需要知道的爬虫常识
  8. 启动hive报错:java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang
  9. 问题生成的多样性会在多大程度上帮助下游QA任务?
  10. ACL 2020 | MobileBERT:一种与任务无关的模型压缩方法