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())
L = list(map(int, input().split()))
res = 0
tmp = [0 for _ in range(N)]

while True:
    
    flag = False
    co = 0
    for i in range(N):
        if L[i] != tmp[i] and flag == True:
            tmp[i] += 1
        elif L[i] != tmp[i] and flag == False:
            tmp[i] += 1
            flag = True
            res += 1
        elif L[i] == tmp[i]:
            flag = False
            
    for i in range(N):
        if L[i] == tmp[i]:
            co += 1
            
    if co == N:
        break
        
print(res)

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

h_diff = [x-y for x,y in zip(h[:-1], h[1:])]
h_diff_pos = [x for x in h_diff if x>0]
ans = sum(h_diff_pos) + h[-1]
print(ans)

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

res = 0
pre = 0
h.insert(0,0)

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

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




res = 0
pre = 0
for i in h:
    if i>pre:
        res += i-pre
    pre = i
else:
    print(res)

Solution (Python):
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

N = ri()
h = rl()

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

print(ans)

Solution (Python):






import numpy as np





N = int(input())
h = np.array([int(x) for x in input().split()])





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

print(ans)

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

Solution (Python):
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

N = ri()
h = rl()
hl = len(h)

ans = 0
flag = 0
for _ in range(sum(h)):
    if sum(h) == 0:
        break
    flag = 1
    for i in range(hl):
        if flag == 1 and h[i] != 0:
            flag = 0
            h[i] = h[i] - 1
            ans += 1
        elif flag == 0 and h[i] == 0:
            flag = 1
        elif flag == 0 and h[i] != 0:
            h[i] = h[i] - 1

print(ans)

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

def out(l):
    ll = bunkatu(l)
    
    res = 0
    
    for l in ll:
        
        
        l2 = [i - 1 for i in l]
        
        
        if not l2:
            continue
        
        if max(l2) > 0:
            res += 1 + out(l2)
        else:
            res += 1
        
    return res

def bunkatu(l):
    
    s = 0 
    ll = []
    
    for k, v in enumerate(l) :
        
        if v <= 0 :
            ll.append(l[s:k])
            s = k + 1
    
    else:
        if s != k + 1:
            ll.append(l[s:k + 1])
    return ll


print(out(l))

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

def garden(lst, count):
    if lst == [] or max(lst) == 0:
        return count
    elif 0 not in lst:
        lst = list(map(lambda a: a-1, lst))
        count += 1
        return garden(lst, count)
    elif 0 in lst:
        return count + garden(lst[:lst.index(0)], 0) + garden(lst[lst.index(0) + 1:], 0)

print(garden(h, 0))

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

def watering(lst, count):
  if lst == [] or max(lst) == 0:
    return count
  if min(lst) == 0:
    return count + watering(lst[:lst.index(0)], 0) + watering(lst[lst.index(0)+1:], 0)
  else:
    count += 1
    lst = list(map(lambda x:x-1, lst))
    return watering(lst, count)
print(watering(hlist, 0))

Solution (Python):

"""
oj dl https://atcoder.jp/contests/abc116/tasks/abc116_b -d test-b
oj test -d test-c -c "python abc116c.py"
oj test -d test-b -c "python abc116b.py" test-b/sample-3.in
"""

from collections import defaultdict
import sys
import math
import time

def deco(func):
    def wrapper(*args, **kwargs):
        s = time.time()
        
        ret = func(*args, **kwargs)
        logging.debug('--end in %f --' % (time.time() - s) )
        return ret
    return wrapper



def sol(S, n):
    cnt = 0
    ttl = sum(S)
    while ttl > 0:
        mark = False
        for i, s in enumerate(S):
            if s > 0:
                mark = True
                S[i]-=1
            elif mark and s == 0:
                break
        logging.debug("ttl:%s, cnt:%s, S:%s" % (ttl, cnt, S))
        cnt += 1
        ttl = sum(S)
    return cnt



