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()))

def f(s=0, e=len(H), depth=0):
    if e == s:
        return 0
    if e - s == 1:
        return H[s] - depth
    for i in range(s, e):
        if H[i] <= depth:
            return f(s, i, depth) + f(i + 1, e, depth)
    return 1 + f(s, e, depth + 1)

print(f())

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

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

m = [[0 for _ in range(max(H) + 1)] for _ in range(N)]

for i in range(N):
    for j in range(H[i]):
        m[i][j] = 1

c = 0
for j in range(max(H)):
    prev = 0
    for i in range(N):
        if prev == 0 and m[i][j] == 1:
            c += 1
        prev = m[i][j]

print(c)

Solution (Python):
n = int(input())
hs = list(map(int, input().split()))
maxh = max(hs)
hs.append(0)
t = 0
for i in range(maxh):
    c = 0
    for j, h in enumerate(hs):
        if c == 0 and i + 1 <= h:
            c = 1
            continue
        if c == 1 and i + 1 > h:
            t += c
            c = 0
print(t)

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

Solution (Python):
N = int(input())
hs = map(int, input().split())
ans = 0
now = 0
for h in hs:
    if h > now:
        ans += (h-now)
        now = h
    now = min(h, now)
print(ans)

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


hmax = -1
for h in hlist:
    hmax = max(hmax, h)


count = 0

while hmax > 0:

    prev_max = False

    for h in hlist:
        if h >= hmax and not prev_max:
            count += 1
            prev_max = True
        if h < hmax:
            prev_max = False

    hmax -= 1


print(count)

Solution (Python):
import numpy as np

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


def splitter(arr):
    '''
    Split arrays into sections that contain only non-zero values.\n
    Returns a list of tuples which are (start, open-end) of sections
    '''

    L = len(arr)
    sections = []
    l,r = 0,0

    while r < L:
        if arr[r] == 0:
            if l == r:
                l += 1
                r += 1
            else:
                sections.append((l,r))
                l = r

        else:
            if r == L-1:
                r += 1
                sections.append((l,r))

            else:
                r += 1

    return sections


def watering(arr):
    '''
    arr: np.array, that can contain both zero and nonzero values
    '''

    L = len(arr)

    if L == 1:
        return arr[0]

    
    sections = splitter(arr)

    
    if not sections:
        return 0

    
    operations = 0
    for l,r in sections:
        operations +=  1 + watering(arr[l:r] - 1)

    return operations


ans = watering(h)

print(ans)

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

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

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

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

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

Solution (Python):
n,*a=map(int,open(0).read().split())
ans=0
pre=0
for i in a:
    if i>pre:
        ans+=i-pre
    pre=i
print(ans)

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

cnt = 0
for _ in range(100):
    flag = 0
    for i in range(N):
        if A[i] != 0:
            A[i] -= 1
            if flag == 0:
                cnt += 1
                flag = 1
        else:
            flag = 0
            
print(cnt)

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

cnt = 0
for _ in range(100):
    flag = 0
    for i in range(N):
        if A[i] != 0:
            if flag == 0:
                cnt += 1
                A[i] -= 1
                flag = 1
            else:
                A[i] -= 1
        else:
            flag = 0
    
            
print(cnt)

Solution (Python):
def countMin(*vals):
  l = len(vals)
  li = []
  for i in range(l):
    li.append(vals[i])
  if l == 1:
    return li[0]
  else:
    m = min(li)
    li2 = list(map(lambda x:x - m, li))
    zlist = []
    for j in range(l):
      if li2[j] == 0:
        zlist.append(j)
    if zlist == [i for i in range(l)]:
      return m
    else:
      prev = 0
      re = 0
      re += m
      for k in range(len(zlist)):
        if prev == 0 and li2[0] != 0:
          re += countMin(*li2[:zlist[k]])
          prev = zlist[k]
        elif zlist[k] - prev <= 1:
          prev = zlist[k]
        else:
          re += countMin(*li2[prev + 1:zlist[k]])
          prev = zlist[k]
      if prev < l - 1:
        re += countMin(*li2[prev + 1:])
      return re

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

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

