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

Solution (Python):
import sys
import os

def file_input():
    f = open('ABC116/input.txt', 'r')
    sys.stdin = f

def minusmin(a):
    
    if len(a)==1:
        
        return a[0]
    else:
        a_min=min(a)
        
        
        j=0
        out=a_min
        for i in range(len(a)):
            a[i]-=a_min
            if a[i]==0:
                
                if i!=j:
                    out+=minusmin(a[j:i])
                j=i+1
            elif i==len(a)-1 and a[i]!=0:
                out+=minusmin(a[j:i+1])
        return out

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

    print(minusmin(h_li))


if __name__ == '__main__':
    main()

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

c = 0
now = 0
for i in range(N):
    if h[i] > now:
        c += h[i] - now
    now = h[i]

print(c)

Solution (Python):
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
n = ii()
H = iil()

now = 0
ans = 0
for item in H:
    if item > now:
        ans += item-now
    now = item

print(ans)

Solution (Python):
from scipy import array, ndimage
input()
H = array(input().split(), dtype='i8')
print(sum(ndimage.label(H > t)[1] for t in range(100)))

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

count = 0
line =[]
line2 =[False] * N
max_h =max(h)
for i in range(1,max_h+1)[::-1]:
    line = [x == i for x in h]
    line2 = line = [x|y for x,y in zip(line,line2)]
    flag = False
    for j in range(N):
        if flag == False:
            if line[j] == True:
                count += 1
                flag = True
            else:
                continue
        else:
            if line[j] == False:
                flag = False

print(count)

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

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


ans = 0

def solve(l):
    global ans
    if len(l) == 1:
        ans += l[0]
    elif len(l) == 0:
        pass
    else:
        minv = min(l)
        ans += minv

        l = [x - minv for x in l]

        zero = l.index(0)

        solve(l[:zero])
        solve(l[zero+1:])

solve(l)

print(ans)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
h.append(0)
cnt = 0
for i in range(max(h),0,-1):
    for j in range(N):
        if h[j] == i and h[j+1] == i:
            h[j] -= 1
        elif h[j] == i:
            cnt += 1
            h[j] -= 1
print(cnt)

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

ans = min(h)
h_ = [n - ans for n in h] + [0]
while max(h_) > 0:
    j = -1
    for i, n in enumerate(h_):
        if n == 0:
            if (j + 1) < i:
                m = min(h_[j+1:i])
                ans += m
                for k in range(j+1, i):
                    h_[k] = h_[k] - m
            j = i

print(ans)

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

Solution (Python):
n = int(input())
H = list(map(int,input().split()))
cnt = 0
for i in range(1,101):
    f = False
    for h in H:
        if f ==False and h>=i:
            cnt+=1
            f = True
        if h<i:
            f =False
print(cnt)

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

print(ans)

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

ans = 0
for i in range(1,101):
	res = 0
	flag = False
	for j in range(n):
		if h[j] >= i:
			flag = True
		if h[j] < i and flag:
			flag = False
			res += 1
	if flag:
		res += 1
	ans += res

print(ans)

Solution (Python):
import sys
import numpy as np
input = sys.stdin.readline
n = int(input())

A = list(map(int,input().split()))
cnt = 0
ans = 0
for i in range(n):
    if cnt ==0 and A[i] == 0:
        continue
    elif cnt ==0 and A[i] != 0:
        cnt = A[i]
        
        ans +=cnt
    elif cnt !=0 and cnt <= A[i]:
        ans += (A[i] - cnt)
        cnt = A[i]
        

        
    elif cnt !=0 and  A[i] < cnt:
        cnt = A[i] 
        
print(ans)

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

res=0

while sum(H)!=0:
	for i in range(n):
		if H[i]==0:
			continue
		else:
			if H[i+1]==0:
				H[i]-=1
				res+=1
			else:
				H[i]-=1

print(res)

Solution (Python):
count = 0
def mizu(h):
    global count

    if len(h) is 0:
        return

    if len(h) is 1:
        count += h[0]
        return

    m = min(h)
    count += m

    h = [i - m for i in h]

    while 0 in h:
        mizu(h[:h.index(0)])
        h = h[h.index(0)+1:]
    mizu(h)

if __name__ == "__main__":
    N = int(input())
    h = list(map(int, input().split()))
    mizu(h)
    print(count)

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

