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()))
cnt = 0
for _ in range(10**4):
    if any(H) is False:
        break
    cnt += 1
    f = False
    for i in range(len(H)):
        if H[i] > 0:
            H[i] -= 1
            f = True
        elif f and H[i] == 0:
            break
print(cnt)

Submission status: Accepted

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

ans = heights[0]
sa =[0 for _ in range(N-1)]

for i in range(N-1):
    sa[i] += heights[i+1] - heights[i]

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

print(ans)

Submission status: Accepted

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

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 = input().split()
h = [int(_h) for _h in h]

counter = h[0]

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

Submission status: Accepted

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

ans = 0
for cheight in range(101):
	renz = False
	for x in range(n):
		if h[x] > cheight:
			if not renz:
				renz = True
				ans += 1
		else:
			renz = False

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, h: "List[int]"):
    cost = 0
    CUR = [0 for i in range(len(h))]
    for i in range(len(CUR)):
        while CUR[i] != h[i]:  # 花が成長しきっていないなら以下の処理を繰り返す
            l = i
            r = i
            for j in range(i, len(CUR)):
                if CUR[i] >= h[j]:
                    break
                r += 1
            grow = 10 ** 16
            for j in range(l, r):
                grow = min(grow, h[j] - CUR[j])
            #cost += 1
            for j in range(l, r):
                CUR[j] += grow
            cost += grow
    print(cost)
    return


# Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, h)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

n = int(input())
arr = list(map(int,input().split()))
bef = arr[0]
cst = arr[0]
for i in range(1,n):
    if bef < arr[i]:
        cst += arr[i]-bef
    bef = arr[i]

print(cst)

Submission status: Accepted

Solution code:
def reduce(x):
	return x-1
def sum(lst):
	cnt = 0
	for i in range(0, len(lst)):
		cnt += lst[i]
	return cnt
def calc(lst):
	if lst == []: return 0
	elif 0 in lst:
		lstlst = []
		newlst = []
		for i in range(0, len(lst)):
			if lst[i] == 0:
				lstlst.append(newlst)
				newlst = []
			else:
				newlst.append(lst[i])
		lstlst.append(newlst)
		return sum(map(calc, lstlst))
	else: return 1 + calc(map(reduce, lst))
	
N = int(raw_input())
h = map(int, raw_input().split())

print calc(h)

Submission status: Accepted

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:
            if i == N-1:
                f_h[i] += 1
                ct += 1
                if f_h[i] == h[i]:
                    judge[i] = True
            else:
                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: Accepted

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

cnt = 0
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: Accepted

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

cnt = 0
for i in range(1, 101):
    flg = False
    for j in range(N):
        if H[j] < i and flg:
            cnt += 1
        flg = H[j] >= i
    if flg:
        cnt += 1
print(cnt)

Submission status: Accepted

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

cnt = 0
for i in range(1,101):
    flg = False
    for j in range(N):
        if H[j] >= i:
            flg = True
        elif flg:
            flg = False
            cnt += 1
    if flg:
        cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
input()
S = list(map(int,input().split()))
i = 0
ans = 0
while True:
    if i >= len(S):
        #print('終わり！')
        break
    for j in range(i+1,len(S)+1):
        #print(S[i:j])
        a = max(S[i:j])
        if j >= len(S):
            #print(1,S[i:j],i,j,ans)
            ans += max(S[i:j])
            i += j
            break
        elif a <= S[j]:
            #print(2,i,j,ans)
            continue
        else:
            #print(3,S[i:j],i,j,ans)
            ans += max(S[i:j])
            ans -= S[j]
            i = j
            break
print(ans)

Submission status: Accepted

Solution code:
_,h=open(0);s=p=0
for x in map(int,h.split()):s+=max(x-p,0);p=x
print(s)

Submission status: Accepted

Solution code:
def allzero(h):
    for i in range(N):
        if h[i] != 0:
            return False
    return True

def move(i):
    if i == N:
        return 0

    for k in range(i, N):
        if h[k] != 0:
            return k
    return 0

N = int(input())
h = list(map(int,input().split()))
i = 0
j = 0
cnt = 0
while not allzero(h):
    while i < N and h[i] != 0:
        h[i] -= 1
        i += 1
    if i != 0:
        cnt += 1
    i = move(i)
print(str(cnt))

Submission status: Accepted

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

i = 0
j = 0
ans = 0
while i < N:
    minh = h[j]
    while True:
        if h[j] == 0:
            if j > 0:
                j -= 1
            break
        if minh > h[j]:
            minh = h[j]
        j += 1
        if j >= N:
            j -= 1
            break
    if minh > 0:
        for k in range(i, j+1):
            h[k] -= minh
        ans += minh
    if h[i] == 0:
        i += 1
    j = i

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
 
def solve(List):
  if List.count(0) == len(List):
    return(0)
  if List.count(0) == 0:
    for i in range(len(List)):
      List[i] -= 1
    return(1 + solve(List))
  elif List[0] == 0:
    List.pop(0)
    return(solve(List))
  else:
    p = 0
    q = 1
    while p == 0:
      if List[q] == 0:
        p = q
      else:
        q += 1
    return(solve(List[:q]) + solve(List[q:]))
print(solve(h))

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
ans = 0
while True:
    flag = False
    for i in range(N):
        if not flag and h[i] > 0:
            h[i] -= 1
            flag = True
        elif flag and h[i] > 0:
            h[i] -= 1
        elif flag and h[i] == 0:
            flag = False
            ans += 1
        else:
            pass
        if i == N-1 and flag:
            ans += 1
        
    if sum(h) == 0:
        break
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int, input().split()))
def split(p):
    res=[]
    tmp=[]
    for pi in p:
        if pi==0:
            if len(tmp)>0:
                res.append(tmp)
                tmp=[]
        else:
            tmp.append(pi)
    if len(tmp)>0:
        res.append(tmp)
    return res
        
def D(p):
    global cnt
    m=min(p)
    cnt+=m
    p=[pi-m for pi in p]
    s=split(p)
    for si in s:
        D(si)
cnt=0
D(h)
print(cnt)

Submission status: Accepted

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

counter = 0

def height_counter(array):
	global counter
	if len(array) == 0:
		return
	if len(array) == 1:
		counter += array[0]
		return
	else:
		b = min(array)
		c = array.count(b)
		counter += b
		for i in range(len(array)):
			array[i] -= b
		for i in range(c):
			d = array.index(0)
			height_counter(array[0:d])
			if d == len(array) - 1:
				array.append(0)
			array = array[d + 1:]
		height_counter(array)

height_counter(a)
print(counter)

Submission status: Accepted

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

while any(h):
    for i in range(N):
        if i+1 >= N:
            if h[i] > 0:
                count += 1
                h[i] -= 1
                break
        if h[i] > 0 and h[i+1] == 0:
            count += 1
        if h[i] > 0:
            h[i] -= 1

print(count)

Submission status: Accepted

Solution code:
#参考させて頂いたサイト
#http://eromog.hatenablog.com/entry/2019/03/13/232245

N = int(input())
h = list(map(int, input().split()))
count = h[0]

for i in range(1, N):
    if h[i] > h[i-1]:
        count += h[i] - h[i-1]

print(count)

Submission status: Accepted

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


def rec(array):

    count = 0

    if sum(array) == 0:
        return count

    if 0 in array:
        none = array.index(0)

        count += rec(array[:none])
        count += rec(array[none + 1:])

    else:
        min_num = min(array)

        for i in range(len(array)):
            array[i] -= min_num
        count += min_num

        count += rec(array)

    return count


print(rec(h))

Submission status: Accepted

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

while set(h) != {0}:
    height = []
    ind = []
    for i in range(n):
        if h[i] != 0:
            height.append(h[i])
            ind.append(i)
        elif h[i] == 0 and len(height) != 0:
            break

    cnt += min(height)
    for i in range(len(height)):
        h[ind[i]] -= min(height)

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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:
	if is_finish(h):
		break
	s = find_start(h)
	h = mizu(h, s)
	count += 1

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def kuksan(array):
    for i in range(0, n):
        if array[i] > 0:
            left = i
            for j in range(left,n + 1):
                if array[j] <= 0:
                    right = j - 1
                    return left, right, True
    return 0,0,0

n = int(input())
h_list = list(map(int, input().split()))
cnt = 0
while True:
    h_list.append(0)
    kukan = kuksan(h_list)
    if kukan[2]:
        for i in range(kukan[0], kukan[1] + 1):
            h_list[i] -= 1
        cnt += 1
    else:
        break

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
h.append(0)
st = 0
res = 0
for i in range(1,101):
  for j in h:
    if j >= i:
      st = 1
    else:
      if st == 1:
        res  =  res + 1
        st = 0
print(res)

Submission status: Accepted

Solution code:
def main():
	n = int(input())
	h = list(map(int, input().split()))
	def shower(area, l, r):
		min_h = 10**9
		min_pos = 0
		total = 0
		for i in range(l, r):
			if area[i] < min_h:
				min_h = area[i]
				min_pos = i
		for i in range(l, r):
			area[i] -= min_h
		total += min_h
		if l < min_pos:
			total += shower(area, l, min_pos)
		if min_pos+1 < r:
			total += shower(area, min_pos+1, r)
		return total
	total = shower(h, 0, n)
	print(total)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

c = 0
for i in range(1, 101):
    on = False
    for h in hn:
        if h >= i:
            if not on:
                on = True
                c += 1
        else:
            on = False
print(c)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    s = S()
    c = s.count("0")
    print(2*min(c,len(s)-c))
    return

#B
def B():
    n = I()
    h = LI()
    ans = 0
    s = deque()
    k = []
    for i in range(n):
        if h[i] == 0:
            if k:s.append(k)
            k = []
        else:k.append(h[i])
    if k:s.append(k)
    while s:
        k = s.popleft()
        if k == []:continue
        m = min(k)
        ans += m
        for i in range(len(k)):
            k[i] -= m
        l = []
        for i in range(len(k)):
            if k[i] == 0:
                if l:s.append(l)
                l = []
            else:l.append(k[i])
        if l:s.append(l)
    print(ans)
    return

#C
def C():
    n,k = LI()
    x = LI()
    ans = float("inf")
    j = bisect.bisect_left(x,0)
    if n == 1:
        print(abs(x[0]))
        quit()
    if j < n:
        if x[j] == 0:
            k -= 1
            x.pop(j)
            n -= 1
    for i in range(n):
        if i < j:
            if abs(i-j) > k:continue
            elif abs(i-j) == k:ans = min(ans,abs(x[i]))
            elif k+i-1 < n:
                ans = min(ans,abs(x[i])+x[k+i-1]-x[i])
        else:
            if abs(i-j)+1 > k:continue
            elif abs(i-j)+1 == k:ans = min(ans,x[i])
            elif i-k+1 >= 0:
                ans = min(ans,abs(x[i])+x[i]-x[i-k+1])

    print(ans)
    return

#D
def D():
    n = I()
    a = LI()
    ans = 0
    p = [1<<i for i in range(100)]
    for i in range(n):
        l = 0
        r = 100
        while r-l > 1:
            m = (l+r)//2
            if a[i]%p[m] == 0:
                l = m
            else:
                r = m
        ans += l
    print(ans)
    return

#E
def E():
    s = LI()
    s.sort()
    ans = s[2]-s[1]
    s[0] += s[2]-s[1]
    s[1] = s[2]
    if s[0]%2 != s[1]%2:
        ans += (s[1]+1-s[0])//2+1
    else:
        ans += (s[1]-s[0])//2
    print(ans)
    return

#F
def F():
    n = I()
    s = SR(n)
    for i in range(n):
        d = defaultdict(int)
        for j in range(len(s[i])):
            d[s[i][j]] += 1
            s[i][j] += str(d[s[i][j]])
    ans = set(s[0])
    for i in range(1,n):
        ans = ans&set(s[i])
    ans = list(ans)
    ans.sort()
    for i in ans:
        print(i[0],end = "")
    print()
    return

#G
def G():
    s = S()
    ans = 0
    for i in range(len(s)-1):
        if s[i] != s[i+1]:ans += 1
    print(ans)
    return

#H
def H():
    n,q = LI()
    ans = [0 for i in range(n+1)]
    for i in range(q):
        l,r = LI()
        ans[l-1] ^= 1
        ans[r] ^= 1
    for i in range(n):
        ans[i+1] ^= ans[i]
        print(ans[i],end = "")
    print()
    return

#I
def I_():
    n = I()
    a = LI()
    ans = 0
    r = 0
    for l in range(n):
        while r < n-1 and a[r+1] > a[r]:
            r += 1
        ans += r-l+1
        if l==r:r += 1
    print(ans)
    return

#J
def J():
    n = I()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n-1):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i+1]-h[i]))
        if i < n-2:
            dp[i+2] =  min(dp[i+2],dp[i]+abs(h[i+2]-h[i]))
    print(dp[n-1])
    return

#Solve
if __name__ == "__main__":
    B()

Submission status: Accepted

Solution code:
n = int(input())
h_li = list(map(int, input().split()))
count = 0
for i in range(100):
    f = 0
    for j in range(n):
        if h_li[j] >= i + 1:
            if f == 0:
                count += 1
                f = 1
        else:
            f = 0
print(count)

Submission status: Accepted

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

count = 0
for h in range(1, 101):
    found = False
    for i in range(N):
        if hs[i] >= h:
            if not found:
                count += 1
                found = True
        elif found:
            found = False
print(count)

Submission status: Accepted

Solution code:
N = int(input())
h = [int(i) for i in input().split()]
hs = sorted(h)
hs.reverse()
c = 0
for i in range(len(hs)-1):
  nh = []
  a = 0
  for j in range(N-1):
    if not h[j]==h[j+1]:
      nh.append(h[j])
    if h[j]==hs[i]:
      h[j] = hs[i+1]
  nh.append(h[N-1])
  if h[N-1]==hs[i]:
    h[N-1] = hs[i+1]
  c += (hs[i]-hs[i+1])*nh.count(hs[i])
c += hs[-1]
print(c)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
l.append(0)
height = 1
counter = 0
while(height<=max(l)):
    for i in range(n+1):
        if height <= l[i-1] and l[i] < height:
            counter += 1
    height += 1
print(counter)

Submission status: Accepted

Solution code:
n = int(input())
h_list = list(map(int, input().split()))
ans = 0
for i in range(max(h_list)):
    count = 0
    b_list = [False]
    for j in range(n):
        if h_list[j] >= i+1 and b_list[-1] == False:
            b_list.append(True)
        elif h_list[j] < i+1 and b_list[-1] == True:
            b_list.append(False)


    ans += sum(b_list)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(n: int, h: "List[int]"):
    ret = 0
    while True:
        tmp = 0
        found = False
        for i in range(n):
            if h[i] > 0:
                h[i] -= 1
                if not found:
                    found = True
                    tmp += 1
            else:
                found = False
        if tmp < 1:
            break
        else:
            ret += tmp
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    h = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, h)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
li_h = list(map(int,input().split()))
li_a = [0]*n 
ans = 0
li_count = [True]*100
while True:
    for i in range(n):
        if li_h[i] == 0:
            li_count[i] = False
    temp = li_count[0]
    if temp == True:
        count = 1
    else:
        count = 0
    for i in range(n):
        if temp != li_count[i]:
            temp = li_count[i]
            if temp == True:
                count += 1
    ans += count

    for i in range(n):
        if li_h[i] != 0:
            li_h[i] -= 1
    if li_h == li_a:
        break

print(ans)

Submission status: Accepted

Solution code:
def f(n):
    if n%2==0: return n//2
    else: return 3*n+1

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

M=max(H)
tmp=0
ans=0
for h in range(M,0,-1):
    tmp=0
    for i in range(N):
        if H[i]==h:
            if tmp==0: ans+=1
            tmp=1
            H[i]-=1
        elif H[i]!=h:
            tmp=0
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
ans = 0
# 下から順に処理していく
for dan in range(max(h)):
    # 同じ"段"での処理
    for i in range(N-1):
        # 途中に0がある場合は手前まで水やり
        if h[i] != 0 and h[i+1] == 0:
            ans += 1
        if h[i] != 0:
            h[i] -= 1
    # 右端まで水やり
    if h[N-1] != 0:
        h[N-1] -= 1
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
def get_number(h):
    if len(h) == 0:
        return 0

    ans = 0
    a = min(h)
    h2 = list(map(lambda x: x - a, h))

    ans = ans + a
    l = []
    for i in h2:
        if i == 0:
            ans = ans + get_number(l)
            l = []
        else:
            l.append(i)
    if len(l) > 0:
        ans = ans + get_number(l)

    return ans

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

print(get_number(h))

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.append(0)
s = 0
ans = 0
if L == [0]*(N+1):
  print(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: Accepted

Solution code:
mizu = 0
def mizuyari(H):
    global mizu
    if len(H) == 0:
        return "finish"
    zeroindex = [-1]
    grow = min(H)
    mizu += grow
    for i in range(len(H)):
        H[i] -= grow
        if H[i] == 0:
            zeroindex.append(i)
    zeroindex.append(len(H))
    for i in range(len(zeroindex) - 1):
        mizuyari(H[zeroindex[i] + 1: zeroindex[i + 1]])

def main():
    global mizu
    N = int(input())
    *H, = map(int, input().split())
    mizuyari(H)
    print(mizu)

main()

Submission status: Accepted

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


def f(left, right, base):
    m = min(h[left:right+1])
    result = 0
    le = -1
    ri = -1
    for i in range(left, right+1):
        if h[i] > m:
            if le == -1:
                le = i
            ri = i
        else:
            if le != -1:
                result += f(le, ri, m)
                le, ri = -1, -1

    if le != -1:
        result += f(le, ri, m)
    return result + (m - base)


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

Submission status: Accepted

Solution code:
def argmin(x):
    xx = 1000
    ans = 0
    for i, xi in enumerate(x):
        if xi < xx:
            xx = xi
            ans = i
    return ans

def count_water(x, threshold):
    if len(x) == 1:
        return x[0] - threshold
    i = argmin(x)
    y = x[i] - threshold
    if i > 0:
        y += count_water(x[:i], x[i])
    if i < len(x) - 1:
        y += count_water(x[i + 1:], x[i])
    return y

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

Submission status: Accepted

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) - 1):
    ans += max(P[j]) - min(P[j+1])
    