count = 0
max_h = max(hs)

for h in range(1, max_h+1):
    in_region = False

    
    for i in range(N):
        
        if h <= hs[i]:
            in_region = True
        else:
            if in_region:
                in_region = False
                count += 1

    if in_region:
        count += 1

print(count)

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

a = [0]*N
answer = 0
while(True):
    area_flag = False
    area = 0
    comp_cnt = 0
    
    for i in range(N):
        if a[i]>=h[i] and h[i]==0:
            comp_cnt+=1
            if area_flag:
                area += 1
            area_flag = False
            continue
        elif a[i]>=h[i]:
            comp_cnt+=1
            if area_flag:
                area += 1
            area_flag = False
            continue
        area_flag = True
        a[i]+= 1
    if area_flag and comp_cnt!=0:
        area += 1
    answer += area
    
    if comp_cnt == N: 
        answer += min(h)
        break


print(answer)

Solution (Python):
def solve(h):
    if len(h) == 0:
        return 0
    if len(h) == 1:
        return h[0]
    if len(h) == 2:
        return max(h)

    mn = min(h)
    x = -1
    for i in range(len(h)):
        if h[i] == mn:
            x = i
        h[i] -= mn
    return mn + solve(h[:x]) + solve(h[x + 1:])

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

print(solve(h))

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

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

result = 0
height = 0

while True:
    zero_cnt = 0
    area = 0
    area_flg = True
    for i in range(n):
        if a[i] > height:
            if area_flg:
                area += 1
                area_flg = False
        else:
            area_flg = True
            zero_cnt += 1
    if zero_cnt == n:
        print(result)
        break
    result += area
    height += 1

Solution (Python):
n=int(input())
arr=list(map(int,input().split()))
def f(arr):
  if len(arr)==0:
    return 0
  if 0 in arr:
    idx=arr.index(0)
    return f(arr[:idx])+f(arr[idx+1:])
  else:
    return 1+f([i-1 for i in arr])
print(f(arr))

Solution (Python):
def f(h):
  if len(h) == 0:
    return 0

  if 0 in h:
    i = h.index(0)
    return f(h[0:i]) + f(h[i+1:])
  else:
    return 1 + f([ x-1 for x in h ])

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

Solution (Python):
import sys
input = sys.stdin.readline

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

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

Solution (Python):

n = int(input())
h = list(map(int, input().split()))
counter = 0
while bool(h):
    for i in range(n):
        if not bool(h[0]): h = h[1:]; break
        h[i] -= 1
        try: 
            if h[i + 1] == 0: counter += 1; break
        except: counter += 1; break
print(counter)

Solution (Python):


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

counter = 0

while bool(h):
    for i in range(n):
        if not bool(h[0]): h = h[1:]; break
        h[i] -= 1
        try:
            if h[i + 1] == 0:
                counter += 1
                break
        except: counter += 1; break

print(counter)

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

new_h = [0] + h + [0]

new_N = N + 2

zero_h = [0 for _ in range(new_N)]
counter = 0
flag = False
while_counter = 0
while(new_h != zero_h):
    
    
    
    
    for i in range(new_N):
        if flag == False:
            if new_h[i] == 0:
                start = i + 1
                flag = True
        elif flag == True:
            if new_h[i] == 0:
                end = i
                if new_h[start:end] == []:
                    start = i + 1
                    flag = True
                    pass
                else:
                    flag = False
                    new_h[start:end] = [new_h[i]-1 for i in range(start,end)]
                    counter += 1

print(counter)

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

Solution (Python):
def water(h):
    x = min(h)
    for i in range(0,len(h)):
        h[i] -= x
        if h[i] == 0:
            p = i
    
    if h == [0]*len(h):
        return x
    elif p == len(h) - 1:
        return x + water(h[:len(h)-1])
    elif p == 0:
        return x + water(h[1:])
    else:
        return x + water(h[0:p]) + water(h[p+1:])

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

print(water(h))

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

for i in h:
    if max < i:
        pass

    else:
        ans += max - i
        
    max = i

