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



Sample input:
4
1 2 2 1


Sample output:
2

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))
n = ii()
H = iil()

now = 0
ans = 0
for item in H:
    if item > now:
        ans += item-now
    now = item

print(ans)

Submission status: Accepted

Solution code:
from scipy import array, ndimage
input()
H = array(input().split(), dtype='i8')
print(sum(ndimage.label(H > t)[1] for t in range(100)))

Submission status: Accepted

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

count = 0
line =[]
line2 =[False] * N
max_h =max(h)
for i in range(1,max_h+1)[::-1]:
    line = [x == i for x in h]
    line2 = line = [x|y for x,y in zip(line,line2)]
    flag = False
    for j in range(N):
        if flag == False:
            if line[j] == True:
                count += 1
                flag = True
            else:
                continue
        else:
            if line[j] == False:
                flag = False

print(count)

Submission status: Accepted

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

Submission status: Accepted

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


ans = 0

def solve(l):
    global ans
    if len(l) == 1:
        ans += l[0]
    elif len(l) == 0:
        pass
    else:
        minv = min(l)
        ans += minv

        l = [x - minv for x in l]

        zero = l.index(0)

        solve(l[:zero])
        solve(l[zero+1:])

solve(l)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = min(h)
h_ = [n - ans for n in h] + [0]
while max(h_) > 0:
    j = -1
    for i, n in enumerate(h_):
        if n == 0:
            if (j + 1) < i:
                m = min(h_[j+1:i])
                ans += m
                for k in range(j+1, i):
                    h_[k] = h_[k] - m
            j = i

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
count=0
for i in range(max(h)):
  keep=0
  for j in range(0,n):
    if keep==0:
      if h[j]!=0:
        keep=h[j]
        count+=1
        h[j]-=1
    else:
      keep=h[j]
      if h[j]>=1:
        h[j]-=1
    
print(count)

Submission status: Accepted

Solution code:
n = int(input())
H = list(map(int,input().split()))
cnt = 0
for i in range(1,101):
    f = False
    for h in H:
        if f ==False and h>=i:
            cnt+=1
            f = True
        if h<i:
            f =False
print(cnt)

Submission status: Accepted

Solution code:
import itertools
import math
n =int(input())
h = list(map(int, input().split()))
ans = 0
act = 0
for i in range(n):
    if act >= h[i]:
        act = h[i]
    else:
        ans += h[i]-act
        act = h[i]

print(ans)

Submission status: Accepted

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

ans = 0
for i in range(1,101):
	res = 0
	flag = False
	for j in range(n):
		if h[j] >= i:
			flag = True
		if h[j] < i and flag:
			flag = False
			res += 1
	if flag:
		res += 1
	ans += res

print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline
n = int(input())

A = list(map(int,input().split()))
cnt = 0
ans = 0
for i in range(n):
    if cnt ==0 and A[i] == 0:
        continue
    elif cnt ==0 and A[i] != 0:
        cnt = A[i]
        
        ans +=cnt
    elif cnt !=0 and cnt <= A[i]:
        ans += (A[i] - cnt)
        cnt = A[i]
        

        
    elif cnt !=0 and  A[i] < cnt:
        cnt = A[i] 
        
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
H=list(map(int,input().split()))
H.append(0)

res=0

while sum(H)!=0:
	for i in range(n):
		if H[i]==0:
			continue
		else:
			if H[i+1]==0:
				H[i]-=1
				res+=1
			else:
				H[i]-=1

print(res)

Submission status: Accepted

Solution code:
count = 0
def mizu(h):
    global count

    if len(h) is 0:
        return

    if len(h) is 1:
        count += h[0]
        return

    m = min(h)
    count += 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 = list(map(int, input().split()))
    mizu(h)
    print(count)

Submission status: Accepted

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

cnt = 0
while max(set(h)) > 0:
    flg = False
    for i in h:
        if i <= 0:
            flg = False
            continue
        else:
            if not flg:
                cnt += 1
            flg = True
    h = list(map(lambda x: x-1, h))

print(cnt)

Submission status: Accepted

