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;
using ll = long long;
using vi = vector<int>;
#define pb push_back
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
using pi = pair<int,int>;
#define f first
#define s second
#define mp make_pair
int main() {
int maxWeight, N;
cin >> maxWeight >> N;
vector<vector<pi>> items(maxWeight + 1, vector<pi>());
for (int curW = 0; curW < N; curW++) {
int V, W, K;
cin >> V >> W >> K;
items[W].pb({ V, K });
}
for (int curW = 0; curW <= maxWeight; curW++) {
sort(all(items[curW]), greater<pi>());
}
cout << "disenchanted\n";
int maxItems = 0;
for (int curW = 1; curW <= maxWeight; curW++) {
maxItems += maxWeight / curW;
}
vector<vi> dp(maxItems + 1, vi(maxWeight + 1, 0));
int numItems = 1;
for (int curW = 1; curW <= maxWeight; curW++) {
if (items[curW].size() == 0) {
continue;
}
int idx = 0;
int copies = items[curW][0].second;
for (int amt = 0; amt < maxWeight / curW; amt++) {
for (int cur = 1; cur <= maxWeight; cur++) {
dp[numItems][cur] = dp[numItems - 1][cur];
if (cur - curW >= 0) {
dp[numItems][cur] = max(dp[numItems][cur], dp[numItems - 1][cur - curW] + items[curW][idx].f);
}
}
numItems++;
copies--;
if (copies == 0) {
idx++;
if (idx >= items[curW].size()) {
break;
}
copies = items[curW][idx].s;
}
}
}
cout << dp[numItems - 1][maxWeight] << "\n";
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:54: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]
54 | if (idx >= items[curW].size()) {
| ~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |