#include <bits/stdc++.h>
#pragma GCC optimize "O2"
using namespace std;
const unsigned long long int MOD = 1000000007;
const long double PI = 2 * acos(0.0);
vector<long long int> ga(long long int n) {
  vector<long long int> a;
  for (long long int i = 0; i < n; i++) {
    long long int p;
    cin >> p;
    a.push_back(p);
  }
  return move(a);
}
vector<unsigned long long int> gau(unsigned long long int n) {
  vector<unsigned long long int> a;
  for (unsigned long long int i = 0; i < n; i++) {
    unsigned long long int p;
    cin >> p;
    a.push_back(p);
  }
  return move(a);
}
vector<string> gas(unsigned long long int n) {
  vector<string> a;
  for (unsigned long long int i = 0; i < n; i++) {
    string p;
    cin >> p;
    a.push_back(p);
  }
  return move(a);
}
template <typename T, typename A>
void pa(vector<T, A> const &a) {
  for (T p : a) {
    cout << p << " ";
  }
  cout << "\n";
}
void yes() { cout << "YES\n"; }
void no() { cout << "NO\n"; }
void yesno(int f) {
  if (f)
    yes();
  else
    no();
}
template <typename T>
long long int ceilSearch(
    const vector<pair<unsigned long long int, unsigned long long int>> &a, T n,
    long long int s, long long int e) {
  if (s > e || a[s] > n || a[e] < n) return -1;
  long long int m = (s + e) / 2, idx;
  if (a[m] == n) {
    idx = ceilSearch(a, n, m + 1, e);
    return idx == -1 ? m : idx;
  } else if (a[m] < n)
    return ceilSearch(a, n, m + 1, e);
  else
    return ceilSearch(a, n, s, m - 1);
}
long long int floorSearch(
    const vector<pair<unsigned long long int, unsigned long long int>> &a,
    unsigned long long int n, long long int s, long long int e) {
  if (s > e || a[s].first > n) return -1;
  if (a[e].first < n) return e;
  long long int m = (s + e) / 2, idx;
  if (a[m].first <= n) {
    idx = floorSearch(a, n, m + 1, e);
    return idx == -1 ? m : idx;
  } else {
    return floorSearch(a, n, s, m - 1);
  }
}
void solve() {
  unsigned long long int n, d;
  cin >> n >> d;
  vector<pair<unsigned long long int, unsigned long long int>> data;
  for (unsigned long long int i = 0; i < n; i++) {
    unsigned long long int a, b;
    cin >> a >> b;
    data.push_back(make_pair(a, b));
  }
  sort(data.begin(), data.end());
  vector<unsigned long long int> sum(n);
  sum[0] = data[0].second;
  for (unsigned long long int i = 1; i < n; i++) {
    sum[i] = sum[i - 1] + data[i].second;
  }
  unsigned long long int m = 0;
  for (unsigned long long int i = 0; i < n; i++) {
    long long int next = floorSearch(data, data[i].first + d - 1, 0, n - 1);
    if (sum[next] - sum[i] + data[i].second > m)
      m = sum[next] - sum[i] + data[i].second;
    ;
  }
  cout << m;
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
