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

"""
C - Grand Garden
https://atcoder.jp/contests/abc116/tasks/abc116_c

"""
import sys



def solve(heights):
    def w(arr):
        stack = []
        buff = []
        while arr:
            h = arr.pop(0)
            if h == 0:
                if buff:
                    stack.append(buff)
                    buff = []
            else:
                buff.append(h)
        if buff:
            stack.append(buff)
        return stack

    ans = 0
    stack = w(heights)
    while stack:
        arr = stack.pop()
        m = min(arr)
        ans += m
        s = w([a-m for a in arr])
        stack.extend(s)
    return ans


def main(args):
    N = int(input())
    heights = [int(h) for h in input().split()]
    ans = solve(heights)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

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

h = [0] + H
c = 0
for i in range(1, 100 + 1):
	for j in range(1, N + 1):
		if h[j - 1] < i and i <= h[j]:
			c += 1
print(c)

Solution (Python):

n = int(input())
h_diff = [int(i) for i in input().split()]
ans = 0
for ni in range(n):
  while h_diff[ni] > 0:
    if 0 in h_diff[ni+1:]:
      r = h_diff[ni+1:].index(0) + (ni+1)
      ans += min(h_diff[ni:r])      
      h_diff[ni:r] = [i - min(h_diff[ni:r]) for i in h_diff[ni:r]]
    else:
      ans += min(h_diff[ni:])      
      h_diff[ni:] = [i - min(h_diff[ni:]) for i in h_diff[ni:]]
print(ans)

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

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

Solution (Python):

"""
Created on Tue May 14 19:41:33 2019

@author: avin"""

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

now = l[0]
sums = l[0]
for i in range(1,n):
    if now < l[i]:
        sums += l[i] - now
    now = l[i]
print(sums)

Solution (Python):
from collections import defaultdict
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N = int(input())
hh = inpl()

ans = 0
def bukatu(hh,L):
    global ans
    if L <= 0:
        return
    elif L == 1:
        ans += hh[0]
        return
    else:
        while not 0 in hh:
            ans += 1
            for i in range(L):
                hh[i] -= 1
        nexthh = [list(map(int,' '.join(h.split('*')).split())) for h in ('*'+'*'.join(map(str,hh))+'*').split('*0*')]
        
        for hh in nexthh:
            bukatu(hh,len(hh))

bukatu(hh,N)
print(ans)

Solution (Python):
def daruma_drop(ls):
  if max(ls)==0:
    return 0
  
  can_drop = min(ls)
  dropped = [ v - can_drop for v in ls ]
  devided = []
  b_idx = -1
  for i in range(len(dropped)+1):
    if i==len(dropped):
      if b_idx!=-1:
        devided.append(dropped[b_idx:])
      break
    
    if b_idx==-1 and dropped[i]!=0:
      b_idx = i
      continue
    
    if b_idx!=-1 and dropped[i]==0:
      devided.append(dropped[b_idx:i])
      b_idx = -1
      continue
  
  return can_drop + sum(map(daruma_drop, devided))
  
n = int(input())
hs = list(map(int, input().split(' ')))
print(daruma_drop(hs))

Solution (Python):
def spritarr(arr):
  ret=[]
  for i in range(arr.count(0)):
    pos=arr.index(0)
    ret.append(arr[:pos])
    arr=arr[pos+1:]
  if len(arr)!=0:
    ret.append(arr)
  return ret

n=int(input())
arr=list(map(int,input().split()))
tmp=spritarr(arr)
ans=0
while len(tmp)!=0:
  tmp2=[]
  for i in range(len(tmp)):
    if len(tmp[i])!=0:
      tm=min(tmp[i])
      ans+=tm
      for j in range(len(tmp[i])):
        tmp[i][j]-=tm
    tmp2.extend(spritarr(tmp[i]))
  tmp=tmp2
print(ans)

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


test = []
tmp = []

while max(h) != 0:
    tmp = []
    for i in range(N):
        if h[i] == 0:
            tmp.append(0)
        else:
            tmp.append(1)
            h[i] -= 1
    test.append(tmp)

ans = 0
flag = 0
for i in test:
    if i[0] == 1:
        ans += 1
    for j in range(1,N):
        if i[j] == 0:
            continue
        elif i[j-1] == 0 and i[j] == 1:
            ans += 1
print(ans)

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

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

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

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

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

print(ans)

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

ans = 0
while True:
    is_segment = False
    segment_count = 0
    for h in hs:
        if h > 0:
            if not is_segment:
                segment_count += 1
            is_segment = True
        else:
            is_segment = False
    if segment_count == 0:
        break
    else:
        ans += segment_count
        hs = [max(0, h-1) for h in hs]
print(ans)

Solution (Python):


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


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h) if x == 0]

    if not any(list_h):
        return min_height

    left_list = list_h[:zero_indexes[0]]
    right_list = list_h[zero_indexes[0]+1:]
    return calc(left_list) + calc(right_list) + min_height


print(calc(h))

Solution (Python):


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


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h) if x == 0]

    if not any(list_h):
        return min_height

    sum_cnt = 0
    left_list = list_h[:zero_indexes[0]]
    right_list = list_h[zero_indexes[0]+1:]
    sum_cnt += calc(left_list) + calc(right_list)

    return sum_cnt + min_height


print(calc(h))

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

Solution (Python):
N = int(input())
li = [ int(it) for it in input().split() ]

sss = 0

for i in range(10000000):
  ma = max(li)
  if (ma==0):
    break
  ind = li.index(ma)

  l = ind
  for i in range(ind,N):
    if ( li[i] != ma ):
      break
    else:
      l = i
  r = ind
  for i in range(ind,-1,-1):
    if ( li[i] != ma ):
      break
    else:
      r = i
  l,r=r,l
  nma = 0
  if (l>0):
    nma = max(nma,li[l-1])
  if (r<N-1):
    nma = max(nma,li[r+1])

  sss += ma-nma
  for i in range(l,r+1):
    li[i]-=ma-nma
print (sss)

Solution (Python):
'''
数列hを棒グラフと見て
0と0で挟まれた山の個数を数える→すべて-1して山を削っていく
出力例２なら
[3 1 2 3 1]をmax(h)回，-1ずつ減らしていく
[2, 0, 1, 2, 0]
[1, 0, 0, 1, 0]
[0, 0, 0, 0, 0]
0になると花に水をやってはいけないから
count0でその花が何番目にあるか，を取得
数列count0で前項との差が1より大きければ山がある＝水をかけれる
'''
n=int(input())
h=list(map(int, input().split()))
sum=0
for i in range(max(h)):
    if 0 not in h:
        h=[X-1 if X>0 else 0 for X in h]
        sum+=1
        
    else:
        count0=[Y for Y, Z in enumerate(h) if Z==0]
        a=count0.copy()
        b=count0.copy()
        del a[0]
        del b[-1]
        c=[x-y for(x,y) in zip(a,b)]
        sum = sum + len(c)-c.count(1)
        if count0[0]>0 and count0[-1]<n-1:
            sum +=2
        elif count0[0]>0 or count0[-1]<n-1:
            sum +=1
        else:
            sum +=0
        h=[i-1 if i>0 else 0 for i in h]
print(sum)

Solution (Python):
n=int(input())
h=list(map(int, input().split()))
sum=0
for i in range(max(h)):
    if 0 not in h:
        h=[X-1 if X>0 else 0 for X in h]
        sum+=1
        
    else:
        count0=[Y for Y, Z in enumerate(h) if Z==0]
        a=count0.copy()
        b=count0.copy()
        del a[0]
        del b[-1]
        c=[x-y for(x,y) in zip(a,b)]
        sum = sum + len(c)-c.count(1)
        if count0[0]>0 and count0[-1]<n-1:
            sum +=2
        elif count0[0]>0 or count0[-1]<n-1:
            sum +=1
        else:
            sum +=0
        h=[i-1 if i>0 else 0 for i in h]
print(sum)

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

link = 0
ans = 0
while True:
    for i in range(N):
        if i < N-1:
            if h[i] > 0:
                h[i] -= 1
                if h[i+1] > 0:
                    link = 1
                else:
                    link = 0
                if link == 0:
                    ans += 1
        else:
            if h[i] > 0:
                h[i] -= 1
                link = 0
                ans += 1

    if all([v == 0 for v in h]):
        print(ans)
        exit()

Solution (Python):
import sys
input = sys.stdin.readline

n = int(input())

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

ans = 0
active = 0

for i in range(n):
    if active >= h[i]:
        active = h[i]
    else:
        ans += h[i] - active
        active = h[i]
print(ans)

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

max_h = max(H)
AH = []
for i in H:
  AH.append([1 if j < i else 0 for j in range(max_h)])
  
AH = list(zip(*AH))

count= 0
for i in AH:
  prev = 0
  for j in i:
    if j != prev and j == 1:
      count += 1
    prev = j

print(count)

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


def water(x):
  global ans

  if any(x) == False:

    return
  else:

    if 0 in x:

      x1, x2 = x[0:x.index(0)], x[x.index(0)+1:]
      water(x1)
      water(x2)
    else:

      ans += min(x)
      x = list(map(lambda i:i-min(x), x))
      water(x)
  return    
  
water(h)
print(ans)

Solution (Python):
N = int(input())
H = list(map(int, input().split()))
H.append(0)
ans = []
start = 0
prev = 0
for i in H:
  if prev > i and prev != start:
    ans.append(prev - start)
    start = i
  elif start > i:
    start = i
  prev = i

print(sum(ans))

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

count = 0
sub_H_list = [H]
while True:
    if len(sub_H_list) == 0:
        break
    sub_H = sub_H_list.pop()
    min_hight = min(sub_H)
    if min_hight == 0:
        
        border = sub_H.index(0)
        if border == 0 and len(sub_H) == 1:
            pass
        elif border == 0:
            sub_H_list.append(sub_H[1:])
        elif border == len(sub_H)-1:
            sub_H_list.append(sub_H[:-1])
        else:
            sub_H_list.append(sub_H[0:border])
            sub_H_list.append(sub_H[border+1:])

    else:
        
        sub_H_list.append(list(map(lambda x: x-min_hight, sub_H)))
        count += min_hight

print(count)

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

def c(a,b): 
  
  if b-a==1:
    return h[a]
  if b==a:
    return 0
  
  try:
    s = h.index(0,a,b)
  except ValueError:
    s = -1
  if s>=0:
    return c(a,s) + c(s+1,b)
  
  m = min(h[a:b])
  for i in range(a,b):
    if h[i] == m:
      index = i
    h[i] -= m
  
  return m + c(a,index) + c(index+1,b)

print(c(0,N))

Solution (Python):
import sys

N = int(sys.stdin.readline())
h = list(map(int, sys.stdin.readline().split()))

max_h = max(h)

shakutori_list = []
shikiri = []

phase = 0

total = 0

for i in range(max_h):
    current_height = i + 1
    phase = 0
    for j in range(N):
        if h[j] >= (current_height) and phase == 0:
            total += 1
            phase = 1
        if h[j] < current_height and phase == 1:
            phase = 0

print(total)

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

ans = 0
cur = 0
while h.count(0) != len(h):
    for i in range(N+2):
        if h[i] == 0:
            if cur > 0:
                ans += 1
            cur = 0
        elif h[i] > 0:
            h[i] -= 1
            cur += 1
print(ans)

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

def split_list(l, number=0):    
    if len(l) == 0:
        return []
    elif number not in l:
        return [l]
    first_occurance = l.index(number)
    result = []
    if first_occurance != 0:
        result.append(l[0:first_occurance])
    for s in split_list(l[first_occurance + 1:]):
        result.append(s)

    return result

queue = split_list(h)
count = 0
score = 0
while len(queue) != count:
    target = queue[count]
    mizu = min(target)
    target = [x - mizu for x in target]
    score += mizu

    for s in split_list(target):
        queue.append(s)
    count += 1

print(score)

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

zerobefore = True
times = 0
while sum(h) > 0:
    
    for i in range(len(h)):

        if h[i] == 0 and zerobefore:
            continue
        elif h[i] == 0 and not zerobefore:
            times += 1
            zerobefore = True 
            break
        elif i == len(h) - 1 and not zerobefore and h[i] != 0:
            h[i] -= 1
            times += 1
            zerobefore = True  
        elif i == len(h) -1 and zerobefore and h[i] != 0:
            h[i] -= 1
            times += 1
            zerobefore = True
        else:
            h[i] -= 1
            zerobefore = False
        
        
        
    
    
            
print(times)

Solution (Python):
from math import factorial
from sys import exit, stdin
N = int(stdin.readline().rstrip())
H = [int(_) for _ in stdin.readline().rstrip().split()]
ans = 0
for i in range(1,N):
    if H[i-1] > H[i]:
        ans += H[i-1]-H[i]
ans += H[-1]
print(ans)

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


def search(start, end):
    sub_list = []

    index = start
    is_searching = False

    sub = 100
    l = start
    while index <= end:

        if H[index] > 0 and not is_searching: 
            is_searching = True
            l = index

        if H[index] == 0 and is_searching: 
            is_searching = False
            r = index - 1
            sub_list.append((l,r, sub))

        if is_searching:
            sub =  min(sub, H[index])

        index += 1
 
    if is_searching:
        sub_list.append((l, end, sub))


    count = 0
    for l, r, s in sub_list:
        for index in range(l, r + 1):
            H[index] -= s

        count += s
        count += search(l, r)

    return count


        
