#include "bits/stdc++.h"
#define sz(x) (int)size(x)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define int long long
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const char nl = '\n';
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int W = 2000;
template<class T> void chmax(T &a, T b) { if (b > a) a = b; }
int32_t main() {
ios::sync_with_stdio(0), cin.tie(0);
int s, n; cin >> s >> n;
struct itemW { int v, k; };
vector<vector<itemW>> a(W + 1);
for (int i = 0; i < n; i++) {
int v, w, k; cin >> v >> w >> k;
a[w].push_back({v, k});
}
struct item { int v, w; };
vector<item> b;
for (int i = 1; i <= W; i++) {
sort(all(a[i]), [](const itemW &a, const itemW &b){
return a.v > b.v;
});
int rem = s / i;
for (auto [v, k] : a[i]) {
int qnt = min(rem, k);
for (int _ = 0; _ < qnt; _++)
b.push_back({v, i});
rem -= qnt;
if (rem == 0) break;
}
}
vector<int> dp(s + 1, -LINF);
dp[0] = 0;
for (auto [v, w] : b)
for (int i = s; i >= w; i--)
chmax(dp[i], dp[i - w] + v);
cout << *max_element(all(dp)) << nl;
return 0;
}
# | 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... |