#P5971. [CTSC2009] 移盘子

[CTSC2009] 移盘子

Description

There are three pegs, named AA, BB, and CC. Initially, AA has NN plates on it, while BB and CC are empty. Each move consists of taking the top plate from one peg and placing it onto another peg. Plates are of three types, denoted by 11, 22, and 33. Your goal is to have all type 11 plates on AA, all type 22 plates on BB, and all type 33 plates on CC in the end. Find the minimum number of moves needed to achieve this goal.

Input Format

The input file trique.in contains:

  • The first line: an integer NN, the total number of plates.
  • The second line: NN numbers, each being 11, 22, or 33. These NN numbers describe the types of all plates initially on peg AA, listed from top to bottom.

Output Format

The output file trique.out contains a single number: the minimum number of moves.

5
1 2 1 3 3
8

Hint

Sample explanation: Initial state as shown below:

Constraints:

  • For 2020% of the testdata, the number of plate types does not exceed 22.
  • For 4040% of the testdata, N300N \leq 300.
  • For 100100% of the testdata, N1000N \leq 1000.

Translated by ChatGPT 5