Problem:


Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq  k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N  \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221



Sample input:
4
1 2 2 1


Sample output:
2

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
gap = [0]
mt = []
va = []
i = 0
while i+1 <= len(h)-1:
    if h[i+1]-h[i] != 0:
        gap.append(h[i+1]-h[i])
        i += 1
    else:
        h.pop(i+1)
for i in range(len(h)-1):    
    if gap[i] >= 0: 
        if gap[i+1] < 0:
            mt.append(h[i])
    else:
        if gap[i+1] > 0:
            va.append(h[i])
if gap[-1] > 0 :
    mt.append(h[-1])
if mt == []:
    mt.append(h[0])
ans = 0
for x in mt:
    ans += x
for x in va:
    ans -= x
print(ans)

Submission status: Accepted

Solution code:

import math
N= int(input())
traget = list(map(int,input().split(" ")))
process = [0] * N 
ans = 0
while True:
    end_flag = True
    target_start = True

    for i in range(N):
        if traget[i] != process[i]:
            process[i] += 1
            end_flag = False
            if target_start:
                ans += 1
                target_start = False
        else:    #targetに到達しているとき
            target_start = True


    # print ("process:",process)
    # print("traget:",traget)
    # print("--"*30)
    if end_flag:
        break
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from math import *
from itertools import *
from collections import *
from functools import *
try:
    from math import gcd
except Exception:
    from fractions import gcd


def solve(N: int, h: "List[int]"):
    ret = 0
    while True:
        b = 0
        while b < len(h) and  h[b] == 0:
            b += 1

        e = b
        while e < len(h) and 0 < h[e]:
            e += 1

        if b == e:
            break

        ret += 1
        for i in range(b, e):
            h[i] -= 1

    return ret


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]"
    result = solve(N, h)
    print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
a = [h]
while len(a) != 0:
    b = []
    for aa in a:
        v = min(aa)
        ans += v

        tmp = []
        for x in aa:
            xx = x - v
            if xx == 0:
                if len(tmp) > 0:
                    b.append(tmp)
                    tmp = []
            else:
                tmp.append(xx)
        if len(tmp) > 0:
            b.append(tmp)
    a = b
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
from copy import deepcopy

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

H = np.array(H)

## 高い花のところから水やりをしていく
## 水やりする場所のindexを取得して、連続範囲数だけ水やりをする
## 水やりした場所は"要水やり数"を1減らして、再度同様に続ける

## arrを受け取り最大の"要水やり数"を持つindexを取得
## そのindexの連続範囲数とidxを返す
def water_area(arr):
  max_idx = np.where(arr == max(arr))
  shift_idx = np.roll(max_idx, 1)
  n_area = len(np.where(max_idx-shift_idx != 1)[0])
  return n_area, max_idx


cnt = 0
arr = deepcopy(H)
while True:
  if np.all(arr == 0):
    break
  n_area, max_idx = water_area(arr)
  cnt += n_area
  arr[max_idx] -= 1
  
print(cnt)
  
  
## max(arr)は一度だけ計算すれば十分（毎度1を引くだけなので）

Submission status: Accepted

Solution code:
n=int(input())
h_list=list(map(int,input().split()))
count=0
end=[]
for i in range(n):
    end.append(0)
while not h_list==end:
    count_0=1
    for i in range(n):
        if h_list[i]==0:
            count_0=1
        else:
            h_list[i]=h_list[i]-1
            if count_0==1:
                count=count+1
            else:
                pass
            count_0=0
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
ans = 0
height = 0
for i in range(len(a)):
  if a[i]>height:
    ans += a[i]-height
    height = a[i]
  else:
    height = a[i]
    
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
cnt=0
a=A[0]
b=0

for i in range(n-1):
  if A[i]<=A[i+1]:
    a=A[i+1]
  else:
    cnt+=a-b
    a=A[i+1]
    b=A[i+1]
if a>b:
  cnt+=a-b
print(cnt)

Submission status: Accepted

Solution code:
N = int(input().strip())
H = list(map(int, input().strip().split(' ')))+[0]
count = 0
for _ in range(max(H)):
  l = 0
  m = max(H)
  for r in range(1,len(H)):
    if H[r] != m:
      if H[l] == m:
        # 水抜き
        H[l:r] = [h-1 for h in H[l:r]]
        count += 1
      l = r
    else:
      if H[l] != m:
        l = r
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
hk = 0
for i in range(n):
    if hk == h[i]:
        continue
    elif hk < h[i]:
        while hk < h[i]:
            hk += 1
            ans += 1
    elif hk > h[i]:
        while hk > h[i]:
            hk -= 1
print(ans)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
sys.setrecursionlimit(10000)

n = int(stdin.readline().rstrip())
flowers = [int(x) for x in stdin.readline().rstrip().split()]


def watering(s, count):
    index_fwd = 10 ** 9
    index_back = 0
    for k in range(len(s)):
        if s[k] > 0:
            index_fwd = k
            break
    if index_fwd == 10 ** 9:
        return count
    for l in range(index_fwd, len(s)):
        if s[l] == 0:
            break
        index_back = l
    for m in range(index_fwd, index_back + 1):
        s[m] -= 1
    count += 1
    return watering(s, count)


print(watering(flowers, 0))

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
ans = 0
now = 1
while now <= max(h):
    kukan = 0
    flag = False # 区間を数え始めていいかどうか
    for i, hi in enumerate(h):
        if hi < now and flag:
            kukan += 1
            flag = False
        elif i == len(h)-1 and hi >= now:
            kukan+=1
        elif hi >= now and flag == False:
            flag = True
    now +=1
    ans += kukan
print(ans)

Submission status: Accepted

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

ans=0
for y in range(max(h))[::-1]:
    rzk=0
    for x in range(n):
        if y+1==h[x]:
            h[x]-=1
            if not rzk:
                ans+=1
                rzk=1
        else:
            rzk=0
            
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


def calc(arr):
    count = min(arr)
    l = 0
    new_arr = [arr[i] - count for i in range(len(arr))]
    for i, v in enumerate(new_arr):
        if v == 0:
            if l < i:
                count += calc(new_arr[l:i])
            l = i + 1
    if l < len(new_arr):
        count += calc(new_arr[l:len(new_arr)])

    return count


ans = calc(h)
print(ans)

Submission status: Accepted

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


def calc(arr, count):
    m = min(arr)
    count += m
    l = 0
    new_arr = [arr[i] - m for i in range(len(arr))]
    for i, v in enumerate(new_arr):
        if v == 0:
            if l < i:
                count = calc(new_arr[l:i], count)
            l = i + 1
    if l < len(new_arr):
        count = calc(new_arr[l:len(new_arr)], count)

    return count


ans = calc(h, 0)
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

H.append(0)
STATUS_UP = 0
STATUS_DOWN = 1

status = STATUS_UP
last_top = 0
last_valley = 0
ans = 0

for i in range(1, N+1):
    if status == STATUS_UP:
        if H[i-1] > H[i]:
            ans += H[i-1] - last_valley
            status = STATUS_DOWN
    else:
        if H[i-1] < H[i]:
            last_valley = H[i-1]
            status = STATUS_UP
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

diff = np.diff(h).tolist()
max_list = []
min_list = []

sign = 1
for i, d in enumerate(diff):
    if d > 0 and sign < 0:
        sign = d
        min_list.append(h[i])

    if d < 0 and sign > 0:
        sign = d
        max_list.append(h[i])


def add(val, max_list, min_list):
    if len(max_list) > 1:
        min_val = min(min_list)
        split_index = min_list.index(min_val)+1
        return min_val-val + \
            add(min_val, max_list[:split_index], min_list[:split_index-1]) + \
            add(min_val, max_list[split_index:], min_list[split_index:])
    else:
        return max_list[0]-val


if max(h) == 0:
    print(0)
else:
    print(add(0, max_list, min_list))

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+1] > h[i]:
        ans += h[i+1] - h[i]
        
print(ans)

Submission status: Accepted

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

max_index = H.index(max(H))
ans = 0
num_before = 0
for i in range(max_index,N):
  if H[i] > num_before:
    ans += H[i] - num_before
  num_before = H[i]
num_before = max(H)
for i in range(-max_index,1):
  if H[-i] > num_before:
    ans += H[-i] - num_before
  num_before = H[-i]
print(ans)

Submission status: Accepted

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

count = 0

for i in range(N):
    while True:
        if H[i] < 1:
            break
        indx_tmp = i
        for j in range(i, N):
            if H[j] == 0:
                break
            indx_tmp = j
        H[i:indx_tmp+1] -= 1
        count += 1
print(count)

Submission status: Accepted

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

# print(L)

flag = 1
cnt = 0

while flag >= 1 : 
    
    left_flag = 1
    for i in range(N) : 
        if(L[i] != 0) : 
            left = i
            left_flag = 0
            break
    if(left_flag == 1) : 
        break # これでwhileから脱出
    
    right_flag = 1
    for j in range(left, N, 1) : 
        if(L[j] == 0) : 
            right = j
            right_flag = 0
            break
    if (right_flag == 1) : 
        right = N

    # print(left, right)

    for k in range(left, right, 1) : 
        L[k] -= 1
        
    # print(L)
    
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
# coding: utf-8
N = int(input())
h = list(map(int,input().split()))
ans = 0
i = 0
while sum(h) != 0:
    if h[i] == 0:
        i+=1
        continue
    for _ in range(i,N):
        if h[_] != 0:
            h[_] = h[_] - 1
        else:
            break
    ans+=1
print(ans)

Submission status: Accepted

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

oldh = 0
ans = 0
for i in h:
    if i - oldh >= 0:
        ans += i - oldh
    oldh = i

print(ans)

Submission status: Accepted

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

def chr(l,b):
    global ans
    if len(l) != 0:
        a = min(l)
        ans += a-b
        c = l.index(a)
        chr(l[:c],a)
        chr(l[c+1:],a)
chr(f,0)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

def f(s=0, e=len(H), depth=0):
    if e == s:
        return 0
    if e - s == 1:
        return H[s] - depth
    for i in range(s, e):
        if H[i] <= depth:
            return f(s, i, depth) + f(i + 1, e, depth)
    return 1 + f(s, e, depth + 1)

print(f())

Submission status: Accepted

Solution code:
N = int(input())

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

m = [[0 for _ in range(max(H) + 1)] for _ in range(N)]

for i in range(N):
    for j in range(H[i]):
        m[i][j] = 1

c = 0
for j in range(max(H)):
    prev = 0
    for i in range(N):
        if prev == 0 and m[i][j] == 1:
            c += 1
        prev = m[i][j]

print(c)

Submission status: Accepted

Solution code:
n = int(input())
hs = list(map(int, input().split()))
maxh = max(hs)
hs.append(0)
t = 0
for i in range(maxh):
    c = 0
    for j, h in enumerate(hs):
        if c == 0 and i + 1 <= h:
            c = 1
            continue
        if c == 1 and i + 1 > h:
            t += c
            c = 0
print(t)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
hs = map(int, input().split())
ans = 0
now = 0
for h in hs:
    if h > now:
        ans += (h-now)
        now = h
    now = min(h, now)
print(ans)

Submission status: Accepted

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


hmax = -1
for h in hlist:
    hmax = max(hmax, h)


count = 0

while hmax > 0:

    prev_max = False

    for h in hlist:
        if h >= hmax and not prev_max:
            count += 1
            prev_max = True
        if h < hmax:
            prev_max = False

    hmax -= 1


print(count)

Submission status: Accepted

Solution code:
import numpy as np

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

#%%
def splitter(arr):
    '''
    Split arrays into sections that contain only non-zero values.\n
    Returns a list of tuples which are (start, open-end) of sections
    '''

    L = len(arr)
    sections = []
    l,r = 0,0

    while r < L:
        if arr[r] == 0:
            if l == r:
                l += 1
                r += 1
            else:
                sections.append((l,r))
                l = r

        else:
            if r == L-1:
                r += 1
                sections.append((l,r))

            else:
                r += 1

    return sections

#%%
def watering(arr):
    '''
    arr: np.array, that can contain both zero and nonzero values
    '''

    L = len(arr)

    if L == 1:
        return arr[0]

    # split into sections that contains only nonzero
    sections = splitter(arr)

    # already only 0s
    if not sections:
        return 0

    # for each section, decrement elements by 1 then recursive call this function
    operations = 0
    for l,r in sections:
        operations +=  1 + watering(arr[l:r] - 1)

    return operations


ans = watering(h)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1,N+1):
  num += max(0,H[i]-H[i-1])
  
print(num)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
ans=0
pre=0
for i in a:
    if i>pre:
        ans+=i-pre
    pre=i
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def countMin(*vals):
  l = len(vals)
  li = []
  for i in range(l):
    li.append(vals[i])
  if l == 1:
    return li[0]
  else:
    m = min(li)
    li2 = list(map(lambda x:x - m, li))
    zlist = []
    for j in range(l):
      if li2[j] == 0:
        zlist.append(j)
    if zlist == [i for i in range(l)]:
      return m
    else:
      prev = 0
      re = 0
      re += m
      for k in range(len(zlist)):
        if prev == 0 and li2[0] != 0:
          re += countMin(*li2[:zlist[k]])
          prev = zlist[k]
        elif zlist[k] - prev <= 1:
          prev = zlist[k]
        else:
          re += countMin(*li2[prev + 1:zlist[k]])
          prev = zlist[k]
      if prev < l - 1:
        re += countMin(*li2[prev + 1:])
      return re

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

Submission status: Accepted

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

count = 0
max_h = max(hs)

for h in range(1, max_h+1):
    in_region = False

    #    print("h", h)
    for i in range(N):
        # print(i)
        if h <= hs[i]:
            in_region = True
        else:
            if in_region:
                in_region = False
                count += 1

    if in_region:
        count += 1

print(count)

Submission status: Accepted

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

a = [0]*N
answer = 0
while(True):
    area_flag = False
    area = 0
    comp_cnt = 0
    # print(a)
    for i in range(N):
        if a[i]>=h[i] and h[i]==0:
            comp_cnt+=1
            if area_flag:
                area += 1
            area_flag = False
            continue
        elif a[i]>=h[i]:
            comp_cnt+=1
            if area_flag:
                area += 1
            area_flag = False
            continue
        area_flag = True
        a[i]+= 1
    if area_flag and comp_cnt!=0:
        area += 1
    answer += area
    # print(area)
    if comp_cnt == N: 
        answer += min(h)
        break


print(answer)

Submission status: Accepted

Solution code:
def solve(h):
    if len(h) == 0:
        return 0
    if len(h) == 1:
        return h[0]
    if len(h) == 2:
        return max(h)

    mn = min(h)
    x = -1
    for i in range(len(h)):
        if h[i] == mn:
            x = i
        h[i] -= mn
    return mn + solve(h[:x]) + solve(h[x + 1:])

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

print(solve(h))

Submission status: Accepted

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

Submission status: Accepted

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

result = 0
height = 0

while True:
    zero_cnt = 0
    area = 0
    area_flg = True
    for i in range(n):
        if a[i] > height:
            if area_flg:
                area += 1
                area_flg = False
        else:
            area_flg = True
            zero_cnt += 1
    if zero_cnt == n:
        print(result)
        break
    result += area
    height += 1

Submission status: Accepted

Solution code:
n=int(input())
arr=list(map(int,input().split()))
def f(arr):
  if len(arr)==0:
    return 0
  if 0 in arr:
    idx=arr.index(0)
    return f(arr[:idx])+f(arr[idx+1:])
  else:
    return 1+f([i-1 for i in arr])
print(f(arr))

Submission status: Accepted

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

  if 0 in h:
    i = h.index(0)
    return f(h[0:i]) + f(h[i+1:])
  else:
    return 1 + f([ x-1 for x in h ])

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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:
# coding: utf-8
n = int(input())
h = list(map(int, input().split()))
counter = 0
while bool(h):
    for i in range(n):
        if not bool(h[0]): h = h[1:]; break
        h[i] -= 1
        try: 
            if h[i + 1] == 0: counter += 1; break
        except: counter += 1; break
print(counter)

Submission status: Accepted

Solution code:
# coding: utf-8

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

counter = 0

while bool(h):
    for i in range(n):
        if not bool(h[0]): h = h[1:]; break
        h[i] -= 1
        try:
            if h[i + 1] == 0:
                counter += 1
                break
        except: counter += 1; break

print(counter)

Submission status: Accepted

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

new_h = [0] + h + [0]
# print(zero_h)
new_N = N + 2

zero_h = [0 for _ in range(new_N)]
counter = 0
flag = False
while_counter = 0
while(new_h != zero_h):
    # while_counter += 1
    # if while_counter > 15:
    #     break
    # print(new_h)
    for i in range(new_N):
        if flag == False:
            if new_h[i] == 0:
                start = i + 1
                flag = True
        elif flag == True:
            if new_h[i] == 0:
                end = i
                if new_h[start:end] == []:
                    start = i + 1
                    flag = True
                    pass
                else:
                    flag = False
                    new_h[start:end] = [new_h[i]-1 for i in range(start,end)]
                    counter += 1

print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def water(h):
    x = min(h)
    for i in range(0,len(h)):
        h[i] -= x
        if h[i] == 0:
            p = i
    
    if h == [0]*len(h):
        return x
    elif p == len(h) - 1:
        return x + water(h[:len(h)-1])
    elif p == 0:
        return x + water(h[1:])
    else:
        return x + water(h[0:p]) + water(h[p+1:])

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

print(water(h))

Submission status: Accepted

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

for i in h:
    if max < i:
        pass

    else:
        ans += max - i
        
    max = i

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())
hs=[int(i) for i in input().split()]
res=hs[0]
for i in range(1,len(hs)):
  if hs[i-1]<=hs[i]:
    res+=(hs[i]-hs[i-1])
  else:
    continue
print(res)

Submission status: Accepted

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

numbers = 0
theend = False

while theend == False:
    if b.count(0) == a:
        theend = True
        break

    indexnotzero = 0

    for i in range(len(b)):
        if b[i] == 0:
            continue
        else:
            indexnotzero = i
            break

    if indexnotzero == len(b) - 1:
        numbers += b[len(b) - 1]
        break

    try:
        length = b.index(0, indexnotzero)
    except ValueError:
        length = len(b)
    minimum = min(b[indexnotzero:length])

    numbers += minimum

    b[indexnotzero:length] = [i - minimum for i in b[indexnotzero:length]]

print(numbers)

Submission status: Accepted

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

numbers = min(b)

b = [i - min(b) for i in b]

theend = False

while theend == False:
  
    if b.count(0) == a:
        theend = True
        break

    indexnotzero = 0

    for i in range(len(b)):
        if b[i] == 0:
            continue
        else:
            indexnotzero = i
            break

    if indexnotzero == len(b) - 1:
        numbers += b[len(b) - 1]
        break

    try:
        length = b.index(0, indexnotzero)
    except ValueError:
        length = len(b)
    minimum = min(b[indexnotzero:length])

    numbers += minimum

    b[indexnotzero:length] = [i - minimum for i in b[indexnotzero:length]]

print(numbers)

Submission status: Accepted

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

for i in h:
    if i < min:
        sum_mizuyari += min - i
        min = i
    elif i > min:
        min = i

print(sum_mizuyari + min)

Submission status: Accepted

Solution code:
n = int(input())
a = [ int(i) for i in input().split() ]
moves = 0
while max(a) != 0:
    i = -1
    j = -1
    for y in range(n):
        if a[y] > 0 and i == -1:
            i = y
        elif a[y] == 0 and i != -1:
            j = y
            break
    if j == -1:
        j = n
    if i == -1:
        break
    mn = 1e9
    for x in range(i,j):
        mn = min(a[x],mn)
    moves += mn
    for x in range(i,j):
        a[x] -= mn
print(moves)

Submission status: Accepted

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

prevc = -1
c = 0
while prevc != c:
  prevc = c
  is_c = False
  newhs = []
  for h in hlist:
    if h == 0:
      is_c = False
      newhs.append(0)
    else:
      if is_c:
        newhs.append(h-1)
      else:
        newhs.append(h-1)
        is_c = True
        c += 1
  hlist = newhs

