# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1003122 | fryingduc | Knapsack (NOI18_knapsack) | C++17 | 1066 ms | 17584 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.
// #pragma GCC optimize("Ofast,unroll-loops")
#include "bits/stdc++.h"
using namespace std;
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define yes cout << "YES\n"
#define no cout << "NO\n"
#ifdef LOCAL
#define debug(val) cerr << "["#val << " = " << (val) << "] "
#define slash() cerr << "\n-----------\n"
#define time() cerr << "Time is: " << 1000 * clock()/CLOCKS_PER_SEC << " ms\n"
#else
#define debug(val) 1407
#define slash() 1407
#define time() 1407
#endif
const string filename = "";
void IO(){
ios_base::sync_with_stdio(0);
cin.tie(0);
if(filename.size()){
string in = filename + ".inp";
string out = filename + ".out";
freopen(in.c_str(), "r", stdin);
freopen(out.c_str(), "w", stdout);
}
}
int n, W;
vector<long long> v, w;
void solve(){
cin >> W >> n;
for(int i=0;i<n;i++){
int a, b, c; cin >> b >> a >> c;
int bits = 1;
c = min(c, W);
while(c - bits >= 0){
if(1ll * a * bits <= W) {
v.push_back(1ll * b * bits);
w.push_back(1ll * a * bits);
}
c -= bits;
bits *= 2;
}
if(c){
if(1ll * c * a <= W) {
v.push_back(1ll * c * b);
w.push_back(1ll * c * a);
}
}
}
vector<long long> dp(W+1);
for(int i=0;i< (int)v.size();i++){
for(int j=W;j>=w[i];j--){
dp[j] = max(dp[j], dp[j-w[i]] + v[i]);
}
}
cout << dp[W];
}
signed main(){
IO();
int test = 1;
/* cin >> test; */
for(int i=1;i<=test;i++){
// cout << "Case " << "#" << i << ": ";
solve();
}
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... |