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 = [int(x) for x in input().split()]
h.append(0)
base = [0 for i in range(N+1)]
ans = 0

while h != base:
    ans += 1
    f = h.index(max(h))
    i = 0
    while h[f+i] == h[f+i+1]:
        h[f+i] -= 1
        i += 1
    h[f+i] -= 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(MAX):
  for j in range(n):
    if progress[j] < h[j]:
      progress[j] += 1
      if j+1 >= n:
        ans += 1
      else:
        if progress[j+1] == h[j+1]:
          ans += 1

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import numpy as np

N = int(stdin.readline().rstrip())
H = [int(x) for x in stdin.readline().rstrip().split()]

def func(h_list, now_h):
  if len(h_list) == 0:
    return 0
  min_idx = [i for i, x in enumerate(h_list) if x == min(h_list)]
  min_h = h_list[min_idx[0]]
  ans = min_h - now_h
  l_idx = 0
  for i in min_idx:
    ans += func(h_list[l_idx:i],min_h)
    l_idx = i+1
  ans += func(h_list[l_idx:],min_h)
  return ans

print(func(H,0))

Submission status: Accepted

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

for i in range(MAX):
  for j in range(n):
    if progress[j] < h[j]:
      progress[j] += 1
      if j+1 >= n:
        ans += 1
      elif j+1 < n:
        if progress[j+1] == h[j+1]:
          ans += 1

print(ans)

Submission status: Accepted

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

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]
print(ans+H[-1])

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N = read_int()[0]
flower = read_int()

ans = 0
def rec(l,r):
    minf = float('INF')
    for i in range(l,r):
        minf = min(minf, flower[i])

    # watering (reverse)
    zero_index = []
    for i in range(l,r):
        flower[i] -= minf
        if flower[i] == 0:
            zero_index.append(i)
    global ans
    ans += minf

    zero_index.append(r)
    zero_index.insert(0,l-1)
    for i in range(len(zero_index) - 1):
        if zero_index[i] + 1 != zero_index[i + 1]:
            rec(zero_index[i] + 1, zero_index[i + 1])


if __name__ == "__main__":
    rec(0, N)
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
flowers_num = int(input())
target_heights = list(map(int, input().split()))

max_height = max(target_heights)

watering_sum = 0
for row_i in range(1, max_height + 1):
    should_water = False

    for target_height in target_heights:
        if row_i <= target_height:
            should_water = True
        elif should_water:
                watering_sum += 1
                should_water = False

    if should_water:
        watering_sum += 1
        should_water = False

print(watering_sum)

Submission status: Accepted

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

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

Submission status: Accepted

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

# 再帰的にカウントしていく
def count(h):
    if len(h) != 0:
        output = 0
        min_elem = min(h)
        output += min_elem
        for index in range(0, len(h)):
            h[index] -= min_elem
        tmp_h = []
        for index in range(0, len(h)):
            if (h[index] > 0):
                tmp_h.append(h[index])
            else:
                output += count(tmp_h)
                tmp_h = []
        output += count(tmp_h)
        return output
    else:
        return 0

h = H
print(count(h))

Submission status: Accepted

Solution code:
#coding:utf-8
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 = [int(_) for _ in input().split()]
ResSum = 0
pre = 0

for i in h:
    if i > pre:
        ResSum += i - pre
    pre = i

print(ResSum)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
b = []
inf = 10**10
start = 0
end = 0
ans = 0

for ii in range(inf):
    if max(h) == 0:
        break

    while h[0] == 0:
        del h[0]

    for i in range(len(h)):
        if h[i] == 0:
            end = i
            #print(b)
            break
        end = len(h)
        b.append(h[i])

    #print(b)
    sa = min(b)
    b.clear()
    #print(sa)

    for j in range(end):
        h[j] = h[j] - sa
        #print(h)

    ans = ans + sa
        

print(ans)

Submission status: Accepted

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

pre = 0
count = 0
for i in range(N):
  if(H[i] > pre):
    count += H[i] - pre
  pre = H[i]
print(count)
###例題からの答えの規則性見つけたタイプの解法

Submission status: Accepted

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

