forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8.cpp
More file actions
38 lines (35 loc) Β· 1.09 KB
/
8.cpp
File metadata and controls
38 lines (35 loc) Β· 1.09 KB
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
#include <bits/stdc++.h>
using namespace std;
// λ‘μ κ°μ(N)μ μμ²ν λ‘μ κΈΈμ΄(M)
int n, m;
// κ° λ‘μ κ°λ³ λμ΄ μ 보
vector<int> arr;
int main(void) {
cin >> n >> m;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
arr.push_back(x);
}
// μ΄μ§ νμμ μν μμμ κ³Ό λμ μ€μ
int start = 0;
int end = 1e9;
// μ΄μ§ νμ μν (λ°λ³΅μ )
int result = 0;
while (start <= end) {
long long int total = 0;
int mid = (start + end) / 2;
for (int i = 0; i < n; i++) {
// μλμ λμ λ‘μ μ κ³μ°
if (arr[i] > mid) total += arr[i] - mid;
}
if (total < m) { // λ‘μ μμ΄ λΆμ‘±ν κ²½μ° λ λ§μ΄ μλ₯΄κΈ°(μΌμͺ½ λΆλΆ νμ)
end = mid - 1;
}
else { // λ‘μ μμ΄ μΆ©λΆν κ²½μ° λ μλ₯΄κΈ°(μ€λ₯Έμͺ½ λΆλΆ νμ)
result = mid; // μ΅λν λ μλμ λκ° μ λ΅μ΄λ―λ‘, μ¬κΈ°μμ resultμ κΈ°λ‘
start = mid + 1;
}
}
cout << result << '\n';
}