#include <iostream>
#include <algorithm>
#include <vector>
int f1(const std::vector<int> &p, int target) {
int sol = 0, now;
std::vector<int>::const_iterator i = p.begin();
std::vector<int>::const_iterator j = p.begin();
i++; j++;
for (; i != p.end() && *i + *j <= target; ++i)
if (i != j)
sol = *i + *j;
if (i > p.begin() + 1)
--i;
else
return 0;
for (;;) {
// loop-out2
if (j >= p.end() - 1)
break;
if (*i + *(j + 1) <= target)
++j;
else
--i;
// loop-out1
if (i == p.begin())
break;
if (i != j && (now = *i + *j) > sol)
sol = now;
}
return sol;
}
int f2(std::vector<int> const &p, int const target) {
int size = p.size();
std::vector<std::vector<int> > table;
std::vector<int>::const_iterator i = p.begin();
for (;i != p.end(); ++i) {
std::vector<int> line;
std::vector<int>::const_iterator j = p.begin();
for (;j != p.end(); ++j)
line.push_back(*i + *j);
table.push_back(line);
}
int sol = 0;
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
if (i != j && table[i][j] <= target && table[i][j] > sol)
sol = table[i][j];
return sol;
}
#if 1
int main() {
int N, D, t;
std::vector<int> p, m;
std::cin >> N >> D;
if (N <= 1 || D == 0)
return 0;
p.reserve(N); m.reserve(D);
for (int i = 0; i < N; i++) { std::cin >> t; p.push_back(t); }
p.push_back(0);
for (int i = 0; i < D; i++) { std::cin >> t; m.push_back(t); }
std::sort(p.begin(), p.end());
std::vector<int>::const_iterator mIterator;
for (mIterator = m.begin(); mIterator != m.end(); mIterator++)
std::cout << f1(p, *mIterator) << std::endl;
return 0;
}
#else
#include <cstdlib>
#define N 10000
#define LIM 100000
int main() {
std::vector<int> p;
for (int seed = 0; seed < N; seed++) {
::srand(seed);
int n = ::rand() % 20;
int d = ::rand() % (LIM * 2);
if (n <= 1)
continue;
std::cout << seed;
for (int i = 0; i < n; i++)
p.push_back(::rand() % LIM);
std::sort(p.begin(), p.end());
int b = f2(p, d);
p.push_back(0);
std::sort(p.begin(), p.end());
int a = f1(p, d);
if (a != b)
std::cout << ":NG." << std::endl;
else
std::cout << ":OK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