from collections import deque



def sol():

  q = deque()

  q.append(H)

  ans = 0

  while len(q) != 0:

    prv_H = q.pop()

    min_h = min(prv_H)

    ans += min_h

    

    tmp_H = ''.join([',' if h-min_h == 0 else str(h-min_h)+' ' for h in prv_H])

    for sub_H in tmp_H.split(','):

      if len(sub_H.replace(' ','')) != 0 :

        q.append([int(h) for h in sub_H.split()])

    #print(ans, q) 

  return ans



N = int(eval(input()))

H = list(map(int,input().split()))

print((sol()))

import itertools





def get_num_1group(input_list):

    num_group = 0

    for key, elems in itertools.groupby(input_list):

        if key == 1:

            num_group += 1

    return num_group





N = int(eval(input()))

H = list(map(int, input().split()))

maxH = max(H)

hist = [[0 for _ in range(N)] for _ in range(maxH)]

for idx, h in enumerate(H):

    for num in range(h):

        hist[num][idx] = 1



print((sum([get_num_1group(hist[num]) for num in range(maxH)])))
from collections import deque


def sol():
    q = deque()
    q.append(H)
    ans = 0
    while len(q) != 0:
        prv_H = q.pop()
        min_h = min(prv_H)
        ans += min_h
        tmp_H = "".join(
            ["," if h - min_h == 0 else str(h - min_h) + " " for h in prv_H]
        )
        for sub_H in tmp_H.split(","):
            if len(sub_H.replace(" ", "")) != 0:
                q.append([int(h) for h in sub_H.split()])
        # print(ans, q)
    return ans


N = int(eval(input()))
H = list(map(int, input().split()))
print((sol()))

import itertools


def get_num_1group(input_list):
    num_group = 0
    for key, elems in itertools.groupby(input_list):
        if key == 1:
            num_group += 1
    return num_group


N = int(eval(input()))
H = list(map(int, input().split()))
maxH = max(H)
hist = [[0 for _ in range(N)] for _ in range(maxH)]
for idx, h in enumerate(H):
    for num in range(h):
        hist[num][idx] = 1
print((sum([get_num_1group(hist[num]) for num in range(maxH)])))

-from collections import deque +import itertools -def sol(): -    q = deque() -    q.append(H) -    ans = 0 -    while len(q) != 0: -        prv_H = q.pop() -        min_h = min(prv_H) -        ans += min_h -        tmp_H = "".join( -            ["," if h - min_h == 0 else str(h - min_h) + " " for h in prv_H] -        ) -        for sub_H in tmp_H.split(","): -            if len(sub_H.replace(" ", "")) != 0: -                q.append([int(h) for h in sub_H.split()]) -        # print(ans, q) -    return ans +def get_num_1group(input_list): +    num_group = 0 +    for key, elems in itertools.groupby(input_list): +        if key == 1: +            num_group += 1 +    return num_group -print((sol())) +maxH = max(H) +hist = [[0 for _ in range(N)] for _ in range(maxH)] +for idx, h in enumerate(H): +    for num in range(h): +        hist[num][idx] = 1 +print((sum([get_num_1group(hist[num]) for num in range(maxH)])))