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;
const int MAXS = 2000;
const int MAXITEM = 20000;
const int INF = 2e9;
int S, N;
vector<pair<int, int> > A[MAXS + 5];
vector<pair<int, int> > items;
int DP[MAXITEM + 5][MAXS + 5];
int f(int idx, int cap) {
if (cap < 0) return -INF;
if (idx == items.size()) return 0;
int &ret = DP[idx][cap];
if (ret != -1) return ret;
auto [w, v] = items[idx];
return ret = max(f(idx + 1, cap), f(idx + 1, cap - w) + v);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> S >> N;
for (int i = 0; i < N; i++) {
int v, w, k;
cin >> v >> w >> k;
A[w].push_back({v, k});
}
for (int w = 1; w <= MAXS; w++) {
sort(A[w].begin(), A[w].end());
int used = MAXS / w + 1;
while (used != 0 && !A[w].empty()) {
auto &[v, k] = A[w].back();
items.push_back({w, v});
k--;
used--;
if (k == 0) A[w].pop_back();
}
}
memset(DP, -1, sizeof DP);
cout << f(0, S) << endl;
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int f(int, int)':
knapsack.cpp:15:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if (idx == items.size()) return 0;
| ~~~~^~~~~~~~~~~~~~~
# | 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... |