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: Runtime Error

Solution code:
h_state=[0]*n

def water(n,h,h_state):
    for i in range(n):
        if h_state[i]<h[i]:
            l=i
            break
        elif i==n-1 and h_state[i]<h[i]:
            l=i
        else:
            l=i+1
    
    for j in range(l,n):
        if h_state[j]==h[j]:
            r=j-1
            break
        else:
            r=j
    
    for k in range(l,r+1):
        h_state[k]+=1

cnt=0
while h_state != h:
    for i in range(n):
        if h_state[i]<h[i]:
            cnt+=1
            water(n,h,h_state)
            break
print(cnt)

Submission status: Runtime Error

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

def f(X):
  for i in range(len(X)-2):
    for j in range(i+2,len(X)):
      if X[i] == 0 and X[j] == 0 and (0 not in X[i+1:j]):
        X = X[0:i+1] + [X[k]-1 for k in range(i+1,j+1)] + X[j:len(X)]
        break
        #こうでないときは0が固まっているはず
  if X[0] == 0:
    for i in range(len(X)-1):
      if X[i] == 0 and X[i+1]!= 0:
        X = X[0:i+1] + [X[k]-1 for k in range(i+1, len(X))]
        break
  else:
    for i in range(len(X)-1):
      if X[i] != 0 and X[i+1] == 0:
        X = [X[k]-1 for i in range(0,i+1)] + X[i+1:len(X)]
        break
  return(X)

count = 0
A = H
while True:
  if H == [0] * n:
    print(count)
    exit()
  else:
    A = f(A)
    count += 1

Submission status: Runtime Error

Solution code:
import sys
import math
import itertools
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
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

N = i()
h = l()
ans = 0
start = 0
Flag = 0
for i in range(N-1):
    if h[i] > h[i+1]:
        Flag = 1
    if h[i] < h[i+1] and Flag == 1:
        kukan = h[start:i+1]
        ans -= h[i]
        ans += max(kukan)
        start = i+1
        Flag = 0
kukan = h[start:-1]
ans += max(kukan)
print(ans)

Submission status: Runtime Error

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

for i in range(N):
    if h[i]<=h[i+1]:
        m=h[i+1]-h[i]
        Answer=Answer+m
        
print(Answer)

Submission status: Runtime Error

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

ans = 0
cnt = 0

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

print(ans)

Submission status: Runtime Error

Solution code:
h=list(map(int,input().split()))
l=[0]
c='plus'
for i in range(n-1):
    if c=='minus' and h[i]<=h[i+1]:
        l.append(i)
    if h[i]<=h[i+1]:c='plus'
    else:c='minus'

w=0
m=0
for i in range(len(l)-1):
    w+=max(h[l[i]:l[i+1]+1])-m
    m=h[l[i+1]]
w+=max(h[l[-1]:n])-m
print(w)

Submission status: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input().split()))
l=[0]
c='plus'
for i in range(n-1):
    if c=='minus' and h[i]<=h[i+1]:
        l.append(i)
    if h[i]<=h[i+1]:c='plus'
    else:c='minus'

w=0
m=0
for i in range(len(l)-1):
    w+=max(h[l[i]:l[i+1]+1])-m
    m=h[l[i+1]]
w+=max(h[l[-1]:-1])-m
print(w)

Submission status: Runtime Error

Solution code:
多分再帰使うんだろうね

Submission status: Runtime Error

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

# check
min_h = min(h)
cnt = min_h
before_idx = 0
for i in range(N):
    if h[i] == min_h or i == N - 1:
        slise_h = h[before_idx:i + 1]

        print(slise_h)
        slise_min = min(slise_h)
        for i, n in enumerate(slise_h):
            if n == slise_min:
                add = n - min_h
            else:
                if i == 0:
                    if slise_h[i + 1] <= n:
                        target = [slise_h[i + 1]]
                    else:
                        target = []
                elif i == len(slise_h) - 1:
                    if slise_h[i - 1] <= n:
                        target = [slise_h[i - 1]]
                    else:
                        target = []
                else:
                    target = [t for t in (slise_h[i - 1], slise_h[i + 1]) if t <= n]
                if not target:
                    add = n - slise_min
                else:
                    add = n - max(target)

            print(f"+ {add}")

            cnt += add
            before_h = n
        before_idx = i + 1

