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:
def main():
    N = int(input())
    hList = list(map(int, input().split()))
    cnt = 0
    hMax = max(hList)
    isRange = False
    for h in range(1, hMax+1):
        for j in range(N):
            if not isRange and hList[j] >= h:
                isRange = True
            if isRange and hList[j] < h:
                isRange = False
                cnt += 1
        if isRange:
            cnt += 1
        isRange = False
    print(cnt)
            

if __name__ == '__main__':
    main()

Submission status: Accepted

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

def grouping(h_list):
    groups = []
    group = []
    for h in h_list:
        if h == 0:
            if group != []:
                groups.append(group)
            group = []
            continue
        group.append(h)
    if group:
        groups.append(group)
    return groups


sum_mizuyari = []
def groups_mizuyari(h_list):
    num_mizuyari = min(h_list)
    h_list = [h - num_mizuyari  for h in h_list]
    sum_mizuyari.append(num_mizuyari)
    groups = grouping(h_list)
    for group in groups:
        groups_mizuyari(group)

groups_mizuyari(h_list)
print(sum(sum_mizuyari))

Submission status: Accepted

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


def mizuyari(h_list, l, r):
    num_mizuyari = min(h_list[l:r+1])
    h_list = [h - num_mizuyari if l <= x and x <= r else h for x, h in enumerate(h_list)]
    zero_posi = h_list[l:].index(0) + l
    if l == zero_posi:
        first_non_zero_posi = [x for x, h in enumerate(h_list) if h != 0]
        if not first_non_zero_posi:
            print(sum_mizuyari + num_mizuyari)
            exit()
        l = first_non_zero_posi[0]
        if list(range(l, len(h_list))) == first_non_zero_posi:
            return h_list, num_mizuyari, l, len(h_list)
        zero_posi = h_list[l:].index(0) + l
    new_r = zero_posi - 1
    return h_list, num_mizuyari, l, new_r
l = 0
r = len(h_list) - 1
sum_mizuyari = 0
while True:
    h_list, num_mizuyari, l, r = mizuyari(h_list, l, r)
    sum_mizuyari += num_mizuyari

Submission status: Accepted

Solution code:
# coding:utf-8

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

#1 2 2 1
# リストを与えると、0より大きい最大の区間までを-1する関数

ans = 0
index = 0

while True:
    #if index == len(h) - 1:
    #    break
    if max(h) == 0:
        break

    
    if h[index] == 0:# indexが0の時
        index += 1
        continue
    
    # 1 (2 2 1)
    ushiro = h[index:]
    for i in range(len(ushiro)):
        if h[index+i] != 0:
            h[index+i] -= 1# 1 (2 2 1) を引いてく
        else:
            break
    ans += 1

print(ans)

Submission status: Accepted

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

cnt = 0
mem = 0
for i in h:
    if i >= mem:
        cnt += i - mem
        mem = i
    else:
        mem = i
print(cnt)

Submission status: Accepted

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

ans = 0
for i in range(max(h)):
  cnt = 0
  for j in range(N):
    h[j] = h[j] - 1
    if h[j] >= 0 and (j == 0 or h[j - 1] < 0):
      cnt += 1
  ans += cnt
print(ans)

Submission status: Accepted

Solution code:
def main(n):
	h = list(map(int, input().split())) + [0]
	ans = 0
	f, e = 0, 1
	while f != n:
		if h[f] == 0:
			f += 1
			if e == f:
				e += 1
		elif e == n or h[e] == 0:
			temp = min(h[f:e])
			for i in range(f, e):
				h[i] -= temp
			ans += temp
			e = f+1
		else:
			e += 1
	print(ans)
	return

main(int(input()))

Submission status: Accepted

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

Submission status: Accepted

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

for x in h:
    result += max(x-pre, 0)
    pre = x
print(result)

Submission status: Accepted

Solution code:
ans = 0

def mizuyari( l ):
    if len( l ) == 0: return # return のみで関数から抜ける
    
    minimum = min( l )
    min_i = l.index( minimum )
    l = list( map(lambda x: x - minimum, l) )
    global ans  # global宣言すると、関数外で宣言した値に代入できる
    ans += minimum
    
    mizuyari( l[:min_i] )
    mizuyari( l[min_i+1:] )


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

mizuyari( h )
print( ans )

Submission status: Accepted

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