print(search(0, N -1))

Solution (Python):
def water(l,x,c):
    if l==1:
        return x[0]
    if l==0:
        return 0
    k=j=0
    for i in x:
        if i==0:
            k=j
            break
        j+=1
    if j<l:
        c += water(k, x[:k], 0)
        c += water(l - (k+1) , x[k+1:], 0)
    else:
        c+=1
        for i in range(l):
            x[i]-=1
        i=0
        while x[i]==0 and i < l-1:
            i+=1
        c+=water(l-i,x[i:],0)
    return c

a=int(input())
b=[int(i) for i in input().split()]
c=water(a,b,0)
print(c)

Solution (Python):
def water(l,x,c):
    if l==1:
        return x[0]
    if l==0:
        return 0
    k=j=0
    for i in x:
        if i==0:
            k=j
            break
        j+=1
    if j<l:
        
        
        c += water(k, x[:k], 0)
        
        
        c += water(l - (k+1) , x[k+1:], 0)
    else:
        c+=1
        for i in range(l):
            x[i]-=1
        i=0
        while x[i]==0 and i < l-1:
            i+=1
        c+=water(l-i,x[i:],0)
    return c

a=int(input())
b=[int(i) for i in input().split()]
c=water(a,b,0)

print(c)

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

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

n = max(lst)
h = 1
ret = 0
while n >= h:
    flg = True
    for x in lst:
        if x >= h and flg:
            ret += 1
            flg = False
        elif h > x:
            flg = True
    h += 1
print(ret)

Solution (Python):
import numpy as np
N = int(input())
lst = [int(x) for x in input().split()]

lst2 = np.zeros((max(lst), N))
for idx, n in enumerate(lst):
    lst2[:n,idx] = 1

ret = 0
for x in lst2:
    flg = True
    for y in x:
        if flg and y == 1:
            ret += 1
            flg = False
        elif y == 0:
            flg = True
print(ret)

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 = int(input())
H = [int(a) for a in input().split(" ")]
ans=0
while any([h!=0 for h in H]):
    i=0
    while i<N and H[i]==0:
        i+=1
    j=i
    minh=H[i]
    while j<N and H[j]!=0:
        minh = min(H[j], minh)
        j+=1
    for k in range(i,j):
        H[k]-=minh
    ans+=minh
print(ans)

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

ans = 0

while sum(h) > 0:
    diff = min([_h for _h in h if _h > 0])
    idx = h.index(diff)
    for i in range(idx, len(h)):
        if h[i] == 0:
            break
        h[i] -= diff
    for i in range(0, idx)[::-1]:
        if h[i] == 0:
            break
        h[i] -= diff
    ans += diff


print(ans)

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

ans = 0
flag = False

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

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

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

nums = [h[0]]

for i in range(N-1):
    if h[i+1] > h[i]:
        nums.append(nums[i] + (h[i+1] - h[i]))
    else:
        nums.append(nums[i])

print(nums[-1])

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

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

count = 0 
def water(i, j): 
    count = 0
    
    
    
    if i == j or lst[i:j] == [0]:
        
        return 0
    elif min(lst[i:j]) == 0: 
        a = lst[i:j].index(0)
        
        
        count = water(i,i + a) + water(i + a+1, j)
        return count
    else: 
        for x in range(i,j):
            lst[x] = lst[x] - 1
        
        count += 1 + water(i, j)
        return count

print(water(0, N))

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
ans = 0
INF = float("inf")
while True:
  
  
  left = -1
  for i in range(N):
    if h[i] != 0:
      left = i
      break
  if left == -1:
    break 
  
  
  
  right = -1
  for i in range(left, N):
    if h[i] == 0:
      right = i - 1
      break
      
    if i == N - 1:
      right = N - 1

  min_length = min(h[left:right+1])
  
  for i in range(left, right+1):
    h[i] -= min_length
  
  ans += min_length
  
  
  
print(ans)

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

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



print(ans)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
gap = [0]
mt = []
va = []
i = 0
while i+1 <= len(h)-1:
    if h[i+1]-h[i] != 0:
        gap.append(h[i+1]-h[i])
        i += 1
    else:
        h.pop(i+1)
