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

# 最も高い花の高さを1減らす
# このとき、隣接した同じ高度の花については1回で高さを減らせるとする

# 高さを減らす花の座標を渡すと、隣接した同じ高度の花も含めて高さを1減らす関数
def bury(x):
    for i in range(1,N):
        if x+i >= N or h[x+i] != h[x]:
            break
        else:
            h[x+i] -= 1
    for i in range(1,N):
        if x-i < 0 or h[x-i] != h[x]:
            break
        else:
            h[x-i] -= 1
    h[x] -= 1

ans = 0
while sum(h) > 0:
    bury(h.index(max(h)))
    ans += 1
    
print(ans)

Submission status: Accepted

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

result = 0
while True:
    start = N
    for i in range(N):
        if h[i] != 0:
            start = i
            break
    if start == N:
        break
    end = N - 1
    count = float('inf')
    for i in range(start, N):
        if h[i] == 0:
            end = i - 1
            break
        if h[i] < count:
            count = h[i]
    for i in range(start, end + 1):
        h[i] -= count
    result += count
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

water=0
count=0

for i in range(n):
    if h[i]>water:
        count+=h[i]-water
    # ブロックの区切り
    water=h[i]
print(count)

Submission status: Accepted

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

w = 0
c = 0
for i in range(n):
    if h[i] > w:
        c += h[i] - w
    w = h[i]

print(c)

Submission status: Accepted

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

water = 0
pre = 0
for now in h_s:
  if now > pre:
    #もしもpreよりもnowの方が大きい場合 pre はnowに内包される
    water += now - pre
  pre = now
print(water)

Submission status: Accepted

Solution code:
from collections import deque

def sol():
  q = deque()
  q.append(H)
  ans = 0
  while len(q) != 0:
    prv_H = q.pop()
    min_h = min(prv_H)
    ans += min_h
    
    tmp_H = ''.join([',' if h-min_h == 0 else str(h-min_h)+' ' for h in prv_H])
    for sub_H in tmp_H.split(','):
      if len(sub_H.replace(' ','')) != 0 :
        q.append([int(h) for h in sub_H.split()])
    #print(ans, q) 
  return ans

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

Submission status: Accepted

Solution code:
n = int(input())
flowers = list(map(int,input().split()))
ans = 0
if n ==1:
    print(flowers[0])
    exit()
while any(flowers):
    count = 0
    for i in range(n):
        if i ==0:
            if flowers[i]!=0:
                count +=1
        else:
            if (flowers[i] > 0) and (flowers[i-1]==0):
                    count +=1
    ans +=count
    flowers = [x-1 if x>0 else 0 for x in flowers]
print(ans)

Submission status: Accepted

Solution code:
import copy
N = int(input())
H = list(map(int,input().split()))
HH = copy.deepcopy(H) 
ans = 0
while HH.count(0) != N:
  for i in range(N):
    if HH[i] == 0:
      pass
    else:
      HH[i] -= 1
      if i == N - 1:
        ans += 1
      else:
        if HH[i+1] == 0:
          ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
from bisect import *
from collections import *
from copy import deepcopy
from datetime import *
from heapq import *
from itertools import *
# from math import *
from operator import *
from pprint import *

sys.setrecursionlimit(10**8)

input = sys.stdin.readline

def main():
    """ main """
    N = int(input())
    HS = list(map(int, input().split()))
    max_h = max(HS)
    ans = 0
    for i in range(1, max_h+1):
        tmp = 0
        state = False
        for j in range(N):
            if HS[j] >= i and not state:
                tmp += 1
                state = True
            elif HS[j] < i and state:
                state = False
        ans += tmp
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n =int(input())
h = list(map(int,input().split()))
i = 0
res = 0
for _ in range(1,10000):
    i = 0
    while h[i] == 0:
        i += 1
        if i >n-1:
            break
    if i > n-1:
        break
    while h[i]>0:
        h[i] -= 1
        i += 1
        if i > n-1:
            break
    res += 1

print(res)

Submission status: Accepted

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

ans, cur = 0, 0
for h in H:
    if cur < h:
        ans += h - cur
    cur = h
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
count = 0
prev = 0
for i in l:
  if i > prev:
    count += i - prev
  prev = i
    
print(count)

Submission status: Accepted

Solution code:
N=int(input());a=0;t=0
for h in map(int,input().split()):
    a+=max(h-t,0);t=h
print(a)

Submission status: Accepted

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


# hがあるうちは回る
while (len(h)):
    now_h = h.pop()
    min_k = min(now_h)
    ans += min_k
    tmp = []
    for i in range(len(now_h)):
        now = now_h[i] - min_k
        if now == 0:
            if len(tmp):
                h.append(tmp)
                tmp = []
        else:
            tmp.append(now)
    if len(tmp):
        h.append(tmp)

print(ans)

Submission status: Accepted

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

while True:
  if h.count(0) == n+1:
    break
  else:
    for i in range(n):
      if h[i] == 0:
        continue
      else:
        h[i] -= 1
        if h[i+1] == 0:
          count += 1
print(count)

Submission status: Accepted

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

ct  = 0
prev = 0
for i in l:
    if i > prev:
        ct += i - prev
    prev = i
    
print(ct)

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

Submission status: Accepted

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

water = 0
before = 0
for i in lis_h:
    if i > before:
        water += i - before
    before = i

print(water)

Submission status: Accepted

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

cnt=0
while any([i!=0 for i in h]):
    start,end=100,100
    for i in range(n+2):
        if h[i]!=0:
            start=min(start,i)
        elif h[i]==0 and start<i:
#            end=min(end,i)
            end=i
            break
    l=[i-1 for i in h[start:end]]
    h=h[:start]+l+h[end:]
    cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

n=int(input())  #数値入力 「N」だけの入力のとき
h=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
cnt=0
L=0
while L != len(h):
    if h[L] != 0:
        for r in range(L+1 ,len(h)):
            if h[r] == 0:
                #do it
                h[L:r] = [i-1 for i in h[L:r]]
                cnt+=1
                break
        else:
            r = len(h)
            h[L:r] = [i-1 for i in h[L:r]]
            cnt+=1
    elif h[L] == 0:
        L+=1
print(cnt)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

n=int(input())  #数値入力 「N」だけの入力のとき
h=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
cnt=0
L=0
while L != len(h):
    if h[L] != 0:
        for r in range(L+1 ,len(h)):
            if h[r] == 0:
                #do it
                for k in range(L,r):
                    h[k]-=1
                cnt+=1
                break
        else:
            r = len(h)
            for k in range(L,r):
                h[k]-=1
            cnt+=1
    elif h[L] == 0:
        L+=1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

h_max = max(inp_h)
h_len = len(inp_h)

ind = [0]*h_len

h_bool = [True if ind[i] == inp_h[i] else False for i in range(h_len)]

def make_index(h_bool):
    try:
        index = h_bool.index(False)
    except:
        index = -1
    return index

num = 0
while True:
    index = make_index(h_bool)
    index_list = []
    if index != -1:
        while True:
            if index < h_len:
                if h_bool[index] == False:
                    index_list.append(index)
                    index += 1
                elif h_bool[index] == True:
                    ind = [ind[i]+1 if i in index_list else ind[i] for i in range(h_len)]
                    h_bool = [True if ind[i] == inp_h[i] else False for i in range(h_len)]
                    num += 1
                    break
            elif index >= h_len:
                ind = [ind[i]+1 if i in index_list else ind[i] for i in range(h_len)]
                h_bool = [True if ind[i] == inp_h[i] else False for i in range(h_len)]
                num += 1
                break
    else:
        break
    
print(num)

Submission status: Accepted

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

