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: Wrong Answer

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

ans = 0
cou = 0

hList = [0] + hList

for i in range(N):
    if hList[i] < hList[i+1]:
        cou = hList[i+1] - hList[i]
    else:
        ans += cou
        cou = 0
ans += cou
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int, input().split()))
print(max(arr) * n - sum(arr) + min(arr) * n)

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int, input().split()))
print(max(arr) * n - sum(arr) + min(arr))

Submission status: Wrong Answer

Solution code:
n = int(input())
arr = list(map(int, input().split()))
print(max(arr) * n - sum(arr))

Submission status: Wrong Answer

Solution code:
n=int(input())
h=list(map(int,input().split()))
ltani=h[0]
yama=h[0]
mizu=0
if n==1:
    print(max(h))
elif n==2:
    print(max(h))
else:
    for i in range(1,n-1):
        if yama>h[i] and h[i]<h[i+1]:
            rtani=h[i]
            mizu=mizu+yama-min(ltani,rtani)
            ltani=h[i]
            yama=h[i]
        if h[i-1]<h[i] and h[i]>=h[i+1]:
            yama=h[i]
    if h[n-2]>h[n-1]:
        rtani=h[n-1]
        mizu=mizu+yama-min(ltani,rtani)
    elif h[n-2]<h[n-1]:
        yama=h[n-1]
        mizu=mizu+yama-ltani
    else:
        if yama==h[n-1]:
            mizu=mizu+h[n-1]-ltani
        else:
            rtani=h[i]
            mizu=mizu+yama-min(ltani,rtani)            
    mizu=mizu+min(h)
    print(mizu)

Submission status: Wrong Answer

Solution code:
n=int(input())
h=list(map(int,input().split()))
ltani=h[0]
yama=h[0]
mizu=0
if n==1:
    print(max(h))
elif n==2:
    print(max(h))
else:
    for i in range(1,n-1):
        if h[i-1]>h[i] and h[i]<h[i+1]:
            rtani=h[i]
            mizu=mizu+yama-min(ltani,rtani)
            ltani=h[i]
        elif h[i-1]<h[i] and h[i]>h[i+1]:
            yama=h[i]
    if h[n-2]>h[n-1]:
        rtani=h[i]
        mizu=mizu+yama-min(ltani,rtani)
        ltani=h[i]
    else:
        yama=h[n-1]
        mizu=mizu+yama-ltani
    mizu=mizu+min(h)
    print(mizu)

Submission status: Wrong Answer

Solution code:
# ABC116C - Grand Garden
n = int(input())
lst = list(map(int, input().rstrip().split())) + [0]
ans = 0
cur = 0
for n, m in zip(lst, lst[1:]):
    print(n, m)
    if cur < n:
        cur = n
    if cur > m:
        ans += cur - m
        cur = m
print(ans)

Submission status: Wrong Answer

Solution code:
# ABC116C - Grand Garden
n = int(input())
lst = list(map(int, input().rstrip().split()))
ans = 0
cur = 0
for n, m in zip(lst, lst[1:]):
    if cur < n:
        cur = n
    if cur > m:
        ans += cur - m
        cur = m
print(ans)

Submission status: Wrong Answer

Solution code:
# ABC116C - Grand Garden
n = int(input())
lst = list(map(int, input().rstrip().split()))
ans = 0
cur = 0
for n, m in zip(lst, lst[1:]):
    print(n, m)
    if cur < n:
        cur = n
    if cur > m:
        ans += cur - m
        cur = m
print(ans)

Submission status: Wrong Answer

Solution code:
def min_other_than_zero(seq):
    seq = set(seq)
    if 0 in seq: seq.remove(0)
    return min(seq)


