CodeEff/ECCO huggingface dataset:
# Original solution
n=int(eval(input()))
h=list(map(int,input().split()))
print((sum([max(0,h[i+1]-h[i]) for i in range(n-1)])+h[0]))

# Optimized solution
n=int(eval(input()))
h=list(map(int,input().split()))
print((sum(max(0,h[i+1]-h[i])for i in range(n-1))+h[0]))
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))

ans = 0
while True:
    minh = 100
    sub = 0
    for i in range(n):
        if h[i] != 0:
            break

    for fin in range(i, n):
        if h[fin] == 0:
            fin -= 1
            break
    for j in range(i,fin + 1):
        if minh > h[j]:
            minh = h[j]
            sub = j
    minus = h[sub]
    for k in range(i, fin + 1):
        h[k] -= minus
    ans += minus
    
    if h.count(0) == n:
        break

print(ans)

# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))

ans = h[0]
for i in range(n - 1):
    ans += max(0, h[i + 1] - h[i])

print(ans)
# Original solution
n = int(eval(input()))
h = list(map(int,input().split()))
count = 0
done = False
while done == False:
    for i in range(n):
        if h[i] >= 0:
            h[i] -= 1
        
        if i > 0 and h[i] < 0 and h[i-1] >= 0:
            count += 1

        if i == n-1 and h[i] >= 0:
            count += 1
    if max(h) == 0:
        done = True
print(count)


# Optimized solution
n = int(eval(input()))
h = list(map(int,input().split()))
count = 0
done = False
while done == False:
    for i in range(n):
        if h[i] >= 0:
            h[i] -= 1
        
        if i > 0 and h[i] < 0 and h[i-1] >= 0:
            count += 1

        if i == n-1 and h[i] >= 0:
            count += 1
    if max(h) <= 0:
        done = True
print(count)
# Original solution
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)))



# Optimized solution
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)
# Original solution
n = int(eval(input()))
H = list(map(int,input().split()))+[0]
res = 0
for i in range(n):
    if H[i] > H[i+1]:
        res += H[i]-H[i+1]
print(res)


# Optimized solution
n = int(eval(input()))
h = [0]+list(map(int,input().split()))+[0]
res = 0
for i in range(1,n+2):
    if h[i-1] < h[i]:
        res += h[i]-h[i-1]
print(res)
# Original solution
count = 0

def rec(l, r):
    global count
    global h

    if l >= r:
        return

    zero = list()
    for i in range(l, r):
        if h[i] == 0:
            zero.append(i)

    if len(zero) > 0:
        for i in zero:
            rec(l, i)
            rec(i+1, r)
    else:
        for i in range(l, r):
            h[i] -= 1
        count += 1
        rec(l, r)

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

rec(0, len(h))
print(count)


# Optimized solution
count = 0

def rec(l, r):
    global count
    global h
    global s

    if l >= r:
        return

    zero = list()
    for i in range(l, r):
        if h[i] == 0:
            zero.append(i)

    if len(zero) > 0:
        for i in zero:
            if not (l, i) in s:
                rec(l, i)
                s.add((l, i))
            if not (i+1, r) in s:
                rec(i+1, r)
                s.add((i+1, r))
    else:
        for i in range(l, r):
            h[i] -= 1
        count += 1
        rec(l, r)

n = int(eval(input()))
h = list(map(int, input().split()))
s = set()

rec(0, len(h))
print(count)
# Original solution
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)
  


# Optimized solution
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)
# Original solution
n = int(eval(input()))
H = list(map(int, input().split()))
T = [0 for _ in range(n)]
c = 0
while True:
  f = False
  S = {0:1}
  for j in range(n):
    if H[j] > 0:
      S[1] = 1
      H[j] -= 1
      f = True
    elif len(S) > 1 and H[j] == 0:
      break
  if f: c += 1
  if H == T: break
print(c)


# Optimized solution
n = int(eval(input()))
H = [0] + list(map(int, input().split())) + [0]
T = [0 for _ in range(n+2)]
c = 0
while True:
	s0 = 0; t1 = 0
	for i in range(0, n+2):
		if H[i] > 0 and s0 == 1:
			H[i] -= 1
			t1 = 1
		elif H[i] == 0 and t1 == 1:
			c += 1
			t1 = 0
		elif H[i] == 0:
			s0 = 1
	if H == T: break
print(c)
# Original solution
def main():
    N = int(eval(input()))
    h = [int(i) for i in input().split()]
    ans = 0
    mul = h[1:N-1].count(0) + 1
    for i in range(max(h)):
        ans += 1 * mul
        z_cnt = 0
        flag = False
        for j in range(N):
            h[j] = (h[j]-1 if h[j] != 0 else h[j]) 
            if h[j] != 0:
                flag = True
            elif j == N-1 and h[j] == 0 and not flag:
                z_cnt -= 1
            elif j != 0 and j != N-1 and h[j] == 0 and flag:
                z_cnt += 1
                flag = False
        mul = z_cnt + 1


    print(ans)

if __name__ == "__main__":
    main()

# Optimized solution
def main():
    N = int(eval(input()))
    h = [0] + [int(i) for i in input().split()]
    ans = 0
    for i in range(N):
        ans += max(0, h[i+1] - h[i])
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
def main():
    N = int(eval(input()))
    h = [0] + [int(i) for i in input().split()]
    ans = 0
    for i in range(N):
        ans += max(0, h[i+1] - h[i])
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    _ = int(eval(input()))
    H = [int(i) for i in input().split()]
    cur_h = 0
    ans = 0
    for h in H:
        if cur_h < h:
            ans += h - cur_h
        cur_h = h
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
N = int(eval(input()))
h = list(map(int, input().split()))
h.append(0)
min_n = 100
ans = 0
cnt = 0
i = 0
while i < N + 1:
    if h[i] != 0:
        cnt += 1
        min_n = min(min_n, h[i])
    elif h[i] == 0 and cnt != 0:
        ans += min_n
        for j in range(i+1):
            h[j] = max(h[j]-min_n, 0)
        min_n = 100
        cnt = 0
        i = -1
    i += 1
print(ans)
    

# Optimized solution
N = int(eval(input()))
h = list(map(int, input().split())) + [0]
ans = 0
#  最大h×N本の全探索
#  連続が途切れる数を下からカウントすれば答えとなる
for i in range(max(h)):
    for j in range(N):
        if h[j] - i > 0 and h[j + 1] - i <= 0:
            ans += 1
print(ans)
# Original solution
from functools import reduce

N = int(eval(input()))
h = [int(s) for s in input().split()]

prev = 0
count = 0

for i in range(N):
    count += h[i] - prev if h[i] > prev else 0
    prev = h[i]

print(count)


# Optimized solution
def grand_garden(N: int, H: list) -> int:
    before = 0
    op = 0
    for h in H:
        if before < h:
            op += h - before
        before = h

    return op


if __name__ == "__main__":
    N = int(eval(input()))
    H = [int(s) for s in input().split()]

    ans = grand_garden(N, H)
    print(ans)
# Original solution
ri = lambda: int(eval(input()))
rl = lambda: list(map(int,input().split()))

N = ri()
h = rl()
hl = len(h)

ans = 0
flag = 0
for _ in range(sum(h)):
    if sum(h) == 0:
        break
    flag = 1
    for i in range(hl):
        if flag == 1 and h[i] != 0:
            flag = 0
            h[i] = h[i] - 1
            ans += 1
        elif flag == 0 and h[i] == 0:
            flag = 1
        elif flag == 0 and h[i] != 0:
            h[i] = h[i] - 1

print(ans)


# Optimized solution
ri = lambda: int(eval(input()))
rl = lambda: list(map(int,input().split()))

N = ri()
h = rl()

ans = 0
pre = 0
for i in h:
    if pre < i:
        ans += i - pre
    pre = i

print(ans)
# Original solution
N = int(eval(input()))
h = list(map(int, input().split()))
if sum(h) == 0:
    print((0))
    exit()
ans = 0
flag = False
while True:
    if h[0] == 0:
        flag = True
    for i in range(N):
        if h[i] == 0 and flag == True:
            continue
        elif h[i] == 0 and flag == False:
            break
        h[i] -= 1
        flag = False
    ans += 1
    if sum(h) == 0:
        break
print(ans)

# Optimized solution
N = int(eval(input()))
array = list(map(int,input().split()))
ans = 0
while True:
    if max(array) == 0:
        break
    flg = False
    for i in range(N):
        if array[i] == 0:
            if flg:
                break
            continue
        array[i] -= 1
        flg = True
    ans += 1
print(ans)
# Original solution
# -*- coding: utf-8 -*-

n = int(eval(input()))
h_list = list(map(int, input().split()))

if not len(h_list) == n :
    raise RuntimeError("Mismatch Length.")

count = 0
while True:
    h_max = max(h_list)

    if h_max == 0:
        break

    isMax_list = [h == h_max for h in h_list]

    for i, isMax in enumerate(isMax_list):
        if isMax:
            h_list[i] -= 1
            if i == 0 or not isMax_list[i-1]:
                count += 1

print(count)


# Optimized solution
N = int(eval(input()))
H = [0] + list(map(int, input().split()))
ans = 0
for i in range(N):
    ans += max(H[i + 1] - H[i], 0)
print(ans)
# Original solution
N = int(eval(input()))
H = list(map(int, input().split()))
M = max(H)

ans = 0
for m in range(1, M + 1):
    i = 0
    while i < N:
        if H[i] >= m:
            ans += 1
            while H[i] >= m:
                i += 1
                if i >= N:
                    break
        else:
            i += 1

print(ans)


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def check(H):
    N = len(H)
    for i in range(N):
        if H[i] != 0:
            return False

    return True


def main():
    N = int(eval(input()))
    H = list(map(int, input().split()))

    ans = 0
    while True:
        if check(H):
            break

        # 区間分割
        i = 0
        while i < N:
            if H[i] == 0:
                i += 1
            else:
                # 区間が始まる
                ans += 1
                while i < N and H[i] > 0:
                    H[i] -= 1
                    i += 1

    print(ans)


if __name__ == "__main__":
    main()
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))
result = 0
while True:
  start = n
  for i in range(n):
    if h[i] != 0:
      start = i
      break
  if start == n:
    break
  end = n - 1
  for i in range(start, n):
    if h[i] == 0:
      end = i - 1
      break
    h[i] -= 1
  result += 1
print(result)



# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))
result = 0
while True:
  start = n
  for i in range(n):
    if h[i] != 0:
      start = i
      break
  if start == n:
    break
  end = n - 1
  count = 101
  for i in range(start, n):
    if h[i] == 0:
      end = i - 1
      break
    if h[i] < count:
      count = h[i]
  for i in range(start, end + 1):
    h[i] -= count
  result += count
print(result)
# Original solution
def main():
    N = int(eval(input()))
    h = list(map(int, input().split()))
    c = h[0]
    for i in range(N - 1):
        if h[i + 1] > h[i]:
            c += h[i + 1] - h[i]
    print(c)


main()


# Optimized solution
N = int(eval(input()))
H = list(map(int, input().split()))
ans = H[0]
for i in range(N - 1):
    if H[i + 1] > H[i]:
        ans += H[i + 1] - H[i]

print(ans)
# Original solution
def inpl(): return [int(i) for i in input().split()]

N = int(eval(input()))
H = inpl()
ans = 0
while True:
    ctr = 1
    for i in range(N):
        if H[i]:
            ans += ctr
            ctr = 0
            H[i] -= 1
        else:
            ctr = 1
    if not any(H):
        break
print(ans)

# Optimized solution
def inpl(): return [int(i) for i in input().split()]

N = int(eval(input()))
H = [0] + inpl()
print((sum([max(0, H[i+1] - H[i]) for i in range(N)])))
# Original solution
import copy
n = int(eval(input()))
h = list(map(int, input().split()))

ans = 0
tmp = 0

for i in range(n):
    ans += h[i]
    tmp = copy.deepcopy(h)
    tmp_ = tmp[i]
    for j in range(i, n):
        if h[j] - tmp_ >= 0:
            h[j] -= tmp_
        else:
            tmp_ = tmp[j]
            h[j] = 0

print(ans)


# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))

ans = 0
tmp = 0

for i in range(n):
    ans += h[i]
    tmp = int(h[i])
    for j in range(i, n):
        if h[j] - tmp >= 0:
            h[j] -= tmp
        else:
            tmp = int(h[j])
            h[j] = 0

print(ans)
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))

ans = 0
hk = 0
for i in range(n):
    if hk == h[i]:
        continue
    elif hk < h[i]:
        while hk < h[i]:
            hk += 1
            ans += 1
    elif hk > h[i]:
        while hk > h[i]:
            hk -= 1
print(ans)

# Optimized solution
n = int(eval(input()))
h = list(map(int, input().split()))
ans = hk = 0
for i in range(n):
    if hk < h[i]:
        ans += h[i] - hk
    hk = h[i]
print(ans)
# Original solution
from collections import deque

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

stack = deque([])
prevPoint = -1

for i in range(N) :
    if h[i] == 0 :
        stack.append((prevPoint+1 , i))
        prevPoint = i
    elif i >= N - 1 :
        stack.append((prevPoint+1,N))

ans = 0

while stack :
    start , end = stack.pop()

    if start >= end :
        ans += h[start]
        h[start] = 0
        continue


    minus = min(h[start:end])
    ans += minus

    prevPoint = start - 1
    for i in range(start,end) :
        h[i] -= minus
        if h[i] == 0 :
            stack.append((prevPoint+1,i))
            prevPoint = i
        elif i >= end - 1 :
            stack.append((prevPoint+1,end))

print(ans)



# Optimized solution
N = int(eval(input()))
H = list(map(int, input().split()))
ans = 0

while max(H) > 0:
    grp = [[]]
    for i, h in enumerate(H):
        if h == 0:
            if len(grp[-1]) > 0:
                grp.append([])
        else:
            grp[-1].append((i, h))
    for g in grp:
        if len(g) == 0:
            continue
        mi = min([h for _, h in g])
        ans += mi
        for i, _ in g:
            H[i] -= mi

print(ans)
# Original solution
n = int(eval(input()))
lst = list(map(int, input().split()))
res = 0
pre_left = 0
while True:
  Max = 0
  w = 0
  left = 0
  right = n
  flag = False
  for i in range(n):
    if lst[i] > 0 and not flag:
      if i == n-1:
        if Max:
          break
      w = 1
      pre_left = left
      left = i
      flag = True
      if i == n-1:
        right = n
        Max = 1
    elif lst[i] > 0 and flag:
      w += 1
      if i == n-1:
        if Max < w:
          Max = w
          right = n
        else:
          left = pre_left
    elif lst[i] <= 0 and flag:
      if Max < w:
        Max = w
        right = i
        flag = False
      else:
        left = pre_left
      w = 0
  if w == n:
    Max = n
  if Max == 0:
    break
  d = min(lst[left:right])
  for i in range(left, right):
    lst[i] -= d
  res += d
      
print(res)

# Optimized solution
def rec(lst, left, right):
    if left == right:
        return 0
    if not 0 in lst[left:right]:
        m = min(lst[left:right])
        lst[left:right] = [x - m for x in lst[left:right]]
        return m + rec(lst, left, right)
    k = lst[left:right].index(0)
    return rec(lst, left, left + k) + rec(lst, left + k+1, right)


N = int(eval(input()))
lst = list(map(int, input().split()))
print((rec(lst, 0, len(lst))))
# Original solution
def dfs(L):
    if len(L) == 0:
        return 0

    ret = min(L)
    L = [e - ret for e in L]
    L2 = []
    for i, e in enumerate(L):
        if e == 0:
            ret += dfs(L2)
            L2 = []
        else:
            L2.append(e)

    ret += dfs(L2)
    return ret

def main():
    n = int(eval(input()))
    H = [int(e) for e in input().split()]

    print((dfs(H)))

if __name__ == "__main__":
    main()


# Optimized solution
def dfs(L):
    if len(L) == 0:
        return 0

    ret = min(L)
    L = [e - ret for e in L]
    L2 = []
    for e in L:
        if e == 0:
            ret += dfs(L2)
            L2 = []
        else:
            L2.append(e)

    ret += dfs(L2)
    return ret

def main():
    n = int(eval(input()))
    H = [int(e) for e in input().split()]

    print((dfs(H)))

if __name__ == "__main__":
    main()
# Original solution
n = int(eval(input()))
h = list(map(int, input().split()))

res = 0
i = 0
while sum(h) != 0:
    if h[i] != 0:
        while i < n and h[i] != 0:
            h[i] -= 1
            i += 1
        res += 1
        i = 0
    else:
        i += 1

print(res)


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(eval(input()))
    H = list(map(int, input().split()))

    res = 0
    i = 0
    while sum(H) != 0:
        if H[i] != 0:
            while i < n and H[i] != 0:
                H[i] -= 1
                i += 1
            res += 1
            i = 0
        else:
            i += 1

    print(res)


if __name__ == '__main__':
    resolve()
# Original solution
n = int(eval(input()))
H = list(map(int,input().split()))
seq = []
K = []
ans = 0
for i in H:
    if i != 0:
        K.append(i)
    else:
        if K:
            seq.append(K)
            K = []
else:
    if K:
        seq.append(K)
while seq:
    L = seq.pop()
    ans += min(L)
    S = [n-min(L) for n in L]
    K = []
    for i in S:
        if i!= 0:
            K.append(i)
        else:
            if K:
                seq.append(K)
                K = []
    else:
        if K:
            seq.append(K)
else:
    print(ans)
    

# Optimized solution
n = int(eval(input()))
seq =[list(map(int,input().split()))]
ans = 0
while seq:
    L = seq.pop()
    ans += min(L)
    S = [n-min(L) for n in L]
    K = []
    for i in S:
        if i!= 0:
            K.append(i)
        else:
            if K:
                seq.append(K)
                K = []
    else:
        if K:
            seq.append(K)
else:
    print(ans)
# Original solution
N = int(eval(input()))
H = list(map(int, input().split()))

# Hを1段ずつ減らしていく.
# 値が正の区間 (left, right) が一つ見つかるごとに答えは1増える.

ans = 0
while sum(H) > 0:
    left = right = 0
    prev = 0
    segment = []
    for i, h in enumerate(H):
        if h == 0 and prev != 0: # H = {..., 0, *(left), *, ..., *, 0(right), ...} (*: 正数)
            right = i
            segment.append((left, right))
        elif h != 0 and prev == 0:
            left = i
            if i == N-1: # H = {..., 0, *(left)} (*: 正数)
                right = i + 1
                segment.append((left, right))
        elif h != 0 and i == N-1: #(prev != 0) H = {..., 0, *(left), ..., *, *} (*: 正数)
            right = i + 1
            segment.append((left, right))
        prev = h
    ans += len(segment)
    #  Hの値が正のところを1段減らす.
    for l, r in segment:
        for i in range(l, r):
            H[i] -= 1
print(ans)

# Optimized solution
N = int(eval(input()))
H = list(map(int, input().split()))

stack = [H]
count = 0
while stack:
    lst = stack.pop()
    m = min(lst)
    count += m
    lst = [l-m for l in lst]
    prev = 0
    left = 0
    for i, h in enumerate(lst):
        if h == 0 and prev != 0: # H = {..., 0, *(left), *, ..., *, 0(right), ...} (*: 正数)
            stack.append(lst[left:i])
        elif h != 0 and prev == 0:
            left = i
            if i == len(lst)-1: # H = {..., 0, *(left)} (*: 正数)
                stack.append(lst[left:i+1])
        elif h != 0 and i == len(lst)-1: #(prev != 0) H = {..., 0, *(left), ..., *, *} (*: 正数)
            stack.append(lst[left:i+1])
        prev = h
print(count)
# Original solution
N=int(eval(input()))
L=[int(i) for i in input().split()]
L.append(0)
ans=0
for i in range(N):
  ans+=max(0,L[i]-L[i+1])
print(ans)

# Optimized solution
N=int(eval(input()))
L=[int(i) for i in input().split()]
#L.append(0)
L.insert(0,0)
ans=0
for i in range(N):
  ans+=max(0,L[i+1]-L[i])
print(ans)
# Original solution
n=int(eval(input()))
h=list(map(int,input().split()))
ans=0
while any(h)==True:
    flag=0
    for i in range(n-1):
        if h[i]>0:
            h[i]-=1
            flag=1
        else:
            if flag==1:
                ans+=1
                flag=0
        if i==n-2:
            if h[n-1]>0:
                h[n-1]-=1
                ans+=1
            else:
                if flag==1:
                    ans+=1

print(ans)


# Optimized solution
n=int(eval(input()))
h=list(map(int,input().split()))
ans=0
if n==1:
    print((h[0]))
    exit()
while any(h)==True:
    flag=0
    for i in range(n-1):
        if h[i]>0:
            h[i]-=1
            flag=1
        else:
            if flag==1:
                ans+=1
                flag=0
        if i==n-2:
            if h[n-1]>0:
                h[n-1]-=1
                ans+=1
            else:
                if flag==1:
                    ans+=1
print(ans)
# Original solution
N = int(eval(input()))
h = list(map(int, input().split()))

ans = 0
i = 0
while i < N:
    while h[i] == 0:
        i += 1
        if i == N: break
    j = i
    if i == N: break
    while h[j] > 0:
        h[j] -= 1
        j += 1
        if j == N: break
    ans += 1

print(ans)


# Optimized solution
n = int(eval(input()))
h = tuple(map(int, input().split()))

ans = 0
cur = 0
for hh in h:
    if cur < hh:
        ans += hh - cur
        cur = hh
    else:
        cur = hh
print(ans)
# Original solution
def solve_range(h, frm, to):
    """[frm, to)"""
    # print(frm, to, h)
    if frm >= to:
        return 0
    res = 0
    start = frm
    for i in range(frm, to):
        h[i] -= 1
        if h[i] < 0:
            if start < i:
                res += solve_range(h, start, i) + 1
            start = i + 1
        elif i == to - 1:
            res += solve_range(h, start, to) + 1
    return res


def solve(h):
    return solve_range(h, 0, n)


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

print((solve(h)))

# Optimized solution
def main():
    N = int(eval(input()))
    H = list(map(int, input().split()))

    cnt = 0
    p = 0
    for h in H:
        cnt += max(0, h - p)
        p = h
    print(cnt)


if __name__ == '__main__':
    main()
# Original solution
N = int(eval(input()))
h = list(map(int, input().split()))
answer = h[0]
for i in range(1, N):
    if h[i] > h[i-1]:
        answer += h[i] - h[i-1]

print(answer)


# Optimized solution
N = int(eval(input()))
H = list(map(int, input().split()))
ans = H[0]
tmp = H[0]
for h in H:
    if tmp < h:
        ans += (h-tmp)
    tmp = h

print(ans)