cnt = 0
while max(set(h)) > 0:
    flg = False
    for i in h:
        if i <= 0:
            flg = False
            continue
        else:
            if not flg:
                cnt += 1
            flg = True
    h = list(map(lambda x: x-1, h))

print(cnt)

Solution (Python):
def f(ls):
    if len(ls) == 0:
        return 0
    cnt = 0
    if not 0 in ls:
        min_ = min(ls)
        ls = [l - min_ for l in ls]
        cnt += min_
    divs = []
    s = 0
    for i, l in enumerate(ls):
        if l == 0:
            if i == 0:
                s = 1
            else:
                divs.append(ls[s:i])
                s = i + 1
    if l != 0:
        if s == i:
            divs.append([ls[s]])
        else:
            divs.append(ls[s:i+1])
    cnt_ = sum([f(d) for d in divs])
    return cnt + cnt_

n = int(input())
ls_ = [int(i) for i in input().split()]
print(f(ls_))

Solution (Python):
n=int(input())
h = [int(i) for i in input().split()]
ans=0
l=0
while True:
	r=l+1
	if(h[l]==0):
		l+=1
		if(l>=n):
			break
		continue
	while(r<n):
		if(h[r]!=0):
			r+=1
		else:
			break
	for i in range(l,r):
		h[i]-=1
	ans+=1
print(ans)

Solution (Python):
import sys 
sys.setrecursionlimit(1000)
n=int(input())
H=list(map(int,input().split()))
def bunkai(H):
    B=[]
    b=0
    f=0
    for i in range(len(H)):
        if H[i]==0:
            if i!=0:
                B.append(H[b:i-1+1])
                b=i+1
            else:
                b=i+1
        elif i==len(H)-1:
            B.append(H[b:])
    return B
def min(lis):
    min=101
    if len(lis)==0:
        return 0
    for i in range(len(lis)):
        if lis[i]<min:
            min=lis[i]
    return min
def mizu(lis,n):
    for i in range(len(lis)):
        lis[i]+=-n
    return lis
def sso(lis,sum):
    
    B=bunkai(lis)
    
    if len(B)==0:
        
        return sum
    for l in B:
        
        x=min(l)
        
        sum+=x
        li=mizu(l,x)
        
        sum=sso(li,sum)
    
    return sum


sum=0
x=sso(H,sum)

    
print(x)

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

Solution (Python):

import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, h: "List[int]"):
    ans = 0
    for k in range(max(h)):
        r = [i for i in range(N) if h[i] > k]
        a = 0
        j = 0
        while j < len(r):
            a += 1
            while j + 1 < len(r) and r[j + 1] == r[j] + 1:
                j += 1
            j += 1
        ans += a
    print(ans)


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):
input()
ans, tmp = 0, 0
for h in map(int, input().split()):
    ans += max(0, h - tmp)
    tmp = h
print(ans)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
board = [[0] * (N + 1) for _ in range(max(h))]
for i in range(N):
    for j in range(h[i]):
        board[j][i + 1] = 1
print(sum(row[i] == 0 and row[i + 1] == 1 for row in board for i in range(N)))

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

ans=0

L=0
while 1==1:
    R=L

    if sum(List)!=0 and List[L]==0:
        L=0
        while List[L]==0:
            L+=1

    if sum(List)==0:
        break
    elif R==len(List)-1:
        List[R]=List[R]-1
        ans+=1
    elif List[L]>List[L+1]:
        List[L]=List[L]-1
        ans+=1
    else:
        checkMAX=1
        while 1==1:
            R+=1
            if R==len(List)-1:
                for i in range(L,R+1):
                    List[i]=List[i]-1
                ans+=1
                break
            elif checkMAX>List[R+1]:
                for i in range(L,R+1):
                    List[i]=List[i]-1
                ans+=1
                break
            else:
                checkMAX=List[R+1]
                pass
    if sum(List)!=0 and List[L]==0:
        L=0
        while List[L]==0:
            L+=1
print(ans)

Solution (Python):


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

ans=0

