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 separate(array,separator):
    results = []
    a = array[:]
    i = 0
    while i<=len(a)-len(separator):
        if a[i:i+len(separator)]==separator:
            results.append(a[:i])
            a = a[i+len(separator):]
            i = 0
        else:
            i+=1
    results.append(a)
    return results


def find(ar):

    if ar==[0]*len(ar):
        return 0
    m=999999999999999
    for i in range(len(ar)):
        if ar[i]<m:
            m=ar[i]
    for i in range(len(ar)):
        ar[i]-=m
    ans=m
    new=separate(ar,[0])
    for i in new:
        ans+=find(i)
    return ans








n=int(input())
ar=[int(i) for i in input().strip().split(" ")]
print(find(ar))

Submission status: Accepted

Solution code:
input()
h = list(map(int, input().split()))
h.append(-1)
ans = 0
state = "up"
for i, j in zip(h[:-1], h[1:]):
    if state == "up" and i > j:
        ans += i
        state = "down"
    if state == "down" and i < j:
        ans -= i
        state = "up"
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
List = list(map(int,input().split()))
ans = 0
temp = 0
for item in List:
  if item > temp:
    ans += item - temp
  temp = item
print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

stdin = sys.stdin
def ns(): return stdin.readline().rstrip()
def ni(): return int(stdin.readline().rstrip())
def nm(): return map(int, stdin.readline().split())
def nl(): return list(map(int, stdin.readline().split()))


def main():
    n = ni()
    H = nl()
    ans = 0
    while not all([h == 0 for h in H]):
        if len(set(H)) == 1:
            ans += H[0]
            H = [0] * n
        else:
            h_max = max(H)
            prev = -10
            for i in range(n):
                if H[i] == h_max:
                    if prev != i - 1:
                        ans += 1
                    prev = i
                    H[i] -= 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,*h=map(int,open(0).read().split())
ans=0
left=0
while True:
    while left<n and h[left]==0:
        left+=1
    
    if left==n:
        break
    
    right=left

    while right+1<n and h[right+1]>0:
        right+=1
    
    ans+=1
    for i in range(left,right+1):
        h[i]-=1
    
print(ans)

Submission status: Accepted

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

    ans = 0
    while True:
        if sum(hs) == 0:
            break

        i = 0
        while (i < n):
            if hs[i] == 0:
                i += 1
            else:
                ans += 1
                while (i < n and hs[i] > 0):
                    hs[i] -= 1
                    i += 1
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
if n==1:
    print(h[0])
elif n==2:
    print(max(h))
else:
    h.append(0)
    h.insert(0,0)
    
    ans=0
    f=[0 for i in range(n+2)]
    
    for i in range(10010):
        a=1
        x=[]
        for j in range(n+2):
            if h[j]<=f[j]:
                x.append(j)
        m=len(x)
        if m==n+2:
            break
        a=m-1
        for j in range(m-1):
            if x[j]+1==x[j+1]:
                a-=1
        ans+=a
        for j in range(n+2):
            f[j]+=1
        
    print(ans)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    A = list(map(int,input().split()))

    ans = 0

    while True:
        
        #全列が0ならば探索終了
        if max(A) == 0:
            break
        
        i = 0
        
        #１列 横に調査
        while i < n:
        
            if A[i] == 0:
                #横にずらす
                i += 1
            else:
                #カウント++
                ans += 1

                #最後か0にぶつかるまで,列の値を１マイナスする
                while i < n and A[i] != 0:
                    #1ずつマイナスする
                    A[i] -= 1
                    #横にずらす
                    i += 1
    print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    h_lst = list(map(int,input().split()))
    zero_count = 0
    count = 0

    for i in range(n):

        if h_lst[i] == 0:
            zero_count += 1


    while zero_count < n:

        flag = False
        tmp_count = 0

        for i in range(n):

            if h_lst[i] > 0:
                flag = True
                h_lst[i] -= 1

                if h_lst[i] == 0:
                    zero_count += 1

            else:
                if flag:
                    tmp_count += 1   ## 0でもプラスする必要があるときは、それ以前に0以外の数字があるとき　→　flagで管理
                    flag = False

        if flag:
            tmp_count += 1

        count += tmp_count  ## 一回ごとに足していく

        
    print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#1段づつ考えていく
