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())
h=list(map(int,input().split()))
H=max(h)
ans=0
for i in range(1,H+1):
  if h[0]>=i:
    piece=1
  else:
    piece=0
  for j in range(n-1):
    if h[j]<i and h[j+1]>=i:
      piece+=1
  ans+=piece
print(ans)

Submission status: Accepted

Solution code:
import sys
L = int(sys.stdin.readline().rstrip())
 
h = list(map(int, sys.stdin.readline().rstrip().split()))
 
if L == 0:
  ans = 0
elif L == 1:
  ans = h[0]
elif L == 2:
  ans = max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]
 
  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
  ans = p - v
print(ans)

Submission status: Accepted

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

while True:
    flag=0
    m=200 #最小値
    for h in H:
        if h==0:
            continue
        else:
            m=min(h,m)
    if m==200:
        break
    else:
        for i in range(n):
            if H[i]==0:
                flag=0
                continue
            else:
                if flag==0:
                    ans+=m
                flag=1
                H[i]-=m
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

ans = 0

while max(H):
    max_index = np.where(H == max(H))[0]
    ren_position = max_index - np.arange(len(max_index))
    ans += len(set(ren_position))

    H = np.where(H == max(H), max(H) - 1, H)

print(ans)

Submission status: Accepted

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

    return N,h

def mizuyari(N,H):
    cnt =0
    flg = False
    h = H

    while flg == False:
    ##開始点を見つける
        start = 0
        for n in range(N):
            if h[n] != 0:
                start = n
                break
            elif n == N-1 and h[n] == 0:
                flg = True
                break
    #開始地点から水やり開始
        for n in range(start,N):
            if h[n] == 0:
                break
            else :
                h[n] -= 1
        cnt += 1

    print(cnt-1)







    



N,h =nyu()
mizuyari(N,h)

Submission status: Accepted

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

cnt = 0
while True:
    if max(h) == 0:
        break
    idx = 0
    while idx < n:
        if h[idx] == 0:
            idx += 1
        else:
            cnt += 1
            while idx < n and h[idx] != 0:
                h[idx] -= 1
                idx += 1

print(cnt)

Submission status: Accepted

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

    while sum(h) > 0:
        for i in range(n+1):
            if i > 0:
                if h[i-1] != 0 and h[i] == 0:
                    num += 1
        for i in range(n+1):
            if h[i] != 0:
                h[i] -= 1
    print(num)
resolve()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

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

ans = 0

for i in range(len(h)): 
    while h[i] != 0:
        j = i
        while j < len(h) and h[j] != 0:
            j += 1
        for k in range(i, j):
            h[k] -= 1
        ans += 1
print(ans)

Submission status: Accepted

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

cnt = min(h)
vector = [cnt] * n
flg = False

for i in range(100):
    pre = -2
    for j in range(n):
        
        if vector[j] < h[j]:
            vector[j] += 1
            pre = j
            if pre == n - 1:
                cnt += 1
        else:
            if pre == j - 1:
                cnt += 1

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = [ int(x) for x in input().split() ]
 
cnt = 0
while True:
    if max(h) == 0:
        break
    idx = 0
    while idx < n:
        if h[idx] == 0:
            idx += 1
        else:
            cnt += 1
            while idx < n and h[idx] != 0:
                h[idx] -= 1
                idx += 1
print(cnt)

Submission status: Accepted

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

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=[int(i) for i in input().split()]
for i in range(n):
  if i==0:
    tmp=h[i]
    ans=tmp
  else:
    if tmp<h[i]:
      ans= ans + h[i] - tmp
      tmp=h[i]
    else:
      tmp=h[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
IS = lambda: sys.stdin.readline().rstrip()
II = lambda: int(IS())
MII = lambda: list(map(int, IS().split()))

def main():
    n = II()
    hh = MII()
    tmp = 0
    cnt = 0
    for h in hh:
        if h < tmp:
            cnt += tmp-h
        tmp = h
    cnt += tmp
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
ns = map(int, input().split())
result = 0
temp = 0
for i in ns:
    if i < temp:
        result += temp-i
    temp = i
result += temp
print(result)

Submission status: Accepted

Solution code:
N=int(input())
H=list(map(int,input().split()))
c=0
while H!=[0]*N:
  for i in range(N):
    if H[i]!=0:
      l=i
      break
  for j in range(l,N):
    if H[j]!=0:
      H[j]-=1
    else:
      break
  c+=1
print(c)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
h = np.array(list(map(int, input().split())))
ans = 0
while (any(h > 0)):
  count = 0
  for i in range(n-1):
    if h[i] > 0 and h[i + 1] <= 0:
      count += 1
  if h[n-1] > 0:
    count += 1
  ans += count
  h -= 1
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(n):
    left = -1
    for j in range(n):
        if flower[j]:
            left = j
            break
    if left == -1:
        break

    MIN = flower[left]
    for k in range(left, n):
        if flower[k] == 0:
            right = k
            break
        else:
            MIN = min(MIN, flower[k])
    else:
        right = n
    for l in range(left, right):
        flower[l] -= MIN
    ans += MIN
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split(" ")))
k=0
for i in range(1,max(h)+1):
    c=0
    for j in h:
        if j >= i:
            if c==0:
                k+=1
                c=1
        else:
            c=0
