from sys import stdin

def main():

    #入力

    readline=stdin.readline

    N=int(readline())

    h=list(map(int,readline().split()))



    count=0

    while 1:

        first=True

        for i in range(N):

            if first and h[i]>=1:

                first=False

                h[i]-=1

            elif first and h[i]==0:

                pass

            elif first is False and h[i]>=1:

                h[i]-=1

            elif first is False and h[i]==0:

                break

        if first:

            break

        else:

            count+=1

    print(count)



if __name__=="__main__":

    main()
from sys import stdin

def main():

    #入力

    readline=stdin.readline

    n=int(readline())

    h=list(map(int,readline().split()))

    ans=0

    while True:

        flag=True

        flag2=True

        for i in range(n):

            if h[i]>0:

                h[i]-=1

                flag=False

                flag2=False

                if i==n-1:

                    ans+=1

            else:

                if flag2==False:

                    flag2=True

                    ans+=1

        if flag: break



    print(ans)



if __name__=="__main__":

    main()
from sys import stdin


def main():
    # 入力
    readline = stdin.readline
    N = int(readline())
    h = list(map(int, readline().split()))
    count = 0
    while 1:
        first = True
        for i in range(N):
            if first and h[i] >= 1:
                first = False
                h[i] -= 1
            elif first and h[i] == 0:
                pass
            elif first is False and h[i] >= 1:
                h[i] -= 1
            elif first is False and h[i] == 0:
                break
        if first:
            break
        else:
            count += 1
    print(count)


if __name__ == "__main__":
    main()

from sys import stdin


def main():
    # 入力
    readline = stdin.readline
    n = int(readline())
    h = list(map(int, readline().split()))
    ans = 0
    while True:
        flag = True
        flag2 = True
        for i in range(n):
            if h[i] > 0:
                h[i] -= 1
                flag = False
                flag2 = False
                if i == n - 1:
                    ans += 1
            else:
                if flag2 == False:
                    flag2 = True
                    ans += 1
        if flag:
            break
    print(ans)


if __name__ == "__main__":
    main()

-    N = int(readline()) +    n = int(readline()) -    count = 0 -    while 1: -        first = True -        for i in range(N): -            if first and h[i] >= 1: -                first = False +    ans = 0 +    while True: +        flag = True +        flag2 = True +        for i in range(n): +            if h[i] > 0: -            elif first and h[i] == 0: -                pass -            elif first is False and h[i] >= 1: -                h[i] -= 1 -            elif first is False and h[i] == 0: -                break -        if first: +                flag = False +                flag2 = False +                if i == n - 1: +                    ans += 1 +            else: +                if flag2 == False: +                    flag2 = True +                    ans += 1 +        if flag: -        else: -            count += 1 -    print(count) +    print(ans)