codeforces 106C. Buns【多重背包】

来源:互联网 发布:linux 安装中文字符集 编辑:程序博客网 时间:2024/05/18 14:46

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种馅料,然后还可以做不带馅儿的包子,首先给出所需的面粉量和能够出售的价格,之后给出每种馅料的量以及该种包子所需的面粉量和馅料量以及和出售的价格,然后问最多能得到多少钱。多重背包,只会找模板。。。


#include <iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<algorithm>#define maxn 1010using namespace std;int c[maxn],d[maxn];int num[maxn],f[maxn];int main(){    int sd,m,a,b,c0,d0;    while(~scanf("%d%d%d%d",&sd,&m,&c0,&d0))    {        for(int i=c0;i<=sd;++i)            f[i]=i/c0*d0;        for(int i=0;i<m;++i)        {            scanf("%d%d%d%d",&a,&b,&c[i],&d[i]);            num[i]=a/b;        }        for(int i=0;i<m;++i)        {            for(int j=0;j<num[i];++j)            {                for(int k=sd;k>=c[i];k--)                {                    f[k]=max(f[k],f[k-c[i]]+d[i]);                }            }        }        printf("%d\n",f[sd]);    }    return 0;}


0 0
原创粉丝点击