Solution code:
def f(ls):
    if len(ls) == 0:
        return 0
    cnt = 0
    if not 0 in ls:
        min_ = min(ls)
        ls = [l - min_ for l in ls]
        cnt += min_
    divs = []
    s = 0
    for i, l in enumerate(ls):
        if l == 0:
            if i == 0:
                s = 1
            else:
                divs.append(ls[s:i])
                s = i + 1
    if l != 0:
        if s == i:
            divs.append([ls[s]])
        else:
            divs.append(ls[s:i+1])
    cnt_ = sum([f(d) for d in divs])
    return cnt + cnt_

n = int(input())
ls_ = [int(i) for i in input().split()]
print(f(ls_))

Submission status: Accepted

Solution code:
n=int(input())
h = [int(i) for i in input().split()]
ans=0
l=0
while True:
	r=l+1
	if(h[l]==0):
		l+=1
		if(l>=n):
			break
		continue
	while(r<n):
		if(h[r]!=0):
			r+=1
		else:
			break
	for i in range(l,r):
		h[i]-=1
	ans+=1
print(ans)

Submission status: Accepted

Solution code:
import sys 
sys.setrecursionlimit(1000)
n=int(input())
H=list(map(int,input().split()))
def bunkai(H):
    B=[]
    b=0
    f=0
    for i in range(len(H)):
        if H[i]==0:
            if i!=0:
                B.append(H[b:i-1+1])
                b=i+1
            else:
                b=i+1
        elif i==len(H)-1:
            B.append(H[b:])
    return B
def min(lis):
    min=101
    if len(lis)==0:
        return 0
    for i in range(len(lis)):
        if lis[i]<min:
            min=lis[i]
    return min
def mizu(lis,n):
    for i in range(len(lis)):
        lis[i]+=-n
    return lis
def sso(lis,sum):
    #print(sum)
    B=bunkai(lis)
    #print(B)
    if len(B)==0:
        #print("return: "+str(sum))
        return sum
    for l in B:
        #print(l)
        x=min(l)
        #print("+"+str(x))
        sum+=x
        li=mizu(l,x)
        #print("li"+str(li))
        sum=sso(li,sum)
    #print("return end:"+str(sum))
    return sum

#print(min([]))
sum=0
x=sso(H,sum)

    
print(x)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
try:
    from typing import List
except ImportError:
    pass


def solve(N: int, h: "List[int]"):
    ans = 0
    for k in range(max(h)):
        r = [i for i in range(N) if h[i] > k]
        a = 0
        j = 0
        while j < len(r):
            a += 1
            while j + 1 < len(r) and r[j + 1] == r[j] + 1:
                j += 1
            j += 1
        ans += a
    print(ans)


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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
input()
ans, tmp = 0, 0
for h in map(int, input().split()):
    ans += max(0, h - tmp)
    tmp = h
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
board = [[0] * (N + 1) for _ in range(max(h))]
for i in range(N):
    for j in range(h[i]):
        board[j][i + 1] = 1
print(sum(row[i] == 0 and row[i + 1] == 1 for row in board for i in range(N)))

Submission status: Accepted

Solution code:
N=int(input())
List = list(map(int, input().split()))

ans=0

L=0
while 1==1:
    R=L

    if sum(List)!=0 and List[L]==0:
        L=0
        while List[L]==0:
            L+=1

    if sum(List)==0:
        break
    elif R==len(List)-1:
        List[R]=List[R]-1
        ans+=1
    elif List[L]>List[L+1]:
        List[L]=List[L]-1
        ans+=1
    else:
        checkMAX=1
        while 1==1:
            R+=1
            if R==len(List)-1:
                for i in range(L,R+1):
                    List[i]=List[i]-1
                ans+=1
                break
            elif checkMAX>List[R+1]:
                for i in range(L,R+1):
                    List[i]=List[i]-1
                ans+=1
                break
            else:
                checkMAX=List[R+1]
                pass
    if sum(List)!=0 and List[L]==0:
        L=0
        while List[L]==0:
            L+=1
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
List = list(map(int, input().split()))

