QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100

#23360. Distant Cards

Statistics

There are $2N$ cards bearing positive integers. They are arranged in a row from left to right.

Each card bears exactly one positive integer between $1$ and $N$, inclusive. Let $X_i$ be the number on the $i$-th card from the left ($1\le i\le2N$).

For each $k$ with $1\le k\le N$, exactly two cards bear $k$. In other words, each positive integer from $1$ to $N$ appears on exactly two cards.

정올이 calls the number of cards lying between the two cards bearing the positive integer $k$ the "number of cards between $k$".

For example, consider the arrangement in the figure below. Here $N=4$ and $X_1=1,X_2=2,X_3=2,X_4=4,X_5=3,X_6=1,X_7=3,X_8=4$.

problem_23360_66589eed6795aaf5a2e5f52e4af46ceb.png
  • Between the two cards bearing $1$ are cards bearing $2,2,4,3$ in order, so the number of cards between $1$ is $4$.
  • There are no cards between the two cards bearing $2$, so the number of cards between $2$ is $0$.
  • Only the card bearing $1$ lies between the two cards bearing $3$, so the number of cards between $3$ is $1$.
  • Between the two cards bearing $4$ are cards bearing $3,1,3$ in order, so the number of cards between $4$ is $3$.

In this example, the greatest number of cards between any $k$ is the number of cards between $1$, which is $4$.

정올이 wants to find the greatest number of cards between $k$ over all positive integers $k$ from $1$ to $N$.

Given the numbers on the cards in their arranged order, write a program that finds the greatest number of cards between any $k$.

Constraints

  • All given numbers are integers.
  • $1\le N\le2\,000$
  • For each $i$ ($1\le i\le2N$): $1\le X_i\le N$
  • For each $k$ ($1\le k\le N$), the number appears exactly twice among $X_1,X_2,\ldots,X_{2N}$.

Subtasks

  1. (10 points) $N\le2$
  2. (15 points) The answer is $0$ or $1$.
  3. (15 points) The answer is $2N-3$ or $2N-2$.
  4. (20 points) $N\le500$
  5. (40 points) No additional constraints.

Input

The first line contains the integer $N$.

The second line contains $2N$ integers $X_1,X_2,\ldots,X_{2N}$, separated by spaces.

Output

Print the answer on the first line.

Examples

Input 1

4
1 2 2 4 3 1 3 4

Output 1

4

Input 2

4
1 2 3 4 4 3 2 1

Output 2

6

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.