# define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <stdio.h>
#include <vector>
#include <cstdlib>
#include <string>
#include <map>
#include <ctime>
#include <list>
#include <stack>
#include <set>
#include <memory.h>
#include <deque>
#include <queue>
#include <cctype>
#include <iomanip>
#include <bitset>
#include <sstream>
typedef long long ll;

using namespace std;

# define pi 2*acos(0.)
# define e exp(1.)
# define N 111111

ll n, l, m, i, j, ans = 0, k, d, x, y;
ll a[N],b[N];
//string s;
vector <pair<ll, ll> >v1, v2;

bool cmp(pair<ll,ll> &p1, pair<ll, ll>&p2){
   if(p1.first != p2.first)  return p1.first<p2.first;
   else return p1.second < p2.second;
}

int main(){

    cin >> n >> d;
    for (i = 0; i < n; ++i)
    {
    cin >> x >> y;
    v1.push_back(make_pair(x, y));
    }

    sort(v1.begin(), v1.end(),cmp);

    b[0] = v1[0].second;

    for (i = 1; i < n; ++i)
        b[i] = b[i - 1] + v1[i].second;

    ans = 0;
    int q = 0;
    for (i = 1; i < n; ++i)
    {

        if (v1[i].first - v1[q].first >= d){
            ans = max(b[i - 1] - b[q], ans);
            while (v1[i].first - v1[q].first >= d)
                q++;
        }
    }
        //if (!x)sum += v1[j].second;

    ans = max(b[n - 1] - b[q-1], ans);

    cout << ans << endl;
    return 0;

}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <stack>
#include <ctime>
#include <functional>
#include<stdio.h>
using namespace std;

#define pi 3.1415921075358979323841073
#define ll long long
#define toint(a) atoi(a.c_str())
#define all(c) c.begin(), c.end()
#define sor(i,j,n) for(int i=j;i<n;++i)
#define fsyo return 0
#define nt endl
#define tpi cout
#define length length()
#define tr(container, it) for( decltype(container.begin()) it = container.begin(); it != container.end(); it++)

string tostring(int a)
{
    ostringstream temp;
    temp << a;
    return temp.str();
}

ll gcd(ll a, ll b)
{
    if (a == 0){
        return b;
    }
    else{
        return gcd(b%a, a);
    }
}

ll lcm(ll a, ll b)
{
    return a*b / gcd(a, b);
}

bool isPrime(unsigned ll n) {
    if (n <= 3) {
        return n > 1;
    }

    if (n % 2 == 0 || n % 3 == 0) {
        return false;
    }

    for (unsigned ll i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0) {
            return false;
        }
    }

    return true;
}

template <class T>
void reverse(T *a, int n){
    T *skizb = a, *verj = a + n - 1;
    while (skizb < verj){
        swap(*skizb, *verj);
        skizb++;
        verj--;
    }
}

ll fact(ll in){
    if (in == 0 || in == 1){
        return 1;
    }
    ll res = 1, mult = 2;
    while (mult < in + 1){
        res *= mult;
        mult++;
    }
    return res;
}

struct str{
    int x, y, z;

    bool operator<(const str rhs) const
    {
        if (x < rhs.x)
        {
            return true;
        }
        else if (x == rhs.x)
        {
            if (y < rhs.y)
            {
                return true;
            }
            else if (y == rhs.y)
            {
                return z < rhs.z;
            }
        }
        return false;
    }

}sv[8];

ll exp(ll n, ll r, ll md)
{
    if (r == 0){
        return 1;
    }
    ll ans = exp(n, r / 2, md);
    ans *= ans;
    ans %= md;
    if (r & 1){
        ans *= n;
        ans %= md;
    }
    return ans%md;
}
ll choose(ll n, ll r, ll md)
{
    ll ans = 1;
    sor(i, n - r + 1, n + 1){
        ans *= i;
        ans %= md;
    }
    sor(i, 1, r + 1){
        ans *= exp(i, md - 2, md);
        ans %= md;
    }
    return ans%md;
}

#define mod 1000000007

pair <ll, ll> a[100007];

int main(){
    ll n, d, mx, tp;

    cin >> n >> d;

    sor(i, 0, n){
        cin >> a[i].first >> a[i].second;
    }

    sort(a, a + n);

    mx = a[0].second;
    tp = mx;
    int k = 0;

    sor(i, 1, n){
        if (a[i].first - a[k].first>=d){
            while (a[i].first - a[k].first >= d){
                ++k;
            }
            mx = max(mx, tp);
            tp = a[i].second;
        }
        else{
            tp += a[i].second;
        }
    }

    tpi << max(mx,tp) << nt;

    fsyo;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <stack>
#include <ctime>
#include <functional>
#include<stdio.h>
using namespace std;

#define pi 3.1415921075358979323841073
#define ll long long
#define toint(a) atoi(a.c_str())
#define all(c) c.begin(), c.end()
#define sor(i,j,n) for(int i=j;i<n;++i)
#define fsyo return 0
#define nt endl
#define tpi cout
#define length length()
#define tr(container, it) for( decltype(container.begin()) it = container.begin(); it != container.end(); it++)

string tostring(int a)
{
    ostringstream temp;
    temp << a;
    return temp.str();
}

ll gcd(ll a, ll b)
{
    if (a == 0){
        return b;
    }
    else{
        return gcd(b%a, a);
    }
}

ll lcm(ll a, ll b)
{
    return a*b / gcd(a, b);
}

bool isPrime(unsigned ll n) {
    if (n <= 3) {
        return n > 1;
    }

    if (n % 2 == 0 || n % 3 == 0) {
        return false;
    }

    for (unsigned ll i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0) {
            return false;
        }
    }

    return true;
}

template <class T>
void reverse(T *a, int n){
    T *skizb = a, *verj = a + n - 1;
    while (skizb < verj){
        swap(*skizb, *verj);
        skizb++;
        verj--;
    }
}

ll fact(ll in){
    if (in == 0 || in == 1){
        return 1;
    }
    ll res = 1, mult = 2;
    while (mult < in + 1){
        res *= mult;
        mult++;
    }
    return res;
}

struct str{
    int x, y, z;

    bool operator<(const str rhs) const
    {
        if (x < rhs.x)
        {
            return true;
        }
        else if (x == rhs.x)
        {
            if (y < rhs.y)
            {
                return true;
            }
            else if (y == rhs.y)
            {
                return z < rhs.z;
            }
        }
        return false;
    }

}sv[8];

ll exp(ll n, ll r, ll md)
{
    if (r == 0){
        return 1;
    }
    ll ans = exp(n, r / 2, md);
    ans *= ans;
    ans %= md;
    if (r & 1){
        ans *= n;
        ans %= md;
    }
    return ans%md;
}
ll choose(ll n, ll r, ll md)
{
    ll ans = 1;
    sor(i, n - r + 1, n + 1){
        ans *= i;
        ans %= md;
    }
    sor(i, 1, r + 1){
        ans *= exp(i, md - 2, md);
        ans %= md;
    }
    return ans%md;
}

#define mod 1000000007

pair <ll, ll> a[100007];

int main(){
    ll n, d, mx, tp;

    cin >> n >> d;

    sor(i, 0, n){
        cin >> a[i].first >> a[i].second;
    }

    if (n == 1){
        tpi << a[0].second << nt;
        fsyo;
    }

    sort(a, a + n);

    mx = a[0].second;
    tp = mx;
    int x=0, y = 1, k=0;

    while (y < n){
        if (a[y].first - a[x].first >= d){
            mx = max(tp, mx);
            while (a[y].first - a[x].first >= d){
                ++x;
            }
            tp = 0;
            k = x;
            while (k <= y){
                tp += a[k].second;
                ++k;
            }
        }
        else{
            tp += a[y].second;
        }
        ++y;
    }

    tpi << max(mx,tp) << nt;

    fsyo;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <stack>
#include <ctime>
#include <functional>
#include<stdio.h>
using namespace std;

#define pi 3.1415921075358979323841073
#define ll long long
#define toint(a) atoi(a.c_str())
#define all(c) c.begin(), c.end()
#define sor(i,j,n) for(int i=j;i<n;++i)
#define fsyo return 0
#define nt endl
#define tpi cout
#define length length()
#define tr(container, it) for( decltype(container.begin()) it = container.begin(); it != container.end(); it++)

string tostring(int a)
{
    ostringstream temp;
    temp << a;
    return temp.str();
}

ll gcd(ll a, ll b)
{
    if (a == 0){
        return b;
    }
    else{
        return gcd(b%a, a);
    }
}

ll lcm(ll a, ll b)
{
    return a*b / gcd(a, b);
}

bool isPrime(unsigned ll n) {
    if (n <= 3) {
        return n > 1;
    }

    if (n % 2 == 0 || n % 3 == 0) {
        return false;
    }

    for (unsigned ll i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0) {
            return false;
        }
    }

    return true;
}

template <class T>
void reverse(T *a, int n){
    T *skizb = a, *verj = a + n - 1;
    while (skizb < verj){
        swap(*skizb, *verj);
        skizb++;
        verj--;
    }
}

ll fact(ll in){
    if (in == 0 || in == 1){
        return 1;
    }
    ll res = 1, mult = 2;
    while (mult < in + 1){
        res *= mult;
        mult++;
    }
    return res;
}

struct str{
    int x, y, z;

    bool operator<(const str rhs) const
    {
        if (x < rhs.x)
        {
            return true;
        }
        else if (x == rhs.x)
        {
            if (y < rhs.y)
            {
                return true;
            }
            else if (y == rhs.y)
            {
                return z < rhs.z;
            }
        }
        return false;
    }

}sv[8];

ll exp(ll n, ll r, ll md)
{
    if (r == 0){
        return 1;
    }
    ll ans = exp(n, r / 2, md);
    ans *= ans;
    ans %= md;
    if (r & 1){
        ans *= n;
        ans %= md;
    }
    return ans%md;
}
ll choose(ll n, ll r, ll md)
{
    ll ans = 1;
    sor(i, n - r + 1, n + 1){
        ans *= i;
        ans %= md;
    }
    sor(i, 1, r + 1){
        ans *= exp(i, md - 2, md);
        ans %= md;
    }
    return ans%md;
}

