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

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

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

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

Solution (Python):

import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
h = LI()
ans = 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):





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

    H = max(h)

    mat = [None] * H
    for i in range(H):
        mat[i] = "".join(["1" if i < h[j] else "0" for j in range(N)])

    ans = 0
    for i in range(H):
        ans += sum([1 if len(s) > 0 else 0 for s in mat[i].split("0")])

    return ans


print(main())

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

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    n = int(input())
    h_list = list(map(int, input().split()))
    l = -1
    r = -1
    result = 0
    while any((h > 0 for h in h_list)):
        for i,height in enumerate(h_list):
            if height > 0:
                if l == -1:
                    l = i
                if i == n-1:
                    r = i
                    break
            else:
                if l == -1:
                    continue
                elif r == -1:
                    r = i - 1
                    break

        if l == 0 and r == 0:
             h_list[0] = h_list[0] - 1
        else:
            for i in range(l,r+1):
                h_list[i] = h_list[i] - 1 
        l = -1
        r = -1
        result += 1
    print(result)
    return


if __name__ == '__main__':
    main()

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())
Hs = list(map(int, input().split()))

cnt = 0
l = 0
r = 1
while max(Hs) > 0:
  if Hs[l] == 0:
    l += 1
    r = l + 1
  elif r < N and Hs[r] > 0:
    r += 1
  else:
    for i in range(l,r):
      Hs[i] -= 1
    cnt += 1
    r = l + 1
  
print(cnt)

Solution (Python):


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



 
 

cnt = 0
while max(h) > 0:
    M = max(h)
    for i in range(n):
        if i == 0:
            flag = False
        
        if h[i] == M and flag == False:
            cnt += 1
            h[i] -= 1
            flag = True
        
        elif h[i] == M and flag == True:
            h[i] -= 1
            
        else:
            flag = False
        
print(cnt)

Solution (Python):
from itertools import chain

def process(l, ans):
  if not l:
    return ans
  elif len(l) == 1:
    ans += l[0]
    return ans
  elif all(l):
    ans += min(l)
    l = list(map(lambda x: x - min(l), l))
    ans = process(l, ans)
  else:
    d = [i for i in range(len(l)) if not l[i]]
    tmp = []
    td = 0
    for i, dd in enumerate(d):
      tmp.append(l[td:dd])
      td = dd + 1
      if i == len(d) - 1 and dd != len(l) -1:
        tmp.append(l[td:])
    for t in tmp:
      ans = process(t, ans)
  return ans

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

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

Solution (Python):
from collections import defaultdict
from collections import deque
import itertools
import math

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n = readInt()
h = readInts()
ans = 0
while h.count(0)!=n:
  boo = 0
  for i in range(len(h)):
    if h[i]!=0:
      if boo==0:
        boo=1
        ans+=1
      h[i]-=1
    else:
      boo=0

print(ans)

Solution (Python):
def resolve():
    '''
    code here
    '''
    N = int(input())
    hs = [int(item) for item in input().split()]

    cnt = 0
    flag = False
    while any(hs):
        
        for i in range(N):
            if hs[i] > 0:
                flag = True
                hs[i] -= 1
            else:
                if flag:
                    cnt += 1
                    flag = False
        else:
            if flag:
                cnt += 1
            flag = False

    print(cnt)



if __name__ == "__main__":
    resolve()

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

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

Solution (Python):
import sys
input=sys.stdin.buffer.readline
from math import*
import copy


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

Solution (Python):

import sys
input = sys.stdin.readline
N = int(input())
h_aft = list(map(int, input().split()))
h_bef =[0]*N


count =0
while sum(h_bef) < sum(h_aft):
    flag =[0]*(N+1)
    for i in range(N):
        if h_bef[i] <h_aft[i]:
            h_bef[i] +=1
            flag[i] =1
    for i in range(N+1):
        if flag[i] ==1 and flag[i+1] ==0:
            count +=1

print(count)

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

