#include <iostream>
void reduction(int* a, int n){
    int t = a[n+1];
    for(int i = 0; i<=n && a[i]<t;++i){
        --a[i];
    }
}
int main(){
   int n;
   std::cin>>n;
   int* a = new int[n];
   for(int i = 0;i<n;++i){
       std::cin>>a[i];
   }
   int j = n-1;
   int s = 0;
   while(a[j]>0){
       //std::cout<<s<<" "<<a[j]<<std::endl;
       s+=a[j];
       //a[j] = 0;
       --j;
       reduction(a,j);
   } 

   std::cout<<s; 
}