def water(H, start, stop):
    if max(H) == 0:
        return

    # water
    valley = min_other_than_zero(H[start:stop])
    for i in range(start, stop):
        H[i] -= valley
    global ans
    ans += valley

    # search for next area
    valley_indices = [start + i for i, h in enumerate(H[start:stop]) if h == 0]
    if len(valley_indices) == 0:
        return
    if valley_indices[0] != start:
        valley_indices = [start - 1] + valley_indices
    if valley_indices[-1] != stop - 1:
        valley_indices += [stop]
    for i in range(len(valley_indices) - 1):
        if valley_indices[i + 1] - valley_indices[i] > 1:
            water(H, valley_indices[i] + 1, valley_indices[i + 1])


N = int(input())
H = [int(s) for s in input().split()]

ans = 0
water(H, 0, N)
print(ans)

Submission status: Wrong Answer

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

ans = 0
check = 0
print(len(h))
for i in h:
    if check < i:
        ans += i-check
    check = i

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    if h[n-1] >= h[n-2]:
        ans += h[n-1]
ans -= cheak
print(ans)

Submission status: Wrong Answer

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

    if h[n-1] >= h[n-2]:
        ans += h[n-1]
ans -= cheak
print(ans)

Submission status: Wrong Answer

Solution code:
def spritarr(arr):
  ret=[]
  for i in range(arr.count(0)):
    pos=arr.index(0)
    ret.append(arr[:pos])
    arr=arr[pos+1:]
  if len(arr)!=0:
    ret.append(arr)
  return ret

n=int(input())
arr=list(map(int,input().split()))
tmp=spritarr(arr)
ans=0
while 1:
  tmp2=[]
  cnt=0
  for i in range(len(tmp)):
    if len(tmp[i])==0:
      cnt+=1
    else:
      tm=min(tmp[i])
      ans+=tm
      for j in range(len(tmp[i])):
        tmp[i][j]-=tm
    tmp2.extend(spritarr(tmp[i]))
  tmp=tmp2
  if cnt==len(tmp2):
    break
print(ans)

Submission status: Wrong Answer

Solution code:
def spritarr(arr):
  ret=[]
  for i in range(arr.count(0)):
    pos=arr.index(0)
    if pos==-1:
      break
    else:
      ret.append(arr[:pos])
      arr=arr[pos+1:]
  if len(arr)!=0:
    ret.append(arr)
  return ret

n=int(input())
arr=list(map(int,input().split()))
tmp=spritarr(arr)
ans=0
while 1:
  tmp2=[]
  cnt=0
  for i in range(len(tmp)):
    if len(tmp[i])==0:
      cnt+=1
    else:
      tm=min(tmp[i])
      ans+=tm
      for j in range(len(tmp[i])):
        tmp[i][j]-=tm
    tmp2.extend(spritarr(tmp[i]))
  tmp=tmp2
  if cnt==len(tmp2):
    break
print(ans)

Submission status: Wrong Answer

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

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


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h) if x == 0]

    if not any(list_h):
        return min_height

    left_list = list_h[:zero_indexes[0]]
    right_list = list_h[zero_indexes[0]+1:]
    return calc(left_list) + calc(right_list)


print(calc(h))

Submission status: Wrong Answer

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

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


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h) if x == 0]

    if not any(list_h):
        return min_height

    initial_flag = zero_indexes[0] == 0
    last_flag = zero_indexes[-1] == len(list_h) - 1

    if initial_flag and last_flag:
        zero_indexes = zero_indexes[1:-1]
        list_h = list_h[1:-1]
    elif initial_flag:
        zero_indexes = zero_indexes[1:]
        list_h = list_h[1:]
    elif last_flag:
        zero_indexes = zero_indexes[:-1]
        list_h = list_h[:-1]

    if not zero_indexes:
        return calc(list_h) + min_height

    sum_cnt = 0
    for i in range(len(zero_indexes)):
        sum_cnt += calc(list_h[:zero_indexes[i]])
        sum_cnt += calc(list_h[zero_indexes[i]+1:])

    return sum_cnt + min_height


print(calc(h))

