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):
def recur(heights):
    global ans
    if heights == [0]:
        return 1
    ans += 1
    new_heights = []
    for h in heights:
        new_heights.append(h - 1)
    for splitted_new_heights in split_heights(new_heights):
        recur(splitted_new_heights)


def split_heights(heights):
    splitted_heights = []
    partial_heights = []
    for h in heights:
        if h == 0 and len(partial_heights) > 0:
            splitted_heights.append(partial_heights)
            partial_heights = []
        elif h != 0:
            partial_heights.append(h)
    if len(partial_heights) > 0:
        splitted_heights.append(partial_heights)
    return splitted_heights


def main():
    global ans
    ans = 0
    N = int(input())
    h = list(map(int, input().split()))
    for splitted_heights in split_heights(h):
        recur(splitted_heights)
    print(ans)


if __name__ == '__main__':
    main()

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

while True:
    for i in range(h.count(0)):
        if h.index(0) == 0: 
            h.pop(0)
        elif h.index(0) > 0:
            tmp = h.index(0)
            break
    else:
        tmp = len(h)

    if not h:
        break

    if tmp == 1:
        cnt += h[0]
        h.pop(0)
    else:
        minNum = min(h[0:tmp])
        cnt += minNum
        for j in range(tmp):
            h[j] -= minNum

print(cnt)

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

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

    pre = i

print(ans)

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

def main():
    n = int(input())
    h = list(map(int, input().split()))
    l = [[0]*100 for _ in range(100)]
    for i in range(len(h)):
        k = h[i]
        for q in range(k):
            l[i][q] = 1
    l = [list(x) for x in zip(*l)]
    ans = 0
    for i in l:
        flg = 0
        for g in i:
            if g == 1:
                if flg == 0:
                    ans += 1
                flg = 1
            if g == 0:
                flg = 0
    print(ans)


if __name__=="__main__":
    main()

Solution (Python):
N=int(input())
h=list(map(int,input().split()))
l=[0]*N
z=0
for i in range(0,100):
    f=False
    for j in range(0,N):
        if h[j]>i:
            if not f:
                f=True
        else:
            if f:
                f=False
                z+=1
    if f:
        z+=1

print(z)

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

print(ans)

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

Solution (Python):
from sys import stdin
readline = stdin.buffer.readline

def watering(h):
    ans = min(h)
    hh = list(map(lambda x: x - ans, h))
    if len(set(hh)) == 1:
        return ans
    fs = 0
    for i, n in enumerate(hh):
        if n == 0:
            if i != fs:
                hhh = hh[fs:i]
                ans += watering(hhh)
                fs = i + 1
            else:
                fs = i + 1
    else:
        hhh = hh[fs:]
        if len(hhh) > 0:
            ans += watering(hhh)
    return ans

def main():
    N = int(readline().rstrip())
    H = list(map(int, readline().rstrip().split()))
    print(watering(H))

if __name__ == "__main__":
    main()

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

def main(n,h):
    count=0
    hmax=max(h)
    for i in range(hmax):
        j=0
        mizuyari=False
        for j in range(n):
            if h[j]>0:
                if mizuyari==False:
                    mizuyari=True
                    count+=1
                h[j]-=1
            else:
                mizuyari=False
    return count

if __name__=='__main__':
    n,h=readinput()
    ans=main(n,h)
    print(ans)

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

active = 0
ans = 0

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

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip.split())
def main():
    n = I()
    h = LI()
    ok = 0
    ng = 0
    ans = 0

    for i,x in enumerate(h):
        if x>0:
            ok = i
            break
    else:
        ok = n

    while ok != n:
        for i,x in enumerate(h[ok+1:]):
            if x == 0:
                ng = ok + 1 + i
                break
        else:
            ng = n
        
        sub = [0]*ok + [1]*(ng-ok) + [0]*(n-ng)

        for i in range(n):
            h[i] -= sub[i]
        
        for i,x in enumerate(h):
            if x>0:
                ok = i
                break
            else:
                ok = n
        ans += 1

    print(ans)
main()

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
H = [h]
ans = 0
while len(H) > 0:
  for e in H:
    if len(e) == 0:
      H.remove(e)
    elif 0 in e:
      a = e[:e.index(0)]
      b = e[e.index(0) + 1:]
      H.append(a)
      H.append(b)
      H.remove(e)
      if [] in H:
        H.remove([])
        if len(H) == 0:
          break
    else:
      ans += min(e)
      c = [e[j] - min(e) for j in range(len(e))]
      a = c[:c.index(0)]
      b = c[c.index(0) + 1:]
      H.append(a)
      H.append(b)
      H.remove(e)
      if [] in H:
        H.remove([])
        if len(H) == 0:
          break
    if [] in H:
      H.remove([])
      if len(H) == 0:
        break
print(ans)

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n = k()
h = l()

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

print(ans+h[-1])

"""
n = k()
s = v()

def check(n):
    if 


comand = ["A","B","X","Y"]
p = list(itertools.permutations(comand,2))
for i in range(len(p)):
    p[i] = "".join(p[i])

for i in 
"""

Solution (Python):
N = int(input())
H = [[0]*N for _ in range(100)]
for j, h in enumerate(map(int, input().split())):
    for i in range(h):
        H[i][j] = 1
cnt = 0
for i in range(100):
    a0 = 0
    for a in H[i]:
        if a != a0 and a0==0:
            cnt += 1
        a0 = a
print(cnt)

Solution (Python):
from sys import stdin
from numpy import argmin

n = int(stdin.readline().strip())
h_lst = [int(x) for x in stdin.readline().split()]
cnt = 0

def glow(lst, start_pos):
  global cnt
  if len(lst) == 0:
    return -1
  elif len(lst) == 1:
    cnt += lst[0] - start_pos
    return -1
  else:
    pos = min(lst)
    cnt += pos - start_pos
    id_ = argmin(lst)
    glow(lst[:id_], pos)
    glow(lst[id_+1:], pos)
    
glow(h_lst, 0)
print(cnt)

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

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

print(ans)

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

ans = 0

    
while True:
    i_of_minus = [i for i, x in enumerate(h) if x <= 0]
    c = len(i_of_minus)
    
    if c >= N:
        break
    if c == 0:
        ans += 1
        h = list(map(lambda x: x-1, h))
    else:
        if i_of_minus[0] != 0:
            ans += 1
        if i_of_minus[-1] != N - 1:
            ans += 1
        for j in range(1, c):
            if i_of_minus[j] - i_of_minus[j-1] > 1:
                ans += 1
        h = list(map(lambda x: x - 1, h))
print(ans)

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

lowc = 0

for i in range(1, N):
    if H[i - 1] > H[i]:
        lowc += H[i - 1] - H[i]
        
print(lowc + H[-1])

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

r=0
b=[a]
while True:
    d=[]
    for x in b:
        m=min(x)
        r+=m
        d.append([i-m for i in x])

    b=[]
    for x in d:
        t=[]
        for y in range(len(x)):
            if y==len(x)-1:
                if x[y]!=0:
                    t.append(x[y])
                    b.append(t)
                if x[y]==0 and x[y-1]!=0:b.append(t)
            elif x[y]!=0:t.append(x[y])
            elif x[y]==0:
                if len(t)!=0:b.append(t)
                t=[]

    d=[]
    for x in b:
        if len(x)<=2:r+=max(x)
        else:d.append(x)
    if len(d)==0:print(r);exit()
    b=d

Solution (Python):
import math
N = int(input())
h = list(map(int, input().split()))
ans = 0
while sum(h) > 0:
    i = 0
    while i < N and h[i] == 0:
        i += 1
    if i < N:
        ans += 1
        while i < N and h[i] > 0:
            h[i] -= 1
            i += 1
print(ans)

Solution (Python):
n = int(input())
h = [int(i) for i in 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):
n = int(input())
hs =list(map(int, input().split()))

ans = hs[-1]
for i in range(1,n):
    if hs[i] < hs[i-1]:
        ans += hs[i-1] -hs[i]
    else:
        continue
        
print(ans)

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

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

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

h.append(0)
pre = True 
next = True 
cnt = 0
for i in range(N):
    if h[i+1]>h[i]:
        next = True 
        if pre == False:
            cnt -= h[i]
    elif h[i+1]<h[i]:
        next = False
        if pre == True:
            cnt += h[i]
    else:
        next = pre 
    pre = next 
print(cnt)

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

l = 0
flag = 1
count = 0
while l < N-1:
  if h[l] == 0:
    l += 1
  elif h[l] != 0:
    r = l+1
    while h[r] != 0:
      r += 1
      if r == N:
        break
    for k in range(l,r):
      h[k] -= 1
    count += 1
print(count+h[N-1])

Solution (Python):
N=int(input())
*H,=map(int,input().split())
L=len(H)

ans=0
active=0

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

Solution (Python):
N=int(input())
*H,=map(int,input().split())
L=len(H)

cnt=0
prv=0

for i in range(N):
    if  prv < H[i]:
        cnt+=H[i]-prv
    prv=H[i]
print(cnt)

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

