# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
465056 | Qw3rTy | Knapsack (NOI18_knapsack) | C++11 | 141 ms | 9944 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 int long long
#define pi pair<int,int>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int maxN = 1e5+5;
const int maxV = 2e3+5;
vector<int> d[maxV];
int S,N;
int f[maxV];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("../test.in","r",stdin);
cin >> S >> N;
for(int i = 1; i <= N; ++i){
int v,w,k; cin >> v >> w >> k;
for(int j = 1; j <= k; j <<= 1){
if(w * j > S)break;
k -= j;
d[w*j].pb(v*j);
}
if(k > 0 && w * k <= S)d[w*k].pb(v*k);
}
for(int i = 1; i <= S; ++i){
if(d[i].size()){ //for each group of objects w/ weight = i
sort(d[i].begin(),d[i].end(),greater<int>());
//Consider only the first floor(S/W) objects
int num = min(S/i,(int)(d[i].size()));
assert(d[i].size() >= num);
for(int j = 0; j < num; ++j){
for(int k = S; k >= i; --k){
f[k] = max(f[k],f[k-i] + d[i][j]);
}
}
}
}
cout << f[S] << '\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... |