#K85162. Maximum Unique Subarray Sum
Maximum Unique Subarray Sum
Maximum Unique Subarray Sum
Given an array of integers, your task is to find the maximum sum of any contiguous subarray that contains only unique elements. In other words, if you choose a subarray (a contiguous portion of the array), all elements in that subarray must be distinct.
The solution requires using an efficient sliding window technique. Mathematically, if we denote the subarray from index ( l ) to ( r ) as ( [a_l, a_{l+1}, \dots, a_r] ), then you need to find: [ \max_{l \leq r} \left{ \sum_{i=l}^{r} a_i ;\text{ subject to all } a_i \text{ being unique}\right} ]
It is possible that the array is empty, in which case the answer is 0.
Note: The input is given via standard input (stdin) and the output must be printed to standard output (stdout).
inputFormat
The input is provided via standard input (stdin) in the following format:
- The first line contains a single integer ( n ) representing the number of elements in the array.
- If ( n > 0 ), the second line contains ( n ) space-separated integers representing the array elements. If ( n = 0 ), no further input is provided.
outputFormat
Print a single integer to standard output (stdout) representing the maximum sum of any contiguous subarray that has all unique elements.## sample
5
4 2 4 5 6
17