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

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


mx=max(hl)
count=0
for height in range(1,mx+1):
    i=0
    while i<n:
        if hl[i]>=height:
            count+=1
            while i<n and hl[i]>=height :
                i+=1
        else:
            i+=1

print(count)

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

H=np.array(H)
ans=0
for i in range(H.max()):
    
    count=0
    now=(H[0]==0)
    if not now:
        count+=1
    for j in range(1,N):
        if now:
            if H[j]!=0:
                count+=1
        
        now=(H[j]==0)
    ans+=count
    
    H[H>0]-=1
print(ans)

Solution (Python):

import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
H = np.array(lr())
Hdiff = np.diff(H)
Hdiff = Hdiff[Hdiff>0]
answer = H[0] + Hdiff.sum()
print(answer)

Solution (Python):
N = int(input())
h = [0] + list(map(int, input().split())) + [0]
counter = 0
fin = [0 for _ in range(N+2)]

while not h == fin:
    for i in range(1, N+2):
        if h[i] == 0 and not h[i-1] == 0:
            counter += 1
    for i in range(1, N+1):
        if not h[i] == 0:
            h[i] -= 1


print(counter)

Solution (Python):

















import sys
sys.setrecursionlimit(10000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    H = readInts()
    allzero = False
    cnt = 0
    while True:
        for i in range(n):
            allzero = True
            if H[i] > 0:
                allzero = False
                left = i
                break
                
        
        if allzero:
            print(cnt)
            exit()
        i = left
        right = left
        while i < n and H[i] > 0:
            right += 1
            i += 1
            if i >= n:
                break
        for j in range(left,right):
            
            H[j] -=1
        
        cnt += 1

if __name__ == '__main__':
  main()

Solution (Python):

import sys

sys.setrecursionlimit(10 ** 5 + 10)


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


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

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

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


resolve()

Solution (Python):
import numpy as np


def RunLengthEncoding(S):
    res = []
    N = len(S)
    i = 0
    while i < N:
        cnt = 1
        while i < N - 1 and S[i] == S[i + 1]:
            cnt += 1
            i += 1
        res.append(S[i])
        i += 1
    return res


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

ans = 0
flower = np.zeros_like(A)
for i in range(1, A.max() + 1):
    f = flower.copy()
    f[A >= i] = 1
    RLE = RunLengthEncoding(f)
    ans += sum(RLE)

print(ans)

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

while max(H) > 0:
    grp = [[]]
    for i, h in enumerate(H):
        if h == 0:
            if len(grp[-1]) > 0:
                grp.append([])
        else:
            grp[-1].append((i, h))
    for g in grp:
        if len(g) == 0:
            continue
        mi = min([h for _, h in g])
        ans += mi
        for i, _ in g:
            H[i] -= mi

print(ans)

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

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
cnt = 0
indL = -1
for i in range(indL+1,N):
    if H[indL+1]==0:
        indL += 1
        continue
    else:break
while indL<N-1:
    hmin = 1000
    indR = N
    for i in range(indL+1,N):
        if H[i]==0:
            indR = i
            break
        else:
            hmin = min(hmin,H[i])
    for i in range(indL+1,indR):
        H[i] -= hmin
    cnt += hmin
    for i in range(indL+1,N):
        if H[indL+1]==0:
            indL += 1
            continue
        else:break
print(cnt)

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

while max(H) > 0:
    grp = [[]]
    for i, h in enumerate(H):
        if h == 0:
            if len(grp[-1]) > 0:
                grp.append([])
        else:
            grp[-1].append((i, h))
    for g in grp:
        if len(g) == 0:
            continue
        mi = min([h for _, h in g])
        ans += mi
        for i, _ in g:
            H[i] -= mi

print(ans)

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

h = [0]*N
ans = 0
while any(a<b for a,b in zip(h,H)):
    ans += 1
    l = r = -1
    for i in range(N):
        if l < 0:
            if h[i]==H[i]: continue
            l = i
        else:
            if h[i]<H[i]: continue
            r = i
            break
    else:
        r = N
    for i in range(l,r):
        h[i] += 1
print(ans)

Solution (Python):
def main():
    _ = int(input())
    H = [int(i) for i in input().split()]
    cur_h = 0
    ans = 0
    for h in H:
        if cur_h < h:
            ans += h - cur_h
        cur_h = h
    print(ans)


if __name__ == '__main__':
    main()

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

ans = 0
for i in range(N-1):
    while True:
        if H[i] == 0:
            break

        for j in range(i+1, N):
            if H[j] == 0:
                break

            H[j] -= 1
        H[i] -= 1
        ans += 1
ans += max(0, H[-1])
print(ans)

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

h += [0]
prev = 0
imos = [None] * (N + 1)
for i in range(N + 1):
    imos[i] = h[i] - prev
    prev = h[i]
    


ans = sum([int(item > 0) * item for item in imos])
print(ans)

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    H = [int(h) for h in input().split()]
    finished = [False] * (N + 1)
    finished[N] = True
    for i in range(N): 
        if H[i] == 0: finished[i] = True
    ch = [0] * N
    count = 0
    while False in finished:
        i = 0
        while i < N:
            if not finished[i]:
                count += 1
                start = i
                end = i + 1
                index = i
                while end <= N:
                    if not finished[end]:
                        end += 1
                    else:
                        for j in range(start, end):
                            ch[j] += 1
                            if ch[j] == H[j]:
                                finished[j] = True
                        break
                i = end
                
            else: i += 1
    print(count)

    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
n= int(input())
lis = list(map(int,input().split()))
ans = 0
status = 0
for high in range(1,101):
    for i in range(n):
        if lis[i] >= high:
            status = 1
        else:
            if status == 1:
                ans += 1
                status = 0
    ans += status
    status = 0
print(ans)

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

Solution (Python):
N = int(input())
A = list(map(int,input().split())) + [0]
ans = 0
while sum(A) > 0:
    f = 0
    for k in range(N+1):
        if A[k] > 0:
            f = 1
            A[k] -= 1
        elif A[k] == 0:
            ans += f
            f = 0

print(ans)

Solution (Python):
N = int(input())
H = list(map(int, input().split()))
i = 0
ctr = 0
done = False

while sum(H) > 0:
    
    if H[i] > 0:
        done = True 
        H[i] -= 1
    else: 
        if done:
            ctr += 1
            done = False
    i += 1
    if i == N:
        i = 0
        if done:
            ctr += 1
            done = False

if done:
    ctr += 1
print(ctr)

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

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

def main():
    mod=10**9+7
    N=I()
    h=LI()
    ans=0
    now=0
    for i in range(N):
        nxt=h[i]
        ans+=max(nxt-now,0)
        now=nxt

    print(ans)
    

main()

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

Solution (Python):
n = int(input())
h = list(map(int,input().split()))
m = max(h)
ans = 0
for i in range(1,m+2):
    for j in range(n):
        if j == 0:
            if h[0] >= i:
                p = 0
            elif h[0] < i:
                p = 1
        if j == n-1:
            if p == 0:
                ans += 1
            if p == 1 and h[j] >=i:
                ans += 1
        else:
            if p == 0:
                if h[j] < i:
                    p = 1
                    ans += 1
            if p == 1:
                if h[j] >= i:
                    p = 0
        
print(ans)

Solution (Python):
def Grand_Garden(n, hl):

    ans = 0
    maxlen = max(hl)
    for i in range(maxlen):
        for w in range(n):
            if hl[w] == 0:
                continue
            elif w+1 == n:
                ans += 1
                hl[w] -= 1
                continue
            elif hl[w+1] == 0:
                ans += 1
            hl[w] -= 1

    return ans

def main():
    n = int(input())
    hl = list(map(int, input().split()))
    print(Grand_Garden(n, hl))

if __name__ == '__main__':
    main()

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

ans = 0
for i in range(N-1):
    while True:
        if H[i] <= 0:
            break

        base = H[i]
        for j in range(i+1, N):
            
            if H[j] <= 0:
                break

            
            if base <= H[j]:
                H[j] -= 1
            else:
                break
        H[i] -= 1
        ans += 1

ans += max(0, H[-1])
print(ans)

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

ans = 0
for i in range(N-1):
    if H[i] == 0:
        continue

    while True:
        base = H[i]
        for j in range(i+1, N):
            
            if H[j] <= 0:
                break

            
            if base <= H[j]:
                H[j] -= 1
            else:
                break
        H[i] -= 1
        ans += 1

        if H[i] <= 0:
            break

ans += max(0, H[-1])
print(ans)

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

ans = 0
while sum(h) != 0:
    for i in range(N):
        if h[i] > 0:
            st = i
            break
    en = N
    for i in range(st+1, N):
        if h[i] <= 0:
            en = i
            break
    for i in range(st, en):
        h[i] -= 1
    ans += 1

print(ans)

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

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

number_of_times = 0

def compare_add():
    added_or_not = False
    index = None
    for i in range(len(x)):
        if index == None:
            if x[i] != h[i]:
                x[i] += 1
                added_or_not = True
                index = i
        else:
            if x[i] != h[i]:
                if i != index + 1:
                    return True
                x[i] += 1
                index = i
    return added_or_not
    
while True:
    y = compare_add()
    if y == False:
        break
    number_of_times += 1

print(number_of_times)

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

count = min(H)
H = [h - count for h in H]
while any(0 < h for h in H):
    l = 0
    while H[l] == 0:
        l += 1
    r = l + 1
    while r < N and 0 < H[r]:
        r += 1

    count += 1
    H = [H[i] - (1 if l <= i < r else 0) for i in range(N)]
print(count)

Solution (Python):
n = int(input())
h_list = list(map(int, input().split()))
ans = 0
while True:
    start = 0
    if all([h == 0 for h in h_list]):
        break
    while h_list[start] == 0:
        start += 1
    end = start
    for i in range(start, n):
        if h_list[i] == 0:
            end = i - 1
            break
    else:
        end = n - 1
    mi = min(h_list[start:end + 1])
    for i in range(start, end + 1):
        h_list[i] -= mi
    ans += mi
print(ans)

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

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

Solution (Python):


import fractions

data = int(input())
nim = list(map(int, input().split()))
result = 0

for i in range(100):
	prev = 0
	for j,h in enumerate(nim):
		if prev == 0 and h != 0:
			result+=1
		prev = h
		if h != 0:
			nim[j]-=1
	if sum(nim) == 0:
		break

print(result)

Solution (Python):
a = [input().split() for m in range(2)]
z=a[1]
b=[int(s) for s in z]
c=int(a[0][0])
cnt=0
while True:
    for i in range(c):

        if b[i]!=0:

            for j in range(i,c):

                if b[j]==0:
                    break

                b[j]-=1
                j+=1

            cnt+=1
            break

    if sum(b)==0:
        break
    
print(cnt)

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

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

from collections import deque

d = deque()

d.append(h)
ans = 0

while d:
    p = d.pop()
    if p:
        p_min = min(p)

        p0 = []
        pp = []

        for i in range(len(p)):
            pp.append(p[i]-p_min)
            if p[i] - p_min == 0:
                p0.append(i)
        ans += p_min
        '''
        print(ans)
        print(pp)
        print(p0)
        '''
    
        for i in range(len(p0)-1):
            d.append(pp[p0[i]+1:p0[i+1]]) 
        d.append(pp[:p0[0]])
        d.append(pp[p0[len(p0)-1]+1:])

print(ans)

Solution (Python):
def main():
  N = int(input())
  H = [int(_) for _ in input().split()]
  a = [0] * N
  a[0] = H[0]
  for i in range(N-1):
    if H[i+1] > H[i]:
      a[i+1] += a[i] + H[i+1] - H[i]
    else:
      a[i+1] = a[i]
  print(a[N-1])
  return

if __name__ == '__main__':
  main()

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

ans = 0
ans += l[0]

for i in range(1,N):
    if l[i-1] >= l[i]:
        continue
    else:
        ans += l[i] - l[i-1]

print(ans)

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

def mizu(h):
  if h==[]:
    return 0
  else:
    t=min(h)
    f=[]
    for i in range(len(h)):
      f.append(h[i]-t)
    u=f.index(0)
    return mizu(f[:u])+mizu(f[u+1:])+t


print(mizu(h))

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

ans = 0
f = [0] * n
while True:
    flg = 0
    for i, ff in enumerate(f):

        if ff < h[i]:
            f[i] += 1
            flg = 1
            if i == n-1:
                ans += 1
        else:
            if flg == 1:
                ans += 1
                flg = 0

    if f == h:
        print(ans)
        break

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

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

def func(l):
    global ans
    if type(l) is int:
        ans += l
        return
    if l == []:
        return
    m = min(l)
    ans += m
    l = list(map(lambda x: x-m, l))
    i = l.index(0)
    if i >=  0:
        func(l[:i])
        func(l[i+1:])

func(h)
print(ans)

Solution (Python):
from collections import deque
N = int(input())

def split_by_zero(l):
    if 0 not in l:
        return [l]
    ret_l = []
    while 0 in l:
        idx = l.index(0)
        if l[:idx]:
            ret_l.append(l[:idx])
        l = l[idx+1:]
    if l:
        ret_l.append(l)
    return ret_l

def main():
    
    H = list(map(int, input().split()))
    H = split_by_zero(H)
    que = deque(H)

    cnt = 0
    while que:
        q = que.popleft()
        q = [i-1 for i in q]
        cnt += 1
        q = split_by_zero(q)
        if q:
            for qq in q:
                que.append(qq)
    
    print(cnt)


if __name__ == "__main__":
    main()

Solution (Python):



counter = 0

def mizu(h):
    global counter
    if len(h) == 0:
        return
    elif len(h) == 1:
        counter += h[0]
        return
    else:
        m = min(h)
        counter += m
        h = [i-m for i in h]
        while 0 in h:
            mizu(h[:h.index(0)])
            h = h[h.index(0)+1:]
        mizu(h)

if __name__ == "__main__":
    N = int(input())
    h = [int(i) for i in input().split()]
    mizu(h)
    print(counter)

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

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

while len(set(A)) != 1 or max(A) != 0:
    for i in range(n):
        if A[i] != 0:
            break
    B = A[i:]
    for j in range(i, n):
        if A[j] != 0:
            A[j] -= 1
        else:
            break
    t += 1

print(t)

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

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

Solution (Python):
import sys

n, *h = map(int, sys.stdin.read().split())

def main():
    cnt = 0
    for _ in range(max(h)):
        flag = 0
        for i in range(n):
            if not h[i]: flag = 0
            else:
                if not flag:
                    flag = 1
                    cnt += 1
                h[i] -= 1
    print(cnt)

if __name__ ==  '__main__':
    main()

Solution (Python):
N = int(input())
arr = list(map(int,input().split()))
N+=1
arr.append(0)
now = 0
min = 100
count = 0

while True:
  min = 100
  if arr[now] == 0:
    now += 1
    if now == N:
      break
  else:
    for i in range(now,N):
      if min > arr[i]:
        if arr[i] == 0:
          last = i
          for j in range(now,i):
            arr[j] = arr[j] - min
          count+=min
          break
        else:
          min = arr[i]
print(count)

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

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

Solution (Python):
from queue import Queue

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

que = Queue()
que.put(H)
ans = 0
while not que.empty():
    H = que.get()
    minh = min(H)
    H = [h - minh for h in H]
    ans += minh
    res = []
    for h in H:
        if h > 0:
            res.append(h)
        elif h == 0 and len(res) > 0:
            que.put(res)
            res = []
    if len(res) > 0:
        que.put(res)
print(ans)

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

def f_state(k):
  state1 = 1 if k>0 else 0
  return(state1)

for i in range(10000):

  state1 = f_state(ary2[0])
  ary2[0] = max([0,ary2[0]-1])
  if state1 ==1:
    ans +=1
  for i in range(1,N):
    if f_state(ary2[i]) == 1 and state1 ==0:
      ans +=1
    state1 = f_state(ary2[i])
    ary2[i] = max([0,ary2[i]-1])
  if sum(ary2)==0:
    break

print(ans)

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

ans = 0
ans += A[0]
for i in range(1,N):
  if A[i-1] >= A[i]:
    continue
  else:
    ans += A[i]-A[i-1]

print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    H=[int(_) for _ in input().split()]
    cunt_p=0
    while sum(H):
        in_p=False
        for i in range(n):
            if H[i]>0:
                H[i]-=1
                if in_p==False:
                    cunt_p+=1
                    in_p=True
            elif H[i]==0 and in_p:
                in_p=False
    print(cunt_p)
                



if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    H=[int(_) for _ in input().split()]
    cunt_p=0
    while sum(H):
        in_p=False
        for i in range(n):
            if H[i]>0:
                H[i]-=1
                if in_p==False:
                    cunt_p+=1
                    in_p=True
            elif H[i]==0 and in_p:
                in_p=False
    print(cunt_p)

if __name__=='__main__':
    main()

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

count = 0
while True:
    
    
    num_block = 0
    for i in range(len(h)):
        if h[i] > 0:
            if i == 0 or h[i - 1] == 0:
                num_block += 1
    
    if num_block == 0:
        break
    
    count += num_block

    
    
    h = list(map(lambda x: max(x - 1, 0), h))

print(count)

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

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

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

a = [0]*N
lst = [0]*(N+1)
cnt = 0

while h != a:
    for i in range(N):
        if h[i] > a[i]:
            h[i] -= 1
            lst[i] = 1
        else:
            h[i] -= 0
            lst[i] = 0
    for j in range(N):
        if lst[j]*lst[j+1] == 0 and lst[j] == 1:
            cnt += 1
        else:
            cnt += 0

print(cnt)

Solution (Python):
N = int(input())
H = [int(h) for h in input().split()]
az = [0]*N
ans = 0
while H != az:
    has_found = False
    for i in range(N):
        if H[i] == 0:
            has_found = False
            continue
        if not has_found:
            has_found = True
            ans += 1
        H[i] -= 1
print(ans)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
ans=0
while True:
    l,m=-1,-1
    for i in range(n):
        if h[i]!=0:
            l=i
            break
    if l==-1:
        break
    if l==n-1:
        ans+=h[l]
        break
    r=l
    for i in range(l,n):
        if h[i]==0:
            break
        else:
            r=i
            if m==-1:
                m=h[i]
            else:
                m=min(m,h[i])

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

Solution (Python):
N = int(input())
H = list(map(int, input().split()))
cnt = 0
while max(H) > 0:
    m = max(H)
    mi = H.index(m)

    
    for i in range(mi+1, N):
        m = min(m, H[i])
        H[i] = max(0, H[i]-m)

    m = H[mi]
    
    for i in range(mi-1, -1, -1):
        m = min(m, H[i])
        H[i] = max(0, H[i]-m)

    cnt += H[mi]
    H[mi] = 0
    
print(cnt)

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

ans = 0
for m in range(1, M + 1):
    i = 0
    while i < N:
        if H[i] >= m:
            ans += 1
            while H[i] >= m:
                i += 1
                if i >= N:
                    break
        else:
            i += 1

print(ans)

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

mins = min(x)
cnt = mins
x -= mins

while sum(x) != 0:
    mask = [1 if xi > 0 else 0 for xi in x]
    x -= mask
    
    for i in range(1, n):
        if mask[i] == 1 and mask[i-1] == 1:
            mask[i-1] = 0

    cnt += sum(mask)
    
print(cnt)

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

ans=0
active=0
for x in h:
    if active>=x:
        active=x 
    else:
        ans+=x-active 
        active=x 
print(ans)

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

Solution (Python):
import sys
L = int(sys.stdin.readline().rstrip())
 
h = list(map(int, sys.stdin.readline().rstrip().split()))
 
if L == 0:
  ans = 0
elif L == 1:
  ans = h[0]
elif L == 2:
  ans = max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]
 
  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
  ans = p - v
