Problem:
 Score : 300 points    Problem Statement In a flower bed, there are N flowers, numbered 1,2,......,N . Initially, the heights of all flowers are 0 .
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k  (1 \leq  k \leq N) , by repeating the following "watering" operation:   Specify integers l and r . Increase the height of Flower x by 1 for all x such that l \leq x \leq r .   Find the minimum number of watering operations required to satisfy the condition.      Constraints  1 \leq N  \leq 100  0 \leq h_i \leq 100  All values in input are integers.         Input Input is given from Standard Input in the following format:  N  h_1  h_2  h_3  ......  h_N       Output Print the minimum number of watering operations required to satisfy the condition.        Sample Input 1 4
1 2 2 1      Sample Output 1 2  The minimum number of watering operations required is 2 .
One way to achieve it is:   Perform the operation with (l,r)=(1,3) .  Perform the operation with (l,r)=(2,4) .        Sample Input 2 5
3 1 2 3 1      Sample Output 2 5       Sample Input 3 8
4 23 75 0 23 96 50 100      Sample Output 3 221

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

    cnt = 0
    p = 0
    for h in H:
        cnt += max(0, h - p)
        p = h
    print(cnt)


if __name__ == '__main__':
    main()

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

Solution (Python):
import numpy as np
import copy

def erase_same(h):
    a_max = np.argmax(h)
    while a_max > 0 and h[a_max] == h[a_max-1]:
        h = np.delete(h, a_max-1)
        a_max = np.argmax(h)
    
    while a_max < len(h) - 1 and h[a_max] == h[a_max+1]:
        h = np.delete(h, a_max+1)
        a_max = np.argmax(h)
    
    return h, a_max

n = int(input())
H = [int(i) for i in input().split()]
h = np.array(H)
cnt = 0

h_min = min(h)
h -= h_min
cnt += h_min

h, a_max = erase_same(h)

while sum(h) > 0:
    if a_max == 0:
        tonari = h[a_max+1]
    elif a_max == len(h) - 1:
        tonari = h[a_max-1]
    else:
        tonari = max(h[a_max-1], h[a_max+1])
    cnt += h[a_max] - tonari
    h[a_max] = copy.deepcopy(tonari)
    h, a_max = erase_same(h)


print(cnt)

Solution (Python):
n = int(input())
*h, = map(int, input().split())
mx = max(h)
a = [[0]*mx for _ in [0]*n]
for i in range(n):
    for j in range(h[i]):
        a[i][j] = 1
ans = 0
for j in range(mx):
    f = 0
    for i in range(n):
        if not f and a[i][j]:
            f = 1
            ans += 1
        elif f and not a[i][j]:
            f = 0
print(ans)

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

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

  while(i < N):
    if h[i] == 0:
      i += 1
      continue
    else:
      ans += 1
      while((h[i] != 0) & (i < N)):
        h[i] -= 1
        i += 1


print(ans)

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

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

ans = sum(max(0,H[i] - H[i - 1]) for i in range(1, len(H)))
print(ans)

Solution (Python):
import math
import string
import collections
from collections import Counter
from collections import deque


def readints():
    return list(map(int, input().split()))


def nCr(n, r):
    return math.factorial(n)//(math.factorial(n-r)*math.factorial(r))


def has_duplicates2(seq):
    seen = []
    for item in seq:
        if not(item in seen):
            seen.append(item)
    return len(seq) != len(seen)


def divisor(n):
    divisor = []
    for i in range(1, n+1):
        if n % i == 0:
            divisor.append(i)
    return divisor



dx = [-1, -1, -1, 0, 0, 1, 1, 1]
dy = [-1, 0, 1, -1, 1, -1, 0, 1]
n = int(input())
h = readints()



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


ans = 0
for i in range(n):
    for j in range(h[i]):
        cnt = func(h[i:])
        ans += 1
        for k in range(0, cnt):
            h[i+k] = h[i+k]-1
print(ans)

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
count = 0
while True:
    f1 = False
    f2 = False
    for h in H:
        if not f1 and h > 0:
            f1 = True
            f2 = True
            count += 1
        elif f1 and h <= 0:
            f1 = False
    if not f2:
        break
    f1 = False
    f2 = False
    H = list(map(lambda x: x-1,H))
print(count)

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

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

    return ans
    

print(solve())

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

ans = h[0]

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

print(ans)

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

ans = 0

