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:
import heapq

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

s = 0
Q = []
heapq.heappush(Q, h)
while Q:
	a = heapq.heappop(Q)
	x = min(a)
	s += x
	for i in range(len(a)):
		a[i] -= x
	L = []
	for i in range(len(a)):
		if a[i] == 0:
			if L!= []:
				heapq.heappush(Q, L)
				L = []
		else:
			L.append(a[i])
	if L != []:
		heapq.heappush(Q, L)

print(s)

Submission status: Accepted

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

count = 0

for i in range(n):
	while(l[i] > 0):
		for j in range(i + 1, n):
			if l[j] == 0:
				break
		for k in range(i, j):
			l[k] -= 1
		count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
watering = False
while sum(h) > 0:
    for i in range(n):
        if h[i] > 0:
            if not watering:
                ans += 1
            watering = True
            h[i] -= 1
        else:
            watering = False
    watering = False
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
listH = list(map(int, input().split()))
listNull=[0]*n
sumW=0
while True:
    if listH == listNull:
            break
    currentL=[]
    index=0
    minM=100
    startI=0
    while index < n:
        if listH[index]!=0:
            if listH[index] < minM:
                minM = listH[index]
            currentL.append(listH[index])
        else:
            if len(currentL)>0:
                index2=startI
                sumW += minM
                while index2 < index:
                    if listH[index2]!=0:
                        listH[index2]-=minM
                    index2+=1
                startI = index+1
                currentL = []
            else:
                pass
        index+=1
    if len(currentL) != 0:
        index2 = startI
        sumW += minM
        while index2 < index:
            if listH[index2] != 0:
                listH[index2] -= minM
            index2 += 1
        startI = index+1
        currentL = []
    else:
        pass

print(sumW)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
res=0
t=0
while(any(h)):
    if h[t]==0:
        t+=1
    else:
        j=False
        for k in range(t,n):
            if h[k]==0:
                j=True
                break
        if j:
            k-=1
        for i in range(t,k+1):
            h[i]-=1
        res+=1
print(res)

Submission status: Accepted

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

def dfs(l):
    if len(l)==1:
        return l[0]
    else:
        a=l.index(min(l))
        if a==0:
            return dfs(l[1:])
        elif a==len(l)-1:
            return dfs(l[:-1])
        else:
            return dfs(l[:a])+dfs(l[a+1:])-min(l)

print(dfs(H))

Submission status: Accepted

Solution code:
a=int(input())
b=input().split()
p=0
n=0
for i in b:
  i=int(i)
  if i>p:
    n+=(i-p)
  p=i
print(n)

Submission status: Accepted

Solution code:
def rec(lst, left, right):
    if left == right:
        return 0
    if not 0 in lst[left:right]:
        m = min(lst[left:right])
        lst[left:right] = [x - m for x in lst[left:right]]
        return m + rec(lst, left, right)
    k = lst[left:right].index(0)
    return rec(lst, left, left + k) + rec(lst, left + k+1, right)


N = int(input())
lst = list(map(int, input().split()))
print(rec(lst, 0, len(lst)))

Submission status: Accepted

Solution code:
import numpy as np
import sys

def split_(arr, ite):
    new = [arr[:ite[0]]]
    if len(ite)==1:
        if ite[0]==len(arr)-1:
            return new
        else:
            new.append(arr[ite[0]+1:])
            return new
    else:
        for i in range(1,len(ite)):
            new.append(arr[ite[i-1]+1:ite[i]])
        if ite[-1]==len(arr)-1:
            return new
        else:
            new.append(arr[ite[-1]+1:])
            return new

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

ans = 0
while h:
    if len(h[0])==1:
        ans += h[0][0]
        h.pop(0)
    else:
        m = min(h[0])
        ans += m
        h[0] -= m
        filtered = list(filter(lambda x: h[0][x]==0, range(len(h[0]))))
        splited = split_(h[0], filtered)
        h.pop(0)
        for s in splited:
            if len(s)>0:
                h.append(s)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
v=0
s=0
ans=0
l.append(0)
for i in range(max(l)):
    v=0
    s=0
    while v<len(l):
        while l[v]!=0:
            v+=1
        if l[s]!=0:
            for y in range(v-s):
                l[s+y]-=1
            ans+=1
        s=v+1
        v+=1
print(ans)

Submission status: Accepted

