#C13366. Second Tallest Tree

    ID: 42896 Type: Default 1000ms 256MiB

Second Tallest Tree

Second Tallest Tree

You are given the heights of trees in a forest. Your task is to determine the height of the second tallest distinct tree. In other words, if you have a list of height values, remove the duplicates and then output the second largest value. If there is no such tree (i.e. when there are less than two distinct heights), output -1.

The problem can be summarized with the following idea: Given a list of integers \(h_1, h_2, \ldots, h_n\), let \(S\) denote the set of distinct heights. If \(|S| \geq 2\), let \(s_1\) be the largest element and \(s_2\) be the second largest element in \(S\). Otherwise, output \(-1\).

Note: The input is provided via standard input (stdin) and the output should be printed to standard output (stdout).

inputFormat

The first line contains a single integer \(n\) which represents the number of trees. The second line contains \(n\) space-separated integers, each denoting the height of a tree.

Example:

5
3 3 4 5 5

outputFormat

Output a single integer: the height of the second tallest tree, or -1 if there is no such tree.

Example:

4
## sample
5
3 3 4 5 5
4