#define mod 1000000007

pair <ll, ll> a[100007];

int main(){
    ll n, d, mx, tp;

    cin >> n >> d;

    sor(i, 0, n){
        cin >> a[i].first >> a[i].second;
    }

    sort(a, a + n);

    mx = a[0].second;
    tp = mx;
    int k = 0;

    sor(i, 1, n){
        if (a[i].first - a[k].first>d){
            while (a[i].first - a[k].first > d){
                ++k;
            }
            mx = max(mx, tp);
            tp = a[i].second;
        }
        else{
            tp += a[i].second;
        }
    }

    tpi << max(mx,tp) << nt;

    fsyo;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <iomanip>
#include <stack>
#include <ctime>
#include <functional>
#include<stdio.h>
using namespace std;

#define pi 3.1415921075358979323841073
#define ll long long
#define toint(a) atoi(a.c_str())
#define all(c) c.begin(), c.end()
#define sor(i,j,n) for(int i=j;i<n;++i)
#define fsyo return 0
#define nt endl
#define tpi cout
#define length length()
#define tr(container, it) for( decltype(container.begin()) it = container.begin(); it != container.end(); it++)

string tostring(int a)
{
    ostringstream temp;
    temp << a;
    return temp.str();
}

ll gcd(ll a, ll b)
{
    if (a == 0){
        return b;
    }
    else{
        return gcd(b%a, a);
    }
}

ll lcm(ll a, ll b)
{
    return a*b / gcd(a, b);
}

bool isPrime(unsigned ll n) {
    if (n <= 3) {
        return n > 1;
    }

    if (n % 2 == 0 || n % 3 == 0) {
        return false;
    }

    for (unsigned ll i = 5; i * i <= n; i += 6) {
        if (n % i == 0 || n % (i + 2) == 0) {
            return false;
        }
    }

    return true;
}

template <class T>
void reverse(T *a, int n){
    T *skizb = a, *verj = a + n - 1;
    while (skizb < verj){
        swap(*skizb, *verj);
        skizb++;
        verj--;
    }
}

ll fact(ll in){
    if (in == 0 || in == 1){
        return 1;
    }
    ll res = 1, mult = 2;
    while (mult < in + 1){
        res *= mult;
        mult++;
    }
    return res;
}

struct str{
    int x, y, z;

    bool operator<(const str rhs) const
    {
        if (x < rhs.x)
        {
            return true;
        }
        else if (x == rhs.x)
        {
            if (y < rhs.y)
            {
                return true;
            }
            else if (y == rhs.y)
            {
                return z < rhs.z;
            }
        }
        return false;
    }

}sv[8];

ll exp(ll n, ll r, ll md)
{
    if (r == 0){
        return 1;
    }
    ll ans = exp(n, r / 2, md);
    ans *= ans;
    ans %= md;
    if (r & 1){
        ans *= n;
        ans %= md;
    }
    return ans%md;
}
ll choose(ll n, ll r, ll md)
{
    ll ans = 1;
    sor(i, n - r + 1, n + 1){
        ans *= i;
        ans %= md;
    }
    sor(i, 1, r + 1){
        ans *= exp(i, md - 2, md);
        ans %= md;
    }
    return ans%md;
}

#define mod 1000000007

pair <ll, ll> a[100007];

int main(){
    ll n, d, mx, tp;

    cin >> n >> d;

    sor(i, 0, n){
        cin >> a[i].first >> a[i].second;
    }

    sort(a, a + n);

    mx = a[0].second;
    tp = mx;

    sor(i, 1, n){
        if (a[i].first - a[i - 1].first>d){
            mx = max(mx, tp);
            tp = a[i].second;
        }
        else{
            tp += a[i].second;
        }
    }

    tpi << max(mx,tp) << nt;

    fsyo;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    long long  f=0;
    long long fsofar=0;
    int j=0;
    for(int i=0;i<n;i++){
        while(j<n && a[j].first-a[i].first<d){ f+=a[j].second; j++;}

        fsofar=max(f,fsofar);
        f=0;
        j=i+1;

    }
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    long long fsofar=0;
    int j=0;
    long long sum[n+1];
    sum[0]=0;
    for(int i=0;i<n;i++) sum[i+1]=sum[i]+a[i].second;
    for(int i=0;i<n;i++){
        while(j<n && a[j].first-a[i].first<d) j++;
        fsofar=max(sum[j]-sum[i],fsofar);
    }
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    long long  f=0;
    long long fsofar=0;
    int j=0;
    for(int i=0;i<n;i++){
        while(j<n && a[j].first-a[i].first<d){ f+=a[j].second; j++;}
        fsofar=max(f,fsofar);
        f=0;

    }
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    long long  f=0;
    long long fsofar=0;
    int j=0;
    for(int i=0;i<n;i++){
        while(j<n && a[j].first-a[i].first<d){ f+=a[j].second; j++;}
        fsofar=max(f,fsofar);
        f=0;
        if(j==n) break;
        j=i+1;

    }
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    long long  f=0;
    long long fsofar=0;
    int j=0;
    for(int i=0;i<n;i++){
        while(j<n && a[j].first-a[i].first<d){ f+=a[j].second; j++;}

        fsofar=max(f,fsofar);
        if(j==n-1) break;
        f=0;
        j=i+1;

    }
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    long long fsofar=0;
    int j=0;
    long long sum[100005];
    sum[0]=0;
    for(int i=0;i<n;i++) sum[i+1]=sum[i]+a[i].second;
    for(int i=0;i<n;i++){
        while(j<n && a[j].first-a[i].first<d) j++;
        fsofar=max(sum[j]-sum[i],fsofar);
    }
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    long long  f=a[0].second;
    long long fsofar=a[0].second;
    for(int i=1;i<n;i++){
        if(a[i].first-a[i-1].first<d) f+=a[i].second;
        else{
            fsofar=max(f,fsofar);
            f=a[i].second;
        }
    }
    fsofar=max(f,fsofar);
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    long long  f=0;
    long long fsofar=0;
    int j=0;
    for(int i=0;i<n;i++){
        while(j<n && a[j].first-a[i].first<d){ f+=a[j].second; j++;}
        fsofar=max(f,fsofar);
        f=0;
        if(j==n-1) break;
        j=i+1;

    }
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    int f=a[0].second;
    int fsofar=a[0].second;
    for(int i=1;i<n;i++){
        if(a[i].second-a[i-1].second<d) f+=a[i].second;
        else{
            fsofar=max(f,fsofar);
            f=a[i].second;
        }
    }
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    int f=a[0].second;
    int fsofar=a[0].second;
    for(int i=1;i<n;i++){
        if(a[i].first-a[i-1].first<d) f+=a[i].second;
        else{
            fsofar=max(f,fsofar);
            f=a[i].second;
        }
    }
    fsofar=max(f,fsofar);
    cout<<fsofar<<endl;
}


#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair <int,int> ii;
int n,d;
vector <ii> a(100005);
bool comp(ii i,ii j){return  i.second>j.second; }
bool comp2(ii i,ii j){return  i.first<j.first; }
int main()
{
    //freopen("c.in","rt",stdin);
    cin>>n>>d;
    a.clear();
    for(int i=0;i<n;i++){
        int x,y;
        cin>>x>>y;
        a.push_back(make_pair(x,y));
    }
    sort(a.begin(),a.end(),comp2);
    /*for(int i=0;i<n;i++){
        cout<<a[i].first<<":"<<a[i].second<<endl;
    }*/
    long long  f=a[0].second;
    long long fsofar=a[0].second;
    for(int i=1;i<n;i++){
        if(a[i].first-a[0].first<d) f+=a[i].second;
        else{
            fsofar=max(f,fsofar);
            f=a[i].second;
        }
    }
    fsofar=max(f,fsofar);
    cout<<fsofar<<endl;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
	//freopen("in.txt", "r", stdin);
	long long n, d;	cin >> n >> d;
	vector<pair<long, long>> c(n);
	vector<long long> q;
	for (int i = 0; i < n; ++i)
	{
		cin >> c[i].first >> c[i].second;
	}
	sort(c.begin(), c.end());
	/*for (int i = 0; i < n; ++i)
	{
		cout << c[i].first << " " << c[i].second << endl;
	}*/
	long long i = 1, mini = 0, res = c[0].second; q.push_back(res);
	while (i < n)
	{
		while (i < n)
		{
			if (c[i].first < c[mini].first + d)res += c[i].second;
			else
			{
				break;
			}
			++i;
		}
		q.push_back(res);
		res -= c[mini].second;
		++mini;
	}
	sort(q.rbegin(), q.rend());
	/*for (int i = 0; i < q.size(); ++i)
	{
		cout << q[i] << endl;
	}*/
	cout << q[0];
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
	int n, d;	cin >> n >> d;
	vector<pair<int, int>> c(n);
	vector<int> q;
	for (int i = 0; i < n; ++i)
	{
		cin >> c[i].first >> c[i].second;
	}
	sort(c.begin(), c.end());
	for (int i = 0; i < n; ++i)
	{
		cout << c[i].first << " " << c[i].second << endl;
	}
	int i = 1, mini = 0, res = c[0].second;
	while (i < n)
	{
		while (i < n)
		{
			if (c[i].first <= c[mini].first + d)res += c[i].second;
			else
			{
				break;
			}
			++i;
		}
		q.push_back(res);
		res -= c[mini].second;
		++mini;
	}
	sort(q.rbegin(), q.rend());
	cout << q[0];
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
	int n, d;	cin >> n >> d;
	vector<pair<int, int>> c(n);
	vector<int> q;
	for (int i = 0; i < n; ++i)
	{
		cin >> c[i].first >> c[i].second;
	}
	sort(c.begin(), c.end());
	int i = 1, mini = 0, res = c[0].second;
	while (i < n)
	{
		while (i < n)
		{
			if (c[i].first <= c[mini].first + d)res += c[i].second;
			else
			{
				break;
			}
			++i;
		}
		q.push_back(res);
		res -= c[mini].second;
		++mini;
	}
	sort(q.rbegin(), q.rend());
	cout << q[0];
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
	//freopen("in.txt", "r", stdin);
	long long n, d;	cin >> n >> d;
	vector<pair<long, long>> c(n);
	vector<long long> q;
	for (int i = 0; i < n; ++i)
	{
		cin >> c[i].first >> c[i].second;
	}
	sort(c.begin(), c.end());
	/*for (int i = 0; i < n; ++i)
	{
		cout << c[i].first << " " << c[i].second << endl;
	}*/
	long long i = 1, mini = 0, res = c[0].second; q.push_back(res);
	while (i < n)
	{
		while (i < n)
		{
			if (c[i].first <= c[mini].first + d)res += c[i].second;
			else
			{
				break;
			}
			++i;
		}
		q.push_back(res);
		res -= c[mini].second;
		++mini;
	}
	sort(q.rbegin(), q.rend());
	cout << q[0];
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
int main()
{
	//freopen("in.txt", "r", stdin);
	/*freopen("out.txt", "w", stdout);*/
	long long n, d;	cin >> n >> d;
	vector<pair<long, long>> c(n);
	vector<long> q;
	for (int i = 0; i < n; ++i)
	{
		cin >> c[i].first >> c[i].second;
	}
	sort(c.begin(), c.end());
	/*for (int i = 0; i < n; ++i)
	{
		cout << c[i].first << " " << c[i].second << endl;
	}*/
	long long i = 1, mini = 0, res = c[0].second; q.push_back(res);
	while (i < n)
	{
		while (i < n)
		{
			if (c[i].first <= c[mini].first + d)res += c[i].second;
			else
			{
				break;
			}
			++i;
		}
		q.push_back(res);
		res -= c[mini].second;
		++mini;
	}

	sort(q.rbegin(), q.rend());
	cout << q[0];
	return 0;
}


//#define _CRT_SECURE_NO_WARNINGS
//#include<iostream>
//#include<set>
//#include<algorithm>
//#include<queue>
//#include<string>
//#include<map>
//#include<string.h>
//using namespace std;
//int n,m,u,v,cnt,ans,cat,mcat;
//vector<vector<int> > g;
//int a[10010], tin[10010], tout[10010];
////char used[10010];
//void dfs(int v,int p) {
//  if (p >= 0) {
//      if (a[v])
//          if (a[p]) mcat++;
//          else {
//              mcat = max(mcat, cat);
//              cat = 1;
//          }
//      else
//      if (a[p]){
//          mcat = max(mcat, cat);
//          cat = 0;
//      }
//  }
//  else if (a[v]) { cat = 1; mcat = 1; }
//  tin[v] = cnt++;
//  for (int i = 0; i < g[v].size(); i++) {
//      int to = g[v][i];
//      if (to!=p) {
//          dfs(to,v);
//      }
//  }
//
//  if (p >= 0) {
//      if (a[v])
//      if (a[p]) mcat--;
//      else {
//          cat--;
//          mcat = max(mcat, cat);
//      }
//  }
//
//  tout[v] = cnt++;
//  if (tin[v] + 1 == tout[v]) {
//      ans+=mcat;
//  }
//}
//int main() {
//  scanf("%d %d", &n, &m);
//  g.resize(n);
//  for (int i = 0; i < n; i++) {
//      scanf("%d", &a[i]);
//  }
//  for (int i = 0; i < n-1; i++) {
//      scanf("%d %d", &u, &v); u--; v--;
//      g[u].push_back(v);
//      g[v].push_back(u);
//  }
//  cnt = 0;
//  dfs(0,-1);
//  cout << ans;
//}
///*
//4 2
//0 1 1 1
//1 2
//2 3
//3 4
//*/
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<set>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<string.h>
using namespace std;
struct pt {
    int a, b;
};
bool cmp(pt x, pt y){
    if (x.a < y.a) return true;
    else if (x.a>y.a) return false;
    else return y.b > y.b;
}
int n, d;
vector<pt> v;
int main() {
    scanf("%d %d", &n, &d);
    pt x;
    for (int i = 0; i < n; i++) {
        scanf("%d %d", &x.a, &x.b);
        v.push_back(x);
    }
    sort(v.begin(), v.end(), cmp);
    int z;
    long long sum,mx=-1;
    for (int i = 0; i < n; i++){
        z = v[i].a;
        sum = 0;
        for (int j = i; j < n; j++) {
            if (v[j].a - z<d)
                sum += v[j].b;
            else
                break;
        }
        if (sum>mx)
            mx = sum;

    }
    printf("%I64d", mx);
}
/*
5 2
1 2
2 3
3 1
4 4
4 2
*/


/*
Release_Machine
Just wait
Just do it
Just believe
*/
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
typedef double db;
const int N=100010;
int d,n,rs[N];
struct pt {
    int a,b;
};
vector<pt> v;
bool cmp(pt i,pt j){
    if(i.a<j.a) return true;
    if(i.a>j.a) return false;
    return i.b>j.b;
}
int bs(int i) {
    int r=n-1,m,l=i;
    while(r-l>1) {
        m=(r+l)>>1;
        if(v[m].a-v[i].a<d) l=m;
        else r=m;
    }
    if(v[r].a-v[i].a<d) return rs[r]-rs[i]+v[i].b;
    else return rs[l]-rs[i]+v[i].b;
}
int main() {
    scanf("%d%d",&n,&d);
    v.resize(n);
    for(int i=0;i<n;i++) {
        scanf("%d%d",&v[i].a,&v[i].b);
    }
    sort(v.begin(),v.end(),cmp);
    rs[0]=v[0].b;
    for(int i=1;i<n;i++) {
        rs[i]=rs[i-1]+v[i].b;
    }
    int mx=-INT_MAX;
    for(int i=0;i<n;i++) {
        mx=max(mx,bs(i));
    }
    printf("%d",mx);
}


/*
Release_Machine
Just wait
Just do it
Just believe
*/
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
typedef double db;
const int N=100010;
int d,n;long long rs[N];
struct pt {
    int a,b;
};
vector<pt> v;
bool cmp(pt i,pt j){
    if(i.a<j.a) return true;
    if(i.a>j.a) return false;
    return i.b>j.b;
}
long long bs(int i) {
    int r=n-1,m,l=i;
    while(r-l>1) {
        m=(r+l)>>1;
        if(v[m].a-v[i].a<d) l=m;
        else r=m;
    }
    if(v[r].a-v[i].a<d) return rs[r]-rs[i]+v[i].b;
    else return rs[l]-rs[i]+v[i].b;
}
int main() {
    scanf("%d%d",&n,&d);
    v.resize(n);
    for(int i=0;i<n;i++) {
        scanf("%d%d",&v[i].a,&v[i].b);
    }
    sort(v.begin(),v.end(),cmp);
    rs[0]=v[0].b;
    for(int i=1;i<n;i++) {
        rs[i]=rs[i-1]+v[i].b;
    }
    long long mx=-INT_MAX;
    for(int i=0;i<n;i++) {
        long long res=bs(i);
        if(res>mx) {mx=res;}

    }
    cout<<mx;
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<set>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<string.h>
using namespace std;
struct pt {
    int a, b;
};
bool cmp(pt x,pt y){
    if (x.a < y.a) return true;
    else if (x.a>y.a) return false;
    else return y.b > y.b;
}
int n,d;
vector<pt> v;
int main() {
    scanf("%d %d", &n, &d);
    pt x;
    for (int i = 0; i < n; i++) {
        scanf("%d %d",&x.a,&x.b );
        v.push_back(x);
    }
    sort(v.begin(),v.end(),cmp);
    int z,sum,mx=-1;
    for (int i = 0; i < n; i++){
        z = v[i].a;
        sum = 0;
        for (int j = i; j < n; j++) {
            if (v[j].a-z<d)
                sum += v[j].b;
            else
                break;
        }
        mx = max(mx,sum);
    }
    printf("%d",mx);
}
/*
5 2
1 2
2 3
3 1
4 4
4 2
*/


/*
Release_Machine
Just wait
Just do it
Just believe
*/
#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<bitset>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
typedef double db;
const int N=100010;
int d,n;long long rs[N];
struct pt {
    int a,b;
};
vector<pt> v;
bool cmp(pt i,pt j){
    if(i.a<j.a) return true;
    if(i.a>j.a) return false;
    return i.b>j.b;
}
int bs(int i) {
    int r=n-1,m,l=i;
    while(r-l>1) {
        m=(r+l)>>1;
        if(v[m].a-v[i].a<d) l=m;
        else r=m;
    }
    if(v[r].a-v[i].a<d) return rs[r]-rs[i]+v[i].b;
    else return rs[l]-rs[i]+v[i].b;
}
int main() {
    scanf("%d%d",&n,&d);
    v.resize(n);
    for(int i=0;i<n;i++) {
        scanf("%d%d",&v[i].a,&v[i].b);
    }
    sort(v.begin(),v.end(),cmp);
    rs[0]=v[0].b;
    for(int i=1;i<n;i++) {
        rs[i]=rs[i-1]+v[i].b;
    }
    int mx=-INT_MAX;
    for(int i=0;i<n;i++) {
        mx=max(mx,bs(i));
    }
    printf("%d",mx);
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<set>
#include<algorithm>
#include<queue>
#include<string>
#include<map>
#include<string.h>
using namespace std;
struct pt {
    int a, b;
};
bool cmp(pt x,pt y){
    if (x.a < y.a) return true;
    else if (x.a>y.a) return false;
    else return y.b > y.b;
}
int n,d;
vector<pt> v;
int main() {
    cin >> n >> d;
    pt x;
    for (int i = 0; i < n; i++) {
        cin >> x.a >> x.b;
        v.push_back(x);
    }
    sort(v.begin(),v.end(),cmp);
    int z,sum,mx=-1;
    for (int i = 0; i < n; i++){
        z = v[i].a;
        sum = 0;
        for (int j = i; j < n; j++) {
            if (v[j].a-z<=d)
                sum += v[j].b;
            else
                break;
        }
        mx = max(mx,sum);
    }
    cout << mx;
}
/*
5 4
0 4

*/


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

long long n, d;
pair<long long, long long> arr[100000];

int main(){
    //freopen("input.txt", "r", stdin);
    scanf("%lld%lld", &n, &d);
    for (int i = 0; i < n; i++){
        scanf("%lld%lld", &arr[i].first, &arr[i].second);
    }
    sort(arr, arr + n);

    long long l = 0;
    long long ans = arr[0].second, temp = arr[0].second;
    for (long long r = 1; r < n; r++){
        if (arr[r].first - arr[l].first < d){
            temp += arr[r].second;
        }
        else{
            temp += arr[r].second;
            while (1){
                if (arr[r].first - arr[l].first >= d)
                    temp -= arr[l].second;
                l++;
                if (arr[r].first - arr[l].first < d)
                    break;
            }
        }
        if (temp > ans)
            ans = temp;
    }
    printf("%lld", ans);
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

long long n, d;
pair<long long, long long> arr[100000];

int main(){
    //freopen("input.txt", "r", stdin);
    scanf("%lld%lld", &n, &d);
    for (int i = 0; i < n; i++){
        scanf("%lld%lld", &arr[i].first, &arr[i].second);
    }
    sort(arr, arr + n);

    long long l = 0;
    long long ans = arr[0].second, temp = arr[0].second;
    for (long long r = 1; r < n; r++){
        if (arr[r].first - arr[l].first < d){
            temp += arr[r].second;
        }
        else{
            temp += arr[r].second;
            while (1){
                if (arr[r].first - arr[l].first >= d)
                    temp -= arr[l].second;
                l++;
                if (arr[r].first - arr[l].first < d)
                    break;
            }
        }
        if (temp > ans)
            ans = temp;
    }
    printf("%lld", ans);
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

long long n, d;
pair<long long, long long> arr[100000];

int main(){
    scanf("%lld%lld", &n, &d);
    for (int i = 0; i < n; i++){
        scanf("%lld%lld", &arr[i].first, &arr[i].second);
    }
    sort(arr, arr + n);

    long long recent_min = arr[0].first;
    long long ans = arr[0].second;
    for (int i = 1; i < n; i++){
        if (arr[i].first - recent_min > d){
            if (ans < arr[i].second){
                ans = arr[i].second;
                recent_min = arr[i].first;
            }
        }
        else
            ans += arr[i].second;
    }
    printf("%lld", ans);
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

int n, d;
pair<long long, long long> arr[100000];

int main(){
    //freopen("input.txt", "r", stdin);
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; i++)
        scanf("%lld%lld", &arr[i].first, &arr[i].second);

    sort(arr, arr + n);

    int l = 0;
    long long ans = arr[0].second, temp = arr[0].second;
    for (int r = 1; r < n; r++){
        if (arr[r].first - arr[l].first < d){
            temp += arr[r].second;
        }
        else{
            temp += arr[r].second;
            while (1){
                if (arr[r].first - arr[l].first >= d)
                    temp -= arr[l].second;
                l++;
                if (arr[r].first - arr[l].first < d)
                    break;
            }
        }
        if (temp > ans)
            ans = temp;
    }
    printf("%lld", ans);
    return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

int n, d;
pair<int, int> arr[100000];

int main(){
    scanf("%d%d", &n, &d);
    for (int i = 0; i < n; i++){
        scanf("%d%d", &arr[i].first, &arr[i].second);
    }
    sort(arr, arr + n);

    int recent_min = arr[0].first;
    int ans = arr[0].second;
    for (int i = 1; i < n; i++){
        if (arr[i].first - recent_min > d){
            if (ans < arr[i].second){
                ans = arr[i].second;
                recent_min = arr[i].first;
            }
        }
        else
            ans += arr[i].second;
    }
    printf("%d", ans);
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

long long n, d;
pair<long long, long long> arr[100000];

int main(){
    scanf("%lld%lld", &n, &d);
    for (int i = 0; i < n; i++){
        scanf("%lld%lld", &arr[i].first, &arr[i].second);
    }
    sort(arr, arr + n);

    long long left = 0;
    long long right = 0;
    long long ans = arr[0].second;
    for (right = 1; right < n; right++){
        if (arr[right].first - arr[left].first < d){
            ans += arr[right].second;
        }
        else{
            long long temp = ans;
            long long templeft = left;
            while (templeft <= right){
                temp -= arr[left].second;
                templeft++;
                temp += arr[left].second;
            }
            if (temp > ans) {
                ans = temp;
                left = templeft;

            };
        }
    }
    printf("%lld", ans);
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;

long long n, d;
pair<long long, long long> arr[100000];

int main(){
    //freopen("input.txt", "r", stdin);
    scanf("%lld%lld", &n, &d);
    for (int i = 0; i < n; i++){
        scanf("%lld%lld", &arr[i].first, &arr[i].second);
    }
    sort(arr, arr + n);

    long long left = 0;
    long long right = 0;
    long long ans = arr[0].second;
    for (right = 1; right < n; right++){
        if (arr[right].first - arr[left].first < d){
            ans += arr[right].second;
        }
        else{
            long long temp = ans;
            long long templeft = left;
            while (templeft < right){
                temp -= arr[templeft].second;
                templeft++;
                temp += arr[templeft].second;
            }
            if (temp > ans) {
                ans = temp;
                left = templeft;
            };
        }
    }
    printf("%lld", ans);
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
#include<time.h>
#include<set>
#include<vector>
#include<numeric>
#include<iomanip>
#include <bitset>
using namespace std;

#define All(a) a.begin(), a.end()
#define _2d(a,row,col,type) type**a=new type*[row]; for (int i=0;i<row;i++) a[i]=new type[col];
#define rep(i,n) for(int i=0;i<int(n);i++)
#define repd(i,n) for(long long i=n-1;i>=0;i--)
#define repi(i,a,n) for(int i=int(a);i<int(n);i++)
#define clr(a, n) memset(a, n, sizeof(a));
#define scn(a,n) rep(i,n) cin>>a[i];
#define scn2(a,row,col) rep(i,row) rep(j,col) cin>>a[i][j];
#define prn(a,n) rep(i,n-1) cout<<a[i]<<" "; cout<<a[n-1]<<endl;
#define prn2(a,row,col) rep(i,row){rep(j,col-1) cout<<a[i][j]<<" "; cout<<a[i][col-1]<<endl;}
#define mp make_pair
#define Odd(x) x%2!=0
#define Even(x) x%2==0
#define Pi 3.14
#define INF 2000000000 // 2 billion
#define read freopen("in.in","r",stdin) // 2 billion
#define write freopen("out.out","w",stdout)

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi > vii;
typedef vector<long> vl;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, string> is;
typedef pair<ll, ll> ii;

int main(){
	ll n, d;
	cin >> n >> d;
	vector<ii> a(n);
	rep(i, n)
		cin >> a[i].second >> a[i].first;
	sort(All(a));
	int res = 0;
	repd(i, a.size()){
		ll temp = a[i].second - a[n-1].second;
		if (abs(temp) < d)
			res+=a[i].first;
		else
			break;
	}
	cout << res;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
#include<time.h>
#include<set>
#include<vector>
#include<numeric>
#include<iomanip>
#include <bitset>
using namespace std;

#define All(a) a.begin(), a.end()
#define _2d(a,row,col,type) type**a=new type*[row]; for (int i=0;i<row;i++) a[i]=new type[col];
#define rep(i,n) for(int i=0;i<int(n);i++)
#define repd(i,n) for(long long i=n-1;i>=0;i--)
#define repi(i,a,n) for(int i=int(a);i<int(n);i++)
#define clr(a, n) memset(a, n, sizeof(a));
#define scn(a,n) rep(i,n) cin>>a[i];
#define scn2(a,row,col) rep(i,row) rep(j,col) cin>>a[i][j];
#define prn(a,n) rep(i,n-1) cout<<a[i]<<" "; cout<<a[n-1]<<endl;
#define prn2(a,row,col) rep(i,row){rep(j,col-1) cout<<a[i][j]<<" "; cout<<a[i][col-1]<<endl;}
#define mp make_pair
#define Odd(x) x%2!=0
#define Even(x) x%2==0
#define Pi 3.14
#define INF 2000000000 // 2 billion
#define read freopen("in.in","r",stdin) // 2 billion
#define write freopen("out.out","w",stdout)

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi > vii;
typedef vector<long> vl;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, string> is;
typedef pair<ll, ll> ii;

int main(){
	ll n, d;
	cin >> n >> d;
	vector<ii> a(n);
	rep(i, n)
		cin >> a[i].second >> a[i].first;
	sort(All(a));
	int res = 0;
	repd(i, a.size()){
		if (abs(a[i].second - a[0].second) < d)
			res+=a[i].first;
		else
			break;
	}
	cout << res;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
#include<time.h>
#include<set>
#include<vector>
#include<numeric>
#include<iomanip>
#include <bitset>
using namespace std;

#define All(a) a.begin(), a.end()
#define _2d(a,row,col,type) type**a=new type*[row]; for (int i=0;i<row;i++) a[i]=new type[col];
#define rep(i,n) for(int i=0;i<int(n);i++)
#define repd(i,n) for(long long i=n-1;i>=0;i--)
#define repi(i,a,n) for(int i=int(a);i<int(n);i++)
#define clr(a, n) memset(a, n, sizeof(a));
#define scn(a,n) rep(i,n) cin>>a[i];
#define scn2(a,row,col) rep(i,row) rep(j,col) cin>>a[i][j];
#define prn(a,n) rep(i,n-1) cout<<a[i]<<" "; cout<<a[n-1]<<endl;
#define prn2(a,row,col) rep(i,row){rep(j,col-1) cout<<a[i][j]<<" "; cout<<a[i][col-1]<<endl;}
#define mp make_pair
#define Odd(x) x%2!=0
#define Even(x) x%2==0
#define Pi 3.14
#define INF 2000000000 // 2 billion
#define read freopen("in.in","r",stdin) // 2 billion
#define write freopen("out.out","w",stdout)

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi > vii;
typedef vector<long> vl;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, string> is;
typedef pair<ll, ll> ii;

//vector<vi > g;
//int m;
//vi vis;
//int dfs(int node){
//
//}

//int main(){
//	int n; cin >> n >> m;
//	g = vector<vi >(n);
//	vis = vi(n);
//	rep(i, n){
//		cin >> vis[i];
//	}
//}

int main(){
	ll n, d;
	cin >> n >> d;
	vector<ii> a(n);
	rep(i, n)
		cin >> a[i].first>> a[i].second;
	sort(All(a));
	ll res;
	vll s1(n), s2(n);
	s1[0] = a[0].second;
	s2[0] = a[0].first;
	repi(i, 1, n) s1[i] += s1[i-1] + a[i].second, s2[i] = a[i].first;
	res = s1[0];
	rep(i,n){
		int idx = lower_bound(All(s2), s2[i] + d) - s2.begin();
		if (i)
			res = max(res, s1[idx - 1] - s1[i - 1]);
		else
			res = max(res, s1[idx - 1]);
	}
	cout << res;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
#include<time.h>
#include<set>
#include<vector>
#include<numeric>
#include<iomanip>
#include <bitset>
using namespace std;

#define All(a) a.begin(), a.end()
#define _2d(a,row,col,type) type**a=new type*[row]; for (int i=0;i<row;i++) a[i]=new type[col];
#define rep(i,n) for(int i=0;i<int(n);i++)
#define repd(i,n) for(long long i=n-1;i>=0;i--)
#define repi(i,a,n) for(int i=int(a);i<int(n);i++)
#define clr(a, n) memset(a, n, sizeof(a));
#define scn(a,n) rep(i,n) cin>>a[i];
#define scn2(a,row,col) rep(i,row) rep(j,col) cin>>a[i][j];
#define prn(a,n) rep(i,n-1) cout<<a[i]<<" "; cout<<a[n-1]<<endl;
#define prn2(a,row,col) rep(i,row){rep(j,col-1) cout<<a[i][j]<<" "; cout<<a[i][col-1]<<endl;}
#define mp make_pair
#define Odd(x) x%2!=0
#define Even(x) x%2==0
#define Pi 3.14
#define INF 2000000000 // 2 billion
#define read freopen("in.in","r",stdin) // 2 billion
#define write freopen("out.out","w",stdout)

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi > vii;
typedef vector<long> vl;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, string> is;
typedef pair<ll, ll> ii;

//vector<vi > g;
//int m;
//vi vis;
//int dfs(int node){
//
//}

//int main(){
//	int n; cin >> n >> m;
//	g = vector<vi >(n);
//	vis = vi(n);
//	rep(i, n){
//		cin >> vis[i];
//	}
//}

int main(){
	ll n, d;
	cin >> n >> d;
	vector<ii> a(n);
	rep(i, n)
		cin >> a[i].second >> a[i].first;
	sort(All(a));
	ll res;
	vll s1(n), s2(n);
	s1[0] = a[0].first;
	s2[0] = a[0].second;
	repi(i, 1, n) s1[i] += s1[i-1] + a[i].first, s2[i] = a[i].second;
	res = s1[n-1];
	repi(i, 1,n){
		int idx = lower_bound(All(s2), s2[i] + d) - s2.begin();
		res = max(res, s1[idx - 1] - s1[i - 1]);
	}
	cout << res;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
#include<time.h>
#include<set>
#include<vector>
#include<numeric>
#include<iomanip>
#include <bitset>
using namespace std;

#define All(a) a.begin(), a.end()
#define _2d(a,row,col,type) type**a=new type*[row]; for (int i=0;i<row;i++) a[i]=new type[col];
#define rep(i,n) for(int i=0;i<int(n);i++)
#define repd(i,n) for(long long i=n-1;i>=0;i--)
#define repi(i,a,n) for(int i=int(a);i<int(n);i++)
#define clr(a, n) memset(a, n, sizeof(a));
#define scn(a,n) rep(i,n) cin>>a[i];
#define scn2(a,row,col) rep(i,row) rep(j,col) cin>>a[i][j];
#define prn(a,n) rep(i,n-1) cout<<a[i]<<" "; cout<<a[n-1]<<endl;
#define prn2(a,row,col) rep(i,row){rep(j,col-1) cout<<a[i][j]<<" "; cout<<a[i][col-1]<<endl;}
#define mp make_pair
#define Odd(x) x%2!=0
#define Even(x) x%2==0
#define Pi 3.14
#define INF 2000000000 // 2 billion
#define read freopen("in.in","r",stdin) // 2 billion
#define write freopen("out.out","w",stdout)

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi > vii;
typedef vector<long> vl;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, string> is;
typedef pair<ll, ll> ii;

int main(){
	ll n, d;
	cin >> n >> d;
	vector<ii> a(n);
	rep(i, n)
		cin >> a[i].second >> a[i].first;
	sort(All(a));
	int res = 0;
	repd(i, a.size()){
		if (a[i].second - a[0].second < d)
			res+=a[i].first;
		else
			break;
	}
	cout << res;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<sstream>
#include<queue>
#include<math.h>
#include<stdlib.h>
#include<stack>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
#include<time.h>
#include<set>
#include<vector>
#include<numeric>
#include<iomanip>
#include <bitset>
using namespace std;

#define All(a) a.begin(), a.end()
#define _2d(a,row,col,type) type**a=new type*[row]; for (int i=0;i<row;i++) a[i]=new type[col];
#define rep(i,n) for(int i=0;i<int(n);i++)
#define repd(i,n) for(long long i=n-1;i>=0;i--)
#define repi(i,a,n) for(int i=int(a);i<int(n);i++)
#define clr(a, n) memset(a, n, sizeof(a));
#define scn(a,n) rep(i,n) cin>>a[i];
#define scn2(a,row,col) rep(i,row) rep(j,col) cin>>a[i][j];
#define prn(a,n) rep(i,n-1) cout<<a[i]<<" "; cout<<a[n-1]<<endl;
#define prn2(a,row,col) rep(i,row){rep(j,col-1) cout<<a[i][j]<<" "; cout<<a[i][col-1]<<endl;}
#define mp make_pair
#define Odd(x) x%2!=0
#define Even(x) x%2==0
#define Pi 3.14
#define INF 2000000000 // 2 billion
#define read freopen("in.in","r",stdin) // 2 billion
#define write freopen("out.out","w",stdout)

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi > vii;
typedef vector<long> vl;
typedef vector<ll> vll;
typedef vector<ull> vull;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, string> is;
typedef pair<int, int> ii;

int main(){
	int n, d;
	cin >> n >> d;
	vector<ii> a(n);
	rep(i, n)
		cin >> a[i].second >> a[i].first;
	sort(All(a));
	int res = 0, temp;
	repd(i, a.size()){
		if (a[i].second - a[0].second < d)
			res+=a[i].first;
		else
			break;
	}
	cout << res;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<long long> > merge_sort(vector<vector<long long> > a)
{
	if (a.size() <= 1)
		return a;
	vector<vector<long long> > b(a.size() / 2);
	vector<vector<long long> > c(a.size() - b.size());
	for (int i = 0; i < b.size(); i++)
		b[i] = a[i];
	for (int i = 0; i < c.size(); i++)
		c[i] = a[b.size() + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < a.size())
	{
		if (i < b.size() && (j >= c.size() || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<c.size() && (i >= b.size() || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n;
	cin >> n;
	int d;
	cin >> d;
	long long MAX = 0;
	vector<vector<long long> > a(n,vector<long long>(2));
	for (int i = 0; i < n; i++)
	{
		cin >> a[i][0] >> a[i][1];
		MAX = max(MAX, a[i][1]);
	}

	a = merge_sort(a);

	for (int i = 0; i < n; i++)
	{
		int j = i + 1;
		long long k = a[i][1];
		for (; j < n && a[j][0] - a[i][0] < d; j++)
			k = k + a[j][1];
		MAX = max(MAX, k);
		if (j == n)
			break;
	}
	cout << MAX;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<int> > merge_sort(vector<vector<int> > a)
{
	int asize = a.size();
	if (asize <= 1)
		return a;
	/*bool flag = true;
	for (int i = 0; i < asize-1;i++)
	if (a[i][0]>a[i+1][0])
	{
	flag = false;
	break;
	}
	if (flag)
	return a;*/
	int bsize = asize / 2;
	int csize = asize - bsize;
	vector<vector<int> > b(bsize);
	vector<vector<int> > c(csize);
	for (int i = 0; i < bsize; i++)
		b[i] = a[i];
	for (int i = 0; i < csize; i++)
		c[i] = a[bsize + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < asize)
	{
		if (i < bsize && (j >= csize || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<csize && (i >= bsize || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n, d;
	scanf("%d%d", &n, &d);
	long long MAX = 0;
	vector<vector<int> > a(n, vector<int>(2));
	for (int i = 0; i < n; i++)
	{
		scanf("%d%d", &a[i][0], &a[i][1]);
		if (a[i][1]>MAX)
			MAX = a[i][1];
	}

	//a = merge_sort(a);
	sort(a.begin(), a.end());

	long long pred = a[0][1];
	int pos = 1;
	for (int i = 0; i < n; i++)
	{
		if (i==0 || a[pos][0] - a[i][0] < d)
		{
			int j = pos;
			int k = pred;
			if (i > 0)
				k -= a[i-1][1];
			for (; j < n && a[j][0] - a[i][0] < d; j++)
				k = k + a[j][1];
			pos = j;
			pred = k;
			MAX = max(MAX, pred);
			if (j == n)
				break;
		}
	}
	cout << MAX;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<int> > merge_sort(vector<vector<int> > a)
{
	int asize = a.size();
	if (asize <= 1)
		return a;
	/*bool flag = true;
	for (int i = 0; i < asize-1;i++)
		if (a[i][0]>a[i+1][0])
		{
			flag = false;
			break;
		}
	if (flag)
		return a;*/
	int bsize = asize / 2;
	int csize = asize - bsize;
	vector<vector<int> > b(bsize);
	vector<vector<int> > c(csize);
	for (int i = 0; i < bsize; i++)
		b[i] = a[i];
	for (int i = 0; i < csize; i++)
		c[i] = a[bsize + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < asize)
	{
		if (i < bsize && (j >= csize || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<csize && (i >= bsize || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n,d;
	scanf("%d%d",&n, &d);
	long long MAX = 0;
	vector<vector<int> > a(n,vector<int>(2));
	for (int i = 0; i < n; i++)
	{
		scanf("%d%d", &a[i][0], &a[i][1]);
		if (a[i][1]>MAX)
			MAX = a[i][1];
	}

	//a = merge_sort(a);
	sort(a.begin(), a.end());

	for (int i = 0; i < n; i++)
	{
		int j = i + 1;
		long long k = a[i][1];
		for (; j < n && a[j][0] - a[i][0] < d; j++)
			k = k + a[j][1];
		MAX = max(MAX, k);
		if (j == n)
			break;
	}
	cout << MAX;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<int> > merge_sort(vector<vector<int> > a)
{
	if (a.size() <= 1)
		return a;
	vector<vector<int> > b(a.size() / 2);
	vector<vector<int> > c(a.size() - b.size());
	for (int i = 0; i < b.size(); i++)
		b[i] = a[i];
	for (int i = 0; i < c.size(); i++)
		c[i] = a[b.size() + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < a.size())
	{
		if (i < b.size() && (j >= c.size() || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<c.size() && (i >= b.size() || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n;
	cin >> n;
	int d;
	cin >> d;
	int MAX = 0;
	vector<vector<int> > a(n,vector<int>(2));
	for (int i = 0; i < n; i++)
	{
		cin >> a[i][0] >> a[i][1];
		MAX = max(MAX, a[i][1]);
	}

	a = merge_sort(a);

	for (int i = 0; i < n; i++)
	{
		int j = i + 1;
		int k = a[i][1];
		for (; j<n && a[j][0] - a[i][0]<d; j++)
			k += a[j][1];
		MAX = max(MAX, k);
		if (j == n)
			break;
	}
	cout << MAX;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<int> > merge_sort(vector<vector<int> > a)
{
	if (a.size() <= 1)
		return a;
	vector<vector<int> > b(a.size() / 2);
	vector<vector<int> > c(a.size() - b.size());
	for (int i = 0; i < b.size(); i++)
		b[i] = a[i];
	for (int i = 0; i < c.size(); i++)
		c[i] = a[b.size() + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < a.size())
	{
		if (i < b.size() && (j >= c.size() || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<c.size() && (i >= b.size() || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n;
	cin >> n;
	int d;
	cin >> d;
	long long MAX = 0;
	vector<vector<int> > a(n,vector<int>(2));
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i][0]);
		scanf("%d", &a[i][1]);
		if (a[i][1]>MAX)
			MAX = a[i][1];
	}

	a = merge_sort(a);

	for (int i = 0; i < n; i++)
	{
		int j = i + 1;
		long long k = a[i][1];
		for (; j < n && a[j][0] - a[i][0] < d; j++)
			k = k + a[j][1];
		MAX = max(MAX, k);
		if (j == n)
			break;
	}
	cout << MAX;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<int> > merge_sort(vector<vector<int> > a)
{
	int asize = a.size();
	if (asize <= 1)
		return a;
	bool flag = true;
	for (int i = 0; i < asize-1;i++)
		if (a[i][0]>a[i+1][0])
		{
			flag = false;
			break;
		}
	if (flag)
		return a;
	int bsize = asize / 2;
	int csize = asize - bsize;
	vector<vector<int> > b(bsize);
	vector<vector<int> > c(csize);
	for (int i = 0; i < bsize; i++)
		b[i] = a[i];
	for (int i = 0; i < csize; i++)
		c[i] = a[bsize + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < asize)
	{
		if (i < bsize && (j >= csize || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<csize && (i >= bsize || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n;
	scanf("%d",&n);
	int d;
	scanf("%d", &d);
	long long MAX = 0;
	vector<vector<int> > a(n,vector<int>(2));
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i][0]);
		scanf("%d", &a[i][1]);
		if (a[i][1]>MAX)
			MAX = a[i][1];
	}

	a = merge_sort(a);

	for (int i = 0; i < n; i++)
	{
		int j = i + 1;
		long long k = a[i][1];
		for (; j < n && a[j][0] - a[i][0] < d; j++)
			k = k + a[j][1];
		MAX = max(MAX, k);
		if (j == n)
			break;
	}
	cout << MAX;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<int> > merge_sort(vector<vector<int> > a)
{
	int asize = a.size();
	if (asize <= 1)
		return a;
	/*bool flag = true;
	for (int i = 0; i < asize-1;i++)
	if (a[i][0]>a[i+1][0])
	{
	flag = false;
	break;
	}
	if (flag)
	return a;*/
	int bsize = asize / 2;
	int csize = asize - bsize;
	vector<vector<int> > b(bsize);
	vector<vector<int> > c(csize);
	for (int i = 0; i < bsize; i++)
		b[i] = a[i];
	for (int i = 0; i < csize; i++)
		c[i] = a[bsize + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < asize)
	{
		if (i < bsize && (j >= csize || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<csize && (i >= bsize || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n, d;
	scanf("%d%d", &n, &d);
	long long MAX = 0;
	vector<vector<int> > a(n, vector<int>(2));
	for (int i = 0; i < n; i++)
	{
		scanf("%d%d", &a[i][0], &a[i][1]);
		if (a[i][1]>MAX)
			MAX = a[i][1];
	}

	//a = merge_sort(a);
	sort(a.begin(), a.end());

	long long pred = a[0][1];
	int pos = 1;
	for (int i = 0; i < n; i++)
	{
		if (i == 0 || a[pos][0] - a[i][0] < d)
		{
			long long k = pred;
			if (i > 0)
			{
					k = k - a[i-1][1];
			}
			for (; pos < n && a[pos][0] - a[i][0] < d; pos++)
				k = k + a[pos][1];
			pred = k;
			MAX = max(MAX, pred);
			if (pos == n)
				break;
		}
		else
		{
			pred -= a[i - 1][1];
		}
	}
	cout << MAX;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<long long> > merge_sort(vector<vector<long long> > a)
{
	if (a.size() <= 1)
		return a;
	vector<vector<long long> > b(a.size() / 2);
	vector<vector<long long> > c(a.size() - b.size());
	for (int i = 0; i < b.size(); i++)
		b[i] = a[i];
	for (int i = 0; i < c.size(); i++)
		c[i] = a[b.size() + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < a.size())
	{
		if (i < b.size() && (j >= c.size() || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<c.size() && (i >= b.size() || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n;
	cin >> n;
	int d;
	cin >> d;
	long long MAX = 0;
	vector<vector<long long> > a(n,vector<long long>(2));
	for (int i = 0; i < n; i++)
	{
		cin >> a[i][0] >> a[i][1];
		MAX = max(MAX, a[i][1]);
	}

	a = merge_sort(a);

	for (int i = 0; i < n; i++)
	{
		int j = i + 1;
		long long k = a[i][1];
		for (; j < n && a[j][0] - a[i][0] <= d; j++)
			k = k + a[j][1];
		MAX = max(MAX, k);
		if (j == n)
			break;
	}
	cout << MAX;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<int> > merge_sort(vector<vector<int> > a)
{
	int asize = a.size();
	if (asize <= 1)
		return a;
	/*bool flag = true;
	for (int i = 0; i < asize-1;i++)
		if (a[i][0]>a[i+1][0])
		{
			flag = false;
			break;
		}
	if (flag)
		return a;*/
	int bsize = asize / 2;
	int csize = asize - bsize;
	vector<vector<int> > b(bsize);
	vector<vector<int> > c(csize);
	for (int i = 0; i < bsize; i++)
		b[i] = a[i];
	for (int i = 0; i < csize; i++)
		c[i] = a[bsize + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < asize)
	{
		if (i < bsize && (j >= csize || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<csize && (i >= bsize || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n,d;
	scanf("%d%d",&n, &d);
	long long MAX = 0;
	vector<vector<int> > a(n,vector<int>(2));
	for (int i = 0; i < n; i++)
	{
		scanf("%d%d", &a[i][0], &a[i][1]);
		if (a[i][1]>MAX)
			MAX = a[i][1];
	}

	a = merge_sort(a);

	for (int i = 0; i < n; i++)
	{
		int j = i + 1;
		long long k = a[i][1];
		for (; j < n && a[j][0] - a[i][0] < d; j++)
			k = k + a[j][1];
		MAX = max(MAX, k);
		if (j == n)
			break;
	}
	cout << MAX;
	return 0;
}


#include <cstdio>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>

#pragma warning(disable: 4996)

using namespace std;

vector<vector<int> > merge_sort(vector<vector<int> > a)
{
	int asize = a.size();
	if (asize <= 1)
		return a;
	//bool flag = true;
	//for (int i = 0; i < asize-1;i++)
		//if (a[i][0]>a[i+1][0])
//		{
	//		flag = false;
		//	break;
//		}
	//if (flag)
		//return a;
	int bsize = asize / 2;
	int csize = asize - bsize;
	vector<vector<int> > b(bsize);
	vector<vector<int> > c(csize);
	for (int i = 0; i < bsize; i++)
		b[i] = a[i];
	for (int i = 0; i < csize; i++)
		c[i] = a[bsize + i];
	b = merge_sort(b);
	c = merge_sort(c);
	int i = 0, j = 0;
	int link = 0;
	while (link < asize)
	{
		if (i < bsize && (j >= csize || b[i][0] <= c[j][0]))
		{
			a[link] = b[i];
			i++;
		}
		else if (j<csize && (i >= bsize || b[i][0]>c[j][0]))
		{
			a[link] = c[j];
			j++;
		}
		link++;
	}
	return a;
}

vector<int> summ(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k += b[j];
		ans.push_back(k % 10);
		k = k / 10;
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> sub(vector<int> a, vector<int> b)
{
	int k = 0;
	vector<int> ans;
	int i = 0, j = 0;
	while (i < a.size() || j < b.size())
	{
		if (i < a.size())
			k += a[i];
		if (j < b.size())
			k -= b[j];
		if (k < 0)
		{
			k = k + 10;
			if (j < b.size())
				b[j + 1] + 1;
			else
				b.push_back(1);
		}
		ans.push_back(k % 10);
		i++;
		j++;
	}
	if (k > 0)
		ans.push_back(k % 10);
	return ans;
}

vector<int> mul(vector<int>a, vector<int>b)
{
	int i = 0, j = 0;
	vector<vector<int> >ans(1);
	ans[0].push_back(0);
	int kol0 = 0;
	while (i < a.size() && a[i] == 0)
	{
		kol0++;
		i++;
	}
	while (j < b.size() && b[j] == 0)
	{

		kol0++;
		j++;
	}
	if (i >= a.size() || j >= b.size())
		return ans[0];
	if (a.size() - i < b.size() - j)
	{
		swap(a, b);
		swap(i, j);
	}
	int r = 1;
	while (j < b.size())
	{
		ans.resize(ans.size() + 1);
		for (int i = 1; i < r; i++)
			ans[r].push_back(0);
		int k = 0;
		for (int p = i; p < a.size(); p++)
		{
			ans[r].push_back(a[p] * b[j] + k);
			k = ans[r][ans[r].size() - 1] / 10;
			ans[r][ans[r].size() - 1] %= 10;
		}
		if (k > 0)
			ans[r].push_back(k);
		r++;
		j++;
	}
	for (int i = 1; i < ans.size(); i++)
		ans[0] = summ(ans[0], ans[i]);
	vector<int> answer;
	for (int i = 0; i < kol0; i++)
		answer.push_back(0);
	for (int i = 0; i < ans[0].size(); i++)
		answer.push_back(ans[0][i]);
	return answer;
}

int compare(vector<int> a, vector<int> b)
{
	if (a.size() > b.size())
		return 1;
	else
		if (a.size() < b.size())
			return -1;
	for (int i = 0; i < a.size(); i++)
	{
		if (a[i] > b[i])
			return 1;
		if (a[i] < b[i])
			return -1;
	}
	return 0;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("dragon.out", "w", stdout);
	int n;
	scanf("%d",&n);
	int d;
	scanf("%d", &d);
	long long MAX = 0;
	vector<vector<int> > a(n,vector<int>(2));
	for (int i = 0; i < n; i++)
	{
		scanf("%d", &a[i][0]);
		scanf("%d", &a[i][1]);
		if (a[i][1]>MAX)
			MAX = a[i][1];
	}

	a = merge_sort(a);

	for (int i = 0; i < n; i++)
	{
		int j = i + 1;
		long long k = a[i][1];
		for (; j < n && a[j][0] - a[i][0] < d; j++)
			k = k + a[j][1];
		MAX = max(MAX, k);
		if (j == n)
			break;
	}
	cout << MAX;
	return 0;
}


#include <iostream>
#include <cmath>

int main(){
    int n;
    int d;
    int m[100000];
    int s[100000];

    std::cin >> n >> d;

    for(int i = 0; i < n; ++i){
        std::cin >> m[i] >> s[i];
    }

    int r = n / 2;
    bool k = true;
    int tmp;
    while (r > 0){
        while (k){
            k = false;
            for(int j = 0; j < n - r; ++j){
                if (m[j] > m[j+r]){

                    k = true;

                    tmp = m[j];
                    m[j] = m[j+r];
                    m[j+r] = tmp;

                    tmp = s[j];
                    s[j] = s[j+r];
                    s[j+r] = tmp;

                }
            }
        }
        k = true;
        r /= 2;
    }

    unsigned max = s[0];
    unsigned sum;

    int i = 0;

    while (i < n) {
        sum = s[i];
        for (int j = i + 1; j < n; ++j){
            if (std::abs(m[j] - m[i]) < d){
                sum += s[j];
                if (max < sum) max = sum;
            }
            else{
                break;
            }
        }
        ++i;
        if (max < sum) max = sum;
    }

    std::cout << max;

    return 0;
}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

void insertionsort(string a[],string b[], int N) {
  for(int i = 1; i < N; i++) {
    int j = i - 1;
    string temp = a[i];
    string TEMP = b[i];

    while(j >= 0 && temp < a[j]) {
      a[j + 1] = a[j];
      b[j + 1] = b[j];
      j--;;
    }

    a[j + 1] = temp;
    b[j + 1] = TEMP;
  }
}

int binary_search(string array[], int left, int right, string key) {
  if (left > right) {
    return -1;
  }

  int mid = (left + right) / 2;

  if (array[mid] == key) {
    return mid;
  }

  if (array[mid] > key) {
    return binary_search(array, left, mid - 1, key);
  }

  return binary_search(array, mid + 1, right, key);
}

int size(string a)
{
    int i=0;
    while(a[i]!='\0')
        i++;
    return i;
}

int main()
{
    int n,m;
    cin>>n>>m;
    string a[m],b[m],d[n];
    char c[n][11];
    for(int i=0;i<m;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<n;i++)
        cin>>c[i];

    insertionsort(a,b,m);

    for(int i=0;i<n;i++)
        {
            int val = binary_search(a, 0, n - 1, c[i]);
            if(size(a[val])>size(b[val]))   d[i]=b[val];
            else                                d[i]=c[i];
        }

    for(int i=0;i<n;i++)
        cout<<d[i]<<' ';
    cout<<'\n';

    return 0;
}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

void insertionsort(string a[],string b[], int N) {
  for(int i = 1; i < N; i++) {
    int j = i - 1;
    string temp = a[i];
    string TEMP = b[i];

    while(j >= 0 && temp < a[j]) {
      a[j + 1] = a[j];
      b[j + 1] = b[j];
      j--;;
    }

    a[j + 1] = temp;
    b[j + 1] = TEMP;
  }
}

int binary_search(string array[], int left, int right, string key) {
  if (left > right) {
    return 1;
  }

  int mid = (left + right) / 2;

  if (array[mid] == key) {
    return mid;
  }

  if (array[mid] > key) {
    return binary_search(array, left, mid - 1, key);
  }

  return binary_search(array, mid + 1, right, key);
}

int size(string a)
{
    int i=0;
    while(a[i]!='\0')
        i++;
    return i;
}

int main()
{
    int n,m;
    cin>>n>>m;
    string a[m],b[m],d[n];
    char c[n][11];
    for(int i=0;i<m;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<n;i++)
        cin>>c[i];

    insertionsort(a,b,m);

    for(int i=0;i<n;i++)
        {
            int val = binary_search(a, 0, n - 1, c[i]);
            if(size(a[val])>size(b[val]))   d[i]=b[val];
            else                            d[i]=c[i];
        }

    for(int i=0;i<n;i++)
        cout<<d[i]<<' ';
    cout<<'\n';

    return 0;;
}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

void insertionsort(string a[],string b[], int N) {
  for(int i = 1; i < N; i++) {
    int j = i - 1;
    string temp = a[i];
    string TEMP = b[i];

    while(j >= 0 && temp < a[j]) {
      a[j + 1] = a[j];
      b[j + 1] = b[j];
      j--;;
    }

    a[j + 1] = temp;
    b[j + 1] = TEMP;
  }
}

int binary_search(string array[], int left, int right, string key) {
  if (left > right) {
    return 0;
  }

  int mid = (left + right) / 2;

  if (array[mid] == key) {
    return mid;
  }

  if (array[mid] > key) {
    return binary_search(array, left, mid - 1, key);
  }

  return binary_search(array, mid + 1, right, key);
}

int size(string a)
{
    int i=0;
    while(a[i]!='\0')
        i++;
    return i;
}

int main()
{
    int n,m;
    cin>>n>>m;
    string a[m],b[m],d[n];
    char c[n][11];
    for(int i=0;i<m;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<n;i++)
        cin>>c[i];

    insertionsort(a,b,m);

    for(int i=0;i<n;i++)
        {
            int val = binary_search(a, 0, n - 1, c[i]);
            //if(val == -1)   continue;
            if(size(a[val])>size(b[val]))   d[i]=b[val];
            else                            d[i]=c[i];
        }

    for(int i=0;i<n;i++)
        cout<<d[i]<<' ';
    cout<<'\n';

    return 0;
}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

void insertionsort(string a[],string b[], int N) {
  for(int i = 1; i < N; i++) {
    int j = i - 1;
    string temp = a[i];
    string TEMP = b[i];

    while(j >= 0 && temp < a[j]) {
      a[j + 1] = a[j];
      b[j + 1] = b[j];
      j--;;
    }

    a[j + 1] = temp;
    b[j + 1] = TEMP;
  }
}

int binary_search(string array[], int left, int right, string key) {
  if (left > right) {
    return -1;
  }

  int mid = (left + right) / 2;

  if (array[mid] == key) {
    return mid;
  }

  if (array[mid] > key) {
    return binary_search(array, left, mid - 1, key);
  }

  return binary_search(array, mid + 1, right, key);
}

int size(string a)
{
    int i=0;
    while(a[i]!='\0')
        i++;
    return i;
}

int main()
{
    int n,m;
    cin>>n>>m;
    string a[m],b[m],d[n];
    char c[n][11];
    for(int i=0;i<m;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<n;i++)
        cin>>c[i];

    insertionsort(a,b,m);
    //for(int i=0;i<m;i++)
    //    cout<<a[i]<<"\t\t\t"<<b[i]<<'\n';

    for(int i=0;i<n;i++)
        {
            int val = binary_search(a, 0, m - 1, c[i]);
            cout<<val<<"\t\t\t"<<c[i]<<'\n';
            if(size(a[val])>size(b[val]))   d[i]=b[val];
            else                            d[i]=c[i];
        }

    for(int i=0;i<n;i++)
        cout<<d[i]<<' ';
    cout<<'\n';

    return 0;
}


#include <iostream>
#include <string>
#include <vector>

using namespace std;

void insertionsort(string a[],string b[], int N) {
  for(int i = 1; i < N; i++) {
    int j = i - 1;
    string temp = a[i];
    string TEMP = b[i];

    while(j >= 0 && temp < a[j]) {
      a[j + 1] = a[j];
      b[j + 1] = b[j];
      j--;;
    }

    a[j + 1] = temp;
    b[j + 1] = TEMP;
  }
}

int binary_search(string array[], int left, int right, string key) {
  if (left > right) {
    return 1;
  }

  int mid = (left + right) / 2;

  if (array[mid] == key) {
    return mid;
  }

  if (array[mid] > key) {
    return binary_search(array, left, mid - 1, key);
  }

  return binary_search(array, mid + 1, right, key);
}

int size(string a)
{
    int i=0;
    while(a[i]!='\0')
        i++;
    return i;
}

int main()
{
    int n,m;
    cin>>n>>m;
    string a[m],b[m],d[n];
    char c[n][11];
    for(int i=0;i<m;i++)
        cin>>a[i]>>b[i];
    for(int i=0;i<n;i++)
        cin>>c[i];

    insertionsort(a,b,m);

    for(int i=0;i<n;i++)
        {
            int val = binary_search(a, 0, n - 1, c[i]);
            if(size(a[val])>size(b[val]))   d[i]=b[val];
            else                            d[i]=c[i];
        }

    for(int i=0;i<n;i++)
        cout<<d[i]<<' ';
    cout<<'\n';

    return 0;;;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<pair<string ,string>> vec;
	vector <string> lec;
	string s1,s2,temp;
	bool found=false;

	for(int i=0;i<m;i++)
	{
		cin>>s1>>s2;
		vec.push_back(make_pair(s1,s2));

		//cout<<vec[i].first <<"  "<<vec[i].second;
	}
	for(int i=0;i<n;i++)
	{
		cin>>temp;
		lec.push_back(temp);
	}
	for (int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if((lec[i]==vec[j].first )&&( vec[j].second.length()<lec[i].length()))
			{
				cout<<vec[j].second<<" ";
				found=true;
				break;
			}
		}
		if (found==false)
			cout<<lec[i]<<" ";
	}

	return 0;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<pair<string ,string>> vec;
	vector <string> lec;
	string s1,s2,temp;
	bool found=false;

	for(int i=0;i<m;i++)
	{
		cin>>s1>>s2;
		vec.push_back(make_pair(s1,s2));

		//cout<<vec[i].first <<"  "<<vec[i].second;
	}
	for(int i=0;i<n;i++)
	{
		cin>>temp;
		lec.push_back(temp);
	}
	for (int i=0;i<n;i++)
	{
		for(int j=0;j;j++)
		{
			if((lec[i]==vec[j].first )&&( vec[j].second.length()<lec[i].length()))
			{
				cout<<vec[j].second<<" ";
				found=true;
				break;
			}
		}
		if (found=false)
			cout<<lec[i]<<" ";
	}

	return 0;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<pair<string ,string>> vec;
	vector <string> lec;
	string s1,s2,temp;

	for(int i=0;i<m;i++)
	{
		cin>>s1>>s2;
		vec.push_back(make_pair(s1,s2));

		//cout<<vec[i].first <<"  "<<vec[i].second;
	}
	for(int i=0;i<n;i++)
	{
		cin>>temp;
		lec.push_back(temp);
	}
	for (int i=0;i<n;i++)
	{
		if((lec[i]==vec[i].first )&&( vec[i].second.length()<lec[i].length()))
			cout<<vec[i].second<<" ";
		else
			cout<<lec[i]<<" ";
	}

	return 0;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<pair<string ,string>> vec;
	vector <string> lec;
	string s1,s2,temp;
	bool found=false;

	for(int i=0;i<m;i++)
	{
		cin>>s1>>s2;
		vec.push_back(make_pair(s1,s2));

		//cout<<vec[i].first <<"  "<<vec[i].second;
	}
	for(int i=0;i<n;i++)
	{
		cin>>temp;
		lec.push_back(temp);
	}
	for (int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if((lec[i]==vec[j].first )&&( vec[j].second.length()<lec[i].length()))
			{
				cout<<vec[j].second<<" ";
				found=true;
				break;
			}
		}
		if (found=false)
			cout<<lec[i]<<" ";
	}

	return 0;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<pair<string ,string>> vec;
	vector <string> lec;
	string s1,s2,temp;

	for(int i=0;i<m;i++)
	{
		cin>>s1>>s2;
		vec.push_back(make_pair(s1,s2));

		//cout<<vec[i].first <<"  "<<vec[i].second;
	}
	for(int i=0;i<n;i++)
	{
		cin>>temp;
		lec.push_back(temp);
	}
	for (int i=0;i<n;i++)
	{
		if((lec[i]==vec[i].first )&&( lec[i].length()<vec[i].second.length()))
			cout<<vec[i].second<<" ";
		else
			cout<<lec[i]<<" ";
	}

	return 0;
}


#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
	int n,m;
	cin>>n>>m;
	vector<pair<string ,string>> vec;
	vector <string> lec;
	string s1,s2,temp;
	bool found=false;

	for(int i=0;i<m;i++)
	{
		cin>>s1>>s2;
		vec.push_back(make_pair(s1,s2));

		//cout<<vec[i].first <<"  "<<vec[i].second;
	}
	for(int i=0;i<n;i++)
	{
		cin>>temp;
		lec.push_back(temp);
	}
	for (int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			if((lec[i]==vec[j].first )&&( vec[j].second.length()<lec[i].length()))
			{
				cout<<vec[j].second<<" ";
				found=true;
				break;
			}
			else
				found=false;
		}
		if (found==false)
			cout<<lec[i]<<" ";
	}

	return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<fstream>
#include<string>
#include<map>
#include<math.h>
#include<vector>
#include <stdio.h>
#include <string.h>
#include<list>
using namespace std;
ofstream fo("output.txt");
ifstream fi("input.txt");
#define maxn 100010
#define N 200005
const int maxm = 50;
char Map[maxm][maxm];
int num[500005];
int main()
{
	map<string, string> a;
	a.clear();
	int n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		string s1, s2;
		cin >> s1 >> s2;
		string c = s1.size() <= s2.size() ? s1 : s2;
		a[s1] = c;
		a[s2] = c;
	}
	for (int i = 0; i < m; i++)
	{
		string b;
		cin >> b;
		cout << a[b] << " ";
	}
	cout << endl;
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<fstream>
#include<string>
#include<map>
#include<math.h>
#include<vector>
#include <stdio.h>
#include <string.h>
#include<list>
using namespace std;
ofstream fo("output.txt");
ifstream fi("input.txt");
#define maxn 100010
#define N 200005
const int maxm = 50;
char Map[maxm][maxm];
int num[500005];
int main()
{
	map<string, string> mp;
	int n, m;
	mp.clear();
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		string a;
		string b;
		cin >> a;
		cin >> b;
		string c = a.size() <= b.size() ? a : b;
		mp[a] = c;
		mp[b] = c;
	}
	for (int i = 0; i < n; i++) {
		string a;
		cin >> a;
		cout << mp[a] << " ";
	}
	cout << endl;
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<fstream>
#include<string>
#include<map>
#include<math.h>
#include<vector>
#include <stdio.h>
#include <string.h>
#include<list>
using namespace std;
ofstream fo("output.txt");
ifstream fi("input.txt");
#define maxn 100010
#define N 200005
const int maxm = 50;
char Map[maxm][maxm];
int num[500005];
int main()
{
	map<string, string> a;
	a[" "] = " ";
	int n, m;
	string s1, s2;
	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		cin >> s1 >> s2;
		if (s1.length() > s2.length())
		{
			a[s1] = s2;
			a[s2] = s2;
		}
		else
		{
			a[s1] = s1;
			a[s2] = s1;
		}
	}
	string b[3001];
	for (int i = 0; i < m; i++)
	{
		cin >> b[i];
		b[i] = a[b[i]];
	}
	for (int i = 0; i < m; i++)
		cout << b[i] << " ";
	cout << endl;
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<fstream>
#include<string>
#include<map>
#include<math.h>
#include<vector>
#include <stdio.h>
#include <string.h>
#include<list>
using namespace std;
ofstream fo("output.txt");
ifstream fi("input.txt");
#define maxn 100010
#define N 200005
const int maxm = 50;
char Map[maxm][maxm];
int num[500005];
int main()
{
	map<string, string> a;
	a[" "] = " ";
	int n, m;

	cin >> n >> m;
	for (int i = 0; i < n; i++)
	{
		string s1, s2;
		cin >> s1 >> s2;
		if (s1.length() > s2.length())
		{
			a[s1] = s2;
			a[s2] = s2;
		}
		else
		{
			a[s1] = s1;
			a[s2] = s1;
		}
	}
	string b[3001];
	for (int i = 0; i < m; i++)
	{
		cin >> b[i];
		b[i] = a[b[i]];
	}
	for (int i = 0; i < m; i++)
		cout << b[i] << " ";
	cout << endl;
	//system("pause");
	return 0;
}


#include<iostream>
#include<math.h>
#include<algorithm>
#include<fstream>
#include<string>
#include<map>
#include<math.h>
#include<vector>
#include <stdio.h>
#include <string.h>
#include<list>
using namespace std;
ofstream fo("output.txt");
ifstream fi("input.txt");
#define maxn 100010
#define N 200005
const int maxm = 50;
char Map[maxm][maxm];
int num[500005];
int main()
{
	map<string, string> mp;
	int n, m;
	mp.clear();
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		string a;
		string b;
		cin >> a >> b;
		string c = a.size() <= b.size() ? a : b;
		mp[a] = c;
		mp[b] = c;
	}
	for (int i = 0; i < n; i++) {
		string a;
		cin >> a;
		cout << mp[a] << " ";
	}
	cout << endl;
	//system("pause");
	return 0;
}