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
while True:
    state = 0
    for i in range(N):
        if state == 0 and h[i] > 0:
            state += 1
            h[i] -= 1
            ans += 1
        elif state == 1 and h[i] > 0:
            h[i] -= 1
        elif state == 1 and h[i] == 0:
            state = 2
            break
    if state == 0:
        break
print(ans)

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

cnt = 0
prev_h = 0

cnt_n = [0] * n

for h in range(1, 101):
  if hn[0] >= h:
    cnt_n[0] = cnt_n[n-1] + 1
  else:
    cnt_n[0] = cnt_n[n-1]
  for i in range(1, n):
    if hn[i] >= h:
      if hn[i - 1] >= h:
        cnt_n[i] = cnt_n[i-1]
      else:
        cnt_n[i] = cnt_n[i-1]+1
    else:
      cnt_n[i] = cnt_n[i-1]

print(max(cnt_n))

Solution (Python):
def main():
    n = int(input())
    *h, = map(int, input().split())

    if len(h) == 1:
        print(h[0])
        return

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


main()

Solution (Python):

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

def mizu(h, s):
	for k in range(s, len(h)):
		if h[k] > 0:
			h[k] -= 1
		else:
			break
	return h

count = 0
while True:
	s = min((i for i in range(len(h)) if h[i] > 0), default = -1)
	if s == -1:
		break
	else:
		h = mizu(h, s)
		count += 1

print(count)

Solution (Python):

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

def mizu(h, s):
	for k in range(s, len(h)):
		if h[k] > 0:
			h[k] -= 1
		else:
			break
	return h
	
def find_start(h):
	s = min((i for i in range(len(h)) if h[i] > 0), default = -1)
	return s

def is_finish(h):
	return sum(h) == 0

count = 0
while True:
	s = find_start(h)
	if s == -1:
		break
	else:
		h = mizu(h, s)
		count += 1

print(count)

Solution (Python):
import numpy as np

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


def recurse(arr):
    if len(arr) == 0:
        return 0
    if all([e == 0 for e in arr]):
        return 0

    cnt = 0
    while all([e > 0 for e in arr]):
        arr = arr - 1
        cnt += 1

    next = []
    for e in arr:
        if e != 0:
            next.append(e)
        else:
            cnt += recurse(np.array(next))
            next.clear()
    if len(next) > 0:
        cnt += recurse(np.array(next))

    return cnt


print(recurse(h))

Solution (Python):
def smaller(x, y):
	if x < y:
		return x
	return y

def allzero(lst):
	for i in range(0, len(lst)):
		if lst[i] != 0:
			return False
	return True

def minimum(lst):
	min = 5000000000000000
	for i in range(0, len(lst)):
		min = smaller(min, lst[i])
	return min

N = int(input())
H = list(map(int, input().split()))
sum = 0
while not allzero(H):
	left = -1
	right = -1
	for i in range(0, N):
		if H[i] != 0:
			left = i
			break
	for i in range(left, N):
		if H[i] == 0:
			right = i
			break
	if right == -1:
		right = N
	min = minimum(H[left:right])
	sum += min
	for i in range(left, right):
		H[i] -= min
print(sum)

Solution (Python):
def solve_range(h, frm, to):
    """[frm, to)"""
    
    if frm >= to:
        return 0
    res = 0
    start = frm
    for i in range(frm, to):
        h[i] -= 1
        if h[i] < 0:
            if start < i:
                res += solve_range(h, start, i) + 1
            start = i + 1
        elif i == to - 1:
            res += solve_range(h, start, to) + 1
    return res


def solve(h):
    return solve_range(h, 0, n)


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

print(solve(h))

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

for i in range(1,100+1):
    flag =True
    for j in range(n):
        if flag==True and l[j]>=i:
            cnt+=1
            flag=False
        elif flag==False and l[j]>=i:
            continue
        else:
            flag=True
print(cnt)

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

ans = 0
cur = 0
for hh in h:
    if cur < hh:
        ans += hh - cur
        cur = hh
    else:
        cur = hh
print(ans)

Solution (Python):



n = int(input())
h = list(map(int,input().split()))
ans = 0
while sum(h) != 0:
    front = False
    for i in range(n):
        if h[i] > 0:
            if not front:
                ans += 1
            front = True
            h[i] -= 1
        else:
            front = False
print(ans)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
h=a[:]
cnt=0
while sum(h)!=0:
  mae=False
  for i in range(N):
    if h[i]>0:
      if mae==False:
        cnt+=1
      mae=True
      h[i]-=1
    else:
      mae=False

print(cnt)

Solution (Python):






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

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

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

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

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
ans = 0
while True:
    if(max(h) == 0):
        break

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

print(ans)

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

cnt = 0
for i in range(100, 0, -1):
    prev = False
    for j in range(N):
        if h[j] == i:
            if not prev:
                cnt += 1
            h[j] -= 1
            prev = True
        else:
            prev = False

print(cnt)

Solution (Python):
n = int(input())
lis=list(map(int,input().split()))
li=[]
y=0
g=0
for i in range(n):
    li.append(lis[i])
    if len(li)>=2:
        if li[-2]>li[-1]:
            y=1
        if y==1 and li[-2]<li[-1]:
            g+=max(li[:-2])-li[-2]
            y=0
            li=[li[-1]]
g+=max(li)
print(g)

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

water=0
alive=1
while alive:
    flag=1
    cnt=0
    for i in range(N):
        if h[i]!=0:
            cnt+=flag
            flag=0
        elif h[i]==0:
            flag=1
    alive=0
    for i in range(N):
        if h[i]>0:
            h[i]-=1
            alive=1
        else:
            h[i]=0
    if alive:
        water+=cnt
print(water)

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


lst = [0]*N
for i in range(N):
    lst[i] = (h[i], i)
lst = list(sorted(lst))


ans = 0

def putLeft(n):
    now = h[n]
    res = 0
    for i in reversed(range(n)):
        if h[i] < now:
            break
        else:
            h[i] -= now
    return res

def putRight(n):
    now = h[n]
    res = 0
    for i in range(n+1, N):
        if h[i] < now:
            break
        else:
            h[i] -= now


for (_, i) in lst:
    if h[i] > 0:
        putLeft(i)
        putRight(i)
        ans += h[i]
        h[i] = 0

print(ans)

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

ret = 0
while H.count(0) != N:
    f = False
    for i in range(N):
        if H[i]:
            if not f:
                f = True
                ret += 1
            H[i] -= 1
        else:
            f = False

print(ret)

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

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

def is_clear(l):
    for ele in l:
        if(ele!=0):
            return False
    return True

while(not is_clear(flowers)):
    tmp_l = []
    
    for i,flower in enumerate(flowers):
        if(flower!=0):
            tmp_l.append(i)
        else:
            if(len(tmp_l)!=0):
                break


    if(len(tmp_l)>=2):
        water = min(flowers[tmp_l[0]:tmp_l[-1]+1])
        for i in tmp_l:
            flowers[i] -= water
    else:
        water = flowers[tmp_l[0]]
        flowers[tmp_l[0]] -= water

    ans += water

print(ans)

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

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

D = []
for i in range(N-1):
    D.append(H[i+1]-H[i])

ans = 0
up = True
down = False
for i in range(N-2):
    if i == 0 and D[i] < 0:
        ans += H[0]
        up = False
    if (D[i] > 0 and D[i+1] < 0) or (up and D[i+1] < 0):
        ans += H[i+1]
        up = False
    elif D[i] > 0 and D[i+1] == 0:
        up = True
    
    if (D[i] < 0 and D[i+1] > 0) or (down and D[i+1] > 0):
        ans -= H[i+1]
        down = False
    elif D[i] < 0 and D[i+1] == 0:
        down = True
if up or D[-1] > 0:
    ans += H[-1]


print(ans)

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

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


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


print(ans)

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


