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()))

ans = 0
def solve(li_h):
    l = 0
    for i in li_h:
        if(i==0):
            l += 1
        else:
            break
    r = l + 1
    for i in li_h[l+1:]:
        if(i==0):
            break
        r += 1
    
    for i in range(l, r):
        li_h[i] -= 1
    
    return li_h

while(sum(h)!=0):
    h = solve(h)
    ans += 1

print(ans)

Submission status: Accepted

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

num = 0
for i in range(n):
    
    if L[i] <= L[i+1]:
        num += L[i+1]-L[i]

print(num)

Submission status: Accepted

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

num = 0
min1 = 0
for i in range(n):
    
    if L[i] <= L[i+1]:
        num += L[i+1]-L[i]
    else:
        min1 = L[i+1]

print(num)

Submission status: Accepted

Solution code:
import math

n = int(input())
hL = list(map(int, input().split(" ")))

ans = 0
for i in range(1, 101):
    r = False
    for h in hL:
        if i <= h:
            if not r:
                ans += 1
            r = True
        else:
            r = False

print(ans)

Submission status: Accepted

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



ans=h[0]

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

Submission status: Accepted

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

import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    n = input_int()
    flowers = [0] + input_int_list()
    section_min = 0
    ans = 0
    for i in range(1, n + 1):
        if flowers[i] < flowers[i - 1]:
            ans += flowers[i - 1] - section_min
            section_min = flowers[i]
    if flowers[n] > section_min:
        ans += flowers[n] - section_min
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def INT():
    return int(input())

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

def MI():
    return map(int, input().split())

N = INT()
h = [0] + LI() + [0]
ans = 0

while not all([h[i] == 0 for i in range(len(h))]):
    min_idx = [idx for idx, flower in enumerate(h) if flower == 0]
    
    for i in range(len(min_idx) - 1):
        idx_1, idx_2 = min_idx[i], min_idx[i + 1]
        if idx_1 + 1 == idx_2:
            continue
        
        min_tall = min(h[idx_1 + 1 : idx_2])
        ans += min_tall
            
        for j in range(idx_1 + 1, idx_2):
            h[j] -= min_tall
        
print(ans)

Submission status: Accepted

Solution code:
def splitcount(l):
    if len(l) == 0:
        return 0
    minl = min(l)
    #print("l",l)
    if minl==0:
        #print("l[:max(0,l.index(0))]",l[:max(0,l.index(0))])
        #print("splitcount(l[l.index(0)+1:])",splitcount(l[l.index(0)+1:]))
        return splitcount(l[:l.index(0)]) + splitcount(l[l.index(0)+1:])
    else:
        return minl + splitcount(list(map(lambda x: x - minl, l)))

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


print(splitcount(h))

Submission status: Accepted

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

V = sum(H)
E = 0

for i in range(1,N):
  E += min(H[i-1],H[i])
  
print(V-E)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
for hh in range(1,max(h)+1):
  flg = False
  for i in range(n):
    if h[i] >= hh:
      flg = True
    else:
      if flg:
        flg = False
        cnt += 1
  if flg:
    flg = False
    cnt += 1      
print(cnt)

Submission status: Accepted

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

flag = False
ans = 0
while not flag:
  for i in range(n):
    if flag:
      if h[i] != 0: h[i] -= 1
      else: break
    elif not flag and h[i] != 0: 
      flag = True
      h[i] -= 1
      ans += 1
  if not flag: break
  flag = False
print(ans)

Submission status: Accepted

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

t = 0

def Water(n):
    global h
    while h[n] != 0:
        h[n] -= 1
        n += 1
        #print(h, t)

for i in range(N):
    while h[i] != 0:
        Water(i)
        t += 1

print(t)

Submission status: Accepted

Solution code:

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

res = 0
for _ in range(100):
    if all(v == 0 for v in X):
        break

    flag = False
    for i in range(N):
        if X[i] > 0:
            X[i] -= 1
            flag = True
        elif flag:
            flag = False
            res += 1

    if flag:
        res += 1

print(res)

Submission status: Accepted

Solution code:
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from heapq import heappop, heappush, heappushpop
import copy
import numpy as np
import sys
INF = float('inf')
mod = 10**9+7


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


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

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


