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 = [int(m) for m in input().split()]
ans = 0
k = 1
tmp = -1
stmp = 0
numtmp = float("INF")
p = 0
while True:
    if k == 1:
        ans += min(h)
        t = min(h)
        for i in range(n):
            h[i] = h[i] - t
    else:
        for i in range(tmp + 1, n):
            p = 0
            if h[i] != 0:
                numtmp = min(numtmp, h[i])
                if numtmp == h[i]:
                    tmp = i
            else:
                tmp = i
                if numtmp != float("INF"):
                    ans += numtmp
                for j in range(stmp, tmp):
                    h[j] = h[j] - numtmp
                stmp = tmp + 1
                numtmp = float("INF")
                p = 1
            if i == n - 1 and p != 1:
                tmp = i + 1
                if numtmp != float("INF"):
                    ans += numtmp
                for j in range(stmp, tmp):
                    h[j] = h[j] - numtmp
                stmp = tmp + 1
                numtmp = float("INF")
                p = 1
    p = 0
    k += 1
    tmp = -1
    stmp = 0
    numtmp = float("INF")
    for i in range(n):
        if h[i] != 0:
            break
        if h[i] == 0:
            if i == n - 1:
                print(ans)
                exit()

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

cnt=0

for k in range(200):
	prev=0
	for i,x in enumerate(H):
		if x!=0:
			H[i]=x-1
			if prev!=0:
				pass
			else:
				cnt+=1
		prev=x
		

print cnt

Solution (Python):
n = int(input())
flower = input().split()
for i in range(n):
    flower[i] = int(flower[i])
a = 0
def water():
    if sum(flower) == 0:
        a = -1
    elif 0 in flower:
        i = 0
        while flower[i] == 0:
            i += 1
        j = i
        try:
            while flower[j+1] != 0:
                j += 1
        except:
            abc = i
        for k in range(i,j+1):
            flower[k] -= 1
    else:
        for i in range(n):
            flower[i] -= 1
ans = 0
while sum(flower) != 0:
    water()
    ans += 1
print(ans)

Solution (Python):
N=int(input())
h=input().split()
h.append(-1)
index=0
count=0
all_zeros=True
for i in h:
    if i!='0':
        all_zeros=False
        break

if all_zeros:
    print('0')
    exit(0)

while True:
    flag=True
    while flag:
        for i in range(index,len(h)):
            h[i]=int(h[i])
            if not h[i]-1<0:
                h[i]-=1
            elif h[index]==0:
                flag=False
                if index==i:
                    pass
                else:
                    count+=1
                break
            else:
                count += 1
                break

        
        
        
    counter=True
    while counter:
        index+=1
        if not h[index]==0:
            counter=False

    if h[index]==-1:
        print(count)
        exit(0)

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

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

print(count)

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

Solution (Python):
N = int(input())
h = [int(x) for x in input().split(" ")]
ans = [0 for _ in range(N)]
for i in range(N):
    if i == 0:
        ans[i] = h[i]
    elif h[i] <= h[i-1]:
        ans[i] = ans[i-1]
    else:
        ans[i] = ans[i-1] + (h[i] -h[i-1])
print(ans[N-1])

Solution (Python):
n = int(input())
H = [0] + list(map(int, input().split())) + [0]
def f(b):
    global H
    H[b] -= 1
    a = b - 1
    c = b + 1
    while 0 < a and H[a] != 0:
        H[a] -= 1
        a -= 1
    while c <= n and H[c] != 0:
        H[c] -= 1
        c += 1
ans = 0
for i in range(1, n + 1):
    while H[i] != 0:
        f(i)
        ans += 1
print(ans)

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

c=0
while True:
      mx=max(L)
      if mx==0:
         break
      idx=L.index(mx)
      L[idx]-=1
      while True:
         try:
            if L[idx+1]==mx:
               L[idx+1]-=1
               idx+=1
            else:
               break
         except:
            break
      c+=1
print(c)

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



ans = 0

def list_split(l):
    list_list = []
    start = 0
    for i in range(len(l)):
        
        if l[i] == 0:
            list_list.append(l[start : i])
            start = i + 1
    list_list.append(l[start : i+1])
    return list_list

def f(lis):
    global ans

    if lis == [0]:
        return 1
    if lis == []:
        return
    
    mini  = min(lis)
    ans+=mini
    for i in range(len(lis)):
        lis[i] -= mini


    list_list = list_split(lis)
    
    for l in list_list:
        
        f(l)




f(h)

print(ans)

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

max_h = max(h_list)
answer = 0

while max_h > 0:
	for i in range(N):
		if max_h == h_list[i]:
			l = i
			for j in range(i, N):
				if max_h != h_list[j]:
					r = j-1
					break
				if j == N-1:
					r = j
			for k in range(l, r+1):
				h_list[k] = h_list[k] - 1
			answer += 1

	max_h = max(h_list)

print(answer)

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

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

j = 0
inde = 0
while(A.count(0) != n):
    for i in range(n):
        if(A[i] == 0):
            if(inde == 1):
                j+=1
                inde = 0
            else:
                continue
        else:
            A[i]-=1
            inde =1
            if(i == n - 1):
                j+=1
        
    inde = 0
print(j)

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

count = 0

while any(h):
    max_h = max(h)
    i = 0

    while i < len(h):
        if h[i] == max_h:
            j = i
            while j < len(h):
                if max_h == h[j]:
                    h[j] = h[j] - 1
                    j += 1
                else:
                    count += 1
                    break
            if j == len(h):
                count += 1
            i = j
        else:
            i += 1
print(count)

Solution (Python):
def hiki(lis):
    if len(lis) == 1:
        return [0], lis[0]
    else:
        new_lis = [i-min(lis) for i in lis]
        return new_lis, min(lis)

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

res = 0

while True:
    if not any(h):
        print(res)
        break

    
    for j in range(n):
        if h[j] == 0:
            continue
        else:
            ind_left = j
            break
    
    right_flag = False
    for j in range(ind_left, n):
        if h[j] != 0:
            continue
        else:
            ind_right = j
            right_flag = True
            break
    if not right_flag:
        ind_right = n+1

    h[ind_left:ind_right], count = hiki(h[ind_left:ind_right])
    res += count

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

ans = 0
st = "0"
while H!=[0]*N:
    for i in range(N):
        if H[i]>0:
            st = "+"
            H[i] -= 1
        elif st=="+" and H[i]==0:
            st = "0"
            ans += 1
    if st=="+":
        ans += 1
    st = "0"

print(ans)

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

ans = 0
s = 0
e = 0
while max(h) != 0:  
    for k in range(N):
        if h[k] != 0:
            s = k
            break
    if s == N-1:
        ans += h[N-1]
        break
    for k in range(s+1,N):
        e = k
        if h[k] == 0:
            break
        if k == N-1:
            e += 1
    t = min(h[s:e])
    for k in range(s,e):
        h[k] -= t
    ans += t
print(ans)

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

Solution (Python):
import numpy as np


N = int(input())
h = np.array(list(map(int, input().split())))
ans = 0
while np.sum(h) != 0:
    last_flg = 0
    water = np.zeros((N), dtype=np.int64)
    for i in range(N):
        
        if h[i] != 0:
            water[i] = 1
        else:
            
            if i == 0:
                
                pass
            elif water[i-1] == 0:
                
                pass
            elif water[i-1] == 1:
                
                ans += 1
                h -= water
                break
        
        if i == N-1:
            last_flg = 1

    if last_flg:
        ans += 1
        h -= water
print(ans)

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

def bunkatsu(list):
    returnlists = [[]]
    i = 0
    for num in list:
        if num != 0:
            returnlists[i].append(num)
        else:
            i = i + 1
            returnlists.append([])
    return returnlists

def mizuyari(list):
    if list == []:
        return 0
    else:
        i = 0
        while (list.count(0) == 0):
            i = i + 1
            list = [x - 1 for x in list]
        retlists = bunkatsu(list)
        for l in retlists:
            i += mizuyari(l)
        return i
