#K8396. Sum of Two Largest Unique Elements

    ID: 36313 Type: Default 1000ms 256MiB

Sum of Two Largest Unique Elements

Sum of Two Largest Unique Elements

Given an array of integers, compute the sum of the two largest unique elements in the array. If the array contains fewer than two unique elements, output the largest unique element. If the array is empty, output 0.

Formally, let \(U = \{a_i \mid a_i \text{ appears in the array}\}\) be the set of unique elements sorted in descending order as \(U_1, U_2, \dots\). Then,

\[ \text{result} = \begin{cases} U_1 + U_2, & \text{if } |U| \geq 2, \\ U_1, & \text{if } |U| = 1, \\ 0, & \text{if } |U| = 0. \end{cases} \]

Examples:

  • For the input array [3, 2, 3, 4, 8, 8], the answer is 12.
  • For the input array [1, 1, 1, 1, 1], the answer is 1.

inputFormat

The input is read from standard input (stdin) and consists of two parts:

  • The first line contains an integer \(n\) representing the number of elements in the array.
  • The second line contains \(n\) space-separated integers. (If \(n = 0\), the second line may be omitted.)

outputFormat

Output a single integer to standard output (stdout), which is the sum of the two largest unique elements as specified in the problem statement.

## sample
6
3 2 3 4 8 8
12