while True:
    if not sum(h):
        break
    for l in range(N):
        if h[l]:
            break
    for r in range(l,N):
        if not h[r]:
            r -= 1
            break
    for i in range(l,r+1):
        h[i] -= 1
    ans += 1

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

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

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

ans = 00
i = 1
for i in range(N - 1):
    if H[i] < H[i + 1]:
        continue
    else:
        ans += H[i] - H[i + 1]

ans += H[N - 1]
print (ans)

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

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

lenh = len(h)

ans = 0

while max(h)>0:
    x = max(h)
    q = 0
    for i in range(lenh):
        if h[i] == x:
            if q == 0:
                ans += 1
                q = 1
            h[i] -= 1
        else:
            q = 0
        
print(ans)

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

point = 0

now = 0

for i in h:
    if now <= i:
        now,point = i,point+(i-now)
    else:
        now = i

print(point)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
from itertools import groupby
ans=0
for i in range(101):
  c=[0]*n
  for j in range(n):
    if h[j]>0:c[j]=1
  gr=groupby(c)
  for k,v in gr:
    if k>0:ans+=1
  for j in range(n):
    if h[j]>0:h[j]-=1
print(ans)

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

Hs = list(map(int, input().split()))
cnt = 0
while 1:
    mode = 0
    for i in range(N):
        if Hs[i]>=1:
            if not mode:
                cnt += 1
            mode = 1
            Hs[i] -= 1
        else:
            mode = 0
    if sum(Hs)==0:
        break
print(cnt)

Solution (Python):
N = int(input())
H = list(map(int,input().split()))
cnt = 0
while True:
    flag = 0
    for i in range(N):
        if H[i]>0:
            cur = i
            flag = 1
            break
    if flag==0:break
    cmin = H[cur]
    flag = 1
    for i in range(cur,N):
        if H[i]>0 and flag==1:
            cmin = min(cmin,H[i])
        elif H[i]>0 and flag==0:
            cur = i
            cmin = H[i]
            flag = 1
        else:
            cnt += cmin
            for k in range(cur,i):
                H[k] -= cmin
            cur = i
            cmin = 0
            flag = 0
    cnt += cmin
    for k in range(cur,N):
        H[k] -= cmin
print(cnt)

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

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

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

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

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)
import math

n=int(input().rstrip())
h=[int(i) for i in input().split()]
ans=0
while 1:
    for i in range(n):
        if h[i]:
            j=i
            break
    else:
        print(ans)
        exit()
    while h[j]!=0:
        h[j]-=1
        if j<n-1:
            j+=1
        else: break
    ans+=1
print(ans)

Solution (Python):
import sys
from collections import Counter
from collections import deque
import math
import fractions
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
h=lmp()
h.append(0)
m=max(h)
ans=0
for i in range(m,0,-1):
    for k in range(n):
        if h[k]==i and h[k+1]!=i:
            ans+=1
        if h[k]==i:
            h[k]-=1
print(ans)

Solution (Python):
n = int(input())
h = list(map(int, input().split()))
def solve(n):
  
  if len(n) == 1:
    return n[0]
  if n == []:
    return 0
  for i in range(len(n)):
    if n[i] == 0:
      return solve(n[:i]) + solve(n[i+1:])
  tempmin = min(n)
  nextn = [n[i]- tempmin for i in range(len(n))]
  return tempmin+solve(nextn)

print(solve(h))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    H = list(map(int, input().split()))

    res = 0
    i = 0
    while sum(H) != 0:
        if H[i] != 0:
            while i < n and H[i] != 0:
                H[i] -= 1
                i += 1
            res += 1
            i = 0
        else:
            i += 1

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n=int(readline())
    h=list(map(int,readline().split()))
    ans=0
    while True:
        flag=True
        flag2=True
        for i in range(n):
            if h[i]>0:
                h[i]-=1
                flag=False
                flag2=False
                if i==n-1:
                    ans+=1
            else:
                if flag2==False:
                    flag2=True
                    ans+=1
        if flag: break

    print(ans)

if __name__=="__main__":
    main()

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

H=np.array(H)
ans=0
while H.max()>0:
    prezero=True
    count=0
    for i in range(N):
        if prezero:
            if H[i]>0:
                count+=1
                prezero=False
        else:
            if H[i]<=0:
                prezero=True
    ans+=count
    H-=1
print(ans)

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

ans = 0
active = 0

