-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2805.cpp
More file actions
48 lines (41 loc) · 854 Bytes
/
2805.cpp
File metadata and controls
48 lines (41 loc) · 854 Bytes
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
#include <iostream>
#include <vector>
#include <algorithm>
#define io ios::sync_with_stdio(false); cin.tie(0);
using namespace std;
int N,M;
long long res=0;
long long cutTree(int h,vector<int> tree){
long long sum=0;
for(int i=0;i<N;i++){
if(tree[i]>h) sum+=tree[i]-h;
}
return sum;
}
int main()
{
io;
cin>>N>>M;
int max_tree=0;
vector<int> tree(N);
for(int i=0;i<N;i++){
cin>>tree[i];
if(max_tree<tree[i]) max_tree=tree[i];
}
int left=max_tree-M,right=max_tree;
int mid=(left+right)/2;
while(left<=right){
long long sum=cutTree(mid,tree);
if(sum>=M){
if(mid>res){
res=mid;
}
left=mid+1;
}else{
right=mid-1;
}
mid=(left+right)/2;
}
cout<<res;
return 0;
}