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 solve(N, Hs):
    def split(s, e):
        d = []
        for i in range(s, e):
            if Hs[i] != 0 and len(d) % 2 == 0:
                d.append(i)
            elif Hs[i] == 0 and len(d) % 2 == 1:
                d.append(i)
        if len(d) % 2 == 1:
            d.append(e)

        return [(d[2 * i], d[2 * i + 1]) for i in range(len(d) // 2)]

    def f(s, e):

        ans = 0
        if s >= len(Hs) or s > e:
            return 0
        if s == e:
            return Hs[s]
        m = min(Hs[s:e])
        c = Hs[s:e].count(0)
        if len(Hs[s:e]) == c:
            return 0
        if m == 0:
            l = split(s, e)
            for ss, ee in l:
                ans += f(ss, ee)
        else:
            ans += m
            for i in range(s, e):
                Hs[i] -= m
            ans += f(s, e)
        return ans

    return f(0, N)

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

Submission status: Accepted

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

opcnt = 0
pmh = 0
for i in range(N):
    if h[i] >= pmh:
        opcnt += h[i] - pmh
    pmh = h[i]

print(opcnt)

Submission status: Accepted

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque
from copy import deepcopy
from functools import lru_cache, reduce
from math import ceil, floor

import heapq
import itertools
import operator


def get_int():
    return int(input())


def get_str():
    return input().strip()


def get_int_list():
    return [int(i) for i in input().split()]


def get_char_list():
    return list(input().strip())


def split_goals_by_m(goals, m):
    ret = []
    tmp = []
    for g in goals:
        if g == m:
            if tmp:
                ret.append(tmp)
                tmp = []
        else:
            tmp.append(g - m)
    if tmp:
        ret.append(tmp)
    return ret


def get_ans(goals):
    m = min(goals)
    gs = split_goals_by_m(goals, m)
    if len(gs) == 0:
        return m
    ret = m
    for g in gs:
        ret += get_ans(g)
    return ret


def main():
    n = get_int()
    hs = get_int_list()

    print(get_ans(hs))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
count = 0


def mizu(h):
    global count
    if len(h) is 0:
        return
    if len(h) is 1:
        count = count + h[0]
        return
    m = min(h)
    count = count + m
    h = [i - m for i in h]
    while 0 in h:
        mizu(h[:h.index(0)])
        h = h[h.index(0)+1:]
    mizu(h)
    return count

if __name__ == "__main__":
    N = int(input())
    h = list(map(int, input().split()))
    mizu(h)
    print(count)

Submission status: Accepted

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

count = h[0]

now_min = h[0]
now_max = h[0]
up = True
h.append(0)

for i in h[1:]:
    if up:
        if now_max <= i:
            now_max = i
        else:
            count += now_max - now_min
            now_min = i
            up = False

    else:
        if now_min >= i:
            now_min = i
        else:
            up = True
            now_max = i

print(count)

Submission status: Accepted

Solution code:
n = int(input())
h = [int(m) for m in input().split()]
ans = 0
k = 1
tmp = -1
stmp = 0
numtmp = float("INF")
p = 0
while True:
    if k == 1:
        ans += min(h)
        t = min(h)
        for i in range(n):
            h[i] = h[i] - t
    else:
        for i in range(tmp + 1, n):
            p = 0
            if h[i] != 0:
                numtmp = min(numtmp, h[i])
                if numtmp == h[i]:
                    tmp = i
            else:
                tmp = i
                if numtmp != float("INF"):
                    ans += numtmp
                for j in range(stmp, tmp):
                    h[j] = h[j] - numtmp
                stmp = tmp + 1
                numtmp = float("INF")
                p = 1
            if i == n - 1 and p != 1:
                tmp = i + 1
                if numtmp != float("INF"):
                    ans += numtmp
                for j in range(stmp, tmp):
                    h[j] = h[j] - numtmp
                stmp = tmp + 1
                numtmp = float("INF")
                p = 1
    p = 0
    k += 1
    tmp = -1
    stmp = 0
    numtmp = float("INF")
    for i in range(n):
        if h[i] != 0:
            break
        if h[i] == 0:
            if i == n - 1:
                print(ans)
                exit()

Submission status: Accepted

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

cnt=0

for k in range(200):
	prev=0
	for i,x in enumerate(H):
		if x!=0:
			H[i]=x-1
			if prev!=0:
				pass
			else:
				cnt+=1
		prev=x
		

print cnt

Submission status: Accepted

Solution code:
n = int(input())
flower = input().split()
for i in range(n):
    flower[i] = int(flower[i])
a = 0
def water():
    if sum(flower) == 0:
        a = -1
    elif 0 in flower:
        i = 0
        while flower[i] == 0:
            i += 1
        j = i
        try:
            while flower[j+1] != 0:
                j += 1
        except:
            abc = i
        for k in range(i,j+1):
            flower[k] -= 1
    else:
        for i in range(n):
            flower[i] -= 1
ans = 0
while sum(flower) != 0:
    water()
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
h=input().split()
h.append(-1)
index=0
count=0
all_zeros=True
for i in h:
    if i!='0':
        all_zeros=False
        break

if all_zeros:
    print('0')
    exit(0)

while True:
    flag=True
    while flag:
        for i in range(index,len(h)):
            h[i]=int(h[i])
            if not h[i]-1<0:
                h[i]-=1
            elif h[index]==0:
                flag=False
                if index==i:
                    pass
                else:
                    count+=1
                break
            else:
                count += 1
                break

        #print(h)
        #print(count)
        #print('\n')
    counter=True
    while counter:
        index+=1
        if not h[index]==0:
            counter=False

    if h[index]==-1:
        print(count)
        exit(0)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
N=int(input())
H=list(map(int,input().split()))+[0]
ans=0
MIN=MAX=0
for i in range(N):
    if H[i]>H[i+1]:
        ans+=H[i]-MIN
        MIN=H[i+1]
    if i==N-1:
        print(ans)
        exit()

Submission status: Accepted

Solution code:
N = int(input())
h = [int(x) for x in input().split(" ")]
ans = [0 for _ in range(N)]
for i in range(N):
    if i == 0:
        ans[i] = h[i]
    elif h[i] <= h[i-1]:
        ans[i] = ans[i-1]
    else:
        ans[i] = ans[i-1] + (h[i] -h[i-1])
print(ans[N-1])

Submission status: Accepted

Solution code:
n = int(input())
H = [0] + list(map(int, input().split())) + [0]
def f(b):
    global H
    H[b] -= 1
    a = b - 1
    c = b + 1
    while 0 < a and H[a] != 0:
        H[a] -= 1
        a -= 1
    while c <= n and H[c] != 0:
        H[c] -= 1
        c += 1
ans = 0
for i in range(1, n + 1):
    while H[i] != 0:
        f(i)
        ans += 1
print(ans)

Submission status: Accepted

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

c=0
while True:
      mx=max(L)
      if mx==0:
         break
      idx=L.index(mx)
      L[idx]-=1
      while True:
         try:
            if L[idx+1]==mx:
               L[idx+1]-=1
               idx+=1
            else:
               break
         except:
            break
      c+=1
print(c)

Submission status: Accepted

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



ans = 0

def list_split(l):
    list_list = []
    start = 0
    for i in range(len(l)):
        # print(start, i)
        if l[i] == 0:
            list_list.append(l[start : i])
            start = i + 1
    list_list.append(l[start : i+1])
    return list_list

def f(lis):
    global ans

    if lis == [0]:
        return 1
    if lis == []:
        return
    # print("list is", lis)
    mini  = min(lis)
    ans+=mini
    for i in range(len(lis)):
        lis[i] -= mini


    list_list = list_split(lis)
    # print(list_list)
    for l in list_list:
        # print("f に　", l , "を入れます")
        f(l)


# print(list_split([1, 0, 1, 2, 0, 3, 0, 2, 3, 0, 1, 0]))

f(h)

print(ans)

Submission status: Accepted

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

max_h = max(h_list)
answer = 0

while max_h > 0:
	for i in range(N):
		if max_h == h_list[i]:
			l = i
			for j in range(i, N):
				if max_h != h_list[j]:
					r = j-1
					break
				if j == N-1:
					r = j
			for k in range(l, r+1):
				h_list[k] = h_list[k] - 1
			answer += 1

	max_h = max(h_list)

print(answer)

Submission status: Accepted

Solution code:
n = int(input())

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

j = 0
inde = 0
while(A.count(0) != n):
    for i in range(n):
        if(A[i] == 0):
            if(inde == 1):
                j+=1
                inde = 0
            else:
                continue
        else:
            A[i]-=1
            inde =1
            if(i == n - 1):
                j+=1
        
    inde = 0
print(j)

Submission status: Accepted

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

count = 0

while any(h):
    max_h = max(h)
    i = 0

    while i < len(h):
        if h[i] == max_h:
            j = i
            while j < len(h):
                if max_h == h[j]:
                    h[j] = h[j] - 1
                    j += 1
                else:
                    count += 1
                    break
            if j == len(h):
                count += 1
            i = j
        else:
            i += 1
print(count)

Submission status: Accepted

Solution code:
def hiki(lis):
    if len(lis) == 1:
        return [0], lis[0]
    else:
        new_lis = [i-min(lis) for i in lis]
        return new_lis, min(lis)

n = int(input())
h = [int(_) for _ in input().split()]

res = 0

while True:
    if not any(h):
        print(res)
        break

    # left
    for j in range(n):
        if h[j] == 0:
            continue
        else:
            ind_left = j
            break
    # right
    right_flag = False
    for j in range(ind_left, n):
        if h[j] != 0:
            continue
        else:
            ind_right = j
            right_flag = True
            break
    if not right_flag:
        ind_right = n+1

    h[ind_left:ind_right], count = hiki(h[ind_left:ind_right])
    res += count

Submission status: Accepted

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

ans = 0
st = "0"
while H!=[0]*N:
    for i in range(N):
        if H[i]>0:
            st = "+"
            H[i] -= 1
        elif st=="+" and H[i]==0:
            st = "0"
            ans += 1
    if st=="+":
        ans += 1
    st = "0"

print(ans)

Submission status: Accepted

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

ans = 0
s = 0
e = 0
while max(h) != 0:  
    for k in range(N):
        if h[k] != 0:
            s = k
            break
    if s == N-1:
        ans += h[N-1]
        break
    for k in range(s+1,N):
        e = k
        if h[k] == 0:
            break
        if k == N-1:
            e += 1
    t = min(h[s:e])
    for k in range(s,e):
        h[k] -= t
    ans += t
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np


N = int(input())
h = np.array(list(map(int, input().split())))
ans = 0
while np.sum(h) != 0:
    last_flg = 0
    water = np.zeros((N), dtype=np.int64)
    for i in range(N):
        # 値がまだある
        if h[i] != 0:
            water[i] = 1
        else:
            # 値がない
            if i == 0:
                # first number
                pass
            elif water[i-1] == 0:
                # 前の値もゼロ
                pass
            elif water[i-1] == 1:
                # 値の切れ目
                ans += 1
                h -= water
                break
        # last number
        if i == N-1:
            last_flg = 1

    if last_flg:
        ans += 1
        h -= water
print(ans)

Submission status: Accepted

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

def bunkatsu(list):
    returnlists = [[]]
    i = 0
    for num in list:
        if num != 0:
            returnlists[i].append(num)
        else:
            i = i + 1
            returnlists.append([])
    return returnlists

def mizuyari(list):
    if list == []:
        return 0
    else:
        i = 0
        while (list.count(0) == 0):
            i = i + 1
            list = [x - 1 for x in list]
        retlists = bunkatsu(list)
        for l in retlists:
            i += mizuyari(l)
        return i
print(mizuyari(hlist))

Submission status: Accepted

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

cnt = 0

while 1 == 1:
  split0, temp = [], []

  # リストリストhを、0以外の塊に分け、split0に入れる
  for i_ in h:
    for i in i_:
      if i == 0:
        if len(temp) >= 1:
          split0.append(temp)
        temp = []

      else:
        temp.append(i)
        
    if len(temp) >= 1:
      split0.append(temp)
    temp = []
        
  if len(temp) >= 1:
    split0.append(temp)

  # リストリストsplit0の各リストから1を引き、リストリストhに格納
  # ただし、すべて要素0の場合はhに格納しない
  h = []
  for i_ in split0:
    i_ = list(map(lambda x: x - 1, i_))
    cnt += 1
    
    if sum(i_) != 0:
      h.append(i_)
      
  if len(h) == 0:
    print(cnt)
    break

Submission status: Accepted

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

c = 0
while any(h):
    ans = 0
    for l in range(n):
        tmp = 0
        r = l
        while h[r]:
            tmp += 1
            r += 1
            if r == n:
                break
        if ans < tmp:
            x = l
            y = r
            ans = tmp
    for i in range(x, y):
        h[i] -= 1
    c += 1
print(c)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
S = [0 for i in range(N)]
res = 0
for h in range(100,0, -1):
    tmp = False
    for i in range(N):
        if H[i] >= h:
            tmp = True
        elif tmp:
            res += 1
            tmp = False
    if tmp:
        res += 1
print(res)

Submission status: Accepted

Solution code:
#!/Users/takahiro.kida/git/atcorder/venv/bin/python

import numpy
import scipy

from collections import defaultdict
from pprint import pprint

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

f = [0 for i in range(N)]

def update_f():
    start = False
    for i in range(N):
        if start and f[i] < h[i]:
            f[i] = f[i] + 1
            continue
        if start and f[i] >= h[i]:
            break
        if f[i] < h[i]:
            start = True
            f[i] = f[i] + 1
    if start:
        return False
    else:
        return True

cnt = 0
while True:
    if update_f():
        break
    else:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
def listcut(array):
    res = []
    kar = []
    for i in range(len(array)):
        if array[i] == 0:
            if len(kar) != 0:
                res.append(kar)
            kar = []
        else:
            kar.append(array[i])
    if len(kar) != 0:
        res.append(kar)
    return res


n = int(input())
h = list(map(int, input().split()))
kai = 0
wak = [h]
while wak != []:
    t = []
    for i in range(len(wak)):
        m = min(wak[i])
        kai += m
        k = list(map(lambda x: x - m, wak[i]))
        s = listcut(k)
        if s != []:
            t += s
    wak = t
print(kai)

Submission status: Accepted

Solution code:
M = int(input())
N = list(map(int, input().split()))
import numpy as np
npN = np.array(N)
count_all=0
for i in range (npN.max()):
    before = npN[0]
    
    if npN[0]==0:
        count=0
    else:
        count=1

    for i in range (len(N)):
        if before == 0 and npN[i]!= 0:
            count+=1
            before = npN[i]
        else:
            before = npN[i]
        if npN[i]!=0:
            npN[i]-=1
    count_all+=count
print(count_all)

Submission status: Accepted

Solution code:
import numpy as np

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

h = np.array(h)

count = 0
while(sum(h) != 0):
    s = 200
    e = n
    for i in range(n):
        if(s == 200):
            if(h[i] != 0): s = i
        elif(h[i] == 0):
            e = i
            break

    count += min(h[s:e])
    h[s:e] -= min(h[s:e])

print(count)

Submission status: Accepted

Solution code:
import sys

N = int(input())
h = [int(i) for i in input().split()]
ans = 0
check = [0] * N
ind = 0
judge = 0


for i in range(10**5):
    judge = 0
    for j in range(N):
        if 1 <= h[j]:
            h[j] -= 1

            if judge == 0:
                ans += 1
                judge = 1
            else:
                judge = 1

        else:
            judge = 0

    if max(h) == 0:
        print(ans)
        sys.exit()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
h = list(map(int, input().split()))
cnt = 0
while h and not h[-1]:
    h.pop()
while h:
    for l in reversed(range(len(h))):
        if not h[l]:
            l += 1
            break
    for i in range(l, len(h)):
        h[i] -= 1
    while h and not h[-1]:
        h.pop()
    cnt += 1
print(cnt)

Submission status: Accepted

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

current_h = [0] * N
c = 0

l = 0
while True:
    escape_loop = False
    while current_h[l] == h[l]:
        if l + 1 < N:
            l += 1
        else:
            escape_loop = True
            break
    if escape_loop:
        break

    r = l
    while True:
        if r + 1 < N and current_h[r + 1] < h[r + 1]:
            r += 1
        else:
            break

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

print(c)

Submission status: Accepted

Solution code:

N = int(input())

l = [int(x) for x in input().split(" ")]



mat = []
for _ in range(N):
	mat.append([])

for index, i in enumerate(l):
	for _ in range(i):
		mat[index].append(1)
	while len(mat[index]) != max(l):
		mat[index].append(0)


def trans(A:list) -> list:
    ans = []
    for col in range(len(A[0])):
        l = []
        for line in range(len(A)):
            l.append(A[line][col])
        ans.append(l)

    return ans    

garden = trans(mat)


count = 0
for line in reversed(garden):
	mark = False
	for index, i in enumerate(line):
		if i == 1 and mark == False:
			count += 1
			line[index] = 0
			mark = True
		
		elif i == 1 and mark == True:
			line[index] = 0

		elif i == 0:
			mark = False


print(count)

Submission status: Accepted

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

ans = 0
while any(height > 0 for height in h):
    ans += 1
    maxh = max(h)
    for i in range(n):
        if h[i] == maxh:
            for j in range(i,n):
                if h[j] == 0:
                    break
                else:
                    h[j] = h[j]-1
        else:
            continue
        break

print(int(ans))

Submission status: Accepted

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


#%%

#add 0 for both sides


#%%
#singlify 0
def singlify(h):
    # print(h)
    h = [0] + h + [0]
    # print(h)
    newh = []
    zero_flag = False
    for i in range(len(h)):
        if h[i] == 0:
            zero_flag = True
        else:
            if zero_flag:
                newh.append(0)
            newh.append(h[i])
            zero_flag = False
        # print(newh)
    newh.append(0)
    return newh


#%%
def minus_one_and_not_lower_than_zero(h):
    for i in range(len(h)):
        h[i] -= 1
        if h[i] == -1:
            h[i] = 0
    return h
#%%
# h = [3,1,2,3,1]

#%%
ans = 0

while True:
    h = singlify(h)
    # print(h)
    ans += h.count(0)-1
    h = minus_one_and_not_lower_than_zero(h)

    if sum(h) == 0:
        break

print(ans)
#%%


#%%

Submission status: Accepted

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

def remove_dpl(x):
  it=iter(x)
  before=next(it)
  res=[before]
  for e in it:
    if e!=before:
      res.append(e)
      before=e
  return res

c=0
h=remove_dpl(h)
while len(h)!=1:
  if h[0]>h[1]:
    c+=h[0]-h[1]
    h[0]=h[1]
  if h[len(h)-2]<h[len(h)-1]:
    c+=(h[len(h)-1]-h[len(h)-2])
    h[len(h)-1]=h[len(h)-2]
  for i in range(1,len(h)-1):
    if h[i]>h[i-1] and h[i]>h[i+1]:
      c+=h[i]-max(h[i-1],h[i+1])
      h[i]=max(h[i-1],h[i+1])
  h=remove_dpl(h)
print(c+h[0])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def watering(h, hk):
    count = 0
    need_water_pre_flower = False
    for i in h:
        need_water = i >= hk
        count += 1 if need_water and not need_water_pre_flower else 0
        need_water_pre_flower = need_water

    return count

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

max_h = max(h)
count_watering = 0
for i in range(1, max_h + 1):
    count_watering += watering(h, i)

print(count_watering)

Submission status: Accepted

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

ans = 0
pre = 0
for i in range(n):
    tmp = a[i] - pre
    if tmp>0:
        ans += tmp
    pre = a[i]

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N= int(input())
lis=list(map(int,input().split()))
S=lis[0]
b=lis[0]
for i in lis:
    if i>b:
        S+=(i-b)
    b=i
print(S)

Submission status: Accepted

Solution code:
def divide(L):
    ans = []
    prev = 0
    for i in range(len(L)):
        if L[i] == 0:
            ans.append(L[prev:i])
            prev = i + 1
    ans.append(L[prev:])
    return ans


def extract(L, v):
    for i in range(len(L)):
        L[i] -= v
    return L


def ans(L):
    if L == [0] * len(L):
        return 0
    count = 0
    divided = divide(L)
    for l in divided:
        if len(l) == 0:
            continue
        m = min(l)
        count += m
        extracted = extract(l, m)
        count += ans(extracted)
    return count


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

Submission status: Accepted

Solution code:
def water(h):
    global ans
    if len(h)==1:
        ans+=h[0]
    else:
        m=min(h)
        ans+=m
        for i in range(len(h)):
            h[i]-=m
        a=h.index(0)
        if a==0:
            water(h[1:])
        elif a==len(h)-1:
            water(h[:-1])
        else:
            water(h[:a])
            water(h[a+1:])

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

Submission status: Accepted

Solution code:
ans = 0
N = int(input())
a = list(map(int,input().split()))
for i in range(max(a)):
  x = 0
  z = 0
  count = 0
  for j in range(N):
    y = a[j]
    if y == 0 and x == 0:
      continue
    elif y != 0 and x == 0:
      count += 1
      a[j] -= 1
      z = 1
      x = 1
    elif y != 0 and z == 0:
      z = 1
      count += 1
      a[j] -= 1
    elif y != 0 and z == 1:
      a[j] -= 1
    elif y == 0 and z == 0:
      continue
    elif y == 0 and z == 1:
      z = 0
  ans += count
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
hk = [int(i) for i in stdin.readline().rstrip().split(" ")]

def calcCount(hks):
    counter = 0
    counter += min(hks)
    nhks = [i - counter for i in hks]
    splitTo = []
    for i in nhks:
        if i == 0:
            if len(splitTo) > 0:
                counter += calcCount(splitTo)
                splitTo = []
        else:
            splitTo.append(i)
    if len(splitTo) > 0:
        counter += calcCount(splitTo)
    return(counter)

print(calcCount(hk))

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
h = list(map(int, input().split()))

h = np.array(h)


def calc(h, s):
#     print(h, s)
    a = np.min(h)
    s += a
    h = h - a
    ind = np.where(h == 0)[0][0]
    h1 = h[:ind]
    h2 = h[ind+1:]
    if len(h1) != 0:
        s = calc(h1, s)
    if len(h2) != 0:
        s = calc(h2, s)
    return s

print(calc(h, 0))

Submission status: Accepted

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

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

Submission status: Accepted

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

count = 0

mini = min(hN)
count += mini

hN = [hi - mini for hi in hN]

while sum(hN) > 0:
    cont = False
    for hi in hN:
        if hi > 0:
            if not cont:
                count += 1
                cont = True
        else:
            cont = False
    hN = [hi - 1 if hi > 0 else hi for hi in hN]
print(count)

Submission status: Accepted

Solution code:
import numpy as np
N=int(input())
H=list(map(int, input().split()))
H=np.array(H)

flag=0


iteration=H.max()
total=0
#print(iteration)


for x in range(iteration):
    count=0
    flag=0
    
    if H[0]>0:
        count+=1
    
    for i in range(len(H)):
        if H[i] <=0:
            flag=1
        elif H[i] >0 and flag==1:
            count+=1
            flag=0
        

        #print(H)
        #print('count',count)
    total+=count
    H-=1
    
print(total)

Submission status: Accepted

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

h = [0]
h.extend(tmp)

res = 0

while 1 == 1:
    if max(h) <= 0:
        break
    for i in range(1,len(h)):
        if h[i] > 0 and h[i-1] <= 0:
            res = res + 1
    h = list(map(lambda x:x - 1,h))

print(res)

Submission status: Accepted

Solution code:
number = int(input())
data = list(map(int, input().split()))
ans = 0

def change(left, right):
    global data, ans
    hiku = min(data[left:right])
    for i in range(left, right):
        data[i] -= hiku
    ans += hiku

def main():
    global data, ans, number
    while 1:
        flg = 0
        if data.count(0) == number:
            break
        for i in range(number):
            if data[i] == 0:
                continue
            for j in range(i, number):
                if data[j] == 0:
                    flg = 1
                    change(i, j)
                    break
                elif j == number -1:
                    flg = 1
                    change(i, j+1)
                    break
            if flg:
                break
    print(ans)

if __name__ == '__main__':
    # a = [1,2,3,4]
    # b = min(a[3:4])
    # print(a[3]-b)
    main()

Submission status: Accepted

Solution code:
N = int(input())
h_list = [int(elem) for elem in input().split()]


num_waterings = 0

while sum(h_list) > 0:
    left = 0
    for right, h in enumerate(h_list):
        if h != 0:
            continue
        else:  # liquidate
            for i in range(left, right):
                h_list[i] -= 1
            if left < right:
                num_waterings += 1
            left = right + 1
            
    if left < N:
        for i in range(left, N):
            h_list[i] -= 1
        num_waterings += 1
    # print(num_waterings)
    # print(h_list)

# print(h_list)
print(num_waterings)

Submission status: Accepted

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

import numpy as np

def bunkatsu(group): # groupを分割して
    #print('group', group)
    if group == [0]*len(group):
        return 0

    tmp = []
    group_split = []
    ans = 0
    for i,e in enumerate(group):
        
        if e != 0:
            tmp.append(e-1)
        elif len(tmp)>0:
            #print('tmp',tmp)
            ans += 1 + bunkatsu(tmp)
            tmp = []
    if len(tmp)>0:
        ans += 1 + bunkatsu(tmp)

    #print('group, ans',group, ans)
    return ans

print(bunkatsu(h))

Submission status: Accepted

Solution code:
import numpy as np

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

cnt=0
while (1):
    target=h.max()
    if target == 0 :
        print(cnt)
        break
    
    flag = 0
    for i, hi in enumerate(h) :
        if hi == target:
            if flag == 0:
                flag = 1
                cnt+= 1
            h[i]-=1
        else :
            flag=0

Submission status: Accepted

Solution code:
import sys

n = int(input())
hs = list(map(int, input().split()))
goal = [0 for _ in range(n)]

count = 0
while True:
    # print(hs)
    if hs == goal:
        print(count)
        sys.exit()

    count += 1
    is_done = False
    for i in range(n):
        if hs[i] == 0:
            if is_done:
                break
        else:
            hs[i] = hs[i] - 1
            is_done = True

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
h = [int(n) for n in input().split(' ')]
start = 0
out = 0

def end(start, h):
    m = h[start]
    for i in range(start, N):
        if h[i] != 0:
            if h[i] < m:
                m = h[i]
        else:
            return i-1, m
    else:
        return i, m

while start < len(h):
    i, m = end(start, h)
    if i != start-1:
        for j in range(start, i+1):
            h[j] -= m
        out += m
    else:
        start += 1
else:
    print(out)

Submission status: Accepted

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

min = []
max = []
ans =0
while l !=[0]*N:
    ans += 1
    s =False
    for i,j in enumerate(l):
        if s:
            if j == 0:
                break
        if j !=0:
            s =True
            l[i]-=1
print(ans)

Submission status: Accepted

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

a = li[0]
ans = a
for i in range(1, len(li)):
    if a >= li[i]:
        a = li[i]
    else:
        ans += li[i]-a
        a = li[i]

print(ans)