Choose numbers to maximize total score, but taking a value removes its neighbors.
Boredom
You are given an array of integers. You may choose any integer value and gain points equal to the sum of all occurrences of in the array. However, after choosing , you are not allowed to choose or .
Your task is to compute the maximum total points you can obtain.
This is equivalent to picking a set of values so that no two chosen values differ by exactly 1, while maximizing the total weight collected from all occurrences of the chosen values.
Input Format
- The first line contains an integer — the number of elements in the array.
- The second line contains integers .
Output Format
- Print one integer: the maximum points that can be obtained.
Constraints
Example 1
Input
5 1 2 3 1 3
Output
4
Explanation
Take value 1 for 2 points and value 3 for 6 points would conflict with 2, but the best choice is taking only value 3 or value 1 and 3 are adjacent? Actually 1 and 3 do not conflict. Total from 1 is 2 and from 3 is 6, so the best score is 8.
Example 2
Input
4 1 2 3 4
Output
6
Explanation
Take values 2 and 4, or 1 and 3. The best is 2 + 4 = 6.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.