Problem:


Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq  k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N  \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221



Sample input:
4
1 2 2 1


Sample output:
2

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
d=[]
a=[]
x=0
k=0
while k<len(h)-1:
  if h[k]==h[k+1]:
    h.pop(k)
    k-=1
  k+=1

for i in range(1,len(h)-1):
  if h[i-1]>h[i]<h[i+1]:
    a.append(h[i])
    x=1
for i in range(1,len(h)-1):
  if h[i-1]<h[i]>h[i+1]:
    d.append(h[i])
    x=1
               
if len(h) >= 2:
  if h[0]>=h[1]:
    d.append(h[0])
  if h[-1]>=h[-2]:
    d.append(h[-1])
  print(sum(d)-sum(a))
else:
  print(h[0])

Submission status: Accepted

Solution code:
n = int(input())

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

tmp = 0
ans = 0

for i in range(n) :
    if tmp <= h[i] :
        ans += h[i] - tmp
        tmp  = h[i]
    else :
        tmp = h[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
cnt = 0
 
while sum(h) > 0:
    min_h = max(h)
    i = h.index(min_h)
    #print(i)
    while i < len(h):
        if h[i] == min_h:
            h[i] -= 1
            i += 1
        else:
            break
    cnt += 1
 
print(cnt)

Submission status: Accepted

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

#連続して0以上の部分を見つける。
#それらの中で一番少ないやつに合わせて全体を引く。
#0のやつが出てくる。それに合わせてまた同じことを繰り返せばできる？
#単調増加している部分とそうでない部分で操作が変わる？0になる部分が端っこじゃない

#隣のやつとの差を足していくだけでええわ。隣のやつとはつながってるから、
ans = h[0]
for i in range(1,n):
  if h[i]-h[i-1] >= 0:
    ans += h[i]-h[i-1]
print(ans)

Submission status: Accepted

Solution code:
import os, sys, re, math

N = int(input())
h = list(map(lambda x: -1 * int(x),input().split(' ')))
cnt = 0

while sum(h) < 0:
	min_h = min(h)
	i = h.index(min_h)
	while i < len(h):
		if h[i] == min_h:
			h[i] += 1
			i += 1
		else:
			break
	cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
def tree(lst):
	if len(lst) == 0:
		return 0
	mi = min(lst)
	lst = [a - mi for a in lst]
	sum = mi
	start = 0
	for i in range(len(lst)):
		if lst[i] == 0:
			sum += tree(lst[start : i])
			start = i + 1
		elif i == len(lst) - 1:
			sum += tree(lst[start:])
	return sum
    
n = int(input())
h = [int(a) for a in input().split()]
print (tree(h))

Submission status: Accepted

Solution code:
import os, sys, re, math

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

def get_count(heights):
    count = 0
    while not 0 in heights:
        count += min(heights)
        heights = [n - min(heights) for n in heights]
    group = []
    groups = [group]
    for h in heights:
        if h != 0:
            group.append(h)
        else:
            group = []
            groups.append(group)
    for group in groups:
        if group:
            count += get_count(group)
    return count

print (get_count(H))

Submission status: Accepted

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

def doit(sub_hs):
    if len(sub_hs) == 0:
        return 0
    for i in range(len(sub_hs)):
        if sub_hs[i] == 0:
            a = 0
            if len(sub_hs[0:i]) > 0:
                min_h = min(sub_hs[0:i])
                a += min_h + doit([h - min_h for h in sub_hs[0:i]])
            if len(sub_hs) >= i:
                a += doit(sub_hs[i+1:])
            return a
    min_h = min(sub_hs)
    a = min_h + doit([h - min_h for h in sub_hs])
    return a
print(doit(hs))

Submission status: Accepted

Solution code:
n = int(input())
fs = list(map(int, input().split(' ')))
# 221
ans = 0
while sum(fs):
    min_val = 99999999999
    min_index = 0
    for i in range(n):
        if fs[i] and fs[i] < min_val:
            min_index = i
            min_val = fs[i]
    ans += min_val
    for l in range(min_index-1, -1, -1):
        if not fs[l]:
            break
        fs[l] -= min_val
    for r in range(min_index, n):
        if not fs[r]:
            break
        fs[r] -= min_val
print(ans)

Submission status: Accepted

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

def doit(s, e, t):
    if s == e:
        return 0
    ret = 1
    i = s
    for j in range(s, e):
        if h[j] == t:
            ret += doit(i, j, t + 1)
            i = j + 1
    ret += doit(i, e, t + 1)
    return ret

print(doit(0, n, 0) - 1)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
test = False
if test:
    n = 100
    from random import randint
    h = [randint(0, 100) for _ in range(n)]
else:
    n = int(input())
    h = list(map(int, input().split()))

a = [0] * n
cost = 0
changed = True
while changed:
    changed = False
    left = 0
    while left < n and a[left] == h[left]:
        left += 1
    right = left
    while right < n and a[right] < h[right]:
        right += 1
    if left < n:
        cost += 1
        changed = True
        for i in range(left, right):
            a[i] += 1
print(cost)

Submission status: Accepted

Solution code:
n = int(input())

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

ans = 0
while True:
    if sum(hl) == 0:
        break
    ans += 1
    flag = False
    for idx, h in enumerate(hl):
        if h > 0:
            hl[idx] -= 1
            flag = True
        elif flag:
            break
        else:
            continue

print(ans)

Submission status: Accepted

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

c=0
cur=0
for i in h:
    if(cur<i):
        c+=i-cur
    cur=i
print(c)

Submission status: Accepted

Solution code:
# 2019-11-12 01:01:32(JST)
import sys
# import collections
# import math
# from string import ascii_lowercase, ascii_uppercase, digits
# from bisect import bisect_left as bi_l, bisect_right as bi_r
# import itertools
# from functools import reduce
# import operator as op
# from scipy.misc import comb # float
# import numpy as np 

def main():
    n, *h = [int(x) for x in sys.stdin.read().split()]
    if n == 1:
        print(h[0])
        sys.exit()

    mountain = [0]
    valley = [0]
    flat = 0
    i = 0
    while True:
        if h[i] == h[i+1]:
            if i < n-2:
                i += 1
                continue
            else:
                print(h[i])
                sys.exit()
        elif h[i] < h[i+1]:
            break
        else:
            mountain.append(h[0])
            break
    i = -1
    while True:
        if h[i-1] == h[i]:
            i -= 1
            continue
        elif h[i-1] > h[i]:
            break
        else:
            mountain.append(h[-1])
            break

    for i in range(1, n-1):
        if flat == 0:
            if h[i-1] < h[i] > h[i+1]:
                mountain.append(h[i])
            elif h[i-1] > h[i] < h[i+1]:
                valley.append(h[i])
            elif h[i-1] < h[i] == h[i+1]:
                flat = 1
            elif h[i-1] > h[i] == h[i+1]:
                flat = -1
        else:
            if flat == 1:
                if h[i] > h[i+1]:
                    mountain.append(h[i])
                    flat = 0
                elif h[i] < h[i+1]:
                    flat = 0
                else:
                    continue
            elif flat == -1:
                if h[i] < h[i+1]:
                    valley.append(h[i])
                    flat = 0
                elif h[i] > h[i+1]:
                    flat = 0
                else:
                    continue

    
    ans = sum(mountain) - sum(valley)
    print(ans)
    

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
ans = 0
for i in range(1,max(h)+1):
    jud = False
    for j in range(N):
        if h[j]>=i:
            if not jud:
                ans += 1
            jud = True
        else:
            jud = False
print(ans)

Submission status: Accepted

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

count = 0
left = 0
for flower in flowers:
  if flower > left:
    count += flower - left
  left = flower

print(count)

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0
interval = 0

for i in range(N):
  #区間が足りない時
  if height[i] > interval:
   #今の区間と必要な区間の差分を取って足し、新たな区間の数を定義する
   ##順番に注意 先にintervalに定義すると、
   ##ansがそれに影響されるためansが差分の分だけ余分にに大きくなる
    ans += height[i] - interval
    interval += height[i] - interval
  #区間が多い時、必要な数に減らす
  else:
    interval = height[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
array = list(map(int,input().split()))
ans = 0
while True:
    if max(array) == 0:
        break
    flg = False
    for i in range(N):
        if array[i] == 0:
            if flg:
                break
            continue
        array[i] -= 1
        flg = True
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
s = L[0]
ans = s
for i in range(0,N):
  if s <= L[i]:
    ans += L[i] - s
  s = L[i]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
ans=0 
l=[int(i) for i in input().split()]
while sum(l):
    c=0 
    f=0 
    for i in range(n):
        if l[i]!=0:
            l[i]-=1 
            c+=1 
        else: 
            if c:
                ans+=1 
                c=0 
    if c:
        ans+=1 
print(ans)

Submission status: Accepted

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

cnt = 0
while not all([x == 0 for x in hli]):
    sIdx = -1
    for i, h in enumerate(hli):
        if h != 0:
            sIdx = i
            break
    eIdx = sIdx + 1
    minNum = hli[sIdx]
    for i in range(sIdx+1, n):
        if hli[i] == 0:
            break
        eIdx = i + 1
        minNum = min(hli[i], minNum)
    for i in range(sIdx, eIdx):
        hli[i] -= minNum

    cnt += minNum
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int,input().split()))
H.append(0)
ans = 0
maxH = max(H)
for h in range(maxH):
    state = 0
    for n in range(N+1):
        if H[n] > 0:
            H[n] -= 1
            state = 1
        elif H[n] == 0 and state == 1:
            state = 0
            ans += 1
        else:
            pass