Solution (Python):
"""
8
4 23 75 0 23 96 50 100
"""

n = int(raw_input())
ais = map(int, raw_input().split(' '))

def f(ais):
	r = [0]
	for i in range(len(ais)):
		
		if len(r) % 2:	
			if (ais[i-1] if i-1 >= 0 else 0) <= ais[i] > (ais[i+1] if i+1 < len(ais) else 0): 
				r.append(ais[i])
		else:
		
			if (ais[i-1] if i-1 >= 0 else 0) >= ais[i] < (ais[i+1] if i+1 < len(ais) else 1000): 
				r.append(ais[i])

	t = 0
	for i in range(0,len(r),2):
		if i + 1 < len(r):
			u,v = r[i],r[i+1]
			t += v-u
	
	return t if len(ais) > 1 else ais[0]
print f(ais)

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

Solution (Python):
def cz(A):
    N = len(A)
    F = [0]
    for i in range(N):
        if A[i] == 0:
            F.append(0)
        else:
            F.append(1)

    res = 0
    for i in range(N):
        if F[i] == 0 and F[i+1] == 1:
            res += 1

    return(res)

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

cnt = 0
while sum(H) > 0:
    cnt += cz(H)
    for i in range(N):
        if H[i] == 0:
            continue
        else:
            H[i] -= 1

print(cnt)

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

count = 0
while not all([x <= 0 for x in h]):
    while h[0] == 0:
        del h[0]
    subcount = 0
    for i in range(len(h)-1):
        if (h[i] > 0 or h[i+1] > 0) and h[i]*h[i+1] == 0:
            subcount += 1
    h = [x - 1 if x - 1 >= 0 else 0 for x in h]
    subcount = subcount // 2 + 1
    count += subcount

print(count)

Solution (Python):

def main():
    n = int(input())
    h=[int(i) for i in input().split()]
    min_h = min(h)
    h_sub = [int(i)-min_h for i in h]
    res = min_h
    i = 0
    while max(h_sub)!=0:
        if(h_sub[i]>0):
            res+=1
            j = i
            while(j<n and h_sub[j]!=0):
                h_sub[j]-=1
                j+=1
        else:
            i+=1
        if(i>=n):
            break
    print(res)

if __name__ == '__main__':
    main()

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

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

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

H_=H.copy()

def get_zero_idx(H_):
    zero_idx = []
    for i in range(len(H_)):
        if H_[i] == 0:
            zero_idx.append(i)
    return zero_idx

zero_idx = get_zero_idx(H_)



ans=0

while True:
    zero_idx = get_zero_idx(H_)

    
    if len(zero_idx) == len(H_):
        break

    if len(zero_idx)==0:
        s=0
        e=N
        if s<e:
            for _ in range(s,e):
                H_[_]=H_[_]-1   
            ans +=1

    elif len(zero_idx)>=1:
        s=0
        e=zero_idx[0]
        if s<e:
            for _ in range(s,e):
                H_[_]=H_[_]-1   
            ans +=1


        for _ in range(1,len(zero_idx)):
            s=zero_idx[_-1]+1
            e=zero_idx[_]
            if s<e:
                for _ in range(s,e):
                    H_[_]=H_[_]-1   
                ans +=1


        s=zero_idx[-1]+1
        e=N
        if s < e:
            for _ in range(s,e):
                H_[_]=H_[_]-1   
            ans +=1


print(ans)

Solution (Python):
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    num = int(input())
    h = list(map(int, input().split()))
    ans = 0

    m=0
    for i in range(0,num):
        for j in range(m,num):
            if j == num-1:
                ans += h[j]
                flag = True
                break
            if h[j] > h[j+1]:
                ans += h[j] - h[j+1]
                m = j
        if flag:
            break
    print(ans)

if __name__ == "__main__":
    main()

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

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

print(ans)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
h = LIST()
ans = 0
tmp = 0
for i in range(n):
    if tmp < h[i]:
        ans += h[i] - tmp
    tmp = h [i]
