# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
886380 | Zero_OP | Knapsack (NOI18_knapsack) | C++14 | 220 ms | 3920 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;
vector<vector<pair<int, int>>> block(S + 1);
while(n--){
int v, w, k;
cin >> v >> w >> k;
block[w].push_back({v, k});
}
vector<ll> dp(S + 1, -1e18);
dp[0] = 0;
for(int i = 1; i <= S; ++i){
if(block[i].empty()) continue;
sort(range(block[i]), greater<pair<int, int>>());
for(int w = S; w >= 0; --w){
int curW = 0, curV = 0;
for(int j = 0; j < block[i].size(); ++j){
for(int k = 1; k <= block[i][j].second and curW <= S; ++k){
curW += i;
curV += block[i][j].first;
if(w >= curW){
maximize(dp[w], dp[w - curW] + curV);
}
}
}
}
}
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... |