print(ans)

Submission status: Accepted

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

ans = 0
cur = 0
for ai in a:
    if ai < cur:
        cur = ai
    else:
        ans += ai - cur
        cur = ai

print(ans)

Submission status: Accepted

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

l = []
for i in range(n):
    tmp = h[i]
    l.append(h[i])
    for j in range(n):
        h[j] -= tmp
        
ans = 0
for i in l:
    if i > 0:
        ans += i

print(ans)

Submission status: Accepted

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

total = 0
counter = 0
while H.count(0) != len(H) and counter < 1:
  left = 0
  right = 0
  for i in range(len(H)):
    if H[i] != 0:
      left = i
      break
  for i in range(i+1, len(H)):
    if H[i] == 0:
      right = i
      break
  #print(left, right)
  min_val = min(H[left:right])
  total += min_val
  H[left:right] = map(lambda x: x - min_val, H[left:right])
#  print(H)
#  counter = 1
print(total)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    H=list(map(int,input().split()))
    cnt=0
    while(sum(H)):
        flag=False
        for i in range(n):
            if((not flag) and H[i]>0):
                flag=True
            if(flag and H[i]==0):
                break
            if(flag):
                H[i]-=1
        cnt+=1
    print(cnt)
resolve()

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int,input().split()))
flags = [i >=1 for i in H]
flags.append(False)