print(prevc)

Submission status: Accepted

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

print(s+h[-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

def f(k):
  global N,h,update
  if k==N:
    return -1
  if h[k]==0:
    return k
  else:
    h[k]-=1
    update=True
    f(k+1)


update=False
N=int(input())
h=list(map(int,input().split(" ")))
r=0
s=0

try:
  while True:
    if h[s]!=0:
      break
    s+=1
except Exception:
  print(0)
  sys.exit(0)

while True:
  f(s)
  if update:
    r+=1
    while s!=N-1 and h[s]==0:
      s+=1
    update=False
  else:
    break

print(r)

Submission status: Accepted

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

h_unique = list(set(h))
#print(h_unique)

check = [1] + [0]*(n) + [1]
h = [-1] + h + [-1]
ans = 0
split = 0
for i in range(max(h)):
  if i in h_unique:
    for j in range(len(h)):
      if h[j] == i:
        check[j] = 1
        if check[j-1] == 0 and check[j+1] == 0:
          split += 1
        elif check[j-1] == 1 and check[j+1] == 1:
          split -= 1
  ans += split+1
  #print(split)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
cnt=0
mini=min(h)
for i in range(len(h)):
    h[i]-=mini
cnt+=mini
for i in range(n):
    while h[i]>0:
        cnt+=1
        for j in range(0,n-i):
            if h[i+j]==0:
                break
            else:
                h[i+j]-=1
print(cnt)

Submission status: Accepted

Solution code:
import sys

def ii2ss(n):
    return [sys.stdin.readline() for _ in range(n)]

def sp2nn(sp, sep=' '):
    return [int(s) for s in sp.split(sep)]

def ss2nn(ss):
    return [int(s) for s in list(ss)]

def count(hh, n):
    c = 0
    b = False
    for h in hh:
        if h >= n:
            if b == False:
                b = True
                c += 1
        else:
            if b == True:
                b = False
    return c
    
def main(ss):
    hh = sp2nn(ss[1])
    total = 0
    n = 1
    while True:
        c = count(hh, n)
        if c == 0:
            break
        total += c
        n += 1
    print(total)

main(ii2ss(2))

Submission status: Accepted

Solution code:
N=int(input())
H=[int(i) for i in input().split()]
S=sum(H)
ans=0
while S>0:
    for i in range(N):
        if H[i]>0:
            break
    for j in range(i,N):
        if H[j]==0:
            break
    if j==N-1 and H[j]>0:
        j+=1
    for k in range(i,j):
        H[k]-=1
    ans+=1
    S-=j-i
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
pre=0
ans=0
for i in map(int,input().split()):
    if i < pre:
        ans+=pre-i
    pre=i
else:
    print(ans+i)

Submission status: Accepted

Solution code:
a= int(input())
b = input().split()
b= [int(s) for s in b]
check=[]
length=[]
head = 0
foot = 0
many=0
for i in range(0,a):
    length.append(0)
    check.append(0)
for i in range(0,a):
    if b[i]==length[i]:
        check[i]=1
    else:
        dum=0
for i in reversed(range(0,a)):
    if check[i]==0:
        head = i
    else:
        dum=0
for i in range(head,a):
    if check[i]==1:
        foot = i
        break
    else:
        foot = a
        dum = 0
while sum(check)!=a:
    for i in range(head,foot):
        length[i]+=1
    for i in range(0,a):
        if b[i]==length[i]:
            check[i]=1
        else:
            dum=0
    for i in reversed(range(0,a)):
        if check[i]==0:
            head = i
        else:
            dum=0
    for i in range(head,a):
            if check[i]==1:
                foot = i
                break
            else:
                foot = a
    many+=1
print(many)

Submission status: Accepted

Solution code:
N=int(input())
height=[int(i) for i in input().split()]
height.append(0)
count=0
for i in range(N):
  while height[i]>0:
    j=0
    while height[i+j]>0:
      height[i+j]=height[i+j]-1
      j=j+1        
    count+=1
print(count)