#include <bits/stdc++.h>
#define FOR(i, l, r, x) for(int i = l; i <= r; i += x)
#define FOD(i, l, r, x) for(int i = l; i >= r; i -= x)
#define debug(x) cout << "[DEBUG]: " << #x << " = " << x << endl;
#define _double_vector_ vector<vector<int>>
#define BIT(x) (1 << (x))
#define pii pair<int, int>
#define fi first
#define se second
//#define int long long
using namespace std;
const int N = 5e6 + 5;
const int mod = 1e9 + 7;
int S, n, dp[2005];
vector <pii> items;
void solve() {
cin >> S >> n; items.reserve(N);
FOR(_, 1, n, 1) {
int v, w, k; cin >> v >> w >> k;
int lim = min(S / w, k);
if (lim == 0) continue;
int tmp = __lg(lim);
FOR(i, 0, tmp, 1) {
int mul = min(BIT(i), k);
int cur_v = v * mul;
int cur_w = w * mul;
FOD(k, S, 0, 1) {
if (k >= cur_w) {
dp[k] = max(dp[k], dp[k - cur_w] + cur_v);
}
}
k -= mul;
if (k == 0) break;
}
}
cout << dp[S] << '\n';
}
signed main() {
#define name "task"
if (ifstream(name".inp")) {
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
52 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:53:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen(name".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... |