# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1036800 | ocasu | Knapsack (NOI18_knapsack) | C++17 | 1073 ms | 1468 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 int long long
const int inf=1e12;
signed main(){
int S,n; cin>>S>>n;
vector<multiset<int>> items(S+1);
for (int i=0; i<n; i++){
int v,w,k; cin>>v>>w>>k;
k=min(k, S/w+1);
while (k>0 and (items[w].size()<=(S/w) or *(items[w].begin()) <= v)){
items[w].insert(v);
if ((int)(items[w].size()) > S/w) items[w].erase(items[w].begin());
k--;
}
}
vector<int> dp(S+1,-inf); //current maximum value of a subset with total weight w
dp[0]=0;
for (int w=0; w<=S; w++){
for (auto v: items[w]){
for (int i=S; i>=w; i--){
dp[i]=max(dp[i], dp[i-w]+v);
}
}
}
int ans=0;
for (auto i: dp) ans=max(ans, i);
cout<<ans<<'\n';
}
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... |