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):
import numpy as np

n = int(input())
h = np.array([0] + [int(item) for item in input().split()] + [0])

count = 0
while np.count_nonzero(h) > 0:
  zid = np.where(h==0)[0]
  for item in range(len(zid)-1):
    if zid[item+1] - zid[item] > 1:
      h[zid[item]+1:zid[item+1]] -= 1
      count += 1
print(count)

Solution (Python):
from itertools import*
from numpy import*
n,*a=map(int,open(0).read().split())
if set(a)=={0}:
    print(0)
    exit()
a=array(a)
count=0
ans=0
while 1:
    count=min(a[a!=0])
    ans+=count*sum(k for k,v in groupby(a>0)if k==True)
    a=a-min(a[a!=0])
    a[a<0]=0
    if sum(a)==0:
        break
print(ans)

Solution (Python):
from itertools import*
from numpy import*
n,*a=map(int,open(0).read().split())
a=array(a)
count=0
ans=0
while 1:
    if sum(a)==0:break
    count=min(a[a!=0])
    ans+=count*sum(k for k,v in groupby(a>0)if k==True)
    a=a-min(a[a!=0])
    a[a<0]=0
print(ans)

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

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

count = 0

for h in sorted(range(1, 101), reverse=True):
    now = 0
    for h_2 in li_h:
        if h_2 >= h and now < h:
            count += 1
        now = h_2

print(count)

Solution (Python):
N=int(input())
h=list(map(int,input().split()))
H=[0]*N
c=0
while H!=h:
    l=0
    r=N
    for i in range(N):
        if H[i]!=h[i]:
            l=i
            break
    for i in range(N):
        if H[i]==h[i]:
            r=i
            if r>l:
                break
            else:
                r=N
    for i in range(l,r):
        H[i]+=1
    c+=1
print(c)

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
hmax = max(h)
count = 0
while hmax > 0:
    maxindex = [i for i,x in enumerate(h) if x == hmax]
    continueNumberCount = 0
    for i in range(len(maxindex)-1):
        if maxindex[i] == maxindex[i+1] - 1:
            continueNumberCount += 1
    count += len(maxindex) - continueNumberCount
    for i in maxindex:
        h[i] -= 1
    hmax -= 1
print(count)

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

count = 0
while True:
    flg_s = False
    for i in range(n):
        if flg_s and h[i] == 0:
            break
        if h[i] > 0:
            flg_s = True
            h[i] -= 1

    if not flg_s:
        break
    count += 1

print(count)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
ans = 0
while sum(h) != 0:
    for i in range(N):
        if(h[i]!=0):
            l = i
            r = N
            break
    for j in range(i+1, N):
        if(h[j]==0):
            r = j
            break

    x = min(h[l:r])
    for i in range(l, r):
        h[i] -= x
    ans += x
print(ans)

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

Solution (Python):
import re

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

def particles(ht):
	global N
	global H
	s = ''
	for i in range(N):
		if H[i] >= ht:
			s += '
		else:
			s += '.'
	return s

result = 0

for j in range(100):
	if particles(j+1).count('
		break
	else:
		result += len(re.compile(r'

print(result)

Solution (Python):
a=int(input())
A=list(map(int,input().split()))+[0]
s=0
m=0
for i in A:
  s+=abs(m-i)
  m=i
print(s//2)

Solution (Python):
a=int(input())
A=list(map(int,input().split()))+[0]
s=0
m=0
for i in A:
  s+=abs(m-i)
  m=i
print(s//2)

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

def water(H):
	if H == []:
		return 0
	
	minH = min(H)
	H_temp = [x - minH for x in H]
	index0 = H_temp.index(0)
	return water(H_temp[0:index0]) + water(H_temp[index0+1:]) + minH

print(water(H))

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
def solve(ls):
    if ls == []:
        return 0
    m = min(ls)
    tmp = [x - m for x in ls]
    i = tmp.index(0) 
    return solve(tmp[0:i]) + solve(tmp[i+1:]) + m
print(solve(h))

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

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
hm=max(h)
s=['']*hm
for k in range(1,hm+1):
  for i in range(n):
    if h[i]>=k:
      s[k-1]+='
    else:
      s[k-1]+=' '


ans=0
for k in range(hm):
  ans+=len(list(s[k].split()))
print(ans)

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

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

su = 0

co = 0

for i in range(max(a)):
    for j in range(n):
        if a[j] > 0 and co == 0:
            co += 1
            su += 1
        if co  >=  1:
            if a[j] == 0:
                co = 0
            else:
                a[j] -= 1
    co = 0

print(su)

Solution (Python):
n = int(input())
H = list(map(int,input().split()))
ans = 0
lst = 0
for h in H:
    if(h > lst):
        ans += h-lst
    lst = h

print(ans)

Solution (Python):
n = int(input())
H = list(map(int,input().split()))
ans = 0
size = sum(H)
while(size > 0):
    count = 0
    temp = 0
    for h in H:
        if(h == 0 and temp > 0):
            ans += 1
            break
        elif(h != 0):
            temp += 1
            H[count] -= 1
            size -= 1
            if(count == n-1):
                ans += 1
        count += 1
print(ans)

Solution (Python):
def minplus(array , count):
    if len(array) == 0:
        return count
    if len(array) == 1:
        return array[0] + count
    count += min(array)
    temparray = [y - min(array) for y in array]
    zeroindex = temparray.index(0)
    return minplus(temparray[:zeroindex],count) + minplus(temparray[zeroindex + 1:],0)

N =  int(input())
hn = [int(x) for x in input().split() ]

print(minplus(hn,0))

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

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

    ans = grand_garden(N, H)
    print(ans)

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

count = 0
while True:
    left = -1
    right = -1
    for i, hh in enumerate(h):
        if hh > 0:
            if left < 0:
                left = i
            right = i
        elif right >= 0:
            break
    if left < 0:
        break
    count += 1
    for i in range(left, right + 1):
        h[i] -= 1
print(count)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
res = 0
for i in range(max(h)):
    f = False
    for j in range(n):
        if h[j] >= 1:
            h[j] -= 1
            f = True
        elif f and h[j] == 0:
            f = False
            res += 1
    if f:
        res += 1
print(res)

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

ans = 0

while max(h):
    mx = max(h)
    on = False

    for i in range(N):
        if h[i] == mx:
            h[i] -= 1
            if not on:
                ans += 1
            on = True
        else:
            on = False

print(ans)

Solution (Python):
def del_double(lst):
    bef = -1
    ret =[]
    for i in lst:
        if bef != i:
            ret.append(i)
            bef=i
    return ret

def f(lst):
    m_ = max(lst)
    idx = lst.index(m_)
    l_idx = idx-1
    if l_idx >=0:
        l = lst[l_idx]
    else:
        l = -1
    
    r_idx = idx+1
    if r_idx < len(lst):
        r = lst[r_idx]
    else:
        r =-1
        
    
    
    if r==-1 and l ==-1:
        return m_ 
    else:
        if r>l:
            lst[idx] = r
            
            return m_ - r + f(del_double(lst))
        else:
            lst[idx] = l
            
            return m_ - l + f(del_double(lst))
N = int(input())  
H = list(map(int,input().split()))

print (f(del_double(H)))

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

ans = 0
height = 1
while(True):
    endFlag = True
    waterFlag = False
    for h in hList:
        if h >= height:
            if not waterFlag:
                ans += 1
            endFlag = False
            waterFlag = True
        elif h < height:
            waterFlag = False
    height += 1
    if endFlag:
        break

print(ans)

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

def n_water(p_arr):
    if len(p_arr) == 0:
        return 0
    if 0 in p_arr:
        i_0 = p_arr.index(0)
        return n_water(p_arr[:i_0])+n_water(p_arr[i_0+1:])
    p_arr = [x-1 for x in p_arr]
    return n_water(p_arr)+1

print(n_water(h_arr))

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
count = 0
for i in range(1, 101):
    flag = False
    for j in range(N):
        if h[j] >= i and flag == False:
            flag = True
            count += 1
        elif h[j] < i and flag == True:
            flag = False
print(count)

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

def rec(H,acc,l,r):
    if r - l == 1:
        return (H[l] - acc) 
    mn = min(H[l:r])
    mx = max(H[l:r])
    if mn == mx:
        return mn - acc
    flg = False
    ans = (mn - acc)
    L = 0
    for i in range(l,r):
        if H[i] != mn:
            if not flg:
                L = i
                flg = True

            if i == r-1:
                ans += rec(H,mn,L,r)
        else:
            if flg:
                ans += rec(H,mn,L,i)
            flg = False
    
    return ans
print(rec(h,0,0,N))

Solution (Python):
def times(hs):
    if len(hs) == 0:
        return 0
    if len(hs) == 1:
        return hs[0]

    minh = min(hs)
    mini = hs.index(minh)

    hs = [h - minh for h in hs]

    return minh + times(hs[:mini]) + times(hs[mini+1:])

_ = input()
hs = list(map(int, input().split()))

print(times(hs))

Solution (Python):
n=int(input())
H=list(map(int,input().split()))
ans=0
i=0
while sum(H)>0:
  ans+=1
  flag=False
  for i in range(n):
    if H[i]>0:
      H[i]-=1
      flag=True
    elif flag is True and H[i]==0:
      break
  
print(ans)

Solution (Python):
n = int(input())
H = [0]+list(map(int, input().split()))
dp = [0]*(n+1)

dp[0] = 0
for i in range(1,n+1):
  if H[i] > H[i-1]:
    dp[i] = dp[i-1] + (H[i] - H[i-1])
  else:
    dp[i] = dp[i-1]
print(dp[n])

Solution (Python):
import numpy as np
N = int(input())
hs = input().split()
hs = np.array([int(h) for h in hs])



def split_array(hs):
    split_indexes = np.where(hs==0)[0]
    n = len(split_indexes)
    split_list = []
    if n > 0:
        split_list.append(hs[0:split_indexes[0]])
        if n > 1:
            for i in range(n-1):
                split_list.append(hs[split_indexes[i]+1:split_indexes[i+1]])
        split_list.append(hs[split_indexes[-1]+1:])
    return split_list

def make_shower(l):
    count = l.min()
    return count, l-count

all_count = 0
c, hs = make_shower(hs)
split_list = split_array(hs)
all_count += c
zero_count = 0

while True:
    zero_count = 0
    split_list_new = []
    for l in split_list:
        if len(l) == 0:
            zero_count += 1
            continue
        count, l_showered = make_shower(l)
        
        all_count += count
        split_list_ = split_array(l_showered)
        split_list_new.extend(split_list_)

    len_split_list = len(split_list) 
    split_list = [l for l in split_list_new] 
    
    if zero_count == len_split_list:
        break
print(all_count)

Solution (Python):
N = int(input())
h = input()

h_n = [int(x) for x in h.split()]
now = []
for i in range(N):
    now.append(0)

count = 0
while True:
    add_range = [0, N]

    for i in range(0,N):
        if (now[i] < h_n[i]):
            add_range[0] = i
            break
    else:
        break
    for i in range(add_range[0]+1,N):
        if (now[i] >= h_n[i]):
            add_range[1] = i
            break
    for i in range(add_range[0], add_range[1]):
        now[i] += 1
    count += 1
print(count)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
ans = 0
while max(h) > 0:
    k = 0
    flag = False
    for i in range(N):
        if h[i] > 0:
            h[i] -= 1
            flag = True
        elif flag:
            k += 1
            flag = False
    if flag:
        k += 1
    ans += k

print(ans)

Solution (Python):


def solve():
    
    N = int(input())
    flowers = [0]*N
    h = [int(x) for x in input().split(" ")]
    ans = h[0]
    stack = h[0]

    for i in range(1, N):
        if h[i] <= stack:
            stack = h[i]
        else:
            ans += (h[i] - stack)
            stack = h[i]
    
    print(ans)


if __name__ == "__main__":
    solve()

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

max_h = max(H)
memo = [['0'] for _ in range(max_h)]
for i, h in enumerate(H):
    for j, v in enumerate(memo):
        if j+1 > h:
            v += '0'
        else:
            v += '1'


for i, v in enumerate(memo):
    v += '0'


for i, v in enumerate(memo):
    memo[i] = ''.join(v)


ans = 0
for i, v in enumerate(memo):
    ans += v.count('10')


print(ans)

Solution (Python):
import heapq

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

s = 0
Q = []
heapq.heappush(Q, h)
while Q:
	a = heapq.heappop(Q)
	x = min(a)
	s += x
	for i in range(len(a)):
		a[i] -= x
	L = []
	for i in range(len(a)):
		if a[i] == 0:
			if L!= []:
				heapq.heappush(Q, L)
				L = []
		else:
			L.append(a[i])
	if L != []:
		heapq.heappush(Q, L)

print(s)

Solution (Python):
n = int(input()) + 1
l = list(map(int, input().split()))
l.append(0)

count = 0

for i in range(n):
	while(l[i] > 0):
		for j in range(i + 1, n):
			if l[j] == 0:
				break
		for k in range(i, j):
			l[k] -= 1
		count += 1
print(count)

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

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

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

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

Solution (Python):
n = int(input())
listH = list(map(int, input().split()))
listNull=[0]*n
sumW=0
while True:
    if listH == listNull:
            break
    currentL=[]
    index=0
    minM=100
    startI=0
    while index < n:
        if listH[index]!=0:
            if listH[index] < minM:
                minM = listH[index]
            currentL.append(listH[index])
        else:
            if len(currentL)>0:
                index2=startI
                sumW += minM
                while index2 < index:
                    if listH[index2]!=0:
                        listH[index2]-=minM
                    index2+=1
                startI = index+1
                currentL = []
            else:
                pass
        index+=1
    if len(currentL) != 0:
        index2 = startI
        sumW += minM
        while index2 < index:
            if listH[index2] != 0:
                listH[index2] -= minM
            index2 += 1
        startI = index+1
        currentL = []
    else:
        pass

print(sumW)

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

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

def dfs(l):
    if len(l)==1:
        return l[0]
    else:
        a=l.index(min(l))
        if a==0:
            return dfs(l[1:])
        elif a==len(l)-1:
            return dfs(l[:-1])
        else:
            return dfs(l[:a])+dfs(l[a+1:])-min(l)

print(dfs(H))

Solution (Python):
a=int(input())
b=input().split()
p=0
n=0
for i in b:
  i=int(i)
  if i>p:
    n+=(i-p)
  p=i
print(n)

Solution (Python):
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(input())
lst = list(map(int, input().split()))
print(rec(lst, 0, len(lst)))

Solution (Python):
import numpy as np
import sys

def split_(arr, ite):
    new = [arr[:ite[0]]]
    if len(ite)==1:
        if ite[0]==len(arr)-1:
            return new
        else:
            new.append(arr[ite[0]+1:])
            return new
    else:
        for i in range(1,len(ite)):
            new.append(arr[ite[i-1]+1:ite[i]])
        if ite[-1]==len(arr)-1:
            return new
        else:
            new.append(arr[ite[-1]+1:])
            return new

n = int(input())
h = [np.array(list(map(int, input().split())))]

ans = 0
while h:
    if len(h[0])==1:
        ans += h[0][0]
        h.pop(0)
    else:
        m = min(h[0])
        ans += m
        h[0] -= m
        filtered = list(filter(lambda x: h[0][x]==0, range(len(h[0]))))
        splited = split_(h[0], filtered)
        h.pop(0)
        for s in splited:
            if len(s)>0:
                h.append(s)
print(ans)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
v=0
s=0
ans=0
l.append(0)
for i in range(max(l)):
    v=0
    s=0
    while v<len(l):
        while l[v]!=0:
            v+=1
        if l[s]!=0:
            for y in range(v-s):
                l[s+y]-=1
            ans+=1
        s=v+1
        v+=1
print(ans)

Solution (Python):
N = [int(x) for x in input().split()]
H = [int(x) for x in input().split()]
INF = 10**9

ans = 0
while True:
    if sum(H) == 0:
        break
    s, e = 0, len(H)
    for i, h in enumerate(H):
        if h != 0:
            s = i
            break
    for i, h in enumerate(H):
        if i <= s:
            continue
        if h == 0:
            e = i
            break
    H[s:e] = [h-1 for h in H[s:e]]
    ans += 1

print(ans)

Solution (Python):

import copy
import numpy as np
import random

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

count = 0

stack = []
stack.append(h)
while len(stack) != 0:
    group = stack.pop()
    

    count += min(group)
    minus = min(group)
    for i in range(len(group)):
        group[i] -= minus

    begin = 0
    
    for i in range(len(group)):
        if group[i] == 0:
            if len(group[begin:i])!=0:
                stack.append(group[begin:i])
            begin = i+1
        elif i == len(group)-1:
            if len(group[begin:])!=0:
                stack.append(group[begin:])
    
    
print(count)

Solution (Python):
N=int(input())
A=list(map(int, input().split()))
ans=0
INF=1000

def solve(L):
    global ans
    if len(L)==1:
        ans+=L[0]
    else:
        min=INF
        index=-1
        for i in range(len(L)):
            if L[i]<=min:
                index=i
                min=L[i]
        for i in range(len(L)):
            L[i]-=min
        ans+=min

        if 0<index<len(L)-1:
            solve(L[:index])
            solve(L[index+1:])
        elif index==0:
            solve(L[index+1:])
        else:
            solve(L[:index])

solve(A)
print(ans)

Solution (Python):
N=int(input())
h=list(map(int,input().split()))
def calc(h):
        if h==[]:
                return 0
        if 0 not in h:
                return calc(list(map(lambda x:x-1,h)))+1
        if h[0]==0:
                return calc(h[1:])
        elif h[-1]==0:
                return calc(h[:-1])
        for i in range(1,len(h)-1):
                if h[i]==0:
                        return calc(h[:i])+calc(h[i+1:])
print(calc(h))

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

def mizuyari(niwa):
	if niwa == []:
		return 0
	if 0 not in niwa:
		return mizuyari(list(map(lambda x: x - 1, niwa))) + 1
	if niwa[0] == 0:
		return mizuyari(niwa[1:])
	elif niwa[-1] == 0:
		return mizuyari(niwa[:-1])
	for i in range(1, len(niwa) - 1):
		if niwa[i] == 0:
			return mizuyari(niwa[:i]) + mizuyari(niwa[i+1:])

print(mizuyari(h))

Solution (Python):
def main():
    N = int(input())
    h = list(map(int, input().split()))
    
    ans = 0
    while sum(h) > 0:
      ans += 1
      flag = False
      for i in range(N):
        if h[i] > 0:
          h[i] -= 1
          flag = True
        elif flag is True:
          break

    print(ans)
        
    
if __name__ == '__main__':
    main()

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

def th_cnt(arr, th):
    old = 0
    cnt = 0
    for a in arr:
        if old == 0 and th < a:
            cnt += 1
            old = 1
        if a <= th:
            old = 0
    return(cnt)

ret = 0
for i in range(max(arr)):
    ret += th_cnt(arr, i)

print(ret)

Solution (Python):
def water(h):
    if len(h) == 0:
        
        return  0
    
    elif len(h) == 1:
        
        return h[0]
    
    elif R0(h) + 1:
        
        
        return water(h[0: R0(h)]) + water(h[R0(h) + 1: len(h)])
    
    else:
        
        for i in range(len(h)):
            h[i] -= 1
        
        return water(h) + 1

def R0(l):
    if 0 in l:
        return l.index(0)
    else:
        return -1

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

print(water(h))

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

def culc(h_tmp):
  if len(set(h_tmp))==1:
    return h_tmp[0]
  elif len(h_tmp)==0:
    return 0
  else:
    min_h = min(h_tmp)
    i = h_tmp.index(min_h)
    h_tmp = list(map(lambda x:x-min_h,h_tmp))
    return culc(h_tmp[:i]) + culc(h_tmp[i+1:]) + min_h

print(culc(h))

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

count = 0

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

count = count + h[N-1]

print(count)

Solution (Python):
class flowers:
    def __init__(self, str_h):
        self.str = str_h.split()
        self.int = []
        self.min = 0
        self.water = 0
        
        self.update_int()
        self.update_min()
    
    def update_str(self):
        self.str = list(map(str, self.int))
        return

    def update_int(self):
        self.int = list(map(int, self.str))
        return
    
    def update_min(self):
        self.min = min(self.int)
        return
    
    def minimize(self):
        self.int = [n-self.min for n in self.int]
        self.water += self.min
        self.update_str()
        self.min = 0
        return
    
    def str_split0(self):
        idxs = [-1] + [i for i,n in enumerate(self.int) if n==0]
        if len(idxs)==1:
            return [self.str]
        else:
            temp_ls = []
            for i in range(len(idxs)-1):
                s = self.str[idxs[i]+1:idxs[i+1]]
                if len(s)>0: temp_ls.append(s)
            if self.int[-1]!=0:
                temp_ls.append(self.str[idxs[-1]+1:])
        return temp_ls

N = input()
str_h = input()

fls = flowers(str_h)
ls_fls = fls.str_split0()
water = 0

while(len(ls_fls)>0):
    
    temp_ls_fls = []
    for fls in ls_fls:
        if len(fls)<=2:
            water += max([int(s) for s in fls])
            continue
        else:
            _fls = flowers(' '.join(fls))
            _fls.minimize()
            _ls_fls = _fls.str_split0()
            water += _fls.water
            temp_ls_fls += _ls_fls
            flg=1
    ls_fls = temp_ls_fls
print(water)

Solution (Python):
from sys import stdin
N=int(stdin.readline().rstrip())
data=[int(x) for x in stdin.readline().rstrip().split()]

data.append(0)
tmp=0
ans=0
for i in data:
    if i < tmp:
        ans+=(tmp-i)
        tmp=i
    else:
        tmp=i
print(ans)

Solution (Python):

k=int(input())
a=list(map(int,input().split()))
a=[0]+a+[0]
count=0

while sum(a)!=0:
    b=[]

    m=-10000
    for i in a:
        if i!=m:
            b.append(i)
            m=i




    for i in range(1,len(b)-1):    
        if b[i]>b[i-1] and b[i]>b[i+1] and b[i]!=0:
            count+=b[i]-max(b[i-1],b[i+1])
            b[i]=max(b[i-1],b[i+1])
            
    a=b.copy()
        
print(count)

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

ans = H[0]
for i in range(N-1):
    dif = H[i+1] - H[i]
    if dif > 0:
        ans += dif

print(ans)

Solution (Python):
from collections import deque


def main():
    N = int(input())
    h = list(map(int, input().split()))
    q = deque()
    q.append(h)
    ans = 0
    while len(q):
        t = q.popleft()
        if len(t) == 0:
            continue
        minimum = min(t)
        ans += minimum
        l = 0
        for i in range(len(t)):
            if t[i] == minimum:
                q.append([x-minimum for x in t[l:i]])
                l = i+1
        q.append([x-minimum for x in t[l:len(t)]])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
count = 0
previousNum = 0
allNumberIsZero = False
while not allNumberIsZero:
    nonZeroExists = False
    for i in range(N):
        if h[i] > 0: nonZeroExists = True
        if previousNum == 0:
            if h[i] > 0:
                count += 1
                previousNum = h[i]
                h[i] -= 1
            else:
                previousNum = 0
        else:
            if h[i] > 0:
                previousNum = h[i]
                h[i] -= 1
            else:
                previousNum = 0
    previousNum = 0
    if not nonZeroExists: allNumberIsZero=True
print(count)

Solution (Python):
mizuyari=0
n=int(input())
s=list(map(int,input().split()))
def hiku(n):
    return n-1
for i in range(100):
    if s[0]>0:
        mizuyari+=1
    for j in range(n-1):
        if s[j]<=0 and s[j+1]>0:
            mizuyari+=1
    s=list(map(hiku,s))
print(mizuyari)

Solution (Python):

n = int(input())
h_ent = list(map(int, input().split()))

count = 0

while True:
    if len(h_ent)  == 0:
        break
    elif h_ent[0] == 0:
        del h_ent[0]   
    elif not 0 in h_ent:
        h_ent = list(map(lambda x:x-1, h_ent))
        count += 1
    else:
        for i in range(0, h_ent.index(0)):
            h_ent[i] -= 1
        count += 1

def out(x):
    print(x)

out(count)

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

counter = 0
while sum(h) != 0:
  flag = 0
  for i in range(N):
    if h[i] != 0:
      h[i] -= 1
      flag += 1
    elif h[i] == 0 and flag !=0:
      counter += 1
      flag = 0
  if flag != 0:
    counter+=1
            
print(counter)

Solution (Python):
n = int(input())
h = input().split()
h = [int(i) for i in h]
sum,sub = 0,0
for i in range(1,n):
	if h[i-1] > h[i]:
		sum += h[i-1] - sub
		sub = h[i]
if h[len(h)-2] <= h[len(h)-1]:
	sum += h[len(h)-1] - sub
print(sum)

Solution (Python):
input()
flowers = list(map(int,input().split()))
def mizuyari(*f):
  num = 0
  if f == ():
    return 0
  f_min = min(f)
  f_min_index = f.index(f_min)
  num += f_min
  f = [i-f_min for i in f]
  num += mizuyari(*f[:f_min_index])
  num += mizuyari(*f[f_min_index+1:])
  return num

ans = mizuyari(0, *flowers)
print(ans)

Solution (Python):
def solve(string):
    n, *h = map(int, string.split())
    return str(h[0] + sum([max(0, h1 - h0) for h0, h1 in zip(h[0:], h[1:])]))


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Solution (Python):
def solve(string):
    n, *h = map(int, string.split())
    count = h[0]
    for h0, h1 in zip(h[0:], h[1:]):
        count += max(0, h1 - h0)
    return str(count)


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Solution (Python):
def solve(string):
    n, *h = map(int, string.split())
    count = 0
    current = 0
    for _h in h:
        count += max(0, _h - current)
        current = _h
    return str(count)


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

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

M = [H[:]]

a = 0
while M!=[]:
	m = M.pop(0)
	a += min(m)
	s = list(map(lambda x:x-min(m), m))

	start = 0
	for i in range(len(s)):
		if s[i]==0:
			t = s[start:i]
			start = i+1
			if t!=[]: M.append(t)

	t = s[start:]
	if t!=[]: M.append(t)

print(a)

Solution (Python):
N = int(input())
h = [0]+[int(x) for x in input().split()]
print(sum([x-y for x,y in zip(h[1:],h[:-1]) if (x-y)>0]))

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

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

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

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

Solution (Python):
n = int(input())
hs = list(map(int, input().split()))
hs.append(0)

ans = 0
ok = False
while not ok:
    ok = True
    for l in range(n):
        if hs[l] > 0:
            for r in range(l, n + 1):
                if hs[r] == 0:
                    ans += 1
                    ok = False
                    break
                else:
                    hs[r] -= 1
            break

print(ans)

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

print(count)

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

cnt = 0
for _ in range(100):
    flag = 1
    for i in range(n):
        if A[i] != 0:
            A[i] -= 1
            if flag == 1:
                cnt += 1
                flag = 0
        else:
            flag = 1

print(cnt)