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):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


def A():
    s = S()
    c = s.count("0")
    print(2*min(c,len(s)-c))
    return


def B():
    n = I()
    h = LI()
    ans = 0
    s = deque()
    k = []
    for i in range(n):
        if h[i] == 0:
            if k:s.append(k)
            k = []
        else:k.append(h[i])
    if k:s.append(k)
    while s:
        k = s.popleft()
        if k == []:continue
        m = min(k)
        ans += m
        for i in range(len(k)):
            k[i] -= m
        l = []
        for i in range(len(k)):
            if k[i] == 0:
                if l:s.append(l)
                l = []
            else:l.append(k[i])
        if l:s.append(l)
    print(ans)
    return


def C():
    n,k = LI()
    x = LI()
    ans = float("inf")
    j = bisect.bisect_left(x,0)
    if n == 1:
        print(abs(x[0]))
        quit()
    if j < n:
        if x[j] == 0:
            k -= 1
            x.pop(j)
            n -= 1
    for i in range(n):
        if i < j:
            if abs(i-j) > k:continue
            elif abs(i-j) == k:ans = min(ans,abs(x[i]))
            elif k+i-1 < n:
                ans = min(ans,abs(x[i])+x[k+i-1]-x[i])
        else:
            if abs(i-j)+1 > k:continue
            elif abs(i-j)+1 == k:ans = min(ans,x[i])
            elif i-k+1 >= 0:
                ans = min(ans,abs(x[i])+x[i]-x[i-k+1])

    print(ans)
    return


def D():
    n = I()
    a = LI()
    ans = 0
    p = [1<<i for i in range(100)]
    for i in range(n):
        l = 0
        r = 100
        while r-l > 1:
            m = (l+r)//2
            if a[i]%p[m] == 0:
                l = m
            else:
                r = m
        ans += l
    print(ans)
    return


def E():
    s = LI()
    s.sort()
    ans = s[2]-s[1]
    s[0] += s[2]-s[1]
    s[1] = s[2]
    if s[0]%2 != s[1]%2:
        ans += (s[1]+1-s[0])//2+1
    else:
        ans += (s[1]-s[0])//2
    print(ans)
    return


def F():
    n = I()
    s = SR(n)
    for i in range(n):
        d = defaultdict(int)
        for j in range(len(s[i])):
            d[s[i][j]] += 1
            s[i][j] += str(d[s[i][j]])
    ans = set(s[0])
    for i in range(1,n):
        ans = ans&set(s[i])
    ans = list(ans)
    ans.sort()
    for i in ans:
        print(i[0],end = "")
    print()
    return


def G():
    s = S()
    ans = 0
    for i in range(len(s)-1):
        if s[i] != s[i+1]:ans += 1
    print(ans)
    return


def H():
    n,q = LI()
    ans = [0 for i in range(n+1)]
    for i in range(q):
        l,r = LI()
        ans[l-1] ^= 1
        ans[r] ^= 1
    for i in range(n):
        ans[i+1] ^= ans[i]
        print(ans[i],end = "")
    print()
    return


def I_():
    n = I()
    a = LI()
    ans = 0
    r = 0
    for l in range(n):
        while r < n-1 and a[r+1] > a[r]:
            r += 1
        ans += r-l+1
        if l==r:r += 1
    print(ans)
    return


def J():
    n = I()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n-1):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i+1]-h[i]))
        if i < n-2:
            dp[i+2] =  min(dp[i+2],dp[i]+abs(h[i+2]-h[i]))
    print(dp[n-1])
    return


if __name__ == "__main__":
    B()

Solution (Python):
n = int(input())
h_li = list(map(int, input().split()))
count = 0
for i in range(100):
    f = 0
    for j in range(n):
        if h_li[j] >= i + 1:
            if f == 0:
                count += 1
                f = 1
        else:
            f = 0
print(count)

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

count = 0
for h in range(1, 101):
    found = False
    for i in range(N):
        if hs[i] >= h:
            if not found:
                count += 1
                found = True
        elif found:
            found = False
print(count)

Solution (Python):
N = int(input())
h = [int(i) for i in input().split()]
hs = sorted(h)
hs.reverse()
c = 0
for i in range(len(hs)-1):
  nh = []
  a = 0
  for j in range(N-1):
    if not h[j]==h[j+1]:
      nh.append(h[j])
    if h[j]==hs[i]:
      h[j] = hs[i+1]
  nh.append(h[N-1])
  if h[N-1]==hs[i]:
    h[N-1] = hs[i+1]
  c += (hs[i]-hs[i+1])*nh.count(hs[i])
c += hs[-1]
print(c)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
l.append(0)
height = 1
counter = 0
while(height<=max(l)):
    for i in range(n+1):
        if height <= l[i-1] and l[i] < height:
            counter += 1
    height += 1
print(counter)

Solution (Python):
n = int(input())
h_list = list(map(int, input().split()))
ans = 0
for i in range(max(h_list)):
    count = 0
    b_list = [False]
    for j in range(n):
        if h_list[j] >= i+1 and b_list[-1] == False:
            b_list.append(True)
        elif h_list[j] < i+1 and b_list[-1] == True:
            b_list.append(False)


    ans += sum(b_list)
print(ans)

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

Solution (Python):

import sys


def solve(n: int, h: "List[int]"):
    ret = 0
    while True:
        tmp = 0
        found = False
        for i in range(n):
            if h[i] > 0:
                h[i] -= 1
                if not found:
                    found = True
                    tmp += 1
            else:
                found = False
        if tmp < 1:
            break
        else:
            ret += tmp
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    h = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, h)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
li_h = list(map(int,input().split()))
li_a = [0]*n 
ans = 0
li_count = [True]*100
while True:
    for i in range(n):
        if li_h[i] == 0:
            li_count[i] = False
    temp = li_count[0]
    if temp == True:
        count = 1
    else:
        count = 0
    for i in range(n):
        if temp != li_count[i]:
            temp = li_count[i]
            if temp == True:
                count += 1
    ans += count

    for i in range(n):
        if li_h[i] != 0:
            li_h[i] -= 1
    if li_h == li_a:
        break

print(ans)

Solution (Python):
def f(n):
    if n%2==0: return n//2
    else: return 3*n+1

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

M=max(H)
tmp=0
ans=0
for h in range(M,0,-1):
    tmp=0
    for i in range(N):
        if H[i]==h:
            if tmp==0: ans+=1
            tmp=1
            H[i]-=1
        elif H[i]!=h:
            tmp=0
print(ans)

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

for dan in range(max(h)):
    
    for i in range(N-1):
        
        if h[i] != 0 and h[i+1] == 0:
            ans += 1
        if h[i] != 0:
            h[i] -= 1
    
    if h[N-1] != 0:
        h[N-1] -= 1
        ans += 1
print(ans)

Solution (Python):
def get_number(h):
    if len(h) == 0:
        return 0

    ans = 0
    a = min(h)
    h2 = list(map(lambda x: x - a, h))

    ans = ans + a
    l = []
    for i in h2:
        if i == 0:
            ans = ans + get_number(l)
            l = []
        else:
            l.append(i)
    if len(l) > 0:
        ans = ans + get_number(l)

    return ans

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

print(get_number(h))

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.append(0)
s = 0
ans = 0
if L == [0]*(N+1):
  print(0)
while s <= N-1:
  if L[s] == 0:
    s += 1
    continue
  for i in range(s,N+1):
    if L[i] > 0:
      L[i] -= 1
    else:
      ans += 1
      break
  if L == [0]*(N+1):
    print(ans)
    exit()

Solution (Python):
mizu = 0
def mizuyari(H):
    global mizu
    if len(H) == 0:
        return "finish"
    zeroindex = [-1]
    grow = min(H)
    mizu += grow
    for i in range(len(H)):
        H[i] -= grow
        if H[i] == 0:
            zeroindex.append(i)
    zeroindex.append(len(H))
    for i in range(len(zeroindex) - 1):
        mizuyari(H[zeroindex[i] + 1: zeroindex[i + 1]])

def main():
    global mizu
    N = int(input())
    *H, = map(int, input().split())
    mizuyari(H)
    print(mizu)

main()

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


def f(left, right, base):
    m = min(h[left:right+1])
    result = 0
    le = -1
    ri = -1
    for i in range(left, right+1):
        if h[i] > m:
            if le == -1:
                le = i
            ri = i
        else:
            if le != -1:
                result += f(le, ri, m)
                le, ri = -1, -1

    if le != -1:
        result += f(le, ri, m)
    return result + (m - base)


print(f(0, n-1, 0))

Solution (Python):
def argmin(x):
    xx = 1000
    ans = 0
    for i, xi in enumerate(x):
        if xi < xx:
            xx = xi
            ans = i
    return ans

def count_water(x, threshold):
    if len(x) == 1:
        return x[0] - threshold
    i = argmin(x)
    y = x[i] - threshold
    if i > 0:
        y += count_water(x[:i], x[i])
    if i < len(x) - 1:
        y += count_water(x[i + 1:], x[i])
    return y

n = int(input())
h = list(map(int, input().split()))
print(count_water(h, 0))

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
P = []
t = []
for i in range(N - 1):
    if h[i] <= h[i+1]:
        t.append(h[i])
    else:
        t.append(h[i])
        P.append(t)
        t = []
t.append(h[N - 1])
P.append(t)
ans = 0
for j in range(len(P) - 1):
    ans += max(P[j]) - min(P[j+1])
    
ans += max(P[len(P)-1])

print(ans)

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

ans = 0
while sum(H) != 0:
    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)

Solution (Python):
N = int(input())
h = [int(i) for i in input().split()]
ans = 0
i = 0
while True:
    if max(h)==0:
        break
    i = 0
    while i < N:
        if h[i]==0:
            i += 1
        else:
            ans += 1
            j = i
            while j<N and h[j]>0:
                h[j] -= 1
                j += 1
print(ans)

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

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

ans = 0

i = 0

while (True):
    if (max(h) == 0):
        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)

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

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

Solution (Python):
import numpy as np
n=int(input())
l=np.array(list(map(int,input().split())))
c=0
for i in range(n):
  for j in range(n-i):
    if 0 in l[i:n-j]:
      pass
    else:
      c+=min(l[i:n-j])
      l[i:n-j]=l[i:n-j]-np.array([min(l[i:n-j])]*(n-j-i))
print(c)

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

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

print(ans)

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
def count(L):
    if L == []:
        return 0
    if 0 in L:
        i = L.index(0)
        return count(L[:i]) + count(L[i+1:])
    else:
        m = min(L)
        L = list(map(lambda x:x-m,L))
        return m + count(L) 
print(count(L))

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

def count(L):
    if len(L) == 1:
        return L[0]
    if L == []:
        return 0
    if 0 in L:
        i = L.index(0)
        return count(L[:i]) + count(L[i+1:])
    else:
        m = min(L)
        L = list(map(lambda x:x-m,L))
        return m + count(L) 
print(count(L))

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

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

print(ans)

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


def mizuyari(partial_list):
    count = 0
    
    if len(partial_list) == 0:
        
        return 0
    if len(partial_list) == 1:
        
        return partial_list[0]
    
    if min(partial_list) != 0:
        count += min(partial_list)
        
        partial_list = list(map(lambda x: x-min(partial_list), partial_list))
    zero_index = []
    
    for i in range(len(partial_list)):
        if partial_list[i] == 0:
            zero_index.append(i)
    
    bunkatsu = []
    bunkatsu.append(partial_list[0:zero_index[0]])
    for i in range(0, len(zero_index)-1):
        bunkatsu.append(partial_list[zero_index[i]+1:zero_index[i+1]])
    bunkatsu.append(partial_list[zero_index[(len(zero_index)-1)]+1:len(partial_list)])
    
    for i in bunkatsu:
        count += mizuyari(i)
    return count


print(mizuyari(h))

Solution (Python):
hn=int(input())
ht=list(map(int,input().split()))
cnt=0
while 1:
    
    for i in range(hn):
        if ht[i]>0:
            st=i
            break
    else:
        break

    for i in range(st+1,hn):
        if ht[i]<=0:
            ed=i
            break
    else:
        ed=hn

    
    mn=min(ht[st:ed])

    
    for i in range(st,ed):
        ht[i]-=mn
    cnt+=mn

print(cnt)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
count=0
while h.count(0)!=len(h):
  stack=[]
  substack=[]
  for i in range(len(h)):
    if h[i]!=0:
      substack.append(h[i])
    else:
      if len(substack)>0:
        stack.append(substack)
      stack.append([0])
      substack=[]
  if len(substack)>0:
    stack.append(substack)
  h=[]
  for i in range(len(stack)):
    if max(stack[i])==0:
      h+=stack[i]
      continue
    x=min(stack[i])
    count+=x
    for j in range(len(stack[i])):
      stack[i][j]-=x
    h+=stack[i]
print(count)

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

mp = 0

while (sum(h) != sum(d)):
    l = 0
    x = 0
    mo = 0
    for i in range(0,N):
        x = i
        if ((h[i] != d[i]) and mo == 0):
            l = i
            mo = 1
        if (((h[i] == d[i]) and mo == 1)):
            mo = 2
        if ((i==N-1) and (mo ==1)):
            x += 1
            mo = 2
        if (mo == 2):
            for j in range(l, x):
                d[j] += 1
            mp += 1
            break
print(mp)

Solution (Python):
N = int(input())
h = [int(x) for x in input().split()]
h.append(0)
base = [0 for i in range(N+1)]
ans = 0

while h != base:
    ans += 1
    f = h.index(max(h))
    i = 0
    while h[f+i] == h[f+i+1]:
        h[f+i] -= 1
        i += 1
    h[f+i] -= 1

print(ans)

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

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
progress = [0]*n
ans = 0
MAX = max(h)

for i in range(MAX):
  for j in range(n):
    if progress[j] < h[j]:
      progress[j] += 1
      if j+1 >= n:
        ans += 1
      else:
        if progress[j+1] == h[j+1]:
          ans += 1

print(ans)

Solution (Python):
from sys import stdin
import numpy as np

N = int(stdin.readline().rstrip())
H = [int(x) for x in stdin.readline().rstrip().split()]

def func(h_list, now_h):
  if len(h_list) == 0:
    return 0
  min_idx = [i for i, x in enumerate(h_list) if x == min(h_list)]
  min_h = h_list[min_idx[0]]
  ans = min_h - now_h
  l_idx = 0
  for i in min_idx:
    ans += func(h_list[l_idx:i],min_h)
    l_idx = i+1
  ans += func(h_list[l_idx:],min_h)
  return ans

print(func(H,0))

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
progress = [0]*n
ans = 0
MAX = max(h)

for i in range(MAX):
  for j in range(n):
    if progress[j] < h[j]:
      progress[j] += 1
      if j+1 >= n:
        ans += 1
      elif j+1 < n:
        if progress[j+1] == h[j+1]:
          ans += 1

print(ans)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
m=max(h)
ans=0
for i in range(m):
    k=m-i
    for j in range(n):
        if j!=n-1:
            if h[j]==k and h[j]==h[j+1]:
                ans-=1
        if h[j]==k:
            ans+=1
            h[j]-=1        
print(ans)

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

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

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

Solution (Python):
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
flower = read_int()

ans = 0
def rec(l,r):
    minf = float('INF')
    for i in range(l,r):
        minf = min(minf, flower[i])

    
    zero_index = []
    for i in range(l,r):
        flower[i] -= minf
        if flower[i] == 0:
            zero_index.append(i)
    global ans
    ans += minf

    zero_index.append(r)
    zero_index.insert(0,l-1)
    for i in range(len(zero_index) - 1):
        if zero_index[i] + 1 != zero_index[i + 1]:
            rec(zero_index[i] + 1, zero_index[i + 1])


if __name__ == "__main__":
    rec(0, N)
    print(ans)

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

Solution (Python):
flowers_num = int(input())
target_heights = list(map(int, input().split()))

max_height = max(target_heights)

watering_sum = 0
for row_i in range(1, max_height + 1):
    should_water = False

    for target_height in target_heights:
        if row_i <= target_height:
            should_water = True
        elif should_water:
                watering_sum += 1
                should_water = False

    if should_water:
        watering_sum += 1
        should_water = False

print(watering_sum)

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

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

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


def count(h):
    if len(h) != 0:
        output = 0
        min_elem = min(h)
        output += min_elem
        for index in range(0, len(h)):
            h[index] -= min_elem
        tmp_h = []
        for index in range(0, len(h)):
            if (h[index] > 0):
                tmp_h.append(h[index])
            else:
                output += count(tmp_h)
                tmp_h = []
        output += count(tmp_h)
        return output
    else:
        return 0

h = H
print(count(h))

Solution (Python):

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

Solution (Python):
N = int(input())
h = [int(_) for _ in input().split()]
ResSum = 0
pre = 0

for i in h:
    if i > pre:
        ResSum += i - pre
    pre = i

print(ResSum)

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
b = []
inf = 10**10
start = 0
end = 0
ans = 0

for ii in range(inf):
    if max(h) == 0:
        break

    while h[0] == 0:
        del h[0]

    for i in range(len(h)):
        if h[i] == 0:
            end = i
            
            break
        end = len(h)
        b.append(h[i])

    
    sa = min(b)
    b.clear()
    

    for j in range(end):
        h[j] = h[j] - sa
        

    ans = ans + sa
        

print(ans)

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

pre = 0
count = 0
for i in range(N):
  if(H[i] > pre):
    count += H[i] - pre
  pre = H[i]
print(count)

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

pre = 0
cnt = 0
for h in H:
	if h > pre:
		cnt += h-pre
	pre = h

print(cnt)

Solution (Python):
N = int(input())
list = list(map(int,input().split()))
pre = 0
sum = 0

for i in list:
    if i > pre:
        sum += i-pre
    pre = i

print(sum)

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

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

h_max = max(hl)
c=0
for i in range(1, h_max+1):
    hit = False
    for j in range(len(hl)):
        if hl[j] >= i: 
            if j==len(hl)-1:
                c+=1
            hit=True
        else:
            if hit==True:
                c+=1
            hit=False

print(c)

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

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

last = 0
ans = 0
for h in H:
    if h >= last:
        ans += h - last
    else:
        if last == 0:
            ans += h
    last = h

print(ans)

Solution (Python):
import copy


n = int(input())
h = list(map(int, input().split()))
a = 0
i = 0
while i < n:
    if h[i] > 0:
        j = copy.copy(i)
        k = h[i]
        while h[j] > 0:
            if h[j] < k:
                k = h[j]
            j += 1
            if j == n:
                break
        for l in range(i, j):
            h[l] -= k
        a += k
    else:
        i += 1
print(a)

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

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

last = 0
ans = 0
for h in H:
    if h >= last:
        ans += h - last
    else:
        if last == 0:
            ans += h
    last = h

print(ans)

Solution (Python):

N = int(input())
H = [0]+list(map(int, input().split()))


a = 0
for i in range(1, N+1):
	a += max(H[i]-H[i-1], 0)

print(a)

Solution (Python):
import numpy as np
N = int(input())
h = np.array([int(i) for i in input().split()])
count=0
 
while(np.max(h)>0):
    start=0
    i=0
    while(h[i]==0):
        i+=1
    start=i
    while(h[i]!=0 and i<N-1):
        i+=1
    if h[i]==0:
        h[start:i]=h[start:i]-1
    else:
        h[start:]=h[start:]-1
    count+=1
 
print(count)

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

ans = 0
former = 0
for flower in h:
    if former < flower:
        ans += flower - former

    former = flower
print(ans)

Solution (Python):
n = int(input())
h_list = [int(i) for i in input().split()]
h_1 = h_list[1:] + [0]
count = 0
for i in range(len(h_list)):
    count += max(h_list[i] - h_1[i], 0)
print(count)

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

count = 0
flag = True

def split_zero(hs):
    lens = []
    tmp = []

    for i in range(0,N):
        if hs[i] == 0 and len(tmp) > 0:
            lens.append(tmp)
            tmp = []
        elif hs[i] > 0:
            tmp.append(hs[i])

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

    return len(lens)

def dec_array(hs):
    for i in range(0,N):
        hs[i] -= 1
        hs[i] = max(hs[i],0)
    return hs


count = 0
while any([h > 0 for h in hs]):
    count += split_zero(hs)
    hs = dec_array(hs)

print(count)

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




a = np.zeros(N)
h_min = min(h)
h_max = max(h)
n_loop = h_max - h_min

cnt = 0
for zzz in range(n_loop):
    tmp = (h >= h_min+1+zzz)*1

    for idx, i in enumerate(tmp):
        if idx==0:
            first = i
            a0 = i
            continue
            
        if i==a0:
            continue
        else:
            if i==1:
                cnt+=1
            a0 = i
    else:
        if first==1:
            cnt+=1
            
print(cnt+h_min)

Solution (Python):
import numpy as np
global count

def recursive_f(h):
    global count
    hmin = min(h)
    count += hmin
    h = h - hmin
    
    zeroidx = list(h).index(0)
    if len(h) == 1:
        return 0
    else :
        if zeroidx > 0 & zeroidx < len(h)-1:
            h1 = h[0:zeroidx]
            h2 = h[zeroidx+1:len(h)]
            if len(h1) > 0:
                recursive_f(h1)
            if len(h2) > 0:
                recursive_f(h2)
            
        elif zeroidx == 0:
            h2 = h[1:]
            if len(h2) > 0:
                recursive_f(h2)
            
        elif zeroidx == len(h)-1:
            h1 = h[0:len(h)-1]
            if len(h1) > 0:
                recursive_f(h1)
            
        


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

h = np.array(h)
count = 0
a = 0
recursive_f(h)
print(count)

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

while(max(h)>0):
    while(h[i]==0):
        i+=1
    start=i
    while(h[i]!=0 and i<N-1):
        i+=1
    if h[i]==0:
        h[start:i]=h[start:i]-1
    else:
        h[start:]=h[start:]-1
    answer+=1
    start=0
    i=0

print(answer)

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

answer = 0
for i in range(1, len(h)):
  if h[i] > h[i-1]:
    answer += h[i] - h[i-1]
  
print(answer)

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

def mizu(h):
    c = 0
    if h == []:
        return 0
    else:
        c = min(h)
        h0 = list(map(lambda x : x - min(h), h))
        if h0[0] == 0:
            h0.remove(0)
            c = c + mizu(h0)
        else:
            for i in range(len(h0)):
                if h0[i] == 0:
                    h1 = h0[0:i]
                    h2 = h0[i:len(h0)]
                    break
            c = c + mizu(h1) + mizu(h2)
        return c

print(mizu(h))

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

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

opr_count = 0
h_highest = max(h)

while any(h):
    flag = 0
    for i, v in enumerate(h):
        if v == h_highest:
            h[i] -= 1
            if not flag:
                opr_count += 1
                flag = 1
                continue
        else:
            flag = 0
    h_highest -= 1

print(opr_count)

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

cnt = 0
while 1:
    if len(h) == 0:
        break
    elif h[0] == 0:
        h.pop(0)
    else:
        i = h.index(0)
        h[:i] = [tmp -1 for tmp in h[:i]]
        cnt +=1
print(cnt)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
cnt = 0
def minus1(arr) :
  for i in range(len(arr)) :
    if(arr[i] != 0) : arr[i] -= 1
  return 0

def discover0(arr) :
  cnt = 0 
  for i in range(1, len(arr)) :
    if(arr[i-1] == 0 and arr[i] == 0):
      cnt += 1
  if(arr[0] == 0) : cnt += 1
  if(arr[len(arr)-1] == 0) : cnt += 1
  return arr.count(0) - cnt

for i in range(max(h)) :
  cnt += discover0(h)
  minus1(h)
  cnt += 1
print(cnt)

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

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

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

c = 0
i = 0

while sum(h) > 0:
    if h[i] > 0:
        j = i
        c = c + 1
        while j < n and h[j] > 0:
            h[j] = h[j] - 1
            j = j + 1
    else:
        i = i + 1

print(c)

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
def f(arr):
    if(sum(arr) == 0):
        return 0
    while(arr[0] == 0):
        arr.pop(0)
    while(arr[-1] == 0):
        arr.pop()
    if (min(arr) == 0):
        index = arr.index(0)
        return f(arr[0:index]) + f(arr[index: len(arr)])
    else:
        minval = min(arr)
        if (len(arr) == 1):
            return minval
        else:
            arr = list(map(lambda x: x- minval, arr))
            if(sum(arr)==0):
                return minval
            else:
                return f(arr) + minval
print(f(h))

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

for i in range(N):
    if i == 0:
        res += H[0]
    else:
        if H[i-1] < H[i]:
            res += H[i] - H[i-1]

print(res)

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

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

node = sum(h)
path = 0
prev = h[0]
for cur in h[1:]:
    path += min(prev, cur)
    prev = cur
print(node - path)

Solution (Python):



import bisect

def recursive(h):
    if len(h) == 0:
        return 0
    pos_zero = [-1]
    min_h = min(h)
    count = min_h
    for i in range(len(h)):
        h[i] -= min_h
        if h[i] == 0:
            pos_zero.append(i)
    
    pos_zero.append(len(h))
    
    if len(pos_zero) > 0:
        for i in range(0, len(pos_zero)-1):
            count += recursive(h[pos_zero[i]+1:pos_zero[i+1]])

    return count
    

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

count = recursive(h)

print(count)

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

c = 0
while any(x > 0 for x in h):
    b = False
    for i in range(len(h)):
        if h[i] > 0:
            b = True
            h[i] -= 1
        elif b:
            break
    if b:
        c += 1
print(c)