# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
914013 | dpsaveslives | Knapsack (NOI18_knapsack) | C++17 | 45 ms | 2080 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 f first
#define s second
using namespace std;
const int MAX = 2010;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int S,N; cin >> S >> N;
vector<pair<int,int>> mp[MAX];
for(int i = 0;i<N;++i){
int v,w,k; cin >> v >> w >> k;
mp[w].push_back({v,min(k,S/w)});
}
vector<int> dp(S+1,0);
for(int i = 1;i<=S;++i){
vector<pair<int,int>> vec = mp[i];
sort(vec.rbegin(),vec.rend());
int cnt = 0;
for(int j = 0;j<vec.size();++j){
int v = vec[j].f, k = vec[j].s;
while(k--){
for(int l = S;l>=i;l--){
dp[l] = max(dp[l],dp[l-i]+v);
}
++cnt;
if(cnt*i >= S) break;
}
if(cnt*i >= S) break;
}
}
cout << dp[S] << "\n";
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... |