#include<iostream>
#include<vector>
using namespace std;
#define int long long
const int N = 1e5 + 10;
int a[N];
vector<pair<int,int> > ve;
signed main()
{
int n;
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i];
ans = 0;
ans += a[n] * n;
for(int i = n - 1; i >= 1; i --)
{
if(a[i] > a[n])
{
ans += a[n] * n;
ve.push_back({i,a[i] - a[n]});
}
if(a[i] <= a[n])
{
ans += a[i] * n;
}
}
vector<int>dle;
while(ve.size())
{
int now = ve[0].first;
int c = ve[0].second;
ans += c * now;
ve.erase(ve.begin());
for(int i = 0; i < ve.size(); i ++)
{
if(ve[i].second > c)
{
ans += c * now;
ve[i].second = ve[i].second - c;
}
else
{
ans += ve[i].second * now;
dle.push_back(i);
}
}
int t = 0;
for(int i = 0; i < dle.size(); i ++)
{
ve.erase(ve.begin() + dle[i] - t);
t += 1;
}
dle.clear();
}
cout << ans;
return 0;
}