for i in range(n):
    if active < h[i]:
        ans += h[i] - active
        active += h[i] - active
    elif h[i] < active:
        active -= active - h[i]




print(ans)

Solution (Python):
def main():
    n = int(input())
    h = list(map(int, input().split()))
    ans = 0
    i = 0
    while i < n:
        if h[i] == 0:
            i += 1
            continue
        x = i+1
        m = h[i]
        while x < n and h[x] != 0:
            if h[x] < m:
                m = h[x]
            x += 1
        for j in range(i, x):
            h[j] -= m
        ans += m
        if h[i] == 0:
            i += 1
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=[0]+list(map(int,input().split()))
    ans=0
    for i in range(len(l)-1):
        x=l[i+1]-l[i]
        if x>0:
            ans+=x
    print(ans)
resolve()

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

N = int(readline())
H = [0] + list(map(int,readline().split()))

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

print(ans)

Solution (Python):
N = int(input())
hs = list(map(int, input().split()))
ans = 0
while max(hs) != 0:
    i = 0
    while i < N:
        if hs[i] == 0:
            i += 1
        else:
            ans += 1
            while i < N and hs[i] > 0:
                hs[i] -= 1
                i += 1

print(ans)

Solution (Python):
n = int(input())
ls = list(map(int,input().split()))
def sub(p,q):
    return p-q
i = 0
p = 1
new=[0 for _ in range(n)]
s = [1 for _ in range(n)]
while ls[ls.index(max(ls))]>0:
    for j in range(len(ls)):
        if j == n-1:
            if ls[j] > 0:
                new[j] = p
            else:
                pass
        else:
            if (ls[j] <= 0) and (ls[j+1] <= 0):
                pass
            elif (ls[j] <= 0) and (ls[j+1] > 0):
                p += 1
            elif (ls[j] > 0) and (ls[j+1] > 0):
                new[j] = p
            else:
                new[j] = p
    if (sum(new) == n) and (len(list(set(new))) == 1):
        i += 1
    else:
        i += len(list(set(new))) -1
    ls = list(map(sub,ls,s))
    new = [0]*n
    p = 1
print(i)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
l=[0]
c='plus'
for i in range(n-1):
    if c=='minus' and h[i]<=h[i+1]:
        l.append(i)
    if h[i]<=h[i+1]:c='plus'
    else:c='minus'

w=0
m=0
for i in range(len(l)-1):
    w+=max(h[l[i]:l[i+1]+1])-m
    m=h[l[i+1]]
w+=max(h[l[-1]:n])-m
print(w)

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

OK = [h == 0 for h in H]
T = [0] * N

ans = 0
while not all(OK):
  flg = False
  for i in range(N):
    if not OK[i]:
      T[i] += 1
      if T[i] == H[i]:
        OK[i] = True
      flg = True
    else:
      if flg:
        ans += 1
      flg = False
  
  if flg:
    ans += 1
    
print(ans)

Solution (Python):

N = int(input())
*H, = map(int, input().split())
 
T = [0]*N
ans = 0
i = 0
while i<N:
    
    if T[i]<H[i]:
        start = i
        high  = T[i]+1
        ans = ans + 1
        i = 0
        for j in range(start, N, 1):
            if T[j]<H[j]:
                T[j] = T[j] + 1
            else:
                break
    else:
        i = i + 1

print(ans)

Solution (Python):
import itertools
 
n = int(input())
H = list(map(int, input().split()))
 
count = 0
h_max = max(H)
for i in range(1, h_max + 1): 
    watering = [(1 if h >= i else 0) for h in H] 
 
    itrs = itertools.groupby(watering)
    watering_group = [k for k, v in itrs if sum(list(v)) > 0] 
    count += sum(watering_group)

print(count)

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

Solution (Python):
import itertools

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

count = 0
h_max = max(H)
for i in range(1, h_max + 1):
    watering = [(1 if h >= i else 0) for h in H]

    itrs = itertools.groupby(watering)
    watering_group = [k for k, v in itrs if sum(list(v)) > 0]
    count += sum(watering_group)

print(count)

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

def water(a):
    if min(a)==max(a):
        return min(a)
    elif min(a)>0:
        m=min(a)
        for i in range(len(a)):
            a[i]-=m
        return water(a)+m
    else:
        s=[]
        l=-1
        for i in range(len(a)):
            if a[i]!=0:
                if l==-1:
                    l=i
                if i==len(a)-1:
                    r=i+1
                    s.append((l,r))
    
            else:
                if l>-1:
                    r=i
                    s.append((l,r))
                    l=-1
    
        res=0
        for k in s:
            res+=water(a[k[0]:k[1]])
        return res

