#K48647. Longest Balanced Subsequence
Longest Balanced Subsequence
Longest Balanced Subsequence
Given a sequence of integers containing only the values 0, 1, and 2, your task is to compute the length of the longest balanced subsequence. A subsequence is considered balanced if it contains an equal number of 0s, 1s, and 2s. The longest balanced subsequence's length will be equal to 3 times the minimum count among 0s, 1s, and 2s in the sequence.
Note: A subsequence does not need to be contiguous. However, in this problem you are asked to determine the maximum possible length of such a balanced subsequence that can be formed from the given sequence.
For example, if the input sequence is [0, 1, 2, 0, 1, 2, 0], the counts are: 0 appears 3 times, 1 appears 2 times, and 2 appears 2 times. The longest balanced subsequence can only use 2 copies of each integer, resulting in a length of 6.
inputFormat
The first line of the input contains a single integer n — the length of the sequence.
The second line contains n space-separated integers, each being either 0, 1, or 2, representing the sequence.
outputFormat
Output a single integer — the length of the longest balanced subsequence.
## sample7
0 1 2 0 1 2 0
6