ans = 0
while any(flags):
    for i in range(N):
        if flags[i] and not flags[i+1]:
            ans += 1
        H[i] = H[i] - 1 if H[i] else 0
    flags = [e >= 1 for e in H]
    flags.append(False)
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
h = list(map(int, input().split()))
h = np.array(h)
ans = 0
def mizuyari(list):
    global ans
    # print(list)
    if len(list) == 0:
        return
    if len(list) == 1:
        ans += min(list)
        return
    min_list = min(list)
    list = list - min_list
    ans += min_list
    zero_list = np.where(list == 0)[0]
    mizuyari(list[:zero_list[0]])
    mizuyari(list[zero_list[-1]+1:])
    for i in range(len(zero_list)-1):
        mizuyari(list[zero_list[i]+1:zero_list[i+1]])
mizuyari(h)
print(ans)

Submission status: Accepted

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

s = [0]*(N+2)
s[1] = h[0]
s[N+1] = -h[N-1]
for i in range(1, N):
    s[i+1] = h[i] - h[i-1]
ans = 0
for x in s:
    if x > 0:
        ans += x
print(ans)

Submission status: Accepted

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

ans = 0

while sum(h) != 0:
    s = 0
    for i in range(N):
        if h[i] == 0 and s == 1:
            ans += 1
            break
        elif h[i] != 0:
            h[i] -= 1
            s = 1
        if i == N - 1:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
