using System;
using System.Reflection;
using System.Linq.Expressions;
using System.Linq;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder {
    public class Program {
        static int[] getKeys(char delimiter) {
            var Keys = Console.ReadLine().Split(' ');
            int[] Ns = new int[Keys.Length];
            for (int i = 0; i < Keys.Length; i++) {
                Ns[i] = int.Parse(Keys[i]);
            }
            return Ns;
        }
        static void Show<T>(T[,] map) {
            int H = map.GetLength(0);
            int W = map.GetLength(1);
            Console.WriteLine("-------------------------");
            for (int i = 0; i < H; i++) {
                for (int j = 0; j < W; j++) {
                    Console.Write(map[i, j] + " ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("-------------------------");
        }
        static void Show<T>(T[,] map, string delimiter) {
            int H = map.GetLength(0);
            int W = map.GetLength(1);
            Console.WriteLine("-------------------------");
            for (int i = 0; i < H; i++) {
                for (int j = 0; j < W; j++) {
                    Console.Write(map[i, j] + delimiter);
                }
                Console.WriteLine();
            }
            Console.WriteLine("-------------------------");
        }
        static void Show<T>(IEnumerable<T> ts) {
            foreach (var item in ts) {
                Console.Write(item + " ");
            }
            Console.WriteLine();
        }
        static void Show<T>(IEnumerable<T> ts, string delimiter) {
            foreach (var item in ts) {
                Console.Write(item + delimiter);
            }
            Console.WriteLine();
        }
        static void Main() {
            int N = int.Parse(Console.ReadLine());
            var Hs = getKeys(' ');
            int Answer = 0;
            if (Hs.Max() == 0) {
                Console.WriteLine(0);
                return;
            }
            int Min = Hs.Where(x => x > 0).Min();
            while (true) {
                Answer += getCost(Hs, Min);
//                Console.WriteLine(Min+","+ Answer);
                for (int i = 0; i < N; i++) {
                    Hs[i] = Math.Max(Hs[i] - Min, 0);
                }
                if (Hs.All(x => x == 0)) {
                    break;
                } else {
                    Min = Hs.Where(x => x > 0).Min();
                }
            }
            Console.WriteLine(Answer);
        }
        static int CountMountain(int[] Hs) {
            bool isMountain = false;
            int count = 0;
            for (int i = 0; i < Hs.Length; i++) {
                if (!isMountain && Hs[i] != 0) {
                    count++;
                    isMountain = true;
//                    Console.WriteLine("In :" + i);
                }
                if (isMountain && Hs[i] == 0) {
                    isMountain = false;
//                    Console.WriteLine("out :" + i);
                }
            }
            return count;
        }
        static int getCost(int[] Hs, int Min) {
            int count = CountMountain(Hs);
//            Console.WriteLine("M:" + count + ", min:" + Min);
            return count * Min;
        }
    }
}


Measured memory_bytes: 13396