print(cnt)

Submission status: Runtime Error

Solution code:
import cython

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

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

Submission status: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input().split()))
h=np.array(h)
ans=0
while True:
  if all(hhh==0 for hhh in h):
    print(ans)
    exit()
  for i in range(n):
    k=n-i
    cnt=0
  for j in range(1,k):
    if all(hh!=0 for hh in h[i:i+j+1]):
      cnt+=1
  h[i:i+cnt+1]=-1
  ans+=1

Submission status: Runtime Error

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

ans = 0
tmp = H[0]

for h in H[1:]:
    if h >= tmp:
        tmp = h
    else:
        ans += tmp - h
        tmp = h
else:
    ans += h

print(ans)

Submission status: Runtime Error

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

ans = 0
tmp = H[0]

for i in range(len(H)-1):
    if H[i] <= H[i+1]:
        tmp = H[i+1]
    else:
        ans += tmp - H[i+1]
else:
    ans += H[i+1]

print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np


def count_water(np_arr):
  least = min(np_arr)
  div_p = np.where(np_arr == least)[0]
  np_arr -= least
  return least + count_water(np_arr[:div_p]) + count_water(np_arr[div_p + 1:])


def main():
  readline = sys.stdin.readline
  
  n = int(readline().rstirp())
  h = np.array(tuple(int(_) for _ in readline().rstrip().split()))
  
  print(count_water(h))

 
if __name__ == "__main__":
  main()

Submission status: Runtime Error

Solution code:
import collections
import numpy as np
#N,K=map(int,input().split())
N=int(input())
h=np.array(list(map(int,input().split())))
ans=0
def sol(l,m,k):
    global ans
    if m==0:
        return ans
    elif m>1:
        for i in range(N-m+2):
            if not 0 in l[i:i+m-1]:
                ans+=np.amin(l[i:i+m-1])
                l[i:i+m-1]-=np.amin(l[i:i+m-1])
        sol(l,m-1,ans)
    else:
        ans+=np.count_nonzero(h)
        sol(l, m - 1, ans)
n=0
sol(h,N,ans)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
#s=input().rstrip()

n=I()
lis=LI()
count=0
while sum(lis)>0:
    i=0
    while i<n:
        if lis[i]>0:
            lis[i]-=1
            state="water"
            #print(1)
        elif lis[i]==0 and state=="water":
            count+=1
            state=""
            #print(2)
        if i==n-1 and state=="water":
            count+=1
            state=""
            #print(3)
        i+=1
print(count)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000)
def watering(l,a,b):
  if b-a <= 1:
    return l[a]
  
  c = 0
  t = a
  
  mi = min(l[a:b])
  for i in range(a,b):
    l[i] -= mi
    
  for i in range(a,b):
    if l[i]==0:
      c += watering(l,t,i)
      t=i+1
  c += watering(l,t,b)
      
  return c+mi

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

print(watering(h,0,n))

Submission status: Runtime Error

Solution code:
n = int(input())
#n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
# abc = [int(input()) for i in range(5)]
up = True
ans = 0
for i in range(n-1):
    if a[i] <= a[i+1]:
        if up == False:
            ans -= a[i]
        up = True
    else:
        if up == True:
            ans += a[i]
        up = False

if a[-2] <= a[-1]:
    ans += a[-1]

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
def cnt_ope(h):
    diff_h = [h[i + 1] - h[i] for i in range(len(h) - 1)]
    ext_h = [0] * (len(diff_h) - 1)
    ext_i = []
    for i in range(len(diff_h) - 1):
        if diff_h[i] < 0 and diff_h[i + 1] > 0:
            ext_h[i] = -1
            ext_i.append(i + 1)
    if ext_i:
        ext_min_i = h.index(min([h[i] for i in ext_i]))
        return (cnt_ope(h[:ext_min_i]) - h[ext_min_i]) + (cnt_ope(h[ext_min_i + 1:]) - h[ext_min_i]) + h[ext_min_i]
    else:
        return max(h)
