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 all(dataStructure) dataStructure.begin(),dataStructure.end()
#define ll long long
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
// _vector <int, 3> a(n, m, k);: int a[n][m][k].
// _vector <int, 3> a(n, m, k, x);: int a[n][m][k] initialized with x.
template <typename T>
struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
}
const int MAX = 2e3 + 3;
const ll MOD[] = {1000000007, 998244353};
int S, n;
ll f[MAX][MAX];
ll dp[MAX][MAX];
vector <pair <int, int>> items[MAX];
void Solve() {
cin >> S >> n;
for (int i = 0; i < n; i++) {
int v, w, k;
cin >> v >> w >> k;
items[w].emplace_back(v, k);
}
for (int i = 1; i <= S; i++) {
int pos = 1;
sort(all(items[i]));
reverse(all(items[i]));
for (auto &&[v, k] : items[i]) {
f[i][min(pos + k - 1, S)] = v;
pos += k;
if (pos > S) break;
}
for (int j = S - 1; j >= 1; j--) f[i][j] = max(f[i][j], f[i][j + 1]);
for (int j = 1; j <= S; j++) f[i][j] += f[i][j - 1];
for (int j = 1; j <= S; j++) {
dp[i][j] = dp[i - 1][j];
for (int k = 1; k * i <= j; k++) dp[i][j] = max(dp[i][j], dp[i - 1][j - k * i] + f[i][k]);
}
}
cout << *max_element(dp[n] + 1, dp[n] + S + 1);
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#define TASK "TASK"
if (fopen(TASK".INP", "r")) {
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
/* int TEST = 1; cin >> TEST; while (TEST--) */ Solve();
cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'void Solve()':
knapsack.cpp:41:29: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for (auto &&[v, k] : items[i]) {
| ^
knapsack.cpp: In function 'int32_t main()':
knapsack.cpp:62:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:63:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | freopen(TASK".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |