Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company.

Kefa has n friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least d units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company!

Input

The first line of the input contains two space-separated integers, n and d (1 ≤ n ≤ 105, ) — the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively.

Next n lines contain the descriptions of Kefa's friends, the (i + 1)-th line contains the description of the i-th friend of type mi, si (0 ≤ mi, si ≤ 109) — the amount of money and the friendship factor, respectively.

Output

Print the maximum total friendship factir that can be reached.

Examples
Input

Copy
4 5
75 5
0 100
150 20
75 1
Output

Copy
100
Input

Copy
5 100
0 7
11 32
99 10
46 8
87 54
Output

Copy
111
Note

In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse.

In the second sample test we can take all the friends.

双指针扫一遍即可;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 200005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} ll n, d;
struct fr {
ll m, s;
}frd[maxn]; bool cmp(fr x, fr y) {
return x.m < y.m;
} ll sum[maxn]; int main()
{
//ios::sync_with_stdio(0);
rdllt(n); rdllt(d);
for (int i = 1; i <= n; i++)rdllt(frd[i].m), rdllt(frd[i].s);
sort(frd + 1, frd + 1 + n, cmp);
ll maxx = -inf;
int point1 = 1, point2 = 1;
for (int i = 1; i <= n; i++)sum[i] = sum[i - 1] + frd[i].s; while (point2 <= n) {
if (frd[point2].m - frd[point1].m < d) {
maxx = max(maxx, sum[point2] - sum[point1 - 1]);point2++;
}
else {
point1++; } }
cout << maxx << endl;
return 0;
}

CF580B Kefa and Company 尺取法的更多相关文章

  1. [CF580B]Kefa and Company(滑动窗口)

    题目链接:http://codeforces.com/problemset/problem/580/B 某人有n个朋友,这n个朋友有钱数m和关系s两个属性.问如何选择朋友,使得这些朋友之间s最大差距小 ...

  2. Codeforces Round #321 (Div. 2) B. Kefa and Company 二分

    B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...

  3. CodeForces 580B(尺取法)

    Kefa and Company 题意:Kefa这个人要去吃饭,他要邀请一些朋友一起去,他的每个朋友有两个属性金钱和关系度,要求邀请的人里边任意两个人之间的金钱差的绝对值不大于d:求被邀请的所有朋友的 ...

  4. Unique Snowflakes UVA - 11572 (离散化+尺取法)

    Emily the entrepreneur has a cool business idea: packaging and selling snowflakes. She has devised a ...

  5. 5806 NanoApe Loves Sequence Ⅱ(尺取法)

    传送门 NanoApe Loves Sequence Ⅱ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K ...

  6. POJ3061 尺取法

    题目大意:从给定序列里找出区间和大于等于S的最小区间的长度. 前阵子在zzuli OJ上见过类似的题,还好当时补题了.尺取法O(n) 的复杂度过掉的.尺取法:从头遍历,如果不满足条件,则将尺子尾 部增 ...

  7. POJ 2739 Sum of Consecutive Prime Numbers(尺取法)

    题目链接: 传送门 Sum of Consecutive Prime Numbers Time Limit: 1000MS     Memory Limit: 65536K Description S ...

  8. CF 701C They Are Everywhere(尺取法)

    题目链接: 传送门 They Are Everywhere time limit per test:2 second     memory limit per test:256 megabytes D ...

  9. CF 321B Kefa and Company(贪心)

    题目链接: 传送门 Kefa and Company time limit per test:2 second     memory limit per test:256 megabytes Desc ...

随机推荐

  1. CentOS 7.2 部署Rsync + Lsyncd服务实现文件实时同步/备份 (二)

    发送端配置: 一.配置密钥 1. 主/从服务器之间启用基于密钥的身份验证.登录发送端服务器并用 " ssh-keygen " 命令生成公共或私有的密钥. 2. 使用 " ...

  2. JavaScript实现重置表单(reset)的方法

    转自:https://www.jb51.net/article/63305.htm <!DOCTYPE html> <html> <head> <script ...

  3. C#封装CRUD到SqlHelper类解读

    1.简单说明一下,一般情况下,数据库连接字符串是在App.config文件中进行配置,然后再在代码中进行引用.因此,我们在这里先看一下App.config文件. 首先看需要添加的内容: 参数说明: n ...

  4. hbase性能调优(转载)

    一.服务端调优 1.参数配置 1).hbase.regionserver.handler.count:该设置决定了处理RPC的线程数量,默认值是10,通常可以调大,比如:150,当请求内容很大(上MB ...

  5. Android 数据库 OrmLite Failed to open database

    04-01 16:49:32.720: E/SQLiteLog(1894): (14) cannot open file at line 30204 of [00bb9c9ce4]04-01 16:4 ...

  6. sqlserver 查询int类型 in (字符串) 报转换int类型出错的问题

    , , '') ) AS c_departNames FROM t_user AS A LEFT JOIN t_role AS B ON A.c_roleId=B.c_roleId 用 CHARIND ...

  7. 托盘在XP下不能显示tooltip,在Vista和Windows7下正常

    转自:http://blog.csdn.net/debehe/article/details/4294053 奇怪的问题,想了很多可能的理由,最终的答案竟然是一开始就被我否认了的一种可能!! 问题现象 ...

  8. Struts2框架06 ValueStack

    原文地址:点击前往 1 什么是ValueStack 称为值栈,Struts提供的共享数据的数据结构 2 为什么要使用ValueStack 从控制器向浏览器传递数据 存储与请求相关的对象信息(sessi ...

  9. IFC数据模型在三维引擎中模拟

  10. WOJ 46 完全背包

    高级的暴力,神仙优化…… 首先$O(n^{3})$的$dp$很好想,然后这样可以$O(1)$地回答询问. 考虑到所有物品的体积是一个连续的区间,所以说我们可以合并一些物品来达到预处理时间均摊的效果. ...