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_flowers = int(input())
heights = [int(h) for h in input().split()]
diff = [h for h in heights]
n_pour = 0

for i in range(n_flowers):
  while diff[i] > 0:
    for j in range(i, n_flowers):
      if diff[j] <= 0:
        diff[i:j] = [d - 1 for d in diff[i:j]]
        n_pour += 1
        break
      elif j == n_flowers - 1:
        diff[i:n_flowers] = [d - 1 for d in diff[i:n_flowers]]
        n_pour += 1
        break

print(n_pour)

Submission status: Accepted

Solution code:
n = int(input())
hs = list(map(int, input().split()))
hs.append(0)
ans = 0
for i in range(len(hs)):
    if len(set(hs))==1 and 0 in hs:
        break
    elif hs[i]==0:
        continue
    else:
        while True:
            if hs[i]==0:
                break
            for j in range(i,len(hs)):
                if hs[j] ==0:
                    for k in range(i,j):
                        hs[k]-=1
                    ans+=1
                    break
print(ans)

Submission status: Accepted

Solution code:
def dfs(_h):
    if len(_h) == 0:
        return
    min_h = min(_h)
    global ans
    ans += min_h
    tmp = []
    for i in range(len(_h)):
        if _h[i] == min_h:
            tmp.append(i)
    tmp.append(N)
    head = 0
    sp = []
    for i in tmp:
        sp.append([x - min_h for x in _h[head:i]])
        head = i+1
    for s in sp:
        dfs(s)

N = int(input())
h = [int(x) for x in input().split()]
ans = 0
dfs(h)
print(ans)

Submission status: Accepted

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

ans = 0

while sum(hn) > 0:
    ans+= 1
    flag = False
    for i in range(n):
        if hn[i] > 0:
            hn[i] -= 1
            flag = True
        elif flag is True:
            break

print(ans)

Submission status: Accepted

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

c = 0
l = 0
while True:
    # update l
    while l != n and hn[l] == 0:
        l += 1
    if l == n:
        break

    # update r
    r = l
    min_num = 1e3
    while r != n and hn[r] != 0:
        min_num = min(min_num, hn[r])
        r += 1
    
    # watering
    for i in range(l, r):
        hn[i] -= min_num
    c += min_num

print(c)

Submission status: Accepted

Solution code:
n = int(input())
f = list(map(int,input().split()))
Garden = [0] * len(f)
Count = 0
cflag = False

while(1):
    for index,item in enumerate(f):
        if item != Garden[index]:
            Garden[index] += 1
            cflag = True
        else:
            if cflag == True:
                Count += 1
            cflag = False
            
    if cflag == True:
        Count += 1
        cflag = False
        
    if f == Garden:
        break

print(Count,end="")

Submission status: Accepted

Solution code:
# coding: utf-8


def count_mountain(li):
    pre = False
    count = 0
    for i in range(1, max(li) + 1):
        for j in range(len(li)):
            now = li[j] >= i
            if pre == False and now == True:
                count += 1
            pre = now
        pre = False

    return count


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

    print(count_mountain(h))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

'''
ゼロでないhのクラスタを見つけ、そのクラスタの最小値を引いていく

e.g.
[3 1 2 3 1]ならば、まず全てゼロでは無いので、最小値1を全体に対して引く。
すると、[2 0 1 2 0]となる。
このとき、[{2} 0 {1 2} 0]といった具合に、ゼロでない部分をまとめていく。
まとめた部分に対しても、同様の処理を行なっていく。
'''


def clustering(h):
    cluster = []
    cluster_pos = []
    small_cluster = []
    small_cluster_pos = []
    for index, value in enumerate(h):
        if(value != 0):
            small_cluster.append(value)
            small_cluster_pos.append(index)
        else:
            cluster.append(small_cluster)
            cluster_pos.append(small_cluster_pos)
            small_cluster = []
            small_cluster_pos = []
    if(value != 0):
        cluster.append(small_cluster)
        cluster_pos.append(small_cluster_pos)

    return cluster, cluster_pos

while(np.sum(h)>0):
    cluster, cluster_pos = clustering(h)
    for i in range(len(cluster)):
        if(len(cluster[i])==0):
            continue

        minimum = np.min(cluster[i])
        counter += minimum
        h[cluster_pos[i]]-= minimum