print(ans)

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

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

Solution (Python):
N=int(input())
hs=[int(i) for i in input().split()]
res=hs[0]
for i in range(1,len(hs)):
  if hs[i-1]<=hs[i]:
    res+=(hs[i]-hs[i-1])
  else:
    continue
print(res)

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

numbers = 0
theend = False

while theend == False:
    if b.count(0) == a:
        theend = True
        break

    indexnotzero = 0

    for i in range(len(b)):
        if b[i] == 0:
            continue
        else:
            indexnotzero = i
            break

    if indexnotzero == len(b) - 1:
        numbers += b[len(b) - 1]
        break

    try:
        length = b.index(0, indexnotzero)
    except ValueError:
        length = len(b)
    minimum = min(b[indexnotzero:length])

    numbers += minimum

    b[indexnotzero:length] = [i - minimum for i in b[indexnotzero:length]]

print(numbers)

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

numbers = min(b)

b = [i - min(b) for i in b]

theend = False

while theend == False:
  
    if b.count(0) == a:
        theend = True
        break

    indexnotzero = 0

    for i in range(len(b)):
        if b[i] == 0:
            continue
        else:
            indexnotzero = i
            break

    if indexnotzero == len(b) - 1:
        numbers += b[len(b) - 1]
        break

    try:
        length = b.index(0, indexnotzero)
    except ValueError:
        length = len(b)
    minimum = min(b[indexnotzero:length])

    numbers += minimum

    b[indexnotzero:length] = [i - minimum for i in b[indexnotzero:length]]

print(numbers)

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

for i in h:
    if i < min:
        sum_mizuyari += min - i
        min = i
    elif i > min:
        min = i

print(sum_mizuyari + min)

Solution (Python):
n = int(input())
a = [ int(i) for i in input().split() ]
moves = 0
while max(a) != 0:
    i = -1
    j = -1
    for y in range(n):
        if a[y] > 0 and i == -1:
            i = y
        elif a[y] == 0 and i != -1:
            j = y
            break
    if j == -1:
        j = n
    if i == -1:
        break
    mn = 1e9
    for x in range(i,j):
        mn = min(a[x],mn)
    moves += mn
    for x in range(i,j):
        a[x] -= mn
print(moves)

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

prevc = -1
c = 0
while prevc != c:
  prevc = c
  is_c = False
  newhs = []
  for h in hlist:
    if h == 0:
      is_c = False
      newhs.append(0)
    else:
      if is_c:
        newhs.append(h-1)
      else:
        newhs.append(h-1)
        is_c = True
        c += 1
  hlist = newhs

print(prevc)

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

print(s+h[-1])

Solution (Python):
n=int(input())
a=0
ans=0
for b in map(int,input().split()):
  if b>=a:
    ans+=b-a
  a=b
print(ans)

Solution (Python):
import sys

def f(k):
  global N,h,update
  if k==N:
    return -1
  if h[k]==0:
    return k
  else:
    h[k]-=1
    update=True
    f(k+1)


update=False
N=int(input())
h=list(map(int,input().split(" ")))
r=0
s=0

try:
  while True:
    if h[s]!=0:
      break
    s+=1
except Exception:
  print(0)
  sys.exit(0)

while True:
  f(s)
  if update:
    r+=1
    while s!=N-1 and h[s]==0:
      s+=1
    update=False
  else:
    break

print(r)

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

h_unique = list(set(h))


check = [1] + [0]*(n) + [1]
h = [-1] + h + [-1]
ans = 0
split = 0
for i in range(max(h)):
  if i in h_unique:
    for j in range(len(h)):
      if h[j] == i:
        check[j] = 1
        if check[j-1] == 0 and check[j+1] == 0:
          split += 1
        elif check[j-1] == 1 and check[j+1] == 1:
          split -= 1
  ans += split+1
  
print(ans)

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

Solution (Python):
import sys

def ii2ss(n):
    return [sys.stdin.readline() for _ in range(n)]

def sp2nn(sp, sep=' '):
    return [int(s) for s in sp.split(sep)]