import logging

logging.basicConfig(level=logging.ERROR, format="%(message)s")
do_submit = True

def input_parse(input_str):
    lines = [x.strip() for x in input_str.split("\n") if x.strip()]
    parsed_lines = [list(map(int, line.split())) for line in lines]
    n = parsed_lines[0][0]
    S = parsed_lines[1]
    return (n, S)


if not do_submit:
    
    
    
    
    n, S= input_parse("""
    5
    3 1 2 3 1
    """)
    print(sol(S, n))
else:
    
    
    n = int(input())
    S = list(map(int, input().split()))
    print(sol(S, n))

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

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

print(acc)

Solution (Python):


n=int(raw_input())
h=map(int,raw_input().split())
res=h[0]
for i in xrange(1,n):
 res+=max(h[i]-h[i-1],0)
print res

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




def lands(l):
    l += [0]
    lands = 0
    for i in range(len(l)):
        if l[i] == 0 and l[i-1] != 0:
            lands += 1
    return lands


def r(h, c):
    c += lands(h)
    
    h = list(map(lambda x: x - 1 if x != 0 else 0, h))
    if h == [0] * len(h):
        print(c)
    else:
        r(h, c)


r(h, 0)

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

Solution (Python):

import sys


def solve(N: int, h: "List[int]"):
    print(watering(h))
    return

def watering(h):
    if sum(h) == 0:
        return 0
    else:
        max_height = max(h)
        s, e = max_sub_sequence(h, max_height)
        assert s >= 0 and e < len(h), "invalid s: {} & e: {}".format(s, e)
        if s == 0 and e == len(h)-1:
            return max_height
        elif s == 0:
            diff = max_height - h[e+1]
            h[s:e+1] = [h[e+1]] * (e+1-s)
            return watering(h) + diff
        elif e == len(h)-1:
            diff = max_height - h[s-1]
            h[s:e+1] = [h[s-1]] * (e+1-s)
            return watering(h) + diff
        else:
            if h[s-1] < h[e+1]:
                diff = max_height - h[e+1]
                h[s:e+1] = [h[e+1]] * (e+1-s)
                return watering(h) + diff
            else:
                diff = max_height - h[s-1]
                h[s:e+1] = [h[s-1]] * (e+1-s)
                return watering(h) + diff

def max_sub_sequence(h, max_height):
    i = 0
    j = 0
    while i < len(h):
        if h[i] == max_height:
            j = i + 1
            while j < len(h) and h[j] == max_height:
                j += 1
            j -= 1
            break
        i += 1
    return i, j

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

if __name__ == '__main__':
    main()

Solution (Python):

n = int(input())

arr = list(map(int,input().strip().split()))
s = []
a = []
arr.append(0)
for i in range(n+1):
    if arr[i]==0:
        if len(a) > 0:
            s.append(a)
            a = []
    else:
        a.append(arr[i])
ans = 0


while len(s)!=0:
    
    d = []
    
    for i in range(len(s)):
        x = min(s[i])
        s[i] = [ s[i][j]-x for j in range(len(s[i])) ]
        a = []
        e = []
        ans+=x
        s[i].append(0)
        for j in range(len(s[i])):
            if s[i][j]==0:
                if len(a) > 0:
                    d.append(a)
                    a = []
            else:
                a.append(s[i][j])
        
    s = d
print(ans)

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

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

count = 0
prev = 0

for h in hn:
    count += max(0, h-prev)
    prev = h

print(count)

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

f = False
c = 0

for k in range(0, 100):
    for i in range(0, n):
        if f:
            if k >= h[i]:
                f = False
                c += 1
        else:
            if k < h[i]:
                f = True
    if f:
        f = False
        c += 1

print(c)