pre = 0
cnt = 0
for h in H:
	if h > pre:
		cnt += h-pre
	pre = h

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
list = list(map(int,input().split()))
pre = 0
sum = 0

for i in list:
    if i > pre:
        sum += i-pre
    pre = i

print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

h_max = max(hl)
c=0
for i in range(1, h_max+1):
    hit = False
    for j in range(len(hl)):
        if hl[j] >= i: #hit
            if j==len(hl)-1:
                c+=1
            hit=True
        else:
            if hit==True:
                c+=1
            hit=False

print(c)

Submission status: Accepted

Solution code:
N = int(input())

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

last = 0
ans = 0
for h in H:
    if h >= last:
        ans += h - last
    else:
        if last == 0:
            ans += h
    last = h

print(ans)

Submission status: Accepted

Solution code:
import copy


n = int(input())
h = list(map(int, input().split()))
a = 0
i = 0
while i < n:
    if h[i] > 0:
        j = copy.copy(i)
        k = h[i]
        while h[j] > 0:
            if h[j] < k:
                k = h[j]
            j += 1
            if j == n:
                break
        for l in range(i, j):
            h[l] -= k
        a += k
    else:
        i += 1
print(a)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
H = [0]+list(map(int, input().split()))
# same as [0]+[int(h) for h in input().split()]

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

print(a)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
h = np.array([int(i) for i in input().split()])
count=0
 
while(np.max(h)>0):
    start=0
    i=0
    while(h[i]==0):
        i+=1
    start=i
    while(h[i]!=0 and i<N-1):
        i+=1
    if h[i]==0:
        h[start:i]=h[start:i]-1
    else:
        h[start:]=h[start:]-1
    count+=1
 
print(count)

Submission status: Accepted

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

ans = 0
former = 0
for flower in h:
    if former < flower:
        ans += flower - former

    former = flower
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h_list = [int(i) for i in input().split()]
h_1 = h_list[1:] + [0]
count = 0
for i in range(len(h_list)):
    count += max(h_list[i] - h_1[i], 0)
print(count)

Submission status: Accepted

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

count = 0
flag = True

def split_zero(hs):
    lens = []
    tmp = []

    for i in range(0,N):
        if hs[i] == 0 and len(tmp) > 0:
            lens.append(tmp)
            tmp = []
        elif hs[i] > 0:
            tmp.append(hs[i])

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

    return len(lens)

def dec_array(hs):
    for i in range(0,N):
        hs[i] -= 1
        hs[i] = max(hs[i],0)
    return hs


count = 0
while any([h > 0 for h in hs]):
    count += split_zero(hs)
    hs = dec_array(hs)

print(count)

#00100111

Submission status: Accepted

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

# N =  8
# h = np.array(list(map(int, "4 23 75 0 23 96 50 100".split())))

a = np.zeros(N)
h_min = min(h)
h_max = max(h)
n_loop = h_max - h_min

cnt = 0
for zzz in range(n_loop):
    tmp = (h >= h_min+1+zzz)*1
#     print(tmp)
    for idx, i in enumerate(tmp):
        if idx==0:
            first = i
            a0 = i
            continue
            
        if i==a0:
            continue
        else:
            if i==1:
                cnt+=1
            a0 = i
    else:
        if first==1:
            cnt+=1
            
print(cnt+h_min)

Submission status: Accepted

Solution code:
import numpy as np
global count

def recursive_f(h):
    global count
    hmin = min(h)
    count += hmin
    h = h - hmin
    
    zeroidx = list(h).index(0)
    if len(h) == 1:
        return 0
    else :
        if zeroidx > 0 & zeroidx < len(h)-1:
            h1 = h[0:zeroidx]
            h2 = h[zeroidx+1:len(h)]
            if len(h1) > 0:
                recursive_f(h1)
            if len(h2) > 0:
                recursive_f(h2)
            
        elif zeroidx == 0:
            h2 = h[1:]
            if len(h2) > 0:
                recursive_f(h2)
            
        elif zeroidx == len(h)-1:
            h1 = h[0:len(h)-1]
            if len(h1) > 0:
                recursive_f(h1)
            
        


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

h = np.array(h)
count = 0
a = 0
recursive_f(h)
print(count)