L=0
while 1==1:
    R=L

    if sum(List)!=0 and List[L]==0:
        L=0
        while List[L]==0:
            L+=1


    
    if sum(List)==0:
        break
    elif R==len(List)-1:
        
        List[R]=List[R]-1
        ans+=1
        
    elif List[L]>List[L+1]:
        List[L]=List[L]-1
        
        ans+=1
        
    else:
        checkMAX=1
        while 1==1:
            
            
            
            R+=1
            if R==len(List)-1:
                
                for i in range(L,R+1):
                    List[i]=List[i]-1
                
                ans+=1
                
                break
            elif checkMAX>List[R+1]:
                
                
                
                for i in range(L,R+1):
                    List[i]=List[i]-1
                
                ans+=1
                
                break
            else:
                checkMAX=List[R+1]
                pass
            
    
    
    
    
            
    
    if sum(List)!=0 and List[L]==0:
        L=0
        while List[L]==0:
            L+=1
    
print(ans)

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

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


if n == 1:
    print(a[0])
    exit()

up = True
ans = 0
for i in range(n-1):
    if a[i] <= a[i+1]:
        if up == False:
            ans -= a[i]
        up = True
    else:
        if up == True:
            ans += a[i]
        up = False

if a[-2] <= a[-1]:
    ans += a[-1]

print(ans)

Solution (Python):
N = int(input())
H = list(map(int,input().split(" ")))
H_orig = H[:]
heights = [0 for i in range(N)]

cnt = 0

while True:
    
    if min(H) > min(heights):
        for i in range(N):
            heights[i] += 1
            H[i] -= 1
        cnt += 1
    else:
        flag = False
        for i in range(N):
            
            if H[i] > 0:
                heights[i] += 1
                H[i] -= 1
                flag = True
            else:
                if flag:
                    cnt += 1
                    flag = False
                    
            if i == N-1 and flag:
                cnt += 1
        
    if H_orig == heights:
        break
    
print(cnt)

Solution (Python):
def calc(list1,list2):
    for l in list2:
        list1[l] += 1
    return list1

n = int(input())
h = list(map(int,input().split()))
f = [0]*n
ans = 0

for i in range(min(h)):
    f = [x+1 for x in f]
    ans += 1

while sum(f) != sum(h):
    flag = False
    tmp = []
    for i in range(n):
        if i == n-1 and h[i] != f[i]:
            tmp.append(i)
            f = calc(f,tmp)
            ans+=1
        elif h[i] == f[i] and flag:
            f = calc(f,tmp)
            ans+=1
            break
        elif h[i] != f[i]:
            flag = True
            tmp.append(i)
else:
    print(ans)

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



while sum(H) > 0:
    i = 0
    memo = []
    while i < N+1:
        if H[i] > 0:
            memo.append(i)
        if H[i] == 0:
            if len(memo):
                for i in memo:
                    H[i] -= 1
                memo = []
                ans += 1
        i += 1

print(ans)

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

while sum(H) > 0:
    i = 0
    memo = []
    while i < N+1:
        if H[i] > 0:
            memo.append(i)
        if H[i] == 0:
            if len(memo):
                for i in memo:
                    H[i] -= 1
                memo = []
                ans += 1
        i += 1

print(ans)

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

ans = 0
while sum(h) > 0:
  for k in range(len(h)):
    if h[k] > 0:
      h = h[k:]
      break
  for k in reversed(range(len(h))):
    if h[k] > 0:
      h = h[:(k+1)]
      break
  cnt = 1
  for k in range(len(h)):
    if h[k] == 0 and h[k+1] > 0:
      cnt += 1
  h = [max(c-1,0) for c in h]
  ans += cnt

print(ans)

Solution (Python):
import itertools
N = int(input())
arr = list(map(int, input().split()))
ans = 0
while sum(arr) != 0:
    tmp = [0]*N
    tmp2 = 0
    for i in range(N):
        if arr[i] != 0:
            tmp[i] = 1
            arr[i] -= 1
    gr = itertools.groupby(tmp)
    for key, group in gr:
        if key != 0:
            tmp2 += 1
    ans += tmp2
print(ans)

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

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

print(cnt)

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

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

c = 0

m = max(h)
for i in range(1,m+1)[::-1]:
    l = -2
    for j in range(n):
        if(h[j] == i):
            if(j != l+1):c += 1
            h[j] -= 1
            l = j
print(c)

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
def dfs(ans, H):
    if not any(H):
        return ans
    flag = 1
    cnt = 0
    for i in range(N):
        if H[i] != 0:
            H[i] -= 1
            if flag:
                flag = 0
                cnt += 1
        else:
            flag = 1
    return dfs(ans + cnt, H)