print(cnt_ope(h))

Submission status: Runtime Error

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
def cnt_ope(h):
    diff_h = [h[i + 1] - h[i] for i in range(len(h) - 1)]
    ext_h = [0] * (len(diff_h) - 1)
    ext_i = []
    for i in range(len(diff_h) - 1):
        if diff_h[i] < 0 and diff_h[i + 1] > 0:
            ext_h[i] = -1
            ext_i.append(i + 1)
    if ext_i:
        ext_i = h.index(min([h[i] for i in ext_i]))
        return (cnt_ope(h[:ext_i]) - h[ext_i]) + (cnt_ope(h[ext_i + 1:]) - h[ext_i]) + h[ext_i]
    else:
        return max(h)
print(cnt_ope(h))

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

int main(){
    int N;cin>>N;
    int array[N];
    int dp[N]={1000};
    for(int i=0;i<N;i++)cin>>array[i];
    dp[0]=array[0];
    for(int i=1;i<N;i++){
        if(array[i]<array[i-1])dp[i]=dp[i-1];
        else dp[i]=dp[i-1]+array[i]-array[i-1];
    }
    cout<<dp[N-1]<<endl;
}

Submission status: Runtime Error

Solution code:
n = int(input())
l = [0]+list(map(int,input().split()))
s = 0
while len(set(l))!=1:
  for i in range(n+1):
    if l[i]==0:
      if l[i+1]==0:
          break
      for j in range(i+1,n+2):
        if l[j]!=0:
          l[j] -=1
          continue
        else:
          s += 1
          break
print(s)

Submission status: Runtime Error

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

while max(H) > 0:
    grp = [[]]
    for i, h in enumerate(H):
        if h == 0:
            if len(grp[-1]) > 0:
                grp.append([])
        else:
            grp[-1].append((i, h))
    for g in grp:
        mi = min([h for _, h in g])
        ans += mi
        for i, _ in g:
            H[i] -= mi

print(ans)

Submission status: Runtime Error

Solution code:
#editorial参照
import sys
input = sys.stdin.read
n,*a, = map(int, input().split( ))
m = min(a)
ans = (n+m-1)//m + 4
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
Hlst = list(map(int,input().split()))
times = 0
l = 0
r = l
while r <= N:
    l = r
    while Hlst[r] == 1:
        r+=1
    for water in range(l,r+1):
        Hlst[water]-=1
        times += 1
print(times)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import sys
L = int(sys.stdin.readline().rstrip())

if L == 1:
  h = int(sys.stdin.readline().rstrip())
else:
  h = list(map(int, sys.stdin.readline().rstrip().split()))

L = N
if L == 0:
  ans = 0
elif L == 1:
  ans = N - h
elif L == 2:
  ans = N - max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]

  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
ans = p - v
print(ans)

Submission status: Runtime Error

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())

if N == 1:
  h = int(sys.stdin.readline().rstrip())
else:
  h = list(map(int, sys.stdin.readline().rstrip().split()))

L = N
if L == 0:
  ans = 0
elif L < 1:
  ans = N - h[0]
elif L < 2:
  ans = N - max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]

  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
ans = p - v
print(ans)

Submission status: Runtime Error

Solution code:
import sys
N = int(sys.stdin.readline().rstrip())
h = list(map(int, sys.stdin.readline().rstrip().split()))
 
L = N
if L == 0:
  ans = 0
elif L < 1:
  ans = N - h[0]
elif L < 2:
  ans = N - max(h)
  
else:
  grad1 = h[0] > h[1]
  p= 0
  v= 0
  if grad1:
    p += h[0]

  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p += h[n-1]
    else:
      v += h[n-1]
    grad1 = grad2
               
  if not grad1:
    p += h[n]
 
