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. vector<int> consistency(int n, int k) {
  29.  
  30. //* Assuming a[0] < b[0]
  31. int s=0, e=n-1;
  32.  
  33. int maxi = -INF;
  34. int x = -1, y = -1;
  35.  
  36. while(s<n && e>=0){
  37. if(a[s] + b[e] > k){
  38. e--;
  39. }
  40. else{
  41. if(a[s] + b[e] > maxi){
  42. maxi = a[s] + b[e];
  43. x = s;
  44. y = e;
  45. }
  46. s++;
  47. }
  48. }
  49.  
  50.  
  51. return {a[x], b[y], maxi};
  52.  
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66. //* a[ ]
  67. //* b[ ]
  68.  
  69. vector<int> practice(int n, int k) {
  70.  
  71.  
  72. }
  73.  
  74.  
  75.  
  76.  
  77. void solve() {
  78.  
  79. int n, k;
  80. cin >> n >> k ;
  81. a.resize(n);
  82. b.resize(n);
  83. for(int i=0; i<n; i++) cin >> a[i];
  84. for(int i=0; i<n; i++) cin >> b[i];
  85.  
  86. auto ans1 = consistency(n, k);
  87.  
  88. cout << ans1[0] << "+" << ans1[1] << "=" << ans1[2] << endl;
  89.  
  90. // auto p = practice(n, k);
  91. // cout << "( " << ans1[0] << "+" << ans1[1] << "=" << ans1[2] << " ) -> ";
  92. // cout << "( " << p[0] << "+" << p[1] << "=" << p[2] << " )" << endl;
  93.  
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100.  
  101. int32_t main() {
  102. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  103.  
  104. int t = 1;
  105. cin >> t;
  106. while (t--) {
  107. solve();
  108. }
  109.  
  110. return 0;
  111. }
Success #stdin #stdout 0.01s 5324KB
stdin
2
5 11
2 5 8 10 15
3 5 8 8 10
4 22
1 4 5 7
10 20 30 40
stdout
8+3=11
1+20=21