240504 예산

Jongleee·2024년 5월 4일
0

TIL

목록 보기
564/737
public static void main(String[] args) throws IOException {
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int n = Integer.parseInt(br.readLine());

	int left = 0;
	int right = -1;
	int[] arr = new int[n];
	StringTokenizer st = new StringTokenizer(br.readLine());
	for (int i = 0; i < n; i++) {
		arr[i] = Integer.parseInt(st.nextToken());
		if (right < arr[i])
			right = arr[i];
	}

	int m = Integer.parseInt(br.readLine());
	while (left <= right) {
		int mid = (left + right) / 2;
		long budget = 0;
		for (int i = 0; i < n; i++) {
			budget += (arr[i] > mid) ? mid : arr[i];
		}
		if (budget <= m) {
			left = mid + 1;
		} else {
			right = mid - 1;
		}
	}
	System.out.println(right);
}

출처:https://www.acmicpc.net/problem/2512

0개의 댓글