l = 0
flag = 1
count = 0
while l < N:
  while h[l] == 0:
    l += 1
    if l == N:
      flag = 0
      break
  if flag == 1:
    if l == N-1:
      r = N-1
    else:
      r = l+1
      while h[r] != 0:
        r += 1
        if r == N:
          break
      r -= 1
    for k in range(l,r+1):
      h[k] -= 1
    count += 1
    
  elif flag == 0:
    break
print(count)

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

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *H = map(int, read().split())

    ans = 0
    while True:
        changed = changed2 = False
        for i in range(N):
            if H[i] > 0:
                H[i] -= 1
                changed = True
                changed2 = True
            else:
                if changed:
                    ans += 1
                    changed = False
        if changed:
            ans += 1
        if not changed2:
            break

    print(ans)
    return


if __name__ == '__main__':
    main()

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

for i in range(N):
  while H[i] < 0:
    H[i] += 1
    j = i+1
    while H[j] < 0:
      H[j] += 1
      j += 1
    cnt += 1

print(cnt)

Solution (Python):
import numpy as np

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

check = [0]*N
height = [0]*N
count = 0

while True:
  check = [1 if h[i] == height[i] else 0 for i in range(N)]
  if check.count(1) == N:
    break
  count += 1
  for i in range(N):
    if check[i] == 0:
      height[i] += 1
      if i<N-1 and check[i+1] == 1:
        break
  
  
  
print(count)

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

s = 0

for i in range(max(h)+1):
    flag = False
    for j in range(n):
        if flag and h[j] == 0:
            s += 1
            flag = False
        elif not flag and h[j] > 0:
            flag = True

        h[j] = max(h[j]-1, 0)
    if flag:
        s += 1

print(s)

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

while a:
    mizu = False
    if sum(h) == 0:
        a = False
        break

    w += 1
    for i in range(n):
        if h[i] != 0:
            h[i] -= 1
            mizu = True
        else:
            if mizu:
                break 


print(w)

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

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

cnt = 0
grow = True
while grow:
    inc = False
    grow = False
    for i in range(n):
        if h[i] == 0:
            inc = False
        else:
            h[i] -= 1
            if not inc:
                inc = True
                grow = True
                cnt += 1
print(cnt)

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

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

print(ans)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
ans = 0
tmp = 0
for x in h:
    if tmp < x:
        ans += x - tmp
    tmp = x
print(ans)

Solution (Python):
import math

n = int(input())
hL = list(map(int, input().split(" ")))

ans = 0
cont = False
while True:
    if len(set(hL)) == 1 and hL[0] == 0:
        break
    cont = False
    cnt = 0
    for j in range(n):
        if hL[j] != 0:
            hL[j] -= 1
            if not cont:
                cnt += 1
            cont = True
            continue
        else:
            cont = False

    ans += cnt

print(ans)

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

ans = 0
while max(h) != 0:
  if h[0] > 0: flag = True
  else: flag = False
    
  if flag:
    for i in range(n):
      if h[i] == 0: break
      h[i] -= 1
    ans += 1
  else:
    for i in range(1,n):
      if flag == False and h[i] == 0: continue
      if flag == True and h[i] == 0: break
      flag = True
      h[i] -= 1
    ans += 1
      
print(ans)

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

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]





N = ii()
H = li()

m = min(H)
cnt = H[0]

for i in range(1, N):
    if H[i] >= H[i-1]:
        cnt += H[i] - H[i-1]

print(cnt)

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

def f(A):
  if len(A) == 0:
    return 0
  elif len(A) == 1:
    return A[0]
  elif len(A) == 2:
    return max(A)

  c = 0
  if A.count(0) == 0:
    c += min(A)
    A = [a - min(A) for a in A]

  B = split_list(A)
  ret = []
  for j in range(len(B)):
    ret.append(f(B[j]))

  return c + sum(ret)

def split_list(A):
  B = []
  tmp = []
  for i in range(len(A)):
    if A[i] != 0:
      tmp.append(A[i])
    else:
      if len(tmp) > 0:
        B.append(tmp)
        tmp = []
  else:
    if len(tmp) > 0:
      B.append(tmp)
  return B

print(f(H))

Solution (Python):
def main():
    n=int(input())
    h=list(map(int,input().split()))
    count = 0
    while max(h) > 0:
        m = 101
        i = 0
        while h[i] == 0:
            i += 1
        j = i
        while i < n:
            if j == n or h[j] == 0:
                count += m
                for x in range(i,j):
                    h[x] -= m
                i = j
                break
            elif h[j] != 0:
                m = min(m, h[j])
            j += 1
    print(count)
    
if __name__ == "__main__":
    main()

Solution (Python):
import sys


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(input())
    if N==1:
        print(input())
        exit()
    h = list(map(int, input().split()))


    dflag = 0
    cmin = 0
    cmax = h[0]
    count = 0
    for i in range(1, N):
        if h[i - 1] < h[i] and dflag == 1:
            count += cmax - cmin
            cmax = h[i]
            dflag = 0
            cmin = h[i - 1]

        elif h[i - 1] > h[i]:
            dflag = 1

        elif h[i - 1] < h[i]:
            cmax = h[i]

    if dflag == 1:
        count += cmax - cmin
    else:
        count += max(cmax, h[i]) - cmin

    print(count)


if __name__ == "__main__":
    main()

Solution (Python):
def separate(array,separator):
    results = []
    a = array[:]
    i = 0
    while i<=len(a)-len(separator):
        if a[i:i+len(separator)]==separator:
            results.append(a[:i])
            a = a[i+len(separator):]
            i = 0
        else:
            i+=1
    results.append(a)
    return results


def find(ar):

    if ar==[0]*len(ar):
        return 0
    m=999999999999999
    for i in range(len(ar)):
        if ar[i]<m:
            m=ar[i]
    for i in range(len(ar)):
        ar[i]-=m
    ans=m
    new=separate(ar,[0])
    for i in new:
        ans+=find(i)
    return ans








n=int(input())
ar=[int(i) for i in input().strip().split(" ")]
print(find(ar))

Solution (Python):
input()
h = list(map(int, input().split()))
h.append(-1)
ans = 0
state = "up"
for i, j in zip(h[:-1], h[1:]):
    if state == "up" and i > j:
        ans += i
        state = "down"
    if state == "down" and i < j:
        ans -= i
        state = "up"
print(ans)

Solution (Python):
n = int(input())
List = list(map(int,input().split()))
ans = 0
temp = 0
for item in List:
  if item > temp:
    ans += item - temp
  temp = item
print(ans)

Solution (Python):
import sys
import numpy as np

stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    H = nl()
    ans = 0
    while not all([h == 0 for h in H]):
        if len(set(H)) == 1:
            ans += H[0]
            H = [0] * n
        else:
            h_max = max(H)
            prev = -10
            for i in range(n):
                if H[i] == h_max:
                    if prev != i - 1:
                        ans += 1
                    prev = i
                    H[i] -= 1
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n,*h=map(int,open(0).read().split())
ans=0
left=0
while True:
    while left<n and h[left]==0:
        left+=1
    
    if left==n:
        break
    
    right=left

    while right+1<n and h[right+1]>0:
        right+=1
    
    ans+=1
    for i in range(left,right+1):
        h[i]-=1
    
print(ans)

Solution (Python):
def main():
    n = int(input())
    hs = list(map(int, input().split()))

    ans = 0
    while True:
        if sum(hs) == 0:
            break

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


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
if n==1:
    print(h[0])
elif n==2:
    print(max(h))
else:
    h.append(0)
    h.insert(0,0)
    
    ans=0
    f=[0 for i in range(n+2)]
    
    for i in range(10010):
        a=1
        x=[]
        for j in range(n+2):
            if h[j]<=f[j]:
                x.append(j)
        m=len(x)
        if m==n+2:
            break
        a=m-1
        for j in range(m-1):
            if x[j]+1==x[j+1]:
                a-=1
        ans+=a
        for j in range(n+2):
            f[j]+=1
        
    print(ans)

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    A = list(map(int,input().split()))

    ans = 0

    while True:
        
        
        if max(A) == 0:
            break
        
        i = 0
        
        
        while i < n:
        
            if A[i] == 0:
                
                i += 1
            else:
                
                ans += 1

                
                while i < n and A[i] != 0:
                    
                    A[i] -= 1
                    
                    i += 1
    print(ans)

Solution (Python):
def main():
    n = int(input())
    h_lst = list(map(int,input().split()))
    zero_count = 0
    count = 0

    for i in range(n):

        if h_lst[i] == 0:
            zero_count += 1


    while zero_count < n:

        flag = False
        tmp_count = 0

        for i in range(n):

            if h_lst[i] > 0:
                flag = True
                h_lst[i] -= 1

                if h_lst[i] == 0:
                    zero_count += 1

            else:
                if flag:
                    tmp_count += 1   
                    flag = False

        if flag:
            tmp_count += 1

        count += tmp_count  

        
    print(count)


if __name__ == '__main__':
    main()

Solution (Python):


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

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

Solution (Python):
def main():
    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)


