#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mod 1000000007
#define inf 1000000000000000000
#define bpc(x) __builtin_popcountll(x)
#define autoit(x,it) for(auto it = x.begin(); it != x.end(); it++)
#define rep(n) for(ll i = 0; i < n; i++)
#define repi(i,n) for(ll i = 0; i < n; i++)

#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
using namespace __gnu_pbds; 

#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> 

using namespace std;

int main()
{
   FAST/**/
   
   ll n;
   cin>>n;
   
   ll arr1[n];
   rep(n)
        cin>>arr1[i];
           
   pair<ll,ll> arr[n];
   rep(n)
        arr[i] = mp(arr1[i],i);
   sort(arr,arr+n);
   ll ans = 0;
   while(1)
   {
       bool done = 0;
       rep(n-1)
       {
           if(arr[i].ss<=i && arr[i+1].ss<=i)
           {
               if(arr[i].ff>arr[i+1].ff)
                    swap(arr[i],arr[i+1]), ans+=arr[i].ff-arr[i+1].ff, done = 1;        
           }
           
           else if(arr[i].ss>=i && arr[i+1].ss>=i)
           {
               if(arr[i].ff<arr[i+1].ff)
                    swap(arr[i],arr[i+1]), ans+=arr[i].ff-arr[i+1].ff, done = 1;        
           }
           
           else if(arr[i].ss<=i && arr[i].ss>=i)
           {
               swap(arr[i],arr[i+1]), ans+=arr[i].ff+arr[i+1].ff, done = 1;        
           }
       }
       if(!done)
            break;
   }
   ans = 0;
   rep(n)
        ans+=arr[i].ff*abs(i-arr[i].ss);
   cout<<ans;        
   
   return 0;        
}  
  
   