Solution (Python):
N = int(input())
L = list(map(int,input().split()))
L.append(0)
K = [0]*(N+1)
cnt = 0
c = 0
flag = True
while flag:
  if K == L:
    flag = False
  else:
    for i in range(N+1):
      if K[i] < L[i]:
        K[i] += 1
        cnt += 1
      else:
        if cnt != 0:
          c += 1
          cnt = 0
print(c)

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

water_times=0
while max(H)>0:
  left_water=0
  for h in H:
    if h>0 and left_water==0:
      water_times+=1
      left_water=1
    elif h>0 and left_water==1:
      pass
    else:
      left_water=0
  H=[h-1 for h in H]

print(water_times)

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

for t in range(len(list)):
    list[t] = int(list[t])

cnt = 0

for idx in range(n):
    
    
    for t in range(101):
        if 0 != list[idx]:
            
            idx2 = n
            t_cnt = min(list[idx:n])
            
            
            
            if t_cnt == 0:
                idx2 = list[idx:n].index(0) + idx
                t_cnt = min(list[idx:idx2])

            for idx3 in range(idx,idx2):
                list[idx3] = list[idx3] - t_cnt
            cnt += t_cnt
        else:
            break
        
print(cnt)

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

for t in range(len(list)):
    list[t] = int(list[t])

cnt = 0

for idx in range(n):
    for t in range(101):
        if 0 != list[idx]:
            t_cnt = min(list[idx:n])
            idx2 = n
            if t_cnt == 0:
                idx2 = list[idx:n].index(0) + idx
                t_cnt = min(list[idx:idx2])
            for idx3 in range(idx,idx2):
                list[idx3] = list[idx3] - t_cnt
            cnt += t_cnt
        else:
            break
        
print(cnt)

Solution (Python):
def main():
    N = int(input())
    hList = list(map(int, input().split()))
    cnt = 0
    hMax = max(hList)
    isRange = False
    for h in range(1, hMax+1):
        for j in range(N):
            if not isRange and hList[j] >= h:
                isRange = True
            if isRange and hList[j] < h:
                isRange = False
                cnt += 1
        if isRange:
            cnt += 1
        isRange = False
    print(cnt)
            

if __name__ == '__main__':
    main()

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

def grouping(h_list):
    groups = []
    group = []
    for h in h_list:
        if h == 0:
            if group != []:
                groups.append(group)
            group = []
            continue
        group.append(h)
    if group:
        groups.append(group)
    return groups


sum_mizuyari = []
def groups_mizuyari(h_list):
    num_mizuyari = min(h_list)
    h_list = [h - num_mizuyari  for h in h_list]
    sum_mizuyari.append(num_mizuyari)
    groups = grouping(h_list)
    for group in groups:
        groups_mizuyari(group)

groups_mizuyari(h_list)
print(sum(sum_mizuyari))

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


def mizuyari(h_list, l, r):
    num_mizuyari = min(h_list[l:r+1])
    h_list = [h - num_mizuyari if l <= x and x <= r else h for x, h in enumerate(h_list)]
    zero_posi = h_list[l:].index(0) + l
    if l == zero_posi:
        first_non_zero_posi = [x for x, h in enumerate(h_list) if h != 0]
        if not first_non_zero_posi:
            print(sum_mizuyari + num_mizuyari)
            exit()
        l = first_non_zero_posi[0]
        if list(range(l, len(h_list))) == first_non_zero_posi:
            return h_list, num_mizuyari, l, len(h_list)
        zero_posi = h_list[l:].index(0) + l
    new_r = zero_posi - 1
    return h_list, num_mizuyari, l, new_r
l = 0
r = len(h_list) - 1
sum_mizuyari = 0
while True:
    h_list, num_mizuyari, l, r = mizuyari(h_list, l, r)
    sum_mizuyari += num_mizuyari

Solution (Python):


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




ans = 0
index = 0

while True:
    
    
    if max(h) == 0:
        break

    
    if h[index] == 0:
        index += 1
        continue
    
    
    ushiro = h[index:]
    for i in range(len(ushiro)):
        if h[index+i] != 0:
            h[index+i] -= 1
        else:
            break
    ans += 1

print(ans)

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

cnt = 0
mem = 0
for i in h:
    if i >= mem:
        cnt += i - mem
        mem = i
    else:
        mem = i
print(cnt)

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

ans = 0
for i in range(max(h)):
  cnt = 0
  for j in range(N):
    h[j] = h[j] - 1
    if h[j] >= 0 and (j == 0 or h[j - 1] < 0):
      cnt += 1
  ans += cnt
print(ans)

Solution (Python):
def main(n):
	h = list(map(int, input().split())) + [0]
	ans = 0
	f, e = 0, 1
	while f != n:
		if h[f] == 0:
			f += 1
			if e == f:
				e += 1
		elif e == n or h[e] == 0:
			temp = min(h[f:e])
			for i in range(f, e):
				h[i] -= temp
			ans += temp
			e = f+1
		else:
			e += 1
	print(ans)
	return

main(int(input()))

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

Solution (Python):

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

for x in h:
    result += max(x-pre, 0)
    pre = x
print(result)

Solution (Python):
ans = 0

def mizuyari( l ):
    if len( l ) == 0: return 
    
    minimum = min( l )
    min_i = l.index( minimum )
    l = list( map(lambda x: x - minimum, l) )
    global ans  
    ans += minimum
    
    mizuyari( l[:min_i] )
    mizuyari( l[min_i+1:] )


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

mizuyari( h )
print( ans )

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

def f(x):
    if len(x) == 1:
        return x[0]
    else:
        xmin = min(y for y in x)
        minindex = x.index(xmin)
        if minindex == 0 :
            return f(x[1:])
        elif minindex == len(x)-1:
            return f(x[:len(x)-1])
        else:
            return (f(x[:minindex]) + f(x[minindex+1:]) -xmin)

print(f(H))

Solution (Python):
s = int(input())
a = list(map(int, input().split()))

incFlg = True   
before_height = 0
sum = 0

for i in range(len(a)-1):
    
    if a[i] > a[i+1]:
        
        if incFlg:
            sum += a[i] - before_height
            incFlg = False
        
        
        
    
    else:
        if not incFlg:
            before_height = a[i]
        incFlg = True

if incFlg:
    sum += a[len(a)-1] - before_height

print(sum)

Solution (Python):
s = int(input())
a = list(map(int, input().split()))

incFlg = True   
before_height = 0
sum = 0

for i in range(len(a)-1):
    
    if a[i] > a[i+1]:
        
        if incFlg:
            sum += a[i] - before_height
            incFlg = False
        
        else:
            before_height = a[i]
    
    else:
        if not incFlg:
            before_height = a[i]
        incFlg = True

if incFlg:
    sum += a[len(a)-1] - before_height

print(sum)

Solution (Python):
def water(n,h):
    start = 0
    tempmin = 1000
    counter = 0
    for i in range(n):
        if h[i] == 0 or i == n-1:
            if i == n-1 and h[i] != 0:
                if h[i] < tempmin:
                    tempmin = h[i]
                i += 1
            check = False 
            for k in range(start,i):
                h[k] -= tempmin
                check = True
            if check:
                counter += tempmin
            start = i + 1
            tempmin = 1000
        else:
            if h[i] < tempmin:
                tempmin = h[i]
    return h,counter

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

counter = 0
while max(h) != 0:
    h,c = water(n,h)
    counter += c
print(counter)

Solution (Python):





n = int(input())
flower_height = list(map(int, input().split()))
count=0
def mizuyari(a):
    return a-1




for i in range(n):
    j=i
    while flower_height[i]!=0:
        while i < n-1:
            if flower_height[i+1] != 0: i+=1
            else: break
        flower_height = list(map(mizuyari,flower_height[:i+1])) + flower_height[i+1:]
        i=j
        count += 1
    
