# LC 1695. Maximum Erasure Value
### [Problem link](https://leetcode.com/problems/maximum-erasure-value/)
###### tags: `leedcode` `medium` `c++` `Sliding Window`
You are given an array of positive integers <code>nums</code> and want to erase a subarray containing **unique elements** . The **score** you get by erasing the subarray is equal to the **sum** of its elements.
Return the **maximum score** you can get by erasing **exactly one** subarray.
An array <code>b</code> is called to be a <span class="tex-font-style-it">subarray of <code>a</code> if it forms a contiguous subsequence of <code>a</code>, that is, if it is equal to <code>a[l],a[l+1],...,a[r]</code> for some <code>(l,r)</code>.
**Example 1:**
```
Input: nums = [4,2,4,5,6]
Output: 17
Explanation: The optimal subarray here is [2,4,5,6].
```
**Example 2:**
```
Input: nums = [5,2,1,2,5,2,1,2,5]
Output: 8
Explanation: The optimal subarray here is [5,2,1] or [1,2,5].
```
**Constraints:**
- <code>1 <= nums.length <= 10<sup>5</sup></code>
- <code>1 <= nums[i] <= 10<sup>4</sup></code>
## Solution 1 - Sliding Window
#### C++
```cpp=
class Solution {
public:
int maximumUniqueSubarray(vector<int>& nums) {
unordered_map<int, int> cnt;
int windowSum = 0;
int ans = 0;
int l = 0;
for (int r = 0; r < nums.size(); r++) {
cnt[nums[r]]++;
windowSum += nums[r];
while (cnt[nums[r]] > 1) {
cnt[nums[l]]--;
windowSum -= nums[l];
l++;
}
ans = max(ans, windowSum);
}
return ans;
}
};
```
>### Complexity
>| | Time Complexity | Space Complexity |
>| ----------- | --------------- | ---------------- |
>| Solution 1 | O(n) | O(n) |
## Note
x