# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
904135 | SalihSahin | Knapsack (NOI18_knapsack) | C++14 | 1 ms | 348 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;
k = min(k, 2047LL);
for(int j = 0; j <= 10; j++){
if(w * (1 << j) > s) continue;
if(k >= (1 << j)){
k -= (1 << j);
items[w * (1 << j)].pb(v * (1 << j));
}
}
for(int j = 0; j <= 10; j++){
if(w * (1 << j) > s) continue;
if(k >= (1 << j)){
k -= (1 << j);
items[w * (1 << j)].pb(v * (1 << j));
}
}
}
vector<pair<int, int> > arr;
for(int i = 1; i <= s; i++){
sort(items[i].begin(), items[i].end());
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... |