Solution code:
N = [int(x) for x in input().split()]
H = [int(x) for x in input().split()]
INF = 10**9

ans = 0
while True:
    if sum(H) == 0:
        break
    s, e = 0, len(H)
    for i, h in enumerate(H):
        if h != 0:
            s = i
            break
    for i, h in enumerate(H):
        if i <= s:
            continue
        if h == 0:
            e = i
            break
    H[s:e] = [h-1 for h in H[s:e]]
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random

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

count = 0

stack = []
stack.append(h)
while len(stack) != 0:
    group = stack.pop()
    # print(group)

    count += min(group)
    minus = min(group)
    for i in range(len(group)):
        group[i] -= minus

    begin = 0
    # print(group)
    for i in range(len(group)):
        if group[i] == 0:
            if len(group[begin:i])!=0:
                stack.append(group[begin:i])
            begin = i+1
        elif i == len(group)-1:
            if len(group[begin:])!=0:
                stack.append(group[begin:])
    # print(stack)
    # print(count)
print(count)

Submission status: Accepted

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

def solve(L):
    global ans
    if len(L)==1:
        ans+=L[0]
    else:
        min=INF
        index=-1
        for i in range(len(L)):
            if L[i]<=min:
                index=i
                min=L[i]
        for i in range(len(L)):
            L[i]-=min
        ans+=min

        if 0<index<len(L)-1:
            solve(L[:index])
            solve(L[index+1:])
        elif index==0:
            solve(L[index+1:])
        else:
            solve(L[:index])

solve(A)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
h=list(map(int,input().split()))
def calc(h):
        if h==[]:
                return 0
        if 0 not in h:
                return calc(list(map(lambda x:x-1,h)))+1
        if h[0]==0:
                return calc(h[1:])
        elif h[-1]==0:
                return calc(h[:-1])
        for i in range(1,len(h)-1):
                if h[i]==0:
                        return calc(h[:i])+calc(h[i+1:])
print(calc(h))

Submission status: Accepted

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

def mizuyari(niwa):
	if niwa == []:
		return 0
	if 0 not in niwa:
		return mizuyari(list(map(lambda x: x - 1, niwa))) + 1
	if niwa[0] == 0:
		return mizuyari(niwa[1:])
	elif niwa[-1] == 0:
		return mizuyari(niwa[:-1])
	for i in range(1, len(niwa) - 1):
		if niwa[i] == 0:
			return mizuyari(niwa[:i]) + mizuyari(niwa[i+1:])

print(mizuyari(h))

Submission status: Accepted

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

    print(ans)
        
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

def th_cnt(arr, th):
    old = 0
    cnt = 0
    for a in arr:
        if old == 0 and th < a:
            cnt += 1
            old = 1
        if a <= th:
            old = 0
    return(cnt)

ret = 0
for i in range(max(arr)):
    ret += th_cnt(arr, i)

print(ret)

Submission status: Accepted

Solution code:
def water(h):
    if len(h) == 0:
        #print("A0")
        return  0
    
    elif len(h) == 1:
        #print("A1")
        return h[0]
    
    elif R0(h) + 1:
        #print("B")
        #print(h[0: R0(h)],h[R0(h): len(h)])
        return water(h[0: R0(h)]) + water(h[R0(h) + 1: len(h)])
    
    else:
        #print("C")
        for i in range(len(h)):
            h[i] -= 1
        #print(h)
        return water(h) + 1

def R0(l):
    if 0 in l:
        return l.index(0)
    else:
        return -1

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

print(water(h))

Submission status: Accepted

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

def culc(h_tmp):
  if len(set(h_tmp))==1:
    return h_tmp[0]
  elif len(h_tmp)==0:
    return 0
  else:
    min_h = min(h_tmp)
    i = h_tmp.index(min_h)
    h_tmp = list(map(lambda x:x-min_h,h_tmp))
    return culc(h_tmp[:i]) + culc(h_tmp[i+1:]) + min_h

print(culc(h))

Submission status: Accepted

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

count = 0

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

count = count + h[N-1]

print(count)

Submission status: Accepted

