题目链接:

http://codeforces.com/problemset/problem/106/C

C. 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 nmc0 and d0 (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10, 1 ≤ c0, d0 ≤ 100). Each of the following m lines contains 4integers. 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.

题目大意:

面包师Lavrenty打算用馅料做几个面包,然后把它们卖掉。

Lavrenty有\(n\)克面团和\(m\)种不同的馅料。馅料种类的下标从\(1到m\),他知道他的第\(i\)种馅料剩下\(a_i\) 克,做一个第\(i\)种馅料的面包,恰恰需要\(b_i\)克的\(i\)种馅料和\(c_i\)克的面团,同时这种面包可以卖\(d_i\)块钱。

他也可以做没有馅的面包。每个这样的面包需要\(c_0\)克面团,可以卖\(d_0\)块Tugrik。所以Lavrenty可以做任何数量的包子,用不同的馅料或者不用馅料,除非用完了面团和馅料。Lavrenty会扔掉烘培面包后剩下的所有多余材料。

求出Lavrenty可以赚取的钱的最大数量。

输入格式

第一行4个整数n,m,\(c_0,d_0\) \(1<=n<=1000,1<=m<=10\)
接下来m行每行四个整数\(a_i,b_i,c_i,d_i\) \(1<=a_i,b_i,c_i,d_i<=100\)

输出格式

输出Lavrenty可以赚取的最大钱数

思路:

多重背包+二进制优化(对多重中的二进制优化)