print(k)

Submission status: Accepted

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

water = 0
ans = 0
for hi in h:
    if water < hi:
        ans += hi - water
    water = hi
print(ans)

Submission status: Accepted

Solution code:
if True:
    n= int(input())
    hn= list(map(int, input().split()))
else:
    import sys
    args = sys.argv
    set_flg = False
    if len(args) == 2:
        set_flg = True
        if args[1] == "2":
            n=5
            hn=[3,1,2,3,1]
        elif args[1] == "3":
            n=8
            hn=[4,23,75,0,23,96,50,100]
        else:
            set_flg = False

    if set_flg == False:
        n=4
        hn=[1,2,2,1]


# print(n)
# print(hn)

water = 0
target_idx = 0

for i in range(0,len(hn)):
    while hn[i] > 0:
        if hn[i] == 0:
            break

        for j in range(i+1,len(hn)):
            # print(hn)
            if hn[j] > 0:
                hn[j] -=1
            else:
                break
        hn[i] -= 1
        water += 1

print(water)

Submission status: Accepted

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

for i in range(N):
    while h[i] != 0:
        for j in range(i, N):
            if h[j] != 0:
                h[j] = h[j] - 1
            elif h[j] == 0:
                break
        ans += 1

print(ans)

Submission status: Accepted

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

base = [0]*(n+1)
base[0] = 1
h_max = max(h)
count = 0

for i in range(h_max):
  for j in range(1,n+1):
    if i == h[j-1]:
      base[j] = 1

    if (base[j-1] == 1) and (base[j] == 0):
      count = count + 1
    
print(count)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input()) # 入力が1つ
# map(int, input().split()) # 入力が複数
h = [int(i) for i in input().split()] # 配列で数字

ans = 0
start = 0
end = 0

ans = 0
while True:
    left = -1
    for i in range(n):
        if h[i]:
            left = i
            break
    if left == - 1:
        break

    right = n
    for i in range(left, n):
        if h[i] == 0:
            right = i
            break

    right_min = min(h[left:right])
    ans += right_min
    for i in range(left, right):
        h[i] -= right_min

print(ans)

Submission status: Accepted

Solution code:
mod = 10 ** 9 + 7
mod2 = 2 ** 61 + 1
from collections import deque
import heapq
from bisect import bisect_left, insort_left, bisect_right

_NUMINT_ALL = list(range(10))


def main():
    ans = solve()
    if ans is not None:
        print(ans)


def solve():
    N = iip(False)
    H = iip(True)
    h = [0 for i in range(N)]

    ans = 0
    while True:
        st = False
        for i in range(N):
            if not st:
                if h[i] != H[i]:
                    st = True
                    h[i] += 1
                    ans += 1
                elif i == N-1:
                    return ans
            elif st is True:
                if h[i] != H[i]:
                    h[i] += 1
                else:
                    break

#####################################################ライブラリ集ここから

def iip(listed=True, num_only=True):  # 数字のinputをlistで受け取る
    if num_only:
        ret = [int(i) for i in input().split()]
    else:
        ret = [int(i) if i in _NUMINT_ALL else i for i in input().split()]

    if len(ret) == 1 and not listed:
        return ret[0]
    return ret


def saidai_kouyakusuu(A):  # 最大公約数
    l = len(A)
    while True:
        m = min(A)
        mx = max(A)
        if m == mx:
            return m

        for i in range(l):
            if A[i] % m == 0:
                A[i] = m
            else:
                A[i] %= m