Submission status: Accepted

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

while(max(h)>0):
    while(h[i]==0):
        i+=1
    start=i
    while(h[i]!=0 and i<N-1):
        i+=1
    if h[i]==0:
        h[start:i]=h[start:i]-1
    else:
        h[start:]=h[start:]-1
    answer+=1
    start=0
    i=0

print(answer)

Submission status: Accepted

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

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

Submission status: Accepted

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

def mizu(h):
    c = 0
    if h == []:
        return 0
    else:
        c = min(h)
        h0 = list(map(lambda x : x - min(h), h))
        if h0[0] == 0:
            h0.remove(0)
            c = c + mizu(h0)
        else:
            for i in range(len(h0)):
                if h0[i] == 0:
                    h1 = h0[0:i]
                    h2 = h0[i:len(h0)]
                    break
            c = c + mizu(h1) + mizu(h2)
        return c

print(mizu(h))

Submission status: Accepted

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

Submission status: Accepted

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

opr_count = 0
h_highest = max(h)

while any(h):
    flag = 0
    for i, v in enumerate(h):
        if v == h_highest:
            h[i] -= 1
            if not flag:
                opr_count += 1
                flag = 1
                continue
        else:
            flag = 0
    h_highest -= 1

print(opr_count)

Submission status: Accepted

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

cnt = 0
while 1:
    if len(h) == 0:
        break
    elif h[0] == 0:
        h.pop(0)
    else:
        i = h.index(0)
        h[:i] = [tmp -1 for tmp in h[:i]]
        cnt +=1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
cnt = 0
def minus1(arr) :
  for i in range(len(arr)) :
    if(arr[i] != 0) : arr[i] -= 1
  return 0

def discover0(arr) :
  cnt = 0 #重複
  for i in range(1, len(arr)) :
    if(arr[i-1] == 0 and arr[i] == 0):
      cnt += 1
  if(arr[0] == 0) : cnt += 1
  if(arr[len(arr)-1] == 0) : cnt += 1
  return arr.count(0) - cnt

for i in range(max(h)) :
  cnt += discover0(h)
  minus1(h)
  cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

c = 0
i = 0

while sum(h) > 0:
    if h[i] > 0:
        j = i
        c = c + 1
        while j < n and h[j] > 0:
            h[j] = h[j] - 1
            j = j + 1
    else:
        i = i + 1

print(c)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
def f(arr):
    if(sum(arr) == 0):
        return 0
    while(arr[0] == 0):
        arr.pop(0)
    while(arr[-1] == 0):
        arr.pop()
    if (min(arr) == 0):
        index = arr.index(0)
        return f(arr[0:index]) + f(arr[index: len(arr)])
    else:
        minval = min(arr)
        if (len(arr) == 1):
            return minval
        else:
            arr = list(map(lambda x: x- minval, arr))
            if(sum(arr)==0):
                return minval
            else:
                return f(arr) + minval
print(f(h))

Submission status: Accepted

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

for i in range(N):
    if i == 0:
        res += H[0]
    else:
        if H[i-1] < H[i]:
            res += H[i] - H[i-1]

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

node = sum(h)
path = 0
prev = h[0]
for cur in h[1:]:
    path += min(prev, cur)
    prev = cur
print(node - path)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import bisect

def recursive(h):
    if len(h) == 0:
        return 0
    pos_zero = [-1]
    min_h = min(h)
    count = min_h
    for i in range(len(h)):
        h[i] -= min_h
        if h[i] == 0:
            pos_zero.append(i)
    
    pos_zero.append(len(h))
    
    if len(pos_zero) > 0:
        for i in range(0, len(pos_zero)-1):
            count += recursive(h[pos_zero[i]+1:pos_zero[i+1]])

    return count
    

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

count = recursive(h)

print(count)

Submission status: Accepted

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

c = 0
while any(x > 0 for x in h):
    b = False
    for i in range(len(h)):
        if h[i] > 0:
            b = True
            h[i] -= 1
        elif b:
            break
    if b:
        c += 1
print(c)

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
h = np.array([0] + [int(item) for item in input().split()] + [0])