def rWater(L):
#  print (L)
  if len(L) == 1:
    return L[0]
  if L[0] == 0:
    return rWater(L[1:])
  if L[-1] == 0:
    return rWater(L[:-1])
  if 0 in L:
    return rWater(L[:L.index(0)]) + rWater(L[L.index(0)+1:])
  return rWater(list(map(lambda x: x-1,L)))+1
print (rWater(h))

Submission status: Accepted

Solution code:
def cal(n):
    if len(n)==1:
        return n[0]
    if len(n)==0:
        return 0
    if 0 in  n:
        indexes=n.index(0)
        ind1=n[indexes+1:]
        ind2=n[:indexes]
        return cal(ind2)+cal(ind1)
    else:
        mins=min(n)
        lists=[n[j]-mins for j in range(len(n))]
        return cal(lists)+mins
num=int(input())
listw=list(map(int,input().split()))
print(cal(listw))

Submission status: Accepted

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

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

Submission status: Accepted

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

def f(x):
  m=min(x)
  n=len(x)
  if n>1:
    x=[x[i]-m for i in range(n)]
    temp=devider(x)
    for t in temp:
      m+=f(t)
  return m

def devider(y):
  output=[]
  n=len(y)
  i=0
  while i<n:
    while i<n and y[i]==0:
      i+=1
      
    if i>=n:
      break
      
    j=i+1
    while j<n and y[j]!=0:
      j+=1
      
    if j-i==1:
      output.append([y[i]])
    else:
      output.append(y[i:j])
      
    i=j
    
  return output

h=devider(h)
ans=0
for htemp in h:
  ans+=f(htemp)
  
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=[int(i) for i in input().split()]
hana=[0]*n
ans=0
left=right=0
while hana!=h:
    for i in range(n):
        right=i
        if hana[i]==h[i]:
            if left<right:
                for j in range(left,right):
                    hana[j]+=1
                ans+=1
            left=right+1
        elif i==n-1:
            if left<n:
                for j in range(left,n):
                    hana[j]+=1
                ans+=1
    left=right=0
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
if sum(h) == 0:
    print(0)
    exit()
ans = 0
flag = False
while True:
    if h[0] == 0:
        flag = True
    for i in range(N):
        if h[i] == 0 and flag == True:
            continue
        elif h[i] == 0 and flag == False:
            break
        h[i] -= 1
        flag = False
    ans += 1
    if sum(h) == 0:
        break
print(ans)

Submission status: Accepted

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

ans = 0
h_min = min(h_list)

ans += h_min

for i in range(N):
    h = h_list[i]
    if i == 0:
        ans += h - h_min
        pre = h
    else:
        if h >= pre:
            ans += h - pre
            pre = h
        else:
            pre = h
print(ans)

Submission status: Accepted

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

ans = 0
h_min = min(h_list)

ans += h_min

pre = h_min
for i in range(N):
    h = h_list[i]
    if i == 0:
        ans += h - h_min
        pre = h
    else:
        if h >= pre:
            ans += h - pre
            pre = h
        else:
            pre = h
print(ans)

Submission status: Accepted

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

cnt = 0

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
global count
count = 0
def calc(data):
    if len(data) == 0:
        return 0
    if min(data) > 0:
        data = list(map(lambda x:x - 1, data))
        global count
        count += 1
        calc(data)
    else:
        k = data.index(min(data))
        calc(data[0:k])
        calc(data[k+1:])
calc(h)
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
tmp = 0

for i in range(n):
    ans += h[i]
    tmp = int(h[i])
    for j in range(i, n):
        if h[j] - tmp >= 0:
            h[j] -= tmp
        else:
            tmp = int(h[j])
            h[j] = 0

print(ans)

Submission status: Accepted

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

ans = 0
tmp = 0

for i in range(n):
    ans += h[i]
    tmp = copy.deepcopy(h)
    tmp_ = tmp[i]
    for j in range(i, n):
        if h[j] - tmp_ >= 0:
            h[j] -= tmp_
        else:
            tmp_ = tmp[j]
            h[j] = 0

