N = int(eval(input()))

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

ans = 0





def water(x):

  global ans

#  print(x, ans)

  if any(x) == False:

#    print(0)

    return

  else:

#    print(1)

    if 0 in x:

#      print(2)

      x1, x2 = x[0:x.index(0)], x[x.index(0)+1:]

      water(x1)

      water(x2)

    else:

#      print(3)

      ans += min(x)

      x = list([i-min(x) for i in x])

      water(x)

  return    

  

water(h)

print(ans)

  

N = int(eval(input()))

h = [int(i) for i in input().split()]

h.append(0)

ans = 0



while any(h):

  l,r = 0, 0

  for i in range(N):

    if h[i] == 0 and h[i+1] != 0:

      l = i+1

    if (h[i] != 0 and h[i+1] == 0):

      r = i+1

      minh = min(h[l:r])

      ans += minh

      for j in range(l,r):

        h[j] -= minh

print(ans)
N = int(eval(input()))
h = list(map(int, input().split()))
ans = 0


def water(x):
    global ans
    #  print(x, ans)
    if any(x) == False:
        #    print(0)
        return
    else:
        #    print(1)
        if 0 in x:
            #      print(2)
            x1, x2 = x[0 : x.index(0)], x[x.index(0) + 1 :]
            water(x1)
            water(x2)
        else:
            #      print(3)
            ans += min(x)
            x = list([i - min(x) for i in x])
            water(x)
    return


water(h)
print(ans)

N = int(eval(input()))
h = [int(i) for i in input().split()]
h.append(0)
ans = 0
while any(h):
    l, r = 0, 0
    for i in range(N):
        if h[i] == 0 and h[i + 1] != 0:
            l = i + 1
        if h[i] != 0 and h[i + 1] == 0:
            r = i + 1
            minh = min(h[l:r])
            ans += minh
            for j in range(l, r):
                h[j] -= minh
print(ans)

-h = list(map(int, input().split())) +h = [int(i) for i in input().split()] +h.append(0) - - -def water(x): -    global ans -    #  print(x, ans) -    if any(x) == False: -        #    print(0) -        return -    else: -        #    print(1) -        if 0 in x: -            #      print(2) -            x1, x2 = x[0 : x.index(0)], x[x.index(0) + 1 :] -            water(x1) -            water(x2) -        else: -            #      print(3) -            ans += min(x) -            x = list([i - min(x) for i in x]) -            water(x) -    return - - -water(h) +while any(h): +    l, r = 0, 0 +    for i in range(N): +        if h[i] == 0 and h[i + 1] != 0: +            l = i + 1 +        if h[i] != 0 and h[i + 1] == 0: +            r = i + 1 +            minh = min(h[l:r]) +            ans += minh +            for j in range(l, r): +                h[j] -= minh