<p>You are given an array of positive integers <code>nums</code> and want to erase a subarray containing&nbsp;<strong>unique elements</strong>. The <strong>score</strong> you get by erasing the subarray is equal to the <strong>sum</strong> of its elements.</p>

<p>Return <em>the <strong>maximum score</strong> you can get by erasing <strong>exactly one</strong> subarray.</em></p>

<p>An array <code>b</code> is called to be a <span class="tex-font-style-it">subarray</span> 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>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,2,4,5,6]
<strong>Output:</strong> 17
<strong>Explanation:</strong> The optimal subarray here is [2,4,5,6].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,2,1,2,5,2,1,2,5]
<strong>Output:</strong> 8
<strong>Explanation:</strong> The optimal subarray here is [5,2,1] or [1,2,5].
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
  int maximumUniqueSubarray(vector<int>& nums) {
    std::unordered_set<int> s;
    int n = nums.size(), ans = 0, tot = 0, prev = -1, x, y;
    for (int i = 0; i < n; ++i) {
      x = nums[i];
      if (s.count(x)) {

	/**
	std::vector<int> v(s.begin(), s.end());
	std::sort(v.begin(), v.end());
	
	std::cout << prev+1 << "\t" << (i-1) << "\t" << tot << "\t" << x << std::endl;
	for (int j = prev+1; j < i; ++j) {
	  std::cout << nums[j] << ", ";
	}
	std::cout << std::endl << std::endl;
	for (int t : v) {
	  std::cout << t << ", ";
	}
	std::cout << std::endl << std::endl;
	/**/
	
	if (ans < tot) {
	  ans = tot;
	}	
	for (++prev; ; ++prev) {
	  y = nums[prev];
	  if (y == x) {
	    break;
	  }
	  s.erase(y);
	  tot -= y;
	}
      }
      else {
	s.insert(x);
	tot += x;
      }
    }

    /**
    std::vector<int> v(s.begin(), s.end());
    std::sort(v.begin(), v.end());
    
    std::cout << prev+1 << "\t" << (n-1) << "\t" << tot << std::endl;
    for (int j = prev+1; j < n; ++j) {
      std::cout << nums[j] << ", ";
    }
    std::cout << std::endl << std::endl;
    for (int t : v) {
      std::cout << t << ", ";
    }
    std::cout << std::endl << std::endl;
    /**/
    
    return (ans > tot) ? ans : tot;
  }

  Solution() {
    // *** need this constructor to boost the speed ***  
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
  }
};

118317