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 tom. Lavrenty knows that he has ai grams left of the i-th stuffing. It takes exactly bi grams of stuffing i and cigrams 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 ford0 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 ≤ 1000, 1 ≤ m ≤ 10, 1 ≤ c0, d0 ≤ 100). Each of the following m lines contains 4 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 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.

思维:让你做蛋糕,有各种口味的奶油,和面,消耗一定的面一定口味的奶油可以卖d元,问最多买多少钱;因为是蛋糕肯定是整个整个的,所以要用到背包;多重背包;

dp[i][k]代表前i种蛋糕在面小于等于k的最大利润;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
using namespace std;
const int INF=0x3f3f3f3f;
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
#define mem(x,y) memset(x,y,sizeof(x))
struct Node{
int a,b,c,d;
};
Node dt[];
int dp[][];
int main(){
int n;
int m;
while(~scanf("%d%d%d%d",&n,&m,&dt[].c,&dt[].d)){
dt[].a=;dt[].b=;
for(int i=;i<=m;i++)scanf("%d%d%d%d",&dt[i].a,&dt[i].b,&dt[i].c,&dt[i].d);
mem(dp,);
for(int i=;i<=m;i++){
for(int j=;j*dt[i].b<=dt[i].a;j++){
for(int k=n;k>=j*dt[i].c;k--){
if(i)dp[i][k]=max(dp[i][k],dp[i-][k-j*dt[i].c]+j*dt[i].d);
else dp[i][k]=max(dp[i][k],dp[i][k-j*dt[i].c]+j*dt[i].d);
}
}
}
printf("%d\n",dp[m][n]);
}
return ;
}

Buns(dp+多重背包)的更多相关文章

  1. HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化)

    HDOJ(HDU).2844 Coins (DP 多重背包+二进制优化) 题意分析 先把每种硬币按照二进制拆分好,然后做01背包即可.需要注意的是本题只需要求解可以凑出几种金钱的价格,而不需要输出种数 ...

  2. HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化)

    HDOJ(HDU).1059 Dividing(DP 多重背包+二进制优化) 题意分析 给出一系列的石头的数量,然后问石头能否被平分成为价值相等的2份.首先可以确定的是如果石头的价值总和为奇数的话,那 ...

  3. HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化)

    HDOJ(HDU).2191. 悼念512汶川大地震遇难同胞――珍惜现在,感恩生活 (DP 多重背包+二进制优化) 题意分析 首先C表示测试数据的组数,然后给出经费的金额和大米的种类.接着是每袋大米的 ...

  4. poj1014 dp 多重背包

    //Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...

  5. CodeForces922E DP//多重背包的二进制优化

    https://cn.vjudge.net/problem/1365218/origin 题意 一条直线上有n棵树 每棵树上有ci只鸟 在一棵树底下召唤一只鸟的魔法代价是costi 每召唤一只鸟,魔法 ...

  6. BZOJ.3425.[POI2013]Polarization(DP 多重背包 二进制优化)

    BZOJ 洛谷 最小可到达点对数自然是把一条路径上的边不断反向,也就是黑白染色后都由黑点指向白点.这样答案就是\(n-1\). 最大可到达点对数,容易想到找一个点\(a\),然后将其子树分为两部分\( ...

  7. hdu1059 dp(多重背包二进制优化)

    hdu1059 题意,现在有价值为1.2.3.4.5.6的石头若干块,块数已知,问能否将这些石头分成两堆,且两堆价值相等. 很显然,愚蠢的我一开始并想不到什么多重背包二进制优化```因为我连听都没有听 ...

  8. ACM学习历程—HDU 1059 Dividing(dp && 多重背包)

    Description Marsha and Bill own a collection of marbles. They want to split the collection among the ...

  9. POJ 1742 Coins ( 经典多重部分和问题 && DP || 多重背包 )

    题意 : 有 n 种面额的硬币,给出各种面额硬币的数量和和面额数,求最多能搭配出几种不超过 m 的金额? 分析 : 这题可用多重背包来解,但这里不讨论这种做法. 如果之前有接触过背包DP的可以自然想到 ...

随机推荐

  1. Minimum Depth of Binary Tree 解答

    Question Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along ...

  2. VBA:Google翻译(含tk算法)

    完整的tk算法: //源自http://translate.google.cn/ TKK=eval('((function(){var a\x3d618632403;var b\x3d14854840 ...

  3. 使用Memcache在PHP中调试方法的介绍及应用

    使用Memcache在PHP中调试方法的介绍及应用 如果我们在网络开发中,特别是大访问量的web项目开发中,为了提高响应速度,减少数据查询运算,那么我们都会选用memcahce.首先我们必须要安装,接 ...

  4. Unity 生命周期

    原文翻译:            Execution Order of Event Functions            事件函数的执行顺序                        Edit ...

  5. UISearchBar 点击X 按钮收键盘

    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;{ NSLog(@"textD ...

  6. kettle工具同步数据乱码-Linux下乱码问题二

    将写好的kettle工程部署到Linux下后,同步的数据都成了乱码,幸运的是数据库有备份. 下面就说一下,kettle工程如何同步两端编码格式都是utf8的数据库. 我们只需要更改kettle数据库连 ...

  7. 有关Oracle cvu和cvuqdisk

    有关Oracle cvu和cvuqdisk cvu的下载链接: http://www.oracle.com/technetwork/products/clustering/downloads/cvu- ...

  8. 【巧妙预处理系列+离散化处理】【uva1382】Distant Galaxy

    给出平面上的n个点,找一个矩形,使得边界上包含尽量多的点. [输入格式] 输入的第一行为数据组数T.每组数据的第一行为整数n(1≤n≤100):以下n行每行两个整数,即各个点的坐标(坐标均为绝对值不超 ...

  9. office 文件在网页中显示

    1.如何在网页上显示word和excel a.可以使用office组件或aspose将word 和excel 转换为pdf 然后在网页上打开pdf,但是效果不是很好 .比如说excel 多个工作薄不是 ...

  10. OS X EI Capitan 安装mysql-5.7.9

    先去下载mysql-5.7.9-osx10.9-x86_64.dmg安装(一直下一步,输入密码即可)mysql-5.7.9-osx10.9-x86_64.pkg好了,启动MySQL服务. Untitl ...