Codeforces Round #350 (Div. 2)_D2 - Magic Powder - 2

时间:2023-03-09 09:48:56
Codeforces Round #350 (Div. 2)_D2 - Magic Powder - 2
D2. Magic Powder - 2
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

题解:二分答案的经典应用


 #include<cstdio>
#define LL long long
#define FFC(i,a,b) for(int i=a;i<=b;i++)
LL a[],b[],n,k,maxn=*1e9+;
void fre(){freopen("c:\\acm\\input.txt","r",stdin);}
LL search(LL l,LL r){
LL mid,sum;
while(l<=r){
mid=(l+r)>>,sum=;
FFC(i,,n){
sum+=a[i]*mid-b[i]>?a[i]*mid-b[i]:;
if(sum>k)break;//防爆LL
}
if(sum==k)return mid;
else if(sum<k)l=mid+;
else r=mid-;
}
return r;
}
int main(){
//fre();
while(~scanf("%I64d%I64d",&n,&k)){
FFC(i,,n)scanf("%I64d",&a[i]);
FFC(i,,n)scanf("%I64d",&b[i]);
printf("%I64d\n",search(,maxn));
}
return ;
}