Problem:
Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85

Solution in Fortran (GNU Fortran 9.2.1) (judge verdict: Wrong Answer)

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