# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
642464 | burak_ozzkan | Knapsack (NOI18_knapsack) | C++14 | 180 ms | 248252 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 nl '\n'
#define int long long
using namespace std;
struct item {
int value;
int piece;
};
void solve(){
int s, n;
cin >> s >> n;
vector< vector<item> > a(2005);
for(int i = 0, w; i < n; i++){
item tmp;
cin >> tmp.value >> w >> tmp.piece;
a[w].push_back(tmp);
}
vector<int> weights;
vector<int> values;
for(int i = 1; i < 2005; i++){
sort(a[i].begin(), a[i].end(), [&](item a, item b){ return a.value > b.value; });
int currWeight = 0;
bool flag = 0;
for(auto x : a[i]){
if(flag) break;
for(int j = 0; j < x.piece; j++){
if(currWeight+i > s) { flag = 1; break; }
currWeight += i;
weights.push_back(i);
values.push_back(x.value);
}
}
}
vector< vector<int> > dp(values.size()+1, vector<int>(s+1, 0));
for(int i = 1; i < values.size()+1; i++){
for(int j = 1; j < s+1; j++){
dp[i][j] = dp[i-1][j];
if(j-weights[i-1] >= 0) dp[i][j] = max(dp[i][j], values[i-1]+dp[i-1][j-weights[i-1]]);
}
}
cout << dp[values.size()][s] << nl;
}
int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1; /*cin >> t;*/ while(t--) solve();
}
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... |