#include <stdio.h>
#include <stdlib.h>
//나무 길이
long long tree_list[1000001];

int compare(const void *a, const void *b) {
    long long A=*(long long *)a;
    long long B=*(long long *)b;
    if (A>B) return 1;
    else return -1;
}
int main() {
    long long n,tree,start,end;
    scanf("%lld %lld",&n, &tree);
    for(int i=0; i<n; i++) {
        scanf("%lld",&tree_list[i]);
    }
    qsort(tree_list,n,sizeof(long long),compare);
    //이분탐색 나무 길이 최소 최대
    start=0;
    end=tree_list[n-1];
    
    while(start<=end) {
        long long mid = start +(end-start)/2;
        //자를 나무 길이 합
        long long res=0;
        //중간 나무 길이보다 긴 나무 모음
        for(int i=0; i<n; i++) {
            if(tree_list[i]>mid) {
                res+=tree_list[i]-mid;
            }
        }
        //나무를 더 많이 모았으면 기준점을 높여서 나무를 줄임
        if(res>=tree) start=mid+1;
        else end=mid-1;
    }
    printf("%lld",end);
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: