Problem:
 Score : 300 points    Problem Statement In a flower bed, there are N flowers, numbered 1,2,......,N . Initially, the heights of all flowers are 0 .
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k  (1 \leq  k \leq N) , by repeating the following "watering" operation:   Specify integers l and r . Increase the height of Flower x by 1 for all x such that l \leq x \leq r .   Find the minimum number of watering operations required to satisfy the condition.      Constraints  1 \leq N  \leq 100  0 \leq h_i \leq 100  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  h_1  h_2  h_3  ......  h_N       Output Print the minimum number of watering operations required to satisfy the condition.        Sample Input 1 4
1 2 2 1      Sample Output 1 2  The minimum number of watering operations required is 2 .
One way to achieve it is:   Perform the operation with (l,r)=(1,3) .  Perform the operation with (l,r)=(2,4) .        Sample Input 2 5
3 1 2 3 1      Sample Output 2 5       Sample Input 3 8
4 23 75 0 23 96 50 100      Sample Output 3 221

Solution (Python):
n = int(input())
heights = list(map(int,input().split()))
ans = 0

for i in range(1,101):
    c = 0
    for j in range(1,n):
        if heights[j-1] >= i and heights[j] < i:
            c += 1
        else:
            continue
    if heights[-1] >= i:
        c += 1
    ans += c
    
print(ans)

Solution (Python):
n = int(input())
heights = list(map(int,input().split()))

ans = 0

for i in range(1,101):
    c = 0
    for j in range(1,n):
        if heights[j-1] >= i and heights[j] < i:
            c += 1
        
        else:
            continue
    
    if heights[-1] >= i:
        c += 1
    
    ans += c
    

print(ans)

Solution (Python):
n = int(input())
list = [int(i) for i in input().split()]
count = 0

for i in range(n-1):
    if list[i]<=list[i+1]:
        continue
    else:
        count += list[i] - list[i+1]

count += list[n-1]
print(count)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
ans=0

while sum(h)>0:
    flug=0
    for i in range(n):
        if h[i]>0:
            h[i]-=1
            flug=1
        else:
            if flug==1:
                ans+=1
                flug=0
    else:
        if flug==1:
            ans+=1
    
print(ans)

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    N=int(readline())
    h=list(map(int,readline().split()))

    count=0
    while 1:
        first=True
        for i in range(N):
            if first and h[i]>=1:
                first=False
                h[i]-=1
            elif first and h[i]==0:
                pass
            elif first is False and h[i]>=1:
                h[i]-=1
            elif first is False and h[i]==0:
                break
        if first:
            break
        else:
            count+=1
    print(count)

if __name__=="__main__":
    main()

Solution (Python):
n = int(input())
hl = list(map(int, input().split()))

hll = []
cnt = 0

zero_check = []
tmp = []
for h in hl:
    if h != 0:
        tmp.append(h)
    elif h == 0 and len(tmp) > 0:
        zero_check.append(tmp)
        tmp = []

if len(tmp) > 0:
    zero_check.append(tmp)

hl = zero_check[:]


while len(hl) > 0:
    for h in hl:
        cnt += 1
        tmp_num = []
        for num in h:
            if num - 1 != 0:
                tmp_num.append(num - 1)
            elif num - 1 == 0 and len(tmp_num) > 0:
                hll.append(tmp_num)
                tmp_num = []
                
            

        if len(tmp_num) > 0:
            hll.append(tmp_num)

    hl = hll
    hll = []
    

print(cnt)

Solution (Python):
from collections import deque
import sys

sys.setrecursionlimit(10 ** 5 + 10)


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

    cluster = deque([H])
    cnt = 0
    while cluster:
        i = cluster.popleft()
        if len(i) != 0:
            mini = min(i)
            cnt += mini
            for j in range(len(i)):
                i[j] -= mini

            j = 0

            for k in range(len(i)):
                if i[k] == 0:
                    cluster.append(i[j:k])

                    j = k + 1
                elif k == len(i) - 1:
                    cluster.append(i[j:len(i)])

    print(cnt)


resolve()

Solution (Python):

from collections import Counter, deque






import sys
sys.setrecursionlimit(10 ** 5 + 10)


def resolve():

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

    cluster=deque([H])
    cnt=0
    while cluster:
        i=cluster.popleft()
        if len(i)!=0:
            mini=min(i)
            cnt += mini
            for j in range(len(i)):
                i[j]-=mini

            j = 0

            for k in range(len(i)):
                if i[k]==0:
                    cluster.append(i[j:k])

                    j=k+1
                elif k==len(i)-1:
                    cluster.append(i[j:len(i)])

    print(cnt)
resolve()

Solution (Python):
N = int(input())
h = list(map(int,(input().split())))
c = 0

while sum(h) > 0:
    m = max(h)
    if h[0] == m:
        c += 1
    for i in range(1,N):
        if h[i] == m:
            if h[i] != h[i-1]:
                c += 1
    for j in range(N):
            if h[j] == m:
                h[j] -= 1

print(c)

Solution (Python):
n = int(input())
list_H = list(map(int, input().split()))
max_value = max(list_H)
list_K = [[0]*n for _ in range(max_value)]
for i in range(max_value):
    l = list_K[i]
    for j in range(n):
        if list_H[j] > i:
            l[j] = 1

ans = []
for i in range(len(list_K)):
    cnt = 0
    l = list_K[i]
    l.append(0)
    for j in range(n):
        if l[j] == 1 and l[j+1] == 0:
            cnt += 1
    ans.append(cnt)

print(sum(ans))

Solution (Python):
N=int(input())
h=list(map(int,input().split()))
cnt=0
L=0
R=0
time=0

while L<=N-1:
  time +=1
  R=-1
  for i in range(L,N):
    if h[i]>0:
      R=i
      h[i] -= 1
    else:
      break
  
  if R>=L:
    cnt +=1
  else:
    L +=1
    
print(cnt)

Solution (Python):
n = int(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)

Solution (Python):
N=int(input())
h=list(map(int,input().split()))
s=h[0]
for i in range (1,N):
  if h[i]>h[i-1]:
    s+=(h[i]-h[i-1])
print(s)

Solution (Python):
def main():
  n = int(input())
  h = input_list()
  ans = 0
  while True:
    ans += count_section_by_zero(h)
    h = list(map(lambda x: 0 if x == 0 else x-1, h))
    if all([v == 0 for v in h]):
      break
  print(ans)

def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count
  
def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return map(str, input().split())

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)

def gcd(*numbers):
    return reduce(fractions.gcd, numbers)

def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce
main()

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
count = 0
i = 0
while sum(h) > 0:
    if h[i] > 0:
        j = i
        count += 1
        while j < N and h[j] > 0:
            h[j] = h[j] - 1
            j += 1
    else:
        i += 1
print(count)

Solution (Python):
n = int(input())
h = [int(x) for x in input().split()]
ans = h[0]
for i in range(1, n):
  if h[i-1] < h[i]:
    ans += h[i]-h[i-1]
print(ans)

Solution (Python):
import copy
N = int(input())
H = [i for i in map(int,input().split())]
res = 0
while True:
  i = 0
  if max(H) == 0:
    break
  while (i<N):
    if H[i] == 0:
      i += 1
    else:
      res += 1
      while (i < N and H[i] > 0):
        H[i] -= 1
        i += 1
print(res)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))

def list_split(list_in):
    list_out=[];
    ind=0
    for i in range(len(list_in)):
        if (list_in[i]==0):
            if sum(list_in[ind:i])>0:
                list_out.append(list_in[ind:i])
            ind=i+1

        if i==(len(list_in)-1):
            if sum(list_in[ind:i+1])>0:
                list_out.append(list_in[ind:i+1])
    return(list_out)


import numpy as np

def count_min(list_in):
    count = 0
    out_list=[]
    for i in list_in:
        tmp_count=min(i)
        tmp=list(np.array(i)-min(i))
        tmp=list_split(tmp)
        if tmp!=[[]]:
            out_list.extend(tmp)
        count=count+tmp_count
    return(count,out_list)

check=0
total_count=0
new_list=list_split(a)
while check==0:
    tmp_count,new_list=count_min(new_list)
    total_count=total_count+tmp_count
    if len(new_list)==0:
        break
    
    
print(total_count)

Solution (Python):
N=int(input())
h=list(map(int,input().split()))

count=1
judge=False
answer=0

while count<=max(h):
    judge=False
    for i in h:
        if i>=count:
            judge=True
        elif i<count and judge==True:
            answer+=1
            judge=False
    if judge==True:
        answer+=1
    count+=1

print(answer)

Solution (Python):
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(input())
    H = [int(e) for e in input().split()]

    print(dfs(H))

if __name__ == "__main__":
    main()

Solution (Python):
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(input())
    H = [int(e) for e in input().split()]

    print(dfs(H))

if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
s = h[0]
c = s
for i in h[1:]:
	if i > s:
		c += i-s
		s = i
	else:
		s = i
print (c)

Solution (Python):
n = int(input())
h = list(map(int,input().split()))

h.append(0)
count = 0
for i in reversed(range(103)):
	for j in range(len(h)-1):
		if i <= h[j] and h[j+1] < i:
			count += 1


print(count)

Solution (Python):
n = int(input())

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

cnt = 0
ans = 0
low = 0
for i in range(n):
    if cnt < h[i]:
        cnt = h[i]
    elif cnt > h[i]:
        ans += cnt - low
        low = h[i]
        cnt = h[i]
    

ans += cnt - low
print(ans)

Solution (Python):


n=int(input())
h=[int(x) for x in input().split()]
h.append(0)
ans=0
for k in reversed(range(1,105)):
    for i in range(n):
        if(h[i]>=k and h[i+1]<k):
            ans+=1
print(ans)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
ans = 0
cnt = 0
while sum(h) != 0:
    
    
    cnt += 1
    beg = 0  
    i = 0
    mn = 200
    while i < n:
        if h[i] == 0:
            if mn != 200:
                ans += mn
                for j in range(beg, i):
                    h[j] -= mn
                mn = 200
            beg = i + 1  
        else: mn = min(mn, h[i])
        i += 1
    if mn != 200:
        ans += mn
        for j in range(beg, n):
            h[j] -= mn
print(ans)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))

count = 0

while any(h):
  lists = [i for i, x in enumerate(h) if x > 0]
  i  = lists[0]
  start = i
  end = i
  while end + 1 < N and h[end + 1] > 0:
    end += 1
  for index in range(start, end + 1):
    h[index] -= 1
  count += 1

print(count)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))

ans = 0
pos = 0

for i in h:
    ans += max(i - pos, 0)
    pos = i

print(ans)

Solution (Python):
count = 0
N = int(input())
h = list(map(int, input().split()))
count =+ min(h)
h = list(map(lambda x:  x-min(h) ,h))


h.append(0)
while max(h) != 0:
    for i in range(len(h)):
        if h[i] <= 0:
            continue
        elif h[i-1] > 0:
            continue
        else:
            for j in range(i+1,len(h)+1):
                if h[j] > 0:
                    continue
                else:
                    count += 1
                    break
    h = list(map(lambda x:  x-1 ,h))
print(count)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))

ans = 0
pos = 0

for i in h:
    ans += max(i - pos, 0)
    pos = i

print(ans)

Solution (Python):




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

    result = postion = 0
    for h in H:
        result += max(h - postion, 0)
        postion = h
    print(result)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
H = [int(i) for i in input().split()]

cnt = 0
K = [0] * N
while True:
    if H == K:
        break

    for l in range(N):
        if H[l] > K[l]:
            cnt += 1
            for r in range(l,N):
                if H[r] == K[r]:
                    break
                K[r] += 1
            break

print(cnt)

Solution (Python):
N = int(input())
h = [int(x) for x in input().split()]

ans = 0
sum_height = sum(h)
while sum_height > 0:
    for i in range(N):
        if h[i] == 0:
            pos = False
            continue
        elif (i == 0 and h[i] > 0) or (i > 0 and not pos):
            ans += 1
        h[i] -= 1
        sum_height -= 1
        pos = True

print(ans)

Solution (Python):
N = int(input())
h = [int(x) for x in input().split()]

ans = 0
sum_height = sum(h)
while sum_height > 0:
    i = 0
    while i < N:
        while i < N and h[i] == 0:
            i += 1
        if i < N:
            ans += 1
        while i < N and h[i] > 0:
            h[i] -= 1
            sum_height -= 1
            i += 1

print(ans)

Solution (Python):
n = int(input())
l = [int(i) for i in input().split()]

cnt = 0
while l.count(0) < n:
    for i in range(n):
        if (i > 0 and l[i-1] == 0 and l[i] == 0) or (i == 0 and l[i] == 0):
            continue
        if l[i] == 0 or i == n-1:
            cnt += 1
    l = [k-1 if k > 0 else k for k in l]

print(cnt)

Solution (Python):
N = int(input())
H = list(map(int, input().split(' ')))

ret = 0
while True:
    left = -1
    for i, h in enumerate(H):
        if h:
            left = i
            break

    if left == -1:
        break

    right = N
    for i in range(left, N):
        if not H[i]:
            right = i
            break

    v = min(H[left: right])
    ret += v
    for i in range(left, right):
        H[i] -= v


print(ret)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))




now = 0
ans = 0
while now < N:
    if h[now] > 0:
        ans += 1
        i = now
        while i < N and h[i] > 0:
            h[i] -= 1
            i += 1
    else:
        now += 1
print(ans)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))



count = 0

while True:
    if max(h) == 0: break
    i = 0
    while i < N:
        if h[i]==0:
            i += 1
        else:
            count += 1
            while i < N and h[i] > 0:
                h[i] -= 1
                i += 1
print(count)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
cnt=0
while any(h):
    f=True
    for i in range(n):
        if f and h[i]==0:
            continue
        if h[i] :
            f=False
            h[i]-=1
        else:
            break
    cnt+=1
    
print(cnt)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))

num_ma = h[0]
ans = 0
flag = True


for i in range(n):
    if h[i] >= num_ma:
        num_ma = h[i]
        flag = True
    else:
        ans += num_ma-h[i]
        num_ma = h[i]
        flag = False

ans += h[n-1]
print(ans)

Solution (Python):
n = int(input())
hs = [int(i) for i in input().split()]
flag = -1
count = 0

while max(hs) > 0:
    flag = -1
    for i in range(n):
        if hs[i] > 0:
            if flag == 1:
                hs[i] -= 1
            else:
                count += 1
                flag = 1
                hs[i] -= 1
        else:
            if flag == 1: 
                flag = -1
print(count)

Solution (Python):
n = int(input())
h = [0] + list(map(int, input().split()))
ans = 0
for i in range(1, 101):
    b = 0
    for j in range(n):
        if h[j] < i and h[j + 1] >= i: b += 1
    ans += b
print(ans)

Solution (Python):
n = int(input())
h = [0] + list(map(int, input().split())) + [0]
ans = 0
mn, mx = 1000, -1
cnt = 1
for i in range(n + 1):
    if h[i] <= h[i + 1]:
        mn = min(mn, h[i])
        mx = max(mx, h[i + 1])
        cnt += 1
    else: 
        if cnt > 1:
            ans += mx - mn
            mn, mx = 1000, -1
            cnt = 1
print(ans)

Solution (Python):

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

for i in range(n):
    s.add(h[i])
s = list(s)
s.append(0)
s.sort()

m = len(s)
    
flag = [0] * (n+1)
flag[0] = 1
ans = 0

for i in range(m-1):
    a = 0
    b = s[i+1]-s[i]
    
    for j in range(n):
        if flag[j+1] == 0 and flag[j] == 1:
            a += 1
    
    for j in range(n):
        if h[j] == s[i+1]:
            flag[j+1] = 1
    ans += (a * b)
    
print(ans)

Solution (Python):
n = int(input())
h = [int(x) for x in input().split()]
i = 0
ans = 0

while sum(h):
    while True:
        if h[i] == 0 and i < n:
            i += 1
        else:
            break

    while True:
        if h[i] != 0:
            h[i] -= 1
            i += 1
        if i >= n or h[i] == 0:
            i = 0
            ans += 1
            break

print(ans)

Solution (Python):
n, *H = map(int, open(0).read().split())

inc = True
peaks = []
for h0, h1 in zip(H, H[1:]):
    if inc:
        if h0 > h1:
            peaks.append(h0)
            inc = False
    else:
        if h0 < h1:
            peaks.append(-h0)
            inc = True
if inc:
    peaks.append(H[-1])
print(sum(peaks))

Solution (Python):
n, *H = map(int, open(0).read().split())

inc = True
maxes, mins = [], []
for h0, h1 in zip(H, H[1:]):
    if inc:
        if h0 > h1:
            maxes.append(h0)
            inc = False
    else:
        if h0 < h1:
            mins.append(h0)
            inc = True
if inc:
    maxes.append(H[-1])
print(sum(maxes)-sum(mins))

Solution (Python):
import numpy as np
N = input()
h = input().split()
h = np.array(h).astype(int)
arr = np.zeros(len(h))

k = 0
def water(arr,h):
    
    if len(arr) == 0:
        return
    
    
    global k
    k = k + 1
    arr = arr + 1
    
    
    ind = []
    i = 0
    for ea,eh in zip(arr,h):
        if ea == eh:
            ind.append(i)
        i = i + 1
    
    
    
    index = 0
    for target in ind:
        water(arr[index:target],h[index:target])
        index = target+1
    water(arr[index:],h[index:])


ind = []
i = 0
for ea,eh in zip(arr,h):
    if ea == eh:
        ind.append(i)
    i = i + 1



index = 0
for target in ind:
    water(arr[index:target],h[index:target])
    index = target+1
water(arr[index:],h[index:])
print(k)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
arr=[x for x in h]
cnt=0

while 1:
  if all([x==0 for x in h]):
    break
  for i in range(n):
    if h[i]!=0:
      s=i
      break
  e=s
  for i in range(s,n-1):
    if h[s]<=h[i+1]:
      e+=1
    else:
      break
  h=[h[i]-1 if s<=i<=e else h[i] for i in range(n)]
  cnt+=1
print(cnt)

Solution (Python):
N = int(input())
H = list(map(int, input().split()))

ans = 0

def solve(arr):
  global ans
  min_h = min(arr)
  ans += min_h
  new_arr = [x - min_h for x in arr]
  sub_arr = []
  for x in new_arr:
    if x == 0:
      if len(sub_arr) > 0:
        solve(sub_arr)
        sub_arr = []
    else:
      sub_arr.append(x)
  else:
    if len(sub_arr) > 0:
      solve(sub_arr)

solve(H)
print(ans)

Solution (Python):
import numpy as np
N = int(input())
h = np.array(list(map(int, input().split())))


cnt = 0
while True:
    if all(h==0):
        break

    i = 0
    while i < N:
        if h[i] == 0:
            i += 1
        else:
            cnt += 1
            while i < N and h[i] > 0:
                
                h[i] -= 1
                i += 1
print(cnt)

Solution (Python):
N = int(input())
h = [int(x) for x in input().split()]

ans = 0
while max(h) :
    flag = False
    for i in range(N) :
        if flag == False :
            if h[i] != 0 :
                h[i] -= 1
                flag = True
        else :
            if h[i] != 0 :
                h[i] -= 1
            else :
                break
    ans += 1

print(ans)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))


def dfs(left, right):
    if left >= right:
        return 0
    idx = left + h[left: right].index(min(h[left: right]))
    x = h[idx]
    for i in range(left, right):
        h[i] -= x
    return dfs(left, idx) + x + dfs(idx+1, right)


print(dfs(0, n))

Solution (Python):
n = int(input())
h = list(map(int, input().split()))

ans = 0
while sum(h) > 0:
    prev = 0
    split = 0
    for v in h:
        if prev == 0 and v != 0:
            split += 1
        prev = v

    ans += split
    h = [v-1 if v > 0 else 0 for v in h]

print(ans)

Solution (Python):
N = int(input())
H = list(map(int, input().split()))

cnt = 0
before = 0

for i in range(N):
    if before < H[i]:
        cnt += H[i] - before
    before = H[i]

print(cnt)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))

ans = 0
prev = 0
for v in h:
    if prev < v:
        ans += v - prev
    prev = v

print(ans)

Solution (Python):
n=int(input())
hh=list(map(int,input().split()))
ans=0
while 1:
    l=0
    while l<n and hh[l]==0:l+=1
    if l==n:break
    r=l
    while r<n and hh[r]>0:r+=1
    mn=min(hh[l:r])
    for i in range(l,r):hh[i]-=mn
    ans+=mn
print(ans)

Solution (Python):
def main():
  import sys
  input = sys.stdin.readline
  n=int(input())
  h=list(map(int,input().split()))
  m=min(h)
  h.append(0)
  ans=m
  for i in range(0,n):
    if h[i]<=h[i+1]:
      continue
    else:
      ans+=h[i]-m
      m=h[i+1]
  print(ans)
if __name__=='__main__':
  main()

Solution (Python):
n=int(input())

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

counter=0
maa=100

for i in range(maa):
    hight = i+1
    flag = False
    for h in hlist:
        if not flag:
            if h>= hight:
                counter+=1
                flag = True
        if flag:
            if h < hight:
                flag = False

print(counter)

Solution (Python):
n = int(input())
a = [int(e) for e in input().split()]
mat = [[a[j]>i  for j in range(n)] for i in range(max(a))]
sliced_mat = [[a for a,b in zip(row,row[1:]+[False]) if a and a!=b]for row in mat]
print(sum([len(row)for row in sliced_mat]))

Solution (Python):
n = int(input())
a = [int(e) for e in input().split()]
tmp_mat = [[a[j]>i  for j in range(n)] for i in range(max(a))]
mat = [[a for a,b in zip(row,row[1:]+[False]) if a and a!=b]for row in tmp_mat]
print(sum([len(row)for row in mat]))

Solution (Python):
def calc(a,n):
    array = [[a[j]>i  for j in range(n)] for i in range(max(a))]
    mat = [[a for a,b in zip(row,row[1:]+[False]) if a and a!=b]for row in array]
    return sum([len(row)for row in mat])
n = int(input())
a = [int(e) for e in input().split()]

print(calc(a,n))

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
c=0
l=[0]*n
while l!=h:
  if l[0]!=h[0]:
    c+=1
    for i in range(1,n-1):
      if l[i]==h[i] and l[i+1]!=h[i+1]:
        c+=1
  else:
    for i in range(n-1):
      if  l[i]==h[i] and l[i+1]!=h[i+1]:
        c+=1
  for i in range(n):
    if l[i]!=h[i]:
      l[i]+=1    
print(c)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
def dfs(l,r):
  if l==r:
    return h[l]
  if r<l:
    return 0
  res=10**18
  for i in range(l,r+1):
    res=min(res,h[i])
  for i in range(l,r+1):
    h[i]-=res
  for i in range(l,r+1):
    if h[i]==0:
      return dfs(l,i-1)+dfs(i+1,r)+res
  return 0
print(dfs(0,n-1))

Solution (Python):
n = int(input())
h = [int(i) for i in input().split()]
r = i = 0
while sum(h) != 0:
    if h[i] == 0:
        i += 1
    else:
        r += 1
        while i < n and h[i] > 0:
            h[i] -= 1
            i += 1
        i = 0
print(r)

Solution (Python):
n = int(input())
h = [int(i) for i in input().split()]
r = i = 0
while max(h) != 0:
    if h[i] == 0:
        i += 1
    else:
        r += 1
        while i < n and h[i] > 0:
            h[i] -= 1
            i += 1
        i = 0
print(r)

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
ans = h[0]
for i in range(N-1):
  if h[i] < h[i+1]:
    ans += h[i+1] - h[i]
print(ans)

Solution (Python):
n,*h=map(int,open(0).read().split());h+=[0];print(sum(max(h[i]-h[i+1],0)for i in range(n)))

Solution (Python):
n,*h=map(int,open(0).read().split());print(sum(max(a-b,0)for a,b in zip(h,h[1:]+[0])))

Solution (Python):
n,*h=map(int,open(0).read().split())
a=h[0]
for i in range(1,n):
    if h[i-1]<h[i]:
        a+=h[i]-h[i-1]
print(a)

Solution (Python):
N=int(input())
h=[int(i) for i in input().split()]+[0]
X=[max(h[i]-h[i+1],0) for i in range(N)]

print(sum(X))

Solution (Python):
N=int(input())
h=list(map(int,input().split()))
min=h[0]
piv=0
l=0
r=0
i=0
cnt=0
while sum(h)>0:
    while (h[l]==0)and(l<=N-1):
        l=l+1
    if l==N:
        break
    else:
        min=h[l]
    for i in range(l,N):
        if h[i]<min:
            if h[i]!=0:
                min=h[i]
            else:
                piv=piv-1
                break
    piv=piv+i
    
    for i in range(l,piv+1):
        h[i]=h[i]-min
    cnt=cnt+min
    l=0
    piv=0
    
    
print(cnt)

Solution (Python):
n = int(input())
high = list(map(int, input().split()))
cnt = 0
before_h = 0
for i in range(n):
    cnt += max(high[i]-before_h,0)
    before_h = high[i]
print(cnt)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))

def solve(l):
    if l == []:
        return 0
    else:
        m = min(l)
        ans = m
        l2 = []
        for i in range(len(l)):
            l2.append(l[i] - m)
        p = l2.index(0)
        return m + solve(l2[:p]) + solve(l2[p + 1:])

print(solve(h))

Solution (Python):

from collections import Counter, deque







import sys



ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    h = list(map(int,ipti().split()))
    h.append(0)

    cnt = 0

    que = deque()
    que.append((0, n, 0))

    while que:
        left, right, height = que.pop()
        flag = False

        for i in range(left, right+1):
            if not flag and h[i] > height:
                nl = i
                flag = True

            if flag and h[i] <= height:
                nr = i
                que.append((nl, nr, height + 1))
                cnt += 1
                flag = False

    print(cnt)



if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
q = deque()

n = int(input())
h = list(map(int, input().split()))
for i in h:
    q.append(i)
q.append(0)

ans = 1
if sum(q) == 0:
    ans = 0
c = 0
while sum(q) != 0:
    p = q.popleft()
    if p != 0:
        q.append(p - 1)
        c = 1
    else:
        q.append(0)
        if c == 1:
            ans += 1
            c = 0
print(ans)

Solution (Python):
N=int(input())
h=list(map(int,input().split()))
ans=0
flag=True
f=-1
e=N-1

for i in range(N):
    if h[i]>0:
        f=0
if f<0:
    print(0)
    exit()

while(flag):
    F=False
    E=False
    for i in range(N):
        if not F:
            if h[i]>0:
                f=i
                F=True
    for i in range(f,N):
        if not E:
            if h[i]==0:
                e=i-1
                E=True

    
    
    if f>e:
        e=N-1
    for i in range(f,e+1):
        h[i]-=1
    ans+=1
    
    flag=False
    for i in range(N):
        if h[i]>0:
            flag=True
print(ans)

Solution (Python):
import sys
from math import *
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    n = int(input())
    Hs = list(map(int, input().split()))
    i = 0
    cnt = 0
    island = 0
    while True:
        if i >= n:
            i = 0
        if sum(Hs) == 0:
            break
        if Hs[i] == 0:
            i += 1
            continue
        else:
            cnt += 1
            while (i < n and Hs[i] > 0):
                Hs[i] -= 1
                i += 1

    print(cnt)
    return

if __name__ == '__main__':
    main()

Solution (Python):





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

ans = 0
while True:
    
    l,r = -1,-1

    for i in range(N):
        if H[i] != 0:
            l = i
            break

    if l == -1:
        break

    for i in range(l,N):
        if H[i] == 0:
            r = i-1
            break

        if i == N-1: r = N-1


    tmp =  min(H[l:r+1])
    for i in range(l,r+1):
        H[i] -= tmp
    ans += tmp

print(ans)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
f = [0] * N

ans = 0
mh = max(h)
for i in range(1, mh+1):
    f = False
    for j in range(N):
        if i <= h[j]:
            if f is True:
                continue
            else:
                f = True
                ans += 1
        else:
            f = False

print(ans)

Solution (Python):
n = int(input())
hs = [int(x) for x in input().split()]

ans = 0
mh = max(hs)
for cur in range(1, mh + 1):
    c = False
    for h in hs:
        if cur <= h:
            if c:
                continue
            else:
                c = True
                ans += 1
        else:
            c = False
print(ans)

Solution (Python):
n = int(input())
s = list(map(int, input().split()))
cost = s[0]
for i in range(1, n):
    if s[i - 1] < s[i]:
        cost += s[i] - s[i - 1]
print(cost)

Solution (Python):
n = int(input())
h = [int(x) for x in input().split()]

cost = h[0]

for i in range(1, n):
    if h[i - 1] < h[i]:
        cost += h[i] - h[i - 1]

print(cost)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))

def minus(l,x):
    for i in range(len(l)):
        l[i] = l[i] - x
    

count = 0
count += min(h)
minus(h, min(h))

for i in range(N-1):
    if h[i] < h[i+1]:
        count += h[i+1] - h[i]
        
count += h[0]
print(count)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))

def minus(l,x):
    for i in range(len(l)):
        l[i] = l[i] - x
    

count = 0
count += min(h)
minus(h, min(h))


for i in range(N-1):
    if h[i] > h[i+1]:
        count -= h[i+1]-h[i]
        
count += h[-1]
print(count)

Solution (Python):
def main():
    
    
    
    
    
    
    
    
    
    
    
    
    
    n = int(input())
    a = list(map(int, input().split()))
    res = a[0]
    for i in range(1,n):
        res += max(0, a[i] - a[i-1])
    print(res)

if __name__ == '__main__':
    main()