题目链接:Codeforces 106C Buns
Lavrenty, a baker, is going to make several buns with stuffings and sell them.
Lavrenty has n grams of dough as well as m different stuffing types. The stuffing types are numerated from 1 to m. Lavrenty knows that he has ai grams left of the i-th stuffing. It takes exactly bi grams of stuffing i and ci grams of dough to cook a bun with the i-th stuffing. Such bun can be sold for di tugriks.
Also he can make buns without stuffings. Each of such buns requires c0 grams of dough and it can be sold for d0 tugriks. So Lavrenty can cook any number of buns with different stuffings or without it unless he runs out of dough and the stuffings. Lavrenty throws away all excess material left after baking.
Find the maximum number of tugriks Lavrenty can earn.
Input
The first line contains 4 integers n, m, c0 and d0 (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10, 1 ≤ c0, d0 ≤ 100). Each of the following m lines contains 4 integers. The i-th line contains numbers ai, bi, ci and di (1 ≤ ai, bi, ci, di ≤ 100).
Output
Print the only number — the maximum number of tugriks Lavrenty can earn.
Examples
input
10 2 2 1
7 3 2 100
12 3 1 10
output
241
input
100 1 25 50
15 5 20 10
output
200
Note
To get the maximum number of tugriks in the first sample, you need to cook 2 buns with stuffing 1, 4 buns with stuffing 2 and a bun without any stuffing.
In the second sample Lavrenty should cook 4 buns without stuffings.
题意:给你n克面团和m种填料,对于第i种填料,它剩余a[i]克,你可以选择b[i]克该填料和c[i]克面团来生产一件物品,价值为d[i]。你也可以直接使用c0克面团来生产一件物品,价值为d0。问你可以获得的最大价值。
思路:背包。
设置dp[i][j][k]为状态——在k克面团的前提下使用前i种填料且第i种填料生成j件物品的最大价值。
状态转移方程较复杂。
首先dp[i][][]的第一个状态取决于dp[i-1][][]的最后一个状态,去掉第一维。其次dp[][j+1][]仅仅取决于dp[][j][],那么我们在递推时,升序枚举j即可,这样去掉第二维。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
const int MAXN = 1e3+10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-4;
int dp[MAXN];
int main()
{
int n, m, c0, d0; cin >> n >> m >> c0 >> d0;
CLR(dp, 0);
for(int i = c0; i <= n; i++) dp[i] = i / c0 * d0;
for(int i = 1; i <= m; i++)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
for(int j = 1; j <= a / b; j++)
{
for(int k = n; k >= c; k--)
dp[k] = max(dp[k-c] + d, dp[k]);
}
}
cout << dp[n] << endl;
return 0;
}