Description

Given an array of positive integers nums, return the maximum sum of a subarray with unique elements (no duplicate values).

Examples

Input:nums = [4,2,4,5,6]
Output:17
Explanation:

[2,4,5,6] = 17.

Input:nums = [1,2,1,3,4,3,2,5]
Output:10
Explanation:

The maximum sum subarray with all unique elements is [1,3,4,3,2,5] — removing duplicates, the best contiguous unique subarray sums to 10.

Input:nums = [1,1,1,1,1]
Output:1
Explanation:

Since all elements are the same, any subarray with unique elements can contain at most one element. The maximum sum is 1.

Constraints

  • 1 ≤ nums.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!