This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
const int inf = 1e9;
int s, n;
int dp[2001][2001];
vector<vector<pair<int, int>>> arr(2001);
int sol(int idx, int weight) {
// base case
if (weight < 0) return -inf;
if (idx == s+1) return 0;
if (dp[idx][weight] != -1) return dp[idx][weight];
int ans = 0;
// not include
ans = max(ans, sol(idx+1, weight));
// include
int currVal = 0, currwt = 0;
for (int i = 0; i < arr[idx].size(); i++) {
int count = arr[idx][i].s;
bool f = true;
while (count > 0) {
currVal += arr[idx][i].f;
currwt += idx;
if (currwt > weight) {
break;
f = false;
}
count--;
ans = max(ans, currVal + sol(idx+1, weight - currwt));
}
if (!f) break;
}
return ans;
}
int main () {
cin >> s >> n;
for (int i = 0; i < n; i++) {
int v, w, k; cin >> v >> w >> k;
if (w > s || k <= 0) continue;
arr[w].push_back({v, k});
}
for (int i = 0; i <= 2000; i++) sort (arr[i].begin(), arr[i].end(), greater<>());
memset(dp, -1, sizeof dp);
int ans = sol(1, s);
cout << ans << endl;
}
Compilation message (stderr)
knapsack.cpp: In function 'int sol(int, int)':
knapsack.cpp:23:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 0; i < arr[idx].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |