fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. signed main(){
  4. long long n, k;
  5. cin >> n >> k;
  6. vector<int> a(n);
  7. vector<int> b(n);
  8. for(int i = 0; i < n; i++){
  9. cin >> a[i];
  10. }
  11. for(int i = 0; i < n; i++){
  12. cin >> b[i];
  13. }
  14. sort(a.begin(), a.end());
  15. sort(b.begin(), b.end());
  16. long long l = 1, r = 2e9;
  17. while(l <= r){
  18. long long mid = (l + r) / 2;
  19. int sum = 0;
  20. for(int i = 0, j = n - 1; i < n; i++){
  21. while(a[i] + b[j] > mid && j > 0) j--;
  22. sum += j + 1;
  23. }
  24. if(sum > k){
  25. r = mid - 1;
  26. }else if(sum < k){
  27. l = mid + 1;
  28. }
  29. }
  30. cout << l;
  31. }
  32. // 2 4 4 6 8
  33. // 1 3 5 7 9
  34.  
Success #stdin #stdout 0.01s 5272KB
stdin
5 10
4 2 6 4 8
7 3 1 9 5
stdout
9