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, sum[31] = {}, sum2[31] = {};
	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];
		sum[i] = x[i] + sum[i - 1];
	}
	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 (n <= 15 || itr->first - x[i] + sum[15] - sum[i] > 0) {
				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);
				}
				if (-itr->first >= -d && -itr->first <= d) {
					ans = max(-itr->second, ans);
				}
			}
			cout << ans << endl;
			return 0;
		}
		for (map<int, int>::iterator itr = dp[1].begin(); itr != dp[1].end(); itr++)
			dp[0][itr->first] = itr->second;
		dp[1].erase(dp[1].begin(), dp[1].end());
	}

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

	dp[0].erase(dp[0].begin(), dp[0].end());
	dp[0][0] = 0;
	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 (itr->first - x[i] + sum[n] - sum[i] >= 0)
				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;

		}
		for (map<int, int>::iterator itr = dp[1].begin(); itr != dp[1].end(); itr++)
			dp[0][itr->first] = itr->second;
		dp[1].erase(dp[1].begin(), dp[1].end());
	}

	for (map<int, int>::iterator itr = dp[0].begin(); itr != dp[0].end(); itr++) {
		d2.push_back(make_pair(itr->first, itr->second));
	}
	dp[0].erase(dp[0].begin(), dp[0].begin());
	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].second , INF);
		int itr = upper_bound(d2.begin(), d2.end(), p) - d2.begin();
		int itr2 = itr;
		itr--;
		for (; itr >= 0; itr--) {
			if (d2[itr].first + d1[i].first >= -d)
				ans = max(ans, d2[itr].second - d1[i].second);
			else
				break;
		}
		p = make_pair(d - d1[i].second, INF);
		bool b = 0;
		if (p.first < 0) {
			p.first *= -1;
			b = 1;
		}
		int itr = upper_bound(d2.begin(), d2.end(), p) - d2.begin();
		int itr2 = itr;
		itr--;
		for (; itr >= 0; itr--) {
			if (b) {
				if (-d2[itr].first + d1[i].second >= -d)
					ans = max(ans, -d2[itr].second + d1[i].first * - 1);
				else
					break;
			}
			else {
				if (d2[itr].first + d1[i].second >= -d)
					ans = max(ans, -d2[itr].second + d1[i].first * - 1);
				else
					break;
			}
		}
		d2.erase(d2.begin() + itr + 1, d2.begin() + itr2);
	}
	for (int i = 0; i < d1.size(); i++) {
		P p = make_pair(d + d1[i].second, INF);
		int itr = upper_bound(d2.begin(), d2.end(), p) - d2.begin();
		int itr2 = itr;
		itr--;
		for (; itr >= 0; itr--) {
			if (d2[itr].first + d1[i].first >= -d)
				ans = max(ans, d2[itr].second - d1[i].second);
			else
				break;
		}
	}
	cout << ans << endl;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:97:21: error: redeclaration of 'long long int itr'
   97 |                 int itr = upper_bound(d2.begin(), d2.end(), p) - d2.begin();
      |                     ^~~
a.cc:82:21: note: 'long long int itr' previously declared here
   82 |                 int itr = upper_bound(d2.begin(), d2.end(), p) - d2.begin();
      |                     ^~~
a.cc:98:21: error: redeclaration of 'long long int itr2'
   98 |                 int itr2 = itr;
      |                     ^~~~
a.cc:83:21: note: 'long long int itr2' previously declared here
   83 |                 int itr2 = itr;
      |                     ^~~~