Language: C++

#include<iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <iterator>
#define int long long
#define INF 1e18
using namespace std;
typedef pair<int, int> P;
signed main() {
	int n, d, ans = 0;
	int x[31], y[31];
	map<int, int> dp[2];
	vector<P> d1, d2;
	cin >> n >> d;
	for (int i = 1; i <= n; i++) {
		cin >> x[i] >> y[i];
	}
	dp[0][0] = 0;
	for (int i = 1; i <= 15; i++) {
		for (map<int, int>::iterator itr = dp[0].begin(); itr != dp[0].end(); itr++) {
			if (!dp[1][itr->first + x[i]] || dp[1][itr->first + x[i]] < itr->second + y[i])
				dp[1][itr->first + x[i]] = itr->second + y[i];
			if (!dp[1][itr->first - x[i]] || dp[1][itr->first - x[i]] < itr->second - y[i])
				dp[1][itr->first - x[i]] = itr->second - y[i];
			if (!dp[1][itr->first] || dp[1][itr->first] < itr->second)
				dp[1][itr->first] = itr->second;
		}

		if (n <= i) {
			for (map<int, int>::iterator itr = dp[1].begin(); itr != dp[1].end(); itr++) {
				if (itr->first >= -d && itr->first <= d) {
					ans = max(itr->second, ans);
				}
			}
			cout << ans << endl;
			return 0;
		}
		copy(dp[1].begin(), dp[1].end(), dp[0]);
		dp[1].erase(dp[1].begin(), dp[1].end());
	}

	for (map<int, int>::iterator itr = dp[15].begin(); itr != dp[15].end(); itr++) {
		d1.push_back(make_pair(itr->second, itr->first));
	}

	for (int i = 1; i <= 15; i++)
		dp[i].erase(dp[i].begin(), dp[i].end());

	for (int i = 16; i <= n; i++) {
		for (map<int, int>::iterator itr = dp[0].begin(); itr != dp[0].end(); itr++) {
			if (!dp[1][itr->first + x[i]] || dp[1][itr->first + x[i]] < itr->second + y[i])
				dp[1][itr->first + x[i]] = itr->second + y[i];
			if (!dp[1][itr->first - x[i]] || dp[1][itr->first - x[i]] < itr->second - y[i])
				dp[1][itr->first - x[i]] = itr->second - y[i];
			if (!dp[1][itr->first] || dp[1][itr->first] < itr->second)
				dp[1][itr->first] = itr->second;
		}
		copy(dp[1].begin(), dp[1].end(), dp[0]);
		dp[1].erase(dp[1].begin(), dp[1].end());
	}

	for (map<int, int>::iterator itr = dp[15].begin(); itr != dp[15].end(); itr++) {
		d2.push_back(make_pair(itr->second, itr->first));
	}

	sort(d1.begin(), d1.end());
	sort(d2.begin(), d2.end());

	for (int i = 0; i < d1.size(); i++) {
		P p = make_pair(d - d1[i].first + 1, -INF);
		int itr = lower_bound(d2.begin(), d2.end(), p) - d2.begin();
		itr--;

		for (; itr >= 0; itr--) {
			if (d2[itr].first + d1[i].first >= -d)
				ans = max(ans, d2[itr].second + d1[i].second);
		}
	}
	cout << ans << endl;
	return 0;
}

Compiler output:
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: In instantiation of 'static _OI std::__copy_move<_IsMove, _IsSimple, _Category>::__copy_m(_II, _II, _OI) [with _II = std::_Rb_tree_iterator<std::pair<const long long int, long long int> >; _OI = std::map<long long int, long long int>; bool _IsMove = false; bool _IsSimple = false; _Category = std::bidirectional_iterator_tag]':
/usr/include/c++/14/bits/stl_algobase.h:521:30:   required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = _Rb_tree_iterator<pair<const long long int, long long int> >; _OI = map<long long int, long long int>]'
  520 |       return std::__copy_move<_IsMove, __memcpyable<_OI, _II>::__value,
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  521 |                               _Category>::__copy_m(__first, __last, __result);
      |                               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:548:42:   required from '_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = _Rb_tree_iterator<pair<const long long int, long long int> >; _OI = map<long long int, long long int>]'
  548 |     { return std::__copy_move_a2<_IsMove>(__first, __last, __result); }
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:556:31:   required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = _Rb_tree_iterator<pair<const long long int, long long int> >; _OI = map<long long int, long long int>]'
  556 |                 std::__copy_move_a1<_IsMove>(std::__niter_base(__first),
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  557 |                                              std::__niter_base(__last),
      |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~
  558 |                                              std::__niter_base(__result)));
      |                                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:651:7:   required from '_OI std::copy(_II, _II, _OI) [with _II = _Rb_tree_iterator<pair<const long long int, long long int> >; _OI = map<long long int, long long int>]'
  650 |       return std::__copy_move_a<__is_move_iterator<_II>::__value>
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  651 |              (std::__miter_base(__first), std::__miter_base(__last), __result);
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:39:7:   required from here
   39 |                 copy(dp[1].begin(), dp[1].end(), dp[0]);
      |                 ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:370:37: error: no match for 'operator++' (operand type is 'std::map<long long int, long long int>')
  370 |           for (; __first != __last; ++__result, (void)++__first)
      |                                     ^~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:371:13: error: no match for 'operator*' (operand type is 'std::map<long long int, long long int>')
  371 |             *__result = *__first;
      |             ^~~~~~~~~