# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
541267 | gromperen | Knapsack (NOI18_knapsack) | C++14 | 57 ms | 5008 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 ll long long
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int s, n; cin >> s >> n;
vector<vector<pair<int,int>>> obj(s+1);
vector<int> v(n), w(n), k(n);
for (int i = 0; i < n; ++i) {
cin >> v[i] >> w[i] >> k[i];
obj[w[i]].push_back({v[i], k[i]});
}
vector<ll> dp(s+1);
for (int i =0; i <=s; ++i) {
if (!obj[i].size()) continue;
sort(obj[i].begin(), obj[i].end(), greater<pair<int,int>>());
int cur_used = 0;
for (int j = 0; j * i <= s && cur_used < obj[i].size(); ++j) {
for (int r = s; r >= i; --r) {
dp[r] = max(dp[r], dp[r-i] + obj[i][cur_used].first);
}
obj[i][cur_used].second--;
if (obj[i][cur_used].second == 0) cur_used++;
}
}
cout << dp[s] << "\n";
/*
for (int i = 0; i< n; ++i) {
for (int j = s; j >= w[i]; --j) {
for (int t = 0; t <= k[i]; ++t) {
if (j >= w[i] * t) {
dp[j] = max(dp[j], dp[j - w[i] * t] + v[i] * t);
}
}
}
}
cout << dp[s] << "\n";
*/
return 0;
}
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... |