# Magic Powder - 2 CodeForces - 670D2 （二分）

## Magic Powder - 2

CodeForces - 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.

Example
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

code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll n,k,a[100002],b[100002];

int main(){
cin >> n >> k;
ll i;
for(i = 0; i < n; i++){
cin >> a[i];
}
for(i = 0; i < n; i++){
cin >> b[i];
}//输入
ll left = 0,right = 2000000000;//使用二分，先定义一个最大边界，然后二分查找个数
ll ans = 0;
while(left <= right){
ll mid = (left+right)/2;
ll m = k;
int flag = 1;
for(i = 0;i < n; i++){//以此判断每种原料
if(a[i] * mid > b[i]){//如果所需原料乘以个数大于所有的原料，使用万能原料
if(m - (a[i] * mid - b[i]) >= 0){//如果万能原料大于等于所需，则求出剩余的万能原料
m -= (a[i] * mid - b[i]);
}
else{
flag = 0;
break;//否则说明这个个数是不可能做出来的，太多
}
}
//else如果所需原料都小于所有的那就不用管了
}
if(flag){//说明这个个数可以，看看更大可不可以
ans = mid;
left = mid + 1;
}
else{//否则找小一点的
right = mid - 1;
}
}
printf("%lld\n",ans);
return 0;
}