if __name__ == '__main__':
    main()

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

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

print(ans)

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())
A = list(map(int, input().split())) + [0]
ans = 0
for h in range(1, 101):
  flag = False
  for a in A:
    if a >= h:
      flag = True
    elif flag:
      ans += 1
      flag = False
print(ans)

Solution (Python):
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
from collections import deque  
from collections import defaultdict  
from heapq import heapify, heappush, heappop, heappushpop  
from bisect import bisect_left, bisect_right  
sys.setrecursionlimit(int(1e+6))
MOD = int(1e+9) + 7



def main():
    N = i2n()
    H = i2nn()
    op = 0
    while True:
        b = False
        hmax = 0
        for i in range(N):
            if H[i] > 0:
                if b == False:
                    b = True
                    op += 1
                H[i] -= 1
                if hmax < H[i]:
                    hmax = H[i]
            else:
                b = False
        if hmax == 0:
            break
    print(op)

main()

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

l = 0
r = 0
cnt = 0

while True:
    l = 0
    fin = False

    while h[l] == 0:
        l += 1
        if l == n:
            fin = True
            break
    if fin:
        break

    r = l
    minval = h[l]
    while h[r] != 0:
        if h[r] < minval:
            minval = h[r]

        r += 1
        if r == n:
            break

    h = h[:l] + [val - minval for val in h[l:r]] + h[r:]
    cnt += minval

print(cnt)

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

Solution (Python):
N = int(input())
h = [int(i) for i in input().split()]
end_flag = False
counter = 0
f = [0 for i in range(N)]
while end_flag == False:
    loop_flag = False
    flag = False
    for i in range(N):
        if f[i] < h[i]:
            flag = True
            loop_flag = True
            f[i] += 1
        else:
            if flag == True:
                counter += 1
                flag = False
    if flag == True:
        counter += 1
    if loop_flag == False:
        end_flag = True
print(counter)

Solution (Python):


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

for c in range(100):
    f = False
    cnt = 0
    if H.count(0) == N: break
    for i in range(N):
        if H[i] > 0:
            H[i] -= 1
            f = True
        else:
            if f == True:
                cnt += 1
                f = False

        if i == N-1 and f == True:
            cnt += 1

    ret += cnt

print(ret)

Solution (Python):
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N=ii()
    H=list(mi())

    for h in H:
        if h != 0:break
    else:
        print(0)
        exit()
    
    ans = 0
    while True:
        i = 0
        while i < N and H[i] == 0:
            i += 1
            
        while i < N:
            
            for j in range(i,N):
                if H[j] == 0: break
                H[j] -= 1
            while j < N and H[j] == 0:
                j += 1
            
            if j == N-1 and H[j] != 0:
                i = N
            else:
                i = j
            ans += 1
        
        count = 0
        for j in range(N):
            count += H[j] == 0
        
        if count == N:
            break
        


    print(ans)
    



        





if __name__ == "__main__":
    main()

Solution (Python):
def garden(a,h):
    if len(a)==0:
        return 0
    m=min(a)
    i=a.index(m)
    return m-h+garden(a[:i],m)+garden(a[i+1:],m)
n=input()
print(garden(list(map(int,input().split())),0))

Solution (Python):
n=input()
def garden(a,h):
    if len(a)==0:
        return 0
    if len(a)==1:
        return a[0]-h
    m=min(a)
    i=a.index(m)
    return m-h+garden(a[:i],m)+garden(a[i+1:],m)
print(garden(list(map(int,input().split())),0))

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

mx = max(h)

h_list = [[0 for _ in range(mx)] for _ in range(N)]

for i in range(N):
    for j in range(h[i]):
        h_list[i][j] = 1


res = 0

for h in range(mx):
    i = 0
    while i < N:
        if h_list[i][h] == 0:
            i += 1
        else:
            res += 1
            while(i < N and h_list[i][h] == 1):
                i += 1
print(res)

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

ans, cnt = 0, sum(h)
while cnt > 0:
    l, r = -1, n
    for i, hi in enumerate(h):
        if l == -1 and hi > 0:
            l = i
        if l > -1 and hi == 0:
            r = i
            ans += 1
            cnt -= r - l
            for j in range(l, r):
                h[j] -= 1
            break
print(ans)

Solution (Python):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

ans=0
while sum(h)>0:
    flag=0
    for i in range(n):
        if flag==0:
            if h[i]>0:
                h[i]-=1
                flag=1
                ans+=1
            else:
                continue
        else:
            if h[i]>0:
                h[i]-=1
            else:
                flag=0
    
print(ans)

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

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

print(res)