#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 (ll 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;
int S, n, m;
vector<pa> a, b;
vector<ll> g[W];
ll dp[W];
void hbmt() {
// inp
cin >> S >> n;
FOR(i, 1, n) {
ll v;
int w, k;
cin >> v >> w >> k;
k = min(S / w, k);
ll heso = 1, sum = 1;
a.push_back({v, w});
while(1) {
heso *= 2;
if(sum + heso > k) break;
sum += heso;
a.push_back({heso * v, heso * w});
}
if(sum < k) {
heso = k - sum;
a.push_back({heso * v, heso * w});
}
}
sort(a.begin(), a.end(), greater<pa>());
for(auto e : a) {
ll v = e.fi, w = e.se;
if(g[w].size() > S / w) continue;
g[w].push_back(v);
}
b.push_back({0, 0});
FOR(i, 0, S) {
for(auto v : g[i]) {
b.push_back({v, i});
}
}
FOR(i, 1, b.size() - 1) {
ll v = b[i].fi;
int w = b[i].se;
FORD(j, S, w) {
dp[j] = max(dp[j - w] + v, dp[j]);
}
}
cout << dp[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:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | freopen("hbmt.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
knapsack.cpp:71:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | 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... |