# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
484682 | Ncode | Knapsack (NOI18_knapsack) | C++17 | 324 ms | 262144 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;
long long dp[10005][10005];
const long long inf = 1e9+5;
int main(){
long long S, n;
cin>>S>>n;
long long v, w, k;
vector<pair<long long, long long>> a;
for(long long i=0;i<n;i++){
cin>>v>>w>>k;
while(k--) a.push_back({v, w});
}
// for(long long i=0;i<n;i++){
// for(long long j=0;j<=S;j++){
// dp[i][j] = inf;
// }
// }
dp[0][0] = 0;
for(long long i=0;i<a.size();i++){
for(long long sum = S; sum >= 0; sum --){
dp[i][sum] = dp[i-1][sum];
if(sum >= a[i].second && dp[i-1][sum - a[i].second] + a[i].first > dp[i][sum]){
dp[i][sum] = dp[i-1][sum - a[i].second] + a[i].first;
}
}
}
// for(long long i=0;i<n;i++){
// for(long long j = 0; j<=S; j++){
// cout<<dp[i][j]<<" ";
// }cout<<endl;
// }
cout<<dp[a.size() - 1][S]<<'\n';
}
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... |