print(mizuyari(hlist))

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

cnt = 0

while 1 == 1:
  split0, temp = [], []

  
  for i_ in h:
    for i in i_:
      if i == 0:
        if len(temp) >= 1:
          split0.append(temp)
        temp = []

      else:
        temp.append(i)
        
    if len(temp) >= 1:
      split0.append(temp)
    temp = []
        
  if len(temp) >= 1:
    split0.append(temp)

  
  
  h = []
  for i_ in split0:
    i_ = list(map(lambda x: x - 1, i_))
    cnt += 1
    
    if sum(i_) != 0:
      h.append(i_)
      
  if len(h) == 0:
    print(cnt)
    break

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

c = 0
while any(h):
    ans = 0
    for l in range(n):
        tmp = 0
        r = l
        while h[r]:
            tmp += 1
            r += 1
            if r == n:
                break
        if ans < tmp:
            x = l
            y = r
            ans = tmp
    for i in range(x, y):
        h[i] -= 1
    c += 1
print(c)

Solution (Python):
N = int(input())
H = list(map(int, input().split()))
S = [0 for i in range(N)]
res = 0
for h in range(100,0, -1):
    tmp = False
    for i in range(N):
        if H[i] >= h:
            tmp = True
        elif tmp:
            res += 1
            tmp = False
    if tmp:
        res += 1
print(res)

Solution (Python):


import numpy
import scipy

from collections import defaultdict
from pprint import pprint

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

f = [0 for i in range(N)]

def update_f():
    start = False
    for i in range(N):
        if start and f[i] < h[i]:
            f[i] = f[i] + 1
            continue
        if start and f[i] >= h[i]:
            break
        if f[i] < h[i]:
            start = True
            f[i] = f[i] + 1
    if start:
        return False
    else:
        return True

cnt = 0
while True:
    if update_f():
        break
    else:
        cnt += 1

print(cnt)

Solution (Python):
def listcut(array):
    res = []
    kar = []
    for i in range(len(array)):
        if array[i] == 0:
            if len(kar) != 0:
                res.append(kar)
            kar = []
        else:
            kar.append(array[i])
    if len(kar) != 0:
        res.append(kar)
    return res


n = int(input())
h = list(map(int, input().split()))
kai = 0
wak = [h]
while wak != []:
    t = []
    for i in range(len(wak)):
        m = min(wak[i])
        kai += m
        k = list(map(lambda x: x - m, wak[i]))
        s = listcut(k)
        if s != []:
            t += s
    wak = t
print(kai)

Solution (Python):
M = int(input())
N = list(map(int, input().split()))
import numpy as np
npN = np.array(N)
count_all=0
for i in range (npN.max()):
    before = npN[0]
    
    if npN[0]==0:
        count=0
    else:
        count=1

    for i in range (len(N)):
        if before == 0 and npN[i]!= 0:
            count+=1
            before = npN[i]
        else:
            before = npN[i]
        if npN[i]!=0:
            npN[i]-=1
    count_all+=count
print(count_all)

Solution (Python):
import numpy as np

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

h = np.array(h)

count = 0
while(sum(h) != 0):
    s = 200
    e = n
    for i in range(n):
        if(s == 200):
            if(h[i] != 0): s = i
        elif(h[i] == 0):
            e = i
            break

    count += min(h[s:e])
    h[s:e] -= min(h[s:e])

print(count)

Solution (Python):
import sys

N = int(input())
h = [int(i) for i in input().split()]
ans = 0
check = [0] * N
ind = 0
judge = 0


for i in range(10**5):
    judge = 0
    for j in range(N):
        if 1 <= h[j]:
            h[j] -= 1

            if judge == 0:
                ans += 1
                judge = 1
            else:
                judge = 1

        else:
            judge = 0

    if max(h) == 0:
        print(ans)
        sys.exit()

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

Solution (Python):

