CodeForces106C.Buns (多重背包)

Description

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 n, m, c0 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 ai, bi, ci and di (1 ≤ ai, bi, ci, di ≤ 100).

Output

Print the only number — the maximum number of tugriks Lavrenty can earn.

Sample Input

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
Sample Output

Hint

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 种馅。馅的编号从1 到 m。小丛知道第i种馅还有ai 克。 当制作含有第i种馅的包子时,小丛需要 bi 克的馅和 ci 克的面团,这样的一个包子卖di 个金币。

当然她也可以做没馅的包子,每做一个没馅包子需要c0 克面团,每个卖d0 个金币。所以,小丛可以做任何数量的有馅或者没有馅的包子,直到面团或者馅都用光了;她可以选择将所有多余的材料扔掉。

请你帮助小丛算出她最多可以赚多少金币。

第一行包含四个整数 n, m, c0 和 d0 (1 ≤ n ≤ 1000, 1 ≤ m ≤ 10, 1 ≤ c0, d0 ≤ 100)。接下来的m 行每行都包含4 个整数,其中第 i行包含 ai, bi, ci 和di (1 ≤ ai, bi, ci, di ≤ 100)。
输出一个数—— 小丛最多可以赚的金币数。
在这里插入图片描述
多重背包 每种物品重量 价值 限定数量
问题转化
面团总量n等价背包容量
m种馅相当于m种物品
空面团最大数量=n/c0
m种面团数量=ai/bi 总馅/每一种馅
w[i]=ci p[i]=di(重量为该面团重量 价值为此种馅包子价值)
三重循环
i物品种类 1-m+1
j当前背包容量 0-n
k为第i种物品数量 不得超过该物品总数num[i]和当前背包容量j(k*w[i]<=j)

n=1000 m=10 k个数 空包子重量为1g n=1000g面团 空包子最多1000个
复杂度 O(mnk)=1e7
2000ms

#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cctype>
using namespace std;

#define PI acos(-1.0)
#define fi first
#define se second
#define pb push_back
#define forn(i,n) for(int i=0;i<n;i++)
#define for1(i,n) for(int i=1;i<=n;i++) 
#define rep(i,a,n)  for(int i=a;i<n;i++)
#define IO                       \
    ios::sync_with_stdio(false); \
    cin.tie(0);                  \
    cout.tie(0)
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

const int INF=0x3f3f3f3f;
const int maxn=(1<<17)+5;
const int mod=1e9+7;

int gcd(int a,int b)
{
	return b!=0?gcd(b,a%b):a;

}

ll dp[15][1005];//11种物品 背包容量1000 
ll w[15],v[15],num[15];//m种物品重量 价值 个数 
int main()
{
	int n,m,c0,d0;//面n背包容量 m种类数 m+1种包子 c0空包子重量 d0价值
	cin>>n>>m>>c0>>d0;
	w[1]=c0;
	v[1]=d0;
	num[1]=n/c0;//总面/一个空包子面=限定最多多少个空包子 
	for(int i=2;i<=m+1;i++)//m次 
	{
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		w[i]=c;//这种包子面重量 
		v[i]=d;//价值 
		num[i]=a/b;//总馅/这种每一个馅 
	} 
	for(int i=1;i<=m+1;i++)//遍历m+1种物品
	{
		for(int j=0;j<=n;j++)//背包容量
		{
			for(int k=0;k<=num[i] && k*w[i]<=j;k++)//不超过这种物品i总个数 k个重w[i]的物品总重量不超过背包容量 
			{
				dp[i][j]=max(dp[i][j],dp[i-1][j-w[i]*k]+k*v[i]);
				//遍历到第i种物品 容量为j的背包 最大价值 
			}
		} 
	}
	ll maxx=0;
	for(int i=0;i<=n;i++)//遍历到第m+1个物品 
		maxx=max(dp[m+1][i],maxx);
	cout<<maxx<<endl;
	return 0;
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值