http://codeforces.com/problemset/problem/670/D2
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 100000000011000000000
output
2000000000
input
10 11000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 10000000001 1 1 1 1 1 1 1 1 1
output
0
input
3 12 1 411 3 16
output
4
input
4 34 3 5 611 12 14 20
output
3
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <vector> #include <algorithm> #include <string> #include <map> using namespace std; #define N 110000 #define met(a, b) memset(a, b, sizeof(a)) #define INF 0x3f3f3f3f const long long Max = 2000000000; typedef long long LL; LL a[N], b[N]; LL n, k; LL Judge(LL mid) { LL i, K1=k, K2=k; for(i=1; i<=n; i++) { if(b[i]<a[i]*mid) { K1 -= (a[i]*mid - b[i]); if(K1<0) return -1; } } for(i=1; i<=n; i++) { if(b[i]<a[i]*(mid+1)) { K2 -= (a[i]*(mid+1) - b[i]); if(K2<0) return 0; } } return 1; } int main() { while(scanf("%I64d%I64d", &n, &k)!=EOF) { LL i; LL mid, L=0, R=Max, ans; met(a, 0); met(b, 0); for(i=1; i<=n; i++) scanf("%I64d", &a[i]); for(i=1; i<=n; i++) scanf("%I64d", &b[i]); while(L<R) { mid = (L+R)/2; ans = Judge(mid); if(ans==0) L = R = mid; if(ans>0) L = mid + 1; if(ans<0) R = mid - 1; } printf("%I64d\n", L); } return 0; }