print(ans)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
h = LIST()
ans = 0
tmp = 0
for i in range(n):
    for j in range(n):
        if tmp < h[i]:
            ans += h[i] - tmp
        tmp = h [i]
print(ans)

Solution (Python):
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
h = LIST()
mh = max(h)
cnt = 0
for i in range(1,mh+1):
    for j in range(n):
        if j == 0:
            flag = cnt_area = 1 if h[j] >= i else 0
        elif flag == 0 and h[j] >= i:
            flag = 1
            cnt_area += 1
        else:
            flag = (h[j] >= i)
    cnt += cnt_area

print(cnt)

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
def g(ls):
    try:
        return ls.index(0)
    except:
        return -1
def f(ls):
    if ls==[]:
        return 0
    if len(ls) == 1:
        return ls[0]
    m = min(ls)
    ls = [x-m for x in ls]
    ans = m
    while ls:
        i = g(ls)
        if i == -1:
            ans += f(ls)
            break
        ans += f(ls[:i])
        ls = ls[i+1:]
    return ans
print(f(h))

Solution (Python):
n = int(input())
Hs = list(map(int,input().split()))
f = [0 for _ in range(n)]
c = 0
r = 0
while r != n:
    r = 1
    for i in range(n):
        if f[i] < Hs[i] :
            if r > 0:
                c += 1
            r = 0
            f[i] += 1
        else:
            r += 1
    r -= 1   
print(c)

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


def down(H, l, r):
    for i in range(l, r):
        H[i] -= 1


s = 0
for h in range(100):
    
    l = 0
    while l < N:
        if H[l]:
            for j in range(l+1, N):
                if H[j] == 0:
                    r = j
                    break
            else:
                r = N
            down(H, l, r)
            s += 1
            l = r+1
        else:
            l += 1
print(s)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    h = in_nl()
    now = [0] * N

    ans = 0
    while h != now:
        ok = [-1] + [i for i, x in enumerate(now) if x == h[i]] + [N]
        lr = []
        for i in range(len(ok) - 1):
            if ok[i + 1] - ok[i] > 1:
                l = ok[i] + 1
                r = ok[i + 1]
                lr.append((l, r))

        for l, r in lr:
            diff = 10**9 + 7
            for i in range(r - l):
                diff = min(diff, h[l + i] - now[l + i])

            ans += diff
            for i in range(r - l):
                now[l + i] += diff

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

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


def ret_n_miz(ls: list):
    ret = 0  
    pre = 0  
    for l in ls:
        if pre == 0 and l > 0:
            ret += 1
        pre = l
    return ret


ans = 0
while sum(H) > 0:
    ans += ret_n_miz(H)
    for i in range(N):
        H[i] = max(0, H[i] - 1)

print(ans)

Solution (Python):

n = int(input())
h = list(map(int,input().split()))
kadan = [0 for _ in range(n)]
cnt = 0
tf = [False for _ in range(n)]

for i in range(1,10**5):
    cnt += 1
    
    
    
    flg = False
    for j in range(n):
        if tf[j] == False:
            flg = True
            if kadan[j] == h[j]:
                tf[j] = True
            else:    
                kadan[j] += 1
        elif tf[j] == True and flg == False:
            pass
        else:
            break
            
    if not False in tf:
        break
print(cnt-1)

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

ans=0
M=max(H)
count=0
while count<=M:
    if H[0]>=count:
        ans+=1
    i=1
    while i<N:
        if H[i]>=count and H[i-1]<count:
            ans+=1
        i+=1
    count+=1

print(ans-1)

Solution (Python):
n = int(input())
lst = list(map(int, input().split()))
lst.append(0)
goal = [0] * (n + 1)
watercnt = 0
if lst[0] == 0:
  phase = '-'
else:
  phase = '+'
