Language: C++

#ifndef ___Class_RMQ
#define ___Class_RMQ

#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

template <typename Type> class RMQ
{
private:

	int size;

	vector<Type> minvalue;
	vector<Type> maxvalue;

	pair<Type, Type> _query(int a, int b, int k, int l, int r)
	{
		if (r <= a || b <= l) { return make_pair(INF, -INF); }
		if (a <= l && r <= b) { return make_pair(minvalue[k], maxvalue[k]); }

		pair<Type, Type> vl = _query(a, b, 2 * k + 1, l, (l + r) / 2);
		pair<Type, Type> vr = _query(a, b, 2 * k + 2, (l + r) / 2, r);

		return make_pair(min(vl.first, vr.first), max(vl.second, vr.second));
	}

public:

	Type INF;

	RMQ(int size_)
	{
		INF = numeric_limits<Type>::max();

		for (size = 1; size < size_;) { size <<= 1; }

		minvalue = vector<Type>(size * 2 - 1, INF);
		maxvalue = vector<Type>(size * 2 - 1, -INF);
	}

	void update(int i, const Type& x)
	{
		i += size - 1;

		minvalue[i] = x;
		maxvalue[i] = x;

		while (i > 0)
		{
			i = (i - 1) / 2;

			minvalue[i] = min(minvalue[2 * i + 1], minvalue[2 * i + 2]);
			maxvalue[i] = max(maxvalue[2 * i + 1], maxvalue[2 * i + 2]);
		}
	}

	pair<Type, Type> query(int s, int t)
	{
		return _query(s, t, 0, 0, size);
	}
};

#endif


#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int power[16]; power[0] = 1;

	for (int i = 1; i < 16; i++)
	{
		power[i] = power[i - 1] * 3;
	}

	int N; long long D;

	cin >> N >> D;

	int mid = N / 2;

	vector<long long> X(N);
	vector<long long> Y(N);

	for (int i = 0; i < N; i++)
	{
		cin >> X[i] >> Y[i];
	}

	vector<pair<long long, long long> > D1, D2;

	for (int i = 0; i < power[mid]; i++)
	{
		long long X3 = 0LL, Y3 = 0LL;

		for (int j = 0; j < mid; j++)
		{
			int temp = (i / power[j]) % 3;

			switch (temp)
			{
			case 0: X3 -= X[j]; Y3 -= Y[j]; break; // Anna
			case 1: X3 += X[j]; Y3 += Y[j]; break; // Bruno
			}
		}

		D1.push_back(make_pair(X3, Y3));
	}

	for (int i = 0; i < power[N - mid]; i++)
	{
		long long X3 = 0LL, Y3 = 0LL;

		for (int j = mid; j < N; j++)
		{
			int temp = (i / power[j - mid]) % 3;

			switch (temp)
			{
			case 0: X3 -= X[j]; Y3 -= Y[j]; break; // Anna
			case 1: X3 += X[j]; Y3 += Y[j]; break; // Bruno
			}
		}

		tmp.first = X3;
		tmp.second = Y3;

		D2.push_back(tmp);
	}

	sort(D2.begin(), D2.end(), [](pair<long long, long long> x, pair<long long, long long> y) { return x.first < y.first; });

	vector<long long> X1;

	RMQ<long long> Y1(D2.size());

	for (int i = 0; i < D2.size(); i++)
	{
		X1.push_back(D2[i].first);

		Y1.update(i, D2[i].second);
	}

	long long Ans = -(1LL << 60);

	int ptr1, ptr2, it;

	for (int i = 0; i < D1.size(); i++)
	{
		ptr1 = distance(X1.begin(), lower_bound(X1.begin(), X1.end(), -D1[i].first - D));
		ptr2 = distance(X1.begin(), upper_bound(X1.begin(), X1.end(), -D1[i].first + D));

		long long rmq = Y1.query(ptr1, ptr2).second;

		if (rmq != -Y1.INF)
		{
			Ans = max(D1[i].second + rmq, Ans);
		}
	}

	cout << Ans << endl;

	return 0;
}

Compiler output:
a.cc: In constructor 'RMQ<Type>::RMQ(int)':
a.cc:36:23: error: 'numeric_limits' was not declared in this scope
   36 |                 INF = numeric_limits<Type>::max();
      |                       ^~~~~~~~~~~~~~
a.cc:36:42: error: expected primary-expression before '>' token
   36 |                 INF = numeric_limits<Type>::max();
      |                                          ^
a.cc:36:48: error: no matching function for call to 'max()'
   36 |                 INF = numeric_limits<Type>::max();
      |                                           ~~~~~^~
In file included from /usr/include/c++/14/vector:62,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   candidate expects 2 arguments, 0 provided
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 0 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:6:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 0 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   candidate expects 2 arguments, 0 provided
a.cc: In function 'int main()':
a.cc:133:17: error: 'tmp' was not declared in this scope; did you mean 'tm'?
  133 |                 tmp.first = X3;
      |                 ^~~
      |                 tm