ans = p - v
print(ans)

Submission status: Runtime Error

Solution code:
N = int(sys.stdin.readline().rstrip())
h = map(int, sys.stdin.readline().rstrip().split())

L = len(h)
if L == 0:
  ans = 0
elif L < 1:
  ans = N - h[0]
elif L < 2:
  ans = N - max(h)
  
else:
  grad1 = h[0] > h[1]
  if grad1:
    p = [h[0]]
  else:
    p = []
    
  v = []
  for n in range(2, L):
    grad2 = h[n-1] > h[n] 
    if grad1 == grad2:
      continue
    elif grad2:
      p.append(h[n-1])
    else:
      v.append(h[n-1])
    grad1 = grad2
               
  if not grad1:
    p.append(h[n])
 
ans = sum(p) - sum(v)
print(ans)

Submission status: Runtime Error

Solution code:
def mizuyari(nums):
  minimum=1000
  maximam=-1
  index=-1
  change=False
  for i,j in enumerate(nums):
    if i==0:
      minimum = j
      maximam = j
      continue
    if i==len(nums):
      if j > maximam:
        maximam = j
      continue
    if j > maximam:
      maximam = j
    if nums[i-1] > j and nums[i+1] > j:
      if change==False:
        minimum = j
        index = i
        change=True
      else:
        if minimum > j:
          minimum = j
          index = i
  if index == -1:
    return maximam
  ans = minimum
  for i,j in enumerate(nums):
    j -= minimum
    if j < 0:
      j=0
    nums[i]=j
  ans += mizuyari(nums[:index]) + mizuyari(nums[index + 1:])
  return ans

n=int(input())
lis=list(map(int,input().split()))
ans=mizuyari(lis)
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

h = map(lambda x:  x-min(h) ,h)
count =+ min(h)
if max(h) == 0:
  return count

h = h.append(0)
while max(h) !=0:
    for i in range(len(h)+1):
        if h[i] == 0:
            continue
        else:
            for j in range(i+1,len(h)+1):
                if h[j] != 0:
                    continue
                else h[j] == 0:
                    count += 1
                    break
    h = map(lambda x:  x-1 ,h)
print(count)

Submission status: Runtime Error

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

inc = True
peaks = [], []
for h0, h1 in zip(H, H[1:]):
    if inc:
        if h0 > h1:
            peaks.append(h0)
            inc = False
    else:
        if h0 < h1:
            peaks.append(-h0)
            inc = True
if inc:
    peaks.append(H[-1])
print(sum(peaks))

Submission status: Runtime Error

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

inc = True
maxes, mins = [], []
for h0, h1 in zip(H, H[1:]):
    if inc:
        if h0 > h1:
            maxes.append(h0)
            inc = False
    else:
        if h0 < h1:
            mins.append(h0)
            inc = True
if inc:
    maxes.append(h1)
print(sum(maxes)-sum(mins))

Submission status: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input().split()))
arr=[x for x in h]
cnt=0

while 1:
  for i in range(n):
    if h[i]!=0:
      s=i
      break
  e=s
  for i in range(s,n-1):
    if h[s]<=h[i+1]:
      e+=1
    else:
      break
  h=[h[i]-1 if s<=i<=e else h[i] for i in range(n)]
  cnt+=1
  if all([x==0 for x in h]):
    break
print(cnt)

Submission status: Runtime Error

Solution code:
N=int(input())
h=list(map(int,input().split()))
s=0
mini=100
def garden(N,h,s,mini):
  p=[]
  a=0
  b=0
  size=0
  for i in range (N):
    if mini>h[i]:
      mini=h[i]
    if i!=N-1:
      if h[i]>h[i+1]:
        p.append(h[i])
        a+=h[i]
        size+=1
    if i!=N-1 and i!=0:
      if h[i]<h[i+1] and (h[i]-h[i-1])*(h[i+1]-h[i])<=0:
        p.append(h[i])
        b+=h[i]
        size+=1
    if i==N-1:
      if h[i-1]<h[i]:
        p.append(h[i])
        a+=h[i]
        size+=1
  s+=a
  s-=b
  flag=0
  for j in range (1,size-2):
    if (p[j]-p[j-1])*(p[j+1]-p[j])>0:
      flag=1
  if flag==1:
    garden(size,p,s,mini)
  if s==0 and h[0]!=0:
    s+=1
  print(s)
  
