#include <bits/stdc++.h>
using namespace std;
int sloution(int st, int en)
{	
  int min=a[st];
  int minpos=st;
  for(int i=st;i<=en;i++){
    if(min>a[i]){
      min=a[i];
      minpos=i;
    }
  }
  int offset=(minpos-st)>(en-minpos)?(en-minpos+1):(minps-st+1);
  int res=min*(1+offset)*offset+(en-st-2*minpos);
  return res+sloution(st,minpos-1)+sloution(minpos+1,en)
}

int main(){
  int N;
  cin>>N;
  int a[N];
  for(int i:N){
    cin>>a[i];
  }
return sloution(0, N-1);

}