# |BZOJ 1649|二维背包|[Usaco2006 Dec]Cow Roller Coaster

BZOJ 1649
Luogu 2854
from: USACO 2006 Dec Sliver(USACO刷题第10题)

f[xi[i]+wi[i]][j]=max(f[xi[i]+wi[i]][j],f[xi[i]][jci[i]]+fi[i])

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
using namespace std;

const int MAXN = 10000 + 5, MAXL = 1000 + 5;

struct data {
int xi, wi, fi, ci;
bool operator < (const data &b) const {
return xi < b.xi;
}
}di[MAXN];
int L, n, B;
LL f[MAXL][MAXL];//设f[i][j]为铺到i,成本为j

void clear() {
ms(f, -127);
f[0][0] = 0;
}
void init() {
clear();
for (int i=1;i<=n;i++) {
scanf("%d%d%d%d", &di[i].xi, &di[i].wi, &di[i].fi, &di[i].ci);
}
sort(di+1, di+1+n);
}
void solve() {
LL ans = 0;
for (int i=1;i<=n;i++) {
for (int j=B;j>=di[i].ci;j--) {
f[di[i].xi+di[i].wi][j] = max(f[di[i].xi+di[i].wi][j], f[di[i].xi][j-di[i].ci]+di[i].fi);
ans = max(ans, f[L][j]);
}
}
if (ans>0) printf("%lld\n", ans); else printf("-1\n");
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);freopen("1.out", "w", stdout);
#endif
while (scanf("%d%d%d", &L, &n, &B)==3) init(), solve();
return 0;
}
08-05 663

10-04 82
10-14 38
11-25 36
10-15 252
08-02 387
08-29 74
04-22 1635
09-16 24
12-01 577
03-11 225
04-03 28
03-20 550
08-22 18