program main
  implicit none
  type pairtype
    integer :: v, k
  end type pairtype
  integer :: n, i, p1, p2
  integer, allocatable :: a(:)
  type(pairtype), allocatable :: a1(:), a2(:)
  read *, n
  allocate (a(n), a1(n), a2(n))
  read *, a
  do i = 1, n
    a1(i) = pairtype(a(i), i)
  end do
  do i = 1, n
    a2(i) = a1(n - i + 1)
  end do
  call mergesort(a2, n)
  do i = 1, n
    a1(i) = a2(n - i + 1)
  end do

  ! print *, a1

  p1 = 1
  p2 = n
  do i = 1, n
    a2 = ins(a1, p1, p2, n)
    if (score(a2, n) > score(a1, n)) then
      a1 = a2
      p2 = p2 - 1
    else
      p1 = p1 + 1
    end if
  end do

  print "(i0)", score(a1, n)

  contains

  subroutine merge_arrays(a, na, b, nb, c, nc)
    ! Merge arrays a and b to c
    ! A better implementation is at ~/repos/fortran/Brainerd/sort2_13/main.f90
    implicit none
    integer, intent(in) :: na, nb, nc
    type(pairtype), intent(in) :: a(na), b(nb)
    type(pairtype), intent(inout) :: c(nc)
    integer :: i, j, k

    if (na + nb /= nc) then
      print *, "subroutine merge_arrays size error"
      stop 161
    end if

    i = 1; j = 1; k = 1
    do while(i <= na .and. j <= nb)
      if (a(i) % v <= b(j) % v) then
        c(k) = a(i)
        i = i + 1
      else
        c(k) = b(j)
        j = j + 1
      endif
      k = k + 1
    enddo
    do while (i <= na)  ! b seems to need similar process
      c(k) = a(i)
      i = i + 1
      k = k + 1
    enddo
  end subroutine merge_arrays

  recursive subroutine mergesort(a, n)
    ! Sort an array a(n)
    implicit none
    integer, intent(in) :: n
    type(pairtype), intent(inout) :: a(n)
    integer :: na
    type(pairtype) :: v, t((n + 1) / 2)

    if (n < 2) then
      return
    else if (n == 2) then
      if (a(1) % v > a(2)% v ) then
        v = a(1)
        a(1) = a(2)
        a(2) = v
      endif
      return
    endif

    na = (n + 1) / 2
    call mergesort(a, na)
    call mergesort(a(na + 1), n - na)

    if (a(na)  % v> a(na + 1) % v) then
      t(1:na) = a(1:na)
      call merge_arrays(t, na, a(na + 1:n), n - na, a, n)
    endif
  end subroutine mergesort

  function ins(a, p1, p2, n) result(a2)
    ! p1にあったものをp2に入れる
    integer, intent(in) :: p1, p2, n
    type(pairtype), intent(in) :: a(n)
    type(pairtype) :: a2(n)
    integer :: p2c
    p2c = p2
    do while (p2c < n)
      if (a(p2c + 1) % v == a(p1) % v) then
        p2c = p2c + 1
      else
        exit
      end if
    end do
    a2(1:p1 - 1) = a(1:p1 - 1)
    a2(p1:p2c - 1) = a(p1 + 1:p2c)
    a2(p2c) = a(p1)
    a2(p2c + 1:n) = a(p2c + 1:n)
  end function ins

  integer(8) function score(a, n)
    integer, intent(in) :: n
    type(pairtype), intent(in) :: a(n)
    integer :: i
    score = 0
    do i = 1, n
      score = score + abs(a(i) % k - i) * a(i) % v
    end do
  end function score

  recursive subroutine quicksort(a)
    type(pairtype), intent(inout) :: a(:)
    type(pairtype) :: pivot, tmp
    integer :: i, j, k, n
    real(4) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot % v <= a(i) % v)
        i = i + 1
      end do
      do while (.not. a(j) % v <= pivot % v)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main