def f(x):
    if len(x) == 1:
        return x[0]
    else:
        xmin = min(y for y in x)
        minindex = x.index(xmin)
        if minindex == 0 :
            return f(x[1:])
        elif minindex == len(x)-1:
            return f(x[:len(x)-1])
        else:
            return (f(x[:minindex]) + f(x[minindex+1:]) -xmin)

print(f(H))

Submission status: Accepted

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

incFlg = True   #登る場合はTure,　降る場合はFalse
before_height = 0
sum = 0

for i in range(len(a)-1):
    #降る場合
    if a[i] > a[i+1]:
        #その前が登り
        if incFlg:
            sum += a[i] - before_height
            incFlg = False
        #その前が下り
        # else:
        #     # before_height = a[i]
    #登る場合
    else:
        if not incFlg:
            before_height = a[i]
        incFlg = True

if incFlg:
    sum += a[len(a)-1] - before_height

print(sum)

Submission status: Accepted

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

incFlg = True   #登る場合はTure,　降る場合はFalse
before_height = 0
sum = 0

for i in range(len(a)-1):
    #降る場合
    if a[i] > a[i+1]:
        #その前が登り
        if incFlg:
            sum += a[i] - before_height
            incFlg = False
        #その前が下り
        else:
            before_height = a[i]
    #登る場合
    else:
        if not incFlg:
            before_height = a[i]
        incFlg = True

if incFlg:
    sum += a[len(a)-1] - before_height

print(sum)

Submission status: Accepted

Solution code:
def water(n,h):
    start = 0
    tempmin = 1000
    counter = 0
    for i in range(n):
        if h[i] == 0 or i == n-1:
            if i == n-1 and h[i] != 0:
                if h[i] < tempmin:
                    tempmin = h[i]
                i += 1
            check = False #水をまいたかどうか
            for k in range(start,i):
                h[k] -= tempmin
                check = True
            if check:
                counter += tempmin
            start = i + 1
            tempmin = 1000
        else:
            if h[i] < tempmin:
                tempmin = h[i]
    return h,counter

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

counter = 0
while max(h) != 0:
    h,c = water(n,h)
    counter += c
print(counter)

Submission status: Accepted

Solution code:
#N本の花があり、それぞれ1,2....,Nと番号が振られている
#最初全ての花の高さは0
#h=[h_1,h_2,...,h_n]が与えられる
#整数l,rを指定して、l<=x<=rを満たす花xの高さを1増やす、という行為をする
#花xの高さがh_xになるための最小水やり回数を求めよ
n = int(input())
flower_height = list(map(int, input().split()))
count=0
def mizuyari(a):
    return a-1
#(0ではない)一番左端の高さに注目する
#その高さから0の高さにぶち当たるまで右に進める
#進んだところに水やりする
#これを繰り返す
for i in range(n):
    j=i
    while flower_height[i]!=0:
        while i < n-1:
            if flower_height[i+1] != 0: i+=1
            else: break
        flower_height = list(map(mizuyari,flower_height[:i+1])) + flower_height[i+1:]
        i=j
        count += 1
    
print(count)

Submission status: Accepted

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


def mizuyari(*f):
    if len(f) == 0:
        return
    min_i = f.index(min(f))
    if f[min_i] != 0:
        global ans
        ans += f[min_i]
        f = [i - f[min_i] for i in f]
    if f[min_i] == 0:
        mizuyari(*f[:min_i])
        mizuyari(*f[min_i + 1:])


l = []
for n in range(N):
    l.append(0)
mizuyari(*H)
print(ans)

Submission status: Accepted

Solution code:
def calc_watering_num(h, offset, left, right):
        if left > right:
                return 0
        min_h = None
        min_h_indices = []
        for i in range(left, right + 1):
                if min_h is None or min_h > h[i] - offset:
                        min_h = h[i] - offset
                        min_h_indices = [i]
                elif min_h == h[i] - offset:
                        min_h_indices.append(i)
        if len(min_h_indices) == right - left + 1:
                return min_h
        num = min_h
        prev = left
        for i in min_h_indices:
                num += calc_watering_num(h, offset + min_h, prev, i - 1)
                prev = i + 1
        num += calc_watering_num(h, offset + min_h, prev, right)
        return num


input()
h = [int(v) for v in input().split()]
print(calc_watering_num(h, 0, 0, len(h) - 1))

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc116/tasks/abc116_c

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

count = 0

while max(h) > 0:
    is_pouring = False
    for i in range(N):
        if h[i] > 0:
            h[i] -= 1
            if not is_pouring:
                count += 1
                is_pouring = True
        else:  # 0
            is_pouring = False
    # print(h)

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(raw_input())
h = raw_input()
h = [int(i) for i in h.split(" ")]
flowers = [0 for i in h]