print(ans)

Submission status: Accepted

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

out = 0

def divide_list_into_zero(l):
  ret = []
  tmp = []
  for i in l:
    if i != 0:
      tmp.append(i)
    elif len(tmp) is not 0:
      ret.append(tmp)
      tmp = []
  if len(tmp) is not 0:
    ret.append(tmp)
  return ret

def substract(l):
  global out
  out += 1
  return [i-1 for i in l]

def watering(l):
  tmp_1 = substract(l)
  tmp_2 = divide_list_into_zero(tmp_1)
  for child in tmp_2:
    watering(child)

for l in divide_list_into_zero(h):
  watering(l)
print(out)

Submission status: Accepted

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

water = 0
before = 0
for i in lis_h:
    if i > before:
        water += i - before
    before = i

print(water)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
while True:
    decrease_cnt = 0
    for j in range(N):
        if h[j] > 0:
            h[j] -= 1
            decrease_cnt += 1
        elif decrease_cnt > 0:
            break
    if decrease_cnt == 0:
        print(cnt)
        break
    else:
         cnt += 1

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def test():
    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]
    print(ans)


if __name__ == "__main__":
    test()

Submission status: Accepted

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

ans=0
while sum(h)>0:
  for i in range(N):
    if i<N-1 and h[i]>0 and h[i+1]==0:
      ans+=1
    else:
      if i==N-1 and h[i]>0:
        ans+=1
    if h[i]>0:
      h[i]-=1
  #print(h)
print(ans)

Submission status: Accepted

Solution code:
def countA(A):
    global cnt
    if len(A)==1:
        cnt += A[0]
    else:
        amin = min(A)
        A = [A[i]-amin for i in range(len(A))]
        cnt += amin
        while A:
            if A[0]==0:
                A.pop(0)
            elif A[-1]==0:
                A.pop()
            else:break
        B = []
        for i in range(1,len(A)):
            if A[i]==0 and A[i-1]==0:
                B.append(i)
        C = []
        for i in range(len(A)):
            if i not in B:
                C.append(A[i])
        C0 = []
        for i in range(len(C)):
            if C[i]==0:
                C0.append(i)
        if len(C0)==0 and len(C)>0:
            countA(C)
        else:
            cur = 0
            for c in C0:
                countA(C[cur:c])
                cur = c+1
            if cur<len(C):
                countA(C[cur:])
            
N = int(input())
H = list(map(int,input().split()))
cnt = 0
countA(H)
print(cnt)

Submission status: Accepted

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

ans=0
while sum(h)>0:
  for i in range(N):
    if i<N-1 and h[i]>0 and h[i+1]==0:
      ans+=1
    else:
      if i==N-1 and h[i]>0:
        ans+=1
    if h[i]>0:
      h[i]-=1
  #print(h)
print(ans)

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:
        break
      else:
        H[j]-=1
    ans+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
data = [int(i) for i in input().split()]
z_frug = 1
counter = 0
point = 0
i = 0
a = 0
while sum(data)!= 0:
  a +=1
  #print(data,counter,i,point)
  try:
    y = data[i]
  except:
    pass
  if y == 0:
    z_frug = (z_frug+1)%2
  if z_frug == 0 or i==N:
    if counter != 0:
      x = min(data[i-counter:i])
      point += x
      for j in range(i-counter,i):
        #print(a)
        data[j] -= x
      i = 0
      counter = 0
    else:
      i += 1
    z_frug = 1
    #break
  else:
    counter += 1
    i += 1
    
print(point)

Submission status: Accepted

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

for i in range(N):
    while H[i] != 0:
        for j in range(i,N):
            if H[j] == 0:
                break
            else:
                H[j] -= 1
        count += 1
    
print(count)

Submission status: Accepted

Solution code:
import numpy as np

