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()))
A = [0]*N
t = 0

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

yet = True
lowest = 0  

while yet:
    water_place = []
    last_need = False    
    start = 0
    end = 0
    for i in range(N):
        if last_need == False and A[i] < H[i]:
            start = i
            last_need = True
        elif last_need == True and A[i] >= H[i]:
            end = i-1
            last_need = False
            water_place.append([start,end])
        
        if i == N - 1 and A[i] < H[i]:
            end = i
            water_place.append([start,end])

    for l,r in water_place:
        t,A = water(l,r,t,A)
    if A == H:
        print(t)
        yet = False

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

H.insert(0, 0)
H.append(0)

a = 0
while sum(H) != 0:
    
    S, E = [], []
    for i in range(n + 1):
        if H[i] == 0 and H[i + 1] != 0:
            S.append(i + 1)
        elif H[i] != 0 and H[i + 1] == 0:
            E.append(i + 1)
    
    for i in range(len(S)):
        s, e = S[i], E[i]
        m = min(H[s:e])
        H[s:e] = [h - m for h in H[s:e]]
        a += m

print(a)

Solution (Python):
def solve(A, k):
  if len(A) == 0:
    return 0
  else:
    m = A.index(min(A))
    return A[m] - k + solve(A[:m], A[m]) + solve(A[m+1:], A[m])
  
if __name__ == '__main__':
  N, *h = map(int, open(0).read().split())
  print(solve(h, 0))

Solution (Python):
def solve(A, k):
  if len(A) == 0:
    return 0
  elif len(A) == 1:
    return A[0] - k
  else:
    m = A.index(min(A))
    return A[m] - k + solve(A[:m], A[m]) + solve(A[m+1:], A[m])
  
if __name__ == '__main__':
  N, *h = map(int, open(0).read().split())
  print(solve(h, 0))

Solution (Python):
N = int(input())
S = str(input())
S = S.split()
S_list = list(S)
count = 0

for i in range(len(S_list)):
    S_list[i] = int(S_list[i])

while S_list.count(0) == 0:
    S_list = list(map(lambda x: x - 1, S_list))
    count = count + 1
    






   
switch = []
for i in range(len(S_list)):
    if S_list[i] == 0:
        switch.append(0)
    else:
        switch.append(1)
        
while switch.count(1) != 0:  
    for i in range(len(S_list)):
        if i == 0:
            pass
        else:
            if switch[i-1] == 0 and switch[i] == 1:
                count = count +1
            else:
                pass
        if switch[i] == 1:
            S_list[i] = S_list[i] - 1
    if switch[0] == 1:
        count = count +1
        
    switch = []
    for i in range(len(S_list)):
        if S_list[i] == 0:
            switch.append(0)
        else:
            switch.append(1)
        
print(count)

Solution (Python):

N = int(input())
H = list(map(int, input().split()))
I = [0] * N

count = 0
updated = True

while updated:
  tmp = -9999
  updated = False
  
  for i in range(N):
    if I[i] != H[i]:
      if tmp + 1 != i:
        count += 1
      I[i] += 1
      updated = True
      tmp = i

print(count)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
cnt=0
ans=0
c=0
for i in range(100000):
    if h==[0]*n:
        break
    for j in range(n):
        if h[j]!=0:
            h[j]-=1
            cnt+=1
        elif h[j]==0 and cnt!=0:
            ans+=1
            cnt=0
            break
        elif h[j]==0 and ans==0:
            continue
        if j==n-1:
            ans+=1
            cnt=0
        
    
print(ans)

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

for i in range(max(h)):
    st = 1 if h[0] != 0 else 0
    for j in range(n):
        if st == 1:
            if h[j] == 0:
                cnt += 1
                st = 0
            else:
                h[j] -= 1
        else:
            if h[j] != 0:
                st = 1
                h[j] -= 1
    else:
        if st == 1:
            cnt += 1
else:
    ans = cnt

print(ans)

Solution (Python):
def solve():
    N, *h = map(int, open(0).read().split())
    
    ans = 0
    for _ in range(10000000):
        l, r = 0, 0
        tmp_min = 1000
        flag = False
        
        for i in range(N):
            if 0 < h[i]:
                l = i
                r = i
                flag = True
                tmp_min = h[i]
                break
        for i in range(l+1, N):
            if h[i] == 0:
                break
            r = i
            tmp_min = min(tmp_min, h[i])
        for i in range(l, r+1):
            h[i] -= tmp_min
        
        if not flag:
            break
        ans += tmp_min
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
import numpy as np

N = int(input())
H = np.array(list(map(int, input().split())))
d = np.diff(H)

ra = []
m = []
for i in range(len(d) - 1):
    if d[i] <= 0 and d[i + 1] > 0:
        ra.append(i + 1)
        m.append(H[i + 1])

ra = [0] + ra + [N]
M = []
for i in range(len(ra) - 1):
    M.append(np.max(H[ra[i]:ra[i + 1]]))

print(sum(M) - sum(m))

Solution (Python):
n = int(input())
lst = [int(i) for i in input().split()]
c=lst[0]
for i in range(1,n):

    if lst[i]>lst[i-1]:
        c+=lst[i]-lst[i-1]
print(c)

Solution (Python):
def main():
    n, *h = map(int, open(0).read().split())
    
    
    ans = h[0]
    
    for i in range(n-1):
        ans += max(h[i+1]-h[i],0)
        
    print(ans)
    return


main()

Solution (Python):


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

cnt=0
for i in range(max(H)):
    flg=0
    for j in range(N):
        if flg==0:
            if H[j]!=0:
                H[j]-=1
                cnt+=1
                flg=1
            else:  
                flg=0
        else:
            if H[j]!=0:
                H[j]-=1
            else:
                flg=0
print(cnt)

Solution (Python):


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

cnt=0
for i in range(max(H)):
    flg=0
    for j in range(N):
        if flg==0:
            if H[j]!=0:
                H[j]-=1
                cnt+=1
                flg=1
            
             
        else:
            if H[j]!=0:
                H[j]-=1
            else:
                flg=0
print(cnt)

Solution (Python):


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

cnt=0
for i in range(max(H)):
    flg=0
    for j in range(N):
        if flg==0:
            if H[j]!=0:
                H[j]-=1
                cnt+=1
                flg=1
            else:
                flg=0
        else:
            if H[j]!=0:
                H[j]-=1
            else:
                flg=0
print(cnt)

Solution (Python):
def main():
    N, *h = map(int, open(0).read().split())
    ans = h[0]
    for i in range(N - 1):
        ans += max(h[i + 1] - h[i], 0)
    print(ans)
    return


main()

Solution (Python):
import sys
n = int(input())
h_ls = [int(i) for i in sys.stdin.readline().split()]
cnt = 0
for i in range(1,101):
    flg = True
    for j in range(n):
        h = h_ls[j]
        if not h:
            flg = True
        else:
            h_ls[j] -= 1
            if flg:
                flg = False
                cnt += 1
print(cnt)

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

Solution (Python):
import math

n = int(input())
h = [int(el) for el in input().split(' ')]

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

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
H = [0]+H+[0]
def trm(li):
  for i in range(len(li)-2,0,-1):
    if li[i-1]<=li[i]<=li[i+1]:
      li.pop(i)
    elif li[i-1]>=li[i]>=li[i+1]:
      li.pop(i)
  return H
H = trm(H)
cnt = 0
for i in range(max(H),0,-1):
  flg = False
  for j in range(len(H)):
    if H[j]==i:
      if not flg:
        cnt +=1
        flg = True
      H[j]=H[j]-1
    else:
      flg = False
print(cnt)

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

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

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

     
def main():
    N = I()

    h = LI()
    cnt = 0

    def dec_h(l, r):
        nonlocal cnt
        nonlocal h
        if l > r:
            return
        min_h = min(h[l:r+1])
        
        for j in range(l, r+1):
            h[j] -= min_h
        cnt += min_h
        st = l
        en = r
        
        for i in range(l, r+1):
            if h[i] == 0:
                en = i-1
                dec_h(st, en)
                st = i+1
            if i == r and st < r+1:
                dec_h(st, r)
    dec_h(0, N-1)
    print(cnt)

main()

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

ans = h[0]

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

print(ans)

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

garden = [0] * n
ans,i = 0,0

while H != garden:
    
    if H[i] != garden[i]:
        
        while 1:
            if n == i or H[i] == garden[i]:
                ans += 1
                break
            else:
                garden[i] += 1
            i += 1
    
    else: i += 1
    if n <= i: i = 0
    

print(ans)

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

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

print(cnt)

Solution (Python):
n = int(input())
H = list(map(int,input().split()))
ans = 0
while H.count(0) != len(H):
  l,r = -1,-1
  for i in range(len(H)):
    if l == -1 and H[i] != 0:
      l = i
      r = l
    elif H[i] != 0:
      r = i
    elif l != -1 and H[i] == 0:
      break
  selected = [ h - 1 for h in H[l:r+1]]
  H[l:r+1] = selected
  ans += 1
print(ans)

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

def devide(h):
    global count
    while 0 not in h:
        for i in range(len(h)):
            h[i] -= 1
        count += 1
    h2 = []
    h3 = []
    for i in h:
        if i == 0:
            h2.append(h3)
            h3 = []
        if i != 0:
            h3.append(i)
    h2.append(h3)

    for i in h2:
        if i == []:
            continue
        else:
            devide(i)
            
devide(h)
print(count)

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
count = 0
def island(h):
    global count
    while 0 not in h:
        for i in range(len(h)):
            h[i] -= 1
        count += 1

def devide(h):
    h2 = []
    h3 = []
    for i in h:
        if i == 0:
            h2.append(h3)
            h3 = []
        if i != 0:
            h3.append(i)
    h2.append(h3)


    for i in h2:
        if i == []:
            continue
        else:
            island(i)
            devide(i)
            
island(h)
devide(h)
print(count)

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
count = 0
def island(h):
    global count
    while 0 not in h:
        for i in range(len(h)):
            h[i] -= 1
        count += 1

def devide(h):
    h2 = []
    h3 = []
    for i in h:
        if i == 0:
            h2.append(h3)
            h3 = []
        if i != 0:
            h3.append(i)
    h2.append(h3)


    for i in h2:
        if i == []:
            continue
        else:
            island(i)
            devide(i)
            
island(h)
devide(h)
print(count)

Solution (Python):
import numpy as np
n = int(input())
h0 = [int(i) for i in input().split()]
h = np.zeros(n,dtype = int)
x = np.zeros(n,dtype = int)

def minh(h,x):
  tmp1 = 100
  for i in range(n):
    if x[i] == 0:
      if h0[i] <= tmp1:
        tmp1 = h0[i]
  return tmp1

def mizu(h,x,tmp):
  for i in range(n):
    if x[i] == 0:
      h[i] = tmp
    if h0[i] == tmp:
      x[i] = 1

def bunnkatu(x):
  for i in range(n):
    if i == 0:
      if x[i] == 0:
        s = 1
      else:
        s = 0
    else:
      if x[i] == 0 and x[i-1] == 1:
        s += 1
  return s

def main():
  tmp = min(h0)
  ans = tmp
  mizu(h,x,tmp)
  while tmp < max(h0):
    tmp0 = minh(h,x)
    s = bunnkatu(x)
    mizu(h,x,tmp0)
    ans += s*(tmp0 - tmp)
    tmp = tmp0
  return ans

ans = main()
print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
amin = min(a)
before = 0
ans = amin
count = 0
for i in range(n):
    now = a[i]-amin
    if before - now < 0:
        count += abs(before - now)
    before = now
print(ans+count)

Solution (Python):
def find_index(l, x):
    return l.index(x) if x in l else -1

def water(sum, H):
    if len(H)==0:
        return sum
    elif len(H)==1:
        sum += H[0]
        return sum
    minimum = min(H)
    sum += minimum
    for i in range(len(H)):
        H[i] -= minimum
    index = find_index(H, 0)
    H1 = H[:index]
    sum = water(sum,H1)
    H2 = H[index+1:]
    sum = water(sum,H2)
    return sum


    water(sum, array)

def main():
    n = int(input())
    H = list(map(int, input().split()))
    sum = 0
    sum += water(sum, H)
    print(sum)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = ril()

res = 0
for x, y in zip([0] + ls, ls):
    if x < y:
        res += y - x
print(res)

Solution (Python):
import sys

input = sys.stdin.readline

ri = lambda: int(input())
rs = lambda: input().rstrip()
ril = lambda: list(map(int, input().split()))
rsl = lambda: input().rstrip().split()
ris = lambda n: [ri() for _ in range(n)]
rss = lambda n: [rs() for _ in range(n)]
rils = lambda n: [ril() for _ in range(n)]
rsls = lambda n: [rsl() for _ in range(n)]

n = ri()
ls = ril()

prev = 0
up = True
res = 0
for x, y in zip(ls, ls[1:] + [0]):
    if up and x > y:
        res += x - prev
        up = False
    elif not up and x < y:
        prev = x
        up = True
print(res)

Solution (Python):
from collections import Counter
import sys

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

res = 0

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

    i = 0
    while i < n:
        if Hs[i] == 0:
            i += 1
            continue
        else:
            res += 1
            while i < n and Hs[i] > 0:
                Hs[i] -= 1
                i += 1

print(res)

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

Solution (Python):
N=int(input())
ls=[int(s) for s in input().split()]
ls.append(0)
a=0
for i in range(N):
  a+=max([0,ls[i]-ls[i+1]])
print(a)

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

while any(H) != 0:
  for i in range(N):
    if H[i]!=0:
      for j in range(i,N):
        if H[j]==0:
          break
        elif j==N-1:
          j+=1
      break
  for k in range(i,j):
    H[k]-=1
  ans+=1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
import numpy as np
N = int(input())
h = [int(x) for x in input().split()]
h = np.array(h,np.int64)
ans = 0
def split_(A):
    global ans
    if np.count_nonzero(A != 0) == 0:
        return
    else:
        minA = A.min()
        A -= minA
        ans += minA
        for i in range(len(A)):
            if A[i] == 0:
                split_(A[:i])
                split_(A[i+1:])
                break
split_(h)
print(ans)

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

count = 0
while sum(H) > 0:
    flag = 0
    for i, h in enumerate(H):
        if h > 0 and flag == 0:
            H[i] -= 1
            flag = 1
        elif h == 0 and flag == 1:
            break
        elif h == 0 and flag == 0:
            continue
        else:
            H[i] -= 1
    count += 1
    
print(count)

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

ans = 0
l.insert(0, 0)
for i in range(n):
    diff = l[i+1] - l[i] 
    if diff > 0:
        ans += diff
    else:
        pass

print(ans)

Solution (Python):
N = int(input())
listh = list(map(int, input().split()))
listans = [listh[0]]
for n in range(1,N-1):
    if (listh[n+1]-listh[n])*(listh[n]-listh[n-1])<=0:
        listans.append(listh[n])
listans.append(listh[N-1])        

l =len(listans)
listans2 = []
for n in range(l-1):
    if listans[n] == listans[n+1]:
        pass
    else:
        listans2.append(listans[n])
listans2.append(listans[l-1])        

L = len(listans2)

listans3 = [listans2[0]]
for n in range(1,L-1):
    if (listans2[n+1]-listans2[n])*(listans2[n]-listans2[n-1])<=0:
        listans3.append(listans2[n])
listans3.append(listans2[L-1])        

ll =len(listans3)
listodd = [listans3[x] for x in range(ll) if x%2 ==1]

listeven = [listans3[x] for x in range(ll) if x%2 ==0]

if len(listodd) == 0:
    print(listeven[0])
elif ll%2 == 0:
    if listeven[0]>listodd[0]:
        del listodd[-1]
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:  
        del listeven[0]
        ans = sum(listodd)-sum(listeven)  
        print(ans)

else:     
    if listeven[0]>listodd[0]:
        ans = sum(listeven)-sum(listodd)
        print(ans)

    else:
        del listeven[-1]
        if len(listeven) >= 1:
            del listeven[0]
        ans = sum(listodd)-sum(listeven)
        print(ans)

Solution (Python):

import sys
input = sys.stdin.readline

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

def solve(l):
    ret = 0
    if len(l)==1:
        return l[0]
    elif 0 in l:
        part_l = []
        for i in l:
            if i != 0:
                part_l.append(i)
            else:
                if len(part_l)>0:
                    ret+=solve(part_l)
                    part_l = []
        if len(part_l)>0:
            ret+=solve(part_l)
        return ret
    else:
        min_l = min(l)
        ret+=min_l
        l = list(map(lambda x:x-min_l,l))
        return ret + solve(l)

print(solve(b))

Solution (Python):
n=int(input())
*L,=map(int,input().split())
w=0
b=0
for i in L:
      if i>b:
            w+=i-b
      b=i
print(w)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
ans = 0
m = 0
tmp_max = l[0]
check = False
for i in range(1, n):
    if l[i - 1] > l[i]:
        check = True
    elif check and l[i - 1] < l[i]:
        ans = ans + tmp_max - m
        tmp_max = l[i]
        m = l[i - 1]
        check = False
    elif not(check):
        tmp_max = l[i]
ans = ans + tmp_max - m
print(ans)

Solution (Python):

import sys
input = sys.stdin.readline

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

def solve(l):
    ret = 0
    if len(l)==1:
        return l[0]
    elif 0 in l:
        part_l = []
        for i in l:
            if i != 0:
                part_l.append(i)
            else:
                if len(part_l)>0:
                    ret+=solve(part_l)
                    part_l = []
        if len(part_l)>0:
            ret+=solve(part_l)
        return ret
    else:
        min_l = min(l)
        ret+=min_l
        l = list(map(lambda x:x-min_l,l))
        return ret + solve(l)

print(solve(b))

Solution (Python):
import numpy as np

N = int(input())
h = np.array([int(i) for i in input().split()])
flag = [0] * N

thrs = 0

if N == 1:
  print(h[0])
  exit()

i = 0
ans = 0
l, r = (0, 0)
while sum(flag) != N:
  j = 0
  while j < l:
    if h[j] == 0:
      flag[j] = 1
    j += 1
  
  if h[l] == 0:
    l += 1
    flag[l] = 1
  
  i = 0
  i += l
  r = i
  while h[i] > thrs:
    r += 1
    i += 1
    if i == N:
      break
  
  if l == r:
    continue
  ans += min(h[l:r])
  h[l:r] = h[l:r] - min(h[l:r])
print(ans)

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

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

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

cnt = 0

while sum(h_li) != 0:
    check = 0
    for i in range(n):
        
        if h_li[i] > 0:
            h_li[i] -= 1
            check = 1
    
        else:
            if check == 1:
                cnt += 1
                check = 0
    else:
        if check == 1:
            cnt += 1
            
print(cnt)

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

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

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

height=[]

result=0
tmp=0

for i in range(max(h)):
    buf=[]
    for j in range(n):
        if h[j] > i:
            buf.append(1)
        else:
            buf.append(0)
    height.append(buf)

for val in height:
    while(tmp < len(val)):
        if val[tmp]==0:
            tmp+=1
        else:
            result+=1
            while(tmp<len(val)):
                if(val[tmp]==0):
                    break
                tmp+=1;
    tmp=0

print(result)

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

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

print(ans)

Solution (Python):
N = int(input())
hs = [int(val) for val in input().split(' ')]




















def recursion(hs, op_num):
    if not hs:
        return op_num-1
    if min(hs) >= 1:
        return recursion([val - 1 for val in hs], op_num+1)

    results = []
    curr_results = []
    for i in range(len(hs)):
        if hs[i] >= 1:
            curr_results.append(hs[i]-1)
        else:
            results.append(curr_results)
            curr_results = []
    results.append(curr_results)
    for result in results:
        op_num += recursion(result, 1)
    return op_num

print(recursion(hs, 0))

Solution (Python):
N = int(input())
hs = [int(val) for val in input().split(' ')]
num = len(hs)
start_idx = None
end_idx = None
op_num = 0
last_op_idx = None
while sum(hs) > 0:
    for i in range(num):
        if hs[i] > 0:
            hs[i] -= 1
            last_op_idx = i
            if i == num - 1:
                op_num += 1
        else:
            if i >= 1 and last_op_idx == i-1:
                op_num += 1
print(op_num)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
f=[0]*n
cnt=0
while 1:
    flag=0
    for i in range(n):
        if h[i]!=f[i]:
            f[i]+=1
            flag=1
        elif h[i]==f[i] and flag==1:
            cnt+=1
            flag=0
    if flag==1:
        cnt+=1

    if h==f:
        break

print(cnt)

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

ans = 0

for h in range(1, max(H) + 1):
    i = 0
    while i < N:
        if H[i] < h:
            i += 1
        else:
            ans += 1
            while i < N and H[i] >= h:
                i += 1

print(ans)

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



def not_all_0(a):
    for i in a:
        if i!=0:
            return True
    else:
        return False

c=0
while(not_all_0(h)):
    d=0
    for i in range(n):
        if d==0:
            if h[i]!=0:
                h[i]-=1
                c+=1
                d=1
        else:
            if h[i]!=0:
                h[i]-=1
            else:
                d=0
print(c)

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

s = [0] * n
cnt = 0

while True:
    flag = 0
    for i in range(n):
        if h[i] != s[i]:
            s[i] += 1
            flag = 1
        elif h[i] == s[i] and flag:
            cnt += 1
            flag = 0
    if flag:
        cnt += 1

    if h == s:
        break

print(cnt)

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

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

ans, base, h_last = 0, 0, 0
for h in H:
    if h < h_last:
        ans += h_last - base
        base = h
    h_last = h
print(ans + H[-1] - base)

Solution (Python):
N=int(input())
H=[0]+[int(x) for x in input().split()]
P=max(H)
count=0
for i in range(P):
  Q=P-i
  for j in range(1,N+1):
    if H[j-1]<Q and H[j]>=Q:
      count +=1
print(count)

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

while True:
    my_bool=True
    for i in range(n):
        if my_bool:
            if a[i]!=0:
                my_bool=False
                ch2=True
                a[i]-=1
        else:
            if a[i]!=0:
                a[i]-=1
                ch=True
                ch2=True
            else:
                if ch2:
                    cnt+=1
                ch=False
                ch2=False
    if ch:
        if my_bool:
            pass
        else:
            
            cnt+=1
    
    if my_bool:
        break
print(cnt)

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):
n=int(input())
a=list(map(int,input().split()))
x=[0]*n
cnt=0
while a!=x:
    flag=True
    for i in range(n):
        if x[i]!=a[i]:
            x[i]+=1
            if i==n-1:
              cnt+=1
        else:
            continue
        for j in range(i+1,n):
            if x[j]!=a[j]:
                x[j]+=1
                if j==n-1:
                    cnt+=1
                    flag=False
                
            else:
                cnt+=1
                
                flag=False
                break
        if flag==False:
          break
print(cnt)

Solution (Python):
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                if len(harray) == 2:
                    harray[1] -= a
                else:
                    for y in range(1,N):
                        harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-1])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                if zeroarray[0]+1 != N:
                    for y in range(zeroarray[0]+1,N):
                            harray[y] -= a
                else:
                    harray[N-1] -= a
                counter += a
        else:
            if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
            if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    if zeroarray[-1]+1 != N:
                        for y in range(zeroarray[-1]+1,N):
                            harray[y] -= a
                    else:
                        harray[N-1] -= a
                    counter += a
            for k in range(len(zeroarray)-1):
                
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]):
                        harray[y] -= a
                    counter += a
                

print(counter)

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

ans = 0
while H:
    h = H.pop(0)
    if not h: continue

    min_h = min(h)
    ans += min_h
    
    s = 0
    
    for i in range(len(h)):
        h[i] -= min_h
        
        if h[i]: continue
        H.append(h[s:i])
        s = i+1
    if s != N: H.append(h[s:i+1])

print(ans)

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

hmax = max(H)
ans = 0

def calc(H):
  n = 0
  count = 0
  for i in H:
    if i == 0:
      if count != 0:
        n += 1
      count = 0
    else:
      count += 1
  return n

for i in range(hmax):
  H.append(0)
  ans += calc(H)
  del H[-1]
  H = list(map(lambda x: max(0, x - 1), H))

print(ans)

Solution (Python):
def count(A, a):
    count = 1 if A[0] >= a else 0
    for i in range(len(A)-1):
        if A[i] < a and A[i+1] >= a:
            count += 1
    return count

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

ans = 0
pre_h = 0
for h in sorted(set(H)):
    ans += count(H, h) * (h - pre_h)
    pre_h = h
print(ans)

Solution (Python):
def solve():
    N = int(input())
    H = list(map(int, input().split()))
    ret = 0
    flag = True
    
    while flag:
        flag = False
        continuous = False
        num_countinuous = 0
        
        for i in range(N):
            if H[i] > 0:
                H[i] -= 1
                flag = True
                if not continuous:
                    num_countinuous += 1
                    continuous = True
            else:
                continuous = False
                
        ret += num_countinuous
        
    print(ret)
    
solve()

Solution (Python):
import sys
input = sys.stdin.readline
N = int(input())
h = list(map(int, input().split()))
res = 0
mh = max(h)
for i in range(mh * N + 1):
  x = N
  for j in range(N - 1):
    if h[j] > 0 and h[j + 1] > 0:
      x -= 1
  for j in range(N):
    if h[j] == 0:
      x -= 1
      continue
    h[j] -= 1
  
  res += x
  if x == 0: break

print(res)