count = 0
while np.count_nonzero(h) > 0:
  zid = np.where(h==0)[0]
  for item in range(len(zid)-1):
    if zid[item+1] - zid[item] > 1:
      h[zid[item]+1:zid[item+1]] -= 1
      count += 1
print(count)

Submission status: Accepted

Solution code:
from itertools import*
from numpy import*
n,*a=map(int,open(0).read().split())
if set(a)=={0}:
    print(0)
    exit()
a=array(a)
count=0
ans=0
while 1:
    count=min(a[a!=0])
    ans+=count*sum(k for k,v in groupby(a>0)if k==True)
    a=a-min(a[a!=0])
    a[a<0]=0
    if sum(a)==0:
        break
print(ans)

Submission status: Accepted

Solution code:
from itertools import*
from numpy import*
n,*a=map(int,open(0).read().split())
a=array(a)
count=0
ans=0
while 1:
    if sum(a)==0:break
    count=min(a[a!=0])
    ans+=count*sum(k for k,v in groupby(a>0)if k==True)
    a=a-min(a[a!=0])
    a[a<0]=0
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

count = 0

for h in sorted(range(1, 101), reverse=True):
    now = 0
    for h_2 in li_h:
        if h_2 >= h and now < h:
            count += 1
        now = h_2

print(count)

Submission status: Accepted

Solution code:
N=int(input())
h=list(map(int,input().split()))
H=[0]*N#花の高さ
c=0#水やり回数
while H!=h:
    l=0
    r=N
    for i in range(N):
        if H[i]!=h[i]:
            l=i
            break
    for i in range(N):
        if H[i]==h[i]:
            r=i
            if r>l:
                break
            else:
                r=N
    for i in range(l,r):
        H[i]+=1
    c+=1
print(c)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
hmax = max(h)
count = 0
while hmax > 0:
    maxindex = [i for i,x in enumerate(h) if x == hmax]
    continueNumberCount = 0
    for i in range(len(maxindex)-1):
        if maxindex[i] == maxindex[i+1] - 1:
            continueNumberCount += 1
    count += len(maxindex) - continueNumberCount
    for i in maxindex:
        h[i] -= 1
    hmax -= 1
print(count)

Submission status: Accepted

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

count = 0
while True:
    flg_s = False
    for i in range(n):
        if flg_s and h[i] == 0:
            break
        if h[i] > 0:
            flg_s = True
            h[i] -= 1

    if not flg_s:
        break
    count += 1

print(count)

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(h[i]!=0):
            l = i
            r = N
            break
    for j in range(i+1, N):
        if(h[j]==0):
            r = j
            break

    x = min(h[l:r])
    for i in range(l, r):
        h[i] -= x
    ans += x
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] > H[i+1]):
    cnt += H[i] - H[i+1]
print(cnt+H[-1])

Submission status: Accepted

Solution code:
import re

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

def particles(ht):
	global N
	global H
	s = ''
	for i in range(N):
		if H[i] >= ht:
			s += '#'
		else:
			s += '.'
	return s

result = 0

for j in range(100):
	if particles(j+1).count('#') == 0:
		break
	else:
		result += len(re.compile(r'#+').findall(particles(j+1)))

print(result)

Submission status: Accepted

Solution code:
a=int(input())
A=list(map(int,input().split()))+[0]
s=0
m=0
for i in A:
  s+=abs(m-i)
  m=i