print(water(h))

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

T = [0]*N
ans = 0
i = 0
while i<N:
    
    if T[i]<H[i]:
        start = i
        high  = T[i]+1
        ans = ans + 1
        i = 0
        for j in range(start, N, 1):
            if T[j]<H[j] and (T[j]+1==high):
                T[j] = T[j] + 1
            else:
                break
    else:
        i = i + 1

print(ans)

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

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

if max==h[n-1]:
    ans+=max
print(ans)

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

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

Solution (Python):


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

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

Solution (Python):

N = int(input())
h = list(map(int, input().split()))
cnt = 0 
while True:
  pre = False
  for i in range(N):
    if h[i] > 0:
      if pre:
        h[i] -= 1
      else:
        h[i] -= 1
        pre = True
        cnt += 1
    else:
      pre = False
  if sum(h) == 0:
    break
print(cnt)

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





i = 0
count = 0

while(i < n):
    if mokuhyo[i] == 0:
        i += 1
        continue
    r = 999999
    for j in range(i+1, n):
        if mokuhyo[j] == 0:
            r = j
            break

    if r == 999999:
        r = n

    gensyo = min(mokuhyo[i:r])
    count += gensyo
    for k in range(i, r):
        mokuhyo[k] -= gensyo


print(count)

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

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
total = 0
for i in range(max(li)):
    a = 0
    b = 0
    for j in range(n):
        if li[j] != 0 and a == 0:
            b += 1
            a = 1
        if li[j] == 0:
            a = 0
        else:
            a = 1
    for j in range(n):
        li[j] -= 1
        if li[j] < 0:
            li[j] = 0
    total += b
print(total)

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

def decrease(lis):
    global count
    length = len(lis)
    if length == 2:
        count += max(lis)
    elif length == 1:
        count += lis[0]
    else:
        minnum = min(lis)
        minidx = lis.index(minnum)
        count += minnum
        lis = list(map(lambda x: x-minnum, lis))
        if minidx >= 1:
            decrease(lis[:minidx])
        if minidx < length - 1:
            decrease(lis[minidx+1:])

count = 0
decrease(H)
print(count)

Solution (Python):
n=int(input())
h=list(map(int,input().split()))
ans=0
pre=0
for hi in h:
  if hi-pre>0:
    ans+=hi-pre
  pre=hi
print(ans)

Solution (Python):
N = int(input())
h = list(map(int,input().split()))
ans = 0
while True:
  if max(h) == 0:
    break
  i = 0
  while i < N:
    if h[i] == 0:
      i+=1
      continue
    else:
      ans += 1
      while (i < N and h[i] > 0):
        h[i] -= 1
        i += 1
print(ans)

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

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

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n = n//a
    count += 1
  if a**count == n:
    return count
  else:
    return count+1





N = I()
H = IL()

prev = 0
ans = 0
for h in H:
  if h > prev:
    ans += h-prev
  prev = h
