N = int(eval(input()))



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



count = 0 #水をやる回数

def water(i, j): #lst[i:j]に水をやるとする

    count = 0

    #print ('A')

    #print ('lst', lst[i:j])

    #print ('i, j=', i, j)

    if i == j or lst[i:j] == [0]:

        #print ('X')

        return 0

    elif min(lst[i:j]) == 0: #lst[i:j]内の最小値が0の時

        a = lst[i:j].index(0)

        #print ('a=', a)

        #print ('B', count)

        count = water(i,i + a) + water(i + a+1, j)

        return count

    else: #lst[i:j]の最小値が0でない時

        for x in range(i,j):

            lst[x] = lst[x] - 1

        #print ('C', count)

        count += 1 + water(i, j)

        return count



print((water(0, N)))



import sys

input = sys.stdin.readline

sys.setrecursionlimit(10 ** 9)

MOD = 10 ** 9 + 7



N = int(eval(input()))

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



if N == 1:

    print((H[0]))

    exit()



ans = 00

i = 1

for i in range(N - 1):

    if H[i] < H[i + 1]:

        continue

    else:

        ans += H[i] - H[i + 1]



ans += H[N - 1]

print (ans)
N = int(eval(input()))
lst = list(map(int, input().split()))
count = 0  # 水をやる回数


def water(i, j):  # lst[i:j]に水をやるとする
    count = 0
    # print ('A')
    # print ('lst', lst[i:j])
    # print ('i, j=', i, j)
    if i == j or lst[i:j] == [0]:
        # print ('X')
        return 0
    elif min(lst[i:j]) == 0:  # lst[i:j]内の最小値が0の時
        a = lst[i:j].index(0)
        # print ('a=', a)
        # print ('B', count)
        count = water(i, i + a) + water(i + a + 1, j)
        return count
    else:  # lst[i:j]の最小値が0でない時
        for x in range(i, j):
            lst[x] = lst[x] - 1
        # print ('C', count)
        count += 1 + water(i, j)
        return count


print((water(0, N)))

import sys

input = sys.stdin.readline
sys.setrecursionlimit(10**9)
MOD = 10**9 + 7
N = int(eval(input()))
H = list(map(int, input().split()))
if N == 1:
    print((H[0]))
    exit()
ans = 00
i = 1
for i in range(N - 1):
    if H[i] < H[i + 1]:
        continue
    else:
        ans += H[i] - H[i + 1]
ans += H[N - 1]
print(ans)

+import sys + +input = sys.stdin.readline +sys.setrecursionlimit(10**9) +MOD = 10**9 + 7 -lst = list(map(int, input().split())) -count = 0  # 水をやる回数 - - -def water(i, j):  # lst[i:j]に水をやるとする -    count = 0 -    # print ('A') -    # print ('lst', lst[i:j]) -    # print ('i, j=', i, j) -    if i == j or lst[i:j] == [0]: -        # print ('X') -        return 0 -    elif min(lst[i:j]) == 0:  # lst[i:j]内の最小値が0の時 -        a = lst[i:j].index(0) -        # print ('a=', a) -        # print ('B', count) -        count = water(i, i + a) + water(i + a + 1, j) -        return count -    else:  # lst[i:j]の最小値が0でない時 -        for x in range(i, j): -            lst[x] = lst[x] - 1 -        # print ('C', count) -        count += 1 + water(i, j) -        return count - - -print((water(0, N))) +H = list(map(int, input().split())) +if N == 1: +    print((H[0])) +    exit() +ans = 00 +i = 1 +for i in range(N - 1): +    if H[i] < H[i + 1]: +        continue +    else: +        ans += H[i] - H[i + 1] +ans += H[N - 1] +print(ans)