#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a), _b = (b); i <= _b; i++)
#define FORD(i, b, a) for (int i = (b), _a = (a); i >= _a; i--)
#define pa pair<ll, ll>
#define fi first
#define se second
#define bit(mask, j) ((mask >> j) & 1)
#define t_test int t;cin >> t;while(t--)
const ll mod = 1e9 + 7;
const ll INF = 1e17;
const ll N = 1e2 + 10;
const ll W = 2e3 + 10;
ll S, n, m;
vector<pa> a;
ll dp[2][W];
void hbmt() {
// inp
cin >> S >> n;
a.push_back({0, 0});
FOR(i, 1, n) {
ll v, w, k;
cin >> v >> w >> k;
ll heso = 1, sum = 1;
a.push_back({v, w});
while(1) {
heso *= 2;
if(sum + heso > k) break;
if(heso > S / w) break;
sum += heso;
a.push_back({heso * v, heso * w});
}
if(sum < k) {
heso = k - sum;
a.push_back({heso * v, heso * w});
}
}
FOR(i, 1, a.size() - 1) {
ll v = a[i].fi, w = a[i].se;
FOR(j, 0, S) {
dp[i % 2][j] = dp[(i - 1) % 2][j];
if(j >= w) {
dp[i % 2][j] = max(dp[i % 2][j] , dp[(i - 1) % 2][j - w] + v);
}
}
}
cout << dp[(a.size() - 1) % 2][S];
// slove
// return subX::slove();
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
if(fopen("hbmt.inp", "r")) {
freopen("hbmt.inp", "r", stdin);
freopen("hbmt.out", "w", stdout);
}
// t_test
hbmt();
return 0;
}
Compilation message (stderr)
knapsack.cpp: In function 'int main()':
knapsack.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen("hbmt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("hbmt.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... |