# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
551913 | training4usaco | Knapsack (NOI18_knapsack) | C++11 | 1 ms | 352 KiB |
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 <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#define MAXV 1000005
#define MAXN 100005
#define MAXS 2005
#define INF 1000000009
long long s, n;
long long v[MAXN], w[MAXN], k[MAXN];
long long dp[MAXS];
vector<pair<long long, long long>> obj[MAXS];
int main() {
cin >> s >> n;
for (int i = 1; i <= n; ++i) {
cin >> v[i] >> w[i] >> k[i];
}
long long mx = -INF;
for(int i = 1; i <= n; ++i) {
mx = max(mx, k[i]);
}
for (int i = 1; i <= n; i++) {
obj[w[i]].push_back({v[i], k[i]});
}
for (int i = 0; i <= s; i++) {
if (obj[i].size() == 0) {
continue;
}
sort(obj[i].begin(), obj[i].end());
int id = 0;
for (int j = 0; j * i < s; j++) {
if (id >= obj[i].size()) {
break;
}
for (int k = s; k >= i; k--) {
dp[k] = max(dp[k], dp[k - i] + obj[i][id].first);
}
--obj[i][id].second;
if (obj[i][id].second == 0) {
++id;
}
}
}
cout << dp[s] << endl;
}
Compilation message (stderr)
# | 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... |