ans=0

L=0
while 1==1:
    R=L

    if sum(List)!=0 and List[L]==0:
        L=0
        while List[L]==0:
            L+=1


    #終了判断
    if sum(List)==0:
        break
    elif R==len(List)-1:
        #リストの端の時
        List[R]=List[R]-1
        ans+=1
        #print("処理1")
    elif List[L]>List[L+1]:
        List[L]=List[L]-1
        #回数
        ans+=1
        #print("処理2")
    else:
        checkMAX=1
        while 1==1:
            #print("マックスの表示")
            #print(checkMAX)
            
            R+=1
            if R==len(List)-1:
                #範囲が決まったとき
                for i in range(L,R+1):#R+1追加あああああああ
                    List[i]=List[i]-1
                #回数
                ans+=1
                #print("処理3")
                break
            elif checkMAX>List[R+1]:
                #print("次の数")
                #print(List[R+1])
                #範囲が決まったとき
                for i in range(L,R+1):#R+1追加あああああああ
                    List[i]=List[i]-1
                #回数
                ans+=1
                #print("処理4")
                break
            else:
                checkMAX=List[R+1]
                pass
            
    #デバッグ
    #print("Lを表示")
    #print(L)
    #print(List)
            
    #print(ans)
    if sum(List)!=0 and List[L]==0:
        L=0
        while List[L]==0:
            L+=1
    #L+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
#n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
# abc = [int(input()) for i in range(5)]

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

up = True
ans = 0
for i in range(n-1):
    if a[i] <= a[i+1]:
        if up == False:
            ans -= a[i]
        up = True
    else:
        if up == True:
            ans += a[i]
        up = False

if a[-2] <= a[-1]:
    ans += a[-1]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int,input().split(" ")))
H_orig = H[:]
heights = [0 for i in range(N)]

cnt = 0

while True:
    #print(heights,H)
    if min(H) > min(heights):
        for i in range(N):
            heights[i] += 1
            H[i] -= 1
        cnt += 1
    else:
        flag = False
        for i in range(N):
            #H[i]
            if H[i] > 0:
                heights[i] += 1
                H[i] -= 1
                flag = True
            else:# H[i] == 0:
                if flag:
                    cnt += 1
                    flag = False
                    
            if i == N-1 and flag:
                cnt += 1
        
    if H_orig == heights:
        break
    
print(cnt)

Submission status: Accepted

Solution code:
def calc(list1,list2):
    for l in list2:
        list1[l] += 1
    return list1

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

for i in range(min(h)):
    f = [x+1 for x in f]
    ans += 1

while sum(f) != sum(h):
    flag = False
    tmp = []
    for i in range(n):
        if i == n-1 and h[i] != f[i]:
            tmp.append(i)
            f = calc(f,tmp)
            ans+=1
        elif h[i] == f[i] and flag:
            f = calc(f,tmp)
            ans+=1
            break
        elif h[i] != f[i]:
            flag = True
            tmp.append(i)
else:
    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
H.append(0) #H[-1]の処理専用
ans = 0

#0が見つかるまで0ではないindexをmemoに残して、0が見つかったらmemoの要素を-1する。
#最後の要素のみ、-1するタイミングがなくなるので、H.append(0)しておいた。
while sum(H) > 0:
    i = 0
    memo = []
    while i < N+1:
        if H[i] > 0:
            memo.append(i)
        if H[i] == 0:
            if len(memo):
                for i in memo:
                    H[i] -= 1
                memo = []
                ans += 1
        i += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
H.append(0) #H[-1]の処理専用
ans = 0

while sum(H) > 0:
    i = 0
    memo = []
    while i < N+1:
        if H[i] > 0:
            memo.append(i)
        if H[i] == 0:
            if len(memo):
                for i in memo:
                    H[i] -= 1
                memo = []
                ans += 1
        i += 1

print(ans)

Submission status: Accepted

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

ans = 0
while sum(h) > 0:
  for k in range(len(h)):
    if h[k] > 0:
      h = h[k:]
      break
  for k in reversed(range(len(h))):
    if h[k] > 0:
      h = h[:(k+1)]
      break
  cnt = 1
  for k in range(len(h)):
    if h[k] == 0 and h[k+1] > 0:
      cnt += 1
  h = [max(c-1,0) for c in h]
  ans += cnt

print(ans)

Submission status: Accepted

Solution code:
import itertools
N = int(input())
arr = list(map(int, input().split()))
ans = 0
while sum(arr) != 0:
    tmp = [0]*N
    tmp2 = 0
    for i in range(N):
        if arr[i] != 0:
            tmp[i] = 1
            arr[i] -= 1
    gr = itertools.groupby(tmp)
    for key, group in gr:
        if key != 0:
            tmp2 += 1
    ans += tmp2
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
L = list(map(int,input().split()))
#h = L[0]
L = [0]+L
h = 0
cnt = 0
for i in range(1,n+1):
    if L[i-1] < L[i]:
        h += L[i] - L[i-1]
        if i == n:
            cnt +=h
    elif L[i-1] >= L[i]:
        cnt +=h
        h = 0

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())

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

c = 0

m = max(h)
for i in range(1,m+1)[::-1]:
    l = -2
    for j in range(n):
        if(h[j] == i):
            if(j != l+1):c += 1
            h[j] -= 1
            l = j
print(c)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int,input().split()))
def dfs(ans, H):
    if not any(H):
        return ans
    flag = 1
    cnt = 0
    for i in range(N):
        if H[i] != 0:
            H[i] -= 1
            if flag:
                flag = 0
                cnt += 1
        else:
            flag = 1
    return dfs(ans + cnt, H)
print(dfs(0, H))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
pre = 0
for i in h:
    ans += max(0, i-pre)
    pre = i
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

while True:
    if h==[0]*(N+1):
        print(ans)
        exit()
        
    for i in range(N):
        if h[i]>0 and h[i+1]==0:
            ans += 1
        
    for i in range(N):
        h[i] = max(h[i]-1, 0)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
pre = 0
for i in h:
    ans += max(0, i-pre)
    pre = i
print(ans)

Submission status: Accepted

Solution code:
counter=0
def answer(h:list) -> int:
  global counter
  if len(h)==0:
    return
  if len(h)==1:
    counter+=h[0]
    return
  min_a=min(h)
  for i in range(len(h)):
    h[i]-=min_a
  counter+=min_a
  answer(h[:h.index(0)])
  answer(h[h.index(0)+1:])

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

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
cnt = 0
while True:
    if len(H) == 1:
        break
    for i in range(len(H)):
        if H[i] == 0:
            if i == 0:
                H.pop(i)
                cnt -= 1
            break
        H[i] -= 1
    cnt += 1

print(cnt+H[0])

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
now = h.copy()

def water(now):
    for i in range(n):
        if now[i] == 0:
            continue
        else:
            for j in range(i, n):
                if now[j] != 0:
                    now[j] -= 1
                else:
                    break
            break
c=0

while True:
    if now == [0]*n:
        break
    water(now)
    c += 1

print(c)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split())) + [0]
az = [0]*(N+1)
ans = 0
while H != az:
    c = 0
    in_itv = False
    for i in range(N+1):
        if H[i] == 0:
            if in_itv:
                c += 1
                in_itv = False
            continue
        in_itv = True
        H[i] -= 1
    ans += c
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.append(0)

kyokusyou = 0
count = 0
gezan = False
for i in range(n):
  if not gezan and a[i+1] < a[i]:
    count += a[i] - kyokusyou
    gezan = True
  elif gezan and a[i+1] > a[i]:
    gezan = False
    kyokusyou = a[i]

print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
_, *h = map(int, open(0).read().split())
s = p = 0
for i in h:
    s += max(i - p, 0)
    p = i
print(s)

Submission status: Accepted

Solution code:

icase=0
if icase==0:
    n=int(input())
    h=list(map(int,input().split()))
elif icase==1:
    n=5
    h=[3,1,2,3,1]
    ans=5