Submission status: Wrong Answer

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

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


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h) if x == 0]

    if not any(list_h):
        # 0配列のとき
        return min_height

    initial_flag = zero_indexes[0] == 0
    last_flag = zero_indexes[-1] == len(list_h) - 1

    if initial_flag and last_flag:
        zero_indexes = zero_indexes[1:-1]
        list_h = list_h[1:-1]
    elif initial_flag:
        zero_indexes = zero_indexes[1:]
        list_h = list_h[1:]
    elif last_flag:
        zero_indexes = zero_indexes[:-1]
        list_h = list_h[:-1]

    if not zero_indexes:
        return calc(list_h) + min_height

    sum_cnt = 0
    for i in range(len(zero_indexes)):
        if i == 0:
            sum_cnt += calc(list_h[:zero_indexes[0]])
        else:
            sum_cnt += calc(list_h[zero_indexes[i - 1] +
                                   1:zero_indexes[i]])
        if i == len(zero_indexes) - 1:
            sum_cnt += calc(list_h[zero_indexes[-1]:])

    return sum_cnt + min_height


print(calc(h))

Submission status: Wrong Answer

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

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


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h) if x == 0]

    if not any(list_h):
        # 0配列のとき
        return min_height

    initial_flag = zero_indexes[0] == 0
    last_flag = zero_indexes[-1] == len(list_h) - 1

    if initial_flag:
        zero_indexes.pop(0)
        list_h.pop(0)

    if len(zero_indexes) > 0 and last_flag:
        zero_indexes.pop()
        list_h.pop()

    if not zero_indexes:
        return calc(list_h) + min_height

    sum_cnt = 0
    for i in range(len(zero_indexes)):
        if i == 0:
            sum_cnt += calc(list_h[:zero_indexes[0]])
        else:
            sum_cnt += calc(list_h[zero_indexes[i - 1] +
                                   1:zero_indexes[i]])
        if i == len(zero_indexes) - 1:
            sum_cnt += calc(list_h[zero_indexes[-1]:])

    return sum_cnt + min_height


print(calc(h))

Submission status: Wrong Answer

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

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


def calc(list_h):
    if not list_h:
        return 0

    min_height = min(list_h)
    list_h2 = [list_h[i] - min_height for i in range(len(list_h))]
    zero_indexes = [i for i, x in enumerate(list_h2) if x == 0]

    if not any(list_h2):
        # 0配列のとき
        return min_height

    if zero_indexes[0] == 0:
        zero_indexes.pop(0)
        list_h2.pop(0)

    if len(zero_indexes) > 0 and zero_indexes[-1] == len(list_h) - 1:
        zero_indexes.pop()
        list_h2.pop()

    if not zero_indexes:
        return calc(list_h2) + min_height

    sum_cnt = 0
    for i in range(len(zero_indexes)):
        if i == 0:
            sum_cnt += calc(list_h2[:zero_indexes[0]])
        else:
            sum_cnt += calc(list_h2[zero_indexes[i - 1] +
                                    1:zero_indexes[i]])
        if i == len(zero_indexes) - 1:
            sum_cnt += calc(list_h2[zero_indexes[-1]:])

    return sum_cnt + min_height


print(calc(h))

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

c = 0
for i in range(n-1):
    if h[i] > h[i+1]:
        c += h[i] - m
        if i == n - 2:
             c += m
    elif i == n - 2:
        c += h[i+1]-h[i] -m

print(c)

Submission status: Wrong Answer

Solution code:
# coding: utf-8

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

c = 0
for i in range(n-1):
    if h[i] > h[i+1]:
        c += h[i] - m
    elif i == n - 2:
        c += h[i+1]-h[i] -m

print(c)

Submission status: Wrong Answer

Solution code:
# 2時間ほぼググってた　80分とか無理
n=int(input())
h=list(map(int, input().split()))
sum=0
for i in range(max(h)):
    if 0 not in h:
        h=[i-1 if i>0 else 0 for i in h]
        sum+=1
        # print(h,1)
    else:
        count0=[i for i, j in enumerate(h) if j==0]
        a=count0.copy() #copy()をつけないとaを変えるとcount0も変わる
        b=count0.copy()
        a.pop(0)
        b.pop(-1)
        c=[x-y for(x,y) in zip(a,b)]#a-bが要素のリストを作る
        sum = sum + len(c)-c.count(1)
        print(count0)
        if count0[0]>0 and count0[-1]<n-1:
            sum +=2
            # print(1)
        elif count0[0]>0 or count0[-1]<n-1:
            sum +=1
            # print(2)
        else:
            sum +=0
        h=[i-1 if i>0 else 0 for i in h]
        # print(h,2)
print(sum)

Submission status: Wrong Answer

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


def water(x):
  global ans
#  print(x, ans)
  if any(x) == False:
#    print(0)
    return
  else:
#    print(1)
    if 0 in x:
#      print(2)
      x1, x2 = x[0:x.index(0)], x[x.index(0)+1:]
      water(x1)
      water(x2)
    else:
#      print(3)
      ans += min(x)
      x = list(map(lambda i:i-min(x), x))
      water(x)
  return ans    
  
print(water(h))

Submission status: Wrong Answer

Solution code:
n = int(input())
h = list(map(int, input().split()))
gap = [0]*n
mt = []
va = []
for i in range(n-1):
    gap[i+1] = h[i+1]-h[i]
    if gap[i] > 0: 
        if gap[i+1] < 0:
            mt.append(h[i])
    else:
        if gap[i+1] > 0:
            va.append(h[i])
if gap[-1] > 0 :
    mt.append(h[-1])
if mt == []:
    mt.append(h[0])
ans = 0
for x in mt:
    ans += x
for x in va:
    ans -= x
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
h = list(map(int, input().split()))
gap = [0]*n
mt = []
va = []
for i in range(n-1):
    gap[i+1] = h[i+1]-h[i]
    if gap[i] >= 0: 
        if gap[i+1] < 0:
            mt.append(h[i])
    else:
        if gap[i+1] > 0:
            va.append(h[i])
if gap[-1] > 0:
    mt.append(h[-1])
if mt == []:
    mt.append(h[0])
ans = 0
for x in mt:
    ans += x
for x in va:
    ans -= x
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
h = list(map(int, input().split()))
gap = [0]*n
mt = []
va = []
for i in range(n-1):
    gap[i+1] = h[i+1]-h[i]
    if gap[i] >= 0: 
        if gap[i+1] < 0:
            mt.append(h[i])
    else:
        if gap[i+1] > 0:
            va.append(h[i])
if gap[-1] > 0 or mt == []:
    mt.append(h[-1])
ans = 0
for x in mt:
    ans += x
for x in va:
    ans -= x
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
h = list(map(int, input().split()))
gap = [0]*n
mt = []
va = []
for i in range(n-1):
    gap[i+1] = h[i+1]-h[i]
    if gap[i] >= 0: 
        if gap[i+1] < 0:
            mt.append(h[i])
    else:
        if gap[i+1] > 0:
            va.append(h[i])
if gap[-1] > 0:
    mt.append(h[-1])
ans = 0
for x in mt:
    ans += x
for x in va:
    ans -= x
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input().strip())
H = list(map(int, input().strip().split(' ')))+[0]
count = 0
for _ in range(max(H)-1):
  l = 0
  for r in range(1,len(H)):
    if H[r] < H[l]:
      # 水抜き
      H[l:r] = [h-1 for h in H[l:r]]
      count += 1
      l = r
    elif H[r] > H[l]:
      # 水抜きしない
      l = r
    else:
      continue
print(count+1)

Submission status: Wrong Answer

Solution code:
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
N = input()
h = [int(x) for x in input().split()]

hmax = max(h)
count = 0
for i in range (hmax):
    a = h.count(0)
    count += a
    h = [x-1 if x >= 1 else 0 for x in h]

print(count)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

else:
    diff = np.diff(h).tolist()
    max_list = []
    min_list = []

    sign = 1
    for i, d in enumerate(diff):
        if d > 0 and sign == -1:
            sign = 1
            min_list.append(h[i])

        if d < 0 and sign == 1:
            sign = -1
            max_list.append(h[i])
    if diff[-1] > 0:
        max_list.append(h[-1])

    def add(val, max_list, min_list):
        if len(max_list) > 1:
            min_val = min(min_list)
            split_index = min_list.index(min_val)+1
            return min_val-val + \
                add(min_val, max_list[:split_index], min_list[:split_index-1]) + \
                add(min_val, max_list[split_index:], min_list[split_index:])
        else:
            return max_list[0]-val

    if max(h) == 0:
        print(0)
    else:
        print(add(0, max_list, min_list))

Submission status: Wrong Answer

Solution code:
N = int(input())

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

m = [[0 for _ in range(max(H) + 1)] for _ in range(N)]

for i in range(N):
    for j in range(H[i]):
        m[i][j] = 1

for i in range(N):
    print(''.join(map(str, m[i])))

c = 0
for j in range(max(H)):
    prev = 0
    for i in range(N):
        if prev == 0 and m[i][j] == 1:
            c += 1
        prev = m[i][j]

print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

m = [[0 for _ in range(max(H) + 1)] for _ in range(N)]

for i in range(N):
    for j in range(H[i]):
        m[i][j] = 1

c = 0
for j in range(max(H)):
    prev = 0
    for i in range(N - 1):
        if prev == 0 and m[i][j] == 1:
            c += 1
        prev = m[i][j]
    if m[i][j] == 1:
        c += 1

print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a = [0]*N
answer = 0
while(True):
    area_flag = False
    area = 0
    comp_cnt = 0
    # print(a)
    for i in range(N):
        if a[i]>=h[i] and h[i]==0:
            comp_cnt+=1
            if area_flag:
                area += 1
            area_flag = False
            continue
        elif a[i]>=h[i]:
            comp_cnt+=1
            if area_flag:
                area += 1
            area_flag = False
            continue
        area_flag = True
        a[i]+= 1
    if area_flag:
        area += 1
    answer += area
    # print(answer)
    if comp_cnt == N: 
        answer += min(h)
        break


print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

#←から一個ずつ配列にアペンドしてって下がったら切ってminとmaxの差を加算
answer = 0
array = [h[0]]
min_sin = 0
for i in range(1,N):
    # print(answer)
    
    array.append(h[i])
    # print(array)
    if h[i] < h[i-1]:
        if i+1 < N and h[i+1] < h[i]:continue
        if min_sin > min(array):
            answer += max(array)-min_sin
        else:
            answer += max(array)-min(array)
        array = [array[-1]]
        min_sin = array[-1]
    else:
        continue

    
answer += max(array)-min(array)

answer += min(h)

print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

#←から一個ずつ配列にアペンドしてって下がったら切ってminとmaxの差を加算
answer = 0
array = [h[0]]
for i in range(1,N):
    # print(answer)
    
    array.append(h[i])
    # print(array)
    if h[i] < h[i-1]:
        if i+1 < N and h[i+1] < h[i]:continue
        answer += max(array)-min(array)
        array = [array[-1]]
    else:
        continue

    
answer += max(array)-min(array)

answer += min(h)

print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

#←から一個ずつ配列にアペンドしてって下がったら切ってminとmaxの差を加算
answer = 0
array = [h[0]]
for i in range(1,N):
    print(answer)
    
    array.append(h[i])
    print(array)
    if h[i] < h[i-1]:
        if i+1 < N and h[i+1] < h[i]:continue
        answer += max(array)-min(array)
        array = [array[-1]]
    else:
        continue

    
answer += max(array)-min(array)

answer += min(h)

print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

#←から一個ずつ配列にアペンドしてって下がったら切ってminとmaxの差を加算
answer = 0
array = [h[0]]
for i in range(1,N):
    # print(answer)
    
    array.append(h[i])
    # print(array)
    if h[i] < h[i-1]:
        answer += max(array)-min(array)
        array = [array[-1]]
    else:
        continue

    
answer += max(array)-min(array)

answer += min(h)

print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())

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


answer = 0