Solution code:
class flowers:
    def __init__(self, str_h):
        self.str = str_h.split()
        self.int = []
        self.min = 0
        self.water = 0
        
        self.update_int()
        self.update_min()
    
    def update_str(self):
        self.str = list(map(str, self.int))
        return

    def update_int(self):
        self.int = list(map(int, self.str))
        return
    
    def update_min(self):
        self.min = min(self.int)
        return
    
    def minimize(self):
        self.int = [n-self.min for n in self.int]
        self.water += self.min
        self.update_str()
        self.min = 0
        return
    
    def str_split0(self):
        idxs = [-1] + [i for i,n in enumerate(self.int) if n==0]
        if len(idxs)==1:
            return [self.str]
        else:
            temp_ls = []
            for i in range(len(idxs)-1):
                s = self.str[idxs[i]+1:idxs[i+1]]
                if len(s)>0: temp_ls.append(s)
            if self.int[-1]!=0:
                temp_ls.append(self.str[idxs[-1]+1:])
        return temp_ls

N = input()
str_h = input()

fls = flowers(str_h)
ls_fls = fls.str_split0()
water = 0

while(len(ls_fls)>0):
    #print(f'ls_fls = {ls_fls}')
    temp_ls_fls = []
    for fls in ls_fls:
        if len(fls)<=2:
            water += max([int(s) for s in fls])
            continue
        else:
            _fls = flowers(' '.join(fls))
            _fls.minimize()
            _ls_fls = _fls.str_split0()
            water += _fls.water
            temp_ls_fls += _ls_fls
            flg=1
    ls_fls = temp_ls_fls
print(water)

Submission status: Accepted

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

data.append(0)
tmp=0
ans=0
for i in data:
    if i < tmp:
        ans+=(tmp-i)
        tmp=i
    else:
        tmp=i
print(ans)

Submission status: Accepted

Solution code:

k=int(input())
a=list(map(int,input().split()))
a=[0]+a+[0]
count=0

while sum(a)!=0:
    b=[]

    m=-10000
    for i in a:
        if i!=m:
            b.append(i)
            m=i




    for i in range(1,len(b)-1):    
        if b[i]>b[i-1] and b[i]>b[i+1] and b[i]!=0:
            count+=b[i]-max(b[i-1],b[i+1])
            b[i]=max(b[i-1],b[i+1])
            
    a=b.copy()
        
print(count)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import deque


def main():
    N = int(input())
    h = list(map(int, input().split()))
    q = deque()
    q.append(h)
    ans = 0
    while len(q):
        t = q.popleft()
        if len(t) == 0:
            continue
        minimum = min(t)
        ans += minimum
        l = 0
        for i in range(len(t)):
            if t[i] == minimum:
                q.append([x-minimum for x in t[l:i]])
                l = i+1
        q.append([x-minimum for x in t[l:len(t)]])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
count = 0
previousNum = 0
allNumberIsZero = False
while not allNumberIsZero:
    nonZeroExists = False
    for i in range(N):
        if h[i] > 0: nonZeroExists = True
        if previousNum == 0:
            if h[i] > 0:
                count += 1
                previousNum = h[i]
                h[i] -= 1
            else:
                previousNum = 0
        else:
            if h[i] > 0:
                previousNum = h[i]
                h[i] -= 1
            else:
                previousNum = 0
    previousNum = 0
    if not nonZeroExists: allNumberIsZero=True
print(count)

Submission status: Accepted

Solution code:
mizuyari=0
n=int(input())
s=list(map(int,input().split()))
def hiku(n):
    return n-1
for i in range(100):
    if s[0]>0:
        mizuyari+=1
    for j in range(n-1):
        if s[j]<=0 and s[j+1]>0:
            mizuyari+=1
    s=list(map(hiku,s))
print(mizuyari)

Submission status: Accepted

Solution code:
#coding:utf-8
n = int(input())
h_ent = list(map(int, input().split()))

count = 0

while True:
    if len(h_ent)  == 0:
        break
    elif h_ent[0] == 0:
        del h_ent[0]   #リストの先頭が0なら消す
    elif not 0 in h_ent:
        h_ent = list(map(lambda x:x-1, h_ent))
        count += 1
    else:
        for i in range(0, h_ent.index(0)):
            h_ent[i] -= 1
        count += 1

def out(x):
    print(x)

out(count)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
input()
flowers = list(map(int,input().split()))
def mizuyari(*f):
  num = 0
  if f == ():
    return 0
  f_min = min(f)
  f_min_index = f.index(f_min)
  num += f_min
  f = [i-f_min for i in f]
  num += mizuyari(*f[:f_min_index])
  num += mizuyari(*f[f_min_index+1:])
  return num

ans = mizuyari(0, *flowers)
print(ans)

Submission status: Accepted

Solution code:
def solve(string):
    n, *h = map(int, string.split())
    return str(h[0] + sum([max(0, h1 - h0) for h0, h1 in zip(h[0:], h[1:])]))


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Submission status: Accepted

Solution code:
def solve(string):
    n, *h = map(int, string.split())
    count = h[0]
    for h0, h1 in zip(h[0:], h[1:]):
        count += max(0, h1 - h0)
    return str(count)


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Submission status: Accepted

Solution code:
def solve(string):
    n, *h = map(int, string.split())
    count = 0
    current = 0
    for _h in h:
        count += max(0, _h - current)
        current = _h
    return str(count)


if __name__ == '__main__':
    print(solve("\n".join([input(), input()])))

Submission status: Accepted

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

M = [H[:]]

a = 0
while M!=[]:
	m = M.pop(0)
	a += min(m)
	s = list(map(lambda x:x-min(m), m))

	start = 0
	for i in range(len(s)):
		if s[i]==0:
			t = s[start:i]
			start = i+1
			if t!=[]: M.append(t)

	t = s[start:]
	if t!=[]: M.append(t)

print(a)

Submission status: Accepted

Solution code:
N = int(input())
h = [0]+[int(x) for x in input().split()]
print(sum([x-y for x,y in zip(h[1:],h[:-1]) if (x-y)>0]))

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0
ok = False
while not ok:
    ok = True
    for l in range(n):
        if hs[l] > 0:
            for r in range(l, n + 1):
                if hs[r] == 0:
                    ans += 1
                    ok = False
                    break
                else:
                    hs[r] -= 1
            break

print(ans)

Submission status: Accepted

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

print(count)

Submission status: Accepted

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

cnt = 0
for _ in range(100):
    flag = 1
    for i in range(n):
        if A[i] != 0:
            A[i] -= 1
            if flag == 1:
                cnt += 1
                flag = 0
        else:
            flag = 1

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
res = 0
tmp = [0 for _ in range(N)]

while True:
    #res += 1
    flag = False
    co = 0
    for i in range(N):
        if L[i] != tmp[i] and flag == True:
            tmp[i] += 1
        elif L[i] != tmp[i] and flag == False:
            tmp[i] += 1
            flag = True
            res += 1
        elif L[i] == tmp[i]:
            flag = False
            
    for i in range(N):
        if L[i] == tmp[i]:
            co += 1
            
    if co == N:
        break
        
print(res)

Submission status: Accepted

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

h_diff = [x-y for x,y in zip(h[:-1], h[1:])]
h_diff_pos = [x for x in h_diff if x>0]
ans = sum(h_diff_pos) + h[-1]
print(ans)

Submission status: Accepted

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

res = 0
pre = 0
h.insert(0,0)

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
# 全要素について前後比較。また、比較時に変数に後方を保存
# 次の要素の方が大きかったら、大きくみると山が一区切りするので差分を答えに加える
# 次の要素が小さい場合は、無視。変数の後方保存だけを行う。
# やってることはつまりh[i+1]-h[i]で正のときのみの評価
res = 0
pre = 0
for i in h:
    if i>pre:
        res += i-pre
    pre = i
else:
    print(res)

Submission status: Accepted

Solution code:
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

N = ri()
h = rl()

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

print(ans)

Submission status: Accepted

Solution code:

# coding: utf-8

# In[5]:


import numpy as np


# In[6]:


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


# In[34]:


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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ri = lambda: int(input())
rl = lambda: list(map(int,input().split()))

N = ri()
h = rl()
hl = len(h)

ans = 0
flag = 0
for _ in range(sum(h)):
    if sum(h) == 0:
        break
    flag = 1
    for i in range(hl):
        if flag == 1 and h[i] != 0:
            flag = 0
            h[i] = h[i] - 1
            ans += 1
        elif flag == 0 and h[i] == 0:
            flag = 1
        elif flag == 0 and h[i] != 0:
            h[i] = h[i] - 1

print(ans)

Submission status: Accepted

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

def out(l):
    ll = bunkatu(l)
    
    res = 0
    
    for l in ll:
        
        #全て-1
        l2 = [i - 1 for i in l]
        
        #[]想定
        if not l2:
            continue
        
        if max(l2) > 0:
            res += 1 + out(l2)
        else:
            res += 1
        
    return res

def bunkatu(l):
    #0でリストを分ける
    s = 0 #start
    ll = []
    
    for k, v in enumerate(l) :
        #最初に0が渡されるかもしれないので0以下
        if v <= 0 :
            ll.append(l[s:k])
            s = k + 1
    #ループ最後
    else:
        if s != k + 1:
            ll.append(l[s:k + 1])
    return ll


print(out(l))

Submission status: Accepted

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

def garden(lst, count):
    if lst == [] or max(lst) == 0:
        return count
    elif 0 not in lst:
        lst = list(map(lambda a: a-1, lst))
        count += 1
        return garden(lst, count)
    elif 0 in lst:
        return count + garden(lst[:lst.index(0)], 0) + garden(lst[lst.index(0) + 1:], 0)

print(garden(h, 0))

Submission status: Accepted

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

def watering(lst, count):
  if lst == [] or max(lst) == 0:
    return count
  if min(lst) == 0:
    return count + watering(lst[:lst.index(0)], 0) + watering(lst[lst.index(0)+1:], 0)
  else:
    count += 1
    lst = list(map(lambda x:x-1, lst))
    return watering(lst, count)
print(watering(hlist, 0))

Submission status: Accepted

Solution code:
#-*- coding: utf-8 -*-
"""
oj dl https://atcoder.jp/contests/abc116/tasks/abc116_b -d test-b
oj test -d test-c -c "python abc116c.py"
oj test -d test-b -c "python abc116b.py" test-b/sample-3.in
"""

from collections import defaultdict
import sys
import math
import time

def deco(func):
    def wrapper(*args, **kwargs):
        s = time.time()
        #print('--start--')
        ret = func(*args, **kwargs)
        logging.debug('--end in %f --' % (time.time() - s) )
        return ret
    return wrapper


#@deco
def sol(S, n):
    cnt = 0
    ttl = sum(S)
    while ttl > 0:
        mark = False
        for i, s in enumerate(S):
            if s > 0:
                mark = True
                S[i]-=1
            elif mark and s == 0:
                break
        logging.debug("ttl:%s, cnt:%s, S:%s" % (ttl, cnt, S))
        cnt += 1
        ttl = sum(S)
    return cnt



import logging
#logging.basicConfig(level=logging.DEBUG, format="%(message)s")
logging.basicConfig(level=logging.ERROR, format="%(message)s")
do_submit = True
#do_submit = False
def input_parse(input_str):
    lines = [x.strip() for x in input_str.split("\n") if x.strip()]
    parsed_lines = [list(map(int, line.split())) for line in lines]
    n = parsed_lines[0][0]
    S = parsed_lines[1]
    return (n, S)


if not do_submit:
    # n, S= input_parse("""
    # 4
    # 1 2 2 1
    # """)
    n, S= input_parse("""
    5
    3 1 2 3 1
    """)
    print(sol(S, n))
else:
    # a, b, c = list(map(int, input().split()))
    # print(sol(a, b, c))
    n = int(input())
    S = list(map(int, input().split()))
    print(sol(S, n))

Submission status: Accepted

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

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

print(acc)

Submission status: Accepted

Solution code:
#abc116c

n=int(raw_input())
h=map(int,raw_input().split())
res=h[0]
for i in xrange(1,n):
 res+=max(h[i]-h[i-1],0)
print res

Submission status: Accepted

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

# 4 5 6 0 3 8 7 4 0 0 3 7 0 0 0 32


def lands(l):
    l += [0]
    lands = 0
    for i in range(len(l)):
        if l[i] == 0 and l[i-1] != 0:
            lands += 1
    return lands


def r(h, c):
    c += lands(h)
    # print(c)
    h = list(map(lambda x: x - 1 if x != 0 else 0, h))
    if h == [0] * len(h):
        print(c)
    else:
        r(h, c)


r(h, 0)

Submission status: Accepted

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

Submission status: Accepted

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


def solve(N: int, h: "List[int]"):
    print(watering(h))
    return

def watering(h):
    if sum(h) == 0:
        return 0
    else:
        max_height = max(h)
        s, e = max_sub_sequence(h, max_height)
        assert s >= 0 and e < len(h), "invalid s: {} & e: {}".format(s, e)
        if s == 0 and e == len(h)-1:
            return max_height
        elif s == 0:
            diff = max_height - h[e+1]
            h[s:e+1] = [h[e+1]] * (e+1-s)
            return watering(h) + diff
        elif e == len(h)-1:
            diff = max_height - h[s-1]
            h[s:e+1] = [h[s-1]] * (e+1-s)
            return watering(h) + diff
        else:
            if h[s-1] < h[e+1]:
                diff = max_height - h[e+1]
                h[s:e+1] = [h[e+1]] * (e+1-s)
                return watering(h) + diff
            else:
                diff = max_height - h[s-1]
                h[s:e+1] = [h[s-1]] * (e+1-s)
                return watering(h) + diff

def max_sub_sequence(h, max_height):
    i = 0
    j = 0
    while i < len(h):
        if h[i] == max_height:
            j = i + 1
            while j < len(h) and h[j] == max_height:
                j += 1
            j -= 1
            break
        i += 1
    return i, j

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

arr = list(map(int,input().strip().split()))
s = []
a = []
arr.append(0)
for i in range(n+1):
    if arr[i]==0:
        if len(a) > 0:
            s.append(a)
            a = []
    else:
        a.append(arr[i])
ans = 0

#print(s)
while len(s)!=0:
    #ans+=len(s)
    d = []
    #print(s)
    for i in range(len(s)):
        x = min(s[i])
        s[i] = [ s[i][j]-x for j in range(len(s[i])) ]
        a = []
        e = []
        ans+=x
        s[i].append(0)
        for j in range(len(s[i])):
            if s[i][j]==0:
                if len(a) > 0:
                    d.append(a)
                    a = []
            else:
                a.append(s[i][j])
        
    s = d
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
count = 0
while any(x > 0 for x in H):
    if H[-1] == 0:
        while H[-1] == 0:
            del H[-1]
    if H[0] == 0:
        while H[0] ==0:
            del H[0]
    for i in range(len(H)):
        if H[i] == 0:
            if H[i + 1] != 0:
                count +=1
        else:
            H[i] -= 1
    count += 1
print(count)

Submission status: Accepted

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

count = 0
prev = 0

for h in hn:
    count += max(0, h-prev)
    prev = h

print(count)

Submission status: Accepted

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

f = False
c = 0

for k in range(0, 100):
    for i in range(0, n):
        if f:
            if k >= h[i]:
                f = False
                c += 1
        else:
            if k < h[i]:
                f = True
    if f:
        f = False
        c += 1

print(c)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
L.append(0)
K = [0]*(N+1)
cnt = 0
c = 0
flag = True
while flag:
  if K == L:
    flag = False
  else:
    for i in range(N+1):
      if K[i] < L[i]:
        K[i] += 1
        cnt += 1
      else:
        if cnt != 0:
          c += 1
          cnt = 0
print(c)

Submission status: Accepted

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

water_times=0
while max(H)>0:
  left_water=0
  for h in H:
    if h>0 and left_water==0:
      water_times+=1
      left_water=1
    elif h>0 and left_water==1:
      pass
    else:
      left_water=0
  H=[h-1 for h in H]

print(water_times)

Submission status: Accepted

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

for t in range(len(list)):
    list[t] = int(list[t])

cnt = 0

for idx in range(n):
    
    # 高さ：0 <= h <=100
    for t in range(101):
        if 0 != list[idx]:
            # 最小値が0以外の場合
            idx2 = n
            t_cnt = min(list[idx:n])
            
            # 最小値が0の場合
            # 0までの最小値を求める
            if t_cnt == 0:
                idx2 = list[idx:n].index(0) + idx
                t_cnt = min(list[idx:idx2])

            for idx3 in range(idx,idx2):
                list[idx3] = list[idx3] - t_cnt
            cnt += t_cnt
        else:
            break
        
print(cnt)

Submission status: Accepted

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

for t in range(len(list)):
    list[t] = int(list[t])

cnt = 0

for idx in range(n):
    for t in range(101):
        if 0 != list[idx]:
            t_cnt = min(list[idx:n])
            idx2 = n
            if t_cnt == 0:
                idx2 = list[idx:n].index(0) + idx
                t_cnt = min(list[idx:idx2])
            for idx3 in range(idx,idx2):
                list[idx3] = list[idx3] - t_cnt
            cnt += t_cnt
        else:
            break
        
print(cnt)