#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int z = 1;
  while (z--) {
    int n, d;
    cin >> n >> d;
    vector<pair<int, int>> friends;
    int arr[n];
    for (int i = 0; i < n; i++) {
      int a, b;
      cin >> a >> b;
      arr[i] = a;
      friends.push_back(make_pair(a, b));
    }
    sort(friends.begin(), friends.end());
    sort(arr, arr + n);
    long long int prefix[n];
    prefix[0] = friends[0].second;
    for (int i = 1; i < n; i++) {
      prefix[i] = prefix[i - 1] + friends[i].second;
    }
    long long int maxx = 0;
    for (int i = 0; i < n; i++) {
      int pos = upper_bound(arr + i, arr + n, arr[i] + d - 1) - arr;
      maxx = max(maxx, prefix[pos - 1] - (i ? prefix[i - 1] : 0));
    }
    cout << maxx << "\n";
  }
  return 0;
}
