CodeForces 106C Buns (01beoba)

来源:互联网 发布:pl sql developer下载 编辑:程序博客网 时间:2024/05/23 07:24

http://codeforces.com/problemset/problem/540/B

C. Buns
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nmc0 and d0 (1 ≤ n ≤ 10001 ≤ m ≤ 101 ≤ c0, d0 ≤ 100). Each of the following m lines contains4 integers. The i-th line contains numbers aibici 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 17 3 2 10012 3 1 10
output
241
input
100 1 25 5015 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, c0, d0 ,n,表示面的总数,m表示下边数据有m行,c0表示做一个馒头需要c0的面,d0表示一个馒头的价值是d0;

ai, bi, ci,di, ai表示做第i种包子一个有的馅,bi表示做第i种包子一个需要bi的陷,ci表示做第i种包子一个需要ci的面,di表示一个该种包子的价值是di;

思路:01背包

#include <iostream>#include <cmath>#include <cstring>#include <algorithm>#include <queue>#include <cstdio>using namespace std;#define N 110#define INF 0x3f3f3f3f#define met(a, b) memset (a, b, sizeof (a))int dp[N*10];int main (){    int n, m, w[N], h[N], v[N], a, b, x, y, z, t;    while (scanf ("%d %d %d %d", &n, &m, &a, &b) != EOF)    {        met (dp, 0);        w[0] = n/a;///可以做的馒头数        h[0] = a;///一个馒头需要的面        v[0] = b;///每个馒头的价值        for (int i=1; i<=m; i++)        {            scanf ("%d %d %d %d", &x, &y, &z, &t);            w[i] = x/y;///可以做的包子数            h[i] = z;///一个包子需要的面            v[i] = t;///每个包子的价值        }        for (int i=0; i<=m; i++)        {            for (int j=1; j<=w[i]; j++)            {                for (int k=n; k>=h[i]; k--)                    dp[k] = max (dp[k], dp[k-h[i]]+v[i]);            }        }        printf ("%d\n", dp[n]);    }    return 0;}


1 0