elif icase==2:
    n=4
    h=[1,2,2,1]
    ans=2
elif icase==3:
    n=8
    h=[4, 23, 75, 0, 23, 96, 50, 100]
    ans=221

hmin=10**6

def hct(hh,ll,rr,hcnt0):
#    print("0: ll:",ll,"rr:",rr,"hcnt0:",hcnt0,"h:",h)
    if ll>=rr:
        return hcnt0
    hcnt0+=1
    hmax=-1
    hhim=-1
    lll=ll
    for i in range(ll,rr):
        hh[i]-=1
        hmax=max(hh[i],hmax)
        if hh[i]>0 and hhim<=0:
            lll=i
            hhim=1
        if hh[i]<=0 and hhim>0:
            rrr=i
            hhim=-1
            hcnt0=hct(hh,lll,rrr,hcnt0)
    if hhim>0:
        hcnt0=hct(hh,lll,rr,hcnt0)
    return hcnt0
        
hcnt=0
ll=0        
hhim=-1
for i in range(n):
    if h[i]==0 and hhim>0:
        hcnt=hct(h,ll,i,hcnt)
        lll=i
        hhim=-1
    elif h[i]==0:
        hhim=-1
    elif h[i]>0 and hhim<0 :
        ll=i
        hhim=1
if hhim>0:
    hcnt=hct(h,ll,n,hcnt)
print(hcnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
j = 0
start = 0
ans = 0
number_list = list(map(int,input().split()))
after_l = []
B = []
for i in range(N):
    for i in range(N):
        if number_list[i] == 0:
            if B != []:
                after_l.append(B)
                B=[]
            after_l.append([0])
        else:
            B.append(number_list[i])
    if B != []:
        after_l.append(B)
    for i in range(len(after_l)):
        C = after_l[i]
        C = min(C)
        ans += C
        after_l[i] = list(map(lambda x:x-C,after_l[i]))
    number_list = sum(after_l,[])
    after_l = []
    B = []
print(ans)

Submission status: Accepted

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

f = [0] * n
ans = 0

while h != f:
    flag = 0
    for i in range(n):
        if i == n - 1:
            if f[i] < h[i]:
                f[i] += 1
                ans += 1
            elif f[i] == h[i] and flag == 0:
                ans += 1
        elif i == 0:
            if f[i] < h[i]:
                f[i] += 1
                flag = 0
            elif f[i] == h[i]:
                flag = 1
        elif i != n - 1:
            if f[i] < h[i]:
                f[i] += 1
                flag = 0
            elif f[i] == h[i] and flag == 0:
                ans += 1
                flag = 1

print(ans)

Submission status: Accepted

Solution code:
counter = 0
def myAnswer(H:list) -> int:
   global counter
   if(len(H)==0):
      return
   if(len(H)==1):
      counter += H[0]
      return
   MIN = min(H)
   for i in range(len(H)):
      H[i] -= MIN
   counter += MIN
   myAnswer(H[:H.index(0)])
   myAnswer(H[H.index(0)+1:])


def modelAnswer():
   return
def main():
   N = int(input())
   H = list(map(int,input().split()))
   myAnswer(H[:])
   print(counter)
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:

N = int(input().rstrip())
Hs = [int(x) for x in input().rstrip().split()]

max_height = max(Hs)

tot_num=0
for i in range(max_height): #iはチェックする高さ
    temp_num = 0
    continuum=0
    for j in range(0,N):
        if continuum==0 and Hs[j]>i:
            temp_num+=1
            continuum+=1
            continue
        elif Hs[j]>i:
            continuum+=1
            continue
        else:
            continuum = 0
            continue
    tot_num += temp_num


print(tot_num)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h_list = list(map(int,input().split()))
ans = 0
f = True
while f:
    f = False
    e = True
    for i in range(n):
        if h_list[i] > 0:
            h_list[i] -= 1
            if e:
                ans += 1
            f = True
            e = False
        else:
            e = True
print(ans)

Submission status: Accepted

Solution code:
n = int(input()) 
from collections import deque
from copy import deepcopy
d = deque()
a = list(map(int,input().split()))
c = 0
for i in range(len(a)):
    d.append(a[i])
def wat(a):
    global c
    if len(a) != 0:
        while a[0] != 0:
            c += 1
            for i in range(len(a)):
                if a[i] >0 :
                    a[i] = a[i] - 1
                else:
                    break
        b = deepcopy(a)
        for i in range(len(b)):
            if a[i] == 0:
                b.popleft()
            else:
                break
        wat(b)
wat(d)
print(c)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
f = True
while f:
    f = False
    e = True
    for i in range(n):
        if h[i] > 0:
            h[i] -= 1
            if e:
                ans += 1
            f = True
            e = False
        else:
            e = True
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    H = [int(a) for a in input().split()]
    
    cnt = 0
    for i in range(max(H)):
        S = [False for i in range(len(H))]
        for j in range(len(H)):
            if H[j] != 0 and S[j] == False:
                for k in range(j, len(H)):
                    if H[k] != 0 and S[k] == False:
                        H[k] -= 1
                        S[k] = True
                    else:
                        break
                cnt += 1
    print(cnt)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0
c = 0
while max(h) != 0:
    flag = False
    for i in range(n):
        if h[i] != 0:
            h[i] -= 1
            flag = True
            if i == n-1:
                ans += 1
                c += 1
        elif flag:
            flag = False
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(x) for x in input().split()]
key = 0
ans = 0
for i in range(n):
  if key <= a[i]:
    ans += a[i]-key
    key = a[i]
  else:
    key = a[i]
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