def main():
    n = I()
    h = list(LI())
    ans = h[0]
    for i in range(1, n):
        if h[i] > h[i-1]:
            ans += h[i] - h[i-1]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def divide_and_conuer(ls):
    mi=ls.index(min(ls))
    res=ls[mi]
    if(ls[mi+1:len(ls)]!=list()):
        res+=divide_and_conuer(ls[mi+1:len(ls)])-ls[mi]
    if(ls[0:mi]!=list()):
        res+=divide_and_conuer(ls[0:mi])-ls[mi]
    return res

def main():
    n = int(input())
    a = list(map(int, input().split()))
    print(divide_and_conuer(a))

if __name__ =="__main__":
    main()

Submission status: Accepted

Solution code:
def solve(L):
    if not L:
        return 0
    if 0 not in L:
        return min(L)+solve([l-min(L) for l in L])
    tmp=0
    l,p=0,False
    for i in range(len(L)):
        if L[i]==0:
            if p:
                p=False
                tmp+=solve(L[l:i])
                l=i+1
            else:
                l=i+1
        else:
            if not p:
                p=True
    return tmp+solve(L[l:])

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

print(solve(H))

Submission status: Accepted

Solution code:
import sys
import math
sys.setrecursionlimit(500000)
INF = float('inf')

def solve(h, water):
    left = 0
    while left < len(h) - 1 and h[left] == 0:
        left += 1
    right = left
    while right < len(h) and h[right] != 0:
        right += 1

    if left < right:
        h[left:right] = [i - 1 for i in h[left:right]]
    elif left == right and h[left] > 0:
        h[left] = h[left] - 1
    else:
        return water
        
    water = solve(h, water + 1)
    return water

def main():
    n = int(input())
    h = list(map(int, input().split()))
    return solve(h, 0)

if __name__ == '__main__':
    print(main())

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#https://note.nkmk.me/python-gcd-lcm/
def f(myList):
    if len(myList) == 0:
        return 0
    else:
        #print(myList)
        tmp = min(myList)
        ans = tmp
        for i in range(len(myList)):
            myList[i] -= tmp
        newList = []
        for i in range(len(myList)):
            pop_a = myList.pop()
            if pop_a != 0:
                newList.append(pop_a)
            else:
                ans += f(newList)
                newList = []
        ans += f(newList)
        return ans 

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

print(f(a))

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
res=0
while True:
  if max(h)==0:
    break

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

Submission status: Accepted

Solution code:
def recur(heights):
    global ans
    if heights == [0]:
        return 1
    ans += 1
    new_heights = []
    for h in heights:
        new_heights.append(h - 1)
    for splitted_new_heights in split_heights(new_heights):
        recur(splitted_new_heights)


def split_heights(heights):
    splitted_heights = []
    partial_heights = []
    for h in heights:
        if h == 0 and len(partial_heights) > 0:
            splitted_heights.append(partial_heights)
            partial_heights = []
        elif h != 0:
            partial_heights.append(h)
    if len(partial_heights) > 0:
        splitted_heights.append(partial_heights)
    return splitted_heights


def main():
    global ans
    ans = 0
    N = int(input())
    h = list(map(int, input().split()))
    for splitted_heights in split_heights(h):
        recur(splitted_heights)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split(" ")))
cnt = 0
minNum = 0
tmp = 0

while True:
    for i in range(h.count(0)):
        if h.index(0) == 0: 
            h.pop(0)
        elif h.index(0) > 0:
            tmp = h.index(0)
            break
    else:
        tmp = len(h)

    if not h:
        break

    if tmp == 1:
        cnt += h[0]
        h.pop(0)
    else:
        minNum = min(h[0:tmp])
        cnt += minNum
        for j in range(tmp):
            h[j] -= minNum

print(cnt)

Submission status: Accepted

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

ans = 0
pre = 0
for i in h:
    if pre < i:
        ans += i-pre

    pre = i

print(ans)

Submission status: Accepted

Solution code:
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
sys.setrecursionlimit(500*500)
import itertools
from collections import Counter,deque

def main():
    n = int(input())
    h = list(map(int, input().split()))
    l = [[0]*100 for _ in range(100)]
    for i in range(len(h)):
        k = h[i]
        for q in range(k):
            l[i][q] = 1
    l = [list(x) for x in zip(*l)]
    ans = 0
    for i in l:
        flg = 0
        for g in i:
            if g == 1:
                if flg == 0:
                    ans += 1
                flg = 1
            if g == 0:
                flg = 0
    print(ans)


if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
N=int(input())
h=list(map(int,input().split()))
l=[0]*N
z=0
for i in range(0,100):
    f=False
    for j in range(0,N):
        if h[j]>i:
            if not f:
                f=True
        else:
            if f:
                f=False
                z+=1
    if f:
        z+=1

print(z)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
readline = stdin.buffer.readline

def watering(h):
    ans = min(h)
    hh = list(map(lambda x: x - ans, h))
    if len(set(hh)) == 1:
        return ans
    fs = 0
    for i, n in enumerate(hh):
        if n == 0:
            if i != fs:
                hhh = hh[fs:i]
                ans += watering(hhh)
                fs = i + 1
            else:
                fs = i + 1
    else:
        hhh = hh[fs:]
        if len(hhh) > 0:
            ans += watering(hhh)
    return ans

def main():
    N = int(readline().rstrip())
    H = list(map(int, readline().rstrip().split()))
    print(watering(H))

if __name__ == "__main__":
    main()

Submission status: Accepted

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

def main(n,h):
    count=0
    hmax=max(h)
    for i in range(hmax):
        j=0
        mizuyari=False
        for j in range(n):
            if h[j]>0:
                if mizuyari==False:
                    mizuyari=True
                    count+=1
                h[j]-=1
            else:
                mizuyari=False
    return count

if __name__=='__main__':
    n,h=readinput()
    ans=main(n,h)
    print(ans)

Submission status: Accepted

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

active = 0
ans = 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:
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip.split())
def main():
    n = I()
    h = LI()
    ok = 0
    ng = 0
    ans = 0

    for i,x in enumerate(h):
        if x>0:
            ok = i
            break
    else:
        ok = n

    while ok != n:
        for i,x in enumerate(h[ok+1:]):
            if x == 0:
                ng = ok + 1 + i
                break
        else:
            ng = n
        # print(ok,ng)
        sub = [0]*ok + [1]*(ng-ok) + [0]*(n-ng)

        for i in range(n):
            h[i] -= sub[i]
        # print(h)
        for i,x in enumerate(h):
            if x>0:
                ok = i
                break
            else:
                ok = n
        ans += 1

    print(ans)
main()

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
H = [h]
ans = 0
while len(H) > 0:
  for e in H:
    if len(e) == 0:
      H.remove(e)
    elif 0 in e:
      a = e[:e.index(0)]
      b = e[e.index(0) + 1:]
      H.append(a)
      H.append(b)
      H.remove(e)
      if [] in H:
        H.remove([])
        if len(H) == 0:
          break
    else:
      ans += min(e)
      c = [e[j] - min(e) for j in range(len(e))]
      a = c[:c.index(0)]
      b = c[c.index(0) + 1:]
      H.append(a)
      H.append(b)
      H.remove(e)
      if [] in H:
        H.remove([])
        if len(H) == 0:
          break
    if [] in H:
      H.remove([])
      if len(H) == 0:
        break
print(ans)

Submission status: Accepted

Solution code:
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")

n = k()
h = l()

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

print(ans+h[-1])

"""
n = k()
s = v()

def check(n):
    if 

#コマンドの種類
comand = ["A","B","X","Y"]
p = list(itertools.permutations(comand,2))
for i in range(len(p)):
    p[i] = "".join(p[i])

for i in 
"""

Submission status: Accepted

Solution code:
N = int(input())
H = [[0]*N for _ in range(100)]
for j, h in enumerate(map(int, input().split())):
    for i in range(h):
        H[i][j] = 1
cnt = 0
for i in range(100):
    a0 = 0
    for a in H[i]:
        if a != a0 and a0==0:
            cnt += 1
        a0 = a
print(cnt)

Submission status: Accepted

Solution code:
from sys import stdin
from numpy import argmin

n = int(stdin.readline().strip())
h_lst = [int(x) for x in stdin.readline().split()]
cnt = 0

def glow(lst, start_pos):
  global cnt
  if len(lst) == 0:
    return -1
  elif len(lst) == 1:
    cnt += lst[0] - start_pos
    return -1
  else:
    pos = min(lst)
    cnt += pos - start_pos
    id_ = argmin(lst)
    glow(lst[:id_], pos)
    glow(lst[id_+1:], pos)
    
glow(h_lst, 0)
print(cnt)

Submission status: Accepted

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

while True:
    if max(h) == 0:
        break
    while i < n:
        if h[i] != 0:
            for j in range(i,n):
                if h[j] == 0:
                    #print(j)
                    break
                h[j] -= 1
            #print(h)
            ans += 1
        else:
            i += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