while lst != goal:
  for i in range(n + 1):
    if phase == '+':
      if lst[i] > 0:
        lst[i] -= 1
      else:
        phase = '-'
        watercnt += 1
    else:
      if lst[i] > 0:
        lst[i] -= 1
        phase = '+'
print(watercnt)

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

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

Solution (Python):
def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A):
    MAXA = max(A)
    prev = 0
    ans = 0
    for a in A:
        if prev < a:
            diff = a - prev
            ans += diff
        prev = a
    return ans


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Solution (Python):
def solve():
    n = int(input())
    h = list(map(int, input().split()))
    print(watering(h))


def watering(h):
    if 0 in h:
        x = h.index(0)
        return watering(h[:x]) + watering(h[x + 1:])
    if h:
        if max(h) == 1:
            return 1
        else:
            h = list(map(lambda x: x - 1, h))
            return watering(h) + 1
    return 0



if __name__ == '__main__':
    solve()

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

Solution (Python):
import sys
import itertools

import time
import math
import heapq
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines




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

X = [H]
ans = 0
while True:
    newX = []
    for group in X:
        f = min(group)
        ans += f
        A = [(h - f) for h in group]
        temp = []
        for a in A:
            if a == 0:
                if len(temp) > 0:
                    newX.append(temp)
                    temp = []
                continue
            temp.append(a)
        if len(temp) > 0:
            newX.append(temp)

    if len(newX) == 0:
        break
    X = newX
print(ans)

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    H = il()

    ret, p = 0, 0
    for n in range(N):
        if p <= H[n]:
            ret += H[n] - p
        p = H[n]
    print(ret)


if __name__ == '__main__':
    main()

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

ans = 0
while h != end:
  strock = 0
  for i in range(N):
    if h[i] != 0:
      strock = 1
      h[i] -= 1
    else:
      if strock == 1:
        ans += 1
        strock = 0
  if strock == 1:
    ans += 1
  
print(ans)

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue

from decimal import Decimal
from collections import defaultdict



sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    H = il()

    flower = [0] * N
    ret = 0
    for i in range(N):
        if flower[i] == H[i]: continue

        j = i
        while N > j:
            if H[j] == flower[j]: break
            j += 1

        diff = H[i] - flower[i]
        mi = MAX
        for n in range(i, j):
            mi = min(mi, H[n] - flower[n])
            flower[n] += min(diff, mi)
        ret += diff

    print(ret)


if __name__ == '__main__':
    main()

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

step = 0
while(True):
  
  if(sum(hs) == 0):
    break
  
  start = None
  stop = None
  
  for i,h in enumerate(hs):

    if(h>0 and start is None):
      start = i
    if(h==0 and start is not None and stop is None):
      stop = i
     	
  if(start is not None and stop is None):
    stop = len(hs)
    
  for i in range(start, stop):
    hs[i] -= 1
    
  step += 1
  
print(step)

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

Solution (Python):


import math



from collections import deque
from collections import Counter




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

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

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

Solution (Python):
import numpy as np




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



def solve(N,h):
    s = 0
    while (np.any(h > 0)):
        w = np.where(h > 0)[0]
        l = w[0]
        r = w[0]
        i = 1
        while (i < len(w)):
            if (w[i] == r + 1):
                r = w[i]
            i += 1
        
        h = np.concatenate([h[:l], h[l:r+1]-1, h[r+1:]])
        s += 1
    return s

print(solve(N,h))

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

from collections import deque
queue = deque([])

queue.append(h)

ans = 0

while(len(queue) > 0):
    new_h = queue.popleft()
    ans += np.min(new_h)
    new_h -= np.min(new_h)
    
    R = np.where(new_h == 0)[0][0]
    new_L = new_h[:R]
    new_R = new_h[R+1:]
    
    if new_L.size > 0:
        queue.append(new_L)
    if new_R.size > 0:
        queue.append(new_R)
    
print(ans)

Solution (Python):
N = int(input())
arr = list(map(int, input().split()))
 