ans = 0

if h.count(0) == 0:
  ans += min(h)
  h = list(map(lambda x:x-min(h) ,h))

while h.count(0) != len(h):

  pos = 0
  for i in range(len(h)):
    if h[i] != 0:
      pos = i
      break

  pos2 = len(h)
  for j in range(pos, len(h)):
    if h[j] == 0:
      pos2 = j
      break

  ans += min(h[pos:pos2])
  h[pos: pos2] = list(map(lambda x: x - min(h[pos:pos2]), h[pos:pos2]))
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
counter = 0
def myAnswer(H:list) -> int:
   global counter
   if len(H) is 0:return
   if len(H) is 1:
      counter = counter + H[0]
      return
   m = min(H)
   counter += m
   H = [i - m for i in H]
   while 0 in H:
      length = len(H[:H.index(0)])
      myAnswer(H[:H.index(0)])
      H = H[H.index(0)+1:]
   myAnswer(H)


def modelAnswer():
   return
def main():
   N = int(input())
   H = list(map(int,input().split()))
   myAnswer(H[:])
   print(counter)
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

# import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
N = int(input())
arrA = list(map(int,input().split()))

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

print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
N = int(input())
arrA = list(map(int,input().split()))

#ゼロでリストを分割する
# def split_list(st):
#     lis = st.split("0")
#     lis = [l for l in lis if l != ""]
#     return lis

def split_list(lis,num=0):
    res = []
    tmp = []
    for i in lis:
        if i != num:
            tmp.append(i)
        elif i == num and len(tmp)>0:
            res.append(tmp)
            tmp = []
        else:
            continue
    else:
        res.append(tmp)
    res = [l for l in res if len(l) != 0]
    return res

stack = split_list(arrA)
ans = 0
#リスト毎に処理.len0は処理しない。
while len(stack)>0:
    lis1 = stack.pop()
    arr = np.array(list(map(int,list(lis1))))
    #最小値を引いたリストにする。最小値をansに加算
    nmin = min(arr)
    ans += nmin
    arr = arr - nmin
    s = list(arr)
    lis2 = split_list(s)
    stack = stack + lis2

print(ans)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

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

ans = 0
while True:
    l, r, = -1, n
    num = -1
    for i in range(n):
        if l == -1:
            if h[i] > 0:
                l = i
                num = h[i]
        else:
            if h[i] == 0:
                r = i
                break
            else:
                num = min(num, h[i])
    if l == -1:
        break

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

data = [0] + data

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

Submission status: Accepted

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

Submission status: Accepted

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