garden(N,h,s,mini)

Submission status: Runtime Error

Solution code:
N=int(input())
h=list(map(int,input().split()))
s=0
mini=100
def garden(N,h,s,mini):
  p=[]
  a=0
  b=0
  size=0
  for i in range (N):
    if mini>h[i]:
      mini=h[i]
    if i!=N-1:
      if h[i]>h[i+1]:
        p.append(h[i])
        a+=h[i]
        size+=1
    if i!=N-1 and i!=0:
      if h[i]<h[i+1] and (h[i]-h[i-1])*(h[i+1]-h[i])<=0:
        p.append(h[i])
        b+=h[i]
        size+=1
    if i==N-1:
      if h[i-1]<h[i]:
        p.append(h[i])
        a+=h[i]
        size+=1
  s+=a
  s-=b
  flag=0
  for j in range (1,size-1):
    if (p[j]-p[j-1])*(p[j+1]-p[j])>0:
      flag=1
  if flag==1:
    garden(size,p,s,mini)
  if s==0 and h[0]!=0:
    s+=1
  print(s)
  
garden(N,h,s,mini)

Submission status: Runtime Error

Solution code:
N=int(input())
h=list(map(int,input().split()))
s=0
mini=100
def garden(N,h,s,mini):
  p=[]
  a=0
  b=0
  size=0
  for i in range (N):
    if mini>h[i]:
      mini=h[i]
    if i!=N-1:
      if h[i]>h[i+1]:
        p.append(h[i])
        a+=h[i]
        size+=1
    if i!=N-1 and i!=0:
      if h[i]<h[i+1] and (h[i]-h[i-1])*(h[i+1]-h[i])<=0:
        p.append(h[i])
        b+=h[i]
        size+=1
    if i==N-1:
      if h[i-1]<h[i]:
        p.append(h[i])
        a+=h[i]
        size+=1
  s+=a
  s-=b
  flag=0
  for j in range (1,size-1):
    if (p[j]-p[j-1])*(p[j+1]-p[j])>0:
      flag=1
  if flag==1:
    garden(size,p,s,mini)
  if s==0:
    s+=1
  print(s)
  
garden(N,h,s,mini)

Submission status: Runtime Error

Solution code:
N=int(input())
h=list(map(int,input().split()))
s=0
mini=100
def garden(N,h,s,mini):
  p=[]
  a=0
  b=0
  size=0
  for i in range (N):
    if mini>h[i]:
      mini=h[i]
    if i!=N-1:
      if h[i]>h[i+1]:
        p.append(h[i])
        a+=h[i]
        size+=1
    if i!=N-1 and i!=0:
      if h[i]<h[i+1] and (h[i]-h[i-1])*(h[i+1]-h[i])<=0:
        p.append(h[i])
        b+=h[i]
        size+=1
    if i==N-1:
      if h[i-1]<h[i]:
        p.append(h[i])
        a+=h[i]
        size+=1
  s+=a
  s-=b
  print(a)
  print(b)
  flag=0
  for j in range (1,size-1):
    if (p[j]-p[j-1])*(p[j+1]-p[j])>0:
      flag=1
  if flag==1:
    garden(size,p,s,mini)
  if s==0:
    s+=1
  print(s)
  
garden(N,h,s,mini)

Submission status: Runtime Error