print(dfs(0, H))

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

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
ans = 0
pre = 0
for i in h:
    ans += max(0, i-pre)
    pre = i
print(ans)

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

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

while True:
    if h==[0]*(N+1):
        print(ans)
        exit()
        
    for i in range(N):
        if h[i]>0 and h[i+1]==0:
            ans += 1
        
    for i in range(N):
        h[i] = max(h[i]-1, 0)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
ans = 0
pre = 0
for i in h:
    ans += max(0, i-pre)
    pre = i
print(ans)

Solution (Python):
counter=0
def answer(h:list) -> int:
  global counter
  if len(h)==0:
    return
  if len(h)==1:
    counter+=h[0]
    return
  min_a=min(h)
  for i in range(len(h)):
    h[i]-=min_a
  counter+=min_a
  answer(h[:h.index(0)])
  answer(h[h.index(0)+1:])

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

Solution (Python):
N = int(input())
H = list(map(int, input().split()))
cnt = 0
while True:
    if len(H) == 1:
        break
    for i in range(len(H)):
        if H[i] == 0:
            if i == 0:
                H.pop(i)
                cnt -= 1
            break
        H[i] -= 1
    cnt += 1

print(cnt+H[0])

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

def water(now):
    for i in range(n):
        if now[i] == 0:
            continue
        else:
            for j in range(i, n):
                if now[j] != 0:
                    now[j] -= 1
                else:
                    break
            break
c=0

while True:
    if now == [0]*n:
        break
    water(now)
    c += 1

print(c)

Solution (Python):
N = int(input())
H = list(map(int, input().split())) + [0]
az = [0]*(N+1)
ans = 0
while H != az:
    c = 0
    in_itv = False
    for i in range(N+1):
        if H[i] == 0:
            if in_itv:
                c += 1
                in_itv = False
            continue
        in_itv = True
        H[i] -= 1
    ans += c
print(ans)

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

kyokusyou = 0
count = 0
gezan = False
for i in range(n):
  if not gezan and a[i+1] < a[i]:
    count += a[i] - kyokusyou
    gezan = True
  elif gezan and a[i+1] > a[i]:
    gezan = False
    kyokusyou = a[i]

print(count)

Solution (Python):

_, *h = map(int, open(0).read().split())
s = p = 0
for i in h:
    s += max(i - p, 0)
    p = i
print(s)

Solution (Python):

icase=0
if icase==0:
    n=int(input())
    h=list(map(int,input().split()))
elif icase==1:
    n=5
    h=[3,1,2,3,1]
    ans=5
elif icase==2:
    n=4
    h=[1,2,2,1]
    ans=2
elif icase==3:
    n=8
    h=[4, 23, 75, 0, 23, 96, 50, 100]
    ans=221

hmin=10**6

def hct(hh,ll,rr,hcnt0):

    if ll>=rr:
        return hcnt0
    hcnt0+=1
    hmax=-1
    hhim=-1
    lll=ll
    for i in range(ll,rr):
        hh[i]-=1
        hmax=max(hh[i],hmax)
        if hh[i]>0 and hhim<=0:
            lll=i
            hhim=1
        if hh[i]<=0 and hhim>0:
            rrr=i
            hhim=-1
            hcnt0=hct(hh,lll,rrr,hcnt0)
    if hhim>0:
        hcnt0=hct(hh,lll,rr,hcnt0)
    return hcnt0
        
hcnt=0
ll=0        
hhim=-1
for i in range(n):
    if h[i]==0 and hhim>0:
        hcnt=hct(h,ll,i,hcnt)
        lll=i
        hhim=-1
    elif h[i]==0:
        hhim=-1
    elif h[i]>0 and hhim<0 :
        ll=i
        hhim=1
if hhim>0:
    hcnt=hct(h,ll,n,hcnt)
print(hcnt)

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

Solution (Python):
N = int(input())
j = 0
start = 0
ans = 0
number_list = list(map(int,input().split()))
after_l = []
B = []
for i in range(N):
    for i in range(N):
        if number_list[i] == 0:
            if B != []:
                after_l.append(B)
                B=[]
            after_l.append([0])
        else:
            B.append(number_list[i])
    if B != []:
        after_l.append(B)
    for i in range(len(after_l)):
        C = after_l[i]
        C = min(C)
        ans += C
        after_l[i] = list(map(lambda x:x-C,after_l[i]))
    number_list = sum(after_l,[])
    after_l = []
    B = []
