#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n,target;
    cin >> n >> target;
    vector<int> tree(n);
    for(int i=0; i<n; i++) {
        cin >> tree[i];
    }
    sort(tree.begin(),tree.end());
    int st=0;
    int en=tree[n-1];
    while(st<=en) {
        int cut = (st+en)/2;
        long long total=0;
        for(auto c:tree) {
            if(c>cut) total+=c-cut;
        }

        if(total>=target) st=cut+1;
        else en=cut-1;
    }
    cout << en;
}

Embed on website

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