h_max = max(h)
ans = 0
for i in range(h_max):
    j = 0
    while j <= n-1:
        if h[j] == 0:
            j += 1
        else:
            while j <= n-1:
                if h[j] == 0:
                    ans += 1
                    j += 1
                    break
                elif j == n-1:
                    h[j] -= 1
                    ans += 1
                    j += 1
                    break
                else:
                    h[j] -= 1
                    j += 1
print(ans)

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


h_max = max(h)
ans = 0
for i in range(h_max):
    c = 0
    flag = 0
    for j in range(n):
        if h[j] != 0:
            h[j] -= 1
            flag = 1
            if j == n-1:
                c += 1
        else:
            if flag == 1:
                c += 1
                flag = 0
    ans += c
print(ans)

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

while sum(h) != 0:
    for i in range(n):
        if h[i] != 0:
            f = True
            h[i] -= 1
        else:
            if f:
                cnt += 1
            f = False
    if f:
        cnt += 1
    f = False
print(cnt)

Solution (Python):
n = int(raw_input())
H = map(int, raw_input().split())
res = 0
for i in range(1, max(H) + 1):
    consecutives = 0
    in_consecutive = False
    for j in range(n):
        if not in_consecutive and H[j] >= i:
            in_consecutive = True
        elif in_consecutive and H[j] < i:
            in_consecutive = False
            consecutives += 1
        else:
            continue
    if in_consecutive:
        consecutives += 1
    res += consecutives
print res

Solution (Python):
import numpy as np

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

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

print(ans)

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

ans = 0
tmp = 0
for i in h:
    if tmp < i:
        ans += i-tmp
    tmp = i
print(ans)

Solution (Python):


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

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

Solution (Python):
import numpy as np

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

hd = np.diff(hl)
print(hl[0]+sum(hd[hd>0]))

Solution (Python):


n = int(input())

h = input().split()

li = [0]
for i in range(n):
    li.append(int(h[i]))

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

print(ans)

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

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

while any(h):
  l,r = 0, 0
  for i in range(N):
    if h[i] == 0 and h[i+1] != 0:
      l = i+1
    if (h[i] != 0 and h[i+1] == 0):
      r = i+1
      minh = min(h[l:r])
      ans += minh
      for j in range(l,r):
        h[j] -= minh
print(ans)

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

cnt = 0

for h in range(1,101):
    exi = 0
    for f in range(N):
        if H[f] >= h:
            tmp = 1
        else:
            tmp = 0
        if tmp != exi:
            cnt += 1 - exi
            exi = 1 - exi
print(cnt)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
water = 0
ite = 0
while (ite < N):
    if h[ite] == 0:
        ite += 1
        continue
    kari_ite = ite
    water += 1
    while (kari_ite < N):
        if h[kari_ite] == 0:
            break
        else:
            h[kari_ite] -= 1
            kari_ite += 1
print(water)

Solution (Python):
a = int(input())
b = list(map(int, input().split()))
b.insert(0, 0)
b.insert(len(b), 0)
f = 0

while True:
    s = -1
    e = -1
    for i in range(len(b)):
        if b[i] != 0 and s == -1:
            s = i
        elif b[i] == 0 and s != -1 and b[i-1] == 0:
            s = i
        elif b[i] == 0 and s != -1:
            e = i
            break
    if s != -1 and e != -1:
        f += 1
        for i in range(s, e):
            b[i] -= 1
    elif s == -1 and e == -1:
        print(f)
        exit()

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

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

print(ans)

Solution (Python):

n1 = int(input())
na2 = [int(i) for i in input().split()]

r3 = 0
t4 = 0

for i4 in range(n1):
    if na2[i4] > t4:
        r3 += na2[i4] - t4
    
    t4 = na2[i4]

print(r3)

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

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

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


def f(x):
    if all([a == 0 for a in x]):
        return 0
    elif min(x) == 0:
        x0 = x.index(0)
        if x0 == 0:
            return f(x[1:])
        elif x0 == len(x) - 1:
            return f(x[:-1])
        else:
            return f(x[:x0]) + f(x[x0 + 1:])
    else:
        return f([a - 1 for a in x]) + 1


print(f(h))

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

def cs(lix):
  if len(lix) == 1:
    return lix[0]
  elif len(lix) == 0:
    return 0
  m = min(lix)
  div = lix.count(m)+1
  lili = [0]*div
  cut = 0
  for i in range(div-1):
    lili[i] = lix[cut:lix[cut:].index(m)+cut]
    cut = lix[cut:].index(m)+cut+1
  if lix[-1] == m:
    lili[div-1] = []
  else:
    lili[div-1] = lix[cut:]
  for i in range(lili.count([])):
    lili.remove([])
  return sum(list(map(cs,lili))) - (len(lili)-1)*m
print(cs(li))

Solution (Python):
import numpy as np

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

cnt = 0
Ha = np.array(H)

for i in range(max(H)):
    for j in range(N):
        if j==0:
            if Ha[0]>0:
                cnt += 1
        else:
            if Ha[j-1]<=0 and Ha[j]>0:
                cnt += 1
    Ha -= 1
print(cnt)

Solution (Python):
import numpy
n = int(input())
a = list(map(int,input().split()))
a.insert(0,0)
a = numpy.array(a)
a = a[1:]-a[:n]
print(a[a>0].sum())

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

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


def water(h):
    if len(h) == 0:
        return 0
    if len(set(h)) == 1:
        return h[0]
    elif not h.count(0):
        h2 = list(map(lambda x: x - 1, h))
        return water(h2) + 1
    else:
        ans = 0
        zeros = [i for i, x in enumerate(h) if x == 0]
        zeros.insert(0, -1)
        zeros.append(len(h))
        for i in range(len(zeros) - 1):
            ans += water(h[zeros[i]+1:zeros[i + 1]])
        return ans

print(water(h))

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

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

dat_map = []
for i in range(n):
    dat_map.append([0] * 100)

for i in range(n):
    for j in range(dat_h[i]):
        dat_map[i][j] = 1

res = 0
for i in range(100):
    cur = 0
    for j in range(n):
        if cur == 0:
            if dat_map[j][i] == 1:
                res += 1
        cur = dat_map[j][i]

print(res)

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

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

maxNum = max(h)
countNumber = 0
countWatering = 0
isfinished = False
for c in range(1, maxNum+1):
    array = []
    for index, num in enumerate(h):
        if (num >= c):
            array.append(index)
    if (len(array) == 0 or c > maxNum):
        break
    startIndex = 0
    beforeVlaue  = array[0]-1
    for a in array:
        if (a-1 != beforeVlaue):
            countWatering+=1
        if (a == array[-1]):
            countWatering+=1
        beforeVlaue = a
    
print(countWatering)

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

count=1
total=0
rem=N
for i in range(1000):
    for j in range(1,N+1):
        if H[j]==i:
            rem -= 1
            H[j]=0
            if H[j-1]!=0 and H[j+1]!=0:
                count+=1
            if H[j-1]==0 and H[j+1]==0:
                count -=1
    if rem==0:
        break
    total += count
    
    
    


print(total)

Solution (Python):
n = int(input())
s = list(map(int,input().split()))
s = [i-1 for i in s]
ans = 0
while(sum(s)!=-n):
    s_new=[i.replace("-1","a") for i in map(str,s)]
    s_join = ''.join(s_new)
    non = s_join.split("a")
    num = len([x for x in non if x])
    ans += num
    s = [max(i-1,-1) for i in s]
print(ans)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
ans = 0
for i in range(1, 110):
  flag = False
  for j in range(n):
    if (flag and h[j]<i) or (j==n-1 and h[j]>=i):
      ans += 1
      flag = False
    elif h[j]>=i:
      flag = True
print(ans)

Solution (Python):
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)

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

while True:
	l = -1
	r = N
	for i in range(N):
		if h[i] > 0:
			l = i
			break
			
	for i in range(l, N):
		if h[i] == 0:
			r = i
			break
			
	if l == -1:
		break
			
	for i in range(l, r):
		h[i] -= 1
		
	m += 1
	
print(m)

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

if __name__=="__main__": main()

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

cnt=0
while(True):
  if max(hlist) == 0:
    break
  
  left = 0
  right = N-1
  
  maxh = max(hlist)
  inseq = False
  for i in range(N):
    if hlist[i] == maxh:
      if not inseq:
        inseq = True
        left = i
    else:
      if inseq:
        right = i-1
        break

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

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

count = min(h_list)
h_list = [item - min(h_list) for item in h_list] + [0]

first = 0
for h in range(len(h_list) - 1):
    if h_list[h] > h_list[h + 1]:
        count += h_list[h] - first
        first = h_list[h + 1]
    else:
        continue
print(count)

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

i = 0
cnt = 0
while 1:
    if i == n:
        print(cnt)
        exit()

    if h[i]==0:
        i+=1
        continue
    j = i
    
    while 1:
        if j==n:
            j-=1
            break
        elif h[j]>=1:
            j+=1
        else:
            j-=1
            break


    cnt += 1
    

    for k in range(i,j+1):
        h[k] -= 1

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

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

while sum(h)!=0:
    i=0
    crnt = h[i]
    while crnt==0:
        i+=1
        crnt=h[i]
    s=i
    while i<(n-1) and h[i]!=0:
        crnt = min(crnt, h[i])
        i+=1
    for ii in range(s, i+1):
        h[ii] = max(0, h[ii]-crnt)
    ans+=crnt

print(ans)

Solution (Python):
import numpy as np

N = int(input())
H = np.array(input().split(), dtype=np.int32)

def myfunc(left, right):
    if right<left:
        return 0
    idx = left+H[left:right+1].argmin()
    m = H[idx]
    H[left:right+1]-=m
    return m+myfunc(left, idx-1)+myfunc(idx+1, right)

print(myfunc(0, N-1))

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

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

print(ans)

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

count = min(h_list)
h_list = [item - min(h_list) for item in h_list] + [0]
first = 0
for h in range(len(h_list) - 1):
    if h_list[h] > h_list[h + 1]:
        count += h_list[h] - first
        first = h_list[h + 1]
    else:
        continue
print(count)

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

def F(L,R):
  if R < L:
    return 0
  idx = L + H[L:R+1].argmin()
  x = H[idx]
  H[L:R+1] -= x
  return x + F(L,idx-1) + F(idx+1,R)

answer = F(0,N-1)
print(answer)

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

Solution (Python):
def mount(l):
    if l == []:
        return True
    else:
        m = l.index(max(l))
        flag = 0
        for i in range(m):
            if l[i] > l[i+1]:
                flag = 1
        for i in range(m,len(l)-1):
            if l[i] < l[i+1]:
                flag = 1
        if flag == 0:
            return True
        else:
            return False
def count(l):
    if mount(l):
        if l != []:
            return(max(l))
        else:
            return 0
    else:
        cnt = min(l)
        l = list(map(lambda x:x-min(l),l))
        start = 0
        for i in range(len(l)):
            if l[i] == 0:
                cnt += count(l[start:i])
                start = i+1
            elif i == len(l)-1:
                cnt += count(l[start:])
        return(cnt)
N = int(input())
h = list(map(int,input().split()))
print(count(h))

Solution (Python):
def all_water(h_list):
  if len(h_list) == 0:
    return 0
  min_h = min(h_list)
  new_list = [x - min_h for x in h_list]
  i = new_list.index(0)
  return min_h + all_water(new_list[:i]) + all_water(new_list[i+1:])

n = input()

height = list(map(int,input().split(" ")))

print(all_water(height))

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


ans = 0
while True:
    if sum(H) == 0:
        break
    begin = 0
    end = 0
    for i in range(N):
        if H[i] != 0:
            begin = i
            break
    
    for i in range(begin, N):
        
        if H[i] == 0:
            
            end = i - 1
            break
        end = i
    
    for i in range(begin, end + 1):
        H[i] -= 1
    ans += 1

print(ans)

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

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

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

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

        j=0
        while h[i+j]>0:
            h[i+j]-=1
            j+=1
            if i+j==N:
              break

print(ans)

Solution (Python):
input()
h =  [int(i) for i in input().split()]
ans = 0
bef = 0 
for i in h:
  if i > bef:
    ans += i - bef
    bef = i
  else:
    bef = i
print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  sm=sum(l)

  c=0
  i=0
  while True:
    if l[i]>0:
      l[i]-=1
      sm-=1

      if i+1==n or l[i+1]==0:
        c+=1

    i+=1
    if i>=n:
      i=0

    if sm==0:
      return c

print(main())

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

count = 0
for h in range(max(H)):
	for n in range(1, N + 1):
		if h < H[n] and H[n - 1] <= h:
			count += 1
print(count)

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

ans = 0

while True:
        if max(A) == 0:
                break
        hoge = 0
        for i in range(N-1):
                if A[i] > 0 and A[i+1]  == 0:
                        ans += 1
        if A[N-1] > 0:
                        ans += 1

        for i in range(N):
                if A[i] != 0:
                        A[i] -= 1

print(ans)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
h.append(0)
cnt = 0
while True:
    flag = True
    for i in h:
        if i != 0:
            flag = False

    if flag:
        break

    l = 0
    while h[l] == 0:
        l += 1

    r = l
    while h[r+1] != 0 and r < n:
        r += 1

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

print(cnt)

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

for i in range(1,higest+1):
    a = 0
    for j in h:
        if j < i:
            a = 1
        elif a == 1:
            count += 1
            a = 0
    if h[0] >= i:
        count += 1

print(count)

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

count = 0
for i in range(max_num+1):
    is_mountain= False
    mountain = 0
    for index, value in enumerate(h):
        if value <= 0:
            if is_mountain:
                mountain += 1
            is_mountain = False
        else:
            is_mountain = True
        h[index] -= 1
    count += mountain
print(count)

Solution (Python):
from collections import deque

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

stack = deque([])
prevPoint = -1

for i in range(N) :
    if h[i] == 0 :
        stack.append((prevPoint+1 , i))
        prevPoint = i
    elif i >= N - 1 :
        stack.append((prevPoint+1,N))

ans = 0

while stack :
    start , end = stack.pop()

    if start >= end :
        ans += h[start]
        h[start] = 0
        continue


    minus = min(h[start:end])
    ans += minus

    prevPoint = start - 1
    for i in range(start,end) :
        h[i] -= minus
        if h[i] == 0 :
            stack.append((prevPoint+1,i))
            prevPoint = i
        elif i >= end - 1 :
            stack.append((prevPoint+1,end))

print(ans)

Solution (Python):
def findlong(a):
    a+=[0]
    l = 0
    c = []
    j = 0
    for i in range(len(a)):
        if(a[i]!=0):
            l+=1
            j = i
        else:
            c.append([l,j])
            l = 0
    return max(c)
n = int(input())
a = list(map(int,input().split()))
l = 0
while(True):
    if(len(set(a))==1 and a[0]==0):
        break
    k = findlong(a)
    del a[-1]
    z = [k[1]-k[0]+1,k[1]]
    for i in range(z[0],z[1]+1):
        a[i]-=1
    l+=1
print(l)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()] 
ans = 0
for i in range(max(a)):
        m = max(a)
        cnt = a.count(m)
        for y in range(len(a)-1):
                if a[y] == m == a[y+1]:
                        cnt -= 1
        a = [(z-1 if z == m else z) for z in a]
        ans += cnt
print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()











def main():
  n=I()
  l=LI()

  sm=sum(l)

  i=0
  c=0
  while True:

    
    

    if l[i]!=0:
      l[i]-=1
      sm-=1

      if i==n-1 or l[i+1]==0:
        c+=1

    i+=1
    if i>=n:
      i=0

      if sm==0:
        break

  return c

print(main())

Solution (Python):

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

count = 0

for k in range(max(h)):
    j = 0
    for i in range(0,len(h)):
        if h[i]==0 and i!=j:
            h[j:i] = h[j:i] -1
            count = count+1
            j = i + 1
        elif i ==len(h)-1 and h[n-1]!=0:
            count = count+1
            h[j:]= h[j:]-1
        elif h[i]==0 and i==j:
            j = i+1
print(count)