for i in range(len(h)-1):    
    if gap[i] >= 0: 
        if gap[i+1] < 0:
            mt.append(h[i])
    else:
        if gap[i+1] > 0:
            va.append(h[i])
if gap[-1] > 0 :
    mt.append(h[-1])
if mt == []:
    mt.append(h[0])
ans = 0
for x in mt:
    ans += x
for x in va:
    ans -= x
print(ans)

Solution (Python):

import math
N= int(input())
traget = list(map(int,input().split(" ")))
process = [0] * N 
ans = 0
while True:
    end_flag = True
    target_start = True

    for i in range(N):
        if traget[i] != process[i]:
            process[i] += 1
            end_flag = False
            if target_start:
                ans += 1
                target_start = False
        else:    
            target_start = True


    
    
    
    if end_flag:
        break
print(ans)

Solution (Python):

import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, h: "List[int]"):
    ret = 0
    while True:
        b = 0
        while b < len(h) and  h[b] == 0:
            b += 1

        e = b
        while e < len(h) and 0 < h[e]:
            e += 1

        if b == e:
            break

        ret += 1
        for i in range(b, e):
            h[i] -= 1

    return ret


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) ]  
    result = solve(N, h)
    print(result)

if __name__ == '__main__':
    main()

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

ans = 0
a = [h]
while len(a) != 0:
    b = []
    for aa in a:
        v = min(aa)
        ans += v

        tmp = []
        for x in aa:
            xx = x - v
            if xx == 0:
                if len(tmp) > 0:
                    b.append(tmp)
                    tmp = []
            else:
                tmp.append(xx)
        if len(tmp) > 0:
            b.append(tmp)
    a = b
print(ans)

Solution (Python):
import numpy as np
from copy import deepcopy

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

H = np.array(H)







def water_area(arr):
  max_idx = np.where(arr == max(arr))
  shift_idx = np.roll(max_idx, 1)
  n_area = len(np.where(max_idx-shift_idx != 1)[0])
  return n_area, max_idx


cnt = 0
arr = deepcopy(H)
while True:
  if np.all(arr == 0):
    break
  n_area, max_idx = water_area(arr)
  cnt += n_area
  arr[max_idx] -= 1
  
print(cnt)

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=int(input())
h_list=list(map(int,input().split()))
count=0
end=[]
for i in range(n):
    end.append(0)
while not h_list==end:
    count_0=1
    for i in range(n):
        if h_list[i]==0:
            count_0=1
        else:
            h_list[i]=h_list[i]-1
            if count_0==1:
                count=count+1
            else:
                pass
            count_0=0
print(count)

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 + 1] - h[i]
print(ans + h[0])

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
ans = 0
height = 0
for i in range(len(a)):
  if a[i]>height:
    ans += a[i]-height
    height = a[i]
  else:
    height = a[i]
    
print(ans)

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
cnt=0
a=A[0]
b=0

for i in range(n-1):
  if A[i]<=A[i+1]:
    a=A[i+1]
  else:
    cnt+=a-b
    a=A[i+1]
    b=A[i+1]
if a>b:
  cnt+=a-b
print(cnt)

Solution (Python):
N = int(input().strip())
H = list(map(int, input().strip().split(' ')))+[0]
count = 0
for _ in range(max(H)):
  l = 0
  m = max(H)
  for r in range(1,len(H)):
    if H[r] != m:
      if H[l] == m:
        
        H[l:r] = [h-1 for h in H[l:r]]
        count += 1
      l = r
    else:
      if H[l] != m:
        l = r
print(count)

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

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

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

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

ans = 0

for i in range(n):
    while h[i]!=0:
        j = i
        while h[j]!=0:
            h[j]-=1
            j+=1
            if j==n:
                break
        ans +=1

print(ans)

Solution (Python):
from sys import stdin
import sys
sys.setrecursionlimit(10000)

n = int(stdin.readline().rstrip())
flowers = [int(x) for x in stdin.readline().rstrip().split()]


def watering(s, count):
    index_fwd = 10 ** 9
    index_back = 0
    for k in range(len(s)):
        if s[k] > 0:
            index_fwd = k
            break
    if index_fwd == 10 ** 9:
        return count
    for l in range(index_fwd, len(s)):
        if s[l] == 0:
            break
        index_back = l
    for m in range(index_fwd, index_back + 1):
        s[m] -= 1
    count += 1
    return watering(s, count)