print(ans)

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

while True:
    flag=0
    m=200 
    for h in H:
        if h==0:
            continue
        else:
            m=min(h,m)
    if m==200:
        break
    else:
        for i in range(n):
            if H[i]==0:
                flag=0
                continue
            else:
                if flag==0:
                    ans+=m
                flag=1
                H[i]-=m
print(ans)

Solution (Python):
import numpy as np

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

ans = 0

while max(H):
    max_index = np.where(H == max(H))[0]
    ren_position = max_index - np.arange(len(max_index))
    ans += len(set(ren_position))

    H = np.where(H == max(H), max(H) - 1, H)

print(ans)

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

    return N,h

def mizuyari(N,H):
    cnt =0
    flg = False
    h = H

    while flg == False:
    
        start = 0
        for n in range(N):
            if h[n] != 0:
                start = n
                break
            elif n == N-1 and h[n] == 0:
                flg = True
                break
    
        for n in range(start,N):
            if h[n] == 0:
                break
            else :
                h[n] -= 1
        cnt += 1

    print(cnt-1)







    



N,h =nyu()
mizuyari(N,h)

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

cnt = 0
while True:
    if max(h) == 0:
        break
    idx = 0
    while idx < n:
        if h[idx] == 0:
            idx += 1
        else:
            cnt += 1
            while idx < n and h[idx] != 0:
                h[idx] -= 1
                idx += 1

