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:
#coding:utf-8
N = int(input())
H = list(map(int,input().split()))
task = []
#print (N,H)
ans = 0
while(1):
    min_H = min(H)
    ans += min_H
    H = [h -  min_H for h  in H]
    idx = H.index(0)
    #print (H)
    head = H[:idx]
    tail = H[idx+1:]
    #print (head,tail)
    if len(head) > 0:
        task.append(head)
    if len(tail) > 0:
        task.append(tail)
    if len(task) == 0:
        break
    H = task.pop(0)
print (ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print Ans

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from statistics import mean, median,variance,stdev
import sys
import math
import fractions

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


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

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

x = ip()                              #入力整数1つ
#x,y= (int(i) for i in input().split())      #入力整数横2つ
#x,y,z = (int(i) for i in input().split())    #入力整数横3つ
#n,m,x,y= (int(i) for i in input().split())  #入力整数横4つ
a = [int(i) for i in input().split()]        #入力整数配列
#a = input()                                  #入力文字列
#a = input().split()                          #入力文字配列

#jの変数はしようできないので注意
num=0
while sum(a):
    l = 0
    while a[l] == 0: l+=1
    r=l
 #   print("left:",l)
    if l != len(a)-1:
        while a[r+1]!=0:
 #           print("right:",r)
            r+=1
            if r == len(a)-1:break
    for i in range(l,r+1):
        a[i]-=1
    num+=1
  #  print(a)
print(num)

Submission status: Accepted

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

ct = 0

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

print(ct)

Submission status: Accepted

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

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

l=par(h)
ans=0

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

Submission status: Accepted

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

ans = 0

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

cnt = 0
#左から1以上の数を未定って0があったらそこまでの数を-1してカウントを1増やす
def mizu(h_list):
    hidari = 0
    migi = len(h_list)
    tmp = []
    for index,elem in enumerate(h_list):
        if elem == 0:
            continue
        if elem >0:
            hidari = index
            break
    for index,elem in enumerate(h_list[hidari:]):
        if elem >0:
            continue
        elif elem == 0:
            migi = hidari + index
            break
    for index,elem in enumerate(h_list):
        if index >= hidari and index <= migi-1:
            elem = elem-1
        tmp.append(elem)
    return tmp

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

Submission status: Accepted

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

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

count = pour_counter(height)
print(count)

Submission status: Accepted

Solution code:
import copy
N = int(input())
series = list(map(int,input().split()))
final = [0 for _ in range(len(series))]
count = 0
while series != final:
	start = 0
	end = 0
	for i in range(len(series)):
		if series[i] == 0:
			if start != i:
				end = i
				m = min(series[start:end])
				for j in range(start,end):
					series[j] -= m
				count += m
			else:
				start = i + 1
		elif i == len(series)-1:
			end = i
			m = min(series[start:i+1])
			for j in range(start,i+1):
				series[j] -= m
			count += m
print(count)

Submission status: Accepted

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

#調べっていってmaxのところでカウントアップ。minで保持。
H.append(0)

res = 0
up = True
bottom = 0
for i in range(N):
    if (up):
    	if H[i] > H[i+1]:
            #ピークを発見
            res += (H[i] - bottom)
            up = False
    else:
        if H[i] < H[i+1]:
            #ボトムを発見
            bottom = H[i]
            up = True
print(res)

Submission status: Accepted

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

counter = 0
maximum = max(h)

while maximum > 0:
    array = []
    for i in range(N):
        if h[i] == maximum:
            if i-1 not in array:
                counter += 1
            h[i] -= 1
            array.append(i)

    maximum -= 1

print(counter)

Submission status: Accepted

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

print(sum(cnt))

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
sorth = sorted(h)[::-1]
count = 0
time = 0
while(h !=[0]*N):
    for i in range(N):
        if i == N-1 and h[i] > 0:
            count += 1
        elif h[i] > 0 and h[i+1] == 0 : count += 1
        h[i] -= 1 if h[i] > 0 else 0
print(count)

Submission status: Accepted

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

ans = 0
i = 0
while i < N:
    while h[i] == 0:
        i += 1
        if i == N: break
    j = i
    if i == N: break
    while h[j] > 0:
        h[j] -= 1
        j += 1
        if j == N: break
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    hs = list(map(int, input().split()))

    cnt = 0
    hss = [hs[:]]
    while True:
        tmp_sum = 0
        for tmp_hs in hss:
            tmp_sum += sum(tmp_hs)
        if tmp_sum == 0:
            break

        for hs_idx, tmp in enumerate(hss):
            if 0 in tmp:
                idx_0 = tmp.index(0)
                del hss[hs_idx]
                hss.append(tmp[:idx_0])
                hss.append(tmp[idx_0+1:])

        for tmp in hss:
            if not tmp:
                continue
            min_tmp = min(tmp)
            cnt += min_tmp

            for i in range(len(tmp)):
                tmp[i] -= min_tmp


        #print(hss)
    print(cnt)

main()

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0
minh = min(h)
ans += min(h)

h = list(map(lambda x: x-minh, h))

if set(h) == {0}:
    print(ans)

else:
    while True:
        rightflag = False

        for i in range(N):
            if h[i] == 0:
                continue
            else:
                left = i
                break

        for j in range(left,N):
            if h[j] != 0:
                continue
            else:
                right = j-1
                rightflag = True
                break

        if not rightflag:
            right = N-1

        # print(left,right)

        tmp = min(h[left:right+1])
        ans += tmp

        for pos in range(left,right+1):
            h[pos] -= tmp

        # print(h)
        if set(h) == {0}:
            break

    print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = 0
h_before = 0
count = 0
MaxLevel = 0
for i in input().split():
	h_before = h
	h = int(i)
	diff = h - h_before
	if diff >= 0:
		count += diff
		MaxLevel = h

print(count)

Submission status: Accepted

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

m = h[0]

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

print(m)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    h = list(map(int, input().split()))
    now_h = []
    for _ in range(N):
        now_h.append(0)
    i = 0
    count = 0
    while True:
        if now_h[i] < h[i]:
            # ノルマに達していない
            end = i
            for j in range(i + 1, N):
                if now_h[j] < h[j]:
                    end = j
                else:
                    break
            for k in range(i, end + 1):
                now_h[k] += 1
            count += 1
        else:
            if i == N - 1:
                break
            i += 1
            continue
    print(count)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def c_grand_garden(N, H):
    ans = min(H)
    for k in range(N):
        H[k] -= ans
    H.append(0)
    for k in range(N):
        if H[k] > H[k + 1]:
            ans += H[k] - H[k + 1]
    return ans

N = int(input())
H = [int(i) for i in input().split()]
print(c_grand_garden(N, H))

Submission status: Accepted

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

cnt = 0

for j in range(N):
  while h[j] > 0:
    for i in range(j, N):
      if h[i] == 0:
        break
      else:
        h[i] -= 1
    cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
def water(a, l, r):
    for i in range(l, r+1):
        a[i] -= 1


N = int(input())
a = list(map(int, input().split()))
cnt = 0
while True:
    l = N
    r = N-1
    for i in range(N):
        if a[i] != 0:
            l = i
            break

    if l == N:
        break
    else:
        for i in range(l, N):
            if a[i] == 0:
                r = i-1
                break
        
    water(a, l, r)
    cnt += 1

print(cnt)

Submission status: Accepted

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

def f(L):
    if len(L) == 0:
        return 0

    if len(L) <= 2:
        return max(L)

    l = min(L)
    i = np.where(L == l)[0][0]
    return f(L[:i] - l) + f(L[i + 1:] - l) + l

print(f(H))

Submission status: Accepted

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

cnt = 0
# 最大値を抽出

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# %%
N = int(input())
h = [int(hk) for hk in input().split()]


# %%
def step(h):
    if sum(h) == 0:
        return 0

    if 0 in h:
        split = h.index(0)
        return step(h[:split]) + step(h[split + 1:])

    else:
        new_h = [hk - 1 for hk in h]
        return 1 + step(new_h)


# %%
print(step(h))

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
p=a[:]
d=max(p)
c=0
for i in range(d):
    k=0
    for i in range(n):
        if a[i]>0:
            if k==0:
                c+=1
                k=1
            a[i]-=1
        else:
            k=0
print(c)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# coding: UTF-8
import sys
#sys.setrecursionlimit(n)
import heapq
import re
import bisect
import random
import math
import itertools
from collections import defaultdict, deque
from copy import deepcopy
from decimal import *

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

count = 0
while(1):
    if sum(a) == 0:
        break
    b = False
    for i in range(n):
        if a[i] == 0:
            if b == True:
                break
            continue
        else:
            a[i] -= 1
            b = True
    count += 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
hs = map(int, input().split())
b = 0
m = 0
for h in hs:
  if b > h:
    m += b - h
  b = h
m += b
print(m)

Submission status: Accepted

Solution code:
import math
import copy




def nCast(number):
    if type(number)==str:
        return int(number)
    for idx in range(0,len(number)):
        if type(number[idx])==str:

            number[idx]=int(number[idx])
        else:
            nCast(number[idx])
    return number
    
def inputArr(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l
def inputArr1(w):
    l=list()
    for idx in range(0,w):
        l.append(input())
    return l

a=nCast(input())
b=nCast(input().split())


def help(max):
    count=0
    tmp=0
    for idx in range(0,len(b)):
        if b[idx]==max:
            b[idx]=b[idx]-1
            if tmp==0:
                tmp=1
        else:
            if tmp==1:
                tmp=0
                count+=1

    return tmp+count

def func():
    result=0
    max=100
    while max!=0:
        result+=help(max)
        max-=1
    return result
print(func())

Submission status: Accepted

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

def uniq(h):
	res = []
	pre = -1
	for x in h:
		if pre != x:
			res.append(x)
		pre = x
	return res

ans = 0
while(True):
	h = uniq(h)
	if len(h)== 1:
		ans += h[0]
		break
	m = max(h)
	ans+=h.count(m)
	h = [x if x != m else m-1 for x in h]
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
l = IL()

dp = [0]*N
dp[0] = l[0]

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

print(dp[-1])

Submission status: Accepted

Solution code:
import collections
import math
import re
import numpy
def ai():return input()#s
def a():return int(input())#a,n or k
def ma():return map(int, input().split())#a,b,c,d or n,k
def ms():return map(str, input().split())#,a,b,c,d
def lma():return list(map(int, input().split()))#x or y
def lms():return list(map(str, input().split()))#x or y
def say(i):return print("Yes" if i == 0 else "No")
def Say(i):return print("YES" if i == 0 else "NO")
k= [0]
l = []
f = 0
s=0
c=0
e =1
a  =a()
x = lma() +[0]
m = min(x)
for i in range(1,a+1):
    if x[i] < x[i-1]:
        s+=x[i-1]-m
        m = x[i]
print(s)

Submission status: Accepted

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

s = [0]*(N+1)
for i in range(N):
    s[i]+=h[i]
    s[i+1]-=h[i]
ans = 0
for si in s:
    if si>0: ans += si
print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    hs = list(map(int, input().split()))
    height = hs[0]
    count=0
    for h in hs[1:]:
        if height>h:
            count+=height-h
            height=h
        else:
            height=h
    print(int(count+height))

Submission status: Accepted

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

ans = 0
c = 0
p = 0
s = 0
while any(h != 0 for h in H):
    for i, h in enumerate(H):
        if h > p:
            c = 1
            p = h
            s = i
        elif h < p:
            break
        else:
            c += 1
    ans += 1
    for i in range(c):
        H[s + i] -= 1
    p = 0
print(ans)

Submission status: Accepted

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

def feedcount(heights):
    counter = 0
    hiszero = True
    for h in heights:
        if h != 0 and hiszero:
            hiszero = False
            counter += 1
        elif h == 0:
            hiszero = True
    return counter

def feedwater(heights):
    return list(map(lambda h: max(0,h-1), heights))

total = 0
while(feedcount(heights) != 0):
    total += feedcount(heights)
    heights = feedwater(heights)
print(total)

Submission status: Accepted

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

mizu = 0

for i in range(len(h_list)-1):
    if h_list[i] > h_list[i+1]:
        mizu += h_list[i] - h_list[i+1]
        
mizu += h_list[-1]

print(mizu)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
# from collections import Counter, defaultdict

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n = II()
H = LI() + [0]

ans = 0
state = 1
for i in range(max(H)):
    for h in H:
        if state:
            if h >= i + 1:
                state = 0
        else:
            if h < i + 1:
                ans += 1
                state = 1


print(ans)

Submission status: Accepted

Solution code:
import numpy as np

def counter(array):
    if array.size == 0:
        return 0
    if array.size == 1:
        return array[0]
    min_num = np.min(array)
    idx = np.argmin(array)
    tmp = array - min_num
    return min_num + counter(tmp[:idx]) + counter(tmp[idx+1:])


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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
hl = list(map(int, input().split()))

res = 0

for i in range(n):
    if hl[i]==0:
        continue

    while hl[i]>0:
        res += 1
        for j in range(i,n):
            if hl[j]==0:
                break
            hl[j] -= 1

print(res)

Submission status: Accepted

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

ans = 0
que = [H]

while que:
    h = que.pop()
    h_min = min(h)
    ans += h_min
    h = [hi - h_min for hi in h]

    tmp = []
    for hi in h:
        if hi == 0:
            if tmp:
                que.append(tmp)
                tmp = []
        else:
            tmp.append(hi)
    else:
        if tmp:
            que.append(tmp)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 20 20:51:36 2019

@author: Yamazaki Kenichi
"""

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

ans = h[0]

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

print(ans)

Submission status: Accepted

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

if n >= 2:
    answer = 0
    while max(h) > 0:
        c = 1 if h[0] > 0 else 0
        for i in range(n - 1):
            if h[i] == 0 and h[i + 1] > 0:
                c += 1
        answer += c
        h = [max([i - 1, 0]) for i in h]
    print(answer)
else:
    print(h[0])

Submission status: Accepted

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

for i in range(1,max_height+1):
    switch = 0
    for j in range(n):
        if switch == 0:
            if h[j] >= i:
                switch = 1
                ans += 1
        if switch == 1:
            if h[j] < i:
                switch = 0

print(ans)

Submission status: Accepted

Solution code:
def A():
    ab, bc, ca = map(int, input().split())
    print(int(bc * ab / 2))
#A()

def B():
    s = int(input())
    f_data = [s]
    i = 1
    a = [s]
    tmp = [0 for i in range(1000000)]
    def f(n):
        if n % 2 == 0:
            return n // 2
        else:
            return 3 * n + 1

    while True:
        key = f(f_data[-1])
        f_data.append(key)
        if tmp[key] == 1:
            ans = i + 1
            break
        tmp[key] = 1
        i += 1
    print(ans)
#B()

def C():
    N = int(input())
    cnt = 0
    h = list(map(int, input().split()))
    while sum(h) != 0:
        cnt += 1
        for i in range(N):
            if h[i] > 0:
                #print(i)
                for j in range(i, N):
                    #print(i, j)
                    if h[j] == 0:
                        break
                    else:
                        h[j] -= 1
                #print(h, cnt)
                break
    print(cnt)
C()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
tank = 0
while True:
    for i in range(len(h)):
        if h[i] == 0 and tank != 0:
            count += 1
            tank = 0

        elif h[i] != 0:
            h[i] -= 1
            tank = 1
    if sum(h) == 0:
        break

print(count)

Submission status: Accepted

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

cnt = 0
while True:
  f = 0
  if list(set(h)) == [0]:
    break
  for i in range(N):
    if h[i] == 0:
      if f == 1:
        f = 0
        cnt += 1
    else:
      h[i] -= 1
      f = 1
  if f == 1:
    cnt += 1
  
    
print(cnt)

Submission status: Accepted

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

ans=0

while h!=[]:
	if h[0]==0:
		h.pop(0)
		continue
	i=0
	while True:
		if i<len(h) and h[i]!=0:
			h[i]-=1
			i+=1
		else:
			i=0
			ans+=1
			break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
while 1:
    for i in range(N):
        if h[i] != 0:
            break
    else:
        break
    con0 = 1
    for i in range(N):
        if con0 == 1 and h[i] != 0:
            c += 1
            con0 = 0
        elif con0 == 0 and h[i] == 0:
            con0 = 1
        if h[i] != 0:
            h[i] -= 1

print(c)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int,input().split()))
A = [max(0,H[i] - H[i + 1]) for i in range(len(H) - 1)]

print(sum(A) + H[-1])

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
h_list = list(map(int, input().split()))
ar = np.zeros(N+2)
ar[1:N+1] = np.array(h_list)


cnt = 0
search_flag = False
ss = 0
while True:
    for i in range(N + 2):
        if search_flag:
            if ar[i] == 0:
                search_flag = False
                end_i = i
                cnt += 1
                ar[start_i:end_i] -= 1
        if not search_flag:
            if ar[i] > 0:
                search_flag = True
                start_i = i
    if sum(ar) == 0:
        break
    ss += 1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
ans=0
hlist=[[int(x) for x in input().split()]]
for h in hlist:
    count=min(h)
    ans+=count
    tmp=[]
    for i in h:
        i=i-count
        if i == 0:
            if tmp==[]:
                continue
            hlist.append(tmp)
            tmp=[]
            continue
        tmp.append(i)
    if len(tmp)>0:
        hlist.append(tmp)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
count = 0
while(h.count(0)!=N):
    for k in range(N):
        if h[k]!=0:
            break
    for j in range(k,N):
        if h[j]==0:
            j-=1
            break
    for i in range(k,j+1):
        h[i]-=1
    count+=1
print(count)

Submission status: Accepted

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

ans = 0
for i in range(1, N+1):
    if hs[i-1] < hs[i]:
        ans += (hs[i] - hs[i-1])
print(ans)

Submission status: Accepted

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

ans=0
c=0
for i in range(N):
    if A[i]>=c:
        ans+=A[i]-c
        c=A[i]
    elif A[i]<c:
        c=A[i]
print(ans)

Submission status: Accepted

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

count = 0
for i in range(1, 101):
  flag = False
  for j in range(n):
    if h[j] >= i:
      if not flag:
        flag = True
        count += 1
    else:
      if flag:
        flag = False
print(count)

Submission status: Accepted

Solution code:
import numpy as np


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

R = 0

for i in range(1, max(H)+1):
    FL = ''.join(['1' if h else '0' for h in (H >= i).tolist()]).split('0')
    while '' in FL:
        FL.remove('')
    R += len(FL)

print(R)

Submission status: Accepted

Solution code:
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(input())
def S(): return input()
n=I()
h=IL()
c=0
while True:
    for i in range(n):
        if h[i]!=0:
            s=i
            break
    else:
        print(c)
        break
    
    for i in range(i,n):
        if h[i]==0:
            e=i
            break
    else:
        e=n
    
    for i in range(s,e):
        h[i]-=1
    c+=1