Kefa and Company CodeForces - 580B

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.

Example

Input

4 575 50 100150 2075 1

Output

100

Input

5 1000 711 3299 1046 887 54

Output

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.

先排序再利用尺取法,不断更新答案

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<cstdio>
 4 #include<string>
 5 using namespace std;
 6
 7 typedef long long ll;
 8 struct node{
 9     int mo;
10     int fr;
11 }N[100005];
12
13 bool operator <(const node& a,const node& b)
14 {   return a.mo<b.mo;
15 }
16
17 int main()
18 {   int n,d;
19     while(~scanf("%d%d",&n,&d)){
20         for(int i=1;i<=n;i++) scanf("%d%d",&N[i].mo,&N[i].fr);
21         sort(N+1,N+n+1);
22         int l=1,r=1;
23         ll ans=0,sum=0,temp=0;
24         while(r<=n){
25             sum=N[r].mo-N[l].mo;
26             temp+=N[r].fr;
27             while(sum>=d){               //一定是大于等于!!!
28                 temp-=N[l].fr;
29                 l++;
30                 sum=N[r].mo-N[l].mo;
31             }
32             r++;
33             ans=max(ans,temp);
34         }
35         printf("%lld\n",ans);
36     }
37     return 0;
38 }
时间: 2024-11-07 14:00:55

Kefa and Company CodeForces - 580B的相关文章

CodeForces 580B Kefa and Company

Description: 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 a

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

排序以后O(n)枚举尾部.头部单调,维护一下就好. #include<bits/stdc++.h> using namespace std; typedef long long ll; //#define LOCAL const int maxn = 1e5+5; struct Node { int m,s; void IN(){ scanf("%d%d",&m,&s); } bool operator < (const Node&rh) co

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

题目链接:http://codeforces.com/problemset/problem/580/B 某人有n个朋友,这n个朋友有钱数m和关系s两个属性.问如何选择朋友,使得这些朋友之间s最大差距小于d并且钱数是最多. 可以用滑动窗口,将m从小到大,s从大到小排列,这时在一个队列里维护队首和队尾,假如队首和队尾的s差距≥d时,就把队尾扔掉队首入队否则就仅队首入队.此时更新一下当前最大值. 1 #include <algorithm> 2 #include <iostream> 3

Codeforces Round #321 (Div. 2) A, B, C, D, E

580A. Kefa and First Steps 题目链接: A. Kefa and First Steps 题意描述: 给出一个序列,求最长公共子序列多长? 解题思路: 水题,签到 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 7 int main () 8 { 9 in

2017-5-6-Train:Codeforces Round #321 (Div. 2)

A. Kefa and First Steps(最长不下降子串) Kefa decided to make some money doing business on the Internet for exactly n days. He knows that on the i-th day (1 ≤ i ≤ n) he makes a**i money. Kefa loves progress, that's why he wants to know the length of the maxi

练习赛44

练习赛44 Begin: 2019-04-03 18:35 PDD:355rlb A Kefa and Park CodeForces 580C 标签:暴力dfs(55) 题目大意: ? 一棵以1为根的树,树上有些点是红的.一个叶子是合法的当且仅当从根到它的路径上出现的连续红点个数不超过m.求有多少个叶子是合法的. 思路: 直接搜索就可以了,注意细节. CODE: #include<bits/stdc++.h> using namespace std; const int N=1e5+5; b

[2016-03-23][codeforces][580][A][Kefa and First Steps]

时间:2016-03-23 13:04:56 星期三 题目编号:[2016-03-23][codeforces][580][A][Kefa and First Steps] 题目大意:求最长上升子串 #include <cstdio> using namespace std; int a[100000 + 100]; int main(){ int n; scanf("%d",&n); for(int i = 0 ;i < n ;++i) scanf(&quo

Codeforces 841A - Generous Kefa

题目链接:http://codeforces.com/problemset/problem/841/A One day Kefa found n baloons. For convenience, we denote color of i-th baloon as si — lowercase letter of the Latin alphabet. Also Kefa has k friends. Friend will be upset, If he get two baloons of

dp + 状态压缩 - Codeforces 580D Kefa and Dishes

Kefa and Dishes Problem's Link Mean: 菜单上有n道菜,需要点m道.每道菜的美味值为ai. 有k个规则,每个规则:在吃完第xi道菜后接着吃yi可以多获得vi的美味值. 问:最多可以获得多少美味值? (1≤m≤n≤18,0≤k≤n∗(n−1)) analyse: 经典的状压DP. 由于最多18道菜,可用一个数s(s<=2^18)来唯一标识一种状态. 对于一个状态s,枚举两个位置i和j:i从已选择的菜中选定,j从未选择的菜中选定. 下一个状态ss的就是:吃完i后接着