#abc381f. F - 1122 Subsequence

F - 1122 Subsequence

当前没有测试数据。

Score : 525525 points

问题陈述

一个正整数序列 X=(X1,X2,)X = (X_1, X_2, \ldots)(可能为空)被称为1122序列,当且仅当它满足以下三个条件:(1122序列的定义与问题D中相同。)

  • X\lvert X \rvert 是偶数。这里,X\lvert X \rvert 表示 XX 的长度。
  • 对于每个满足 1iX21\leq i\leq \frac{|X|}{2} 的整数 iiX2i1X_{2i-1}X2iX_{2i} 相等。
  • 每个正整数在 XX 中要么不出现,要么恰好出现两次。也就是说,XX 中包含的每个正整数在 XX 中恰好出现两次。

给定一个由正整数组成的长度为 NN 的序列 A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N),请打印出 AA 的一个**子序列(不一定是连续的)**的最大长度,该子序列是一个1122序列。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

A sequence X=(X1,X2,)X = (X_1, X_2, \ldots) of positive integers (possibly empty) is called a 1122 sequence if and only if it satisfies all of the following three conditions: (The definition of a 1122 sequence is the same as in Problem D.)

  • X\lvert X \rvert is even. Here, X\lvert X \rvert denotes the length of XX.
  • For each integer ii satisfying 1iX21\leq i\leq \frac{|X|}{2}, X2i1X_{2i-1} and X2iX_{2i} are equal.
  • Each positive integer appears in XX either not at all or exactly twice. That is, every positive integer contained in XX appears exactly twice in XX.

Given a sequence A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) of length NN consisting of positive integers, print the maximum length of a subsequence (not necessarily contiguous) of AA that is a 1122 sequence.

Constraints

  • 1N2×1051\leq N \leq 2 \times 10^5
  • 1Ai201\leq A_i \leq 20
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

Output

Print the maximum length of a (not necessarily contiguous) subsequence of AA that is a 1122 sequence.

Sample Input 1

7
1 3 3 1 2 2 1

Sample Output 1

4

For example, choosing the 11-st, 44-th, 55-th, and 66-th elements of AA, we get (1,1,2,2)(1, 1, 2, 2), which is a 1122 sequence of length 44.
There is no longer subsequence that satisfies the conditions for a 1122 sequence, so the answer is 44.

Sample Input 2

1
20

Sample Output 2

0