poj 3624 (背包入门)


入门题,最近又一次想不起来背包的dp方法,无奈又得找道水题找找感觉...

Charm Bracelet
Time Limit:1000MS Memory Limit:65536K
Total Submissions:15063 Accepted:6880

Description

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 theN(1 ≤N≤ 3,402) available charms. Each charmiin the supplied list has a weightWi(1 ≤Wi≤ 400), a 'desirability' factorDi(1 ≤Di≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more thanM(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:NandM
* Lines 2..N+1: Linei+1 describes charmiwith two space-separated integers:WiandDi

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

Source

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define N 13000
int dp[N];

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

优质内容筛选与推荐>>
1、sql.date
2、asp微信扫一扫代码,用asp写的实现调用微信扫一扫功能
3、redis 学习(15)-- GEO
4、Linux系统文件的三个重要时间详解
5、js基础对象属性和方法


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号