Search for the maximum sum of a subarray with unique elements

JavaScript Hard arrays

Task «Search for the maximum sum of a subarray with unique elements»

Given an array of integers, find the maximum sum of a contiguous subarray in which all elements are unique (no repetitions). If no such subarray exists, return 0. Constraints: array length up to 10^5, element values up to 10^9 in absolute value. Implement an algorithm with O(n) time complexity.

Input Format

First line: an integer n (1 ≤ n ≤ 10^5) — the length of the array. Second line: n integers separated by spaces.

Output Format

One integer — the maximum sum of a subarray with unique elements.

Examples

Example 1

INPUT
4 5 5 5 5
OUTPUT
5

Example 2

INPUT
4 -1 -2 -3 -4
OUTPUT
0

Example 3

INPUT
6 5 -3 5 -3 5 -3
OUTPUT
5
main.js