P(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    ans=l[0]
    
    
    for i in range(len(l)-1):
        ans+=max(l[i+1]-l[i],0)
    print(ans)
resolve()

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    l=[0]+l
    ans=0
    for i in range(len(l)-1):
        ans+=max(l[i+1]-l[i],0)
    print(ans)
resolve()

Solution (Python):

import sys
input = sys.stdin.readline









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

ans = 0
x = [H]

while len(x) > 0:
    A = x.pop()
    minA = min(A)
    k = A.index(minA)
    for i in range(len(A)):
        A[i] -= minA
    ans += minA
    if A[0:k] != []:
        x.append(A[0:k])
    if A[k+1:] != []:
        x.append(A[k+1:])

print(ans)

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

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

print(ans)

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

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

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

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

print(ans)

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

ans = 0
tmp = 0
max_h = max(H)
for i in range(max_h,0,-1):
    H_c = H.copy()
    if H[0]==i:
        ans+=1
        H_c[0]-=1
    for j in range(1,n):
        if H[j]==i:
            if H[j-1]!=H[j]:
                ans+=1
                H_c[j]-=1
            else:
                H_c[j]-=1
    H = H_c
print(ans)

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

Solution (Python):
n = int(input())
hl = list(map(int, input().split()))
hl.insert(0, 1)

cnt = 0
l = 1
r = n
while True:
    if sum(hl) == 1:
        break
    for i in range(1, n + 1):
        if hl[i] >= 1:
            l = i
            break
    for j in range(l, n + 1):
        if hl[j] == 0:
            r = j -1
            break
        if not 0 in hl[l:]:
            r = n
    if l == r:
        hl[l] -= 1
    else:
        for k in range(l, r+1):
            hl[k] -= 1
    cnt += 1
print(cnt)

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

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

Solution (Python):
import numpy as np

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

ans = 0
while h.sum() > 0:
    
    minus = h[np.where(h > 0)].min()

    
    target = np.where(h >= minus)[0]

    
    diff = np.diff(target)
    cnt = np.where(diff > 1)[0].shape[0] + 1
    
    
    ans += minus * cnt

    
    h[target] -= minus

print(ans)

Solution (Python):
N = int(input())
hlist = list(map(int, input().split()))
def count_island(height):
    res = 0
    sink = True
    for h in hlist:
        if sink and h > height:
            res += 1
            sink = False
        if h <= height:
            sink = True
    return res

ans = 0
for height in range(max(hlist)):
    ans += count_island(height)
print(ans)

Solution (Python):
from collections import deque


def main():
    N = int(input())
    s = deque()
    H = [int(h) for h in input().split()]

    s = deque()
    h0 = 0
    count = 0
    for h in H:
        if s and s[-1] > h:
            count += (s[-1] - h)
            while s and s[-1] > h:
                s.pop()
        s.append(h)
    count += s[-1]
    print(count)


main()

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

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

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

if n == 1:
    print(s[0])
    exit()

b = 0
res = 0
for i in range(n - 1):
    if s[i] <= s[i + 1]:
        continue
    else:
       res += s[i] - b
       b = s[i + 1]

if s[n - 1] >= s[n - 2]:
    res += s[n - 1] - b
print(res)

Solution (Python):

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

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from heapq import heappush, heappop
def input(): return sys.stdin.readline().strip()


def main():
    N = int(input())
    H = list(map(int, input().split())) + [-1]

    ans = 0
    for n in range(1, max(H) + 1):
        conti = 0
        exist = False
        for h in H:
            if h >= n and exist:
                continue
            elif h >= n:
                exist = True
            elif h < n and exist:
                conti += 1
                exist = False
        ans += conti
    print(ans)



if __name__ == "__main__":
    main()

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

ans = 0
while True:
    if sum(h) <= 0:
        print(ans)
        exit()
    M = h.index(max(h))
    start = M
    end = M+1
    for i in range(M,-1,-1):
        if h[i] > 0:
            start = i
        else:
            break
    if end != n:
        for i in range(M,n):
            if h[i] > 0:
                end = i+1
            else:
                break
    for i in range(start,end):
        h[i] -= 1
    ans += 1

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

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

N = getN()
H = getList()
now = H[0]
ans = 0
for i in range(1, N):
    if now <= H[i]:
        now = H[i]
    else:
        ans += H[i - 1] - H[i]
        now = H[i]
ans += H[-1]
print(ans)

Solution (Python):
n = int(input())
seq =[list(map(int,input().split()))]
ans = 0
while seq:
    L = seq.pop()
    ans += min(L)
    S = [n-min(L) for n in L]
    K = []
    for i in S:
        if i!= 0:
            K.append(i)
        else:
            if K:
                seq.append(K)
                K = []
    else:
        if K:
            seq.append(K)
else:
    print(ans)

Solution (Python):
n = int(input())
H = list(map(int,input().split()))
seq = []
K = []
ans = 0
for i in H:
    if i != 0:
        K.append(i)
    else:
        if K:
            seq.append(K)
            K = []
else:
    if K:
        seq.append(K)
while seq:
    L = seq.pop()
    ans += min(L)
    S = [n-min(L) for n in L]
    K = []
    for i in S:
        if i!= 0:
            K.append(i)
        else:
            if K:
                seq.append(K)
                K = []
    else:
        if K:
            seq.append(K)
else:
    print(ans)

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

ans = 0
tmp = h = H[0]

for h in H[1:]:
    if h >= tmp:
        tmp = h
    else:
        ans += tmp - h
        tmp = h
else:
    ans += h

print(ans)

Solution (Python):
import itertools
import math
import fractions
import functools
import copy
n = int(input())
h  = list(map(int, input().split()))

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