ans += max(P[len(P)-1])

print(ans)

Submission status: Accepted

Solution code:
N = int(input())  # 数値入力
H = list(map(int, input().split()))  # リスト入力（数値）  map関数はリストの各要素に対して関数を適用してくれる

ans = 0
while sum(H) != 0:
    i = 0
    while(i < N):
        if(H[i] == 0):
            i += 1
        else:
            ans += 1
            while((i < N) and (H[i] > 0)):
                H[i] -= 1
                i += 1

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = [int(i) for i in input().split()]
ans = 0
i = 0
while True:
    if max(h)==0:
        break
    i = 0
    while i < N:
        if h[i]==0:
            i += 1
        else:
            ans += 1
            j = i
            while j<N and h[j]>0:
                h[j] -= 1
                j += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())  # 花の数

h = list(map(int, input().split()))  # それぞれの花の高さのリスト

ans = 0

i = 0

while (True):
    if (max(h) == 0):
        break
    i = 0
    while(i < N):
        if(h[i] == 0):
            i += 1  # 0なら何もせずに次に進む
        else:
            ans += 1
            while((i < N) and (h[i] > 0)):
                h[i] -= 1
                i += 1

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
#隣接する花との差を考える
count=h[0]
for i in range(1,n):
    if h[i-1]<h[i]:
        count+=h[i]-h[i-1]
print(count)

Submission status: Accepted

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(n-i):
    if 0 in l[i:n-j]:
      pass
    else:
      c+=min(l[i:n-j])
      l[i:n-j]=l[i:n-j]-np.array([min(l[i:n-j])]*(n-j-i))
print(c)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
def count(L):
    if L == []:
        return 0
    if 0 in L:
        i = L.index(0)
        return count(L[:i]) + count(L[i+1:])
    else:
        m = min(L)
        L = list(map(lambda x:x-m,L))
        return m + count(L) 
print(count(L))

Submission status: Accepted

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

def count(L):
    if len(L) == 1:
        return L[0]
    if L == []:
        return 0
    if 0 in L:
        i = L.index(0)
        return count(L[:i]) + count(L[i+1:])
    else:
        m = min(L)
        L = list(map(lambda x:x-m,L))
        return m + count(L) 
print(count(L))

Submission status: Accepted

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

ans = 0
pre = 0
for i in h:
    if pre < i:
        ans += i-pre
    pre = i

print(ans)

Submission status: Accepted

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: Accepted

Solution code:
hn=int(input())
ht=list(map(int,input().split()))
cnt=0
while 1:
    #範囲決め
    for i in range(hn):
        if ht[i]>0:
            st=i
            break
    else:
        break

    for i in range(st+1,hn):
        if ht[i]<=0:
            ed=i
            break
    else:
        ed=hn

    #水やり回数＝最小の花
    mn=min(ht[st:ed])

    #水をやる
    for i in range(st,ed):
        ht[i]-=mn
    cnt+=mn

print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
count=0
while h.count(0)!=len(h):
  stack=[]
  substack=[]
  for i in range(len(h)):
    if h[i]!=0:
      substack.append(h[i])
    else:
      if len(substack)>0:
        stack.append(substack)
      stack.append([0])
      substack=[]
  if len(substack)>0:
    stack.append(substack)
  h=[]
  for i in range(len(stack)):
    if max(stack[i])==0:
      h+=stack[i]
      continue
    x=min(stack[i])
    count+=x
    for j in range(len(stack[i])):
      stack[i][j]-=x
    h+=stack[i]
print(count)

Submission status: Accepted

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

mp = 0

while (sum(h) != sum(d)):
    l = 0
    x = 0
    mo = 0
    for i in range(0,N):
        x = i
        if ((h[i] != d[i]) and mo == 0):
            l = i
            mo = 1
        if (((h[i] == d[i]) and mo == 1)):
            mo = 2
        if ((i==N-1) and (mo ==1)):
            x += 1
            mo = 2
        if (mo == 2):
            for j in range(l, x):
                d[j] += 1
            mp += 1
            break
print(mp)