#print([i for i, x in enumerate(l_dup) if x == 'x'])
ans = 0
#for i in range(-1):
    #print("unko")
while True:
    i_of_minus = [i for i, x in enumerate(h) if x <= 0]
    c = len(i_of_minus)
    #print(h)
    if c >= N:
        break
    if c == 0:
        ans += 1
        h = list(map(lambda x: x-1, h))
    else:
        if i_of_minus[0] != 0:
            ans += 1
        if i_of_minus[-1] != N - 1:
            ans += 1
        for j in range(1, c):
            if i_of_minus[j] - i_of_minus[j-1] > 1:
                ans += 1
        h = list(map(lambda x: x - 1, h))
print(ans)

Submission status: Accepted

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

lowc = 0

for i in range(1, N):
    if H[i - 1] > H[i]:
        lowc += H[i - 1] - H[i]
        
print(lowc + H[-1])

Submission status: Accepted

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

r=0
b=[a]
while True:
    d=[]
    for x in b:
        m=min(x)
        r+=m
        d.append([i-m for i in x])

    b=[]
    for x in d:
        t=[]
        for y in range(len(x)):
            if y==len(x)-1:
                if x[y]!=0:
                    t.append(x[y])
                    b.append(t)
                if x[y]==0 and x[y-1]!=0:b.append(t)
            elif x[y]!=0:t.append(x[y])
            elif x[y]==0:
                if len(t)!=0:b.append(t)
                t=[]

    d=[]
    for x in b:
        if len(x)<=2:r+=max(x)
        else:d.append(x)
    if len(d)==0:print(r);exit()
    b=d

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = [int(i) for i in 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())
hs =list(map(int, input().split()))

ans = hs[-1]
for i in range(1,n):
    if hs[i] < hs[i-1]:
        ans += hs[i-1] -hs[i]
    else:
        continue
        
print(ans)

Submission status: Accepted

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

for i in range(max(h)):
  flag=False
  cnt=0
  for j in range(n):
    if h[j]>0 and flag==False:
      flag=True
      cnt+=1
    if h[j]<=0 and flag==True:
      flag=False
  ans+=cnt
  for j in range(n):
    h[j]-=1
print(ans)

Submission status: Accepted

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

h.append(0)
pre = True 
next = True 
cnt = 0
for i in range(N):
    if h[i+1]>h[i]:
        next = True 
        if pre == False:
            cnt -= h[i]
    elif h[i+1]<h[i]:
        next = False
        if pre == True:
            cnt += h[i]
    else:
        next = pre 
    pre = next 
print(cnt)

Submission status: Accepted

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

l = 0
flag = 1
count = 0
while l < N-1:
  if h[l] == 0:
    l += 1
  elif h[l] != 0:
    r = l+1
    while h[r] != 0:
      r += 1
      if r == N:
        break
    for k in range(l,r):
      h[k] -= 1
    count += 1
print(count+h[N-1])

Submission status: Accepted

Solution code:
N=int(input())
*H,=map(int,input().split())
L=len(H)

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,=map(int,input().split())
L=len(H)

cnt=0
prv=0

for i in range(N):
    if  prv < H[i]:
        cnt+=H[i]-prv
    prv=H[i]
print(cnt)

Submission status: Accepted

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

l = 0
flag = 1
count = 0
while l < N:
  while h[l] == 0:
    l += 1
    if l == N:
      flag = 0
      break
  if flag == 1:
    if l == N-1:
      r = N-1
    else:
      r = l+1
      while h[r] != 0:
        r += 1
        if r == N:
          break
      r -= 1
    for k in range(l,r+1):
      h[k] -= 1
    count += 1
    #print(h)
  elif flag == 0:
    break
print(count)

Submission status: Accepted

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

while True:
    if sum(h) == 0:
        break

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, *H = map(int, read().split())

    ans = 0
    while True:
        changed = changed2 = False
        for i in range(N):
            if H[i] > 0:
                H[i] -= 1
                changed = True
                changed2 = True
            else:
                if changed:
                    ans += 1
                    changed = False
        if changed:
            ans += 1
        if not changed2:
            break

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(lambda x: -int(x), input().split())) + [0]
cnt = 0

for i in range(N):
  while H[i] < 0:
    H[i] += 1
    j = i+1
    while H[j] < 0:
      H[j] += 1
      j += 1
    cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input().rstrip())
