백준 2805번 문제는 이분검색을 알고리즘을 사용하면 되는데
쉬운듯 어렵운듯??
#include <stdio.h>
int tree[1000001];
int main(void){
int N, M;
int max = 0;
int left, right;
int result = 0;
scanf("%d %d", &N, &M);
for (int i = 0; i < N; i++){
scanf("%d", &tree[i]);
if (max < tree[i])
max = tree[i];
}
left = 0; right = max;
while (left <= right){
int mid = (left + right) / 2;
int total = 0;
for (int i = 0; i < N; i++)
if (mid < tree[i])
total += tree[i]-mid;
if (total >= M){
if (result<mid)
result = mid;
left = mid + 1;
}
else{
right = mid - 1;
}
}
printf("%d\n", result);
return 0;
}