580B.md (1881B)
1 Source: https://codeforces.com/problemset/problem/580/B 2 3 B. Kefa and Company 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. 22 23 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! 24 25 Input 26 27 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. 28 29 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 *m**i*, *s**i* (0 ≤ *m**i*, *s**i* ≤ 109) — the amount of money and the friendship factor, respectively. 30 31 Output 32 33 Print the maximum total friendship factir that can be reached. 34 35 Examples 36 37 Input 38 39 ``` 40 4 5 41 75 5 42 0 100 43 150 20 44 75 1 45 ``` 46 47 Output 48 49 ``` 50 100 51 ``` 52 53 Input 54 55 ``` 56 5 100 57 0 7 58 11 32 59 99 10 60 46 8 61 87 54 62 ``` 63 64 Output 65 66 ``` 67 111 68 ``` 69 70 Note 71 72 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. 73 74 In the second sample test we can take all the friends.