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

    ans = 0
    while(1):
        if num_list.count(0) == n:
            break
        i = 0
        while(i < n):
            if num_list[i] == 0:
                i += 1
            else:
                ans += 1
                while(i < n and num_list[i] != 0):
                    num_list[i] -= 1
                    i += 1

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,*h=map(int,open(0).read().split())
ans=h[0]
for i in range(n-1):
    ans+=max(h[i+1]-h[i],0)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
h = [int(i) for i in input().split()]

def func(h):
    if len(h) == 0:
        return 0
    res = min(h)
    h = [i - min(h) for i in h]
    next_h = []
    flag = False
    tmp = 0
    for i in range(len(h)):
        if h[i] != 0 and not flag:
            flag = True
            tmp = i
        if h[i] == 0 and flag:
            next_h.append(h[tmp:i])
            flag = False
    if flag:
        next_h.append(h[tmp:len(h)])
    #print(h)
    #print(next_h)
    for i in range(len(next_h)):
        res += func(next_h[i])
    return res

print(func(h))

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

def func(arr):
    if len(arr) == 0:
        return 0
    tmp = min(arr)
    arr = [i - tmp for i in arr]
    n_arr = []
    strt = 0
    flag = False
    for i in range(len(arr)):
        if arr[i] == 0 and flag:
            n_arr.append(arr[strt:i])
            flag = False
        if arr[i] != 0 and not flag:
            flag = True
            strt = i
    if flag:
        n_arr.append(arr[strt:])
    s = tmp
    for i in n_arr:
        s += func(i)
    return s

n = INT()
h = LIST()
ans = func(h)
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

def func(arr):
    if len(arr) == 0:
        return 0
    tmp = min(arr)
    for i in range(len(arr)):
        arr[i] -= tmp
    n_arr = []
    strt = 0
    flag = True if arr[0] != 0 else False
    for i in range(len(arr)):
        if arr[i] == 0 and flag:
            n_arr.append(arr[strt:i])
            flag = False
        if arr[i] != 0 and not flag:
            flag = True
            strt = i
    if flag:
        n_arr.append(arr[strt:])
    s = tmp
    for i in n_arr:
        s += func(i)
    return s

n = INT()
h = LIST()
ans = func(h)
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

def func(arr):
    if len(arr) == 0:
        return 0
    if len(arr) == 1:
        return arr[0]
    tmp = min(arr)
    for i in range(len(arr)):
        arr[i] -= tmp
    n_arr = []
    strt = 0
    flag = True if arr[0] != 0 else False
    for i in range(len(arr)):
        if arr[i] == 0 and flag:
            n_arr.append(arr[strt:i])
            flag = False
        if arr[i] != 0 and not flag:
            flag = True
            strt = i
    if flag:
        n_arr.append(arr[strt:])
    s = tmp
    for i in n_arr:
        s += func(i)
    return s

n = INT()
h = LIST()
ans = func(h)
print(ans)

Submission status: Accepted

Solution code:
n,*h=map(int,open(0).read().split())
ans=h[0]
for i in range(n-1):
  ans+=max(h[i+1]-h[i],0)
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    def countis(inputlist):
        ret = 0
        lenlist = len(inputlist)
        for i in range (lenlist-1):
            if inputlist[i] != 0 and inputlist[i+1] == 0:
               ret += 1
        return ret
    n = int(input())
    h = list(map(int, input().split()))
    h.append(0)
    ans = 0
    while True:
        if sum(h) == 0:
            print(ans)
            exit()
        else:
            ans += countis(h)
            h = list(map(lambda x: x-1, h))
            for j in range(len(h)):
                if h[j] <0 :
                    h[j] =0
resolve()

Submission status: Accepted

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

Submission status: Accepted

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

def counter(l):
  count = 1
  nc = 'True'
  for i in l:
    if i > 0:
      nc = 'False'
    elif i <= 0 and nc == 'False':
      count += 1
      nc = 'True'
  if l[-1] <= 0:
    count -=1
  return count

ans = 0
while any([i>0 for i in l]):
  ans +=  counter(l)
  l = l - np.ones(n)
print(ans)

Submission status: Accepted

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

cnt = 0
while True:
    allzero = True
    for i in range(N):
        if(h[i] != 0):
            allzero = False
    if allzero: break

    l = 0
    while h[l] == 0:l += 1
    r = l
    while r <N-1 and h[r+1]!=0:
        r +=1
    cnt += 1
    for i in range(l,r + 1,1):
        h[i] -=1
print(cnt)

Submission status: Accepted

Solution code:
import numpy as np

def search_min(h):
    global ans
    my_min = min(h)
    ans += my_min
    if len(h) == 1:
        return 0
    min_index = [i for i, k in enumerate(h) if k == my_min]
    h -= my_min
    if len(min_index) > 1:
        for i in range(len(min_index) - 1):
            h_new = h[min_index[i] + 1: min_index[i + 1]]
            if len(h_new) > 0:
                search_min(h_new)
    h_new = h[: min_index[0]]
    if len(h_new) > 0:
        search_min(h_new)
    h_new = h[min_index[-1] + 1:]
    if len(h_new) > 0:
        search_min(h_new)
        
    return 0
    
if __name__ == "__main__":
    N = int(input())
    H = list(map(int, input().split()))
    H_new = [H[i] for i in range(1, len(H)) if H[i] != H[i - 1]]
    H_new.insert(0, H[0])
    H_new = np.array(H_new)
    ans = 0
    search_min(H_new)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1,101):
    c = 0
    for j in range(1,n):
        if heights[j-1] >= i and heights[j] < i:
            c += 1
        else:
            continue
    if heights[-1] >= i:
        c += 1
    ans += c
    
print(ans)

Submission status: Accepted

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

ans = 0

for i in range(1,101):
    c = 0
    for j in range(1,n):
        if heights[j-1] >= i and heights[j] < i:
            c += 1
        
        else:
            continue
    
    if heights[-1] >= i:
        c += 1
    
    ans += c
    

print(ans)

Submission status: Accepted

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

for i in range(n-1):
    if list[i]<=list[i+1]:
        continue
    else:
        count += list[i] - list[i+1]

count += list[n-1]
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    h=list(map(int,readline().split()))

    count=0
    while 1:
        first=True
        for i in range(N):
            if first and h[i]>=1:
                first=False
                h[i]-=1
            elif first and h[i]==0:
                pass
            elif first is False and h[i]>=1:
                h[i]-=1
            elif first is False and h[i]==0:
                break
        if first:
            break
        else:
            count+=1
    print(count)

if __name__=="__main__":
    main()

Submission status: Accepted

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

hll = []
cnt = 0

zero_check = []
tmp = []
for h in hl:
    if h != 0:
        tmp.append(h)
    elif h == 0 and len(tmp) > 0:
        zero_check.append(tmp)
        tmp = []

if len(tmp) > 0:
    zero_check.append(tmp)

hl = zero_check[:]
# print(hl)

while len(hl) > 0:
    for h in hl:
        cnt += 1
        tmp_num = []
        for num in h:
            if num - 1 != 0:
                tmp_num.append(num - 1)
            elif num - 1 == 0 and len(tmp_num) > 0:
                hll.append(tmp_num)
                tmp_num = []
                # cnt += 1
            # print('num:{}, tmp_num:{}, hll:{}, cnt:{}'.format(num, tmp_num, hll, cnt))

        if len(tmp_num) > 0:
            hll.append(tmp_num)

    hl = hll
    hll = []
    # print('hl:{}'.format(hl))

print(cnt)

Submission status: Accepted

Solution code:
from collections import deque
import sys

sys.setrecursionlimit(10 ** 5 + 10)


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

    cluster = deque([H])
    cnt = 0
    while cluster:
        i = cluster.popleft()
        if len(i) != 0:
            mini = min(i)
            cnt += mini
            for j in range(len(i)):
                i[j] -= mini

            j = 0

            for k in range(len(i)):
                if i[k] == 0:
                    cluster.append(i[j:k])

                    j = k + 1
                elif k == len(i) - 1:
                    cluster.append(i[j:len(i)])

    print(cnt)


resolve()

Submission status: Accepted

Solution code:
# import bisect
from collections import Counter, deque
# import copy
# from fractions import gcd
# from functools import reduce
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline

def resolve():

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

    cluster=deque([H])
    cnt=0
    while cluster:
        i=cluster.popleft()
        if len(i)!=0:
            mini=min(i)
            cnt += mini
            for j in range(len(i)):
                i[j]-=mini

            j = 0

            for k in range(len(i)):
                if i[k]==0:
                    cluster.append(i[j:k])

                    j=k+1
                elif k==len(i)-1:
                    cluster.append(i[j:len(i)])

    print(cnt)
resolve()

Submission status: Accepted

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

while sum(h) > 0:
    m = max(h)
    if h[0] == m:
        c += 1
    for i in range(1,N):
        if h[i] == m:
            if h[i] != h[i-1]:
                c += 1
    for j in range(N):
            if h[j] == m:
                h[j] -= 1

print(c)

Submission status: Accepted

Solution code:
n = int(input())
list_H = list(map(int, input().split()))
max_value = max(list_H)
list_K = [[0]*n for _ in range(max_value)]
for i in range(max_value):
    l = list_K[i]
    for j in range(n):
        if list_H[j] > i:
            l[j] = 1

ans = []
for i in range(len(list_K)):
    cnt = 0
    l = list_K[i]
    l.append(0)
    for j in range(n):
        if l[j] == 1 and l[j+1] == 0:
            cnt += 1
    ans.append(cnt)

print(sum(ans))

Submission status: Accepted

Solution code:
N=int(input())
h=list(map(int,input().split()))
cnt=0
L=0
R=0
time=0

while L<=N-1:
  time +=1
  R=-1
  for i in range(L,N):
    if h[i]>0:
      R=i
      h[i] -= 1
    else:
      break
  #print(time,L,R,h)
  if R>=L:
    cnt +=1
  else:
    L +=1
    
print(cnt)

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  n = int(input())
  h = input_list()
  ans = 0
  while True:
    ans += count_section_by_zero(h)
    h = list(map(lambda x: 0 if x == 0 else x-1, h))
    if all([v == 0 for v in h]):
      break
  print(ans)

def count_section_by_zero(data):
  count = 0
  flg = False
  start = 0
  for i, d in enumerate(data):
    if flg is False and d != 0:
      count += 1
      flg = True
      
    if d == 0:
      flg = False
  return count
  
def input_list():
  return list(map(int, input().split()))

def input_list_str():
  return 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)

# 2で割り切れる回数
def divide_two(arg):
  c = 0
  while True:
    if c >= 2:
      break
    if arg % 2 != 0:
      break
    arg //= 2
    c += 1
  return c

# 素因数分解
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

import math
import fractions
import collections
from functools import reduce
main()

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
count = 0
i = 0
while sum(h) > 0:
    if h[i] > 0:
        j = i
        count += 1
        while j < N and h[j] > 0:
            h[j] = h[j] - 1
            j += 1
    else:
        i += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy
N = int(input())
H = [i for i in map(int,input().split())]
res = 0
while True:
  i = 0
  if max(H) == 0:
    break
  while (i<N):
    if H[i] == 0:
      i += 1
    else:
      res += 1
      while (i < N and H[i] > 0):
        H[i] -= 1
        i += 1
print(res)

Submission status: Accepted

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

def list_split(list_in):
    list_out=[];
    ind=0
    for i in range(len(list_in)):
        if (list_in[i]==0):# & sum(list_in[ind:i])>0:
            if sum(list_in[ind:i])>0:
                list_out.append(list_in[ind:i])
            ind=i+1

        if i==(len(list_in)-1):
            if sum(list_in[ind:i+1])>0:
                list_out.append(list_in[ind:i+1])
    return(list_out)


import numpy as np

def count_min(list_in):
    count = 0
    out_list=[]
    for i in list_in:
        tmp_count=min(i)
        tmp=list(np.array(i)-min(i))
        tmp=list_split(tmp)
        if tmp!=[[]]:
            out_list.extend(tmp)
        count=count+tmp_count
    return(count,out_list)

check=0
total_count=0
new_list=list_split(a)
while check==0:
    tmp_count,new_list=count_min(new_list)
    total_count=total_count+tmp_count
    if len(new_list)==0:
        break
    
    
print(total_count)

Submission status: Accepted

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

count=1
judge=False
answer=0

while count<=max(h):
    judge=False
    for i in h:
        if i>=count:
            judge=True
        elif i<count and judge==True:
            answer+=1
            judge=False
    if judge==True:
        answer+=1
    count+=1

print(answer)

Submission status: Accepted

Solution code:
def dfs(L):
    if len(L) == 0:
        return 0

    ret = min(L)
    L = [e - ret for e in L]
    L2 = []
    for e in L:
        if e == 0:
            ret += dfs(L2)
            L2 = []
        else:
            L2.append(e)

    ret += dfs(L2)
    return ret

def main():
    n = int(input())
    H = [int(e) for e in input().split()]

    print(dfs(H))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def dfs(L):
    if len(L) == 0:
        return 0

    ret = min(L)
    L = [e - ret for e in L]
    L2 = []
    for i, e in enumerate(L):
        if e == 0:
            ret += dfs(L2)
            L2 = []
        else:
            L2.append(e)

    ret += dfs(L2)
    return ret

def main():
    n = int(input())
    H = [int(e) for e in input().split()]

    print(dfs(H))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

h.append(0)
count = 0
for i in reversed(range(103)):
	for j in range(len(h)-1):
		if i <= h[j] and h[j+1] < i:
			count += 1


print(count)

Submission status: Accepted

Solution code:
n = int(input())

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

cnt = 0
ans = 0
low = 0
for i in range(n):
    if cnt < h[i]:
        cnt = h[i]
    elif cnt > h[i]:
        ans += cnt - low
        low = h[i]
        cnt = h[i]
    

ans += cnt - low
print(ans)

Submission status: Accepted

Solution code:


n=int(input())
h=[int(x) for x in input().split()]
h.append(0)
ans=0
for k in reversed(range(1,105)):
    for i in range(n):
        if(h[i]>=k and h[i+1]<k):
            ans+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
cnt = 0
while sum(h) != 0:
    #print(ans, h)
    #if cnt == 10: break
    cnt += 1
    beg = 0  # 始点
    i = 0
    mn = 200
    while i < n:
        if h[i] == 0:
            if mn != 200:
                ans += mn
                for j in range(beg, i):
                    h[j] -= mn
                mn = 200
            beg = i + 1  # 始点の書き換え
        else: mn = min(mn, h[i])
        i += 1
    if mn != 200:
        ans += mn
        for j in range(beg, n):
            h[j] -= mn
print(ans)

Submission status: Accepted

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

count = 0

while any(h):
  lists = [i for i, x in enumerate(h) if x > 0]
  i  = lists[0]
  start = i
  end = i
  while end + 1 < N and h[end + 1] > 0:
    end += 1
  for index in range(start, end + 1):
    h[index] -= 1
  count += 1

print(count)

Submission status: Accepted

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

ans = 0
pos = 0

for i in h:
    ans += max(i - pos, 0)
    pos = i

print(ans)

Submission status: Accepted

Solution code:
count = 0
N = int(input())
h = list(map(int, input().split()))
count =+ min(h)
h = list(map(lambda x:  x-min(h) ,h))
# if max(h) == 0:
#     print(count)
h.append(0)
while max(h) != 0:
    for i in range(len(h)):
        if h[i] <= 0:
            continue
        elif h[i-1] > 0:
            continue
        else:
            for j in range(i+1,len(h)+1):
                if h[j] > 0:
                    continue
                else:
                    count += 1
                    break
    h = list(map(lambda x:  x-1 ,h))
print(count)

Submission status: Accepted

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

ans = 0
pos = 0

for i in h:
    ans += max(i - pos, 0)
    pos = i

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


def main():
    _ = int(input())
    H = list(map(int, input().split()))

    result = postion = 0
    for h in H:
        result += max(h - postion, 0)
        postion = h
    print(result)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

cnt = 0
K = [0] * N
while True:
    if H == K:
        break

    for l in range(N):
        if H[l] > K[l]:
            cnt += 1
            for r in range(l,N):
                if H[r] == K[r]:
                    break
                K[r] += 1
            break

print(cnt)

Submission status: Accepted

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

ans = 0
sum_height = sum(h)
while sum_height > 0:
    for i in range(N):
        if h[i] == 0:
            pos = False
            continue
        elif (i == 0 and h[i] > 0) or (i > 0 and not pos):
            ans += 1
        h[i] -= 1
        sum_height -= 1
        pos = True

print(ans)

Submission status: Accepted

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

ans = 0
sum_height = sum(h)
while sum_height > 0:
    i = 0
    while i < N:
        while i < N and h[i] == 0:
            i += 1
        if i < N:
            ans += 1
        while i < N and h[i] > 0:
            h[i] -= 1
            sum_height -= 1
            i += 1

print(ans)

Submission status: Accepted

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

cnt = 0
while l.count(0) < n:
    for i in range(n):
        if (i > 0 and l[i-1] == 0 and l[i] == 0) or (i == 0 and l[i] == 0):
            continue
        if l[i] == 0 or i == n-1:
            cnt += 1
    l = [k-1 if k > 0 else k for k in l]

print(cnt)

Submission status: Accepted

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

ret = 0
while True:
    left = -1
    for i, h in enumerate(H):
        if h:
            left = i
            break

    if left == -1:
        break

    right = N
    for i in range(left, N):
        if not H[i]:
            right = i
            break

    v = min(H[left: right])
    ret += v
    for i in range(left, right):
        H[i] -= v


print(ret)

Submission status: Accepted

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

# nowはまだ0になっていない花のうち左端の添え字
# nowが0からはじまりn-1まで
# まだ残りの高さが0になっていない場合は、その花に水をやり、同時に可能なだけ右の花にも水をやる
now = 0
ans = 0
while now < N:
    if h[now] > 0:
        ans += 1
        i = now
        while i < N and h[i] > 0:
            h[i] -= 1
            i += 1
    else:
        now += 1
print(ans)

Submission status: Accepted

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

# ローカルピークの場所をカウントして、その和が回答だと思う。

count = 0

while True:
    if max(h) == 0: break
    i = 0
    while i < N:
        if h[i]==0:
            i += 1
        else:
            count += 1
            while i < N and h[i] > 0:
                h[i] -= 1
                i += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

num_ma = h[0]
ans = 0
flag = True


for i in range(n):
    if h[i] >= num_ma:
        num_ma = h[i]
        flag = True
    else:
        ans += num_ma-h[i]
        num_ma = h[i]
        flag = False

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

Submission status: Accepted

Solution code:
n = int(input())
hs = [int(i) for i in input().split()]
flag = -1
count = 0

while max(hs) > 0:
    flag = -1
    for i in range(n):
        if hs[i] > 0:
            if flag == 1:
                hs[i] -= 1
            else:
                count += 1
                flag = 1
                hs[i] -= 1
        else:
            if flag == 1: 
                flag = -1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#116C
n = int(input())
h = list(map(int,input().split()))
s = set([])

for i in range(n):
    s.add(h[i])
s = list(s)
s.append(0)
s.sort()

m = len(s)
    
flag = [0] * (n+1)
flag[0] = 1
ans = 0

for i in range(m-1):
    a = 0
    b = s[i+1]-s[i]
    
    for j in range(n):
        if flag[j+1] == 0 and flag[j] == 1:
            a += 1
    
    for j in range(n):
        if h[j] == s[i+1]:
            flag[j+1] = 1
    ans += (a * b)
    
print(ans)

Submission status: Accepted

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

while sum(h):
    while True:
        if h[i] == 0 and i < n:
            i += 1
        else:
            break

    while True:
        if h[i] != 0:
            h[i] -= 1
            i += 1
        if i >= n or h[i] == 0:
            i = 0
            ans += 1
            break

print(ans)

Submission status: Accepted

Solution code:
n, *H = map(int, open(0).read().split())

inc = True
peaks = []
for h0, h1 in zip(H, H[1:]):
    if inc:
        if h0 > h1:
            peaks.append(h0)
            inc = False
    else:
        if h0 < h1:
            peaks.append(-h0)
            inc = True
if inc:
    peaks.append(H[-1])
print(sum(peaks))

Submission status: Accepted

Solution code:
n, *H = map(int, open(0).read().split())

inc = True
maxes, mins = [], []
for h0, h1 in zip(H, H[1:]):
    if inc:
        if h0 > h1:
            maxes.append(h0)
            inc = False
    else:
        if h0 < h1:
            mins.append(h0)
            inc = True
if inc:
    maxes.append(H[-1])
print(sum(maxes)-sum(mins))

Submission status: Accepted

Solution code:
import numpy as np
N = input()
h = input().split()
h = np.array(h).astype(int)
arr = np.zeros(len(h))

k = 0
def water(arr,h):
    # waterをかける場所がない
    if len(arr) == 0:
        return
    # waterをかける
    #print("water")
    global k
    k = k + 1
    arr = arr + 1
    # 水が足りている花があるかチェック
    # 目標の高さ == 現在の高さ　のインデックスを見つける
    ind = []
    i = 0
    for ea,eh in zip(arr,h):
        if ea == eh:
            ind.append(i)
        i = i + 1
    #print(h,"ind : ",ind)
    # インデックスを基準に分ける
    # それぞれについてwaterをかける
    index = 0
    for target in ind:
        water(arr[index:target],h[index:target])
        index = target+1
    water(arr[index:],h[index:])
# 水が足りている花があるかチェック
# 目標の高さ == 現在の高さ　のインデックスを見つける
ind = []
i = 0
for ea,eh in zip(arr,h):
    if ea == eh:
        ind.append(i)
    i = i + 1
#print(h,"ind : ",ind)
# インデックスを基準に分ける
# それぞれについてwaterをかける
index = 0
for target in ind:
    water(arr[index:target],h[index:target])
    index = target+1
water(arr[index:],h[index:])
print(k)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
arr=[x for x in h]
cnt=0

while 1:
  if all([x==0 for x in h]):
    break
  for i in range(n):
    if h[i]!=0:
      s=i
      break
  e=s
  for i in range(s,n-1):
    if h[s]<=h[i+1]:
      e+=1
    else:
      break
  h=[h[i]-1 if s<=i<=e else h[i] for i in range(n)]
  cnt+=1
print(cnt)

Submission status: Accepted

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

ans = 0

def solve(arr):
  global ans
  min_h = min(arr)
  ans += min_h
  new_arr = [x - min_h for x in arr]
  sub_arr = []
  for x in new_arr:
    if x == 0:
      if len(sub_arr) > 0:
        solve(sub_arr)
        sub_arr = []
    else:
      sub_arr.append(x)
  else:
    if len(sub_arr) > 0:
      solve(sub_arr)

solve(H)
print(ans)

Submission status: Accepted

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

# ダルマ落としみたいにやる
cnt = 0
while True:
    if all(h==0):
        break

    i = 0
    while i < N:
        if h[i] == 0:
            i += 1
        else:
            cnt += 1
            while i < N and h[i] > 0:
                # 落とし
                h[i] -= 1
                i += 1
print(cnt)