#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl "\n"
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll n;
    cin >> n;
    ll arr[n];
    ll sum=0;
    for (ll i=0;i<=n-1;i++)
    {
        cin >> arr[i];
        sum+=arr[i];
    }

    sort(arr,arr+n);
    ll ans=0;
    for (ll i=0;i<=n-1;i++)
    {
        sum-=arr[i];
        ans+=sum-((n-i-1)*arr[i]);
    }


    cout << ans << endl;
    return 0;
}
#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
using namespace std;
using ll = long long;

int main(){
  int N;
  cin >> N;
  vector<ll> A(N);
  for(int i=0; i<N; ++i){
    cin >> A[i];
  }
  
  ll ans = -1;
  for(int i=0; i<N; ++i){
    ll max_x = A[i];
    for(int j=i; j<N; ++j){
      max_x = min(max_x, A[j]);
      ans = max(ans, max_x*(j-i+1));
    }
  }

  cout << ans << endl;

  return 0;
}
