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

using namespace std;
int n;
long long total;
int res=0;
int x[10005];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    cin >> n;
    for(int i=0; i<n; i++) {
        cin >> x[i];
    }
    cin >> total;
    int st=0,en=*max_element(x,x+n);
    while(st<=en) {
        int mid=(st+en)/2;
        long long my_total=0;
        for(int i=0; i<n; i++){
            if(x[i]>mid) my_total+=mid;
            else my_total+=x[i];
        }

        
        if(my_total > total) {
            en = mid - 1;
        } else {
            st = mid + 1;
        }


        //if(my_total<=total) {
        //    ans = mid;
        //    st=mid+1;
        //} else en=mid-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: