2805번: 나무 자르기

📝 Memo


Memory : 5020 KB

Time : 224 ms


🖥️ Code


#include<stdio.h>
#pragma warning(disable:4996)

int tree[1000001];
int main() {
	int N, M;
	scanf("%d %d", &N, &M);
	long long int sum;
	int cut,bot,top;
	for (int i = 0; i < N; i++) {
		scanf("%d", &tree[i]);
	}
	bot = 0;
	top = 1000000001;
	while (bot+1 != top) {
		sum = 0;
		cut = (bot + top) / 2;
		for (int i = 0; i < N; i++) {
			if (tree[i] > cut) {
				sum += tree[i]-cut;
			}
		}
		if (sum >= M) {
			bot = cut;
		}
		else {
			top = cut;
		}
	}
	printf("%d", bot);
}