import sys

 

stdin = sys.stdin

 

ni = lambda: int(ns())

na = lambda: list(map(int, stdin.readline().split()))

ns = lambda: stdin.readline().rstrip()

nas = lambda: stdin.readline().split()



n = ni()

a = na()



res = 0

while sum(a):

    res += 1

    f = False

    for i in range(n):

        if a[i] > 0:

            a[i] -= 1

            f = True

        elif f is True:

            break



print(res)
import sys



stdin = sys.stdin

inf = 1 << 60

mod = 1000000007



ni      = lambda: int(ns())

nin     = lambda y: [ni() for _ in range(y)]

na      = lambda: list(map(int, stdin.readline().split()))

nan     = lambda y: [na() for _ in range(y)]

nf      = lambda: float(ns())

nfn     = lambda y: [nf() for _ in range(y)]

nfa     = lambda: list(map(float, stdin.readline().split()))

nfan    = lambda y: [nfa() for _ in range(y)]

ns      = lambda: stdin.readline().rstrip()

nsn     = lambda y: [ns() for _ in range(y)]

ncl     = lambda y: [list(ns()) for _ in range(y)]

nas     = lambda: stdin.readline().split()



n = ni()

a = na()



flag = True

ans = 0

while flag:

    flag = False

    last = 0

    for i in range(n):

        t = a[i]

        if a[i] != 0:

            flag = True

            if last == 0:

                ans += 1

            a[i] -= 1

        last = t



print(ans)

import sys

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()
n = ni()
a = na()
res = 0
while sum(a):
    res += 1
    f = False
    for i in range(n):
        if a[i] > 0:
            a[i] -= 1
            f = True
        elif f is True:
            break
print(res)

import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007
ni = lambda: int(ns())
nin = lambda y: [ni() for _ in range(y)]
na = lambda: list(map(int, stdin.readline().split()))
nan = lambda y: [na() for _ in range(y)]
nf = lambda: float(ns())
nfn = lambda y: [nf() for _ in range(y)]
nfa = lambda: list(map(float, stdin.readline().split()))
nfan = lambda y: [nfa() for _ in range(y)]
ns = lambda: stdin.readline().rstrip()
nsn = lambda y: [ns() for _ in range(y)]
ncl = lambda y: [list(ns()) for _ in range(y)]
nas = lambda: stdin.readline().split()
n = ni()
a = na()
flag = True
ans = 0
while flag:
    flag = False
    last = 0
    for i in range(n):
        t = a[i]
        if a[i] != 0:
            flag = True
            if last == 0:
                ans += 1
            a[i] -= 1
        last = t
print(ans)

+inf = 1 << 60 +mod = 1000000007 +nin = lambda y: [ni() for _ in range(y)] +nan = lambda y: [na() for _ in range(y)] +nf = lambda: float(ns()) +nfn = lambda y: [nf() for _ in range(y)] +nfa = lambda: list(map(float, stdin.readline().split())) +nfan = lambda y: [nfa() for _ in range(y)] +nsn = lambda y: [ns() for _ in range(y)] +ncl = lambda y: [list(ns()) for _ in range(y)] -res = 0 -while sum(a): -    res += 1 -    f = False +flag = True +ans = 0 +while flag: +    flag = False +    last = 0 -        if a[i] > 0: +        t = a[i] +        if a[i] != 0: +            flag = True +            if last == 0: +                ans += 1 -            f = True -        elif f is True: -            break -print(res) +        last = t +print(ans)