Birthday Presents
InterviewTime limit2sMemory limit512 MB
Pick a subset of presents whose price range is below D, maximizing total satisfaction.
- Level
Medium5 of 10
- Topics
- Sorting, Sliding window, Two pointers, Prefix sum
- Solved
- No attempts yet
Problem
Today is Kangmin's birthday. Kangmin has N friends, and every friend prepared one birthday present for him. Each present has a price P and a satisfaction value V. P is the price of the present, and V is a number for how happy Kangmin is when he receives that present.
Kangmin wants every present. The trouble is that if the price of one friend's present differs from the price of another friend's present by D or more, the friend who gave the cheaper present may feel sorry. Kangmin's own happiness matters to him, but he does not want a birthday present to make a friend feel sorry, so after some thought he decided to accept presents from only some of his friends.
Find the largest total satisfaction Kangmin can feel when he picks presents so that nobody feels sorry. Nobody feels sorry exactly when the price difference between the most expensive and the cheapest accepted present is smaller than D.
Input
The first line contains the number of friends N and the smallest price difference D that makes a friend feel sorry. (, )
Each of the next N lines contains the price P and the satisfaction value V of one present. (, )
Output
Print the maximum total satisfaction Kangmin can feel.