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. 剑指offer-面试题16.反转链表

    题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后的头结点 链表结点定义如下: struct ListNode { int m_nKey; ListNode* m_pNext; } 其实 ...

  2. oracle获取某一字段字符串长度

    用length方法 select t.* from tp_area t where substr(t.area_id,0,2)='03' and length(t.area_id)>2

  3. Java安装根目录

    bin存放了Java的操作工具,比如编译工具javac.启动JVM的Java等 db存放了Java测试的数据库Derby,企业不用 include存放C++的头文件 jre运行环境,里面有JVM li ...

  4. Python 自动化脚本学习(三)

    函数 例子 def hello(): print("hello" + "world"); 有参数的函数 def hello(name): print(" ...

  5. 【转换模型+扫描线】【UVA1398】Meteor

    The famous Korean internet company nhn has provided an internet-based photo service which allows The ...

  6. 关于mysql binlog日志的格式说明

    Binary Log 记录方式 Row Level Binary Log会记录成每一行数据被修改的形式,然后在Slave端再对相同的数据进行修改. 如果修改了表的结构,那么binlog日志记录的是重新 ...

  7. jQuery UI的基本使用方法与技巧

    一.概述 jQuery UI is a widget and interaction library built on top of the jQuery JavaScript Library, th ...

  8. 配置基于NotePad++工具下的C#开发环境

    1.打开NotePad++,打开Notepad++的插件(plugins)菜单-->Plugin Manager-->Show Plugin Manager-->勾选NppExec- ...

  9. SQL Server2008R2安装失败问题之语言包问题

           今天安装SQL Server2008 的时候出现了,如下的的问题,安装过程在ExcuteStandardTimingsWorkflow时候报错,结束安装.       提示:       ...

  10. C# 开机启动代码

     开机启动/关闭 代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...