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: wisdom level; 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
n
wi
fi
Output Format
Return an integer denoting the maximum possible sum of the friendship factors of selected friends.
Evaluation Parameters
Sample Input
Sample Output
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
Selecting a third and second friend will result in a total friendship factor of
Thus 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