# check if it is finished
def is_finished(flowers, h):
	for i in range(len(flowers)):
		if flowers[i]!=h[i]:
			return False
	return True

c = 0
while is_finished(flowers, h)==False:
	i = 0
	while i<len(flowers):
		if flowers[i]<h[i]:
			break
		i += 1
	l = i
	while i<len(flowers):
		if flowers[i]>=h[i]:
			break
		i += 1
	r = i-1

	for i in range(l,r+1):
		flowers[i] += 1
	c += 1
print(c)

Submission status: Accepted

Solution code:
n = map(int,input().split())
data = list(map(int,input().split()))
res = 0
data.append(0)
while(max(data)!=0):
    pre_zero = -1
    for idx in range(len(data)):
        if(data[idx] == 0):
            if(idx == pre_zero+1):
                pre_zero = idx
                continue
            min_value = min(data[pre_zero+1:idx])
            for x in range(pre_zero+1,idx):
                data[x] -= min_value
            res += min_value
            pre_zero = idx
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
now = 0
count= 0
for i in range(n):
  if now>=h[i]:
    now = h[i]
  else:
    count+=h[i]-now
    now = h[i]
print(count)

Submission status: Accepted

Solution code:
def solve():
	N = int(input())
	h = [int(i) for i in input().split()]
	ans = 0
	for i in range(0, N):
		ans += max(h[i] - (0 if i - 1 < 0 else h[i - 1]), 0)
	return ans

print(solve())

Submission status: Accepted

Solution code:

import numpy as np

class MyClass:
    def __init__(self):
        self.sum = 0

    def water(self, H):
        if len(H)<=0:
            return
        min_h = np.min(H)
        self.sum += min_h
        H -= min_h
        on = False
        for i in range(len(H)):
            if not on:
                if H[i]>0:
                    st = i
                    on = True
                else:
                    continue
            else:
                if H[i]>0:
                    continue
                else:
                    ed = i
                    on = False
                    self.water(H[st:ed])
        if on:
            self.water(H[st:])
        return


def main():
    N = int(input())
    H = np.array([int(x) for x in input().split()]) # Length of N
    my_class = MyClass()
    my_class.water(H)
    print(my_class.sum)

if __name__=='__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

peak = []
trough = []
h = [0] + h + [0]
before = 0
for a, b in zip(h[:-1], h[1:]):
    after = b - a
    if after == 0:
        continue
    elif before > 0 > after:
        peak.append(a)
    elif before < 0 < after:
        trough.append(a)
    before = after

print(sum(peak) - sum(trough))

Submission status: Accepted

Solution code:
N = int(input())
hin = input().split(" ")

hls = []
count = 0
for i in range(0,N):
    hls.append(int(hin[i]))

def water(l,r):
    if l == r:
        return -1
    for i in range(l,r):
        hls[i] = hls[i] - 1
    global count
    count = count + 1

def all0():
    for i in range(0,N):
        if hls[i] != 0:
            return False
    return True
        
while True:
    nowNode = 0
    endNode = 0

    while endNode < N:
        while hls[endNode] != 0:
            endNode = endNode + 1
            if endNode == N:
                break
        water(nowNode,endNode)
        endNode = endNode + 1
        nowNode = endNode
        if all0():
            break

    if all0():
        break

print(count)

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())
A=list(map(int,input().split()))+[0]
ans=0

while 1:
  for i in range(n):
    if A[i]>0:
      A[i]-=1
      if A[i+1]==0:
        ans+=1
  if not A==[0 for i in range(n+1)]:
    continue
  break
print(ans)

Submission status: Accepted

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

count=0
while 1:
  flag=0
  for i in range(N):
    if h[i]>0:
      h[i] -= 1
      flag = 1
      if h[i+1] == 0:
        count += 1
      else:
        pass
  if flag == 0:
    break
print(count)

Submission status: Accepted

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

maximum = max(heights)

splits = 0
for h in range(maximum, 0, -1):
    before = None
    sp = 0
    for i, _ in enumerate(heights):
        if heights[i] == h:
            if before != i - 1:
                sp += 1
            before = i
            heights[i] -= 1
    # print('height', h, ':', sp)
    splits += sp

print(splits)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
i = 0
start = 0
end = 0
ans = 0

if h.count(0) == n:
    print(0)