print(s//2)

Submission status: Accepted

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

def water(H):
	if H == []:
		return 0
	
	minH = min(H)
	H_temp = [x - minH for x in H]
	index0 = H_temp.index(0)
	return water(H_temp[0:index0]) + water(H_temp[index0+1:]) + minH

print(water(H))

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
def solve(ls):
    if ls == []:
        return 0
    m = min(ls)
    tmp = [x - m for x in ls]
    i = tmp.index(0) 
    return solve(tmp[0:i]) + solve(tmp[i+1:]) + m
print(solve(h))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

su = 0

co = 0

for i in range(max(a)):
    for j in range(n):
        if a[j] > 0 and co == 0:
            co += 1
            su += 1
        if co  >=  1:
            if a[j] == 0:
                co = 0
            else:
                a[j] -= 1
    co = 0

print(su)

Submission status: Accepted

Solution code:
n = int(input())
H = list(map(int,input().split()))
ans = 0
lst = 0
for h in H:
    if(h > lst):
        ans += h-lst
    lst = h

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
H = list(map(int,input().split()))
ans = 0
size = sum(H)
while(size > 0):
    count = 0
    temp = 0
    for h in H:
        if(h == 0 and temp > 0):
            ans += 1
            break
        elif(h != 0):
            temp += 1
            H[count] -= 1
            size -= 1
            if(count == n-1):
                ans += 1
        count += 1
print(ans)

Submission status: Accepted

Solution code:
def minplus(array , count):
    if len(array) == 0:
        return count
    if len(array) == 1:
        return array[0] + count
    count += min(array)
    temparray = [y - min(array) for y in array]
    zeroindex = temparray.index(0)
    return minplus(temparray[:zeroindex],count) + minplus(temparray[zeroindex + 1:],0)

N =  int(input())
hn = [int(x) for x in input().split() ]

print(minplus(hn,0))

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:
def grand_garden(N: int, H: list) -> int:
    before = 0
    op = 0
    for h in H:
        if before < h:
            op += h - before
        before = h

    return op


if __name__ == "__main__":
    N = int(input())
    H = [int(s) for s in input().split()]

    ans = grand_garden(N, H)
    print(ans)

Submission status: Accepted

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

count = 0
while True:
    left = -1
    right = -1
    for i, hh in enumerate(h):
        if hh > 0:
            if left < 0:
                left = i
            right = i
        elif right >= 0:
            break
    if left < 0:
        break
    count += 1
    for i in range(left, right + 1):
        h[i] -= 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

while max(h):
    mx = max(h)
    on = False

    for i in range(N):
        if h[i] == mx:
            h[i] -= 1
            if not on:
                ans += 1
            on = True
        else:
            on = False

print(ans)

Submission status: Accepted

Solution code:
def del_double(lst):
    bef = -1
    ret =[]
    for i in lst:
        if bef != i:
            ret.append(i)
            bef=i
    return ret

def f(lst):
    m_ = max(lst)
    idx = lst.index(m_)
    l_idx = idx-1
    if l_idx >=0:
        l = lst[l_idx]
    else:
        l = -1
    
    r_idx = idx+1
    if r_idx < len(lst):
        r = lst[r_idx]
    else:
        r =-1
        
    #print(idx,l_idx,r_idx,"l,r=",l,r,lst)
    # time.sleep(0.5)
    if r==-1 and l ==-1:
        return m_ 
    else:
        if r>l:
            lst[idx] = r
            #print("\t",m_ - r,m_,r)
            return m_ - r + f(del_double(lst))
        else:
            lst[idx] = l
            #print("\t",m_ - l,m_,l)
            return m_ - l + f(del_double(lst))
N = int(input())  
H = list(map(int,input().split()))

print (f(del_double(H)))

Submission status: Accepted

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

ans = 0
height = 1
while(True):
    endFlag = True
    waterFlag = False
    for h in hList:
        if h >= height:
            if not waterFlag:
                ans += 1
            endFlag = False
            waterFlag = True
        elif h < height:
            waterFlag = False
    height += 1
    if endFlag:
        break

print(ans)

Submission status: Accepted

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

def n_water(p_arr):
    if len(p_arr) == 0:
        return 0
    if 0 in p_arr:
        i_0 = p_arr.index(0)
        return n_water(p_arr[:i_0])+n_water(p_arr[i_0+1:])
    p_arr = [x-1 for x in p_arr]
    return n_water(p_arr)+1

print(n_water(h_arr))

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
count = 0
for i in range(1, 101):
    flag = False
    for j in range(N):
        if h[j] >= i and flag == False:
            flag = True
            count += 1
        elif h[j] < i and flag == True:
            flag = False
print(count)

Submission status: Accepted

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

def rec(H,acc,l,r):
    if r - l == 1:
        return (H[l] - acc) 
    mn = min(H[l:r])
    mx = max(H[l:r])
    if mn == mx:
        return mn - acc
    flg = False
    ans = (mn - acc)
    L = 0
    for i in range(l,r):
        if H[i] != mn:
            if not flg:
                L = i
                flg = True

            if i == r-1:
                ans += rec(H,mn,L,r)
        else:
            if flg:
                ans += rec(H,mn,L,i)
            flg = False
    
    return ans
print(rec(h,0,0,N))

Submission status: Accepted

Solution code:
def times(hs):
    if len(hs) == 0:
        return 0
    if len(hs) == 1:
        return hs[0]

    minh = min(hs)
    mini = hs.index(minh)

    hs = [h - minh for h in hs]

    return minh + times(hs[:mini]) + times(hs[mini+1:])

_ = input()
hs = list(map(int, input().split()))

print(times(hs))

Submission status: Accepted

Solution code:
n=int(input())
H=list(map(int,input().split()))
ans=0
i=0
while sum(H)>0:
  ans+=1
  flag=False
  for i in range(n):
    if H[i]>0:
      H[i]-=1
      flag=True
    elif flag is True and H[i]==0:
      break
  # print(H)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
H = [0]+list(map(int, input().split()))
dp = [0]*(n+1)

dp[0] = 0
for i in range(1,n+1):
  if H[i] > H[i-1]:
    dp[i] = dp[i-1] + (H[i] - H[i-1])
  else:
    dp[i] = dp[i-1]
print(dp[n])

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
hs = input().split()
hs = np.array([int(h) for h in hs])

# hs = np.array([0,4,23,75,23,0,96,50,100])

def split_array(hs):
    split_indexes = np.where(hs==0)[0]
    n = len(split_indexes)
    split_list = []
    if n > 0:
        split_list.append(hs[0:split_indexes[0]])
        if n > 1:
            for i in range(n-1):
                split_list.append(hs[split_indexes[i]+1:split_indexes[i+1]])
        split_list.append(hs[split_indexes[-1]+1:])
    return split_list

def make_shower(l):
    count = l.min()
    return count, l-count

all_count = 0
c, hs = make_shower(hs)
split_list = split_array(hs)
all_count += c
zero_count = 0
#全部のリストが空になったところで終了
while True:
    zero_count = 0
    split_list_new = []
    for l in split_list:
        if len(l) == 0:
            zero_count += 1
            continue
        count, l_showered = make_shower(l)
        # print("count",count,l,l_showered)
        all_count += count
        split_list_ = split_array(l_showered)
        split_list_new.extend(split_list_)

    len_split_list = len(split_list) # リストの長さを取っておく
    split_list = [l for l in split_list_new] #代入
    
    if zero_count == len_split_list:
        break
print(all_count)

Submission status: Accepted

Solution code:
N = int(input())
h = input()

h_n = [int(x) for x in h.split()]
now = []
for i in range(N):
    now.append(0)

count = 0
while True:
    add_range = [0, N]

    for i in range(0,N):
        if (now[i] < h_n[i]):
            add_range[0] = i
            break
    else:
        break
    for i in range(add_range[0]+1,N):
        if (now[i] >= h_n[i]):
            add_range[1] = i
            break
    for i in range(add_range[0], add_range[1]):
        now[i] += 1
    count += 1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
ans = 0
while max(h) > 0:
    k = 0
    flag = False
    for i in range(N):
        if h[i] > 0:
            h[i] -= 1
            flag = True
        elif flag:
            k += 1
            flag = False
    if flag:
        k += 1
    ans += k

print(ans)

Submission status: Accepted

Solution code:


def solve():
    # 解説放送を実装
    N = int(input())
    flowers = [0]*N
    h = [int(x) for x in input().split(" ")]
    ans = h[0]
    stack = h[0]

    for i in range(1, N):
        if h[i] <= stack:
            stack = h[i]
        else:
            ans += (h[i] - stack)
            stack = h[i]
    
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

max_h = max(H)
memo = [['0'] for _ in range(max_h)]
for i, h in enumerate(H):
    for j, v in enumerate(memo):
        if j+1 > h:
            v += '0'
        else:
            v += '1'


for i, v in enumerate(memo):
    v += '0'


for i, v in enumerate(memo):
    memo[i] = ''.join(v)


ans = 0
for i, v in enumerate(memo):
    ans += v.count('10')


print(ans)