#include <bits/stdc++.h>
using namespace std;
int main() {
  long n = 0;
  long d = 0;
  cin >> n;
  cin >> d;
  struct fr {
    long money;
    long friendship;
  };
  vector<fr> friends;
  friends.reserve(n);
  auto c = n;
  while (c--) {
    fr nf;
    cin >> nf.money;
    cin >> nf.friendship;
    friends.push_back(nf);
  }
  sort(begin(friends), end(friends), [](const fr& lhs, const fr& rhs) {
    if (lhs.money != rhs.money) return (lhs.money < rhs.money);
    return (lhs.friendship > rhs.friendship);
  });
  long long sum = 0;
  long long maxSum = 0;
  long long minMoneyIndex = 0;
  for (size_t i = 0; i < friends.size(); ++i) {
    while ((friends[i].money - friends[minMoneyIndex].money) >= d) {
      sum -= friends[minMoneyIndex].friendship;
      ++minMoneyIndex;
    }
    sum += friends[i].friendship;
    maxSum = max(sum, maxSum);
    if (sum < 0) sum = 0;
  }
  cout << maxSum << "\n";
};
