#include <iostream>
#include <algorithm>
#define ll long long int
using namespace std;
struct node{
ll m,s;
};
bool f(node n1,node n2){
return n1.m<n2.m;
}
int main() {
// your code goes here
int N,D;
cin>>N>>D;
node n[N];
for(int i =0;i<N;i++){
cin>>n[i].m>>n[i].s;
}
sort(n,n+N,f);
ll l_max = 0,g_max = 0,sum = 0;
for(int i =0;i<N;i++){
sum+=n[i].s;
}
l_max = sum;
for(int i =0;i<N;i++){
int j = N-1;
if(i>0){
l_max = sum - n[i-1].s;
sum = sum - n[i-1].s;
}
else{
l_max = sum;
}
while(n[j].m-n[i].m >= D && j>i){
l_max = l_max - n[j].s;
j--;
}
if(g_max<l_max){
g_max = l_max;
}
}
cout<<g_max<<endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YWxnb3JpdGhtPgojZGVmaW5lIGxsIGxvbmcgbG9uZyBpbnQKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCgpzdHJ1Y3Qgbm9kZXsKICAgIGxsIG0sczsKfTsKCmJvb2wgZihub2RlIG4xLG5vZGUgbjIpewogICAgcmV0dXJuIG4xLm08bjIubTsKfQoKaW50IG1haW4oKSB7CiAgICAvLyB5b3VyIGNvZGUgZ29lcyBoZXJlCiAgICBpbnQgTixEOwogICAgY2luPj5OPj5EOwogICAgbm9kZSBuW05dOwogICAgZm9yKGludCBpID0wO2k8TjtpKyspewogICAgICAgIGNpbj4+bltpXS5tPj5uW2ldLnM7CiAgICB9CiAgICBzb3J0KG4sbitOLGYpOwogICAgbGwgbF9tYXggPSAwLGdfbWF4ID0gMCxzdW0gPSAwOwogICAgZm9yKGludCBpID0wO2k8TjtpKyspewogICAgCXN1bSs9bltpXS5zOwogICAgfQogICAgbF9tYXggPSBzdW07CiAgICBmb3IoaW50IGkgPTA7aTxOO2krKyl7CiAgICAgICAgaW50IGogPSBOLTE7CiAgICAgICAgaWYoaT4wKXsKICAgICAgICAJbF9tYXggPSBzdW0gLSBuW2ktMV0uczsKICAgICAgICAJc3VtID0gc3VtIC0gbltpLTFdLnM7CiAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgCWxfbWF4ID0gc3VtOwogICAgICAgIH0KICAgICAgICB3aGlsZShuW2pdLm0tbltpXS5tID49IEQgJiYgaj5pKXsKICAgICAgICAJbF9tYXggPSBsX21heCAtIG5bal0uczsKICAgICAgICAgICAgai0tOwogICAgICAgIH0KICAgICAgICBpZihnX21heDxsX21heCl7CiAgICAgICAgICAgIGdfbWF4ID0gbF9tYXg7CiAgICAgICAgfQogICAgfQogICAgY291dDw8Z19tYXg8PGVuZGw7CiAgICByZXR1cm4gMDsKfQ==