fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int unsigned int
  4. signed main(){
  5. int n;
  6. long long k;
  7. cin >> n >> k;
  8. vector<int> a(n);
  9. vector<int> b(n);
  10. for(int i = 0; i < n; i++){
  11. cin >> a[i];
  12. }
  13. for(int i = 0; i < n; i++){
  14. cin >> b[i];
  15. }
  16. sort(a.begin(), a.end());
  17. sort(b.begin(), b.end());
  18. int l = 1, r = 2e9;
  19. while(l <= r){
  20. int mid = (l + r) / 2;
  21. long long sum = 0;
  22. for(int i = 0, j = 0; i < n; i++){
  23. while(a[i] + b[j] <= mid && j < n) j++;
  24. while(a[i] + b[j] > mid && j > 0) j--;
  25. // cout << j << endl;
  26. sum += j;
  27. }
  28. cout << l << " " << r << " " << sum << endl;
  29. if(sum > k){
  30. r = mid - 1;
  31. }else if(sum < k){
  32. l = mid + 1;
  33. }else{
  34. cout << mid;
  35. break;
  36. }
  37. }
  38. }
  39.  
  40. // 2 4 4 6 8
  41. // 1 3 5 7 9
  42.  
  43.  
  44.  
  45.  
  46.  
Success #stdin #stdout 0.01s 5276KB
stdin
5 10
4 2 6 4 8
7 3 1 9 5
stdout
1 2000000000 25
1 999999999 25
1 499999999 25
1 249999999 25
1 124999999 25
1 62499999 25
1 31249999 25
1 15624999 25
1 7812499 25
1 3906249 25
1 1953124 25
1 976561 25
1 488280 25
1 244139 25
1 122069 25
1 61034 25
1 30516 25
1 15257 25
1 7628 25
1 3813 25
1 1906 25
1 952 25
1 475 25
1 237 25
1 118 25
1 58 25
1 28 25
1 13 4
8 13 8
11 13 25
11 11 25