print(ans)

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

f = [0] * n
ans = 0

while h != f:
    flag = 0
    for i in range(n):
        if i == n - 1:
            if f[i] < h[i]:
                f[i] += 1
                ans += 1
            elif f[i] == h[i] and flag == 0:
                ans += 1
        elif i == 0:
            if f[i] < h[i]:
                f[i] += 1
                flag = 0
            elif f[i] == h[i]:
                flag = 1
        elif i != n - 1:
            if f[i] < h[i]:
                f[i] += 1
                flag = 0
            elif f[i] == h[i] and flag == 0:
                ans += 1
                flag = 1

print(ans)

Solution (Python):
counter = 0
def myAnswer(H:list) -> int:
   global counter
   if(len(H)==0):
      return
   if(len(H)==1):
      counter += H[0]
      return
   MIN = min(H)
   for i in range(len(H)):
      H[i] -= MIN
   counter += MIN
   myAnswer(H[:H.index(0)])
   myAnswer(H[H.index(0)+1:])


def modelAnswer():
   return
def main():
   N = int(input())
   H = list(map(int,input().split()))
   myAnswer(H[:])
   print(counter)
if __name__ == '__main__':
   main()

Solution (Python):

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

max_height = max(Hs)

tot_num=0
for i in range(max_height): 
    temp_num = 0
    continuum=0
    for j in range(0,N):
        if continuum==0 and Hs[j]>i:
            temp_num+=1
            continuum+=1
            continue
        elif Hs[j]>i:
            continuum+=1
            continue
        else:
            continuum = 0
            continue
    tot_num += temp_num


print(tot_num)

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

ans = 0
buf = 0
  
for i in range(n):
  if(buf < h[i]):
    ans += h[i] - buf
  buf = h[i]

print(ans)

Solution (Python):
n = int(input())
h_list = list(map(int,input().split()))
ans = 0
f = True
while f:
    f = False
    e = True
    for i in range(n):
        if h_list[i] > 0:
            h_list[i] -= 1
            if e:
                ans += 1
            f = True
            e = False
        else:
            e = True
print(ans)

Solution (Python):
n = int(input()) 
from collections import deque
from copy import deepcopy
d = deque()
a = list(map(int,input().split()))
c = 0
for i in range(len(a)):
    d.append(a[i])
def wat(a):
    global c
    if len(a) != 0:
        while a[0] != 0:
            c += 1
            for i in range(len(a)):
                if a[i] >0 :
                    a[i] = a[i] - 1
                else:
                    break
        b = deepcopy(a)
        for i in range(len(b)):
            if a[i] == 0:
                b.popleft()
            else:
                break
        wat(b)
wat(d)
print(c)

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

Solution (Python):
def main():
    N = int(input())
    H = [int(a) for a in input().split()]
    
    cnt = 0
    for i in range(max(H)):
        S = [False for i in range(len(H))]
        for j in range(len(H)):
            if H[j] != 0 and S[j] == False:
                for k in range(j, len(H)):
                    if H[k] != 0 and S[k] == False:
                        H[k] -= 1
                        S[k] = True
                    else:
                        break
                cnt += 1
    print(cnt)

if __name__ == "__main__":
    main()

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

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

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

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

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

print(ans)

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

ans = 0

if h.count(0) == 0:
  ans += min(h)
  h = list(map(lambda x:x-min(h) ,h))

while h.count(0) != len(h):

  pos = 0
  for i in range(len(h)):
    if h[i] != 0:
      pos = i
      break

  pos2 = len(h)
  for j in range(pos, len(h)):
    if h[j] == 0:
      pos2 = j
      break

  ans += min(h[pos:pos2])
  h[pos: pos2] = list(map(lambda x: x - min(h[pos:pos2]), h[pos:pos2]))
  
print(ans)

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

Solution (Python):
counter = 0
def myAnswer(H:list) -> int:
   global counter
   if len(H) is 0:return
   if len(H) is 1:
      counter = counter + H[0]
      return
   m = min(H)
   counter += m
   H = [i - m for i in H]
   while 0 in H:
      length = len(H[:H.index(0)])
      myAnswer(H[:H.index(0)])
      H = H[H.index(0)+1:]
   myAnswer(H)


