forked from snigdha920/Codeforces-edu-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kth-Sum-Step-5-C.cpp
46 lines (43 loc) · 860 Bytes
/
Kth-Sum-Step-5-C.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
#include <bits/stdc++.h>
using namespace std;
typedef long long int lli;
int n, k;
vector <lli> a, b;
bool calc( lli x ) {
lli cnt = 0;
int i = 0, j = 0;
while( i < n && j < n) {
if( a[i] + b[j] >= x) {
j++;
} else {
i++;
cnt += (n-j);
}
}
return cnt < k;
}
int main() {
cin >> n >> k;
a.resize(n);
b.resize(n);
for(int i = 0; i < n; i++) {
cin >> a[i];
}
for(int i = 0; i < n; i++) {
cin >> b[i];
}
sort(a.begin(), a.end());
sort(b.begin(), b.end(), greater<lli>());
lli l = 1, r = 2e9 + 10;
for (int i = 0; i < 80; i++) {
lli mid = (l + r)/2;
if( calc(mid) ) {
l = mid;
} else {
r = mid;
}
}
cout << l << endl;
// cout << calc(9);
return 0;
}