l=1 , r= 가장 큰 예산 금액 이분탐색 진행해서 총 예산안 초과하는지 체크
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int n=Integer.parseInt(br.readLine());
int[] arr=new int[n];
st=new StringTokenizer(br.readLine());
int max=-1;
for(int i=0;i<n;i++){
arr[i]=Integer.parseInt(st.nextToken());
max=Math.max(max,arr[i]);
}
int total=Integer.parseInt(br.readLine());
long l=1,r=max;
long mid=0,sum=0;
while(l<=r){
mid=(l+r)>>1;
sum=0;
for(int i=0;i<n;i++){
if(arr[i]<mid)sum+=arr[i];
else sum+=mid;
}
if(sum>total)r=mid-1;
else l=mid+1;
}
System.out.println(r);
}
}
#이분탐색