Solution code:
N=int(input())
h=list(map(int,input().split()))
s=0
mini=100
def garden(N,h,s,mini):
  p=[]
  a=0
  b=0
  size=0
  for i in range (N):
    if mini>h[i]:
      mini=h[i]
    if i!=N-1:
      if h[i]>h[i+1]:
        p.append(h[i])
        a+=h[i]
        size+=1
    if i!=N-1 and i!=0:
      if h[i]<h[i+1] and (h[i]-h[i-1])*(h[i+1]-h[i])<=0:
        p.append(h[i])
        b+=h[i]
        size+=1
    if i==N-1:
      if h[i-1]<h[i]:
        p.append(h[i])
        a+=h[i]
        size+=1
  s+=a
  s-=b
  flag=0
  for j in range (1,size-1):
    if (p[j]-p[j-1])*(p[j+1]-p[j])>0:
      flag=1
  if flag==1:
    garden(size,p,s,mini)
  print(s)
  
garden(N,h,s,mini)

Submission status: Runtime Error

Solution code:
print(int(input())

Submission status: Runtime Error

Solution code:
N=int(input())
h=list(map(int,input().split()))
s=0
mini=100
def kadan(N,h,s,mini):
  p=[]
  a=0
  b=0
  size=0
  for i in range (N):
    if mini>h[i]:
      mini=h[i]
    if i!=N-1:
      if h[i]>h[i+1]:
        p.append(h[i])
        a+=h[i]
        size+=1
    if i!=N-1 and i!=0:
      if h[i]<h[i+1] and (h[i]-h[i-1])*(h[i+1]-h[i])<=0:
        p.append(h[i])
        b+=h[i]
        size+=1
    if i==N-1:
      if h[i-1]<h[i]:
        p.append(h[i])
        a+=h[i]
        size+=1
  s+=a
  s-=b
  if size>5:
    kadan(size,p,s,mini)
  print(s)
  
kadan(N,h,s,mini)

Submission status: Runtime Error

Solution code:
"""
0で断絶が生じなければ、一番低い部分は共通で処理できる

Nもhも小さいので、貪欲？にやればもんだいなさそう。
0以外で連続してる部分それぞれについて、実際に水やりを行う。
伸ばすよりはhを短くする方が処理的に楽
"""




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

ans = 0
l,r = 0,0
while True:
    if l > N-1 or r > N-1:
        l,r = 0,0

    while l < N H[l] == 0:
        l += 1

    if l >= N:
        break

    r = l
    while r < N and H[r] != 0:
        r += 1

    tmp = min(H[l:min(r,N)])
    for i in range(l,min(r,N)):
        H[i] -= tmp
    ans += tmp
    l = r + 1

    

print(ans)

Submission status: Runtime Error

Solution code:
"""
0で断絶が生じなければ、一番低い部分は共通で処理できる

Nもhも小さいので、貪欲？にやればもんだいなさそう。
0以外で連続してる部分それぞれについて、実際に水やりを行う。
伸ばすよりはhを短くする方が処理的に楽
"""




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

ans = 0
l,r = 0,0
while True:
    if l > N-1 or r > N-1:
        l,r = 0,0

    while H[l] == 0:
        l += 1

    if l == N:
        break
    
    r = l
    while r < N and H[r] != 0:
        r += 1

    tmp = min(H[l:min(r,N)])
    for i in range(l,min(r,N)):
        H[i] -= tmp
    ans += tmp
    l = r + 1

    if sum(H) == 0:
        break

print(ans)

Submission status: Runtime Error

Solution code:


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

ans = 0
l,r = 0,0
while True:
    if l > N-1 or r > N-1:
        l,r = 0,0

    while H[l] == 0:
        l += 1
    r = l
    while r < N and H[r] != 0:
        r += 1

    tmp = min(H[l:min(r,N)])
    for i in range(l,min(r,N)):
        H[i] -= tmp
    ans += tmp
    l = r + 1

    if sum(H) == 0:
        break

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(int(input()))
cost = s[0]
for i in range(1, n):
    if s[i - 1] < s[i]:
        cost += s[i] - s[i - 1]
print(cost)

Submission status: Runtime Error

Solution code:
n = int(input())
h = list(map(int, input().split()))
hantei = [0 for _ in range(n)]
hantei.append(1)
result = 0
h.append(10*3)

while any(hantei[i] != 1 for i in range(n)):
    min_h = min(h)
    cost = 0
    for i in range(len(h)):
        if hantei[i]==1:
            result += cost
            cost = 0
            h[i] = 10**3
        else:
            h[i] -= min_h
            if h[i] == 0:
                hantei[i] = 1
            cost = min_
print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
# h = list(map(int, input().split()))
h = [0]
for _ in range(N):
    h.append(int(input()))
answer = 0
for i in range(1, N+1):
    answer += max(H[i] - H[i-1], 0)

print(answer)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
def water(flowers, count):
    print(f'count : {count}')
    print(f'flowers : {flowers}')
    if len(flowers) is 0:
        return count
    count = count + min(flowers)
    flowers = [int(e-min(flowers)) for e in flowers]
    while 0 in flowers:
        count = water(flowers[:flowers.index(0)], count)
        flowers = flowers[flowers.index(0)+1: ]
    count = water(flowers, count)
    return count

n = int(input())
flowers = [int(e) for e in input().split()]

count = 0
print(f'Initialize : {flowers}')
water(flowers, count)
# while flowers.count(0) != 0:
#     print(f'{"="*20} iteration{i}')
#     flowers, count = make_zero(flowers, count)
#     print(f'flowers : {flowers}')
#     print(f'count : {count}')

Submission status: Runtime Error

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: Runtime Error

Solution code:
N = int(input().split())
H = list(map(int,input().split()))
y = 0
z = H[i]
ans = 0
for i in range(N):
    x = H[i]
    if x >= y:
        y = x
    else:
        ans += y
        z = min(z,x)
        for j in range(i,N):
            p = H[j]
            z = min(z,p)
            H[j] -= z
print(ans)

Submission status: Runtime Error

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
#from scipy.misc import comb

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

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

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

k = 0
while sum(h_list)>0:
  bool_list = [True if i==max(h_list) else False for i in h_list]
  indexes = []
  true_index = bool_list.index(True)
  indexes.append(true_index)
  while bool_list(true_index):
    true_index += 1
    indexes.append(true_index)
  h_list = [h_list[i]-1 for i in indexes]
  k += 1
  
print(k)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
while max(a) != 0:
  # print(a,cnt)
  for i in range(n):
    if a[i] != 0:
      a[i] -= 1
      betweenZero = False
    elif betweenZero == False:
      cnt += 1
      betweenZero = True
  if betweenZero == False:
    cnt += 1
  betweenZero = True
print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [0] * N
for i in range(N):
    A[i] = int(input())

lst=sorted(set(A))

target = {}
for i in range(len(lst)):
    target[lst[i]] = i
    
for a in A:
    print(target[a])

Submission status: Runtime Error

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

def best(plants, counter):
    all0 = True
    started = False
    for i,p in enumerate(plants):    
        if p > 0:
            all0 = False
        if p == 0:
            if started:
                break
            else:
                continue
        plants[i] -= 1
        started = True
    
    if all0:
        return counter
    return best(plants, counter+1)
    
counter = best(hs, 0)
print(counter)

Submission status: Runtime Error

Solution code:
N = int(input())
H = list(map(int,input().split()))
H = [0]+H+[0]
for i in range(N,0,-1):
  if H[i-1]<=H[i]<=H[i+1]:
    H.pop(i)
  if H[i-1]>=H[i]>=H[i+1]:
    H.pop(i)
cnt = 0
flg = True
while flg:
  m = max(H)
  if m == 0:
    break
  i = H.index(m)
  d = min(H[i]-H[i+1], H[i]-H[i-1])
  cnt += d
  H[i+1]=max(0,H[i+1]-d)
  H[i-1]=max(0,H[i-1]-d)
  H.pop(i)
print(cnt)

Submission status: Runtime Error

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