program main
    implicit none
    integer n
    integer, allocatable, dimension(:) :: a, s
    integer i,j,sum, max, r_i, flag 
    real(8) r
    read(*,*) n
    allocate(a(n))
    allocate(s(n))
    read(*,*) a


    max = 0
    
    do j = 1,10000

    sum = 0

    do i = 1,n
        s(i) = 0
    end do

    do i = 1,n
10      continue
        r = rand()
        r_i = int(r*dble(n)) + 1
        if(s(r_i) == 0) then
            s(r_i) = 1
            sum = sum + a(i)*abs(i-r_i)
        else
            go to 10
        end if
    end do

    if (sum > max) then
        max = sum
    end if

    end do

    write(*,*) max


end program