|
- #include <cstdio>
- #include <algorithm>
- typedef long long LL;
- using namespace std;
- const int N = 1010;
- const LL INF = 1ll << 62;
- struct A {
- LL cost, val, group;
- bool operator < (const A &x) {
- return group < x.group;
- }
- }a[N];
- struct Group {
- LL s, t, small;
- }g[N];
- LL f[N];
- int main() {
- LL V, n;
- scanf("%lld%lld", &V, &n);
- for(int i = 1; i <= n; i++) {
- scanf("%lld%lld%lld", &a[i].cost, &a[i].val, &a[i].group);
- }
- sort(a + 1, a + n + 1);
- for(int i = 1; i <= n; i++) {
- if(a[i].group != a[i - 1].group) {
- g[a[i].group].s = i;
- }
- g[a[i].group].t = i;
- }
- for(int i = 1; i <= a[n].group; i++) {
- g[i].small = INF;
- for(int j = g[i].s; j <= g[i].t; j++) {
- g[i].small = min(g[i].small, a[j].cost);
- }
- }
-
- for(int k = 1; k <= a[n].group; k++) { /// k-th group
- for(int i = V; i >= g[k].small; i--) { /// i V
- for(int j = g[k].s; j <= g[k].t; j++) { /// j which
- if(i >= a[j].cost) {
- f[i] = max(f[i], f[i - a[j].cost] + a[j].val);
- }
- }
- }
- }
- LL ans = 0;
- for(int i = 1; i <= V; i++) {
- ans = max(ans, f[i]);
- }
- printf("%lld", ans);
- return 0;
- }
复制代码 |
|