count =0
while True:
    if sum(arr) == 0:
        break
    count +=1
    start = False
 
    for i in range(N):
        if arr[i] != 0:
            arr[i] -= 1
            start = True
            continue
        if arr[i] == 0 and start == True:
            break
print(count)

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

if sum(arr) == 0:
    print(0)
    exit()




count =0
while True:
    count +=1
    start = False

    for i in range(N):
        if arr[i] != 0:
            arr[i] -= 1
            start = True
            continue
        if arr[i] == 0 and start == True:
            break
    if sum(arr) == 0:
        break
print(count)

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

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

hmax = max(h)
hmin = min(h)


ans = hmin
for i in range(hmin+1, hmax+1):
    count = 0
    flag = False
    for j in h:
        if j < i and flag:
            flag = False
        elif j >= i and not flag:
            flag = True
            count += 1
    ans += count

print(ans)

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

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

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

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



under=min(h)
for i in range(n):
    h[i]-=under
ans=under

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

def isallzero(h):
    ans = True
    for i in h:
        if i != 0:
            ans = False
            break
    return ans 

def func(h):
    if isallzero(h):
        return -1
    for i in range(len(h)):
        if h[i] != 0:
            return i

ans = -1
while True:
    ans += 1


    if isallzero(h):
        break

    m = func(h)
    for i in range(m, n): 
        if h[i] != 0:
            h[i] -= 1
        else:
            break

print(ans)

Solution (Python):
def main():
    n = int(input())
    hs = list(map(int, input().split()))

    ans = 0
    while True:
        if max(hs) == 0:
            break
        i = 0
        while (i < n):
            if hs[i] == 0:
                i += 1
            else:
                ans += 1
                while (i < n and hs[i] > 0):
                    hs[i] -= 1
                    i += 1
    print(ans)


if __name__ == "__main__":
    main()

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

print(ans)

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

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

h_state=[0]*n

def water(n,h,h_state):
    for i in range(n):
        if h_state[i]<h[i]:
            l=i
            break
        elif i==n-1 and h_state[i]<h[i]:
            l=i
        else:
            l=i+1
    
    for j in range(l,n):
        if h_state[j]==h[j]:
            r=j-1
            break
        else:
            r=j
    
    for k in range(l,r+1):
        h_state[k]+=1

cnt=0
while h_state != h:
    for i in range(n):
        if h_state[i]<h[i]:
            cnt+=1
            water(n,h,h_state)
            break
print(cnt)

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

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

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

res = 0

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

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

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

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

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

print(count)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
ans = 0
for k in range(100, 0, -1):
    ans += sum(hi == k for hi in h)
    ans -= sum(hi == hj == k for hi, hj in zip(h, h[1:]))
    h = list(map(lambda x: min(x, k - 1), h))
print(ans)

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

Solution (Python):



def cnt(A, x):
    N = len(A)
    i = 0
    ret = 0
    while i<N:
        if A[i]<x:
            i += 1
        else:
            ret += 1
            while i<N and A[i]>=x:
                i += 1
    return ret

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

    ret = 0
    for i in range(1,101):
        ret += cnt(A,i)

    print(ret)

if __name__ == '__main__':
    main()

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

while max(h) > 0:
    i = 0
    while i < n:
        if h[i] == 0:
            i += 1
        else:
            res += 1
            while i < n and h[i] > 0:
                h[i] -= 1
                i += 1
    
print(res)

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

i = min(h)
ans = i
h = list(map(lambda x:x-i,h))

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

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n, *h = map(int, read().split())

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

print(ans)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
ans=0
temp=[0]*n
while True:
  a=0
  b=1
  cnt=1
  for i in range(n):
    if h[i]!=temp[i]:
      temp[i]=temp[i]+1
      a=1
      b=0
    elif a==1:
      cnt=cnt+1
      a=0
      b=1
  if b==1:
    cnt=cnt-1
  ans=ans+cnt
  if sum(temp)==sum(h):
    break
print(ans)

Solution (Python):

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