#その1段の連結成分の個数が水やりをする回数である
n=int(input())
h=list(map(int,input().split()))
#連結部分の探索
ans=0
for i in range(max(h)):
  j=0
  num=0
  while j<n:
    if h[j]==0:
      j=j+1
    else:
      num=num+1
      while j<n and h[j]>0:
        h[j]=h[j]-1
        j=j+1
  ans=ans+num
print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

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):
    if h[i+1]>h[i]:
        ans += h[i+1]-h[i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split())) + [0]
ans = 0
for h in range(1, 101):
  flag = False
  for a in A:
    if a >= h:
      flag = True
    elif flag:
      ans += 1
      flag = False
print(ans)

Submission status: Accepted

Solution code:
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
sys.setrecursionlimit(int(1e+6))
MOD = int(1e+9) + 7
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

def main():
    N = i2n()
    H = i2nn()
    op = 0
    while True:
        b = False
        hmax = 0
        for i in range(N):
            if H[i] > 0:
                if b == False:
                    b = True
                    op += 1
                H[i] -= 1
                if hmax < H[i]:
                    hmax = H[i]
            else:
                b = False
        if hmax == 0:
            break
    print(op)

main()

Submission status: Accepted

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

l = 0
r = 0
cnt = 0

while True:
    l = 0
    fin = False

    while h[l] == 0:
        l += 1
        if l == n:
            fin = True
            break
    if fin:
        break

    r = l
    minval = h[l]
    while h[r] != 0:
        if h[r] < minval:
            minval = h[r]

        r += 1
        if r == n:
            break

    h = h[:l] + [val - minval for val in h[l:r]] + h[r:]
    cnt += minval

print(cnt)

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-1] >= h[i]:
    continue
  else:
    cnt += h[i]-h[i-1]
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
h = [int(i) for i in input().split()]
end_flag = False
counter = 0
f = [0 for i in range(N)]
while end_flag == False:
    loop_flag = False
    flag = False
    for i in range(N):
        if f[i] < h[i]:
            flag = True
            loop_flag = True
            f[i] += 1
        else:
            if flag == True:
                counter += 1
                flag = False
    if flag == True:
        counter += 1
    if loop_flag == False:
        end_flag = True
print(counter)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

for c in range(100):
    f = False
    cnt = 0
    if H.count(0) == N: break
    for i in range(N):
        if H[i] > 0:
            H[i] -= 1
            f = True
        else:
            if f == True:
                cnt += 1
                f = False

        if i == N-1 and f == True:
            cnt += 1

    ret += cnt

print(ret)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N=ii()
    H=list(mi())

    for h in H:
        if h != 0:break
    else:
        print(0)
        exit()
    
    ans = 0
    while True:
        i = 0
        while i < N and H[i] == 0:
            i += 1
            
        while i < N:
            # print(i)
            for j in range(i,N):
                if H[j] == 0: break
                H[j] -= 1
            while j < N and H[j] == 0:
                j += 1
            # print(H)
            if j == N-1 and H[j] != 0:
                i = N
            else:
                i = j
            ans += 1
        
        count = 0
        for j in range(N):
            count += H[j] == 0
        
        if count == N:
            break
        


    print(ans)
    



        





if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def garden(a,h):
    if len(a)==0:
        return 0
    m=min(a)
    i=a.index(m)
    return m-h+garden(a[:i],m)+garden(a[i+1:],m)
n=input()
print(garden(list(map(int,input().split())),0))

Submission status: Accepted

Solution code:
n=input()
def garden(a,h):
    if len(a)==0:
        return 0
    if len(a)==1:
        return a[0]-h
    m=min(a)
    i=a.index(m)
    return m-h+garden(a[:i],m)+garden(a[i+1:],m)
print(garden(list(map(int,input().split())),0))

Submission status: Accepted

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

mx = max(h)

h_list = [[0 for _ in range(mx)] for _ in range(N)]

for i in range(N):
    for j in range(h[i]):
        h_list[i][j] = 1

# print(h_list)
res = 0

for h in range(mx):
    i = 0
    while i < N:
        if h_list[i][h] == 0:
            i += 1
        else:
            res += 1
            while(i < N and h_list[i][h] == 1):
                i += 1
print(res)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

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

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

Solution code:
##a = int(stdin.readline().rstrip())
##b, c = [int(x) for x in stdin.readline().rstrip().split()]
##s = stdin.readline().rstrip()
##a = list(set(b))  重複を削除
##a = list(map(int,[int(x) for x in stdin.readline().rstrip().split()])) a[0, 1, 2, ...]
##a = [[0] * 2 for _ in range(n)]  a[0,0]
##v = [s[i: i+2] for i in range(0, len(s), 2)]
##  s="12345678" を v=["12","34","56","78"] にするイメージ
##s = "".join(list)リストを文字列に変換[""]は区切り文字
##a.sort(key=itemgetter(1))リストをソートできるよ(from operator import itemgetterが必要)
##sort_p = sorted(p) 昇順ソート

# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
import numpy as np
import random
import bisect
import copy
# stdin = open("sample.txt")


MOD = int(1e9+7)
INF = float('inf')
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

def keta(kazu): # 入力されたintを桁ごとに分解，リストで出力
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers): # 最小公倍数
    return reduce(math.gcd, numbers)

def combination(m,n): # mCn
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  # (x**n)の計算の高速化
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x

def fact(n): # nの素因数分解の結果を辞書で出力 fuct(24) → {2: 3, 3: 1}
    arr = {}
    temp = n
    for i in range(2,int(n**0.5)+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt
    if temp != 1:
        arr[temp] = 1
    if arr == {}:
        arr[n] = 1
    return arr
    
def main():
    N = int(stdin.readline().rstrip())
    H = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
    ans = 0
    now = 0
    ok = 0
    while H.count(0) != N:
        while True:
            if H[ok] == 0:
                ok += 1
                break
            else:
                now = ok
                while True:
                    if now >= N or H[now] == 0:
                        ans += 1
                        break
                    else:
                        H[now] -= 1
                        now += 1

    print(ans)
                
main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
ans = 0
while(any(a)):
    f = True
    x = a[0]
    for i,ai in enumerate(a):
        if ai:
            a[i] -= 1
        if ai < 1:
            if x != 0:
                break
        x = ai
    x = a[0]
    ans += 1
    #print(a)
print(ans)

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):
    if H[i]<H[i+1]:
      ans += H[i+1]-H[i]
  return ans
print(solve())

Submission status: Accepted

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

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


print(cnt)

Submission status: Accepted

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

H.append(0)
cnt = 0
for i in range(N + 1):
    cnt += max(H[i] - H[i - 1], 0)


print(cnt)

Submission status: Accepted

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

ans = 0
zero = 0

for h in H:
    if h == 0:
        zero += 1

while zero < N:
    flg = False
    cnt = 0

    for i in range(N):
        if H[i] > 0:
            flg = True
            H[i] -= 1

            if H[i] == 0:
                zero += 1
        else:
            if flg:
                cnt += 1
                flg = False

    if flg:
        cnt  += 1

    ans += cnt


print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans=0
for i in range(k):
  j=0
  while j<n:
    #連結成分の数を数える
    if h[j]==0:
      j=j+1
    else:
      while j<n and h[j]!=0:
        h[j]=max(h[j]-1,0)
        j=j+1
      ans=ans+1
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

cnt=0
l=deque([h])
bot=deque([0])
while len(l)>0:
    tmp=l.popleft()
    b=bot.popleft()
    cnt+=min(tmp)-b
    g=deque([n for n in range(len(tmp)) if tmp[n]==min(tmp)])
    g.appendleft(-1)
    g.append(N)
    
    for i in range(1,len(g)):
        if len(tmp[g[i-1]+1:g[i]])>0:
            l.append(tmp[g[i-1]+1:g[i]])
            bot.append(min(tmp))

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
while h.count(0) != N:
    beforeWater = False
    for i in range(N):
        if h[i] > 0:
            if not beforeWater:
                count += 1
            h[i] = h[i] - 1
            beforeWater = True
        else:
            beforeWater = False

print(count)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
cnt = 0
for i in range(max(h)):
    bol = 0
    for j in h:
        if j > i:
            bol = 1
        else:
            cnt += 1 if bol == 1 else 0
            bol = 0
    if bol == 0:
        cnt -= 1