else:
    while True:
        i = 0
        for y in range(n):
            if h[y] != 0:
                start = y
                break
        for x in range(start,n):
            if h[x] != 0:
                h[x] -= 1
                if x == n-1:
                    ans += 1
            else:
                start = 0
                i = 0
                ans += 1
                break
        if h.count(0) == n:
            break
    print(ans)

Submission status: Accepted

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


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

    s = [0]
    ans = 0
    for i in range(N):
        if len(s) and s[-1] <= H[i]:
            ans += H[i] - s[-1]
        else:
            while len(s) and s[-1] >= H[i]:
                s.pop()
        s.append(H[i])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0

for i in range(101):
    for j in range(n):
        if h[j] <= i and h[j + 1] > i:
            ans += 1

print(ans)

Submission status: Accepted

Solution code:
def main(condition, number_of_trials):
    while True:
        min_value = min(condition)
        condition = list(map(lambda x: x - min_value, condition))
        condition = remove_neighboring_zeros(condition)
        number_of_trials += min_value
        if condition == []:
            break
        if 0 not in condition:
            continue
        divided_list = divide_list_by_specified_value(condition, 0)
        for each_part_condition in divided_list:
            number_of_trials = main(each_part_condition, number_of_trials)
        break
    return number_of_trials


def divide_list_by_specified_value(list_to_be_divided, divider):
    before_idx = 0
    divided_list = []
    for idx, each_elem in enumerate(list_to_be_divided):
        if each_elem == divider:
            if list_to_be_divided[before_idx:idx] != []:
                divided_list.append(list_to_be_divided[before_idx:idx])
            before_idx = idx + 1
    if list_to_be_divided[before_idx:] != []:
        divided_list.append(list_to_be_divided[before_idx:])
    return divided_list


def remove_neighboring_zeros(array):
    while len(array) > 0 and array[-1] == 0:
        array.pop(len(array) - 1)
    while len(array) > 0 and array[0] == 0:
        array.pop(0)
    return [each_elem for idx, each_elem in enumerate(array) if
            idx == 0 or each_elem != array[idx - 1] or each_elem != 0]


if __name__ == '__main__':
    n = int(input())
    condition = list(map(int, input().split()))
    print(main(condition, 0))

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split(" ")))
h.append(0)
d=[]
for i in range(n):
    d.append(h[i+1]-h[i])
c=0
for j in range(len(d)):
    k=d[j]
    if k < 0:
        c-=k
print(c)

Submission status: Accepted

Solution code:
from sys import stderr, setrecursionlimit
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, **kwargs):
    print(*args, file=stderr, **kwargs)

n = getInt()
h = getInts()
ans = 0
l = 0
r = 0
while l < n:
    r = l
    while l < n and h[l] == 0:
        l += 1
        r += 1
    while r < n and h[r] != 0:
        r += 1
    if r == l:
        break
    tmp = min(h[l:r])
    ans += tmp
    h = h[:l] + list(map(lambda x:x-tmp, h[l:r])) + h[r:]
    debug(l, r, h)

print(ans)

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())
heights = list(map(int, input().split()))

def nextgen(comb):
    lis = comb[0]
    j = comb[1]
    if len(lis) == 1 and lis[0] != 0:
        j += 1  
    for i in range(len(lis)-1):
        if (i != len(lis) and (lis[i] == 0 and lis[i+1] != 0)) or (i == 0 and lis[i] != 0):
            j += 1
        if lis[i] != 0:
            lis[i] -= 1
    if lis[len(lis)-1] != 0:
        lis[len(lis)-1] -= 1
    return [lis, j]

comb = [heights, 0]
while (comb[0].count(0) != len(comb[0])):
    comb = nextgen(comb)

print(comb[1])

Submission status: Accepted

Solution code:
import numpy as np

n = int(input())
flower = [int(part) for part in input().split()]
flower = np.array(flower)

count = 0
highest = max(flower)
for h in range(1, highest+1):
    signal = (flower >= h)
    #print(signal)
    
    pre = False
    for now in signal:
        #print(pre, now)
        if not pre and now:
            count += 1
        pre = now
print(count)

Submission status: Accepted

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

def f(li):
  global ans
  
  #終了条件
  if li==[]:
    del li
    return
  
  #再帰
  min_num=min(li)
  if min(li)>0:
    ans+=min(li)
    li=list(map(lambda x: x-min(li), li))
  li=[li[:li.index(0)],li[li.index(0)+1:]]
  f(li[0])
  f(li[1])

f(h)
print(ans)

Submission status: Accepted

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

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

count = 0