from itertools import groupby

n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
li = [0] + li
zantei = 0
point = 0
for i in range(len(li)):
    if li[i-1] <= li[i]:
        point += li[i]-li[i-1]

print(point)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
g = [[1] * (n+1) for i in range(max(h))]
for i in range(n):
  for j in range(h[i]):
    g[j][i] = 0
c = 0
for i in range(max(h)):
  for j in range(n):
    if g[i][j] == 0 and g[i][j+1] == 1:
      c += 1
print(c)

Submission status: Accepted

Solution code:
N=int(input())
h=list(map(int,input().split()))
p=h[0]
for i in range(N-1):
  if p<h[i+1]:
    p=h[i+1]
  else:
    p=p
 
q=1
r=0
if p==0:
    print(0)
else:
    while q<=p:
        if h[0]>=q:
            r=r+1
        else:
            r=r
        for i in range(N-1):
              if h[i]<q and h[i+1]>=q:
                r=r+1
              else:
                r=r
        q=q+1
    print(r)

Submission status: Accepted

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


def minus(x,y): 
    if x > 0:
        return(x - y) 
    else:
        return(x)

def non_zero_min(hs):
    hmin = 1000000 
    for h in hs: 
        if h > 0: 
            if h < hmin: 
                hmin = h 
    return(hmin)


def zero_div_counter(hs):
    length = 0
    n_div = 0
    for i in range(len(hs)):
        h = hs[i]
        if h > 0:
            length += 1
        elif h == 0:
            if length > 0:
                n_div += 1
                length = 0
            else:
                length = 0  
        if i == len(hs)-1:
            if length > 0:
                n_div += 1
    return(n_div)

count = 0

orig_max = max(hs)

for i in range(N):
    # count separation
    n_div = zero_div_counter(hs)
#     print("ndiv: ",n_div)
    nz_min = non_zero_min(hs)
    
    # add count with considering separation
    count += n_div * nz_min
#     print("count:",count)
    
    # update hs
    hs = [minus(x,nz_min) for x in hs] 
#     print("hs  :",hs)

print(count)

Submission status: Accepted

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

flowers = [0 for i in range(n)]
count = 0

def pour(li):
    li.insert(0, 0)
    pour_count = 0
    for i in range(len(li)-1):
        if li[i] == 0 and li[i+1] == 1:
            pour_count += 1
    return pour_count


while flowers != num_list:
    pour_list = [0] * n
    for i in range(n):
        if flowers[i] != num_list[i]:
            pour_list[i] = 1
            flowers[i] += 1
    count += pour(pour_list)

print(count)

Submission status: Accepted

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

maxh=max(A)
ans=0
for i in range(1,maxh+1):
  # i 以上の高さのブロック数をカウントする
  block=0
  cur=False
  if A[0]>=i:
    block+=1
    cur=True
  for j in range(1,len(A)):
    if A[j]>=i:
      if not cur:
        block+=1
        cur=True
    else:
      cur=False
  ans+=block

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = [int(x) for x in input().split()] + [0]
ans = 0
i = 0
while(1):
    if (i == N): break
    if (H[i] == 0): 
        i += 1
        continue
    j = i + 1
    m = H[i]
    while(1):
        if (H[j] == 0): break
        m = min(m, H[j])
        j += 1
    ans += m
    for k in range(i, j):
        H[k] -= m
print(ans)

Submission status: Accepted

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

