# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
813094 | AliVu | Knapsack (NOI18_knapsack) | C++17 | 1084 ms | 17784 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 int long long
#define el '\n'
const int maxn = 2e3 + 3, N = 1e5 + 5;
int dp[2][maxn];
vector<pair<int, int>> a;
void Solve() {
int S, n; cin >> S >> n;
for(int i = 1; i <= n; i++) {
int v, w, k; cin >> v >> w >> k;
int p = 1;
while(p < k) {
a.push_back({v * p, w * p});
k -= p;
p <<= 1;
}
if(k) a.push_back({v * k, w * k});
}
for(int i = 0; i < 2; i++) for(int j = 0; j < maxn; j++) {
dp[i][j] = -1;
if(j == 0) dp[i][j] = 0;
}
for(int k = 0; k < a.size(); k++) {
int i = k % 2;
for(int j = 0; j < maxn; j++) {
if(dp[i][j] != -1 && j + a[k].second < maxn)
dp[1 - i][j + a[k].second] = max(dp[1 - i][j + a[k].second], dp[i][j] + a[k].first);
dp[1 - i][j] = max(dp[1 - i][j], dp[i][j]);
}
}
int ans = 0;
for(int i = 1; i <= S; i++) {
ans = max({ans, dp[0][i], dp[1][i]});
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);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... |