/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1ll << (x)))
#define debug(a, l, r) for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';
const int MAXN = 3e4 + 5;
const int MAXS = 2005;
int s, n = 0, w[MAXN], v[MAXN];
vector<pii> items[MAXS];
void init() {
int _n;
cin >> s >> _n;
for (int i = 1; i <= _n; ++i) {
int val, weight, num;
cin >> val >> weight >> num;
items[weight].push_back({val, num});
}
// Group items by weight
for (int weight = 1; weight <= s; ++weight) {
if (items[weight].empty()) continue; // Early break
int target = s / weight;
sort(all(items[weight]), greater<pii>());
vector<int> chosen;
for (auto &info : items[weight]) {
if (target == 0) break;
int take = min(info.se, target);
target -= take;
for (int i = 1; i <= take; ++i) {
v[++n] = info.fi;
w[n] = weight;
}
}
}
}
const int INF = 1e18 + 5;
int dp[MAXS];
void solve() {
fill(dp, dp + s + 1, -INF);
dp[0] = 0;
for (int i = 1; i <= n; ++i)
for (int j = s; j >= w[i]; --j)
if (dp[j - w[i]] != -INF)
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
cout << *max_element(dp, dp + s + 1);
}
signed main() {
#ifdef NCTHANH
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(nullptr); cout.tie(nullptr);
init();
solve();
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... |