def ss2nn(ss):
    return [int(s) for s in list(ss)]

def count(hh, n):
    c = 0
    b = False
    for h in hh:
        if h >= n:
            if b == False:
                b = True
                c += 1
        else:
            if b == True:
                b = False
    return c
    
def main(ss):
    hh = sp2nn(ss[1])
    total = 0
    n = 1
    while True:
        c = count(hh, n)
        if c == 0:
            break
        total += c
        n += 1
    print(total)

main(ii2ss(2))

Solution (Python):
N=int(input())
H=[int(i) for i in input().split()]
S=sum(H)
ans=0
while S>0:
    for i in range(N):
        if H[i]>0:
            break
    for j in range(i,N):
        if H[j]==0:
            break
    if j==N-1 and H[j]>0:
        j+=1
    for k in range(i,j):
        H[k]-=1
    ans+=1
    S-=j-i
print(ans)

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

Solution (Python):
n=int(input())
pre=0
ans=0
for i in map(int,input().split()):
    if i < pre:
        ans+=pre-i
    pre=i
else:
    print(ans+i)

Solution (Python):
a= int(input())
b = input().split()
b= [int(s) for s in b]
check=[]
length=[]
head = 0
foot = 0
many=0
for i in range(0,a):
    length.append(0)
    check.append(0)
for i in range(0,a):
    if b[i]==length[i]:
        check[i]=1
    else:
        dum=0
for i in reversed(range(0,a)):
    if check[i]==0:
        head = i
    else:
        dum=0
for i in range(head,a):
    if check[i]==1:
        foot = i
        break
    else:
        foot = a
        dum = 0
while sum(check)!=a:
    for i in range(head,foot):
        length[i]+=1
    for i in range(0,a):
        if b[i]==length[i]:
            check[i]=1
        else:
            dum=0
    for i in reversed(range(0,a)):
        if check[i]==0:
            head = i
        else:
            dum=0
    for i in range(head,a):
            if check[i]==1:
                foot = i
                break
            else:
                foot = a
    many+=1
print(many)

Solution (Python):
N=int(input())
height=[int(i) for i in input().split()]
height.append(0)
count=0
for i in range(N):
  while height[i]>0:
    j=0
    while height[i+j]>0:
      height[i+j]=height[i+j]-1
      j=j+1        
    count+=1
print(count)

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
cnt = 0
for _ in range(10**4):
    if any(H) is False:
        break
    cnt += 1
    f = False
    for i in range(len(H)):
        if H[i] > 0:
            H[i] -= 1
            f = True
        elif f and H[i] == 0:
            break
print(cnt)

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

ans = heights[0]
sa =[0 for _ in range(N-1)]

for i in range(N-1):
    sa[i] += heights[i+1] - heights[i]

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