n = int(input())
h = list(map(int, input().split()))
cnt = 0
while h and not h[-1]:
    h.pop()
while h:
    for l in reversed(range(len(h))):
        if not h[l]:
            l += 1
            break
    for i in range(l, len(h)):
        h[i] -= 1
    while h and not h[-1]:
        h.pop()
    cnt += 1
print(cnt)

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

current_h = [0] * N
c = 0

l = 0
while True:
    escape_loop = False
    while current_h[l] == h[l]:
        if l + 1 < N:
            l += 1
        else:
            escape_loop = True
            break
    if escape_loop:
        break

    r = l
    while True:
        if r + 1 < N and current_h[r + 1] < h[r + 1]:
            r += 1
        else:
            break

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

print(c)

Solution (Python):

N = int(input())

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



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

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


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

    return ans    

garden = trans(mat)


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

		elif i == 0:
			mark = False


print(count)

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

ans = 0
while any(height > 0 for height in h):
    ans += 1
    maxh = max(h)
    for i in range(n):
        if h[i] == maxh:
            for j in range(i,n):
                if h[j] == 0:
                    break
                else:
                    h[j] = h[j]-1
        else:
            continue
        break

print(int(ans))

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









def singlify(h):
    
    h = [0] + h + [0]
    
    newh = []
    zero_flag = False
    for i in range(len(h)):
        if h[i] == 0:
            zero_flag = True
        else:
            if zero_flag:
                newh.append(0)
            newh.append(h[i])
            zero_flag = False
        
    newh.append(0)
    return newh



def minus_one_and_not_lower_than_zero(h):
    for i in range(len(h)):
        h[i] -= 1
        if h[i] == -1:
            h[i] = 0
    return h




ans = 0

while True:
    h = singlify(h)
    
    ans += h.count(0)-1
    h = minus_one_and_not_lower_than_zero(h)

    if sum(h) == 0:
        break

print(ans)

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

def remove_dpl(x):
  it=iter(x)
  before=next(it)
  res=[before]
  for e in it:
    if e!=before:
      res.append(e)
      before=e
  return res

c=0
h=remove_dpl(h)
while len(h)!=1:
  if h[0]>h[1]:
    c+=h[0]-h[1]
    h[0]=h[1]
  if h[len(h)-2]<h[len(h)-1]:
    c+=(h[len(h)-1]-h[len(h)-2])
    h[len(h)-1]=h[len(h)-2]
  for i in range(1,len(h)-1):
    if h[i]>h[i-1] and h[i]>h[i+1]:
      c+=h[i]-max(h[i-1],h[i+1])
      h[i]=max(h[i-1],h[i+1])
  h=remove_dpl(h)
print(c+h[0])

Solution (Python):

def watering(h, hk):
    count = 0
    need_water_pre_flower = False
    for i in h:
        need_water = i >= hk
        count += 1 if need_water and not need_water_pre_flower else 0
        need_water_pre_flower = need_water

    return count

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

max_h = max(h)
count_watering = 0
for i in range(1, max_h + 1):
    count_watering += watering(h, i)

print(count_watering)

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

ans = 0
pre = 0
for i in range(n):
    tmp = a[i] - pre
    if tmp>0:
        ans += tmp
    pre = a[i]

print(ans)

Solution (Python):
import numpy as np
N= int(input())
lis=list(map(int,input().split()))
S=lis[0]
b=lis[0]
for i in lis:
    if i>b:
        S+=(i-b)
    b=i
print(S)

Solution (Python):
def divide(L):
    ans = []
    prev = 0
    for i in range(len(L)):
        if L[i] == 0:
            ans.append(L[prev:i])
            prev = i + 1
    ans.append(L[prev:])
    return ans


def extract(L, v):
    for i in range(len(L)):
        L[i] -= v
    return L


def ans(L):
    if L == [0] * len(L):
        return 0
    count = 0
    divided = divide(L)
    for l in divided:
        if len(l) == 0:
            continue
        m = min(l)
        count += m
        extracted = extract(l, m)
        count += ans(extracted)
    return count


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

