//  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;
}