fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26. vector<int> b;
  27.  
  28. void consistency(int n, int k) {
  29.  
  30. //* Assuming a[0] < b[0]
  31. int i=0, j=n-1;
  32.  
  33. int maxSum = 0;
  34. int x = -1, y = -1;
  35.  
  36. while(i<n && j>=0){
  37. int sum = a[i] + b[j];
  38. if(sum <= k){
  39. if(sum>maxSum){
  40. maxSum = sum;
  41. x = i;
  42. y = j;
  43. }
  44. i++;
  45. }
  46. else if(sum >= k){
  47. j--;
  48. }
  49. }
  50.  
  51. cout << maxSum << " => " << a[x] << " , " << b[y] << endl;
  52.  
  53. }
  54.  
  55.  
  56. void solve() {
  57.  
  58. int n, k;
  59. cin >> n >> k ;
  60. a.resize(n);
  61. b.resize(n);
  62. for(int i=0; i<n; i++) cin >> a[i];
  63. for(int i=0; i<n; i++) cin >> b[i];
  64.  
  65. consistency(n, k);
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73. int32_t main() {
  74. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  75.  
  76. int t = 1;
  77. // cin >> t;
  78. while (t--) {
  79. solve();
  80. }
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0.01s 5280KB
stdin
5 11
2 5 8 10 15
3 5 8 8 10
stdout
11 => 8 , 3