def modelAnswer():
   return
def main():
   N = int(input())
   H = list(map(int,input().split()))
   myAnswer(H[:])
   print(counter)
if __name__ == '__main__':
   main()

Solution (Python):
















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

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

print(ans)

Solution (Python):









import numpy as np






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







def split_list(lis,num=0):
    res = []
    tmp = []
    for i in lis:
        if i != num:
            tmp.append(i)
        elif i == num and len(tmp)>0:
            res.append(tmp)
            tmp = []
        else:
            continue
    else:
        res.append(tmp)
    res = [l for l in res if len(l) != 0]
    return res

stack = split_list(arrA)
ans = 0

while len(stack)>0:
    lis1 = stack.pop()
    arr = np.array(list(map(int,list(lis1))))
    
    nmin = min(arr)
    ans += nmin
    arr = arr - nmin
    s = list(arr)
    lis2 = split_list(s)
    stack = stack + lis2

print(ans)

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

ans = 0
while True:
    l, r, = -1, n
    num = -1
    for i in range(n):
        if l == -1:
            if h[i] > 0:
                l = i
                num = h[i]
        else:
            if h[i] == 0:
                r = i
                break
            else:
                num = min(num, h[i])
    if l == -1:
        break

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

print(ans)

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

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

data = [0] + data

ans = 0
for i in range(n):
    dif = data[i + 1] - data[i]
    if (dif > 0):
        ans += dif
print(ans)

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

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

Solution (Python):
def I(): return int(input())
def LI(): return list(map(int,input().split()))
N = I()
h = LI()+[0]
ans = 0
while 1:
    if max(h)==0:
        break
    ans += 1
    for i in range(N+1):
        if h[i]>0 and h[i+1]==0:
            h[i] -= 1
            break
        h[i] = max(h[i]-1,0)
print(ans)

Solution (Python):
def I(): return int(input())
def LI(): return list(map(int,input().split()))
N = I()
h = LI()+[0]
ans = 0
while 1:
    if max(h)==0:
        break
    ans += 1
    for i in range(N+1):
        h[i] -= 1
        if h[i]>=0 and h[i+1]<=0:
            break
print(ans)

Solution (Python):
N=int(input())
H=list(map(int,input().split()))
m=0
ans=0
i=1
while i<N:
  while i<N and H[i]>=H[i-1]:
    i+=1
  ans+=H[i-1]-m
  while i<N and H[i]<=H[i-1]:
    i+=1
  m=H[i-1]
if N==1:
  print(H[0])
elif H[-1]>m:
  print(ans+H[-1]-m)
else:
  print(ans)

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
last = li[0]
count = li[0]

for i,l in enumerate(li):
    if i == 0:
        continue
    if l > last:
        count += l - last
    last = l
print(count)

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
all_li = [li]

def conv(target_list):
    count = 0
    flag = True
    while flag:
        all_tmp = []
        for each_list in target_list:
            tmp = []
            m = min(each_list)
            count += min(each_list)
            
            for e in each_list:
                if e-m != 0:
                    tmp.append(e-m)
                else:
                    if tmp:
                        all_tmp.append(tmp)
                        tmp = []
            if tmp:
                all_tmp.append(tmp)
        if len(all_tmp) == 0:
            flag = False
        target_list = all_tmp
    return count

print(conv(all_li))

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

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
maxh = max(h)
ans = 0
for j in range(maxh):
    countnow = False
    for i in range(n):
        if h[i] != 0:
            h[i] -= 1
            if not countnow:
                ans+=1
                countnow=True
        else:
            countnow = False
print(ans)

Solution (Python):


def solve():

    N = int(input())
    Hlist = list(map(int, input().split()))
    ans = 0

    while True:

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


if __name__ == "__main__":
    solve()

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

ans = 0
for i in range(1,max(h)+1):
  ls = [0]*N
  flag = 0
  
  for n in range(N):
    if h[n] >= i:
      ls[n] = 1
  
  for n in range(N):
    if ls[n] ==1:
      if flag == 0:
        ans += 1
        flag = 1
    else:
      flag = 0
print(ans)