def rec(h) :
    m = min(h)
    for i in range(len(h)) :
        h[i] -= m
    
    ret = m
    pre = 0
    for i in range(len(h)) :
        if h[i] == 0 :
            if i - pre > 0 :
                ret += rec(h[pre:i])
            pre = i + 1
        elif i == len(h) - 1 :
            ret += rec(h[pre:])
    return ret
    
print(rec(h))

Submission status: Accepted

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

count = 0

while sum(H) != 0:
    temp = 0
    H_max = max(H)
    H_no_max = [h for h in H if h != H_max]
    if len(H_no_max) == 0:
        count += H_max
        break
    H_second_max = max(H_no_max)
    dif = H_max - H_second_max
    
    temp += H.count(H_max)
    for n in range(N-1):
        if H[n] == H[n+1] == H_max:
            temp -= 1
    for n in range(N):
        if H[n] == H_max:
            H[n] = H[n] - dif
    count += dif * temp

print(count)

Submission status: Accepted

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

if n == 1:
	print(h[0])
else:
	cnt = 0
	while 1:
		while h[0] == 0:
			del h[0]
			if len(h) == 1:
				print(cnt+h[0])
				exit(0)
		while h[len(h)-1] == 0:
			del h[len(h)-1]
			if len(h) == 1:
				print(cnt+h[0])
				exit(0)
		num = 1
		i = 0
		while 1:
			while h[i] != 0:
				i += 1
				if i == len(h):
					break
			if i == len(h):
				break
			num += 1
			while h[i] == 0:
				i += 1
		cnt += num
		for i in range(len(h)):
			if h[i] != 0:
				h[i] -= 1

Submission status: Accepted

Solution code:
number_of_flowers = int(input())

intended_height = [int(x) for x in input().split(' ')]

times = 0

stop = False

while True:
    times = times + 1
    watered = False
    # print('ready water')
    for i in range(0, len(intended_height)):
        if intended_height[i] != 0:
            intended_height[i] = intended_height[i] - 1
            # print(intended_height)
            watered = True
        else:
            if watered:
                break
    # print(sum(intended_height))
    if not watered:
        times -= 1
        break

print(times)

Submission status: Accepted

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

def calc(i,j):
    SUM=0
    T=H[i:j]
    TH=H
    if(len(T)==0):
        return 0
    
    tmp=min(T)
    for a in range(i,j):
        H[a]-=tmp
    SUM+=tmp
    
    List=[n for n, v in enumerate(T) if v == min(T)]

    if(len(T)>1):
        B=0
        for b in List:
            SUM+=calc(i+B,i+b)
            B=b+1
        SUM+=calc(i+B,j)

    return SUM


if __name__ == "__main__":

    print(calc(0,N))
    
    exit()

Submission status: Accepted

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
ans = 0
while sum(h) > 0:
    L = 0
    if 0 not in h:
        ans += 1
        for i in range(n):
            h[i] -= 1
    else:
        for i in range(n):
            if h[i] == 0 and L != i:
                for j in range(L,i):
                    h[j] -= 1
                ans += 1
                L = i + 1
            elif h[L] == 0:
                L += 1
            if i == n-1 and h[i] != 0:
                for j in range(L,n):
                    h[j] -= 1
                ans += 1
print(ans)

Submission status: Accepted

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

if min(h) > 0:
    tmp = min(h)
    h = list(map(lambda x: x - tmp, h))
    count += tmp

while True:
    star = -1
    end = -1
    frag = False
    min_num = -1
    if min(h) == max(h):
        break

    for i in range(N):
        if h[i] > 0 and not frag:
            star = i
            frag = True
        if h[i] == 0 and frag:
            end = i
            break
    if end == -1:
        end = len(h)

    min_num = min(h[star:end])
    count += min_num
    for j in range(min_num):
        for i in range(star, end):
            h[i] -= 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
s = L[0]
ans = s
for i in range(0,N):
  if s <= L[i]:
    ans += L[i] - s
  s = L[i]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

flowers = [0] * N

start = 0
end = 0

con = 0

