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



