Question: Problem Statement Alex is planning to go on a trip with his friends. Alex has n friends; each one of them has two properties: 1

Problem Statement
Alex is planning to go on a trip with his friends. Alex has n friends; each one of them has two properties: 1) wisdom level; 2) friendship factor with Alex.
Alex wants to select some of his friends so that the difference between the wisdom levels of any two selected friends is not greater than d and the total sum of the friendship factors of all the selected friends is as large as possible.
Help Alex find the maximum possible sum of the friendship factors of selected friends.
Input Format
The first line contains an integer d, denoting the maximum allowed wisdom level difference between any two selected friends.
The second line contains an integer n, denoting Alex's number of friends.
The following n lines denote wi, the wisdom level of each friend.
Then, the following line contains an integer n.
Then, the following n lines denote fi, the friendship factor of each friend.
Constraints
1<=n<=105
0<=wi<=109
0<=fi<=109
Output Format
Return an integer denoting the maximum possible sum of the friendship factors of selected friends.
Evaluation Parameters
Sample Input
15
3
10
20
30
3
5
15
25
Sample Output
40
Explanation
There are two possible ways for Alex to select friends for the trip: selecting the first and second friends or the second and third friends. All three friends can't be selected as the difference between the wisdom levels of the first and third friends is greater than d.
Selecting a first and second friend will result in a total friendship factor of 20.
Selecting a third and second friend will result in a total friendship factor of 40.
Thus 40 is the maximum possible sum of friendship factors we can create.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!