Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions