# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
670846 | supdude | Knapsack (NOI18_knapsack) | C++17 | 1094 ms | 340 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;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
int nxt(){int x; cin >> x;return x;}
string snxt(){string x; cin >> x;return x;}
char cnxt(){char x; cin >> x;return x;}
void setIO(){
#ifndef ONLINE_JUDGE
freopen("feast.out", "w", stdout);
freopen("feast.in", "r", stdin);
#endif
}
void solve() {
ll s,n;
cin >> s >> n;
vector<vector<ll>> dp(n+1, vector<ll>(s+1));
for (ll i = 1; i <= n; i++){
ll v,w,k;
cin >> v >> w >> k;
vector<ll> prev(dp[i-1]);
while(k--){
for (ll j = 1; j <= s; j++){
dp[i][j] = max(dp[i][j], prev[i]);
//we use item here
if (j - w >= 0) {
dp[i][j] = max(dp[i][j], prev[j-w] + v);
}
//we don't use item here
}
prev = dp[i];
}
/*
for (int k = 0; k <= n; k++){
for (int j = 0; j <= s; j++){
cout << dp[k][j] << " \n"[j == s];
}
}
cout << endl;
*/
}
cout << dp[n][s] << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
// setIO();
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++){
//cout << "Case #" << i << ": ";
solve();
}
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... |