Under construction

Still being built. Things may change

Flag Rotation

Interview

Time limit2sMemory limit1024 MB

Summary
Given the stripe colours of a square flag, count the cells to repaint so the row stripes match the columns of a counter-clockwise rotation.
Level

Easy3 of 10

Topics
Hash map, Array
Solved
No attempts yet

Problem

A common pattern for country flags is three stripes. Some flags are really similar, and have exactly the same stripes in the same order, but the flag is rotated by 9090 degrees. Using a sequence of colours, we can make a flag on a square grid by covering each row with the colours in the sequence. Another way to make a similar flag (rotated by 9090 degrees) is to cover each column with the colours in the sequence.

In preparation for the Flags You Paint for Countries meeting, you notice a mistake for one of the flags: it should have been rotated counter-clockwise by 9090 degrees! The square canvas is already stuck to a wall using super glue, so you cannot take it off and rotate it. You want to know how much work it would take to repaint one flag into the other. In one second, you can repaint one 1×11 \times 1 square to any colour. What is the minimum number of seconds needed to repaint the flag? For the flag in the first sample input (Figure F.1), 3434 squares need to be repainted.

Figure F.1: Two flags corresponding to the first sample input. Colour 11 is yellow, 22 is red, 33 is white, and 10001000 is blue. When the first flag should be rotated counter-clockwise by 9090 degrees without actually rotating the canvas, 3434 squares need to be repainted.

Input

The input consists of:

  • One line with an integer nn (1≤n≤2⋅1051\leq n\leq 2 \cdot 10^5), the number of stripes in the flag.
  • One line with nn integers aa (1≤a≤1091\leq a\leq 10^9), the colours of the stripes.

Output

Output the number of cells that need to be repainted when the flag should be rotated counter-clockwise by 9090 degrees.

Examples3

  1. Example 1

    Input
    7
    1 2 2 3 1000 2 1
    
    Expected output
    34
    
  2. Example 2

    Input
    4
    10 12 12 10
    
    Expected output
    8
    
  3. Example 3

    Input
    5
    6 5 5 6 5
    
    Expected output
    12