print(ans)

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
ans = h[0]
for i in range(len(h)-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 = h[0]
for i in range(1,len(h)):
  if h[i-1] < h[i]:
    ans += (h[i] - h[i-1])

print(ans)

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

counter = h[0]

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

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

ans = 0
for cheight in range(101):
	renz = False
	for x in range(n):
		if h[x] > cheight:
			if not renz:
				renz = True
				ans += 1
		else:
			renz = False

print(ans)

Solution (Python):

import sys


def solve(N: int, h: "List[int]"):
    cost = 0
    CUR = [0 for i in range(len(h))]
    for i in range(len(CUR)):
        while CUR[i] != h[i]:  
            l = i
            r = i
            for j in range(i, len(CUR)):
                if CUR[i] >= h[j]:
                    break
                r += 1
            grow = 10 ** 16
            for j in range(l, r):
                grow = min(grow, h[j] - CUR[j])
            
            for j in range(l, r):
                CUR[j] += grow
            cost += grow
    print(cost)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    h = [int(next(tokens)) for _ in range(N)]  
    solve(N, h)


if __name__ == '__main__':
    main()

Solution (Python):
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

n = int(input())
arr = list(map(int,input().split()))
bef = arr[0]
cst = arr[0]
for i in range(1,n):
    if bef < arr[i]:
        cst += arr[i]-bef
    bef = arr[i]

print(cst)

Solution (Python):
def reduce(x):
	return x-1
def sum(lst):
	cnt = 0
	for i in range(0, len(lst)):
		cnt += lst[i]
	return cnt
def calc(lst):
	if lst == []: return 0
	elif 0 in lst:
		lstlst = []
		newlst = []
		for i in range(0, len(lst)):
			if lst[i] == 0:
				lstlst.append(newlst)
				newlst = []
			else:
				newlst.append(lst[i])
		lstlst.append(newlst)
		return sum(map(calc, lstlst))
	else: return 1 + calc(map(reduce, lst))
	
N = int(raw_input())
h = map(int, raw_input().split())

print calc(h)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
f_h = [0 for i in range(N)]
judge = [False if f_h[i] != h[i] else True for i in range(N)]

ct = 0
while False in judge:
    flg = 0
    for i in range(N):
        if judge[i] is False:
            if i == N-1:
                f_h[i] += 1
                ct += 1
                if f_h[i] == h[i]:
                    judge[i] = True
            else:
                flg = 1
                f_h[i] += 1
                if f_h[i] == h[i]:
                    judge[i] = True
        else:
            if flg == 1:
                ct += 1
                flg = 0
            else:
                pass
print(ct)

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

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

print(cnt)

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

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

print(cnt)

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

cnt = 0
for i in range(1, 101):
    flg = False
    for j in range(N):
        if H[j] < i and flg:
            cnt += 1
        flg = H[j] >= i
    if flg:
        cnt += 1
print(cnt)

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

cnt = 0
for i in range(1,101):
    flg = False
    for j in range(N):
        if H[j] >= i:
            flg = True
        elif flg:
            flg = False
            cnt += 1
    if flg:
        cnt += 1
print(cnt)

Solution (Python):
input()
S = list(map(int,input().split()))
i = 0
ans = 0
while True:
    if i >= len(S):
        
        break
    for j in range(i+1,len(S)+1):
        
        a = max(S[i:j])
        if j >= len(S):
            
            ans += max(S[i:j])
            i += j
            break
        elif a <= S[j]:
            
            continue
        else:
            
            ans += max(S[i:j])
            ans -= S[j]
            i = j
            break
print(ans)

Solution (Python):
_,h=open(0);s=p=0
for x in map(int,h.split()):s+=max(x-p,0);p=x
print(s)

Solution (Python):
def allzero(h):
    for i in range(N):
        if h[i] != 0:
            return False
    return True

def move(i):
    if i == N:
        return 0

    for k in range(i, N):
        if h[k] != 0:
            return k
    return 0

N = int(input())
h = list(map(int,input().split()))
i = 0
j = 0
cnt = 0
while not allzero(h):
    while i < N and h[i] != 0:
        h[i] -= 1
        i += 1
    if i != 0:
        cnt += 1
    i = move(i)
print(str(cnt))

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

i = 0
j = 0
ans = 0
while i < N:
    minh = h[j]
    while True:
        if h[j] == 0:
            if j > 0:
                j -= 1
            break
        if minh > h[j]:
            minh = h[j]
        j += 1
        if j >= N:
            j -= 1
            break
    if minh > 0:
        for k in range(i, j+1):
            h[k] -= minh
        ans += minh
    if h[i] == 0:
        i += 1
    j = i

print(ans)

Solution (Python):
N = int(input())
h = list(map(int, input().split()))
 
def solve(List):
  if List.count(0) == len(List):
    return(0)
  if List.count(0) == 0:
    for i in range(len(List)):
      List[i] -= 1
    return(1 + solve(List))
  elif List[0] == 0:
    List.pop(0)
    return(solve(List))
  else:
    p = 0
    q = 1
    while p == 0:
      if List[q] == 0:
        p = q
      else:
        q += 1
    return(solve(List[:q]) + solve(List[q:]))
print(solve(h))

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

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

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

Solution (Python):
n=int(input())
h=list(map(int, input().split()))
def split(p):
    res=[]
    tmp=[]
    for pi in p:
        if pi==0:
            if len(tmp)>0:
                res.append(tmp)
                tmp=[]
        else:
            tmp.append(pi)
    if len(tmp)>0:
        res.append(tmp)
    return res
        
def D(p):
    global cnt
    m=min(p)
    cnt+=m
    p=[pi-m for pi in p]
    s=split(p)
    for si in s:
        D(si)
cnt=0
D(h)
print(cnt)

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

counter = 0

def height_counter(array):
	global counter
	if len(array) == 0:
		return
	if len(array) == 1:
		counter += array[0]
		return
	else:
		b = min(array)
		c = array.count(b)
		counter += b
		for i in range(len(array)):
			array[i] -= b
		for i in range(c):
			d = array.index(0)
			height_counter(array[0:d])
			if d == len(array) - 1:
				array.append(0)
			array = array[d + 1:]
		height_counter(array)

height_counter(a)
print(counter)

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

while any(h):
    for i in range(N):
        if i+1 >= N:
            if h[i] > 0:
                count += 1
                h[i] -= 1
                break
        if h[i] > 0 and h[i+1] == 0:
            count += 1
        if h[i] > 0:
            h[i] -= 1

print(count)

Solution (Python):



N = int(input())
h = list(map(int, input().split()))
count = h[0]

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

print(count)

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


def rec(array):

    count = 0

    if sum(array) == 0:
        return count

    if 0 in array:
        none = array.index(0)

        count += rec(array[:none])
        count += rec(array[none + 1:])

    else:
        min_num = min(array)

        for i in range(len(array)):
            array[i] -= min_num
        count += min_num

        count += rec(array)

    return count


print(rec(h))

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

while set(h) != {0}:
    height = []
    ind = []
    for i in range(n):
        if h[i] != 0:
            height.append(h[i])
            ind.append(i)
        elif h[i] == 0 and len(height) != 0:
            break

    cnt += min(height)
    for i in range(len(height)):
        h[ind[i]] -= min(height)

print(cnt)

Solution (Python):


N = int(input())
H = list(map(int, input().split()))
ans = 0
for j in range(max(H)):
    for i in range(N-1):
        if H[i] != 0 and H[i+1] == 0:
            ans += 1
        if H[i] != 0:
            H[i] -= 1
    if H[N-1] != 0:
        H[N-1] -= 1
        ans += 1
print(ans)

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

Solution (Python):

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

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

def is_finish(h):
	if sum(h) == 0:
		return True
	else:
		return False

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

print(count)

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

Solution (Python):
def kuksan(array):
    for i in range(0, n):
        if array[i] > 0:
            left = i
            for j in range(left,n + 1):
                if array[j] <= 0:
                    right = j - 1
                    return left, right, True
    return 0,0,0

n = int(input())
h_list = list(map(int, input().split()))
cnt = 0
while True:
    h_list.append(0)
    kukan = kuksan(h_list)
    if kukan[2]:
        for i in range(kukan[0], kukan[1] + 1):
            h_list[i] -= 1
        cnt += 1
    else:
        break

print(cnt)

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
h.append(0)
st = 0
res = 0
for i in range(1,101):
  for j in h:
    if j >= i:
      st = 1
    else:
      if st == 1:
        res  =  res + 1
        st = 0
print(res)

Solution (Python):
def main():
	n = int(input())
	h = list(map(int, input().split()))
	def shower(area, l, r):
		min_h = 10**9
		min_pos = 0
		total = 0
		for i in range(l, r):
			if area[i] < min_h:
				min_h = area[i]
				min_pos = i
		for i in range(l, r):
			area[i] -= min_h
		total += min_h
		if l < min_pos:
			total += shower(area, l, min_pos)
		if min_pos+1 < r:
			total += shower(area, min_pos+1, r)
		return total
	total = shower(h, 0, n)
	print(total)

if __name__ == '__main__':
    main()

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

c = 0
for i in range(1, 101):
    on = False
    for h in hn:
        if h >= i:
            if not on:
                on = True
                c += 1
        else:
            on = False
print(c)