# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
886376 | Zero_OP | Knapsack (NOI18_knapsack) | C++14 | 1032 ms | 18632 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>
using namespace std;
#define range(v) begin(v), end(v)
#define compact(v) v.erase(unique(range(v)), end(v))
template<class T> bool minimize(T& a, T b){
if(a > b) return a = b, true;
return false;
}
template<class T> bool maximize(T& a, T b){
if(a < b) return a = b, true;
return false;
}
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
int S, n;
void Zero_OP(){
cin >> S >> n;
vector<pair<ll, int>> items;
while(n--){
int v, w, k;
cin >> v >> w >> k;
k = min(k, S / w);
int pw2 = 1;
while(k >= pw2){
k -= pw2;
if(1LL * w * pw2 <= 1LL * S){
items.push_back({1LL * v * pw2, w * pw2});
}
pw2 <<= 1;
}
if(k > 0 and 1LL * w * k <= S){
items.push_back({1LL * v * k, w * k});
}
}
vector<ll> dp(S + 1, -1e18);
dp[0] = 0;
for(auto [earn, weight] : items){
for(int i = S; i >= weight; --i){
maximize(dp[i], dp[i - weight] + earn);
}
}
cout << *max_element(range(dp));
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
#define task "antuvu"
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
Zero_OP();
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... |