def sort_tuples(l, index):  # タプルのリストを特定のインデックスでソートする
    if isinstance(l, list):
        l.sort(key=lambda x: x[index])
        return l
    else:
        l = list(l)
        return sorted(l, key=lambda x: x[index])


def count_elements(l):  # リストの中身の個数を種類分けして辞書で返す
    d = {}
    for i in l:
        if i in d:
            d[i] += 1
        else:
            d[i] = 1
    return d


def safeget(l, index, default="exception"):  # listの中身を取り出す時、listからはみ出たり
    if index >= len(l):                      # マイナスインデックスになったりするのを防ぐ
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。配列の長さは", len(l), "です"]))
        else:
            return default
    elif index < 0:
        if default == "exception":
            raise Exception("".join(["safegetに不正な値 ", index, "が渡されました。負の値は許可されていません"]))
        else:
            return default
    else:
        return l[index]


def iipt(l, listed=False, num_only=True):  # 縦向きに並んでいるデータをリストに落とし込む(iip利用)
    ret = []
    for i in range(l):
        ret.append(iip(listed=listed, num_only=num_only))
    return ret


def sortstr(s):  # 文字列をソートする
    return "".join(sorted(s))


def iip_ord(startcode="a"):  # 文字列を数字の列に変換する(数字と文字は1:1対応)
    if isinstance(startcode, str):
        startcode = ord(startcode)
    return [ord(i) - startcode for i in input()]


def YesNo(s):  # TrueFalseや1, 0をYesNoに変換する
    if s:
        print("Yes")
    else:
        print("No")


def fprint(s):  # リストを平たくしてprintする(二次元リストを見やすくしたりとか)
    for i in s:
        print(i)


def bitall(N):  # ビット全探索用のインデックスを出力
    ret = []
    for i in range(2 ** N):
        a = []
        for j in range(N):
            a.append(i % 2)
            i //= 2
        ret.append(a)
    return ret

def split_print_space(s):  # リストの中身をスペース区切りで出力する
    print(" ".join([str(i) for i in s]))


def split_print_enter(s):  # リストの中身を改行区切りで出力する
    print("\n".join([str(i) for i in s]))


def soinsuu_bunkai(n):  # 素因数分解
    ret = []
    for i in range(2, int(n ** 0.5) + 1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):  # nCrをmodを使って計算する
    if n <= 0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n - r + 1, n + 1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r + 1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        # print(f"{n}C{r} = {ret}")
        pass
    return ret


def inv(n, mod):  #  modnにおける逆元を計算
    return power(n, mod - 2)


def power(n, p, mod_=mod):  # 繰り返し二乗法でn**p % modを計算
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p // 2, mod_) ** 2) % mod_
    if p % 2 == 1:
        return (n * power(n, p - 1, mod_)) % mod_


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import deque
from collections import Counter
from itertools import product, permutations,combinations
from operator import itemgetter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right, bisect
#pypyではscipy, numpyは使えない
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, minimum_spanning_tree
#from scipy.sparse import csr_matrix, coo_matrix, lil_matrix
import numpy as np
from fractions import gcd
from math import ceil,floor, sqrt, cos, sin, pi, factorial
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**8)
INF = float('inf')
MOD = 10**9+7

def main():
  N = int(readline())
  H = np.array(readline().split(), dtype = np.int32)
  
  def F(L,R):
    if R<L:
      return 0
    idx = L + H[L:R+1].argmin()
    x = H[idx]
    H[L:R+1] -= x
    return x + F(L, idx-1) + F(idx+1,R)
  
  answer = F(0,N-1)
  print(answer)
  
  
  
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    n = int(input())
    h = list(map(int, input().split()))
    res = 0
    while sum(h):
        for i in range(n):
            if h[i]==0:
                continue
            h[i] -= 1
            if i==n-1:
                res += 1
            elif h[i+1]==0:
                res += 1
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
while True:
    if max(h) == 0:
        break
    idx = 0
    while idx < n:
        if h[idx] == 0:
            idx += 1
        else:
            cnt += 1
            while idx < n and h[idx] != 0:
                h[idx] -= 1
                idx += 1

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
v=sum(l)
e=0
for i in range(max(l)):
  ef=0
  for j in l:
    if j>i:
      ef+=1
    else:
      if ef>0:
        e+=ef-1
      ef=0
  if ef>1:
    e+=ef-1
