Magic Powder(Codeforces 670D1/D2)---(暴力/二分)

Code forces 670D1

This problem is given in two versions that differ only by constraints. If you can solve this problem in large constraints, then you can just write a single solution to the both versions. If you find the problem too difficult in large constraints, you can write solution to the simplified version only.

Waking up in the morning, Apollinaria decided to bake cookies. To bake one cookie, she needs n ingredients, and for each ingredient she knows the value ai — how many grams of this ingredient one needs to bake a cookie. To prepare one cookie Apollinaria needs to use all n ingredients.

Apollinaria has bi gram of the i-th ingredient. Also she has k grams of a magic powder. Each gram of magic powder can be turned to exactly 1 gram of any of the ningredients and can be used for baking cookies.

Your task is to determine the maximum number of cookies, which Apollinaria is able to bake using the ingredients that she has and the magic powder.

Input

The first line of the input contains two positive integers n and k (1 ≤ n, k ≤ 1000) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 1000), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Examples

Input

3 1
2 1 4
11 3 16

Output

4

Input

4 3
4 3 5 6
11 12 14 20

Output

3

Note

In the first sample it is profitably for Apollinaria to make the existing 1 gram of her magic powder to ingredient with the index 2, then Apollinaria will be able to bake 4 cookies.

In the second sample Apollinaria should turn 1 gram of magic powder to ingredient with the index 1 and 1 gram of magic powder to ingredient with the index 3. Then Apollinaria will be able to bake 3 cookies. The remaining 1 gram of the magic powder can be left, because it can't be used to increase the answer.

题意:有 n 种材料, k 个魔法粉,魔法粉可以变成任何一种材料,给出制作一个曲奇需要的 n 个材料的数量和 n 个材料总的量,求 最多能做几个曲奇饼干(max)?

题解:数据范围(<=1000)不大,直接暴力求解。先求出不用魔法粉能做出来的最少的曲奇数m,然后m++,判断是否可行

#include<stdio.h>
#include<string.h> 
#include<math.h>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;
const int  max_n=1005;

int n,k,m=1e8,flag;
struct node
{
	int one;
	int all;
	int vis;
}a[max_n];

int main() 
{
	while(scanf("%d%d",&n,&k)==2)
	{
		flag=0,m=1e8;
		for(int i=0;i<n;i++)scanf("%d",&a[i].one);
		for(int i=0;i<n;i++)scanf("%d",&a[i].all);
		for(int i=0;i<n;i++)
		{
			a[i].vis=a[i].all/a[i].one;
			if(a[i].vis<m)m=a[i].vis;
			//printf("%d %d %d \n",a[i].one,a[i].all,a[i].vis);
		}
		int k1=k;
		while(1)
		{
			m++; flag=0;
			for(int i=0;i<n;i++)
			{
				if(a[i].vis<m)
				{
					flag+=m*a[i].one-a[i].all;
				}
			}
			if(flag>k)
			{
				m--;
				break;
			}
		}
		printf("%d",m);
	}
	return 0;
}

Code forces 670D2(数据范围变大了,需要二分求解)

The term of this problem is the same as the previous one, the only exception — increased restrictions.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Examples

Input

1 1000000000
1
1000000000

Output

2000000000

Input

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1 1 1 1 1

Output

0

Input

3 1
2 1 4
11 3 16

Output

4

Input

4 3
4 3 5 6
11 12 14 20

Output

3

题意一致,不过数据范围变成了1e9,暴力会TLE,需要二分查找

题解:二分查找(0~2e9+5)ans

#include<stdio.h>
#include<string.h> 
#include<math.h>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;
const LL max_n=100000+5;

LL n,k,flag;
struct node
{
	LL one;
	LL all;
	LL vis;
}a[max_n];

bool pd(LL m)
{
	flag=k;
	for(int i=0;i<n;i++)
	{
		if(a[i].vis<m)
		{
			if(flag<m*a[i].one-a[i].all)return 0;
			flag-=(m*a[i].one-a[i].all);
		}
	}
	return 1;
}

int main() 
{
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	while(scanf("%lld%lld",&n,&k)==2)
	{
		for(LL i=0;i<n;i++)scanf("%lld",&a[i].one);
		for(LL i=0;i<n;i++)scanf("%lld",&a[i].all);
		for(LL i=0;i<n;i++)a[i].vis=a[i].all/a[i].one;
		LL low=0,high=2e9+5,mid=0;
		while(low<high)
		{
			mid=(low+high+1)/2;
			//printf("###pd(mid)=%d\n",pd(mid));
			if(pd(mid))low=mid;
			else high=mid-1;
		}
		printf("%lld\n",high);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43484493/article/details/87392564
今日推荐