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:
n = int(input())
goal = list(map(int,input().split()))
now = [0]*n
ans = 0

while goal != now:
    x = 0
    for i in range(n):
        if x == 0 and now[i] != goal[i]:
            x = 1
            now[i] += 1
        elif x == 1 and now[i] != goal[i]:
            now[i] += 1
        elif x == 1 and now[i] == goal[i]:
            break
    ans += 1

print(ans)

Submission status: Accepted

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

sumh = h[0]
for x in range(N):
    if x != N - 1:
        if h[x + 1] > h[x]:
            sumh += h[x + 1] - h[x]
print(sumh)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
while sum(H) > 0:
    pre = 0
    for i in range(N):
        if pre > 0 and H[i] == 0:
            break
        if H[i] != 0:
            H[i] -= 1
            pre = H[i] + 1
    ans += 1

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    h = list(map(int, input().split())) + [0]
    ans = 0; conti = False
    for line in range(1, 101):
        for i in range(N+1):
            if h[i] >= line and conti:
                continue
            elif h[i] >= line and not conti:
                conti = True
                ans += 1
            elif h[i] < line and conti:
                conti = False
            elif h[i] < line and not conti:
                continue
            else:
                pass

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline
INF = float("inf")


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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
h = list(input().split())
flower = deque([int(h[i]) for i in range(N)])
goal = [0 for i in range(N)]
count = 0

flower.append(0)

while flower != deque([]):
    for i in range(len(flower)):
        if flower[0] == 0:
            flower.popleft()
        else:
            break
    
    for i in range(len(flower)-1):
        if flower[i] == 0 and flower[i+1] != 0:
            count += 1
        elif flower[i] == 0:
            continue
        elif i == len(flower) - 1:
            count += 1
            flower[i] -= 1
        else:
            flower[i] -= 1
    count += 1

print(count-1)

Submission status: Accepted

Solution code:
n=int(input())
hs=list(map(int,input().split()))
c=0
m=max(hs)
while not m==0:
    b=0
    for i,h in enumerate(hs):
        if h==m:
            if not b==m:
                c+=1
            hs[i]-=1
        b=h
    m=max(hs)
print(c)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
sum_h = sum(h)
sum_now = 0
ans = 0

while sum_h != sum_now:
    for i in range(n):
        if h[i] != 0:
            start = i
            break
     
    goal = n
    
    for i in range(n-start):
        if h[start+i] == 0:
            goal = start + i
            break
            
        if start + i == n-1:
            goal == n
        
    water = min(h[start:goal])
    
    for i in range(start,goal):
        h[i] -= water
        
    ans += water
    sum_now += water * (goal-start)
        
print(ans)

Submission status: Accepted

Solution code:
import fractions
import sys
from functools import reduce
import math

input = sys.stdin.readline


def main():
    n = int(input())
    h = input_list()
    count = 0
    while list(set(h)) != [0]:
        count += 1
        flag = False
        for i, v in enumerate(h):
            if v != 0:
                flag = True
                h[i] -= 1
            else:
                if flag:
                    break
    print(count)

def divide_two(n):
    c = 0
    while True:
        if c >= 2:
            break
        if n % 2 != 0:
            break
        n //= 2
        c += 1
    return c

def get_camulative(l):
    import itertools
    # 累積和
    return [0] + list(itertools.accumulate(l))

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


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


def input_list_str():
    return list(map(str, input().split()))


def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)


def gcd(*numbers):
    return reduce(fractions.gcd, numbers)


def gcd_list(numbers):
    return reduce(fractions.gcd, numbers)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n = int(ipt())
    h = [int(i) for i in ipt().split()]
    ans = 0
    f = True
    mih = min(h)
    for i,hi in enumerate(h):
        if hi == mih:
            f = False
        elif f:
            ans += max(0,hi-h[i+1])
        else:
            ans += max(0,hi-h[i-1])
    print(ans+mih)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
Tree_List=list(map(int,input().split()))
ct =0
for i in range(n):
    while Tree_List[i] != 0:
        k = i + 1
        while k < n and Tree_List[k] != 0: 
            k+=1
        Tree_List[i:k]=map(lambda x:x-1,Tree_List[i:k])
        ct += 1
        
print(ct)

Submission status: Accepted

Solution code:
n=int(input())
Tree_List=list(map(int,input().split()))
ct =0
for i in range(n):
    while Tree_List[i] != 0:
        k = i
        while k + 1 < n and Tree_List[k + 1] != 0: 
            k+=1
        Tree_List[i:k + 1]=map(lambda x:x-1,Tree_List[i:k + 1])
        ct += 1
        
print(ct)

Submission status: Accepted

Solution code:
n=int(input())
Tree_List=list(map(int,input().split()))
ct =0
for i in range(n):
    while Tree_List[i] != 0:
        k = i
        while Tree_List[k] != 0: 
            k+=1
            if k == n:
                break
        Tree_List[i:k]=map(lambda x:x-1,Tree_List[i:k])
        ct += 1
        
print(ct)

Submission status: Accepted

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

while any(h for h in H):
	count += 1
	flag = False
	for i, h in enumerate(H):
		if h:
			flag = True
			H[i] -= 1
		else:
			if flag == True:
				break
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
h=list(map(int,input().split()))
h.insert(0,0)
h.append(0)
p=max(h)
arr=np.array(h)
cnt=0
for i in range(p,0,-1):
    x=np.copy(arr)
    x[x!=i]=0
    x[x==i]=1
    temp=np.arange(len(x))
    temp[x>0]=0
    np.maximum.accumulate(temp,out=temp)
    left=temp
    x_reversed=x[::-1]
    temp=np.arange(len(x))
    temp[x_reversed>0]=0
    np.maximum.accumulate(temp,out=temp)
    right=len(x)-1-temp[::-1]
    y=right-left-1
    y[x==0]=0  
    z=np.nonzero(np.diff(np.nonzero(x==0)[0])-1)
    cnt+=len(z[0])
    arr=arr-x
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

p=max(h)
cnt=[]
ct=0


for i in range(p):
    ct=0
    for j in range(len(h)-1):
        if h[j]==p-i:
            if h[j]==h[j+1]:
                h[j]-=1
                if j==len(h)-2:
                    ct+=1
                    h[j+1]-=1
            else:
                ct+=1
                h[j]-=1
        if j==(len(h)-2):
            if h[j]!=p-i and h[j+1]==p-i:
                h[j+1]-=1
                ct+=1
    cnt.append(ct)
    #print(h,ct)


k=sum(cnt)
if len(h)==1:
    print(h[0])
else:
    print(k)

Submission status: Accepted

Solution code:
n=0
Answer=0
N=int(input())
h=list(map(int,input().split()))
h.insert(0,0)

for i in range(N):
    if h[n]<=h[n+1]:
        m=h[n+1]-h[n]
        Answer=Answer+m
    else :
        pass
    n=n+1
print(Answer)

Submission status: Accepted

Solution code:
from itertools import groupby
n = int(input())
h = list(map(int,input().split()))
max_h = max(h)
for i in range(n):
    h[i] = [h[i],0]
ans = 0
for i in range(max_h):
    for is_more,group in groupby(h, key=lambda a:bool(a[0] > a[1])):
        group = list(group)
        if is_more:
            ans += 1
    for hs in h:
        hs[1] += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int, input().split()))
count = 0
def mizu(h):
    global count
    if len(h) == 0:
        return
    if len(h) == 1:
        count += h[0]
        return
    m = min(h)
    count += m
    for i in range(len(h)):
        h[i] -= m
    while 0 in h: #[1,1,0,1,0,1]の場合[1,1]と[1,0,1]に分かれるから
        mizu(h[:h.index(0)])
        h = h[h.index(0)+1:]
    mizu(h)

mizu(h)
print(count)

Submission status: Accepted

Solution code:
import sys


if __name__ == "__main__":
    n = int(input())
    h = list(map(int,input().split()))
    cnt = 0
    ans = 0
    while True:
        if sum(h) == 0:
            break
        koho = []
        p = []
        for i in range(n):
            if h[i] != 0:
                koho.append(h[i])
            else:
                p.append(koho)
                p.append([0])
                koho = []
        if len(koho)>0:
            p.append(koho)
        for i in range(len(p)):
            if len(p[i]) == 0:
                continue
            d = min(p[i])
            ans += d
            for j in range(len(p[i])):
                p[i][j] -= d
        h = []
        for i in range(len(p)):
            for j in range(len(p[i])):
                h.append(p[i][j])
        cnt += 1
    print(ans)

Submission status: Accepted

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

ans = 0
for i in range(1, max(lh)+1):
    if lh[0] >= i:
        cnt = 1
    else:
        cnt = 0
    j = 1
    while j < n:
        if lh[j-1] < i and lh[j] >= i:
            cnt += 1
        j += 1
    ans += cnt

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
cnt = int()
for i in reversed(range(1, max(h)+1)):
    tmp = list()
    for j in h:
        if i <= j:
            tmp.append(1)
        else:
            tmp.append(0)
    #print(tmp)
    if tmp[0]==1:
        cnt += 1
    for j in range(1, n):
        if tmp[j]-tmp[j-1] == 1:
            cnt += 1

print(cnt)

Submission status: Accepted

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

ans = 0
cnt = 0

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

print(ans)

Submission status: Accepted

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

ans = 0
cnt = 0

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=0
ans=min(h)
l=[min(h)]*n
#print(l)
while l!=h:
    x=0
    for i in range(n):
        if x==0:
            if i!=n-1:
                if l[i]!=h[i]:
                    l[i]+=1
                    x=1
            else:
                if l[i]!=h[i]:
                    l[i]+=1
                    x=1
                    ans+=1
        elif i!=n-1:
            if l[i]!=h[i]:
                l[i]+=1
            else:
                ans+=1
                break
        else:
            if l[i]!=h[i]:
                l[i]+=1
                ans+=1
            else:
                ans+=1
    #print(l,ans)
print(ans)

Submission status: Accepted

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

print(sum([h[i] - h[i - 1] for i in range(1, n) if h[i] > h[i - 1]]) + h[0])

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
# sys.setrecursionlimit(int(pow(10, 2)))
# sys.stdin = open("input.txt", "r")
# sys.stdout = open("output.txt", "w")
mod = int(pow(10, 9) + 7)
mod2 = 998244353
def data(): return sys.stdin.readline().strip()
def out(*var, end="\n"): sys.stdout.write(' '.join(map(str, var))+end)
def l(): return list(sp())
def sl(): return list(ssp())
def sp(): return map(int, data().split())
def ssp(): return map(str, data().split())
def l1d(n, val=0): return [val for i in range(n)]
def l2d(n, m, val=0): return [l1d(n, val) for j in range(m)]





# @lru_cache(None)
def solve(A):
    if(len(A)==0):
        return 0
    if(max(A)==min(A)):
        return min(A)
    if(0 not in A):
        x=min(A)
        A=[A[i]-x for i in range(len(A))]
        ans=x
        i=0
        while(i<len(A)):
            while(i<len(A) and A[i]==0):
                i+=1
            k=0
            while(i+k<len(A) and A[i+k]!=0):
                k+=1
            ans+=solve(A[i:i+k])
            i+=k
        return ans
    ans=0
    i=0
    while(i<len(A)):
        while(i<len(A) and A[i]==0):
            i+=1
        k=0
        while(i+k<len(A) and A[i+k]!=0):
            k+=1
        ans+=solve(A[i:i+k])
        i+=k
    return ans





n=l()
h=l()
ans=solve(h)
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
#n=int(input())
n = int(input())
h=list(map(int,input().split()))
isStarted = False
fin = False
flowers = [0] * len(h)
all = 0
for i in h:
    all += i
done = False
ans=0
while (not done):
    tmp = 0
    isStarted=False
    for i in range(len(flowers)):
        if (flowers[i] == h[i] and isStarted):
            ans+=1
            isStarted = False
            break
        if (flowers[i] < h[i] and not isStarted):
            isStarted = True
        if (isStarted):
            flowers[i] += 1
        tmp += flowers[i]
        if (all == tmp):
            done = True
        if (isStarted and i == len(flowers) - 1):
            ans+=1
            isStarted = False
            break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
hlist = list(map(int, input().split()))
hlist.insert(0, 0)
hlist.append(0)

water = 0
ceros = hlist.count(0)

while ceros!= len(hlist):
  Hlist = []
  cerolist = []
  for i in range(len(hlist)):
    Hlist.append(hlist[i])

  for j in range(ceros):
    p = Hlist.index(0)
    cerolist.append(p)
    Hlist[p] = 'None'

  for k in range(ceros- 1):
    p = cerolist[k]
    q = cerolist[k+ 1]
    if(q- p!= 1): 
      m = min(hlist[p+ 1: q])
      water+= m
      for l in range(p+ 1, q):
        hlist[l]-= m
      
  ceros = hlist.count(0)
print(water)

Submission status: Accepted

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

def dac(hs):
  if not hs:
    return 0
  result = min(hs)
  hs = [h-result for h in hs]
  start = 0
  for i in range(len(hs)):
    if hs[i] == 0:
      result += dac(hs[start:i])
      start = i+1
  if start < len(hs):
    result += dac(hs[start:])
  
  return result
    
print(dac(hs))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc116/tasks/abc116_c C - Grand Garden
n=int(input())
Tree_List=list(map(int,input().split()))

ct =0
for i in range(n):
    while Tree_List[i] != 0:
        ##一番最後の要素を確認しているようであればそこだけ処理すればよい
        if i == n-1:
            Tree_List[i] -= 1
        ##それ以外は、一緒に処理ができるものがないか確認する
        else:
            ##二番目以降の探索
            k = i + 1
            while Tree_List[k] != 0: 
                k+=1
                if k == n:
                    break
            Tree_List[i:k]=map(lambda x:x-1,Tree_List[i:k])
        
        ct += 1
        
print(ct)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
h = LIST()
h.insert(0, 0)
 
ans = 0
for i in range(1, max(h)+1):
	for j in range(1, len(h)):
		if h[j-1] < i and i <= h[j]:
			ans += 1
 
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
h = LIST()
ans = 0
while sum(h) > 0:
	flg = False
	for i in range(N):
		if flg and h[i] == 0:
			flg = False
			ans += 1
		elif h[i] != 0:
			flg = True
			h[i] -= 1
	if flg:
		ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
#from decimal import *

N = INT()
S = LIST()
S.insert(0, 0)

ans = 0
for i in range(1, max(S)+1):
	for j in range(1, len(S)):
		if S[j-1] < i and i <= S[j]:
			ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ok = [False for _ in range(n)]
ans = 0
while not all(ok):
    l = ok.index(False)
    for i in range(l, n):
        if ok[i] == True:
            break
        else:
            r = i
    m = min(h[l:r+1])
    ans += m
    for i in range(l, r + 1):
        h[i] -= m
        if h[i] == 0:
            ok[i] = True
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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


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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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