#pragma GCC optimize("Ofast")
#pragma GCC optimize("avx2")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define int long long
using namespace std;
#define all(x) x.begin(), x.end()
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
template<typename T> bool smin(T& a, const T& b) { if (b < a) { a = b; return true; } return false;}
template<typename T> bool smax(T& a, const T& b) { if (a < b) { a = b; return true; } return false;}
template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a){return out << a.first << " " << a.second;}
template<typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &x: v) in >> x; return in; }
template<typename T>ostream &operator<<(ostream &out, const vector<T> &v) { for (const T &x: v) out << x << ' ';return out;}
template <class K, class V>
using ht = gp_hash_table<K, V, hash<K>, equal_to<K>, direct_mask_range_hashing<>, linear_probe_fn<>, hash_standard_resize_policy<hash_exponential_size_policy<>, hash_load_check_resize_trigger<>, true>>;
#ifndef ONLINE_JUDGE
#define dout(...) cerr << "Line:" << __LINE__ << " [" << #__VA_ARGS__ << "] = ["; _print(__VA_ARGS__)
#else
#define dout(...)
#endif
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int gen_random(int l, int r) {
return uniform_int_distribution<int>(l, r)(rng);
}
void pbit(int a) {
cout << a << " : " << bitset<40>(a) << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int s, n;
cin >> s >> n;
vector<int> v(n + 1), w(n + 1), k(n + 1);
for (int i = 1; i <= n; i++) {
cin >> v[i] >> w[i] >> k[i];
}
// vector<vector<int>> dp(2, vector<int>(s + 1, -1));
// function<int(int, int)> f = [&](int i, int W) -> int {
// if (i >= n) return 0;
// if (dp[1 - (i & 1)][W] != -1) return dp[1 - (i & 1)][W];
// int ans = f(i + 1, W);
// for (int j = 1; j <= k[i]; j++) {
// if (W - j * w[i] < 0) break;
// ans = max(ans, j * v[i] + f(i + 1, W - j * w[i]));
// }
// return dp[i & 1][W] = ans;
// };
vector<vector<int>> dp(2, vector<int>(s + 1, 0));
for (int i = 1; i <= n; i++) {
for (int W = 1; W <= s; W++) {
int res = dp[1 - (1 & i)][W];
for (int c = 1; c <= k[i]; c++) {
if (W - c * w[i] >= 0) {
res = max(c * v[i] + dp[1 - (1 & i)][W - c * w[i]], res);
} else {
break;
}
}
dp[i&1][W] = res;
}
}
int ans = 0;
for (int i = 0; i <= 1; i++) {
for (int W = 1; W <= s; W++) {
ans = max(ans, dp[i][W]);
}
}
cout << ans;
return 0;
}
Compilation message (stderr)
knapsack.cpp:2:28: warning: bad option '-favx2' to pragma 'optimize' [-Wpragmas]
2 | #pragma GCC optimize("avx2")
| ^
knapsack.cpp:12:48: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
12 | template<typename T> bool smin(T& a, const T& b) { if (b < a) { a = b; return true; } return false;}
| ^
knapsack.cpp:13:48: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
13 | template<typename T> bool smax(T& a, const T& b) { if (a < b) { a = b; return true; } return false;}
| ^
knapsack.cpp:14:82: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
14 | template <class A, class B> ostream &operator<<(ostream &out, const pair<A, B> &a){return out << a.first << " " << a.second;}
| ^
knapsack.cpp:15:67: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
15 | template<typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &x: v) in >> x; return in; }
| ^
knapsack.cpp:16:73: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
16 | template<typename T>ostream &operator<<(ostream &out, const vector<T> &v) { for (const T &x: v) out << x << ' ';return out;}
| ^
knapsack.cpp:25:35: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
25 | inline int gen_random(int l, int r) {
| ^
knapsack.cpp:28:16: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
28 | void pbit(int a) {
| ^
knapsack.cpp:31:13: warning: bad option '-favx2' to attribute 'optimize' [-Wattributes]
31 | signed main() {
| ^
# | 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... |