print(count)

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


def mizuyari(*f):
    if len(f) == 0:
        return
    min_i = f.index(min(f))
    if f[min_i] != 0:
        global ans
        ans += f[min_i]
        f = [i - f[min_i] for i in f]
    if f[min_i] == 0:
        mizuyari(*f[:min_i])
        mizuyari(*f[min_i + 1:])


l = []
for n in range(N):
    l.append(0)
mizuyari(*H)
print(ans)

Solution (Python):
def calc_watering_num(h, offset, left, right):
        if left > right:
                return 0
        min_h = None
        min_h_indices = []
        for i in range(left, right + 1):
                if min_h is None or min_h > h[i] - offset:
                        min_h = h[i] - offset
                        min_h_indices = [i]
                elif min_h == h[i] - offset:
                        min_h_indices.append(i)
        if len(min_h_indices) == right - left + 1:
                return min_h
        num = min_h
        prev = left
        for i in min_h_indices:
                num += calc_watering_num(h, offset + min_h, prev, i - 1)
                prev = i + 1
        num += calc_watering_num(h, offset + min_h, prev, right)
        return num


input()
h = [int(v) for v in input().split()]
print(calc_watering_num(h, 0, 0, len(h) - 1))

Solution (Python):


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

count = 0

while max(h) > 0:
    is_pouring = False
    for i in range(N):
        if h[i] > 0:
            h[i] -= 1
            if not is_pouring:
                count += 1
                is_pouring = True
        else:  
            is_pouring = False
    

print(count)

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

Solution (Python):
n = int(raw_input())
h = raw_input()
h = [int(i) for i in h.split(" ")]
flowers = [0 for i in h]


def is_finished(flowers, h):
	for i in range(len(flowers)):
		if flowers[i]!=h[i]:
			return False
	return True

c = 0
while is_finished(flowers, h)==False:
	i = 0
	while i<len(flowers):
		if flowers[i]<h[i]:
			break
		i += 1
	l = i
	while i<len(flowers):
		if flowers[i]>=h[i]:
			break
		i += 1
	r = i-1

	for i in range(l,r+1):
		flowers[i] += 1
	c += 1
print(c)

Solution (Python):
n = map(int,input().split())
data = list(map(int,input().split()))
res = 0
data.append(0)
while(max(data)!=0):
    pre_zero = -1
    for idx in range(len(data)):
        if(data[idx] == 0):
            if(idx == pre_zero+1):
                pre_zero = idx
                continue
            min_value = min(data[pre_zero+1:idx])
            for x in range(pre_zero+1,idx):
                data[x] -= min_value
            res += min_value
            pre_zero = idx
print(res)

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

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

Solution (Python):
def solve():
	N = int(input())
	h = [int(i) for i in input().split()]
	ans = 0
	for i in range(0, N):
		ans += max(h[i] - (0 if i - 1 < 0 else h[i - 1]), 0)
	return ans

print(solve())

Solution (Python):

import numpy as np

class MyClass:
    def __init__(self):
        self.sum = 0

    def water(self, H):
        if len(H)<=0:
            return
        min_h = np.min(H)
        self.sum += min_h
        H -= min_h
        on = False
        for i in range(len(H)):
            if not on:
                if H[i]>0:
                    st = i
                    on = True
                else:
                    continue
            else:
                if H[i]>0:
                    continue
                else:
                    ed = i
                    on = False
                    self.water(H[st:ed])
        if on:
            self.water(H[st:])
        return


def main():
    N = int(input())
    H = np.array([int(x) for x in input().split()]) 
    my_class = MyClass()
    my_class.water(H)
    print(my_class.sum)

if __name__=='__main__':
    main()

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

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

peak = []
trough = []
h = [0] + h + [0]
before = 0
for a, b in zip(h[:-1], h[1:]):
    after = b - a
    if after == 0:
        continue
    elif before > 0 > after:
        peak.append(a)
    elif before < 0 < after:
        trough.append(a)
    before = after