This is the code

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<sstream>
#include<stack>
#include<string>
#include<set>
#include<vector>
using namespace std;
#define PI acos(-1.0)
#define EPS 1e-8
#define MOD 1e9+7
#define LL long long
#define ULL unsigned long long     //1844674407370955161
#define INT_INF 0x7f7f7f7f      //2139062143
#define LL_INF 0x7f7f7f7f7f7f7f7f //9187201950435737471
// ios::sync_with_stdio(false);
// 那么cin, 就不能跟C的 scanf,sscanf, getchar, fgets之类的一起使用了。;
const int dr[]= {0, -1, 1,0, -1, -1, 1, 1};
const int dc[]= { 1, 0, 0, -1,-1, 1, -1, 1};
const int N = 100005;
int a[N];//表示第i种馅料剩余数量
int b[N];//表示做一个第i种馅饼需要的馅料数量
int c[N];//表示做一个第i种馅饼需要的面团数量
int d[N];//表示做一个第i种馅饼的收益
int num[N];//表示馅能够做几个,优化
int dp[N];
//0-1背包过程(减)
void ZERO_ONE_PACK(int cost,int m,int w) //m一般为容量或限制条件
{for(int i=m; i>=cost; i--)dp[i]=max(dp[i], dp[i-cost]+w);
}
//完全背包过程(加)
void COMPLETE_PACK(int cost,int m,int w) //m一般为容量或限制条件
{for(int i=cost; i<=m; i++)dp[i]=max(dp[i],dp[i-cost]+w);
}
void MULTIPLY_PACK(int cost,int m,int num,int w) //m一般为背包容量和限制条件
{//花费足够//这道题目就是馅料的数量足够if(cost*num>=m){COMPLETE_PACK(cost,m,w);return ;}//馅料的不够//二进制优化int k=1;while(k<num){ZERO_ONE_PACK(cost*k,m,k*w);num-=k;k*=2;}ZERO_ONE_PACK(cost*num,m,num*w);
}
int main()
{/*n 面团的千克m 馅料的种类*/int n,m,c0,d0;while(~scanf("%d%d%d%d",&n,&m,&c0,&d0)){for(int i=1; i<=m; i++){scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);num[i]=a[i]/b[i];//表示馅能够做几个}for(int i=1; i<=n; i++)dp[i]=i/c0*d0;//dp初始化的是i克面团不使用馅料,所能够卖的钱数for(int i=1; i<=m; i++)MULTIPLY_PACK(c[i],n,num[i],d[i]);//对每一种馅料进行求printf("%d\n",dp[n]);}return 0;
}

[CF106C]Buns -多重背包相关推荐

  1. Codeforces Beta Round #82 (Div. 2) C. Buns(多重背包)

    题目链接 题意:面包师Lavrenty打算用馅料做几个面包,然后把它们卖掉.Lavrenty有n克面团和m种不同的馅料.馅料种类的下标从1到m,他知道他的第i种馅料剩下ai 克,做一个第i种馅料的面包 ...

  2. 单调队列多重背包时间复杂度O(vn)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 多重背包问题: 有N种物品和容量为V的背包,若第i种物品,容量为v[i],价值为w[i],共有n[i]件.怎样装才能使背包内的物品总价值最大? 网 ...

  3. 背包问题(多重背包+0-1背包)

    一:0-1背包问题 #include<iostream> #include<algorithm> #include<cstring> const int maxn= ...

  4. 背包模型dp1:01背包,完全背包,多重背包的两大优化的详解

    01背包问题: 状态表示:f[i][j]表示从只从前i个物体里面选,切总体积不超过j的选法的集合状态表示:f[i][j]表示从只从前i个物体里面选,切总体积不超过j的选法的集合状态表示:f[i][j] ...

  5. POJ 3260 多重背包+完全背包

    前几天刚回到家却发现家里没网线 && 路由器都被带走了,无奈之下只好铤而走险尝试蹭隔壁家的WiFi,不试不知道,一试吓一跳,用个手机软件简简单单就连上了,然后在浏览器输入192.168 ...

  6. hdu 3732(01背包转多重背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3732 思路:这么大的数据,用01背包肯定会TLE的,01背包转多重背包..最多也就11*11=121件 ...

  7. 动态规划 4、基础背包问题总结(多重背包与多重背包的转化)

    描述: 有N种物品和一个容量为V的背包.第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 变式:有的物品 ...

  8. 多重背包单调队列优化思路_多重背包之单调队列优化理论性总结

    多重背包之单调队列优化: 若用F[j]表示对容量为j的背包,处理完前i种物品后,背包内物品可达到的最大总价值,并记m = min(n, j / v).放入背包的第i种物品的数目可以是:0.1.2--, ...

  9. HDU 2079 选课时间(题目已修改,注意读题) 母函数 || 多重背包

    今天做这题才知道原来母函数的原型不是从第二个括号开始,那不过是优化而已,除了1^n,2^n那种类型可以从2开始外其他都要从1开始.好了,上代码吧. #include<stdio.h> in ...

  10. Lightoj 1231 - Coin Change (I) (裸裸的多重背包)

    题目链接: Lightoj  1231 - Coin Change (I) 题目描述: 就是有n种硬币,每种硬币有两个属性(价值,数目).问用给定的硬币组成K面值,有多少种方案? 解题思路: 赤果果的 ...

最新文章

  1. python函数用法详解2(变量的作用域(全局变量、局部变量)、共享全局变量、函数返回值、函数的参数(位置参数、关键字参数、默认参数、不定长参数)、拆包、交换变量值、引用、可变和不可变类型)
  2. “互联网+”非遗谋定市场化-万祥军:经信研究世屹文化
  3. mysql脚本简书,mysql一键安装脚本
  4. 截取两个标签之间的文本
  5. 真彩色图像数据量 计算_军职在线大学计算机基础(自主模式)
  6. 互联网日报 | 爱奇艺会员宣布11月13日起涨价;淘宝特价版月活用户破7000万;我国成功发射一箭十三星...
  7. postfix邮件服务器
  8. window环境读linux文件,Windows本地环境和Linux腾讯云服务器之间传输文件的方法
  9. centos7-docker-swarmkit集群应用
  10. coreseek 利用python作数据源建立索引
  11. 电脑ps4,人在外心在家,教你用PC/MAC远程打PS4
  12. Smoke Loader、AZORult***通过虚假海啸警报传播到了日本
  13. POJ1753(枚举)
  14. MySQL5.7安装教程(workbench安装教程会在之后出)(32位和64位均可)
  15. 2016年最权威的1000集大型web前端视频教程(爱创课堂出品)
  16. Android基本界面元素的使用与讲解
  17. 创业公司的软件研发规范
  18. java圆的面积_JAVA求圆的面积
  19. Android手机AP模式下本机IP
  20. 【关于Citespace和JRE(JAVA运行环境)的详细安装教程】

热门文章

  1. C语言中文网设计模式,C语言和设计模式(访问者模式)
  2. 树莓派系统安装和调试 总结整理篇
  3. 计算机桌面上的声音图标没了怎么办,电脑声音图标不见了怎么办超详细教程
  4. Windows中使用pip下载任何包都报错
  5. 通过periodic_task.periodic_task 实现周期性任务的原理
  6. 【TAPD】快速上手
  7. golang 实现苹果内购服务端验证
  8. 2022-2028年中国沉香产业竞争现状及投资前景分析报告
  9. 希尔伯特曲线 java_希尔伯特曲线(示例代码)
  10. RC电路时间常数的定义及计算