#C8211. Longest Beautiful Subsequence

    ID: 52169 Type: Default 1000ms 256MiB

Longest Beautiful Subsequence

Longest Beautiful Subsequence

You are given an integer n and an array of n integers. A subsequence is called beautiful if it contains at least one element that occurs more than once. In other words, if there exists an element in the subsequence with a frequency of at least 2, then the subsequence is considered beautiful.

Your task is to determine the length of the longest beautiful subsequence. Note that if the given array contains any element that appears at least twice, then the whole array is a beautiful subsequence. Otherwise, if all elements are distinct, no beautiful subsequence exists and the answer is 0.

Mathematical formulation:

Let \(A = [a_1, a_2, \dots, a_n]\) be the array. Define the frequency function \(f(x)\) which counts the number of times x appears in \(A\). The array is beautiful if \(\exists~x \text{ such that } f(x) \ge 2\). If it is beautiful, the answer is \(n\); otherwise, the answer is 0.

inputFormat

The input is read from stdin and has the following format:

The first line contains a single integer n representing the size of the array.
The second line contains n space-separated integers representing the elements of the array.

outputFormat

Output a single integer to stdout representing the length of the longest beautiful subsequence. If no beautiful subsequence exists (i.e. if all elements are distinct), output 0.

## sample
7
3 1 4 1 5 9 2
7