print(counter)

Submission status: Accepted

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

count = 0
while True:
    for n in range(N):
        if H[n] == 0:
            continue
        elif n == N-1:
            count += 1
        elif H[n+1] == 0:
            count += 1
        H[n] -= 1
    if sum(H) == 0:
        break
print(count)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

def lsearch(h): # 左端を探す
    for i in range(len(h)):
        if h[i] != 0:
            return i
    return len(h)-1

def rsearch(h, l): # 右端を探す
    for i in range(l, len(h)):
        if h[i] == 0:
            return i
    return len(h)

n=int(input())
h=list(map(int, input().split()))
cnt=0
while any(h):
    l=lsearch(h)
    r=rsearch(h, l)
    for i in range(l, r):
        h[i] -= 1
    cnt+=1
print(cnt)

Submission status: Accepted

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

rtn=0
btm=0
top=0
for Hi in HN:
  if top<Hi:
    top=Hi
  elif top==btm and btm>Hi:
    top=Hi
    btm=Hi    
  elif top>Hi:
    rtn+=top-btm
    top=Hi
    btm=Hi
rtn+=top-btm
print(rtn)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
# import numpy as np


s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


def search(target):
    if len(target) == 0:
        return 0
    elif len(target) == 1:
        return target[0]
    else:
        count = 0
        if 0 not in target:
            for i in range(len(target)):
                target[i] -= 1
            count += 1
        cand = []
        while 0 in target:
            idx = target.index(0)
            cand.append(target[:idx])
            target = target[idx+1:]
        cand.append(target)
        for s in cand:
            count += search(s)
        return count


N = d_in()
h_list = ds_in()

print(search(h_list))

Submission status: Accepted

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

co=0
su=sum(H)
hana=0
while su>0 :
    #print(hana)
    if H[hana]>0 :
        H[hana]-=1
        su-=1
        if hana==n-1 or H[hana+1]==0 :
            co+=1
    hana+=1
    if hana==n :
        hana=0

print(co)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
hn = list(map(int, input().split()))

def f(hn):
    cnt = min(hn)
    hn2 = [h - cnt for h in hn]
    sub_hn = []
    tmp = []
    for h in hn2:
        if h == 0:
            if len(tmp) > 0:
                sub_hn.append(tmp)
                tmp = []
        else:
            tmp.append(h)

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

    return cnt + sum(map(f, sub_hn))

print(f(hn))

Submission status: Accepted

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

def calc(x:list, count:int, before:int):
    if len(x) == 0:
        return 0
    sortedx = sorted(x)
    count = count + (sortedx[0] - before)
    afterList = []

    for _ in range(sortedx.count(sortedx[0])):
        idx = x.index(sortedx[0])
        if len(x[0:idx]) > 0:
            afterList.append(x[0:idx])
        del x[0:idx+1]
    if len(x) > 0:
        afterList.append(x)

    for x in afterList:
        count = calc(x, count, sortedx[0])

    return count

print(calc(hList, 0, 0))

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
h=np.array(list(map(int,input().split())))
ans=0
now=0
while(now<N):
    init=now
    while(1):
        if h[now]==0:
            if now==init:
                now+=1
                break
            else:
                h[init:now]-=1
                ans+=1
                now=init
                break
        elif now==N-1:
            h-=1
            ans+=1
            now=init
            break            
        now+=1
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
H = list(map(int, input().split()))
hnow = 0
cou = 0

for i in range(n):
    h = H[i]
    if h > hnow:
        cou += (h - hnow)
        hnow = h
    elif h < hnow:
        hnow = h

print(cou)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
h.append(0)
c = 0
while sum(h):
    for i in range(len(h)):
        if (0 < h[i] and h[i + 1] == 0):
            c += 1
    for i in range(len(h)):
        if h[i] != 0:
            h[i] -= 1

print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

count = 0
p = 0

for h in map(int, input().split()):
    count += max(0, h-p)
    p = h

print(count)

Submission status: Accepted

