# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
652358 | chanhchuong123 | Knapsack (NOI18_knapsack) | C++14 | 65 ms | 1812 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 <bits/stdc++.h>
using namespace std;
#define task "C"
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int S = 2020;
int s, n;
long long dp[S];
vector<pair<int, int>> a;
vector<pair<int, int>> occ[S];
main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen(task".inp","r")) {
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
cin >> s >> n;
for (int i = 1; i <= n; i++) {
int V, W, K; cin >> V >> W >> K;
occ[W].push_back({V, K});
}
for (int w = 1; w <= s; w++) {
sort(all(occ[w]));
int lim = s / w;
while (lim > 0 && occ[w].size()) {
if (!occ[w].back().se) occ[w].pop_back();
else {
occ[w].back().se--; lim--;
a.push_back({occ[w].back().fi, w});
}
}
}
n = a.size();
for (int i = 0; i < n; i++) {
for (int j = s; j >= 0; j--) {
if (j + a[i].se > s) continue;
maxi(dp[j + a[i].se], dp[j] + a[i].fi);
}
}
cout << dp[s];
}
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... |