using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            consoleReader.String();
            var h = consoleReader.SplitInt32();


            return Calc(h);
        }

        int Calc(int[] h)
        {
            switch (h.Length)
            {
                case 0:
                    return 0;
                case 1:
                    return h[0];
                default:
                    break;
            }

            var minIndex = MinIndex(h);
            var min = h[minIndex];
            var newH1 = h.Take(minIndex).Select(i => i - min).ToArray();
            var newH2 = h.Skip(minIndex + 1).Select(i => i - min).ToArray();
            return min + Calc(newH1) + Calc(newH2);
        }


        int MinIndex(int[] h)
        {
            var min = int.MaxValue;
            var index = -1;
            for (int i = 0; i < h.Length; i++)
            {
                if (h[i] < min)
                {
                    min = h[i];
                    index = i;
                }
            }
            return index;
        }

        const int mod = 1000000007;
        private readonly ConsoleReader consoleReader;
        public Program() : this(new ConsoleReader()) { }
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program().Calc());
        public static string MultiLine<T>(IEnumerable<T> source) => string.Join("\n", source);
    }

    public class ConsoleReader
    {
        public virtual string String() => Console.ReadLine();
        public int Int32() => int.Parse(String());
        public long Int64() => long.Parse(String());
        public ulong UInt64() => ulong.Parse(String());
        public double Double() => double.Parse(String());
        public string[] Split() => String().Split().ToArray();
        public int[] SplitInt32() => String().Split().Select(int.Parse).ToArray();
        public long[] SplitInt64() => String().Split().Select(long.Parse).ToArray();
        public ulong[] SplitUInt64() => String().Split().Select(ulong.Parse).ToArray();
        public double[] SplitDouble() => String().Split().Select(double.Parse).ToArray();

        public IEnumerable<string> Repeat(int count)
        {
            for (int i = 0; i < count; i++)
                yield return String();
        }
        public IEnumerable<int> RepeatInt32(int count)
        {
            for (int i = 0; i < count; i++)
                yield return Int32();
        }
        public IEnumerable<long> RepeatInt64(int count)
        {
            for (int i = 0; i < count; i++)
                yield return Int64();
        }
        public IEnumerable<string[]> RepeatSplit(int count)
        {
            for (int i = 0; i < count; i++)
                yield return Split();
        }
        public IEnumerable<int[]> RepeatSplitInt32(int count)
        {
            for (int i = 0; i < count; i++)
                yield return SplitInt32();
        }
    }
}


Measured memory_bytes: 13396