print(sum(peak) - sum(trough))

Solution (Python):
N = int(input())
hin = input().split(" ")

hls = []
count = 0
for i in range(0,N):
    hls.append(int(hin[i]))

def water(l,r):
    if l == r:
        return -1
    for i in range(l,r):
        hls[i] = hls[i] - 1
    global count
    count = count + 1

def all0():
    for i in range(0,N):
        if hls[i] != 0:
            return False
    return True
        
while True:
    nowNode = 0
    endNode = 0

    while endNode < N:
        while hls[endNode] != 0:
            endNode = endNode + 1
            if endNode == N:
                break
        water(nowNode,endNode)
        endNode = endNode + 1
        nowNode = endNode
        if all0():
            break

    if all0():
        break

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

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

while 1:
  for i in range(n):
    if A[i]>0:
      A[i]-=1
      if A[i+1]==0:
        ans+=1
  if not A==[0 for i in range(n+1)]:
    continue
  break
print(ans)

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

count=0
while 1:
  flag=0
  for i in range(N):
    if h[i]>0:
      h[i] -= 1
      flag = 1
      if h[i+1] == 0:
        count += 1
      else:
        pass
  if flag == 0:
    break
print(count)

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

maximum = max(heights)

splits = 0
for h in range(maximum, 0, -1):
    before = None
    sp = 0
    for i, _ in enumerate(heights):
        if heights[i] == h:
            if before != i - 1:
                sp += 1
            before = i
            heights[i] -= 1
    
    splits += sp

print(splits)

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
i = 0
start = 0
end = 0
ans = 0

if h.count(0) == n:
    print(0)
else:
    while True:
        i = 0
        for y in range(n):
            if h[y] != 0:
                start = y
                break
        for x in range(start,n):
            if h[x] != 0:
                h[x] -= 1
                if x == n-1:
                    ans += 1
            else:
                start = 0
                i = 0
                ans += 1
                break
        if h.count(0) == n:
            break
    print(ans)

Solution (Python):




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

    s = [0]
    ans = 0
    for i in range(N):
        if len(s) and s[-1] <= H[i]:
            ans += H[i] - s[-1]
        else:
            while len(s) and s[-1] >= H[i]:
                s.pop()
        s.append(H[i])

    print(ans)


if __name__ == '__main__':
    main()

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

ans = 0

for i in range(101):
    for j in range(n):
        if h[j] <= i and h[j + 1] > i:
            ans += 1

print(ans)

Solution (Python):
def main(condition, number_of_trials):
    while True:
        min_value = min(condition)
        condition = list(map(lambda x: x - min_value, condition))
        condition = remove_neighboring_zeros(condition)
        number_of_trials += min_value
        if condition == []:
            break
        if 0 not in condition:
            continue
        divided_list = divide_list_by_specified_value(condition, 0)
        for each_part_condition in divided_list:
            number_of_trials = main(each_part_condition, number_of_trials)
        break
    return number_of_trials


def divide_list_by_specified_value(list_to_be_divided, divider):
    before_idx = 0
    divided_list = []
    for idx, each_elem in enumerate(list_to_be_divided):
        if each_elem == divider:
            if list_to_be_divided[before_idx:idx] != []:
                divided_list.append(list_to_be_divided[before_idx:idx])
            before_idx = idx + 1
    if list_to_be_divided[before_idx:] != []:
        divided_list.append(list_to_be_divided[before_idx:])
    return divided_list


def remove_neighboring_zeros(array):
    while len(array) > 0 and array[-1] == 0:
        array.pop(len(array) - 1)
    while len(array) > 0 and array[0] == 0:
        array.pop(0)
    return [each_elem for idx, each_elem in enumerate(array) if
            idx == 0 or each_elem != array[idx - 1] or each_elem != 0]


