-
Notifications
You must be signed in to change notification settings - Fork 0
/
2805_나무자르기.cpp
64 lines (46 loc) · 846 Bytes
/
2805_나무자르기.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
using namespace std;
vector<int> v;
int N, M;
long long cuttingTree(long cut) {
long long sum = 0;
for (int i = 0; i < N; i++) {
if (v[i] - cut > 0) {
sum += v[i] - cut;
}
}
return sum;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long start, end, mid;
long result = 0;
cin >> N >> M;
for (int i = 0; i < N; i++) {
int tree_height;
cin >> tree_height;
v.push_back(tree_height);
}
sort(v.begin(), v.end());
start = 1;
end = v.back();
while (start <= end) {
mid = (start + end) / 2;
long long tree= cuttingTree(mid);
if (tree < M) {
end = mid - 1;
}
else {
start = mid + 1;
result = max(result, mid);
}
}
cout << result;
return 0;
}