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 Ruby (2.7.1) (judge verdict: Wrong Answer)

n=gets.to_i
array=gets.split.map(&:to_i)
ans=0
ansArray=Array.new(n,0)
leftsumArray=Array.new(n,0)
rightsumArray=Array.new(n,0)
arrayreverse=array.reverse
hash={}

array.map.with_index{|value,index|
    if hash[value]
        hash[value] = hash[value].push(index)
    else
        hash[value] = [index]
    end
}

leftsum=0
array.map.with_index{|value,index|
    leftsum+=value
    leftsumArray[index]=leftsum
}

rightsum=0
arrayreverse.map.with_index{|value,index|
    rightsum+=value
    rightsumArray[index]=rightsum
}

rightsumArray=rightsumArray.reverse
hash=hash.sort.reverse

leftnow=0
rightnow=n-1

hash.map{|value|
    uresisa=value[0]
    array2=value[1]
    
    array2.map{|place|
      leftdis=(place-leftnow).abs
      rightdis=(place-rightnow).abs
        
    #   leftsum=leftsumArray[place-1] ? leftsumArray[place-1] : 0
    #   rightsum=rightsumArray[place+1] ? rightsumArray[place+1] : 0
      
      if place==0
          leftsum=0
      else
          leftsum=array[0...place].select{|n| n<place}.inject(&:+)
      end
      
      if place==n-1
            rightsum=0
      else
            rightsum=array[place+1..n-1].select{|n| n<=place}.inject(&:+)
       end
      
      if leftsum==nil
          leftsum=0
      end
      
      if rightsum==nil
        rightsum=0
        end
      
     
      if leftdis*uresisa + rightsum > leftsum + rightdis*uresisa
          ansArray[leftnow]=uresisa
          ans+=leftdis*uresisa
          leftnow+=1
      else
          ansArray[rightnow]=uresisa
          ans+=rightdis*uresisa
          rightnow-=1
     end

    }
    
}

puts ans