h_list = [h]
# print(min(h_list[0]))
# print(len(h_list))
while(1):
    if len(h_list)==0:break
    h_list_sub = []
    for i in range(len(h_list)):
        if h_list[i] == []: break
        min_h = min(h_list[i])
        # print(min_h)
        answer += min_h
        min_ind = []
        for j in range(len(h_list[i])):
            if h_list[i][j]== min_h:
                min_ind.append(j)

        for j in range(len(h_list[i])):
            h_list[i][j] -= min_h
        # print(min_ind)
        for j in range(0,len(min_ind)):
            if j == 0 and min_ind[j]!=0:
                h_list_sub.append(h_list[i][:min_ind[j]])
            elif j == 0 and min_ind[j]==0:
                continue
            # elif j == len(min_ind)-1:
            #     h_list_sub.append(h_list[i][min_ind[j]+1:])
            else:
                h_list_sub.append(h_list[i][min_ind[j-1]+1:min_ind[j]])
        # print(min_ind[-1])
        if min_ind[-1]!=len(h_list[i])-1:
            # print("ok")
            h_list_sub.append(h_list[i][min_ind[-1]+1:])


    h_list = h_list_sub
    # print(h_list)

print(answer)

Submission status: Wrong Answer

Solution code:
def f(k):
  global N,h,update
  if k==N:
    return -1
  if h[k]==0:
    return k
  else:
    h[k]-=1
    update=True
    f(k+1)


update=False
N=int(input())
h=list(map(int,input().split(" ")))
r=0
s=0

while True:
  f(s)
  if update:
    r+=1
    while s!=N-1 and h[s]==0:
      s+=1
    update=False
  else:
    break

print(r)

Submission status: Wrong Answer

Solution code:
def f(k):
  global N,h,update
  if k==N:
    return -1
  if h[k]==0:
    return k
  else:
    h[k]-=1
    update=True
    f(k+1)


update=False
N=int(input())
h=list(map(int,input().split(" ")))
r=0
s=0

while True:
  f(s)
  if update:
    r+=1
    while s!=N and h[s]==0:
      s+=1
    update=False
  else:
    break

print(r)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

#←から一個ずつ配列にアペンドしてって下がったら切ってminとmaxの差を加算
answer = 0
array = [h[0]]
for i in range(1,N):
    # print(answer)
    array.append(h[i])
    if h[i] < h[i-1]:
        answer += max(array)-min(array)
        array = [array[-1]]
    else:
        continue

    
answer += max(array)-min(array)

answer += min(h)

print(answer)

Submission status: Wrong Answer

Solution code:
N = int(input())
h = list(map(int, input().split()))
f_h = [0 for i in range(N)]
judge = [False if f_h[i] != h[i] else True for i in range(N)]

ct = 0
while False in judge:
    flg = 0
    for i in range(N):
        if judge[i] is False:
            flg = 1
            f_h[i] += 1
            if f_h[i] == h[i]:
                judge[i] = True
        else:
            if flg == 1:
                ct += 1
                flg = 0
            else:
                pass
print(ct)

Submission status: Wrong Answer

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

cnt = 1
for H in range(max(h)):
    #print(h)
    for i in range(N):
        if h[i] >0:
            h[i] -= 1
            if h[i+1] == 0:
                cnt +=1

print(cnt)

Submission status: Wrong Answer

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

cnt = 1
for H in range(max(h)-1):
    #print(h)
    for i in range(N):
        if h[i] >0:
            h[i] -= 1
            if h[i+1] == 0:
                cnt +=1

print(cnt)

Submission status: Wrong Answer

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

def mizu(h, s):
	for k in range(s, len(h)):
		if h[k] > 0:
			h[k] -= 1
		else:
			break
	return h
	
def find_start(h):
	s = 0
	for k in range(len(h)):
		if h[k] > 0:
			s = k
			break
	return s

def is_finish(h):
	if sum(h) == 0:
		return True
	else:
		return False

count = 0
while True:
	s = find_start(h)
	h = mizu(h, s)
	count += 1
	if is_finish(h):
		break

print(count)

Submission status: Wrong Answer

