import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for (int i=0; i<n; i++){
            arr[i]=in.nextInt();
 //           System.out.println(arr[i]);
        }
        int cost = 0;
        int num = 0;
        int anum=0;
        int max = arr[n-1];
        int t=0;
//3 4 4 5 5 9 11 11 13 15 15 15 16
 // 0 1 2 3 4 5 6  7  8  9  10 11 12 13
  //1211109 8 7 6  5  4  3  2  1  0
        while (max>0){
        if(anum<n){
        int imax=n-1-anum;
        max = arr[imax];
        num = 0;
        for (int i=imax; i>-1; i--){
            if(arr[i]==max){
                num++;
                arr[i]=0;
            }else{
                break;
            }
//            System.out.println(arr[n-1-i]);
        }
 //      System.out.println("n"+num);
        for(int i =0; i<n; i++){
            if(arr[i]>0){
                arr[i]-=num;
                if(arr[i]<0){
                    arr[i]=0;
                }
 }
        
        }
        t=num*max;
//      System.out.println(t);
        cost+=t;
        anum+=num;}
        else{
            break;
        }
        
        }
        if(t<0){
            cost-=t;
        }
        System.out.println(cost);
    }
}
