Language: C++


#include <cstdio>
#include <cassert>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <limits>

//vector,limits?????????
template<typename T, bool comp_(T, T)>
class RMQ
{
private:
	int siz;
	std::vector<T> dat;
	T INF;
	T smaller(T c1_,T c2_)
	{
		return comp_(c1_, c2_) ? c1_ : c2_;
	}
	T rawq(int a_, int b_, int k_, int l_, int r_)
	{
		if (r_ <= a_ || b_ <= l_)return INF;
		if (a_ <= l_ && r_ <= b_)return dat[k_];
		else
		{
			T vl = rawq(a_, b_, k_ * 2 + 1, l_, (l_ + r_) / 2);
			T vr = rawq(a_, b_, k_ * 2 + 2, (l_ + r_) / 2, r_);
			return smaller(vl, vr);
		}
	}
public:
	RMQ(int n_)
	{
		siz = 1;
		INF = comp_(std::numeric_limits<T>::max(), std::numeric_limits<T>::min()) ? std::numeric_limits<T>::min() : std::numeric_limits<T>::max();
		while (siz < n_)
		{
			siz *= 2;
		}
		dat.resize(2 * siz + 1);
		for (int i = 0; i < 2 * siz + 1; ++i)dat[i] = INF;
	}
	void update(int k_, T a_)
	{
		k_ += siz - 1;
		dat[k_] = a_;
		while (k_ > 0)
		{
			k_ = (k_ - 1) / 2;
			dat[k_] = smaller(dat[k_ * 2 + 1], dat[k_ * 2 + 2]);
		}
	}
	T query(int a, int b)
	{
		return rawq(a, b, 0, 0, siz);
	}
};

using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

#define In_(x) scanf("%lld",&x)

ll n, d;
ll three[16] = {1};
vector<pll> xy;
vector<pll>half1;
vector<pll>half2;

bool great(ll c1_, ll c2_) { return c1_ > c2_; }
RMQ < ll, great> rmq(14348907);

int main()
{
	for (ll i = 1; i <= 15; ++i)three[i] = three[i - 1] * 3;
	In_(n); In_(d);
	for (ll i = 0; i < n; ++i)
	{
		ll x, y;
		In_(x);
		In_(y);
		xy.emplace_back(x, y);
	}
	ll half = n / 2;
	half1.resize(three[half]);
	for (ll hsh = 0; hsh < three[half]; ++hsh)
	{
		ll market = 0;
		ll value = 0;
		for (ll i = 0; i < half; ++i)
		{
			switch ((hsh / three[i]) % 3)
			{
			case 0:break;
			case 1:market -= xy[i].first; value -= xy[i].second; break;
			case 2:market += xy[i].first; value += xy[i].second; break;
			default:assert(false); break;
			}
		}
		half1[hsh] = { market, value };
	}
	half2.resize(three[n - half]);
	//rmq = RMQ<ll, great>(three[n - half]);
	for (ll hsh = 0; hsh < three[n - half]; ++hsh)
	{
		ll market = 0;
		ll value = 0;
		for (ll i = 0; i < n - half; ++i)
		{
			switch ((hsh / three[i]) % 3)
			{
			case 0:break;
			case 1:market -= xy[i + half].first; value -= xy[i + half].second; break;
			case 2:market += xy[i + half].first; value += xy[i + half].second; break;
			default:assert(false); break;
			}
		}
		half2[hsh] = { market, value };
	}
	sort(half1.begin(), half1.end());
	sort(half2.begin(), half2.end());
	for (int i = 0; i < half2.size(); ++i)
	{
		rmq.update(i, half2[i].second);
	}
	ll ans = 0;
	int count = 0;
	for (pll &s : half1)
	{
		ll market = s.first;
		ll marmin = -market - d;
		ll marmax = -market + d;
		int left = lower_bound(half2.begin(), half2.end(), make_pair(marmin ,LLONG_MIN)) - half2.begin();
		int right = upper_bound(half2.begin(), half2.end(), make_pair(marmax, LLONG_MAX)) - half2.begin();
		if (left != right)
		{
			ans = max(ans, rmq.query(left, right) + s.second);
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:136:86: error: 'LLONG_MIN' was not declared in this scope
  136 |                 int left = lower_bound(half2.begin(), half2.end(), make_pair(marmin ,LLONG_MIN)) - half2.begin();
      |                                                                                      ^~~~~~~~~
a.cc:8:1: note: 'LLONG_MIN' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
    7 | #include <set>
  +++ |+#include <climits>
    8 | #include <limits>
a.cc:137:87: error: 'LLONG_MAX' was not declared in this scope
  137 |                 int right = upper_bound(half2.begin(), half2.end(), make_pair(marmax, LLONG_MAX)) - half2.begin();
      |                                                                                       ^~~~~~~~~
a.cc:137:87: note: 'LLONG_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'