input()
H = np.array([int(h) for h in input().split()])
k = np.array([1, -1])
c = 0
while (H >= 1).any():
    m = H[H >= 1].min()
    n_blocks = (np.convolve(H >= 1, k))
    c += (n_blocks < 0).sum() * m
    H = np.maximum(H - m, 0)

print(c)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC116 C - Grand Garden
import numpy as np
n = int(input())
h = list(map(int,input().split()))
l = [9]*n
t = 0
x = 0
if n == 1:
    print(h[0])
    exit()

while max(h) > 0:
    if x != 0:
        for i in range(n):
            h[i] -= 1

    for i in range(n):
        if h[i] > 0:
            l[i] = 1
        else:
            l[i] = 0

    for i in range(n-1):
        if i == 0 and l[0] == 1:
            t += 1
        if l[i+1]-l[i] == 1:
            t += 1
    x += 1

print(t)

Submission status: Accepted

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

for _ in range(max(h)):
    vi = [i for i in range(n) if h[i] > 0]
    cnt = 1
    for i in range(len(vi)-1):
        cnt += (vi[i] != vi[i+1] -1)
    ans += cnt
    h = list(map(lambda x: x - 1, h))
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = [int(x) for x in input().split()]
ans = 0
for i in range(N):
  mi = H[i]
  ans += mi
  for j in range(i+1,N):
    if mi<=H[j]:
      H[j] -=mi
    else:
      mi = min(mi,H[j])
      H[j]=0
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
if __name__ == '__main__':
    N = int(input())
    H = np.array(list(map(int, input().split())))
    
    count = 0
    while H.sum() != 0:
        start = 0
        for i in range(N):
            if H[i] == 0:
                end = i
                if len(H[start:end]) > 0:
                    num = H[start:end].min()
                    H[start:end] -= num
                    count += num
                start = i+1
        if len(H[start:i+1]) > 0:
            num = H[start:i+1].min()
            H[start:i+1] -= num
            count += num
    print(count)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
# 水やり = (l<=i<=r)区間のh[i]を-1すると考える
# hの要素が全て0になるまで繰り返す
while sum(h) != 0:
    # 初期化
    left = float("inf")
    right = float("inf")
    # 連続した自然数の区間を見つける
    # 左端番号leftの設定
    for i in range(n):
        if h[i] > 0:
            left = i
            break
    # 右端番号rightの設定
    for j in range(i + 1, n):
        if h[j] <= 0:
            right = j
            break
    if right == float("inf"):
        right = n
    # 見つけた区間の全ての要素を-1
    for i in range(left, right):
        h[i] -= 1
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
while sum(h) != 0:
    left = float("inf")
    right = float("inf")
    for i in range(n):
        if h[i] > 0:
            left = i
            break
    for j in range(i + 1, n):
        if h[j] <= 0:
            right = j
            break
    if right == float("inf"):
        right = n
    for i in range(left, right):
        h[i] -= 1
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n=int(input())
H=list(map(int,input().split()))
count=0
while True:
    start,end=0,0
    for i in range(n):
        if H[i]!=0:
            H[i]-=1
            start=1
            if end:
                count+=1
                end=0
        else:
            if start:
                end=1
    if not start:
        break
    count+=1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
count=0
i=0
while(i<n):
  if(h[i]==0):
    i=i+1
    continue
  j=i
  while(j<n):
    if(h[j]==0):
      break
    if(h[j]>0):
      h[j]=h[j]-1
      j=j+1
      continue
  count=count+1
  #print(h)
print(count)

Submission status: Accepted

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

while True:
    if 0 not in h:
        for i in range(N):
            h[i] -= 1
        ans += 1
    elif set(h) == {0}:
        break
    else:
        tmp = 0
        for i in range(N):
            if h[i] > 0:
                h[i] -= 1
                tmp = 1
            elif tmp == 0:
                continue
            else:
                break
        ans += tmp