Solution (Python):
def water(h):
    global ans
    if len(h)==1:
        ans+=h[0]
    else:
        m=min(h)
        ans+=m
        for i in range(len(h)):
            h[i]-=m
        a=h.index(0)
        if a==0:
            water(h[1:])
        elif a==len(h)-1:
            water(h[:-1])
        else:
            water(h[:a])
            water(h[a+1:])

n=int(input())
h=list(map(int,input().split()))
ans=0
water(h)
print(ans)

Solution (Python):
ans = 0
N = int(input())
a = list(map(int,input().split()))
for i in range(max(a)):
  x = 0
  z = 0
  count = 0
  for j in range(N):
    y = a[j]
    if y == 0 and x == 0:
      continue
    elif y != 0 and x == 0:
      count += 1
      a[j] -= 1
      z = 1
      x = 1
    elif y != 0 and z == 0:
      z = 1
      count += 1
      a[j] -= 1
    elif y != 0 and z == 1:
      a[j] -= 1
    elif y == 0 and z == 0:
      continue
    elif y == 0 and z == 1:
      z = 0
  ans += count
print(ans)

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
hk = [int(i) for i in stdin.readline().rstrip().split(" ")]

def calcCount(hks):
    counter = 0
    counter += min(hks)
    nhks = [i - counter for i in hks]
    splitTo = []
    for i in nhks:
        if i == 0:
            if len(splitTo) > 0:
                counter += calcCount(splitTo)
                splitTo = []
        else:
            splitTo.append(i)
    if len(splitTo) > 0:
        counter += calcCount(splitTo)
    return(counter)

print(calcCount(hk))

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

h = np.array(h)


def calc(h, s):

    a = np.min(h)
    s += a
    h = h - a
    ind = np.where(h == 0)[0][0]
    h1 = h[:ind]
    h2 = h[ind+1:]
    if len(h1) != 0:
        s = calc(h1, s)
    if len(h2) != 0:
        s = calc(h2, s)
    return s

print(calc(h, 0))

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

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

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

count = 0

mini = min(hN)
count += mini

hN = [hi - mini for hi in hN]

while sum(hN) > 0:
    cont = False
    for hi in hN:
        if hi > 0:
            if not cont:
                count += 1
                cont = True
        else:
            cont = False
    hN = [hi - 1 if hi > 0 else hi for hi in hN]
print(count)

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

flag=0


iteration=H.max()
total=0



for x in range(iteration):
    count=0
    flag=0
    
    if H[0]>0:
        count+=1
    
    for i in range(len(H)):
        if H[i] <=0:
            flag=1
        elif H[i] >0 and flag==1:
            count+=1
            flag=0
        

        
        
    total+=count
    H-=1
    
print(total)

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

h = [0]
h.extend(tmp)

res = 0

while 1 == 1:
    if max(h) <= 0:
        break
    for i in range(1,len(h)):
        if h[i] > 0 and h[i-1] <= 0:
            res = res + 1
    h = list(map(lambda x:x - 1,h))

print(res)

Solution (Python):
number = int(input())
data = list(map(int, input().split()))
ans = 0

def change(left, right):
    global data, ans
    hiku = min(data[left:right])
    for i in range(left, right):
        data[i] -= hiku
    ans += hiku

def main():
    global data, ans, number
    while 1:
        flg = 0
        if data.count(0) == number:
            break
        for i in range(number):
            if data[i] == 0:
                continue
            for j in range(i, number):
                if data[j] == 0:
                    flg = 1
                    change(i, j)
                    break
                elif j == number -1:
                    flg = 1
                    change(i, j+1)
                    break
            if flg:
                break
    print(ans)

if __name__ == '__main__':
    
    
    
    main()

Solution (Python):
N = int(input())
h_list = [int(elem) for elem in input().split()]


num_waterings = 0

