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

ans = 0
for i in range(1, max(h) + 1):
    mode = 0
    for j in range(n):
        if mode == 0:
            if h[j] < i:
                continue
            else:
                mode = 1
                ans += 1
        else:
            if h[j] >= i:
                continue
            else:
                mode = 0

print(ans)

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

hmax = max(h)
count = 0
for i in range(hmax):
	flag = 0
	for j in h:
		if i < j:
			if flag == 1:
				continue
			else:
				count += 1
				flag = 1
		else:
			flag = 0
print(count)

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

Solution (Python):
num = int(input())
targets = map(int, input().split())
targets = list(targets)


global water_cnt
water_cnt = 0
def exe_water(init_state):
    global water_cnt
    
    if sum(init_state) == 0:
        return
    temp_water_cnt = min(init_state)
    water_cnt += temp_water_cnt

    lowest = temp_water_cnt
    
    sub_state = []
    for h in init_state:
        substracted_h = h - lowest
        if substracted_h != 0:
            sub_state.append(substracted_h)
        else:
            exe_water(sub_state)
            
            sub_state = []
    
    exe_water(sub_state)

exe_water(targets)
print(water_cnt)

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

for _ in range(N):
	matrix.append([])

for index, i in enumerate(l):
	for _ in range(i):
		matrix[index].append(1)
	while len(matrix[index]) != max(l):
		matrix[index].append(0)

def trans(A:list) -> list:
    ans = []
    for col in range(len(A[0])):
        l = []
        for line in range(len(A)):
            l.append(A[line][col])
        ans.append(l)
    return ans    

ll = trans(matrix)
count = 0
for line in reversed(ll):
	mark = False
	for index, i in enumerate(line):
		if i == 1 and mark == False:
			count += 1
			line[index] = 0
			mark = True
		
		elif i == 1 and mark == True:
			line[index] = 0

		elif i == 0:
			mark = False
print(count)

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
mlist = []
hmax = max(H)
count = 0
divide = 1
for i in range(hmax+1):
    for n,k in enumerate(H):
        if k >= i:
            mlist.append(n)
            
    for p in range(len(mlist)-1):
        if mlist[p + 1] - mlist[p] != 1:
            divide+=1
    count+=divide
    divide = 1
    mlist.clear()
print(count-1)

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

ans = 0
while np.max(H) != 0:

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

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

while len(H) :
    if not H[0] :
        H.pop(0)
    else :
        ans += 1
        for i in range(len(H)) :
            if H[i] :
                H[i] -= 1
            else :
                break
print(ans)

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

Solution (Python):
N, *H = map(int, open(0).read().split())
cnt = 0
prv = 0
for h in H:
    cnt += max(0, h - prv)
    prv = h
print(cnt)

Solution (Python):
N, *H = map(int, open(0).read().split())
pt = 0
ans = 0
while pt < N:
    cur = pt
    flag = 0
    while cur < N and H[cur]:
        H[cur] -= 1
        cur += 1
        flag = 1
    while pt < N and H[pt] == 0:
        pt += 1
    ans += flag
print(ans)

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

ans = 0
m = 0
for h in H:
    ans += max(h-m, 0)
    m = h
print(ans)

Solution (Python):
n=int(input())
l=[int(i) for i in input().split()]
s=sum(l)
res=0
while s>0:
  res+=1
  i=0;j=0
  for i in range(n):
    if l[i]>0:
      l[i]-=1
      s-=1
      break
  for j in range(i+1,n):
    if l[j]==0:
      break
    else:
      l[j]-=1
      s-=1
print(res)

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

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

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

Solution (Python):
def main() -> None:
    n = int(input())
    h = [int(x) for x in input().split()]
    cnt = 0

    while len(h) > 0:
        i = len(h) - 1
        flg = False
        while i >= 0 and h[i] > 0:
            h[i] -= 1
            i -= 1
            flg = True

        if flg:
            cnt += 1
        if h[-1] == 0:
            h.pop(-1)

    print(cnt)


if __name__ == "__main__":
    main()

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

ans = 0
MAX = max(H)
for h in range(MAX):
    kukan = False
    for i in range(N):
        if H[i] == 0:
            if kukan:
                ans += 1
                kukan = False
        else:
            H[i] -= 1
            kukan = True
    if kukan:ans+=1
    
            
print(ans)

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

ans = 0

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

Solution (Python):

n = int(input())
h = list(map(int, input().split()))
def lr(h):
  start = -1
  last = n
  for i, num in enumerate(h):
    if num != 0:
      start = i
      break
  for i, num in enumerate(h):
    if num == 0 and i > start:
      last = i
      break
  return start, last

def water(l, r, h):
  for i in range(l, r):
    h[i] -= 1
  return h

times = 0

while True:
  l, r = lr(h)
  if l == -1:
    break
  h = water(l, r, h)
  times += 1

print(times)

Solution (Python):
N=int(input())
x=[int(i) for i in input().split()]
x.append(0)
cnt=0
while x.count(0)!=N+1:
    for i in range(N):
        if x[i+1]==0 and x[i]>0:
            cnt+=1
        if x[i]>0:
            x[i]-=1
print(cnt)

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

print(ans)

Solution (Python):
def solve(l):
    if len(l)<2:
        return max(0,l[0])
    m=10**18
    a=0
    for i in range(len(l)):
        if l[i]<1:
            if i+1<len(l):
                a+=solve(l[i+1:])
            else:
                l=l[:i]
            break
        m=min(m,l[i])
    l=l[:i+1]
    for i in range(len(l)):
        l[i]-=m
    return solve(l)+a+m*(m!=10**18)

n=int(input())
*h,=map(int,input().split())
print(solve(h))

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

Solution (Python):
import numpy as np

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

split_h = []
split_h.append(h)

ans = 0
list_h = []
while len(split_h):
    sph = split_h.pop()

    min_h = sph.min()
    sph -= min_h
    ans += min_h

    if np.all(sph == 0):
        continue

    for elem in sph:
        if elem != 0:
            list_h.append(elem)
        else:
            if list_h:
                split_h.append(np.array(list_h))
                list_h = []
    if list_h:
        split_h.append(np.array(list_h))
        list_h = []

print(ans)

Solution (Python):
import numpy as np

def my_index(l, x):
    return [i for i, _x in enumerate(l) if _x == x]

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

split_h = []
split_h.append(h)

ans = 0
list_h = []
while len(split_h):
    sph = split_h.pop()

    min_h = sph.min()
    sph -= min_h
    ans += min_h

    if np.all(sph == 0):
        continue

    for elem in sph:
        if elem != 0:
            list_h.append(elem)
        else:
            if list_h:
                split_h.append(np.array(list_h))
                list_h = []
    if list_h:
        split_h.append(np.array(list_h))
        list_h = []

print(ans)

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


def count(A):
    minimum = min(A)
    stack = [[]]
    for num in A:
        if num > minimum:
            stack[-1] += [num - minimum]
        else:
            stack += [[]]
    return minimum + sum([count(array) for array in stack if array != []])


print(count(h))

Solution (Python):



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

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def spaceinput():
    return list(map(int,input().split(" ")))


def search_right(l):
    global res,N,h
    minv=h[l]
    for j in range(l,N):

        if h[j]==0:
            minv=np.min(h[l:j])

            h[l:j]-=minv

            res+=minv
            return
        elif j==N-1:

            minv=np.min(h[l:N])
            h[l:N]-=minv
            res+=minv
            return





import numpy as np
N=int(input())
h=np.array(spaceinput())
res=0

while sum(h)>0:
    
    for i in range(N):
        if h[i]>0:
            search_right(i)
            break


print(res)

Solution (Python):
N=int(input())
x=[int(i) for i in input().split()]
x.append(0)
cnt=0
while x.count(0)!=N+1:
    for i in range(N):
        if x[i+1]==0 and x[i]>0:
            cnt+=1
        if x[i]>0:
            x[i]-=1
print(cnt)

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

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

initial_gr_list = [h]

num = 0
def func(gr_list):
    global num
    n_gr_list = [] 
    n2_gr_list = [] 
    
    num_ima = int(num) 
    
    
    for gr in gr_list:
        if gr == []:
            pass
        else:  
            n_gr = []
            
            for i, x in enumerate(gr):
                
                if x == 0:
                    n_gr_list.append(n_gr) 
                    n_gr = [] 
                elif i == len(gr)-1: 
                    n_gr.append(x)  
                    n_gr_list.append(n_gr)  
                    n_gr = [] 
                else:
                    n_gr.append(x) 
    
    
    
    
    for n_gr in n_gr_list:
        if n_gr ==[]:
            pass
        else:
            gm = min(n_gr)
            num += gm  
            n2_gr = [max(x - gm, 0) for x in n_gr]
            n2_gr_list.append(n2_gr)
            
            
    
    

    if sum(sum(x) for x in n2_gr_list) == 0: 
        return
    else:
        func(n2_gr_list) 