Solution code:
def count(h_list):
    if len(h_list) == 0:
        return 0
    if len(h_list) == 1:
        return h_list[0]
    if 0 not in h_list:
        return 1 + count([x-1 for x in h_list])
    elif all(x == 1 for x in h_list):
        return 1
    return sum([count(x) for x in divide_by_zero(h_list)])


def divide_by_zero(arr):
    non_zero = []
    for ar in arr:
        if ar == 0:
            if non_zero:
                yield non_zero
                non_zero = []
        else:
            non_zero.append(ar)
    yield non_zero


n = int(input())
h_list = [int(x) for x in input().split(" ")]
print(count(h_list))

Submission status: Accepted

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

while max(h) > 0:
    hmax = max(h)
    hmaxi = h.index(hmax)
    h[hmaxi] -= 1
    count += 1
    for i in range(N-hmaxi-1):
        if hmaxi == N-1:
            break
        elif h[hmaxi+i+1] == hmax:
            h[hmaxi+i+1] -= 1
        else:
            break

print(count)

Submission status: Accepted

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

h.append(0)
ans = 0
while(sum(h) > 0):
    interval_start = 0
    interval_end = 0
    interval_min = 101
    in_interval = False
    for i in range(N + 1):
        if not in_interval:
            if h[i] == 0:
                continue
            else:
                in_interval = True
                interval_start = i
                interval_min = h[i]
        else:
            if h[i] == 0:
                in_interval = False
                interval_end = i
                ans += interval_min
                for j in range(interval_start, interval_end):
                    h[j] -= interval_min
            else:
                interval_min = min(interval_min, h[i])           
print(ans)

Submission status: Accepted

Solution code:
a = int(input())
b = input().split()
c = []
for i in b:
    c.append(int(i))

max = 0
ans = 0
for i in c:
    if max < i:
        ans += i - max
    max = i

print(ans)

Submission status: Accepted

Solution code:
import math
import sys
from operator import itemgetter

# N, K = map(int, input().split())
# h = list(map(int, input().split()))
# S = [input() for _ in range(H)]
# X = [[0 for j in range(100)] for i in range(200)]


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

## solve
ans = 0

while (max(h) > 0):
	k = 0
	j = 0
	for i in range(len(h)):
		if (h[i] > 0):
			k = i
			break
	j = 0
	for i in range(k, len(h)):
		if (h[i] != 0):
			j += 1
		else:
			break

	for i in range(k, k+j):
		h[i] -= 1
	ans += 1
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

def solve(h):
    if len(h) == 0:
        return 0
    if len(h) == 1:
        return h[0]
    if max(*h) == 0:
        return 0
    min_v = min(*h)
    if min_v != 0:
        return min_v + solve([a - min_v for a in h])
    zero_ind = h.index(0)

    aaa, bbb = h[:zero_ind], h[zero_ind + 1:]
    return solve(aaa) + solve(bbb)


def main():
    input()
    h = [int(a) for a in input().split()]

    res = solve(h)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
H=[int(i) for i in input().split()]
W=[0]*N
num=0
i=0
while i<N:
    if W[i]==H[i]:
        i+=1
        continue
    for j in range(i,N):
        if W[j]==H[j]:
            break
        W[j]+=1
    num+=1
print(num)

Submission status: Accepted

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

# print(N)
# print(H)

mizu_num=0
renzoku= False
while(True):
    #水やりがいらない花を探す
    for i in range(len(H)):
        if H[i]>0:
            H[i]-=1
            renzoku=True
        else:
            if renzoku==True and i!=0:
                mizu_num+=1
            renzoku=False
    # print(renzoku,mizu_num)
    if renzoku==True:
        mizu_num+=1

    # print(H, mizu_num)

    if not any(H):
        break

print(mizu_num)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding:utf-8 -*-


def f(flowers:list):
  li = []
  n_li = []
  for flower in flowers:
    if flower == 0:
      if len(n_li) != 0:
        li += [n_li]
        n_li = []
    else:
      n_li += [flower-1]
  li += [n_li] if len(n_li) != 0 else []
  count = len(li)
  if li == []:
    return 0

  _sum = 0
  for l in li:
    _sum += f(l)
  return count + _sum



if __name__=='__main__':
  N = int(input())
  flowers = [int(x) for x in input().split()]
  print(f(flowers))

Submission status: Accepted

Solution code:
n = int(input())
lst = input().split(" ")
a = [int(item) for item in lst]
ans = 0
while sum(a) > 0:
    left = 0
    right = -1
    for i in range(len(a)):
        if not a[i] == 0:
            left = i
            break
    for i in range(left, len(a)):
        if a[i] == 0:
            right = i
            break
    if -1 == right:
        right = len(a)
    right = right - 1
    min_val = a[left]
    for i in range(left, right + 1):
        min_val = min(min_val, a[i])
    ans += min_val
    for i in range(left, right + 1):
        a[i] -= min_val
print(ans)

Submission status: Accepted

Solution code:
import time
s=0
def min(arr):
    m=arr[0]
    for i in range(1,len(arr)):
        if m>arr[i]:
            m=arr[i]
    return m

def mizu(arr):
    global s
    if len(arr)==1:
        
        s+=arr[0]
    elif len(arr)>1:
        m=min(arr)
        b=[]

        for i in range(len(arr)):
            arr[i]-=m
        s+=m

        for i in range(len(arr)):
            if arr[i]!=0:
                b.append(arr[i])
            else:
                mizu(b)
                b=[]
        if arr[-1]!=0:
            mizu(b)

n=int(input())
k=list(map(int,input().split()))
mizu(k)
print(s)

Submission status: Accepted

Solution code:
class Solver(object):
    def __init__(self):
        self._input()
        self._solve()

    def _input(self):
        self.n = int(input())
        self.h_s = list(map(int, input().split(" ")))

    def _solve(self):
        counter = 0
        while True:
            watered = False
            watering = False
            for i in range(self.n):
                if self.h_s[i] > 0:
                    if not watering:
                        watered = True
                        watering = True
                        counter += 1
                    self.h_s[i] -= 1
                else:
                    if watering:
                        watering = False
            if watered == False:
                break
        print(counter)


if __name__ == "__main__":
    Solver()

Submission status: Accepted

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

def count_divided(H):
    indices = [i for i, h in enumerate(H) if h > 0]
    counter = 0
    for i in range(len(indices) - 1):
        if H[indices[i] + 1] <= 0:
           counter += 1
    if len(indices) != 0:
        if indices[-1] == len(H) - 1:
            counter += 1
        if H[-1] <= 0:
            counter += 1
    return counter

def all_negative(H):
    flag = True
    for h in H:
        if h >= 0:
            flag = False
    return flag

counter = 0

while(not all_negative(H)):
    counter += count_divided(H)
    H = [h - 1 for h in H]

print(counter)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int ,input().split(" ")))
flag = 0
h.append(0)
ct = 0
while sum(h):
    for i in range(len(h)):
        if h[i]:
            h[i] = h[i] - 1
            flag = 1
        else:
            if flag:
                ct += 1
                flag = 0
print(ct)

Submission status: Accepted

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

def l_r(lis):
    l = 0
    r = 0
    for i in range(len(lis)):
        if lis[i] > 0:
            l = i
            break
    if l == len(lis)-1:
        r = l
    else:
        for i in range(l+1,len(lis)):
            if lis[i] <= 0:
                r = i
                break
            else:
                r = len(lis)-1
    return l,r

if h == []:
    print(0)
else:
    counta = 0
    while True:
        if all([x<=0 for x in h]):
            break
        else:
            l,r = l_r(h)
            for i in range(l,r+1):
                h[i] = h[i] - 1
            counta += 1
    print(counta)

Submission status: Accepted

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

def l_r(lis):
    l = 0
    r = 0
    for i in range(len(lis)):
        if lis[i] > 0:
            l = i
            break
    if l == len(lis)-1:
        r = l
    else:
        for i in range(l+1,len(lis)):
            if lis[i] <= 0:
                r = i
                break
            else:
                r = len(lis)-1
    return l,r

if h == []:
    print(0)
else:
    counta = 0
    while True:
        if all([x<=0 for x in h]):
            break
        else:
            l,r = l_r(h)
            for i in range(l,r+1):
                h[i] = h[i] - 1
            counta += 1
    print(counta)

Submission status: Accepted

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

def sub(arr, count):
    while 1:
        if arr == []:
            return count
        elif 0 in arr:
            index = arr.index(0)
            arr1 = arr[0:index]
            arr2 = arr[index + 1:len(arr)]
            count = count + sub(arr1, 0) + sub(arr2, 0)
            return count
        else:
            arr = [i - 1 for i in arr]
            count += 1 

print(sub(arr, 0))

Submission status: Accepted

Solution code:
#ABC116C-GrandGarden.py

N = int(input())
H = input().split()
h = []
r, l = 0, 0
times = 0

for i in range(N):
    h.append(int(H[i]))

h.append(0)

def decidePosition(h):
    m = len(h)
    n = len(h)
    flag = 0
    for i in range(len(h)):
        if h[i] != 0 and flag == 0:
            m = i
            flag = 1
        elif h[i] == 0 and flag == 1:
            n = i
            break
    return m, n

while r != len(h):
    r, l = decidePosition(h)
    for i in range(l-r):
        i += r
        h[i] = h[i] - 1
    times += 1

print(times-1)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
# Luis Yoichi Morales January 2019

#from sys import stdin

#N= int(raw_input())
#h=[]
#h.extend( map(int, stdin.readline().split())   )

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



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

Submission status: Accepted

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

cnt = h[0]
for i in range(len(h) - 1):
    sa = h[i+1] - h[i]
    if sa > 0:
        cnt += sa

print(cnt)

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
#  import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
import sys
import numpy as np


#  sys.setrecursionlimit(10**7)
#  inf = 10 ** 20
#  eps = 1.0 / 10**10
#  mod = 10**9+7
#  dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
#  ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    N = I()
    h = LI()
    h = np.array(h)
    #  print(N, h)
    # 0以上の塊をみつける
    # その塊に水をやる
    # 以上を繰り返す
    count = 0
    while True:
        #  print(h)
        first_non_zero_idx = [idx for idx, i in enumerate(h) if i != 0]
        if len(first_non_zero_idx) == 0:
            # 終了
            print(count)
            return
        else:
            first_non_zero_idx = first_non_zero_idx[0]

        first_zero_idx = [idx for idx, i in enumerate(h) if idx > first_non_zero_idx and i == 0]
        if len(first_zero_idx) == 0:
            # 0がないので最後まで水をやる
            first_zero_idx = len(h)
        else:
            first_zero_idx = first_zero_idx[0]

        # 水をやる
        min_value = min(h[first_non_zero_idx:first_zero_idx])
        h[first_non_zero_idx:first_zero_idx] -= min_value
        count += min_value





main()

Submission status: Accepted

Solution code:
import sys
import numpy as np


#  sys.setrecursionlimit(10**7)
#  inf = 10 ** 20
#  eps = 1.0 / 10**10
#  mod = 10**9+7
#  dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
#  ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    N = I()
    h = LI()
    h = np.array(h)
    #  print(N, h)
    # 0以上の塊をみつける
    # その塊に水をやる
    # 以上を繰り返す
    count = 0
    while True:
        #  print(h)
        first_non_zero_idx = [idx for idx, i in enumerate(h) if i != 0]
        if len(first_non_zero_idx) == 0:
            # 終了
            print(count)
            return
        else:
            first_non_zero_idx = first_non_zero_idx[0]

        first_zero_idx = [idx for idx, i in enumerate(h) if idx > first_non_zero_idx and i == 0]
        if len(first_zero_idx) == 0:
            # 0がないので最後まで水をやる
            first_zero_idx = len(h)
        else:
            first_zero_idx = first_zero_idx[0]

        # 水をやる
        h[first_non_zero_idx:first_zero_idx] -= 1
        count += 1





main()

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
import numpy as np


sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]


def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)

def main():
    N = I()
    h = LI()
    h = np.array(h)
    #  print(N, h)
    # 0以上の塊をみつける
    # その塊に水をやる
    # 以上を繰り返す
    count = 0
    while True:
        #  print(h)
        first_non_zero_idx = [idx for idx, i in enumerate(h) if i != 0]
        if len(first_non_zero_idx) == 0:
            # 終了
            print(count)
            return
        else:
            first_non_zero_idx = first_non_zero_idx[0]

        first_zero_idx = [idx for idx, i in enumerate(h) if idx > first_non_zero_idx and i == 0]
        if len(first_zero_idx) == 0:
            # 0がないので最後まで水をやる
            first_zero_idx = len(h)
        else:
            first_zero_idx = first_zero_idx[0]

        # 水をやる
        h[first_non_zero_idx:first_zero_idx] -= 1
        count += 1