while sum(h_list) > 0:
    left = 0
    for right, h in enumerate(h_list):
        if h != 0:
            continue
        else:  
            for i in range(left, right):
                h_list[i] -= 1
            if left < right:
                num_waterings += 1
            left = right + 1
            
    if left < N:
        for i in range(left, N):
            h_list[i] -= 1
        num_waterings += 1
    
    


print(num_waterings)

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

import numpy as np

def bunkatsu(group): 
    
    if group == [0]*len(group):
        return 0

    tmp = []
    group_split = []
    ans = 0
    for i,e in enumerate(group):
        
        if e != 0:
            tmp.append(e-1)
        elif len(tmp)>0:
            
            ans += 1 + bunkatsu(tmp)
            tmp = []
    if len(tmp)>0:
        ans += 1 + bunkatsu(tmp)

    
    return ans

print(bunkatsu(h))

Solution (Python):
import numpy as np

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

cnt=0
while (1):
    target=h.max()
    if target == 0 :
        print(cnt)
        break
    
    flag = 0
    for i, hi in enumerate(h) :
        if hi == target:
            if flag == 0:
                flag = 1
                cnt+= 1
            h[i]-=1
        else :
            flag=0

Solution (Python):
import sys

n = int(input())
hs = list(map(int, input().split()))
goal = [0 for _ in range(n)]

count = 0
while True:
    
    if hs == goal:
        print(count)
        sys.exit()

    count += 1
    is_done = False
    for i in range(n):
        if hs[i] == 0:
            if is_done:
                break
        else:
            hs[i] = hs[i] - 1
            is_done = True

Solution (Python):
N = int(input())
h = [int(i) for i in input().split()]
m = max(h)
ans = 0
for i in range(1, m+1)[::-1]:
    pre = 0
    for j in range(len(h)):
        
        if h[j] == i:
            if pre != i:
                ans += 1
            pre = h[j]
            h[j] -= 1
        else:
            pre = h[j]
print(ans)

Solution (Python):
N = int(input())
h = [int(n) for n in input().split(' ')]
start = 0
out = 0

def end(start, h):
    m = h[start]
    for i in range(start, N):
        if h[i] != 0:
            if h[i] < m:
                m = h[i]
        else:
            return i-1, m
    else:
        return i, m

while start < len(h):
    i, m = end(start, h)
    if i != start-1:
        for j in range(start, i+1):
            h[j] -= m
        out += m
    else:
        start += 1
else:
    print(out)

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

min = []
max = []
ans =0
while l !=[0]*N:
    ans += 1
    s =False
    for i,j in enumerate(l):
        if s:
            if j == 0:
                break
        if j !=0:
            s =True
            l[i]-=1
print(ans)

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

a = li[0]
ans = a
for i in range(1, len(li)):
    if a >= li[i]:
        a = li[i]
    else:
        ans += li[i]-a
        a = li[i]

print(ans)

Solution (Python):

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

ans = 0
while(1):
    min_H = min(H)
    ans += min_H
    H = [h -  min_H for h  in H]
    idx = H.index(0)
    
    head = H[:idx]
    tail = H[idx+1:]
    
    if len(head) > 0:
        task.append(head)
    if len(tail) > 0:
        task.append(tail)
    if len(task) == 0:
        break
    H = task.pop(0)
print (ans)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
count = 0
while True:
    for tmp in range(N):
        if h[tmp] != 0:
            break
    else:
        break
    for i in range(tmp, N):
        if h[i] == 0:
            break
        else:
            h[i] = h[i] - 1
    count += 1


print(count)

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

order0=[[h[t],t] for t in range(N)]
order0.sort()
order=[o[1] for o in order0]

Ans=0
Z=[-1]
for t in range(N):
    i=order[t]
    Num=h[i]
    c=0
    for j in range(N):
        if j not in Z and (j-1) in Z:
            c+=1
    Ans+=c*Num
    Z.append(i)
    
    h=[max(0,h0-Num) for h0 in h]

print Ans

Solution (Python):
N=int(input())
import numpy as np
h= np.array(list(map(int,input().split())))
h=np.append(h,[0])
m=np.max(h)
count=0
if N==1:
    print(m)
