# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
967243 | SmuggingSpun | Knapsack (NOI18_knapsack) | C++14 | 44 ms | 4180 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>
#define taskname "knapsack"
using namespace std;
template<class T>void maximize(T& a, T b){
if(a < b){
a = b;
}
}
const int lim = 2e3 + 1;
int cnt[lim], dp[lim];
vector<int>a[lim];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
memset(cnt, 0, sizeof(cnt));
int S, n;
cin >> S >> n;
vector<int>v(n), w(n), k(n);
for(int i = 0; i < n; i++){
cin >> v[i] >> w[i] >> k[i];
}
vector<int>p(n);
iota(p.begin(), p.end(), 0);
sort(p.begin(), p.end(), [&] (int i, int j) -> bool{
return v[i] > v[j];
});
for(int& i : p){
int max_size = S / w[i];
while(a[w[i]].size() < max_size && k[i] > 0){
a[w[i]].emplace_back(v[i]);
k[i]--;
}
}
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= S; i++){
for(int j = S; j >= i; j--){
for(int k = 0, value = 0; k < a[i].size() && i * (k + 1) <= j; k++){
maximize(dp[j], dp[j - i * (k + 1)] + (value += a[i][k]));
}
}
}
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... |