while flowers != H:
	for i in range(0,N):
		if H[i] > flowers[i]:
			start = i
			for j in range(i,N):
				if H[j] == flowers[j]:
					end = j
					break
				else:
					end = N
			break


	for k in range(start,end):
		flowers[k] += 1
	con += 1
	start = 0
	end = 0
	
	


print(con)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
list1 = list(map(int,input().split()))
cout = 0 #最終的な水やり回数
now_water = 0 #現在水やりをしている高さ-1(これは常に最高値を持つ)
i = 0
def func(flower):
	global cout
	global i
	if flower == n-1: #一番右側の花の処理
		if list1[flower] <= now_water:
			cout += 1
			i = flower + 1
			return
		else:
			cout += 1
			i = flower + 1
			return

	elif list1[flower] <= now_water: #終了条件
		cout += 1 #一連の水やり操作の終了
		i = flower + 1
		return
	else:
		func(flower + 1)

while now_water <= max(list1): #花の高さがlist内の最大値になるまで
	i = 0
	while i < n: #now_waterに対して一巡させる
		if list1[i] > now_water:
			func(i)
		else:
			i+=1
	now_water += 1





print(cout)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
b=l[0]
ans=b
for i in l:
  if b<=i:
    ans+=i-b
  b=i
print(ans)

Submission status: Accepted

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

ans = int(0)
ans += l[0]
for i in range(n-1):
    if l[i] < l[i+1]:
        ans += l[i+1]-l[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

m=max(h)
c=0

for i in range(1,m+1):
  s=-1
  for j in range(n):
    if s==-1 and h[j]>=i:
      s=j
    if s!=-1 and h[j]<i:
      s  =-1
      c += 1
  else:
    if s!=-1:
      c += 1

print(c)

Submission status: Accepted

Solution code:
def merge(hight):
    if len(hight) == 0:
        return 0
    if len(hight) == 1:
        return hight[0]
    m = min(hight)
    m_idx = hight.index(m)
    hight = [h - m for h in hight]
    left_hight = hight[:m_idx]
    right_hight = hight[m_idx + 1:]
    return  merge(left_hight) + merge(right_hight) + m

if __name__ == '__main__':
    N = int(input())
    h = list(map(int, input().split()))
    print(merge(h))

Submission status: Accepted

Solution code:
N=int(input())
H=list(map(int,input().split()))
ans=0
M=0
for i in range(N):
  if(M<H[i]):
    M=H[i]
for i in range(M):
  X=0
  for j in range (N):
    H[j]=min(H[j],M-i)
    if(j):
      if(H[j]!=H[j-1] and H[j-1]==M-i):
        X+=1
    if(j==N-1):
      if(H[j]==M-i):
        X+=1
  ans+=X
print(ans)

Submission status: Accepted

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

for i in range (0,N-1):
  if h[i] > h[i+1]:
    x = x + (h[i] - h[i+1])
x = x + (h[N-1] - min(h))
print(x)

Submission status: Accepted

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

count = 0
flag = True
while flag:
    for i in range(N-1):
        if h[i] != 0 and h[i+1] == 0:
            count += 1
        if h[i] > 0:
            h[i] -= 1
    if h[N-1] > 0:
        h[N-1] -= 1
        count += 1
        
    count_0 = 0
    for i in range(N):
        if h[i] == 0:
            count_0 += 1
    if count_0 == N:
        flag = False

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

while h != [0]*n:
  prev = -1
  subcount = 0 #部分列何個あるか
  for index in range(n):
    if prev != -1 and prev != 0 and h[index] == 0:
      subcount += 1
    prev = h[index]
    h[index] = max(h[index]-1, 0)
      
  if prev != 0:
    subcount+=1
  
  answer+=subcount

print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
import copy
H_memo = copy.copy(H)
cnt = 0
for i in range(N):
    cnt += H[i]
    if i < N-1 and H_memo[i] <= H_memo[i+1]:
        H[i+1] -= H_memo[i]
    elif i < N-1 and H_memo[i] > H_memo[i+1]:
        H[i+1] = 0
print(cnt)

Submission status: Accepted

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

ans = 0
p = 0
started = True # 前の項が0であるか

while any(h):

    cur = h[p] # 現在位置の値
    
    if cur > 0:
        h[p] -= 1
        if started:
            started = False
            ans += 1
    else:
        started = True
         
    p += 1
    
    if p >= N:
        p = 0 # 最初から
        started = True

print(ans)

Submission status: Accepted

Solution code:
n = int(input())

h = list(map(int,input().split()))
c=0
while max(h)>0:   
    ##断裂の数を数える
    if h[0] > 0:
      h.insert(0,0)
    if h[-1] > 0:
      h.append(0)
    p=0
    for i in range(len(h)-1):
      if h[i]>=1 and h[i+1]<=0:
        p+=1
    ##加算値は断裂
    c+=max(1,p)
    ##地盤沈下させる
    h = [n-1 for n in h]
    #print("c=",c,"p=",p,"h=",h)
    
print(c)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
h.append(0)
min_n = 100
ans = 0
cnt = 0
i = 0
while i < N + 1:
    if h[i] != 0:
        cnt += 1
        min_n = min(min_n, h[i])
    elif h[i] == 0 and cnt != 0:
        ans += min_n
        for j in range(i+1):
            h[j] = max(h[j]-min_n, 0)
        min_n = 100
        cnt = 0
        i = -1
    i += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
H=list(map(int,input().split()))
ans=0
a=0
for i in H:
  ans+=max(i-a,0)
  a=i
print(ans)

Submission status: Accepted

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

ans = 0
prev = 0
for h_i in h:
    ans += max(h_i - prev, 0)
    prev = h_i
print(ans)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
import numpy as np

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

diff = H[1:] - H[:-1]
diff = [x if x > 0 else 0 for x in diff]
answer = H[0] + sum(diff)
print(answer)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())

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

for i in range(1,101):
    judge=False
    for item in H:
        if judge:
            if item>=i:
                continue
            else:
                judge=False
        else:
            if item>=i:
                judge=True
                count+=1
            else:
                continue
print(count)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
h.insert(0,0)
sum = 0
bottom = 0
for i, hn in enumerate(h):
  if (i == 0) :
    continue
  if (hn < h[i-1]):
    bottom = hn
  else:
    sum += hn - h[i-1]
print(sum)

Submission status: Accepted

Solution code:
n = int(input())
array = [int(x) for x in input().split()]
now = 0
cnt = 0
for a in array:
  if a > now:
    cnt += a-now
  now = a
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
array = [int(x) for x in input().split()]
now = 0
cnt = 0
for a in array:
  if a > now:
    cnt += a-now
  now = a
print(cnt)

Submission status: Accepted

Solution code:
# 一番高い花の両隣に同じ高さの花があれば1減らす
# 自分も1減らす
# これを繰り返す
N = int(input())
h = list(map(int, input().split()))
def bury(x):
    for i in range(1,N):
        if x+i >= N or h[x+i] != h[x]:
            break
        else:
            h[x+i] -= 1
    for i in range(1,N):
        if x-i < 0 or h[x-i] != h[x]:
            break
        else:
            h[x-i] -= 1
    h[x] -= 1
 
ans = 0
while sum(h) > 0:
    bury(h.index(max(h)))
    ans += 1
    
print(ans)

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    h = list(map(int, input().rstrip('\n').split()))
    l = 0
    cnt = 0
    for i in range(n):
        if l < h[i]:
            cnt += h[i] - l
        l = h[i]
    print(cnt)


if __name__ == '__main__':
    slove()

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
def reverse():
    p = h.index(max(h))
    a = 0
    if p == n-1:
        a += 1
        h[p] -= 1
        return a
    elif h[p] == h[p+1] and p < n-1:
        h[p] -= 1
        return a
    else:
        a += 1
        h[p] -= 1
        return a

while sum(h) > 0:
    ans += reverse()
print(ans)