else:
    for i in range(m):
        for j in range(N):
            if h[j]>0 and h[j+1]<=0:
                count+=1
        h=h-1
    print(count)

Solution (Python):
from statistics import mean, median,variance,stdev
import sys
import math
import fractions

def j(q):
    if q==1: print("YES")
    else:print("NO")
    exit(0)


def ct(x,y):
    if (x>y):print("")
    elif (x<y): print("")
    else: print("")

def ip():
    return int(input())

x = ip()                              



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




num=0
while sum(a):
    l = 0
    while a[l] == 0: l+=1
    r=l
 
    if l != len(a)-1:
        while a[r+1]!=0:
 
            r+=1
            if r == len(a)-1:break
    for i in range(l,r+1):
        a[i]-=1
    num+=1
  
print(num)

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

ct = 0

while(max(H) > 0):
    t = max(H)
    k = H.index(t)
    s = {k}
    while(k < N-1 and H[k+1] == H[k]):
        s.add(k+1)
        k += 1
    else:
        for j in s:
            H[j] -= 1
        ct += 1

print(ct)

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

def par(l):
    res=[]
    tmp=[]
    for i in range(len(l)):
        if l[i]==0 and len(tmp)>0:
            res.append(tmp)
            tmp=[]
        if l[i]>0:
            tmp.append(l[i])
    if len(tmp)>0:
        res.append(tmp)
    return res

l=par(h)
ans=0

while len(l)>0:
    tmp=[]
    for block in l:
        m = min(block)
        ans+=m
        tmp+=par([e-m for e in block])
    l=tmp
print(ans)

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

ans = 0

for j in range (max(h)):
  flag =0 
  
  for i in range(n):
    if h[i]==0 and flag ==0:
      flag=1
      
    if not(h[i]==0) and flag ==1:
      ans +=1
      flag =0

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

print(ans)

Solution (Python):
n = int(input())
h = [0] + list(map(int,input().split())) + [0]
check = 0
while True:
    checkcheck = check
    c = False
    for i in range(n+1):
        if h[i] == 0 and h[i+1] != 0 and c == False:
            check += 1
            c = True
        if h[i] != 0:
            h[i] -= 1
        if h[i+1] == 0:
            c = False
    if check == checkcheck:break
print(check)

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

Solution (Python):
_ = int(input())
h = list(map(int, input().split()))
c = 0
while sum(h) != 0:
    c += 1
    non0 = False
    for i, n in enumerate(h):
        if non0 and n == 0:
            break
        if 1 <= n:
            non0 = True
            h[i] = n - 1
print(c)

Solution (Python):
def solve(xs):
	if xs == []:
		return 0
	m = min(xs)
	ans = m
	ys = []
	for i in xs:
		if i == m:
			ans = ans + solve(ys)
			ys = []
		else:
			ys.append(i - m)
	ans = ans + solve(ys)
	return ans

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

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

cnt = 0

def mizu(h_list):
    hidari = 0
    migi = len(h_list)
    tmp = []
    for index,elem in enumerate(h_list):
        if elem == 0:
            continue
        if elem >0:
            hidari = index
            break
    for index,elem in enumerate(h_list[hidari:]):
        if elem >0:
            continue
        elif elem == 0:
            migi = hidari + index
            break
    for index,elem in enumerate(h_list):
        if index >= hidari and index <= migi-1:
            elem = elem-1
        tmp.append(elem)
    return tmp

while (not(all(elem == 0 for elem in h_list))) :
    h_list = mizu(h_list)
    
    cnt += 1
print(cnt)

Solution (Python):
def pour_counter(height):
	if not height:
		return 0
	elif 0 in height:
		index = height.index(0)
		
		
		return pour_counter(height[:index]) + pour_counter(height[index+1:])
	else:
		min_height = min(height)
		for i, element in enumerate(height):
			height[i] = element - min_height
		return pour_counter(height) + min_height

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

count = pour_counter(height)
print(count)