print(ans)

Submission status: Accepted

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

count = a[0]

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

print(count)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
h = list(map(int, input().split()))
h = np.array(h)
h_diff = h[1:] - h[:N-1]
count = 0
for i in range(N-1):
    count -= min(h_diff[i], 0)
    
print(count + h[N-1])

Submission status: Accepted

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

l=r=0
count=0


while any(h)==True:
    for i in range (n):
        if h[i]!=0:
            l = i
            break
    if all(h[l+1:n])==True:
        r = n-1
    else:
        for k in range (l+1,n):
            if h[k] == 0:
                r = k-1
                break

    for s in range (l,r+1):
        h[s] = h[s]-1

    count += 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
H = [int(i) for i in input().split()]
Min = min(H)
ans = Min
for i in range(N):
    H[i] -= Min
dif = []
pre = 0
for i in range(N):
    if pre!=H[i] and H[i]-pre>0:
        dif.append(H[i]-pre)
    else:
        dif.append(0)
    pre = H[i]

print(ans+sum(dif))

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n = I()
L = [-INF] + LI()
flag = True
cnt = 0
j = 0
while flag:
    flag = False
    for i in range(n):
        if L[i] <= j < L[i+1]:
            cnt += 1
            flag = True
    j += 1



print(cnt)

Submission status: Accepted

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

I = []
for _ in range(max(H)):
    I.append([1 if h > 0 else 0 for h in H])
    H = [h-1 if h > 0 else 0 for h in H]
    
for i in I:
    j = [l for l in ''.join([str(k) for k in i]).split('0') if l != '']
    ans += len(j)
    
print(ans)

Submission status: Accepted

Solution code:
def resolve():
    n = int(input())
    h = list(map(int, input().split()))
    res = 0
    for height in range(1, max(h) + 1):
        flag = False
        for i in range(n):
            if h[i] >= height and not flag:
                flag = True
                res += 1
            elif h[i] < height:
                flag = False
    print(res)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

if N==1:
    print(h[0])
    exit()

mi=min(h)
p=0
h0=h[0]
c=h0-mi
ans=mi+h0
for i in range(1,N):
    if h0<h[i]:
        c+=h[i]-h0
    elif h0>h[i]:
        c=h0-min(h[p:i])
        ans+=c
        p = i
        c=h[i]-mi
    h0=h[i]

ans+=c-min(h[p:])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    h = [int(i) for i in input().split()]
    ans = 0
    mul = h[1:N-1].count(0) + 1
    for i in range(max(h)):
        ans += 1 * mul
        z_cnt = 0
        flag = False
        for j in range(N):
            h[j] = (h[j]-1 if h[j] != 0 else h[j]) 
            if h[j] != 0:
                flag = True
            elif j == N-1 and h[j] == 0 and not flag:
                z_cnt -= 1
            elif j != 0 and j != N-1 and h[j] == 0 and flag:
                z_cnt += 1
                flag = False
        mul = z_cnt + 1


    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
H = np.array([int(v) for v in input().split()])
hm = np.max(H)
cnt = 0
for h in range(1, hm + 1):
    hh = H >= h
    f = False
    for n in range(N):
        # H[n] がh以上の区間が連続する回数を数える
        if not f and hh[n]:
            cnt += 1
            f = True
        elif not hh[n]:
            f = False
print(cnt)

Submission status: Accepted

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

cur = 0
total = 0
while h != [0] * N:
    for i in range(cur, N):
        if cur == i and h[i] == 0:
            cur += 1
        elif h[i] == 0:
            if cur == i - 1:
                total += h[i - 1]
                h[i - 1] = 0
            else:
                min_h = min(h[cur:i])
                total += min_h
                for j in range(cur, i):
                    h[j] -= min_h
        elif i == N - 1:
            min_h = min(h[cur:N])
            total += min_h
            for j in range(cur, N):
                h[j] -= min_h
print(total)