#P9310. Luna's Couples Date

    ID: 22465 Type: Default 1000ms 256MiB

Luna's Couples Date

Luna's Couples Date

Luna came up with an unusual idea. She arranged 2n friends in a line and assigned each a number in the range \(1\sim n\), with each number appearing exactly twice. Each pair of friends having the same number is considered a couple.

To send every couple on a date, the couple must stand adjacent in the line (i.e. with no one between them). Luna can perform two kinds of operations:

  • Swap Operation: Swap any two adjacent persons. (Costs 1 operation per swap.)
  • Date Operation: If two adjacent persons form a couple (i.e. they have the same number), Luna can send them on a date. They then leave the line, and the rest of the people shift forward to close the gap. (This removal also costs 1 operation.)

Operations can be performed in any order. For example, Luna can perform several swaps, then a date operation, and then swap again.

Your task is to compute the minimum number of operations (swaps and date removals) needed to send every couple on a date.

Note: The answer is the sum of all swap operations plus the number of date operations (one per couple removal).

inputFormat

The first line contains an integer n representing the number of couples.

The second line contains 2n integers. Each integer is between 1 and n and every number appears exactly twice.

outputFormat

Output a single integer: the minimum number of operations needed so that all couples have gone on a date.

sample

2
1 2 1 2
3