#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;
}