You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
785 B

#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int w[N], v[N], n, m, f[N][N]; //引入dp数组保存已经计算过的结果
int dfs(int i, int j) {
//判断这种情况是否被计算过,若被计算过就可以直接返回结果不用进行下面的一系列判断计算
if (f[i][j] >= 0) return f[i][j];
int res = 0;
if (i == n + 1) return res;
if (w[i] > j) res = dfs(i + 1, j);
else res = max(dfs(i + 1, j), dfs(i + 1, j - w[i]) + v[i]);
f[i][j] = res; //保存求出结果
return res;
}
int main() {
//初始化数组元素为-1
memset(f, -1, sizeof(f));
cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> w[i] >> v[i];
cout << dfs(0, m) << endl;
return 0;
}