poj2634 简单01背包

/*

一直没复习;感觉以前学的忘光了

*/


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <iomanip>
#define maxn 3500
int cost[maxn],worth[maxn];
int dp[12885];
using namespace std;
int max(int a, int b)
{
	return a>b?a:b;
}
void ZeroOnePack(int cost, int worth, int V)
{
	for(int j = V; j >= cost; j--)
		dp[j] = max(dp[j-cost]+worth,dp[j]);
}
int main(int argc, char *argv[])
{
	int N,M;
	while(scanf("%d%d",&N,&M)!=EOF)
	{
		memset(dp,0,sizeof(dp));
		for(int i = 0; i < N; i++)
			scanf("%d%d",&cost[i],&worth[i]);
		for(int i = 0; i < N; i++)
			ZeroOnePack(cost[i],worth[i],M);
		printf("%d\n",dp[M]);
	}
	return 0;
}


posted @ 2013-04-14 16:36  简洁是智慧的灵魂  阅读(144)  评论(0)    收藏  举报