#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const long long int INF = 1e18;
const int MX = 300001;
int rownum[] = {-1, 0, 0, 1};
int colnum[] = {0, -1, 1, 0};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int t, x, y, p, q, c, e, i, j, n, m, k;
  cin >> n >> p;
  vector<pair<long long int, long long int> > v;
  vector<long long int> vf;
  for (i = 0; i < n; i++) {
    cin >> x >> y;
    v.push_back(make_pair(x, y));
    vf.push_back(x);
  }
  sort(v.begin(), v.end());
  sort(vf.begin(), vf.end());
  for (i = 1; i < n; i++) {
    v[i].second += v[i - 1].second;
  }
  k = 0;
  for (i = 0; i < n; i++) {
    q = upper_bound(vf.begin(), vf.end(), vf[i] + p - 1) - vf.begin();
    if (i == 0)
      k = max(k, v[q - 1].second);
    else
      k = max(k, v[q - 1].second - v[i - 1].second);
  }
  cout << k;
}
