您的位置:首页 > 其它

Codeforces Round #350 (Div. 2) D2. Magic Powder - 2 670D 【二分答案】

2016-07-20 11:19 351 查看
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<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define rep(i,l,r) for(int i = l;i<=r;i++)
#define Rep(i,l,r) for(int i = l;i<r;i++)
using namespace std;
const int maxn = 100010;
int n,k;
typedef long long ll;
int a[maxn],b[maxn];

/*bool check(int cnt)
{
ll kk = k;
Rep(i,0,n){
ll use = (ll)a[i]*cnt-b[i];
if(use<=0)continue;
else{
if(kk<use)return false;
else kk-=use;
}
}
return true;
}*/
bool check(int cnt) {
int kk = k;

for (int i = 0; i < n; ++i) {
ll owe = (ll)a[i] * cnt - b[i];

if (owe <= 0) continue;
else {
if (kk < owe) return false;
else kk -= owe;
}
}

return true;
}

void ir() {
scanf("%d%d", &n, &k);

for (int i = 0; i < n; ++i) scanf("%d", &a[i]);
for (int i = 0; i < n; ++i) scanf("%d", &b[i]);
}

int main()
{
scanf("%d%d",&n,&k);
Rep(i,0,n)scanf("%d",a+i);
Rep(i,0,n)scanf("%d",b+i);
int m = 0;Rep(i,0,n)m = max(m, (int)((b[i] + k) / a[i]) + 1);
int l = 0,r = m;
// [i,r)
while(r-l>1)
{
int mid = ((ll)r+l)>>1;
if(check(mid)){
l = mid;
}else{
r = mid;
}
}
printf("%d",l);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: