[백준] - 2805/나무자르기

2020. 1. 8. 01:22Algorithm

4 7
20 15 10 17
15
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;

public class Main_2805 {

	public static void main(String[] args) throws FileNotFoundException {
		System.setIn(new FileInputStream("src/input.txt"));
		Scanner sc = new Scanner(System.in);
		int N= sc.nextInt();
		int M=sc.nextInt();
		int[] arr= new int[N];
		for(int i=0;i<N;i++) {
			arr[i]=sc.nextInt();
		}
		Arrays.sort(arr);
		int ans=0;
		
		long sum=0;
		int start=0;
		int end=arr[N-1];
		while(true) {
			int middle=(start+end)/2;
			sum=0;
			for(int i=0;i<N;i++) {
				long tmp = arr[i]-middle;
				if(tmp>=0) {
					sum+=tmp;
				}
			}
			if(sum>=M) {
				start=middle+1;
			}else if(sum<M) {
				end=middle-1;
			}
			if(start>end) {
				break;
			}
		}
		System.out.println(end);
	}
}


 

'Algorithm' 카테고리의 다른 글

[BOJ/17135] - 캐슬 디펜스  (0) 2020.06.06
[BOJ/17136] - 색종이 붙이기  (0) 2020.06.06
[BOJ/2146] - 다리 만들기  (0) 2020.06.06
[BOJ/17822] - 원판돌리기  (0) 2020.06.05
[알고리즘] 세그먼트 트리 (indexed tree)  (0) 2020.01.09