h = [int(i) for i in input().rstrip().split()]

check = [0]*N
height = [0]*N
count = 0

while True:
  check = [1 if h[i] == height[i] else 0 for i in range(N)]
  if check.count(1) == N:
    break
  count += 1
  for i in range(N):
    if check[i] == 0:
      height[i] += 1
      if i<N-1 and check[i+1] == 1:
        break
  #print(height)
  
  
print(count)

Submission status: Accepted

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

s = 0

for i in range(max(h)+1):
    flag = False
    for j in range(n):
        if flag and h[j] == 0:
            s += 1
            flag = False
        elif not flag and h[j] > 0:
            flag = True

        h[j] = max(h[j]-1, 0)
    if flag:
        s += 1

print(s)

Submission status: Accepted

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

while a:
    mizu = False
    if sum(h) == 0:
        a = False
        break

    w += 1
    for i in range(n):
        if h[i] != 0:
            h[i] -= 1
            mizu = True
        else:
            if mizu:
                break 


print(w)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
grow = True
while grow:
    inc = False
    grow = False
    for i in range(n):
        if h[i] == 0:
            inc = False
        else:
            h[i] -= 1
            if not inc:
                inc = True
                grow = True
                cnt += 1
print(cnt)

Submission status: Accepted

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

for i in range(1, hmax+1):
    for j in range(n):
        if f and (h[j] < i):
            ans += 1
        f = h[j] >= i
    if f:
        ans += 1
    f = False

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
tmp = 0
for x in h:
    if tmp < x:
        ans += x - tmp
    tmp = x
print(ans)

Submission status: Accepted

Solution code:
import math

n = int(input())
hL = list(map(int, input().split(" ")))

ans = 0
cont = False
while True:
    if len(set(hL)) == 1 and hL[0] == 0:
        break
    cont = False
    cnt = 0
    for j in range(n):
        if hL[j] != 0:
            hL[j] -= 1
            if not cont:
                cnt += 1
            cont = True
            continue
        else:
            cont = False

    ans += cnt

print(ans)

Submission status: Accepted

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

ans = 0
while max(h) != 0:
  if h[0] > 0: flag = True
  else: flag = False
    
  if flag:
    for i in range(n):
      if h[i] == 0: break
      h[i] -= 1
    ans += 1
  else:
    for i in range(1,n):
      if flag == False and h[i] == 0: continue
      if flag == True and h[i] == 0: break
      flag = True
      h[i] -= 1
    ans += 1
      
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N = ii()
H = li()

m = min(H)
cnt = H[0]

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

print(cnt)

Submission status: Accepted

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

def f(A):
  if len(A) == 0:
    return 0
  elif len(A) == 1:
    return A[0]
  elif len(A) == 2:
    return max(A)

  c = 0
  if A.count(0) == 0:
    c += min(A)
    A = [a - min(A) for a in A]

  B = split_list(A)
  ret = []
  for j in range(len(B)):
    ret.append(f(B[j]))

  return c + sum(ret)

def split_list(A):
  B = []
  tmp = []
  for i in range(len(A)):
    if A[i] != 0:
      tmp.append(A[i])
    else:
      if len(tmp) > 0:
        B.append(tmp)
        tmp = []
  else:
    if len(tmp) > 0:
      B.append(tmp)
  return B

print(f(H))

Submission status: Accepted

Solution code:
def main():
    n=int(input())
    h=list(map(int,input().split()))
    count = 0
    while max(h) > 0:
        m = 101
        i = 0
        while h[i] == 0:
            i += 1
        j = i
        while i < n:
            if j == n or h[j] == 0:
                count += m
                for x in range(i,j):
                    h[x] -= m
                i = j
                break
            elif h[j] != 0:
                m = min(m, h[j])
            j += 1
    print(count)
    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys


def input():
    return sys.stdin.readline().rstrip()


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


    dflag = 0
    cmin = 0
    cmax = h[0]
    count = 0
    for i in range(1, N):
        if h[i - 1] < h[i] and dflag == 1:
            count += cmax - cmin
            cmax = h[i]
            dflag = 0
            cmin = h[i - 1]

        elif h[i - 1] > h[i]:
            dflag = 1

        elif h[i - 1] < h[i]:
            cmax = h[i]

    if dflag == 1:
        count += cmax - cmin
    else:
        count += max(cmax, h[i]) - cmin

    print(count)


if __name__ == "__main__":
    main()