Solution code:
n = int(input())
h_list = list(map(int, input().split()))
left = 0
right = 0
cnt = 0
while True:
    stop = False
    for i in range(left, n):
        if h_list[i] <= 0:
            stop = True
            right = i
            break
    if not stop and cnt != 0:
        right = n
    else:
        break

    h_list[left:right] = [h - 1 for h in h_list[left:right]]
    cnt += 1

    stop = False
    for i in range(right, n):
        if h_list[i] > 0:
            stop = True
            left = i
            break
    if not stop:
        for i in range(0, n):
            if h_list[i] > 0:
                stop = True
                left = i
                break
        if not stop:
            break
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
h_list = list(map(int, input().split()))
left = 0
right = 0
cnt = 0
while True:
    stop = False
    for i in range(left, n):
        if h_list[i] <= 0:
            stop = True
            right = i
            break
    if not stop:
        right = n

    h_list[left:right] = [h - 1 for h in h_list[left:right]]
    cnt += 1

    stop = False
    for i in range(right + 1, n):
        if h_list[i] > 0:
            stop = True
            left = i
            break
    if not stop:
        for i in range(0, n):
            if h_list[i] > 0:
                stop = True
                left = i
                break
        if not stop:
            break
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.append(0)
s = 0
ans = 0
while s <= N-1:
  if L[s] == 0:
    s += 1
    continue
  for i in range(s,N+1):
    if L[i] > 0:
      L[i] -= 1
    else:
      ans += 1
      break
  if L == [0]*(N+1):
    print(ans)
    exit()

Submission status: Wrong Answer

Solution code:
N = int(input())
h = list(map(int, input().split()))
P = []
t = []
for i in range(N - 1):
    if h[i] <= h[i+1]:
        t.append(h[i])
    else:
        t.append(h[i])
        P.append(t)
        t = []
t.append(h[N - 1])
P.append(t)
ans = 0
for j in range(len(P)):
    ans += max(P[j])
    
ans -= min(P[len(P)-1])

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
l=np.array(list(map(int,input().split())))
c=0
for i in range(n):
  for j in range(i,n):
    if 0 in l[i:j+1]:
      pass
    else:
      l[i:j+1]=l[i:j+1]-np.array([min(l[i:j+1])]*(j-i+1))
      c+=min(l[i:j+1])
print(c)

Submission status: Wrong Answer

Solution code:
import numpy as np
n=int(input())
l=np.array(list(map(int,input().split())))
c=0
for i in range(n):
  for j in range(i,n):
    if 0 in l[i:j+1]:
      pass
    else:
      l[i:j+1]=l[i:j+1]-np.array([1]*(j-i+1))
      c+=1
print(c)

Submission status: Wrong Answer

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


def mizuyari(partial_list):
    count = 0
    print(partial_list)
    if len(partial_list) == 0:
        print('end return------------ 0 ')
        return 0
    if len(partial_list) == 1:
        print('end return------' + str(partial_list[0]))
        return partial_list[0]
    # リストに0がない場合は，どれかが0になるまでリストの各要素の数値を減らす
    if min(partial_list) != 0:
        count += min(partial_list)
        print('count --------' + str(min(partial_list)))
        partial_list = list(map(lambda x: x-min(partial_list), partial_list))
    zero_index = []
    # 分割
    for i in range(len(partial_list)):
        if partial_list[i] == 0:
            zero_index.append(i)
    print(zero_index)
    bunkatsu = []
    bunkatsu.append(partial_list[0:zero_index[0]])
    for i in range(0, len(zero_index)-1):
        bunkatsu.append(partial_list[zero_index[i]+1:zero_index[i+1]])
    bunkatsu.append(partial_list[zero_index[(len(zero_index)-1)]+1:len(partial_list)])
    print(bunkatsu)
    for i in bunkatsu:
        count += mizuyari(i)
    return count


print(mizuyari(h))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

# 再帰的にカウントしていく
def count(h):
    print(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)
        print(output)
        return output
    else:
        return 0

h = H
print(count(h))