# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
973265 | SeenSiravit | Knapsack (NOI18_knapsack) | C++14 | 54 ms | 3920 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 ll long long
using namespace std;
const int mxN = 1e5 + 5 , mxS = 2e3 + 5;
int n,s;
vector<pair<int,int>> bag[mxS];
ll dp[mxS];
int main(){
scanf("%d %d",&s,&n);
for(int i=1;i<=n;i++){
int v,w,k;
scanf("%d %d %d",&v,&w,&k);
bag[w].push_back({v , min(k , s/w)});
}
for(int w=1;w<=s;w++) sort(bag[w].begin() , bag[w].end() , greater<pair<int,int>>());
// for(int w=1;w<=s;w++){
// if(bag[w].empty()) continue;
// printf("w = %d : " , w);
// for(auto [v , k] : bag[w]) printf("%d %d , " , v , k);
// printf("\n");
// }
for(int w=1;w<=s;w++){
if(bag[w].empty()) continue;
int lim_k = s / w;
int curr = bag[w][0].second , idx = 0;
for(int k=1;k<=lim_k;k++){
for(int cap=s;cap>=w;cap--) dp[cap] = max(dp[cap] , dp[cap - w] + bag[w][idx].first);
if(k >= curr){
idx++;
curr += bag[w][idx].second;
if(idx>= int(bag[w].size())) break;
}
}
}
printf("%lld" , dp[s]);
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... |