-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathD1 - Magic Powder - 1.cpp
54 lines (48 loc) · 1.33 KB
/
D1 - Magic Powder - 1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Problem Link : https://codeforces.com/contest/670/problem/D1
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
// will check that Apollinaria can bake 'number' cookies or not.
bool isPossible(int number, int a[], int b[], int n, int k){
for(int i = 0; i < n; i++){
if(b[i] < number*a[i]){
if(k < number*a[i]-b[i])
return false;
else
k -= number*a[i]-b[i];
}
}
return true;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k;
cin >> n >> k;
int a[n],b[n];
for(int i = 0; i < n; i++){
cin >> a[i];
}
for(int i = 0; i < n; i++){
cin >> b[i];
}
int l = 0,h = 0; // scope of cookies -> minimum Apollinaria can bake 0 and maximum she can bake h cookies.
for(int i = 0; i < n; i++){ // setting value of 'h' for binary search
if(a[i] > 0){
h = max(h,k+b[i]/a[i]);
}
}
int ans = 0;
while(l <= h){
int mid = l + (h-l)/2;
if(isPossible(mid, a, b, n, k)){ // it is possible to bake mid cookies so now i will try to maximize it and mark mid as possible answer.
ans = mid;
l = mid+1;
}else{
h = mid-1;
}
}
cout << ans << endl; // Print answer.
return 0;
}