Let's swap

3.4

13 votes
Algorithms, Greedy Algorithms, Merge Sort, Sorting
Problem

Y is alone too and has a permutation p1,p2,...,pn of numbers from 1 to n.

Y thinks that a permutation p1,p2,...,pn  beautifulness is defined as value of |pii|,1in.

Y can swap two elements of the permutation at most once, what is the maximum beautifulness that Y can get?

Input

First line contains only n.

Second line contains the permutation p1,p2,...,pn separated by space.

1n105

1pin, all pi are distinct.

Output

The only line of output contains an integer, maximum beautifulness that Y can get.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

Y can swap 1th and 5th element and the permutation becomes 5,4,2,3,1 with beautifulness |51|+|42|+|23|+|34|+|51|=12.

Editor Image

?