首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】580B - Kefa and Company(二分)

【CodeForces】580B - Kefa and Company(二分)

作者头像
FishWang
发布2025-08-26 20:31:46
发布2025-08-26 20:31:46
11400
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

B. Kefa and Company

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

代码语言:javascript
代码运行次数:0
运行
复制
4 5
75 5
0 100
150 20
75 1

output

代码语言:javascript
代码运行次数:0
运行
复制
100

input

代码语言:javascript
代码运行次数:0
运行
复制
5 100
0 7
11 32
99 10
46 8
87 54

output

代码语言:javascript
代码运行次数:0
运行
复制
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.

这道题我手写了二分,听队友说直接for循环搜也能过,神奇。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <algorithm>
using namespace std;
int n,d;
struct node
{
	int w,ant;
}data[100000+11];
__int64 sum[100000+11];
bool cmp(node a,node b)
{
	return a.w < b.w;
}
int upper_find(int x)
{
	int l = 0;
	int r = n - 1;
	int mid;
	while (r >= l)
	{
		mid = (l + r) >> 1;
		if (data[mid].w > x)
			r = mid - 1;
		else
			l = mid + 1;
	}
	return l;
}
int main()
{
	__int64 ans;
	int st,endd;
	while (~scanf ("%d %d",&n,&d))
	{
		for (int i = 0  ; i < n ; i++)
			scanf ("%d %d",&data[i].w,&data[i].ant);
		sort(data , data + n , cmp);
		sum[0] = data[0].ant;
		for (int i = 1 ; i < n ; i++)
			sum[i] = sum[i-1] + data[i].ant;
		ans = 0;
		for (int i = 0 ; i < n ; i++)
		{
			endd = upper_find(data[i].w + d - 1) - 1;
			ans = max (ans , sum[endd] - sum[i] + data[i].ant);
		}
		printf ("%I64d\n",ans);
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档