5709 01背包
时间限制: 1 s
空间限制: 128000 KB
题目等级 : 黄金 Gold
题目描述 Description
cjw很奇怪,他喜欢吃巧克力,tr送给他一个包,包容量是S,cjw可以自己去巧克力店里取巧克力,已知有n块巧克力,每块有一个重量W[i]和一个使cjw快乐的价值K[i],请写一个程序求出cjw能得到的最大总快乐价值。
输入描述 Input Description
输入文件第一行包含一个正整数S,表示包重S。( 0<S<=5000 )
第二行包含N个正整数,表示S中元素个数。(0<n<=1000)
第3~3+N-1行包含N个正整数W[i],K[i]。( 0<W[i],K[i]<=100000)
输出描述 Output Description
输出文件一行只包含一个正整数X,表示最大快乐价值。
样例输入 Sample Input
5
3
1 5
2 3
3 4
样例输出 Sample Output
9
数据范围及提示 Data Size & Hint
无
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 5 const int N = 1010 ; 6 int f[N][N]; 7 int mo[N],zh[N]; 8 int s,n,m,k; 9 10 int main() 11 { 12 scanf("%d",&s); 13 scanf("%d",&n); 14 for(int i=1;i<=n;++i) 15 { 16 scanf("%d%d",&zh[i],&mo[i]); 17 } 18 for(int i=1;i<=n;++i) 19 { 20 for(int j=s;j>=1;--j) 21 { 22 if(j-zh[i] >= 0) f[i][j] = max(f[i-1][j], f[i-1][j-zh[i]] + mo[i]); 23 else f[i][j] = f[i-1][j]; 24 } 25 } 26 printf("%d",f[n][s]); 27 return 0; 28 }