forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
57 lines (45 loc) · 1.39 KB
/
B.cpp
File metadata and controls
57 lines (45 loc) · 1.39 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << X << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define ll long long
#define SZ(x) ((int) (x).size())
using namespace std;
const int MN = 200111;
int n, k, s[MN];
bool check(int each) {
multiset<int> all;
FOR(i,1,n) all.insert(s[i]);
REP(turn,k) {
if (all.empty()) break;
// Grab the largest
auto it = all.end(); --it;
int ln = *it; all.erase(it);
if (ln > each) return false;
if (all.empty()) break;
it = all.upper_bound(each - ln);
if (it == all.begin()) continue;
--it;
all.erase(it);
}
return all.empty();
}
int main() {
while (scanf("%d%d", &n, &k) == 2) {
FOR(i,1,n) scanf("%d", &s[i]);
int l = 1, r = 2000111;
int res = r;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
res = mid;
r = mid - 1;
}
else l = mid + 1;
}
cout << res << endl;
}
}