func(initial_gr_list)

print(num)

Solution (Python):


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

def all_under0(data):
  res = 1
  for d in data:
    if d > 0:
      res = 0
      break
  return res

def count_0(data):
  res = 0
  if data[0] > 0:
    res += 1
  for i in range(1,len(data)):
    if data[i] > 0 and data[i-1] <= 0:
      res += 1
  return res

ans = 0
while all_under0(h) == 0:
  ans += count_0(h)
  for i in range(len(h)):
    h[i] -= 1

print(ans)

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

while any(h):
  l,r=0,0
  while l<n and not h[l]:
    l+=1
  r=l
  while r<n and h[r]:
    r+=1
  v=min(h[l:r])
  for i in range(l,r):
    h[i]-=v
  ans+=v

print(ans)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
b = []
i = 0
while sum(a) != 0:
    for j in range(len(a)):
        if a[j] !=0:
            b.append(j)
            a[j] -= 1
    for k in range(len(b)-1):
        if b[k+1]-b[k] != 1:
            i += 1
    i += 1
    b = []
print(i)

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

	hm = max(h)
	count = 0
	for i in range(hm):
		f = False
		for j in h:
			if i < j:
				if f:
					continue
				else:
					count += 1
					f = True
			else:
				f = False

	print(count)


if __name__ == '__main__':
	main()

Solution (Python):
n=int(input())
hl=list(map(int,input().split()))
c=0
mi=min(hl)
c+=mi
hdebug=hl[:]
hdebug.append(0)
if mi!=0:
  for i in range(len(hl)):
    hl[i]-=mi
hl.append(0)
for x in range(100):
  a=101
  for i in range(len(hl)):
    if hl[i]!=0 and a>hl[i] and a==101:
      a=i
    if hl[i]==0 and a<i:
      mi2=min(hl[a:i])
      c+=mi2
      for j in range(a,i):
        hl[j]-=mi2
print(c)

Solution (Python):
""" AtCoder """

import sys
input = sys.stdin.readline

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

def f(h):
    if h == []:
        return 0
    elif len(h) == 1:
        return h[0]
    elif 0 in h:
        return f(h[:h.index(0)]) + f(h[h.index(0)+1:])
    else:
        min_h = min(h)
        for i in range(len(h)):
            h[i] -= min_h
        return f(h[:h.index(0)]) + f(h[h.index(0)+1:]) + min_h

print(f(h))

Solution (Python):
def solve(l):
    if len(l)<2:
        return max(0,l[0])
    m=10**18
    a=0
    for i in range(len(l)):
        if l[i]<1:
            if i+1<len(l):
                a+=solve(l[i+1:])
            else:
                l=l[:i]
            break
        m=min(m,l[i])
    l=l[:i+1]
    for i in range(len(l)):
        l[i]-=m
    return solve(l)+a+m*(m!=10**18)

n=int(input())
*h,=map(int,input().split())
print(solve(h))

Solution (Python):
import sys
from collections import deque

import numpy as np

sys.setrecursionlimit(200000)


def input():
    return sys.stdin.readline()[:-1]


def ii(t: type = int):
    return t(input())


def il(t: type = int):
    return list(map(t, input().split()))


def imi(N: int, t: type = int):
    return [ii(t) for _ in range(N)]


def iml(N: int, t: type = int):
    return [il(t) for _ in range(N)]


def solve():
    N = ii()
    H = il()
    c = 0
    queue = deque([H])
    while queue:
        line = queue.popleft()
        a = []
        for x in line:
            if x < 0:
                continue
            if x != 0:
                a.append(x - 1)
            elif a:
                queue.append(a)
                a = []
        if a:
            queue.append(a)
        c += 1
    return c - 1


if __name__ == "__main__":
    print(solve())

Solution (Python):
from sys import stdin

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

    h = H[:]
    ans = 0
    while any(h):
        flag = 0
        for i, n in enumerate(h):
            if n > 0:
                h[i] -= 1
            else:
                flag = 0
                continue
            if not flag:
                flag = 1
                ans += 1
    print(ans)

input = lambda: stdin.readline()
main()

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

def dfs(L):
    for i,l in enumerate(L):
        if l != 0:
            break
    L = L[i:N]
    s = 0
    res = 0
    for i,l in enumerate(L):
        if l == 0:
            L_ = [l_-1 for l_ in L[s:i]]
            s = i+1
            if len(L_) != 0:
                res += 1
                res += dfs(L_)
        elif i == len(L)-1:
            L_ = [l_-1 for l_ in L[s:len(L)]]
            s = i+1
            if len(L_) != 0:
                res += 1
                res += dfs(L_)
    return res

if sum(h) == 0:
    print(0)
else:
    print(dfs(h))

Solution (Python):

def inpl(): return list(map(int, input().split()))
N = int(input())
F = [0]*N
H = inpl()
ans = 0
while True:
    for l in range(N):
        if F[l] < H[l]:
            for r in range(l, N):
                if F[r] < H[r]:
                    continue
                break
            else:
                r += 1
            if r > N:
                r = N
            for j in range(l, r):
                F[j] += 1
            ans += 1
            break
    else:
        break

print(ans)

Solution (Python):
def clean(hh):
    while hh:
        if hh[0]==0:
            del hh[0]
        else:
            break

def apply(hh):
    dh = min(hh)
    return (dh, [h-dh for h in hh])

def solve(hh):
    clean(hh)
    if not hh:
        return 0
    ans = 0
    try:
        idx = hh.index(0)
        ans = solve(hh[idx+1::])
    except ValueError:
        idx = len(hh)
    lhh = hh[:idx]
    dh, lhh = apply(lhh)
    dh += ans
    return dh + solve(lhh)



def main():
    N = int(input())
    hh = list(map(int, input().split()))
    print(solve(hh))



if __name__ == '__main__':
    main()

Solution (Python):
def water(list_a):
    if list_a == [0] * len(list_a):
        return 0
    
    elif 0 in list_a:
        n = list_a.index(0)
        return water(list_a[:n]) + water(list_a[n+1:])

    else:
        mini = min(list_a)
        for i in range(len(list_a)):
            list_a[i] -= mini
        return water(list_a) + mini

n = int(input())
list_h = list(map(int, input().split()))
print(water(list_h))

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

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

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
if h == [0]*N:
    print(0)
    exit()
for i in range(1,10001):
    f = False
    for j in range(N):
        if h[j] > 0:
            h[j] -= 1
            f = True
        elif h[j] <= 0 and f:
            break
    if h == [0]*N:
        ans = i
        break
print(ans)

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

base = [0] * len(list)

result = 0
flag = False
exit = False

while not exit:
  for i, value in enumerate(list):
    if base[i] < value:
      if flag is False:
        flag = True
        result += 1
      base[i] += 1
    else:
      if flag is True:
        flag = False
    if base == list:
      exit = True
  flag = False

print(result)

Solution (Python):
import numpy as np
N = int(input())
h = list(map(int, input().split()))
diff = np.array([i for i in h])




def find_min_idx(l):
    
    non_0_list = [i for i in l if i != 0]
    min_value = min(non_0_list)
    min_idx = np.where(l == min_value)[0][0]
    return min_idx


def get_min_value(l, min_idx):
    return l[min_idx]


def get_end_idx(l, min_idx, min_value):
    for idx, i in enumerate(l):
        if idx > min_idx:
            if i == 0:
                return idx - 1
            if i < min_value or i == 0:
                return idx - 1
    else:
        return len(l) - 1


def find_start_idx(l, min_idx):
    if len(l[:min_idx]) == 0:
        return 0
    for idx, i in enumerate(reversed(l[:min_idx])):
        if i == 0:
            return idx
    else:
        return len(l[:min_idx])-1


count = 0
while not all([i == 0 for i in diff]):
    
    
    
    min_idx = find_min_idx(diff)
    min_value = get_min_value(diff, min_idx)
    end_idx = get_end_idx(diff, min_idx, min_value)
    start_idx = find_start_idx(diff, min_idx)
    
    
    
    for idx, i in enumerate(diff):
        if i == 0:
            continue

        if idx <= end_idx:
            if idx >= min_idx - start_idx - 1:
                diff[idx] -= min_value
            elif idx >= min_idx:
                diff[idx] -= min_value

    count += min_value

    

