将背包九讲往后看了看,学习了一下分组背包。来做几道入门题,试试手。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <numeric>
#include <limits.h>
#include <set>
using namespace std; int dp[], value[][]; int main () {
ios :: sync_with_stdio (false);
int m, n;
while (cin >> m >> n) {
if (m == && n == ) break;
memset (value, , sizeof(value));
memset (dp, , sizeof(dp));
for (int i = ; i <= m; ++ i) {
for (int j = ; j <= n; ++ j) {
cin >> value[i][j];
}
}
for (int i = ; i <= m; ++ i) {
for (int j = n; j >= ; j --) {
for (int k = ; k <= j; ++ k) {
dp[j] = max(dp[j], dp[j - k] + value[i][k]);
}
}
}
cout << dp[n] << endl;
}
return ;
}