// https://www.geeksforgeeks.org/given-two-sorted-arrays-number-x-find-pair-whose-sum-closest-x/
#include <iostream>
using namespace std;
int main() {
int n,m,x;
cin >> n >> m;
int a[n], b[m];
for (int i=0;i<n;i++)
cin>>a[i];
for (int i=0;i<m;i++)
cin>>b[i];
cout << "Enter the search element:";
cin>>x;
int i=0, j = m-1, s = INT_MAX, l ,r;
while (i<n && j >= 0){
int d = abs(x - a[i] - b[j]);
if ( d < s){
s = d;
l = i;
r = j;
}
if (a[i] + b[j] < x && a[i] < b[j])
i++;
else if (a[i] + b[j] < x && a[i] > b[j])
j++;
else if (a[i] + b[j] > x && a[i] < b[j])
j--;
else if (a[i] + b[j] > x && a[i] > b[j])
i--;
}
cout << a[l] << " and " << b[r];
}
// https://www.geeksforgeeks.org/given-two-sorted-arrays-number-x-find-pair-whose-sum-closest-x/
#include <iostream>
using namespace std;
int main() {
int n,m,x;
cin >> n >> m;
int a[n], b[m];
for (int i=0;i<n;i++)
cin>>a[i];
for (int i=0;i<m;i++)
cin>>b[i];
cout << "Enter the search element:";
cin>>x;
int i=0, j = m-1, s = INT_MAX, l ,r;
while (i<n && j >= 0){
int d = abs(x - a[i] - b[j]);
if ( d < s){
s = d;
l = i;
r = j;
}
if (a[i] + b[j] < x)
i++;
else
j--;
}
cout << a[l] << " and " << b[r];
}