# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
776956 | qenneth | Knapsack (NOI18_knapsack) | C++17 | 250 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.
#pragma GCC optimize("O3")
#pragma GCC target("popcnt")
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false);cin.tie(NULL)
#define all(x) x.begin(), x.end()
#define chmax(x, a) x = max(x, a);
#define chmin(x, a) x = min(x, a);
// usaco
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int MOD = 1e9 + 7;
int main() {
FASTIO;
// setIO("snakes");
int s, n;
cin >> s >> n;
vector<vector<int>> a(n, vector<int>(3, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
cin >> a[i][j];
}
}
vector<vector<long long>> dp(n + 1, vector<long long> (s + 1));
for (int i = 0; i < n; i++) {
for (int j = 0; j <= s; j++) {
// if (dp[i][j] == -1) continue;
dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
for (int k = 0; k <= min(s, a[i][2]); k++) {
if (j + k * a[i][1] <= s) {
dp[i + 1][j + k * a[i][1]] = max(dp[i + 1][j + k * a[i][1]], dp[i][j] + 1ll * k * a[i][0]);
}
}
}
}
cout << dp[n][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... |