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 a(l):
  if len(l) == 0:
    return 0
  elif len(l) == 1:
    return l[0]
  else:
    m = min(l)
    for j in range(len(l)):
      if m == l[j]:
        return l[j] + a(map(lambda x: x-m,l[0:j])) + a(map(lambda x: x-m,l[j+1:]))
        

i = int(input())
l = raw_input().split()
l = map(lambda x: int(x),l)

print a(l)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for s, t in zip(h[:-1], h[1:]):
  ans += max(t-s, 0)

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
c_l = list(map(int, input().split()))
p_l = [0]*n
ans = 0
is_prev_correct = True
while True:
  for i in range(n):
    if c_l[i] == p_l[i]:
      is_prev_correct = True
    elif is_prev_correct:
      p_l[i] += 1
      ans += 1
      is_prev_correct = False
    else:
      p_l[i] += 1
  is_prev_correct = True
  if p_l == c_l:
    break
print(ans)

Submission status: Accepted

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

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def ceil(a, b=1): return int(-(-a // b))
def round(x): return int((x*2+1) // 2)
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N = INT()
aN = LIST()

ans = 0
for i in range(1, max(aN)+1):
    l = r = 0
    # 外ループで左を動かす
    while l < N:
        # 内ループは条件を満たす限り右を動かす
        while r < N and aN[r] >= i:
            r += 1
        if l != r:
            ans += 1
            l = r
        else:
            l += 1
            r += 1
print(ans)

Submission status: Accepted

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

def calc(h):
    N = len(h)
    r = 0
    min_h = min(h)
    if 0 < min_h:
        r += min_h
        for i in range(N):
            h[i] -= min_h

    while h[0] != 0:
        j = N
        for i in range(1, N):
            if h[i] == 0:
                j = i
                break
        r += 1
        for i in range(j):
            h[i] -= 1

    if N == 1:
        return r
    else:
        return r + calc(h[1:])


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

    r = calc(h)

    print(r)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
hs = list(map(int, input().split()))
flags = [i >= 1 for i in hs]
# 最後のエッジ
flags.append(False)

ans = 0
while any(flags):
    for i in range(n):
        # 1→0 のエッジ
        if flags[i] and not flags[i+1]:
            ans += 1
        hs[i] = hs[i] - 1 if hs[i] else 0
    flags = [j>=1 for j in hs]
    flags.append(False)
print(ans)

Submission status: Accepted

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

cnt = 0
while True:
    flag = True
    prev = -1
    # print(hs)
    for i in range(n):
        h = hs[i]
        if h == 0:
            # 0→0
            if prev == -1:
                continue
            # 1→0
            else:
                cnt += 1
                prev = -1
        else:
            # 0→1
            if prev == -1:
                flag = False
                hs[i] -= 1
                prev = 0
            # 1→1
            else:
                hs[i] -= 1
    if prev != -1:
        cnt += 1
    if flag:
        break
print(cnt)

Submission status: Accepted

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

ans = 0
k = 1

flags = [li[i] >= k for i in range(N)]
flags.append(False)

while any(flags):
    cnt = 0
    for i in range(N):
        if flags[i] and not flags[i + 1]:
            cnt += 1

    k += 1
    ans += cnt
    flags = [li[i] >= k for i in range(N)]
    flags.append(False)

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [[int(h) for h in input().split()]]
m = 0
while A != []:
    for i in range(len(A[0])):
        if A[0][i] == 0:
            if i == 0:
                if A[0] == [0]:
                    del A[0]
                else:
                    del A[0][0]
            elif i != 0 and i == len(A[0]) - 1:
                del A[0][i]
            else:
                A.append([A[0][j] for j in range(i)])
                A.append([A[0][j] for j in range(i + 1, len(A[0]))])
                del A[0]
            break
    else:
        m = m + 1
        A.append([A[0][i] - 1 for i in range(len(A[0]))])
        del A[0]
print(m)

Submission status: Accepted

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

ans = 0

while len(L) :
    if L[0] == 0 :
        L.pop(0)
    else :
        ans += 1
        for i in range(len(L)) :
            if L[i] != 0 :
                L[i] -= 1
            else :
                break
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
H=[int(i) for i in input().split()]
W=[0]*N
num=0
i=0
while i<N:
    if W[i]==H[i]:
        i+=1
        continue
    for j in range(i,N):
        if W[j]>=H[j]:
            break
        W[j]+=1
    num+=1
    #print(W)
print(num)

Submission status: Accepted

Solution code:
import sys

def main():
    sys.setrecursionlimit(1000000) # recursion limit hack
    buf = input()
    N = int(buf)
    buf = input()
    buflist = buf.split()
    h = list(map(int, buflist))
    done = []
    height = []
    for i in range(N):
        if h[i] == 0:
            done.append(True)
        else:
            done.append(False)
        height.append(0)
    print(recursion_water(h, done, height, 0))

def recursion_water(h, done, height, count):
    for i in range(len(h)):
        if done[i]:
            continue
        else:
            count += 1
            for j in range(i, len(h)):
                if not done[j]:
                    height[j] += 1
                    if height[j] == h[j]:
                        done[j] = True
                else:
                    break
            return recursion_water(h, done, height, count)
    return count

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def garden(fw):
    if len(fw) == 0:
        return 0
    if len(fw) == 1:
        return fw[0]
    else:
        fwmin = min(fw)
        fwmin_idx = fw.index(fwmin)
        fwl = [x - fwmin for x in fw[:fwmin_idx]]
        fwr = [x - fwmin for x in fw[fwmin_idx + 1:]]
        return garden(fwl) + fwmin + garden(fwr)

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

Submission status: Accepted

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

def watering(num_list):
    num_list_ = []
    l_ = [str(l-1) for l in map(int, num_list)]
    num_list_.extend(list_split(l_, '0'))

    return num_list_

def list_split(num_list, s):
    list_ = [n if n != s else 'x' for n in num_list]
    return [_.split() for _ in ' '.join(list_).split('x')  if _ is not '' and _ is not ' ']

h_ = list_split(h, '0') 

i = 0
while(1):
    if len(h_) == 0:
        break
    else:
        new_h = []
        for _ in h_:
            new_h.extend(watering(_))
            i += 1
        h_ = new_h
            
print(i)

Submission status: Accepted

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

def watering(num_list):
    num_list_ = []
    l_ = [str(l-1) for l in map(int, num_list)]
    num_list_.extend(list_split(l_, '0'))

    return num_list_

def list_split(num_list, s):
    if '0' not in num_list:
        return [num_list]
    else:
        new_list = []
        
        n0 = num_list[:num_list.index(s)]
        n1 = num_list[num_list.index(s)+1:]
        
        for _ in [n0, n1]:
            if _ == []:
                continue
            else:
                new_list.extend(list_split(_, '0'))

        return new_list    

h_ = list_split(h, '0') 

i = 0
while(1):
    if len(h_) == 0:
        break
    else:
        new_h = []
        for _ in h_:
            new_h.extend(watering(_))
            i += 1
        h_ = new_h
            
print(i)

Submission status: Accepted

Solution code:
N = int(input())

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

count = a[0]

for i in range(1,N):
    if a[i-1] < a[i]:
        count += a[i]-a[i-1]
print(count)

Submission status: Accepted

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

def f(H):
    if len(H) <= 1:
        return sum(H)
    v = min(H)
    i = H.index(v)
    X = [h - v for h in H]
    return f(X[:i]) + v + f(X[i+1:])

print(f(H))

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
h_list = np.array(list(map(int, input().split())))
count = 0

def count_mountain(h_list):
    pre = -1000
    count = 0
    for h in h_list:
        if pre <= 0 and h > 0:
            count += 1
        pre = h
    return count


for i in range(np.max(h_list)):
    count += count_mountain(h_list)
    h_list -= 1

print(count)

Submission status: Accepted

Solution code:
n= int(input())
h = [int(i) for i in input().split()]
import numpy as np
h = np.array(h)
zero = np.array([int(0) for i in range(len(h))])

def calc(h, zero, minus):
    for k in range(len(h)):
        if minus[k] != 0:
            if k == len(h) - 1:
                zero[k] = zero[k] + 1
                return
            for w in range(k+1, len(h)):
                if minus[w] == 0:
                    zero[k:w] = zero[k:w] + 1
                    return
                elif w == len(h) - 1:
                    zero[k::] = zero[k::] + 1
                    return

def main(h, zero):
    for i in range(1, 1000001):
        minus = h - zero
        if np.sum(minus) == 0:
            print(i-1)
            return
        calc(h ,zero, minus)
main(h, zero)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

print(c)

Submission status: Accepted

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

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

print(sum(h))

Submission status: Accepted

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

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

print(s)

Submission status: Accepted

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

while(sum(h) != 0):
  l = -1
  r = -1
  min_h = 0
  #print("")
  #print(h)
  #print("")
  for i in range(h_len):
    if h[i] > 0:
      l = i
      break
  if l == -1:
    #print("l -1 break")
    break
  #print("l : "+str(l))
  
  for j in range(l,h_len):
    if h[j] > 0:
      r = j+1
      #print("for j break")
    else:
      break
  if r == -1:
    #print("r -1 break")
    break
  #print("r : "+str(r))
  
  min_h = min(h[l:r])
  h[l:r] = [h[k] - min_h for k in range(l,r)]
  count += min_h

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
while True:
    l,r = 0,0

    for i in range(len(h)):
        if h[i] != 0:
            l = i
            break

    if h.count(0) == len(h):
        break

    for i in range(l+1,len(h)):
        if h[i] == 0:
            r = i
            break
        if i + 1 == len(h):
            r = i+1

    if l == len(h) - 1:
        ans += h[-1]
        break



    for i in range(l,r):
        h[i] -= 1

    if l == r:
        h[l] -= 1

    ans += 1


print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0

while True:
    if not any(h):
        break
    flag = False
    for i, e in enumerate(h):
        if e != 0:
            flag = True
            h[i] -= 1
            if i == len(h) - 1:
                ans += 1
        else:
            if flag == True:
                ans += 1
                flag = False
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
 
sys.setrecursionlimit(10**5) 
 
def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n = ni()
h = list(li())
h.append(0)

# 非ゼロの左端を選んでいるか
haveleft = False
ans = 0

while any([hk > 0 for hk in h]):
    for k in range(len(h)):
        if not haveleft:
            if h[k] == 0:
                continue
            else:
                haveleft = True
                h[k] -= 1
                
        else:
            if h[k] == 0:
                ans += 1
                haveleft = False
            else:
                h[k] -= 1
            
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
import numpy as np
N = int(input())
L = np.array(list(map(int, input().split())))

cnt = 0
while np.any(L > 0):
    for i in range(N):
        if L[i] > 0:
            start = i
            end = i + 1
            for j in range(start+1, N):
                end = j
                if not L[j] > 0:
                    break
                end += 1
            break
        
    
    cnt += L[start:end].min()
    L[start:end] -= L[start:end].min()

print(cnt)

Submission status: Accepted

Solution code:
from  numpy  import *

N=int(input())
a=list(map(int,input().split(" ")))
s=0
while True :
  curr=""
  pos=0
  for index,item in enumerate(a):  
    if item==0:
      curr+='-'
    else:
      pos+=1
      curr+=str(item)
      a[index]=item-1
  if pos==0:
    break
  s+=count_nonzero([len(x) for x in curr.split("-")])
  
print(s)

Submission status: Accepted

Solution code:
from functools import reduce

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

prev = 0
count = 0

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

print(count)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split())) + [0]
counter = 0
while (max(li) > 0):
  for r in range(len(li)):
    if li[r] > 0:
      break
  
  for l in range(r, len(li)):
    if li[l] == 0:
      break

  li = [li[i]-1 if i >= r and i < l else li[i] for i in range(len(li))]

  counter += 1

print(counter)

Submission status: Accepted

Solution code:
def run(N, h):
    start_flg = False
    ret = 0
    for i in range(max(h)):
        start_flg = False
        for j in range(N):
            if h[j] > 0:
                h[j] -= 1
                if not start_flg:
                    start_flg = True
                    ret += 1
            else:
                start_flg = False
    return ret


def main():
    N = int(input())
    h = list(map(int, input().split()))
    print(run(N, h))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(N):
  dec = A[i]
  ans += dec
  for j in range(i+1,N):
    if A[j] == 0:
      break
    dec = min(dec, A[j])
    A[j] -= dec
  
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
answer = 0
while sum(h):
    min = 1000
    sta = 0
    for i, hi in enumerate(h):
        if hi != 0:
            if hi < min:
                min = hi
            if i == N-1:
                if sta <= i:
                    for j in range(sta, i+1):
                        h[j] -= min
                    answer += min
        else:
            if sta < i:
                for j in range(sta, i):
                    h[j] -= min
                answer += min
                min = 1000
            sta = i+1
print(answer)

Submission status: Accepted

Solution code:
def solve(h):
    n = len(h)
    res = 0
    for y in range(1, max(h)+1):
        x = 0
        while x < n:
            if (h[x] >= y):
                res += 1
                while (x < n) and (h[x] >= y):
                    x += 1
            x += 1
    return res

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

Submission status: Accepted

Solution code:
import numpy as np

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

count = 0
def recur(array):
    global count
    if len(array) == 0 :
        pass
    else:
        count += min(array)
        array = array - min(array)
        for i in range(len(array)):
            if array[i] == 0:
                array_1 = array[:i]
                array_2 = array[i+1:]
                break
        recur(array_1)
        recur(array_2)
    return count

print(recur(array))

Submission status: Accepted

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


def solver(array):
    cnt = 0
    if array == [0] or not array:
        return 0
    while True:
        aarray = []

        for num in array:
            if num != 0:
                aarray.append(num)
            else:
                cnt += solver(aarray)
                aarray = []
        else:
            if aarray:
                cnt += 1
                cnt += solver([max(0, x-1) for x in aarray])
            break

    return cnt

print(solver(h_array))

Submission status: Accepted

Solution code:
n = int(input())
h_array = [0]+[int(x) for x in input().split()]
hh_array = []
res = 0
tmp = 0
for i, j in enumerate(h_array[:-1]):
    if h_array[i] < h_array[i + 1]:
        hh_array.append(h_array[i+1] - h_array[i])
    else:
        hh_array.append(0)

print(sum(hh_array))

Submission status: Accepted

Solution code:
ans = 0


def dfs(h):
    global ans

    if len(h) == 0:
        return None

    mi = min(h)
    ans += mi
    h = list(map(lambda i: i - mi, h))

    while True:
        r = h.index(0)
        dfs(h[:r])
        h = h[r+1:]
        if 0 not in h:
            dfs(h)
            break


if __name__ == "__main__":
    N = int(input())
    H = [int(i) for i in input().split()]
    dfs(H)
    print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby

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

l = H
count = 0
for i in range(max(H), 0, -1):
    count += sum(key for key, _ in groupby(l, key=lambda x: x==i))
    l = [l_i-1 if l_i == i else l_i for l_i in l]
    # print(l)

print(count)

Submission status: Accepted

Solution code:
input()
def gg(a):
	if len(a)==0:
		return 0
	m=0
	s = min(a)
	for i in range(len(a)):
		a[i] -= s
		if a[i] == 0:
			m=i
	return s + gg(a[:m]) + gg(a[m+1:])
print(gg(list(map(int,input().split()))))

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10000000)


n = input()
h2 = list(map(int, input().split()))
j2 = -2
count2 = 0


def maax(count, j, h):
    if max(h) == 0:
        print(count)
        return
    k = max(h)
    
    for i in range(len(h)):
        if h[i] == k:
            if j+1 != i:
                count = count+1
    
            j = i
            h[i] = k-1
            maax(count, j, h)
    

maax(count2, j2, h2)

Submission status: Accepted

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

count = li[0]

for k in range(len(li)- 1):
    if li[k] < li[k + 1]:
        count += li[k + 1] - li[k]
print(count)

Submission status: Accepted

Solution code:
N=int(input())
b=input()
arr=[int(n) for n in b.split()]
ans=0
for i in range(N):
  while(arr[i]):
    ans+=1
    for j in range(i,N):
      if not arr[j]:
        break
      arr[j]-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = [int(s) for s in input().split()]
ans = 0
for i in range(N):
    while h[i]:
        ans += 1
        j = i
        for j in range(i, N):
            if not h[j]:
                break
            h[j] -= 1
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

def water_and_split(h_list):
    if h_list.shape[0] <= 2:
        counter = max(h_list)
    else:
        if np.argmin(h_list) == 0:
            counter = min(h_list)
            counter += water_and_split(h_list[1:]-min(h_list))
        elif np.argmin(h_list) == h_list.shape[0]-1:
            counter = min(h_list)
            counter += water_and_split(h_list[:-1]-min(h_list))
        else:
            counter = min(h_list)
            min_idx = np.argmin(h_list)
            counter += water_and_split(h_list[:min_idx]-min(h_list)) + water_and_split(h_list[min_idx+1:]-min(h_list))
    return counter

count = water_and_split(h_list)
print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 20 11:25:20 2019
ABC116C
@author: maezawa
"""

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

h.append(0)
cnt = 0
finished = False
while True:
    #print(h, cnt)  
    i = 0
    while True:
        if not h:
            break
        elif h[i] == 0:
            h.pop(0)
        else:
            break
    if not h:
        break
    l = 0
    r = 0
    minh = 101
    while True:
        if h[r] == 0 and r != l and minh != 0:
            for i in range(l,r):
                h[i] -= minh
            cnt += minh
            #print(h, cnt, l, r)
            l = r+1
            if l > len(h):
                break
        elif h[r] < minh:
            minh = h[r]
        r += 1
        if r >= len(h):
            break
    if not h:
        break

print(cnt)

Submission status: Accepted

Solution code:
def find_zeros(a):
    res = []
    for i in range(len(a)):
        if a[i] == 0:
            res.append(i)
    return res


def solve():
    N = input()
    h = [int(n) for n in input().split()]

    cnt = 0
    while sum(h) != 0:
        idx_zeros = find_zeros(h)
        idx_zeros = [-1] + idx_zeros + [len(h)]
        for l, r in zip(idx_zeros[:-1], idx_zeros[1:]):
            for i in range(l+1,r):
                h[i] -= 1
            if l+1<r:
                cnt += 1
    return cnt 


def main():
    ans = solve()
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, = [int(x) for x in input().split()]
h = [int(x) for x in input().split()]
r = h[0]
for i in range(1, N):
  r += max(0, h[i]-h[i-1])
print(r)

Submission status: Accepted

Solution code:
def f(l):
    sta = True
    for i,j in enumerate(l):
        if sta:
            if j!=0:
                sta=False
                l[i]-=1
        else:
            if j==0:
                break
            else:
                l[i]-=1
    return l



N =int(input())
h =[int(i) for i in input().split()]
m = 0
while h != [0]*N:
    h =f(h)
    m += 1
print(m)

Submission status: Accepted

Solution code:
input();h=list(map(int,input().split()));print(sum(max(0,h[i+1]-h[i])for i in range(len(h)-1))+h[0])

Submission status: Accepted

Solution code:
import sys
import numpy as np
import collections

def parse(inp):
    N=int(next(inp))
    H=np.array(next(inp).split(),dtype=int)
    return H
  
def split(H):
    Splited=np.array([],dtype=int)
    SplitPoint=[]
    for i in range(len(H)):
        if(H[i]==0):
            SplitPoint.append(i)
    
    if(SplitPoint==[]):
        Splited=np.array([H])
        return Splited
            
    Splited=np.split(H,SplitPoint)
    
            
    
    for i in range(len(Splited)):
        if(Splited[i].size==0):
             continue
        elif(Splited[i][0]==0):
            Splited[i]=np.delete(Splited[i],[0])
    
    return Splited
                
def main(H):
    H=collections.deque(H)
#    print(H)

    answer=0
    
    while H:
        Hnow=H.popleft()
        if(Hnow.size==0):
            continue
#        print("now: ",Hnow)
        Hnowmin=Hnow.min()
        answer+=Hnowmin
        for i in range(len(Hnow)):
            Hnow[i]=Hnow[i]-Hnowmin
#        print("decreased: ",Hnow)
        for i in range(len(split(Hnow))):
            if(split(Hnow)[i].size!=0):
#                print("append: ",split(Hnow)[i])
                H.append(split(Hnow)[i])
#        print("H: ",H)
    return answer

print(main(split(parse(sys.stdin))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=list(input().split())
a=[int(S) for S in a]
mini=min(a)
count=0
#print(a)
for i in range(0,N-1):
    if a[i+1]>a[i]:
        count+=a[i+1]-a[i]
        #print(a[i+1],a[i])
#print(count)
count+=a[0]
print(count)

Submission status: Accepted

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

count = 0
for _ in range(100):
    flag = 1
    for i in range(n):
        if H[i] > 0:
            H[i] -= 1
            if flag:
                count += 1
                flag = 0
        else:
            flag = 1
            
print(count)

Submission status: Accepted

Solution code:
n = int(input())
h_list = [int(_) for _ in input().split()]
ans = 0
while sum(h_list) != 0:
    # 0で区切られた、1以上の数のかたまりを数える
    # 要素が全て1以上ならansに+1して次のループ
    if h_list.count(0) == 0:
        ans += 1
    else:
        for i in range(1, n):
            # 1以上から0に下がった時にかたまりをカウント
            if h_list[i] == 0 and h_list[i - 1] != 0:
                ans += 1
            elif i == n - 1 and h_list[i] != 0:
                ans += 1
    # 1以上の箇所を1段ずつ落とす
    for i in range(n):
        if h_list[i] != 0:
            h_list[i] -= 1

print(ans)

Submission status: Accepted

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

# AtCoder Beginner Contest 116：C - Grand Garden
import numpy as np

def op(hlist):
    if len(hlist) == 0:
        return 0
    elif len(hlist) == 1:
        return hlist[0]
    min_h = min(hlist)
    hlist -= min_h
    s = min_h
    bi = 0
    for i in range(len(hlist)):
        if hlist[i] == 0:
            s += op(hlist[bi:i])
            bi = i + 1
    s += op(hlist[bi:])
    return s


def main():
    N = int(raw_input().strip())
    h = np.array([int(i) for i in raw_input().strip().split(' ')])
    print op(h)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*h=map(int,open(0).read().split());print(sum(max(0,b-a)for a,b in zip([0]+h[:-1],h)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def get_nonzero_range(A):
    start, end = None, None
    N = len(A)
    for i in range(N):
        if A[i] != 0:
            start = i
            for j in range(i+1, N):
                if A[j] == 0:
                    end = j
                    return start, end
    else:
        return start, end

N = int(input())
H = list(map(int, input().split()))
H.append(0)
num = 0
while True:
    start, end = get_nonzero_range(H)
    if start == None:
        break
    else:
        for i in range(start, end):
            H[i] -= 1
        num += 1
print(num)

Submission status: Accepted

Solution code:
def water(current_flower):
    start_index = 0
    end_index = N
    catch_flag = False
    for n in range(N):
        if(catch_flag == False and current_flower[n] != 0):
            catch_flag = True
            start_index = n
        if(catch_flag == True and current_flower[n] == 0):
            end_index = n
            break
    for n in range(start_index, end_index):
        current_flower[n] = current_flower[n] - 1
    return current_flower

N = int(input().strip())
h_list = list(map(int, input().strip().split()))

current_flower = list(h_list)
water_count = 0
while(sum(current_flower) != 0):
    current_flower = water(current_flower)
    water_count += 1
print(water_count)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
h = np.array([int(x) for x in input().split()])

def count_num(z):
    prev = 0
    c = 0
    for i in range(len(z)):
        if prev <= 0 and z[i] > 0:
            c += 1
        prev = z[i]
    return c

count = 0
for i in range(100):
    count += count_num(h)
    h = h - 1
    if np.sum(h > 0) == 0:
        break

print(count)

Submission status: Accepted

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