# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
983305 | fasgqwrgqwg | Knapsack (NOI18_knapsack) | C++17 | 0 ms | 0 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;
#ifndef ONLINE_JUDGE
#include "/debug.cpp"
#else
#define debug(...) 0
#endif
#define yes cout << "YES" << '\n'
#define no cout << "NO" << '\n'
#define pll pair<long long, long long>
#define pii pair<int, int>
#define newl "\n"
#define vt vector
#define ar array
#define LOOP(iter) iter.begin(), iter.end()
#define FOR(i, n) for (ll i = 0; i < n; i++)
#define FORK(i, n, k) for (ll i = k; i < n; i++)
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
#define pb(x) push_back(x)
#define ff first
#define ss second
#define CEIL(a, b) ((a + b - 1) / b)
#define fileio(in, out) \
freopen(in, "r", stdin); \
freopen(out, "w", stdout)
const int MAX_N = 1e5 + 5;
const ll MOD_INV = 500000004;
const ll MOD = 1e9 + 7;
const ll INF = 10e9 + 5;
const ld EPS = 1e-9;
template<class T> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0;
}
void solve() {
ll n, s;
cin >> s >> n;
vector<ll> v(n), w(n), k(n);
for (int i = 0; i < n; i++) {
cin >> v[i];
cin >> w[i];
cin >> k[i];
}
vector<vector<pll>> dp(n + 1, vector<pll>(s + 1, {0, 0}));
for (ll i = 1; i <= n; i++) {
for (ll j = 0; j <= s; j++) {
dp[i][j] = dp[i - 1][j];
if (j - w[i] >= 0) {
if (dp[i][j].ff < dp[i - 1][j - w[i]].ff + v[i]) {
dp[i][j].ff = dp[i - 1][j - w[i]].ff + v[i];
dp[i][j].ss = 1;
}
if (dp[i][j].ff < dp[i][j - w[i]].ff and dp[i][j].ss + 1 <= k[i]) {
dp[i][j].ff = dp[i][j - w[i]].ff + v[i];
dp[i][j].ss++;
}
}
}
}
cout << dp[n][s].ff;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
// cout << "Case #" << t << ": ";
solve();
}
}