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 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. return {a[x], b[y], maxSum};
  52.  
  53. }
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. vector<int> practice(int n, int k) {
  69.  
  70.  
  71.  
  72. return {};
  73. }
  74.  
  75.  
  76.  
  77.  
  78. void solve() {
  79.  
  80. int n, k;
  81. cin >> n >> k ;
  82. a.resize(n);
  83. b.resize(n);
  84. for(int i=0; i<n; i++) cin >> a[i];
  85. for(int i=0; i<n; i++) cin >> b[i];
  86.  
  87. auto ans1 = consistency(n, k);
  88.  
  89. cout << ans1[0] << "+" << ans1[1] << "=" << ans1[2] << endl;
  90.  
  91. // auto p = practice(n, k);
  92. // cout << "( " << ans1[0] << "+" << ans1[1] << "=" << ans1[2] << " ) -> ";
  93. // cout << "( " << p[0] << "+" << p[1] << "=" << p[2] << " )" << endl;
  94.  
  95.  
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. int32_t main() {
  103. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  104.  
  105. int t = 1;
  106. cin >> t;
  107. while (t--) {
  108. solve();
  109. }
  110.  
  111. return 0;
  112. }
Success #stdin #stdout 0s 5332KB
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