Under construction

Still being built. Things may change

Dimensional Gate

Time limit1sMemory limit1024 MB

Summary
Given a permutation, find the cheapest swaps (cost is the squared gap between swapped gate numbers) that merge all cycles into one.
Level

Medium7 of 10

Topics
Union-find, Greedy, Sorting, Array
Solved
No attempts yet

Problem

A dimensional gate is a very convenient means of transport. It looks like an ordinary door, but once you open it and walk through, you arrive in a different space. However, the gates cannot be used in the reverse direction.

ANA country has NN cities, numbered 1,2,⋯ ,N1, 2, \cdots, N. Insu, a dimensional magician who handles these gates, made one gate leading to each of cities 1,2,⋯ ,N1, 2, \cdots, N, numbered each gate 1,2,⋯ ,N1, 2, \cdots, N, and installed them one per city at random. The gate installed in city ii has number aia_i, and it leads to city aia_i.

Insu found that it may be impossible to visit every other city from a given city using only gates. So he plans to cast a spell several times. Each spell swaps the gates of two different cities he chooses. The spell costs mana equal to the square of the difference between the two gate numbers. For example, if city 1 has gate 4 (leading to city 4) and city 4 has gate 6 (leading to city 6), swapping them costs (4−6)2=4(4-6)^2=4 mana.

Find the minimum mana needed to make it possible to visit all other cities from any single city using only gates. Also find how the spells should be cast. The number of spells does not need to be minimal.

Input

The first line contains an integer NN (2≤N≤200 0002 \le N \le 200\,000).

The second line contains integers a1,a2,⋯ ,aNa_1, a_2, \cdots, a_N (1≤ai≤N1 \le a_i \le N), separated by spaces.

Output

Print the minimum mana CC and the number of spells MM, separated by a space. The number of spells does not need to be minimal.

From the second line, print MM lines in order. Each line contains the two city numbers whose gates are swapped, separated by a space.

Examples2

  1. Example 1

    Input
    3
    1 3 2
    
    Expected output
    1 1
    1 3
    
  2. Example 2

    Input
    3
    3 1 2
    
    Expected output
    0 0