print(max(h)+cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FileName: 	C_fix_2
# CreatedDate:  2020-07-30 18:19:19 +0900
# LastModified: 2020-07-30 18:49:07 +0900
#


import os
import sys
# import numpy as np
# import pandas as pd
from itertools import groupby


def main():
    n = int(input())
    H = list(map(int, input().split()))
    A = []
    for i in range(max(H)+1):
        A.append([1 if h > i else 0 for h in H])

    cnt = 0
    for a in A:
        ans = groupby(a)
        for k, g in ans:
            if 1 in list(g):
                cnt += 1
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

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

ans = 0
now = 0

for h in H:
    if h <= now:
        now = h
    else:
        ans += h - now
        now = h

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

# from numba import njit

# from collections import Counter
# from itertools import accumulate
# import numpy as np
# from heapq import heappop,heappush
# from bisect import bisect_left

# @njit
def solve(n,a):
  h_max = max(a)
  res = 0

  for h in range(h_max):
    flag = 0 #一つ前に水をやる必要があるかどうか

    for i in range(n):
      if a[i] > h and not flag:
        res += 1
        flag = 1
      elif a[i] <= h:
        flag = 0

  return res



def main():
  N = int(input())
  # N,M = map(int,input().split())
  a = list(map(int,input().split()))
  print(solve(N,a))
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

def mizuyari(l):
    if l == 0 or l == []:
        return 0
    nin = min(l)
    for i in range(len(l)):
        l[i] -= nin
    index = l.index(0)
    return nin + mizuyari(l[:index]) + mizuyari(l[index+1:])


print(mizuyari(h))

Submission status: Accepted

Solution code:
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

if __name__ == "__main__":
    n = I()
    a = Intl()
    cnt = 0
    def dfs(l):#区間i,jについて最小水やり回数を求める
        global cnt
        if l == [] or l == [0]:return
        elif 0 in l:#0の前と後ろで分割して探索
            i = l.index(0)
            dfs(l[:i])
            dfs(l[i+1:])
        else:
            l = [k-1 for k in l]
            cnt += 1
            dfs(l)
    dfs(a)
    print(cnt)

Submission status: Accepted

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

ans = 0
active = 0

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

Submission status: Accepted

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

    tmp = 0
    ans = 0
    goukei = sum(inlis)
    while goukei > 0:
        for i in range(n):
            if inlis[i] > 0:
                #print(inlis)
                inlis[i] -= 1
                tmp += 1
                ans += 1
            else:
                if tmp > 0:
                    ans -= tmp - 1
                    tmp = 0
        if tmp > 0:
            ans -= tmp - 1
            tmp = 0
        goukei = sum(inlis)
        #print(ans, inlis, tmp)
        

    print(ans)

            



if __name__ == "__main__":
    main()

Submission status: Accepted

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

l = -1
r = -1
ans = 0
while sum(h) > 0:
    for i in range(n):
        if l == -1 and h[i] > 0:
            l = i
            
        if l != -1 and h[i] == 0:
            r = i-1
        elif  l != -1 and i == n-1:
            r = i
        
        if l != -1 and r != -1:
            ans += 1
            for j in range(l, r+1):
                h[j] -= 1
            else:
                l = -1
                r = -1

print(ans)

Submission status: Accepted

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

cnt=0
flg=1
#flg=0  #0開始  1終わり
while max(h)>0:
    # print('h:',h)
    for i in range(N+1):
        if h[i] >=1:
            flg=0
        elif flg==0 and h[i]==0:
            flg=1
            cnt+=1
    for j in range(N):
        h[j]-=1
        if h[j]<0:
            h[j]=0

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

def mizuyari(h):
    if len(h)==0:
        return 0
    elif len(h)==1:
        return h[0]

    m = min(h)
    l = 0
    r = 0
    sm = 0

    while r < len(h):
        if h[r]==m:
            sm += mizuyari(list(map(lambda x: x-m, h[l:r])))
            l = r+1
        r += 1
    sm += mizuyari(list(map(lambda x: x-m, h[l:r])))

    return m + sm

print(mizuyari(h))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
def water(l, r, sub):
    if l == r:
        return 0
    i = l + h[l:r].index(min(h[l:r]))
    a = water(l, i, h[i]) 
    b = water(i + 1, r, h[i])
    return h[i] - sub + a + b
print(water(0, n, 0))

Submission status: Accepted

Solution code:
import numpy as np

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

def count(b):
    b = np.clip(b, 0, 1)
    return int(b[0] > 0) + np.sum(np.diff(b) > 0)

heights = np.array(H)

cnts = []
for water in range(100):
    arr = np.clip(heights - water, 0, 101)
    cnts.append(count(arr))

print(np.sum(cnts))

Submission status: Accepted

Solution code:
# abc116_c.py
# https://atcoder.jp/contests/abc116/tasks/abc116_c

# C - Grand Garden /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# 花壇に N本の花が咲いており、それぞれ 1,2,......,N と番号が振られています。
# 最初、全ての花の高さは 0 です。 数列 h={h1,h2,h3,......} が入力として与えられます。
# 以下の「水やり」操作を繰り返すことで、すべての k(1≦k≦N) に対して花 k の高さを hkにしたいです。
#     整数 l,rを指定する。l≦x≦r を満たすすべての x に対して、花 x の高さを 1高くする。
# 条件を満たすための最小の「水やり」操作の回数を求めてください。

# 制約
#     1≦N≦100
#     0≦hi≦100
#     入力はすべて整数である。

# 入力
# 入力は以下の形式で標準入力から与えられます。
# N
# h1 h2 h3 ...... hN

# 出力
# 条件を満たすような最小の「水やり」操作の回数を出力してください。

# 入力例 1
# 4
# 1 2 2 1

# 出力例 1
# 2

# 「水やり」操作の回数は 2回が最小です。 以下が一つの例です。
#     (l,r)=(1,3)の「水やり」操作を行う。
#     (l,r)=(2,4)の「水やり」操作を行う。

# 入力例 2
# 5
# 3 1 2 3 1

# 出力例 2
# 5

# 入力例 3
# 8
# 4 23 75 0 23 96 50 100

# 出力例 3
# 221


def calculation(lines):
    # X = lines[0]
    N = int(lines[0])
    values = list(map(int, lines[1].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i+1]))

    result = get_cnt(values)

    return [result]


def get_cnt(values):
    # print('----------------------------------------------')
    # print(f'values=[{values}]')
    ret = 0
    mi = min(values)
    # print(f'mi=[{mi}]')
    ret += mi
    values = [value - mi for value in values]
    # print(f'values=[{values}]')
    lst = None
    for value in values:
        # print(f'    value=[{value}], lst=[{lst}]')
        if value != 0:
            if lst is None:
                lst = [value]
            else:
                lst.append(value)
        else:
            if lst is None:
                pass
            else:
                ret += get_cnt(lst)
                lst = None
    if lst is not None:
        ret += get_cnt(lst)
    return ret


def func_f(n):
    if n % 2 == 0:
        return int(n/2)
    else:
        return 3*n + 1


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['4', '1 2 2 1']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['5', '3 1 2 3 1']
        lines_export = [5]
    if pattern == 3:
        lines_input = ['8', '4 23 75 0 23 96 50 100']
        lines_export = [221]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    if len(args) == 1:
        mode = 0
    else:
        mode = int(args[1])
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(2)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    # if mode > 0:
    #     print(f'lines_input=[{lines_input}]')
    #     print(f'lines_export=[{lines_export}]')
    #     print(f'lines_result=[{lines_result}]')
    #     if lines_result == lines_export:
    #         print('OK')
    #     else:
    #         print('NG')
    # finished = time.time()
    # duration = finished - started
    # print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect

sys.setrecursionlimit(10**6)


def zz():
    return list(map(int, sys.stdin.readline().split()))


def z():
    return int(sys.stdin.readline())


def S():
    return sys.stdin.readline()


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


N = z()
H = zz()
H = np.array(H)
ans = 0
while (sum(H) != 0):
    s = 0
    while (s < N):
        if(H[s] != 0):
            break
        s += 1
    e = s
    while (e < N):
        if (H[e] == 0):
            break
        e += 1
    # print(H, s, ? e, '-', min(H[s:e]))
    H[s:e] -= 1
    ans += 1
print(ans)

Submission status: Accepted

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