print(watering(flowers, 0))

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
ans = 0
now = 1
while now <= max(h):
    kukan = 0
    flag = False 
    for i, hi in enumerate(h):
        if hi < now and flag:
            kukan += 1
            flag = False
        elif i == len(h)-1 and hi >= now:
            kukan+=1
        elif hi >= now and flag == False:
            flag = True
    now +=1
    ans += kukan
print(ans)

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

ans=0
for y in range(max(h))[::-1]:
    rzk=0
    for x in range(n):
        if y+1==h[x]:
            h[x]-=1
            if not rzk:
                ans+=1
                rzk=1
        else:
            rzk=0
            
print(ans)

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

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


def calc(arr):
    count = min(arr)
    l = 0
    new_arr = [arr[i] - count for i in range(len(arr))]
    for i, v in enumerate(new_arr):
        if v == 0:
            if l < i:
                count += calc(new_arr[l:i])
            l = i + 1
    if l < len(new_arr):
        count += calc(new_arr[l:len(new_arr)])

    return count


ans = calc(h)
print(ans)

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


def calc(arr, count):
    m = min(arr)
    count += m
    l = 0
    new_arr = [arr[i] - m for i in range(len(arr))]
    for i, v in enumerate(new_arr):
        if v == 0:
            if l < i:
                count = calc(new_arr[l:i], count)
            l = i + 1
    if l < len(new_arr):
        count = calc(new_arr[l:len(new_arr)], count)

    return count


ans = calc(h, 0)
print(ans)

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

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

print(ans)

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

H.append(0)
STATUS_UP = 0
STATUS_DOWN = 1

status = STATUS_UP
last_top = 0
last_valley = 0
ans = 0

for i in range(1, N+1):
    if status == STATUS_UP:
        if H[i-1] > H[i]:
            ans += H[i-1] - last_valley
            status = STATUS_DOWN
    else:
        if H[i-1] < H[i]:
            last_valley = H[i-1]
            status = STATUS_UP
print(ans)

Solution (Python):
import numpy as np

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

diff = np.diff(h).tolist()
max_list = []
min_list = []

sign = 1
for i, d in enumerate(diff):
    if d > 0 and sign < 0:
        sign = d
        min_list.append(h[i])

    if d < 0 and sign > 0:
        sign = d
        max_list.append(h[i])


def add(val, max_list, min_list):
    if len(max_list) > 1:
        min_val = min(min_list)
        split_index = min_list.index(min_val)+1
        return min_val-val + \
            add(min_val, max_list[:split_index], min_list[:split_index-1]) + \
            add(min_val, max_list[split_index:], min_list[split_index:])
    else:
        return max_list[0]-val


if max(h) == 0:
    print(0)
else:
    print(add(0, max_list, min_list))

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

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

ans = 0

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

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

max_index = H.index(max(H))
ans = 0
num_before = 0
for i in range(max_index,N):
  if H[i] > num_before:
    ans += H[i] - num_before
  num_before = H[i]
num_before = max(H)
for i in range(-max_index,1):
  if H[-i] > num_before:
    ans += H[-i] - num_before
  num_before = H[-i]
print(ans)

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

count = 0

for i in range(N):
    while True:
        if H[i] < 1:
            break
        indx_tmp = i
        for j in range(i, N):
            if H[j] == 0:
                break
            indx_tmp = j
        H[i:indx_tmp+1] -= 1
        count += 1
print(count)

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



flag = 1
cnt = 0

while flag >= 1 : 
    
    left_flag = 1
    for i in range(N) : 
        if(L[i] != 0) : 
            left = i
            left_flag = 0
            break
    if(left_flag == 1) : 
        break 
    
    right_flag = 1
    for j in range(left, N, 1) : 
        if(L[j] == 0) : 
            right = j
            right_flag = 0
            break
    if (right_flag == 1) : 
        right = N

    

    for k in range(left, right, 1) : 
        L[k] -= 1
        
    
    
    cnt += 1
print(cnt)

Solution (Python):

N = int(input())
h = list(map(int,input().split()))
ans = 0
i = 0
while sum(h) != 0:
    if h[i] == 0:
        i+=1
        continue
    for _ in range(i,N):
        if h[_] != 0:
            h[_] = h[_] - 1
        else:
            break
    ans+=1
print(ans)

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

oldh = 0
ans = 0
for i in h:
    if i - oldh >= 0:
        ans += i - oldh
    oldh = i

print(ans)

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

def chr(l,b):
    global ans
    if len(l) != 0:
        a = min(l)
        ans += a-b
        c = l.index(a)
        chr(l[:c],a)
        chr(l[c+1:],a)
chr(f,0)
print(ans)