print(v-e)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
ans = 0
while True:
    i = 0
    if not any(h):  # hのリストがなくなるまで続ける
        break
    while i < N:
        if h[i] == 0: # 高さが0なら、次の花へ
            i += 1
        else:
            ans += 1 # 高さが1以上なら、その花の高さを1減らし、答えを1追加する
            while i < N and h[i] != 0:
                h[i] -= 1
                i += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
h.append(0)
count = 0
for j in range(100):
    work = []
    temp = []
    for el in h:
        if el == 0:
            if temp != []:
                m = min(temp)
                count += m
                for i in range(len(temp)):
                    temp[i] -= m
                temp.append(0)
                work.append(temp)
                temp = []
        else:
            temp.append(el)
    h = []
    for w in work:
        for el in w:
            h.append(el)
print(count)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
ans = 0
while True:
    i = 0
    if not any(h):
        break
    while i < N:
        if h[i] == 0:
            i += 1
        else:
            ans += 1
            while i < N and h[i] != 0:
                h[i] -= 1
                i += 1
print(ans)

Submission status: Accepted

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

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
h = LI()
ans = 0
while sum(h) > 0:
    flg = False
    for j in range(n):
        if h[j] > 0:
            if not flg:
                flg = True
                ans += 1
            h[j] -= 1
        else:
            flg = False
print(ans)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, *h = map(int, open(0).read().split())
    ans = 0
    for i, j in zip([0] + h, h):
        if i < j:
            ans += (j - i)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    n, *h = map(int, open(0).read().split())
    ans = 0
    f = True
    base = 0
    for i, j in zip(h, h[1:]):
        g = i <= j
        if f and not g:
            ans += i - base
        if not f and g:
            base = i
        f = g
    else:
        if f:
            ans += h[-1] - base
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n = ni()
a = na()

flag = True
ans = 0
while flag:
    flag = False
    last = 0
    for i in range(n):
        t = a[i]
        if a[i] != 0:
            flag = True
            if last == 0:
                ans += 1
            a[i] -= 1
        last = t

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=0
def mizu(h_list):
  global ans
  if not h_list:
    return
  if max(h_list)==0:
    return
  m=min(h_list)
  ans+=m
  h_list_=list(map(lambda x :x-m, h_list))
  zero_ind = h_list_.index(0)
  mizu(h_list_[:zero_ind])
  mizu(h_list_[zero_ind+1:])
mizu(h)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
cnt = 0
while True:
    if max(h) == 0:
        print(cnt)
        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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
while True:
    if not any(h):
        break
        
    i = 0
    while i < N:
        if h[i] == 0:
            i += 1
        else:
            ans += 1
            while i < N and h[i] != 0:
                h[i] -= 1
                i += 1

print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    lst = list(map(int, input().split()))
    groups = [lst]
    ans = 0

    while len(groups) != 0:
        #print('------')
        targetgroup = groups.pop()

        minval = min(targetgroup)
        targetgroup = [v - minval for v in targetgroup]
        ans += minval
        #print(targetgroup)

        tmp = []
        for index, v in enumerate(targetgroup):
            if v != 0:
                tmp.append(v)
            if (v==0 or index==len(targetgroup)-1) and (len(tmp) != 0):
                groups.append(tmp)
                tmp = []
        #print(groups)
    print(ans)
main()

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
cnt = 0
i = 0
for _ in range(100 * 100):
    if h[i] > 0:
        j = i
        cnt += 1
        while j < N and h[j] > 0:
            h[j] = h[j] - 1
            j += 1
    else:
        i += 1
        if i == N:
            break
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
zeros=[]
prev=0
for i in  range(n):
  if h[i]==0:
    if i==0:
      prev=i+1
    else:
      if prev<=i-1:
        zeros.append((prev,i-1))
      prev=i+1
if prev<=n-1:
  zeros.append((prev,n-1))
cnt=0
while zeros:
  l,r=zeros.pop()
  m=min(h[l:r+1])
  cnt+=m
  prev=l
  for i in range(l,r+1):
    h[i]-=m
    if h[i]==0:
      if i==l:
        prev=i+1
      else:
        if prev<=i-1:
          zeros.append((prev,i-1))
        prev=i+1
  if prev<=r:
    zeros.append((prev,r))
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 active >= h[i]:
        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()))

h1 = h+ []
lng = len(h1)
ans = 0
while not all([i == 0 for i in h1]):
    left = lng
    right = 0
    amin = float("inf")
    for i in range(lng):
        if h1[i] == 0 and amin == float("inf"):
            continue
        if amin > 0 and h1[i] == 0:
            break
        amin = min(amin, h1[i])
        left = min(left, i)
        right = max(right, i)

    ans += amin
    for i in range(left, right+1):
        h1[i] -= amin

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
h.append(0)
ans = 0
while h.count(0) < N+1:
  count = 0
  for i in range(N+1):
    if h[i] > 0:
      count += 1
      h[i] -= 1
    else:
      if count > 0:
        ans += 1
      count = 0
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):
    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()))
flower=[0]*N
count=0
on=0
for j in range(max(h)):
    for i in range(N):
        if i==0:
            on=0
        if flower[i]<h[i]:
            flower[i]+=1
            on=1
            if i==N-1:
                count+=1
        elif on==1 and flower[i]==h[i]:
            count+=1
            on=0
        #print(flower, i, on)
        #print(count)
print(count)

Submission status: Accepted

Solution code:
N=int(input())
h=list(map(int,input().split()))
h.append(0)
ans=0
flag = False
for hei in range(1,101):
  for i in range(N+1):
    if h[i] >= hei and i!=N:
      flag = True
    else:
      if flag:
        ans+=1
        flag = False
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
H = np.array(list(map(int, input().split())))
cnt = 0
f_f = 0 #前の花
n_f = 0 #今の花
v = 0 # 谷の数

cnt = min(H)
H -=cnt
precnt = 0
for i in range(max(H)):
    a = 0
    for j in range(N):
        v=0
        if j == 0 and H[j] > 0:
            v+=1
        elif H[j] > 0 and H[j-1] == 0:
            v+=1
        cnt+=v
        a+=v

    for j in range(N):
        if H[j] > 0:
            H[j]-=1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
h.append(0)
cnt = 0
while h != [0]*(n+1):
  for i in range(n):
    if (h[i] != 0) and (h[i+1] == 0):
      h[i] -= 1
      cnt += 1
    elif h[i] != 0:
      h[i] -= 1
else:
  print(cnt)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    hlist = list(map(int, input().split()))
    tot = 0
    for i in range(N):
        if i==0:
            tot += hlist[i]
        else:
            if hlist[i]-hlist[i-1]>0:
                tot += hlist[i]-hlist[i-1]
    
    print(tot)
solve()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
f = [0]*n
fflag = [False]*n
cnt = 0
flag = False
while False in fflag:
    for i in range(n):
        if f[i] == h[i]:
            fflag[i] = True
        if not fflag[i]:
            f[i] += 1
            flag = True
        elif flag:
            cnt += 1
            flag = False
    if flag:
        cnt += 1
        flag = False
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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:
num = int(input())
count = 0
num_list = list(map(int,input().split()))
while(1):
  dummy = max(num_list)
  if dummy == 0:
    break
  flag = "nocheck"
  for i in range(num):
    if num_list[i] == dummy:
      num_list[i] -= 1
      flag = "check"
    else:
      if flag == "check":
        flag = "nocheck"
        break
  count += 1
print(count)

Submission status: Accepted

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

change_count = 0

while True:
    for i in range(n+1):
        status = [0] * 2
        if h_ls[i] == 0:
            status[0] = 'zero'
        else:
            status[0] = 'not_zero'
        if h_ls[i+1] == 0:
            status[1] = 'zero'
        else:
            status[1] = 'not_zero'
        if status[0] != status[1]:
            change_count+= 1
    for i in range(n+2):
        if h_ls[i] != 0:
            h_ls[i] -= 1
    if h_ls == [0] * (n+2):
        break

count = change_count // 2
print(count)

Submission status: Accepted

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def f(arr):
    m = min(arr)
    for i in range(len(arr)):
        arr[i] -= m
    buf = []
    ret = m
    for i in arr:
        if i == 0:
            if len(buf) > 0:
                ret += f(buf)
                buf = []
        else:
            buf.append(i)
    if len(buf) > 0:
        ret += f(buf)
    return ret

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

main()