Pick elements in increasing value order, add selected values to the score, and mark their neighbors so they can no longer be chosen.
You are given an integer array nums.
You repeatedly perform the following process until every element is marked:
- Choose an unmarked element with the smallest value. If there are multiple, choose the one with the smallest index.
- Add its value to your score.
- Mark that element and also mark its immediate neighbors (the elements directly to the left and right, if they exist).
Return the final score after all elements are marked.
This problem asks you to simulate the marking process efficiently by always processing the smallest available value first.
Input Format
- An integer array
numsof lengthn. - Each
nums[i]is the value at indexi. - The process uses 0-based indexing conceptually, but the exact indexing is not part of the input.
Output Format
- Return a single integer: the final score obtained after repeatedly selecting and marking elements.
Constraints
1 <= n <= $10^{5}$1 <= nums[i] <= $10^{5}$- The answer fits in a 64-bit signed integer.
Example 1
Input
nums = [2, 1, 3, 4, 5, 2]
Output
7
Explanation
Choose 1 at index 1, add 1 to the score, and mark indices 0, 1, and 2. Next choose 2 at index 5, add 2, and mark indices 4, 5. Finally choose 4 at index 3, add 4. The total score is 1 + 2 + 4 = 7.
Example 2
Input
nums = [10, 1, 1, 10]
Output
11
Explanation
Choose the first 1 (smallest index among ties), add 1, and mark its neighbors. The remaining unmarked 10 at the other end can still be chosen, so the score becomes 1 + 10 = 11.
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.