// C. Kefa and Company
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vb> vvb;
typedef vector<vll> vvll;
typedef vector<pll> vpll;
typedef vector<string> vs;
typedef unordered_map<ll, ll> umll;
template<class T>
using pq = priority_queue<T, vector<T>, greater<T>>;
#define io \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr);
void solve() {
int n, d;
cin >> n >> d;
vector<pair<int, int>> v(n);
for (int i = 0; i < n; ++i) {
cin >> v[i].first >> v[i].second;
}
sort(v.begin(), v.end());
ll sum = 0, ans = INT_MIN, l = 0, r = 0;
while (r < n) {
if (v[r].first - v[l].first < d) {
sum += v[r].second;
r++;
} else {
sum -= v[l].second;
l++;
}
ans = max(ans, sum);
}
cout << ans;
}
int main() {
io;
ll tests = 1;
// cin >> tests;
while (tests--) {
solve();
}
return 0;
}
Ly8gIEMuIEtlZmEgYW5kIENvbXBhbnkKCgojcHJhZ21hIEdDQyB0YXJnZXQoImF2eDIiKQojcHJhZ21hIEdDQyBvcHRpbWl6YXRpb24oIk8zIikKI3ByYWdtYSBHQ0Mgb3B0aW1pemF0aW9uKCJ1bnJvbGwtbG9vcHMiKQoKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiBsb25nIGRvdWJsZSBsZDsKdHlwZWRlZiBwYWlyPGxsLCBsbD4gcGxsOwp0eXBlZGVmIHZlY3Rvcjxib29sPiB2YjsKdHlwZWRlZiB2ZWN0b3I8aW50PiB2aTsKdHlwZWRlZiB2ZWN0b3I8bGw+IHZsbDsKdHlwZWRlZiB2ZWN0b3I8dmk+IHZ2aTsKdHlwZWRlZiB2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