Language: C++

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
long long n, d, power[17]; pair<long long, long long>a[30];
vector<pair<long long, long long> > vec1, vec2, c1, c2;
// ------ RMQ Class ------ //
template<typename Type>
class RMQ {
private:
	int size_; std::vector<Type> dat;
public:
	RMQ() : size_(0), dat(std::vector<Type>()) {};
	RMQ(int size__) {
		for (size_ = 1; size_ < size__; ) size_ <<= 1;
		dat.resize(size_ << 1, std::numeric_limits<Type>::max());
	}
	template<class T>
	RMQ(T begin_, T end_) {
		int n = end_ - begin_;
		for (size_ = 1; size_ < n; size_ <<= 1); dat.resize(size_ << 1, std::numeric_limits<Type>::max());
		for (int i = 0; i < n; i++) dat[i + size_] = *(begin_ + i);
		for (int i = size_ - 1; i > 0; i--) dat[i] = std::min(dat[i << 1], dat[(i << 1) + 1]);
	}
	inline unsigned size() { return size_; }
	inline void update(unsigned i, Type x) {
		i += size_; dat[i] = x;
		while (i > 1) {
			i >>= 1;
			dat[i] = std::min(dat[i << 1], dat[i << 1 | 1]);
		}
	}
	inline Type query(unsigned l, unsigned r) {
		Type ret = std::numeric_limits<Type>::max();
		l += size_; r += size_;
		while (l != r) {
			if (l & 1) ret = std::min(ret, dat[l++]); l >>= 1;
			if (r & 1) ret = std::min(ret, dat[--r]); r >>= 1;
		}
		return ret;
	}
};
int main() {
	cin >> n >> d;
	for (int i = 0; i < n; i++)cin >> a[i].first >> a[i].second;
	int Mid = n / 2; sort(a, a + n);
	for (int i = 0; i < Mid; i++)vec1.push_back(make_pair(a[i].first, a[i].second));
	for (int i = Mid; i < n; i++)vec2.push_back(make_pair(a[i].first, a[i].second));
	power[0] = 1; for (int i = 1; i < 17; i++)power[i] = power[i - 1] * 3;

	for (int i = 0; i < power[vec1.size()]; i++) {
		long long Ax = 0, Bx = 0, Ay = 0, By = 0;
		for (int j = 0; j < vec1.size(); j++) {
			int Y = (i / power[j]) % 3;
			if (Y == 0) { Ax += vec1[j].first; Ay += vec1[j].second; }
			if (Y == 1) { Bx += vec1[j].first; By += vec1[j].second; }
		}
		c1.push_back(make_pair(Ax - Bx, Ay - By));
	}
	for (int i = 0; i < power[vec2.size()]; i++) {
		long long Ax = 0, Bx = 0, Ay = 0, By = 0;
		for (int j = 0; j < vec2.size(); j++) {
			int Y = (i / power[j]) % 3;
			if (Y == 0) { Ax += vec2[j].first; Ay += vec2[j].second; }
			if (Y == 1) { Bx += vec2[j].first; By += vec2[j].second; }
		}
		c2.push_back(make_pair(Ax - Bx, Ay - By));
	}
	sort(c2.begin(), c2.end());
	RMQ<long long> seg(c2.size());
	for (int i = 0; i < c2.size(); i++)seg.update(i, c2[i].second);
	long long minx = 1LL << 62;
	for (int i = 0; i < c1.size(); i++) {
		int pos1 = lower_bound(c2.begin(), c2.end(), make_pair(-c1[i].first - d, -1LL << 62)) - c2.begin();
		int pos2 = lower_bound(c2.begin(), c2.end(), make_pair(-c1[i].first + d + 1, -1LL << 62)) - c2.begin();
		if (pos1 < pos2) {
			long long pos3 = seg.query(pos1, pos2);
			minx = min(c1[i].second + pos3, minx);
		}
	}
	cout << -minx << endl;
	return 0;
}

Compiler output:
a.cc: In constructor 'RMQ<Type>::RMQ(int)':
a.cc:16:45: error: 'numeric_limits' is not a member of 'std'
   16 |                 dat.resize(size_ << 1, std::numeric_limits<Type>::max());
      |                                             ^~~~~~~~~~~~~~
a.cc:16:64: error: expected primary-expression before '>' token
   16 |                 dat.resize(size_ << 1, std::numeric_limits<Type>::max());
      |                                                                ^
a.cc:16:70: error: no matching function for call to 'max()'
   16 |                 dat.resize(size_ << 1, std::numeric_limits<Type>::max());
      |                                                                 ~~~~~^~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/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:3:
/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 constructor 'RMQ<Type>::RMQ(T, T)':
a.cc:21:86: error: 'numeric_limits' is not a member of 'std'
   21 |                 for (size_ = 1; size_ < n; size_ <<= 1); dat.resize(size_ << 1, std::numeric_limits<Type>::max());
      |                                                                                      ^~~~~~~~~~~~~~
a.cc:21:105: error: expected primary-expression before '>' token
   21 |                 for (size_ = 1; size_ < n; size_ <<= 1); dat.resize(size_ << 1, std::numeric_limits<Type>::max());
      |                                                                                                         ^
a.cc:21:111: error: no matching function for call to 'max()'
   21 |                 for (size_ = 1; size_ < n; size_ <<= 1); dat.resize(size_ << 1, std::numeric_limits<Type>::max());
      |                                                                                                          ~~~~~^~
/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
/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 member function 'Type RMQ<Type>::query(unsigned int, unsigned int)':
a.cc:34:33: error: 'numeric_limits' is not a member of 'std'
   34 |                 Type ret = std::numeric_limits<Type>::max();
      |                                 ^~~~~~~~~~~~~~
a.cc:34:52: error: expected primary-expression before '>' token
   34 |                 Type ret = std::numeric_limits<Type>::max();
      |                                                    ^
a.cc:34:58: error: no matching function for call to 'max()'
   34 |                 Type ret = std::numeric_limits<Type>::max();
      |                                                     ~~~~~^~
/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
/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