Ashish's Dilemma

0

0 votes
Very-Easy
Problem

Ashish is provided with n numbers in an array A[n]. He wants to rearrange the numbers in the array so as to convert the provided array into a sorted array. The sorted array should have the numbers in ascending order. However, he's only allowed to rearrange numbers by swapping two numbers of the array (any two, the numbers need not be consecutive) at a time. The time taken to swap any two numbers located at index i and j is (i-j)^2 minutes. The indexing of the array starts from 1.

For example, if the initial array is {6, 4, 1, 20, 32} would be converted to {1, 4, 6, 20, 32} by swapping numbers at index 1 and 3 and the time required to doo so would be (1-3)^2 = 4 minutes.

Due to the upcoming compre exams, he wants to accomplish the task as soon as possible.

Given an array, can you help him determine the minimum time (in minutes) it will take to convert the array to a sorted one?

Input Format:

The first line is an integer n, denoting the number of elements in the array

The second line contains n space-separated integers.

Constraints:

1<=n<=10^5 0<=A[i]<=10^9

Output Format:

Print an integer denoting the minimum time taken (in minutes) to sort the array.

Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

In this case, the minimum time is taken if we swap A_3 with A_4, A_4 with A_5 and then again A_3 with A_4 (as opposed to directly swapping A_3 with A_5 which would take 4 minutes).

Editor Image

?