main()

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

list = [int(i) for i in input().split()]
count = 0

for i in range(n-1):
    if list[i+1] - list[i] > 0:
        count += list[i+1] - list[i]
count += list[0]
print(count)

Submission status: Accepted

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

ans = 0

for i in range(N+1):
    if(h_list[i] - h_list[i-1] > 0):
        ans += (h_list[i] - h_list[i-1])

print(ans)

Submission status: Accepted

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
l = input()
a = [int(i) for i in input().split()] 
n = 0
num = a[0]
for i in a:
  max = a[0]
  
  
  if i > a[n-1] and n > 0:
    num += (i-a[n-1])
  n += 1
print(num)

Submission status: Accepted

Solution code:
n = int(input())
height = [0] + [int(i) for i in input().split()]
count = 0

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

print(count)

Submission status: Accepted

Solution code:
# import numpy as np
# import gc

if __name__ == '__main__':
    n=int(input())
    l=list(map(int, input().split()))
    #v:vertex
    ans,v=0,0
    for i in range(1,n):
        v+=min(l[i-1],l[i])
    print(sum(l)-v)

Submission status: Accepted

Solution code:
N = int(input())
height = [0] + [int(i) for i in input().split()]
count = 0
for i in range(N):
  if height[i+1] > height[i]:
    count += height[i+1] - height[i]
print(count)

Submission status: Accepted

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
c,h = 0,h+[0]
while sum(h):
	f = 0
	for i in range(n+1):
		if f and h[i]==0:
			m,f = min(h[l:i]),0
			for j in range(l,i): h[j]-=m
			c+=m
		elif 1-f and h[i]: f,l = 1,i
print(c)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
hs = [0] + input().split()
cnt = 0

for i in range(1, len(hs)):
  cnt += max(int(hs[i]) - int(hs[i-1]), 0)
print(cnt)

Submission status: Accepted

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

ans = h[0]

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

ans = 0
for i in range(100 + 1):
	#100まで
	for j in range(n+1):
		ishigher = h > i
		if(j==0):
			if ishigher[j] == True:
				ans += 1
		elif j==n:
			if ishigher[j-1] == True:
				ans += 1
		elif (ishigher[j] != ishigher[j-1]):
			ans += 1

# False→TrueとTrue→Falseを数えているから、2で割ったものが答え
print (ans//2) # 整数除算

Submission status: Accepted

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

ans = 0
for c_h in range(0, max_h):
    continueing = False
    for h_i in h:
        if not continueing and h_i > c_h:
            ans += 1
            continueing = True
        elif h_i <= c_h:
            continueing = False

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
parts = [h]

while True:
    found = False
    new_parts = []
    for part in parts:
        min_v = min(part)
        cnt += min_v
        if part:
            new_part = []
            for v in part:
                if v > min_v:
                    new_part.append(v - min_v)
                elif new_part:
                    new_parts.append(new_part)
                    new_part = []
            if new_part:
                new_parts.append(new_part)
            found = True
    if not found:
        break
    parts = new_parts

print(cnt)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
h = [int(a) for a in input().split()]
h = np.array([0] + h + [0]) # 最初と最後をゼロ埋め

diff = np.diff(h)

print(np.sum(diff[diff>0]))

Submission status: Accepted

Solution code:
# coding:utf-8
# C - Grand Garden

N = int(input())
h =  [int(i) for i in input().split()]
h += [0]
ans = 0
add = False

while True:
    mini_ans = 0
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                mini_ans += 1
            add = False
    if mini_ans == 0:
        break
    else:
        ans += mini_ans

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
h += [0]
add = False
ans = 0
for n in range(100):
    for i in range(N+1):
        if h[i] != 0:
            h[i] -= 1
            add = True
        else:
            if add:
                ans += 1
            add = False
print(ans)