if __name__ == '__main__':
    n = int(input())
    condition = list(map(int, input().split()))
    print(main(condition, 0))

Solution (Python):
n=int(input())
h=list(map(int,input().split(" ")))
h.append(0)
d=[]
for i in range(n):
    d.append(h[i+1]-h[i])
c=0
for j in range(len(d)):
    k=d[j]
    if k < 0:
        c-=k
print(c)

Solution (Python):
from sys import stderr, setrecursionlimit
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, **kwargs):
    print(*args, file=stderr, **kwargs)

n = getInt()
h = getInts()
ans = 0
l = 0
r = 0
while l < n:
    r = l
    while l < n and h[l] == 0:
        l += 1
        r += 1
    while r < n and h[r] != 0:
        r += 1
    if r == l:
        break
    tmp = min(h[l:r])
    ans += tmp
    h = h[:l] + list(map(lambda x:x-tmp, h[l:r])) + h[r:]
    debug(l, r, h)

print(ans)

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

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

def nextgen(comb):
    lis = comb[0]
    j = comb[1]
    if len(lis) == 1 and lis[0] != 0:
        j += 1  
    for i in range(len(lis)-1):
        if (i != len(lis) and (lis[i] == 0 and lis[i+1] != 0)) or (i == 0 and lis[i] != 0):
            j += 1
        if lis[i] != 0:
            lis[i] -= 1
    if lis[len(lis)-1] != 0:
        lis[len(lis)-1] -= 1
    return [lis, j]

comb = [heights, 0]
while (comb[0].count(0) != len(comb[0])):
    comb = nextgen(comb)

print(comb[1])

Solution (Python):
import numpy as np

n = int(input())
flower = [int(part) for part in input().split()]
flower = np.array(flower)

count = 0
highest = max(flower)
for h in range(1, highest+1):
    signal = (flower >= h)
    
    
    pre = False
    for now in signal:
        
        if not pre and now:
            count += 1
        pre = now
print(count)

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

def f(li):
  global ans
  
  
  if li==[]:
    del li
    return
  
  
  min_num=min(li)
  if min(li)>0:
    ans+=min(li)
    li=list(map(lambda x: x-min(li), li))
  li=[li[:li.index(0)],li[li.index(0)+1:]]
  f(li[0])
  f(li[1])

f(h)
print(ans)

Solution (Python):


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

count = 0

def f(l):
    global count

    
    if l == []:
        del l
        return

    
    min_num = min(l)
    if min(l) > 0:
        count += min(l)
        l = list(map(lambda x: x-min(l), l))
    min_list = [i for i, x in enumerate(h) if x == min(h)]
    l = [l[:l.index(0)], l[l.index(0)+1:]]
    f(l[0])
    f(l[1])

f(h)
print(count)

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

ans=0
for i in s:
  if i>0:
    ans+=i
print(ans)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
l_set = list(set(l))

def func(l,k):
    ans = min(l)-k
    l_i = [i for i, x in enumerate(l) if x == min(l)]
    l_split = []
    for i in range(len(l_i)+1):
        if i==0:
            l_ = l[:l_i[i]]
            if(l_):
                l_split.append(l_)
        elif i==len(l_i):
            l_ = l[l_i[i-1]+1:]
            if(l_):
                l_split.append(l_)
        else:
            l_ = l[l_i[i-1]+1:l_i[i]]
            if(l_):
                l_split.append(l_)
    k = min(l)
    for l_ in l_split:
        ans += func(l_,k)
    return ans

ans = func(l,0)
print(ans)

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

count = 0
for i in range(maxh):
    flag = False
    for j in range(N):
        if h[j] > 0:
            flag = True
        if h[j] == 0 and flag:
            count += 1
            flag = False
        elif j == N-1 and flag:
            count += 1
    h = [x-1 if x >= 1 else 0 for x in h]
print(count)