#include<iostream> using namespace std; const int N = 1010; int f[N][N], w[N], v[N]; int n, m; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> w[i] >> v[i]; for (int i = 1; i <= n; ++i) { for (int j = 0; j <= m; ++j) { f[i][j] = f[i-1][j]; if (j >= w[i]) f[i][j] = max(f[i][j], f[i-1][j-w[i]]+v[i]); } } cout << f[n][m] << endl; return 0; } //一维优化 #include<iostream> using namespace std; const int N = 1010; int f[N], w[N], v[N]; int n, m; int main() { cin >> n >> m; for (int i = 1; i <= n; ++i) cin >> w[i] >> v[i]; for (int i = 1; i <= n; ++i) { //和完全背包问题不同, 需要从大到小 for (int j = m; j >= w[i]; --j) { f[j] = max(f[j], f[j-w[i]]+v[i]); } } cout << f[m] << endl; return 0; }