https://www.acmicpc.net/problem/2512
#include<stdio.h>
int main() {
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++) scanf("%d",&arr[i]);
int max;
scanf("%d",&max);
int tmp;
for(int i=0;i<n-1;i++){
for(int j=i;j<n;j++){
if(arr[i]>arr[j]){
tmp=arr[i];
arr[i]=arr[j];
arr[j]=tmp;
}
}
}
int left=0;
int right=arr[n-1];
while(left<=right){
int mid=(left+right)/2;
int now=0;
for(int i=0;i<n;i++){
if(mid>=arr[i]) now+=arr[i];
else now+=mid;
}
if(now>max) right=mid-1;
else left=mid+1;
}
printf("%d",right);
}