print(cnt)

Solution (Python):
def resolve():
    n = int(input())
    h = list(map(int, input().split()))
    h.append(0)
    num = 0

    while sum(h) > 0:
        for i in range(n+1):
            if i > 0:
                if h[i-1] != 0 and h[i] == 0:
                    num += 1
        for i in range(n+1):
            if h[i] != 0:
                h[i] -= 1
    print(num)
resolve()

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

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

ans = 0

for i in range(len(h)): 
    while h[i] != 0:
        j = i
        while j < len(h) and h[j] != 0:
            j += 1
        for k in range(i, j):
            h[k] -= 1
        ans += 1
print(ans)

Solution (Python):
n, *h = map(int, open(0).read().split())

cnt = min(h)
vector = [cnt] * n
flg = False

for i in range(100):
    pre = -2
    for j in range(n):
        
        if vector[j] < h[j]:
            vector[j] += 1
            pre = j
            if pre == n - 1:
                cnt += 1
        else:
            if pre == j - 1:
                cnt += 1

print(cnt)

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

Solution (Python):
n = int(input())
h = [ int(x) for x in input().split() ]
 
cnt = 0
while True:
    if max(h) == 0:
        break
    idx = 0
    while idx < n:
        if h[idx] == 0:
            idx += 1
        else:
            cnt += 1
            while idx < n and h[idx] != 0:
                h[idx] -= 1
                idx += 1
print(cnt)

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

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

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

Solution (Python):
import sys
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))

def main():
    n = II()
    hh = MII()
    tmp = 0
    cnt = 0
    for h in hh:
        if h < tmp:
            cnt += tmp-h
        tmp = h
    cnt += tmp
    print(cnt)

if __name__ == '__main__':
    main()

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

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

Solution (Python):
n = int(input())
ns = map(int, input().split())
result = 0
temp = 0
for i in ns:
    if i < temp:
        result += temp-i
    temp = i
result += temp
print(result)

Solution (Python):
N=int(input())
H=list(map(int,input().split()))
c=0
while H!=[0]*N:
  for i in range(N):
    if H[i]!=0:
      l=i
      break
  for j in range(l,N):
    if H[j]!=0:
      H[j]-=1
    else:
      break
  c+=1
print(c)