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> maxvalue;

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

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

		return make_pair(max(vl, vr));
	}

public:

	Type INF;

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

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

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

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

		maxvalue[i] = x;

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

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

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

#endif


#include <iostream>

int main()
{
	int i, j;

	int power[16]; power[0] = 1;

	for (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 (i = 0; i < N; i++)
	{
		cin >> X[i] >> Y[i];
	}

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

	long long X3, Y3;

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

		for (j = 0; j < mid; j++)
		{
			switch ((i / power[j]) % 3)
			{
			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 (i = 0; i < power[N - mid]; i++)
	{
		X3 = 0LL;
		Y3 = 0LL;

		for (j = mid; j < N; j++)
		{
			switch ((i / power[j - mid]) % 3)
			{
			case 0: X3 -= X[j]; Y3 -= Y[j]; break; // Anna
			case 1: X3 += X[j]; Y3 += Y[j]; break; // Bruno
			}
		}

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

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

	int D1_size = D1.size();
	int D2_size = D2.size();

	vector<long long> X1, Y1;

	RMQ<long long> rmq(N != 30 ? D2_size : 0);

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

		if (N != 30)
		{
			rmq.update(i, D2[i].second);
		}
		else
		{
			Y1.push_back(D2[i].second);
		}
	}

	long long Ans = numeric_limits<long long>::min() / 2;

	int ptr1, ptr2;

	for (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));

		if (N != 30)
		{
			Ans = max(D1[i].second + rmq.query(ptr1, ptr2), Ans);
		}
		else
		{
			for (j = ptr1; j < ptr2; j++)
			{
				Ans = max(D1[i].second + Y1[j], Ans);
			}
		}
	}

	cout << Ans << endl;

	return 0;
}

Compiler output:
a.cc: In constructor 'RMQ<Type>::RMQ(int)':
a.cc:35:23: error: 'numeric_limits' was not declared in this scope
   35 |                 INF = numeric_limits<Type>::max() / 2;
      |                       ^~~~~~~~~~~~~~
a.cc:35:42: error: expected primary-expression before '>' token
   35 |                 INF = numeric_limits<Type>::max() / 2;
      |                                          ^
a.cc:35:48: error: no matching function for call to 'max()'
   35 |                 INF = numeric_limits<Type>::max() / 2;
      |                                           ~~~~~^~
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:153:25: error: 'numeric_limits' was not declared in this scope
  153 |         long long Ans = numeric_limits<long long>::min() / 2;
      |                         ^~~~~~~~~~~~~~
a.cc:153:40: error: expected primary-expression before 'long'
  153 |         long long Ans = numeric_limits<long long>::min() / 2;
      |                                        ^~~~
a.cc: In instantiation of 'Type RMQ<Type>::_query(int, int, int, int, int) [with Type = long long int]':
a.cc:58:10:   required from 'Type RMQ<Type>::query(int, int) [with Type = long long int]'
   58 |                 return _query(s, t, 0, 0, size);
      |                        ^~~~~~
a.cc:164:38:   required from here
  164 |                         Ans = max(D1[i].second + rmq.query(ptr1, ptr2), Ans);
      |                                                  ~~~~~~~~~^~~~~~~~~~~~
a.cc:26:33: error: no matching function for call to 'make_pair(const long long int&)'
   26 |                 return make_pair(max(vl, vr));
      |                        ~~~~~~~~~^~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:64:
/usr/include/c++/14/bits/stl_pair.h:1132:5: note: candidate: 'template<class _T1, class _T2> constexpr std::pair<typename std::__strip_reference_wrapper<typename std::decay<_Tp>::type>::__type, typename std::__strip_reference_wrapper<typename std::decay<_Tp2>::type>::__type> std::make_pair(_T1&&, _T2&&)'
 1132 |     make_pair(_T1&& __x, _T2&& __y)
      |     ^~~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:1132:5: note:   candidate expects 2 arguments, 1 provided