Untitled
unknown
plain_text
a year ago
1.5 kB
3
Indexable
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
const double PI = 3.14159265358979;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
const ll nax = 100005;
const int LOG = 25;
class MaxValueKnapsackSolver {
int n, w;
vector<int> weight, value;
vector<vector<int> > dp;
int calculateMaxValue(int item, int remainingWeight) {
if (item > n) {
return 0;
}
if (remainingWeight <= 0) {
return 0;
}
if (dp[item][remainingWeight] != -1) {
return dp[item][remainingWeight];
}
int ans = calculateMaxValue(item + 1, remainingWeight);
if (remainingWeight >= weight[item]) {
ans = max(ans, value[item] + calculateMaxValue(item + 1, remainingWeight - weight[item]));
}
return dp[item][remainingWeight] = ans;
}
public:
void solve() {
cin >> n >> w;
weight.resize(n + 1);
value.resize(n + 1);
dp.assign(n + 1, vector<int> (w + 1, -1));
for (int i = 1; i <= n; i++) {
cin >> weight[i] >> value[i];
}
cout << calculateMaxValue(1, w) << endl;
}
};
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t = 1;
// cin >> t;
while(t--) {
MaxValueKnapsackSolver maxValueKnapsackSolver;
maxValueKnapsackSolver.solve();
}
return 0;
}Editor is loading...
Leave a Comment