print(count)

Solution (Python):

def grand_garden(hlist):
    all_zero = False
    count = 0
    while(not all_zero):
        nonzero = False
        for i, h_i in enumerate(hlist):
            if h_i != 0:
                nonzero = True
                hlist[i] = hlist[i] -1
            elif nonzero:
                break


        if nonzero:
            count = count + 1
        else:
            all_zero = True


    return count




n = int( input())
hlist = input().split()
for i, h_i in enumerate(hlist):
    hlist[i] = int(hlist[i])

count = grand_garden(hlist)

print(count)

Solution (Python):
import numpy as np
N = int(input())
hlist = np.array([_ for _ in map(int,input().split())])

max = np.max(hlist)
min = np.min(hlist)

newlist = np.zeros_like(hlist)
num = 0

list = []

for i in range(1,N):
    sa = hlist[i-1]-hlist[i]
    if sa > 0:
        list.append(sa)
list.append(hlist[N-1])

print(sum(list))

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
count = 0
while any(h):
    l = -1
    r = -1
    for i in range(N):
        if l == -1 and h[i] != 0:
            l = i
        if r == -1 and h[i] == 0:
            r = i-1
        if r == -1 and i == len(h)-1:
            r = i
        if r < l:
            r = -1
        if l == len(h) - 1:
            r = l
    if l != -1 and r != - 1:
        for j in range(l, r+1):
            if h[j] != 0:
                h[j] -= 1
    count += 1
print(count)

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

def chk(n, ls):
    ret = 0
    flag = False
    for i in ls:
        if i >= n and flag == False:
            ret += 1
            flag = True
        elif i < n:
            flag = False
    return ret

ans = 0
i = 1
hmax = max(h)
while i <= hmax:
    ans += chk(i, h)
    i += 1
print(ans)

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

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

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

ans = 0
def dfs(l,r):
    global ans
    tmp = H[l:r]
    if len(tmp) == 0: return
    if len(tmp) == 1:
        ans += tmp[0]
        H[l] = 0
        return
    sub = min(tmp)
    ans += sub
    for i in range(l,r):
        H[i] -= sub
    tmp = H[l:r]
    if 0 in tmp:
        izero = l + tmp.index(0)
        if izero != l: dfs(l,izero)
        if izero != r: dfs(izero+1,r)

dfs(0,N)
print(ans)

Solution (Python):



def solve(h):
    if len(h) == 1:
        return h[0]
    elif len(h) == 0:
        return 0

    r = 0
    m = min(h)
    if m > 0:
        
        for i in range(len(h)):
            h[i] -= m
        r += m

    
    start = 0
    for i in range(len(h)):
        if i < start:
            continue
        if h[i] == 0:
            r += solve(h[start:i])
            start = i + 1
    r += solve(h[start:])
    return r


    start = 0
    r = 0
    for i in range(len(h)):
        if h[i] == 0:
            c = sorted(h[:i], reverse=True)[0]
            return c + solve(h[i:])
    else:
        sorted(h, reverse=True)[0]
    print(i)
    return r

if __name__ == '__main__':
    N = int(input())
    h = list(map(int, input().split()))

    c = solve(h)
    print(c)

Solution (Python):
n=int(input())
ii=input().split(" ")
flower=[]
count=0
end=n

for i in ii:
  flower.append(int(i))



while True:
  end=n
  if flower[0]==0:
    del flower[0]
    n=n-1
    if n==0:
      print(str(count))
      exit()
    else:
      continue
  for j in range(0,n):
    if flower[j]==0:
      end=j
      break
    
  for m in range (0,end):
    flower[m]=flower[m]-1
  
  count=count+1
  
  continue

Solution (Python):


N = int(input())
hi = [int(h) for h in input().split()]

def divide(l):
    div = []
    d = []
    for h in l:
        if (h == 0):
            div.append(d)
            d = []
        else:
            d.append(h)
    div.append(d)
    return div

def watering(l, n):
    div = divide(l)
    cnt = n
    for d in div:
        if not d:
            continue
        else:
            watered = [i - 1 for i in d]
            cnt += 1
            cnt = watering(watered, cnt)
    return cnt
    
print(watering(hi, 0))

Solution (Python):
import numpy as np

n = int(input())
heights = np.array(list(map(int, input().split())))

idx = list()
counts = list()


def div(vec):
    if len(vec) <= 0:
        return
    a_min = np.argmin(vec)
    count = vec[a_min]

    counts.append(count)
    idx.append(a_min)

    vec -= count
    div(vec[:a_min])
    div(vec[a_min + 1:])


div(heights)
print(np.sum(counts))

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

def do(l):
  ans = 0
  if len(l)==0:
    return 0
  m= min(l)
  ans += m
  
  l = (list)(map(lambda x:x-m,l))
  
  s=0
  ret = []
  for i in range(len(l)):
    if l[i]==0:
      ret.append(l[s:i])
      s = i+1
  ret.append(l[s:])
  for i in ret:
    ans += do(i)
  return ans

print(do(l))

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



min_H = min(H)
H = [h - min_H for h in H]
count_water = min_H


def water(L):
    
    c = 0
    if L == []:
        return 0
    elif len(L) <= 2:
        
        return max(L)
    else:
        min_L = min(L)
        c += min_L
        L = [l - min_L for l in L]
        zero_ind = -1
        for (i,l) in enumerate(L):
            if l == 0:
                c += water(L[zero_ind+1:i])
                zero_ind = i
        if L[-1] != 0:
            c += water(L[zero_ind+1:])
        return c

count_water += water(H)
print(count_water)

Solution (Python):
import copy
N = int(input())
old_h_list = [list(map(int, input().split()))]
ans = 0
while True:
    new_h_list = []
    tmp = []
    for h in old_h_list:
        for i, hi in enumerate(h):
            if hi == 0:
                if tmp == []:
                    continue
                new_h_list.append(tmp)
                tmp = []
                continue
            elif i == len(h)-1:
                tmp.append(hi)
                new_h_list.append(tmp)
                tmp = []
                continue
            tmp.append(hi)
    for i, h in enumerate(new_h_list):
        new_h_list[i] = [max(0, hi-1) for hi in h]
        ans += 1
    if sum([hi for h in new_h_list for hi in h]) == 0:
        break
    old_h_list = copy.copy(new_h_list)
print(ans)

Solution (Python):

N = int(input())
h_list = list(map(int, input().split()))
pour_cnt = 0

def cnt(remains, pour_cnt):
    tmp = []
    next_remains = []
    
    for index, num in enumerate(remains):
        if num != 0:
            tmp.append(num)
            if (index+1 == N):
                min_num = min(tmp)
                pour_cnt += min_num
                for tmp_num in tmp:
                    next_remains.append(tmp_num-min_num)
                tmp = []
        elif len(tmp) != 0:
            min_num = min(tmp)
            pour_cnt += min_num
            for tmp_num in tmp:
                next_remains.append(tmp_num-min_num)
            next_remains.append(0)
            
            tmp = []
        else:
            next_remains.append(0)

    if next_remains.count(0) == N:
        print(pour_cnt)
    else:
        cnt(next_remains, pour_cnt)

cnt(h_list, pour_cnt)

Solution (Python):
def mizuyari2(row):
    cnt = 0
    _min = min(row)
    cnt += _min
    row2 = [i - _min for i in row]

    res = [[]]
    for e in row2:
        if e != 0:
            res[-1].append(e)
        else:
            res.append([])

    return cnt, res


def mizuyari(ll):
    cnt = 0

    for l in ll:
        if l:
            cnt2, hs = mizuyari2(l)
            
            cnt += cnt2
            cnt += mizuyari(hs)
    return cnt


def main2():
    n = int(input())
    hs = [int(i) for i in input().split()]
    hs = [hs]
    cnt = mizuyari(hs)
    print(cnt)


if __name__ == '__main__':
    main2()

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
def func(n):
    return n - 1
ans = 0
flag = True
flag2 = False
while True:
    for i in h:
        if i > 0:
            if flag2 == False:
                ans += 1
            flag = False
            flag2 = True
        else:
            flag2 = False
    if flag:
        break
    h = list(map(func, h))
    flag = True
    flag2 = False
    
print(ans)