# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
906424 | SalihSahin | Knapsack (NOI18_knapsack) | C++17 | 200 ms | 262144 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 pb push_back
#define mp make_pair
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e17*2;
const int N = 2e5 + 5;
int32_t main(){
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
int s, n;
cin>>s>>n;
vector<int> items[s+1];
for(int i = 0; i < n; i++){
int v, w, k;
cin>>v>>w>>k;
for(int j = 1; j <= min(k, s/w); j++){
items[w].pb(v);
}
/*
vector<int> cnt(11);
cnt[0] = min(k, 2048LL);
for(int i = 0; i < 10; i++){
int x = (cnt[i] - 1)/2;
cnt[i] -= x*2;
cnt[i+1] += x;
}
for(int i = 0; i < 10; i++){
if((1 << i) * w > s) break;
for(int j = 0; j < cnt[i]; j++){
items[w * (1 << i)].pb(v * (1 << i));
}
}
*/
}
vector<pair<int, int> > arr;
for(int i = 1; i <= s; i++){
sort(items[i].rbegin(), items[i].rend());
while(items[i].size() * i > s){
items[i].pop_back();
}
for(auto it: items[i]) arr.pb(mp(i, it));
}
vector<int> dp(s+1);
for(int i = 0; i < arr.size(); i++){
for(int j = s - arr[i].first; j >= 0; j--){
dp[j + arr[i].first] = max(dp[j + arr[i].first], dp[j] + arr[i].second);
}
}
int ans = 0;
for(int i = 0; i <= s; i++){
ans = max(ans, dp[i]);
}
cout<<ans<<endl;
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... |