-
Notifications
You must be signed in to change notification settings - Fork 0
/
EKO.cpp
47 lines (40 loc) · 847 Bytes
/
EKO.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
// https://www.spoj.com/problems/EKO/
// #Binary-Search
// Problem Name - EKO - Eko
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
bool isPossible(ll *a,ll n,ll m,ll mid){
ll temp = 0 ;
for(ll i=0;i<n;i++){
if(a[i] > mid){
temp = temp + a[i] - mid;
if(temp >= m)
return true;
}
}
return false;
}
ll maxHeight(ll *a, ll n, ll m){
ll s=0, e = LONG_MAX, ans = 0;
while(s<=e){
ll mid = (s+e)/2;
if(isPossible(a,n,m,mid)){
ans = mid;
s = mid + 1;
}else{
e = mid - 1;
}
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
ll n,m;
cin>>n>>m;
ll a[n];
for(ll i=0;i<n;i++)
cin>>a[i];
cout<<maxHeight(a,n,m)<<endl;
return 0;
}