def f(l):
    global count

    # 終了条件
    if l == []:
        del l
        return

    # 再帰
    min_num = min(l)
    if min(l) > 0:
        count += min(l)
        l = list(map(lambda x: x-min(l), l))
    min_list = [i for i, x in enumerate(h) if x == min(h)]
    l = [l[:l.index(0)], l[l.index(0)+1:]]
    f(l[0])
    f(l[1])

f(h)
print(count)

Submission status: Accepted

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

ans=0
for i in s:
  if i>0:
    ans+=i
print(ans)

Submission status: Accepted

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

def func(l,k):
    ans = min(l)-k
    l_i = [i for i, x in enumerate(l) if x == min(l)]
    l_split = []
    for i in range(len(l_i)+1):
        if i==0:
            l_ = l[:l_i[i]]
            if(l_):
                l_split.append(l_)
        elif i==len(l_i):
            l_ = l[l_i[i-1]+1:]
            if(l_):
                l_split.append(l_)
        else:
            l_ = l[l_i[i-1]+1:l_i[i]]
            if(l_):
                l_split.append(l_)
    k = min(l)
    for l_ in l_split:
        ans += func(l_,k)
    return ans

ans = func(l,0)
print(ans)

Submission status: Accepted

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

count = 0
for i in range(maxh):
    flag = False
    for j in range(N):
        if h[j] > 0:
            flag = True
        if h[j] == 0 and flag:
            count += 1
            flag = False
        elif j == N-1 and flag:
            count += 1
    h = [x-1 if x >= 1 else 0 for x in h]
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
H = input()
H = H.split()
for i, val in enumerate(H):
    H[i] = int(val)
maxNum = max(H)
counter = 0
for i in range(maxNum):
    for j, val in enumerate(H):
        if H[j] != 0:
            H[j] -= 1
            if j < (N-1):
                if H[j + 1] == 0:
                    counter += 1
            if j == (N - 1):
                counter += 1
    # print(H)
    # print(counter)
print(counter)

Submission status: Accepted

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

ans = 0
min1 = 0

for i in h:
    if i > min1:
        ans += i - min1
    min1 = i
    
print(ans)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    h_lst = list(map(int, input().split()))
    while h_lst[0] == 0:
        del h_lst[0]
        if h_lst == []:
            break
    ans = 0
    while len(h_lst) > 0:
        r = len(h_lst) - 1
        for i in range(len(h_lst) - 1):
            if h_lst[i + 1] == 0:
                r = i
                break

        block_min = min(h_lst[:r + 1])
        for i in range(r + 1):
            h_lst[i] -= block_min
        ans += block_min

        while h_lst[0] == 0:
            del h_lst[0]
            if h_lst == []:
                break

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split(' ')))
ans = 0
min1 = 0
for hk in h:
    if hk > min1:
        ans += hk - min1
    min1 = hk
print(ans)

Submission status: Accepted

Solution code:
def solve(height):
    
    water_count = 0 #水やり回数

    while True:
        index = [i for i, v in enumerate(height) if v >0] #水をやる必要のある花のリスト インデックスが連続した範囲でしか水やりできない
    
        # 花の水やり範囲を決定
        min_index = index[0] #インデックスの最小値
        max_index = index[0] #インデックスの最大値
    
        while True:
            if (max_index+1) in index:
                max_index += 1
            else:
                break
    
        for flower in range(min_index, max_index+1):
            height[flower] -= 1 
        water_count += 1 #水やりカウンタ増やす
        
        if sum(height) == 0: #水やりが終わったら終了
            break
    print(water_count)
    
def solve_zero(height): #元々のリストがすべて０のとき
    print(0)

if __name__ == "__main__":
    N = int(input()) 
    height = list(map(int, input().split())) #入力：花の高さ
    if sum(height) == 0:
        solve_zero(height)
    else:
        solve(height)

Submission status: Accepted

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

prev = 0
ret = 0
for k in ks:
    if prev < k:
        ret += k - prev
    prev = k

print(ret)

Submission status: Accepted

Solution code:
def solve(lis):
    # 全要素が0の場合
    if is_all_zero(lis):
        return 0

    # 0がある場合、0を境に分割
    zero_index = search_zero_index(lis)
    if zero_index != -1:
        return solve(lis[:zero_index]) + solve(lis[zero_index+1:])

    # 0が無い場合、最小値の数をリスト全体から引く
    min_num = min(lis)
    lis = list(map(lambda x: x - min_num, lis))
    return min_num + solve(lis)

def is_all_zero(lis):
    return len(list(filter(lambda x : x == 0, lis))) == len(lis)

def search_zero_index(lis):
    if 0 in lis:
        return lis.index(0)
    else:
        return -1

if __name__ == "__main__":
    N = int(input())
    h_list = list(map(int, input().split()))
    print(solve(h_list))

Submission status: Accepted

Solution code:

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

counter = min(h)
h = list(map(lambda x, y=min(h): x-y, h))
# print(min(h[1:3]))
while sum(h) > 0:
    start = False
    # print(h)
    for i in range(len(h)):
        if not start and h[i] > 0:
            start = True
            start_index = i
            continue
        elif start and h[i] == 0:
            start = False
            water = min(h[start_index:i])
            counter += water
            h = h[:start_index] + \
                list(map(lambda x, y=water: x - y, h[start_index:i])) + h[i:]
    if start:
        water = min(h[start_index:])
        counter += water
        h = h[:start_index] + \
            list(map(lambda x, y=water: x - y, h[start_index:]))
print(counter)

Submission status: Accepted

Solution code:
N=int(input())
h=list(map(int,input().split()))
h.insert(0,0)
count=0
def find(h,x):
    count=0
    for i in range(0,N):
        if h[i]<x and h[i+1]>=x:
            count=count+1
    return count
for j in range(1,max(h)+1):
    count=count+find(h,j)
print(count)

Submission status: Accepted

Solution code:
input()
H = list(map(int, input().split()))
count = 0
while any([h != 0 for h in H]):
    n = 0
    prev = 0
    for h in H:
        if prev == 0 and h != 0:
            n += 1
        prev = h
    count += n
    H = list(map(lambda x: x if x == 0 else x - 1, H))
print(count)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
# h = input().replace(" ", "")
# print(h)
count = 0
while 1:
    if sum(h) == 0:
        break
    else:
        # print(h)
        # h = h.strip("0")
        if 0 in h:
            while 1:
                num = h.pop(0)
                if num != 0:
                    h.insert(0, num)
                    if 0 in h:
                        i = h.index(0)
                        h = list(map(lambda x: x - 1, h[:i]))+h[i:]
                    else:
                        h = list(map(lambda x: x-1, h))
                    break
            # i = h.index(!0)
            # h = str(int(h)-int(str("1")*i+"0"*(len(h)-i)))
            count += 1
        else:
            h = list(map(lambda x: x-1, h))
            # h = str(int(h) - int(str("1" * len(h))))
            count += 1

print(count)

Submission status: Accepted

Solution code:
def calc_range_num(h,height,max_height,ans):#各階層にいくつ区間があるか
    for i in range(len(h)):
        if h[i]>height:
            if i+1<len(h) and h[i]>h[i+1] and h[i+1] <=height:
            #基本的には、高さが下がったところでインクリメント
                #print(i,h[i],ans+1,height)
                ans+=1
            elif i==len(h)-1:
                #print(i,h[i],ans+1,height)
                ans+=1
    height+=1
    if height>=max_height:
        return ans
    else:
        ans = calc_range_num(h,height,max_height,ans)
    return ans
N = int(input())
h = list(map(int, input().split()))
max_height=max(h)
ans=0
print(calc_range_num(h,0,max_height,ans))

Submission status: Accepted

Solution code:
def solve(h):
    if len(h)<2:
        return h[0]
    s=[i for i in range(len(h))if h[i]<1]
    s=[-1]+s+[len(h)]
    a=0
    for l in[h[i+1:j]for i,j in zip(s,s[1:])]:
        if not l:continue
        v=min(l)
        l=[t-v for t in l]
        a+=v+solve(l)
    return a
input()
print(solve(list(map(int,input().split()))))

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


# aとbの最大公約数
def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


# aとbの最小公倍数
def lcm(a, b):
    g = gcd(a, b)
    return a / g * b


def main():
    N = int(input())
    H = list(map(int, input().split())) + [0]

    ans = 0
    change = True
    while change:
        change = False

        left = 0
        found = False
        for i in range(len(H)):
            if H[i] != 0:
                if not found:
                    found = True
                    left = i

            if H[i] == 0:
                if found:
                    ans += 1
                    change = True
                    for j in range(left, i):
                        H[j] -= 1

                    left = i + 1
                    found = False

    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(max(h)):
    flag = False
    for num in h:
        if num > i and flag == False:
            flag = True
            ans += 1
        elif num > i and flag == True:
            pass
        else:
            flag = False
print(ans)