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 sys
from collections import Counter
from collections import deque
import math
import fractions
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n=int(input())
h=lmp()
h.append(0)
m=max(h)
ans=0
for i in range(m,0,-1):
    for k in range(n):
        if h[k]==i and h[k+1]!=i:
            ans+=1
        if h[k]==i:
            h[k]-=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
def solve(n):
  #print(n)
  if len(n) == 1:
    return n[0]
  if n == []:
    return 0
  for i in range(len(n)):
    if n[i] == 0:
      return solve(n[:i]) + solve(n[i+1:])
  tempmin = min(n)
  nextn = [n[i]- tempmin for i in range(len(n))]
  return tempmin+solve(nextn)

print(solve(h))

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(input())
    H = list(map(int, input().split()))

    res = 0
    i = 0
    while sum(H) != 0:
        if H[i] != 0:
            while i < n and H[i] != 0:
                H[i] -= 1
                i += 1
            res += 1
            i = 0
        else:
            i += 1

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    h=list(map(int,readline().split()))
    ans=0
    while True:
        flag=True
        flag2=True
        for i in range(n):
            if h[i]>0:
                h[i]-=1
                flag=False
                flag2=False
                if i==n-1:
                    ans+=1
            else:
                if flag2==False:
                    flag2=True
                    ans+=1
        if flag: break

    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

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

H=np.array(H)
ans=0
while H.max()>0:
    prezero=True
    count=0
    for i in range(N):
        if prezero:
            if H[i]>0:
                count+=1
                prezero=False
        else:
            if H[i]<=0:
                prezero=True
    ans+=count
    H-=1
print(ans)

Submission status: Accepted

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

ans = 0
active = 0

for i in range(n):
    if active < h[i]:
        ans += h[i] - active
        active += h[i] - active
    elif h[i] < active:
        active -= active - h[i]
#     print(active)
#     print(ans)

# print("-------")
print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    h = list(map(int, input().split()))
    ans = 0
    i = 0
    while i < n:
        if h[i] == 0:
            i += 1
            continue
        x = i+1
        m = h[i]
        while x < n and h[x] != 0:
            if h[x] < m:
                m = h[x]
            x += 1
        for j in range(i, x):
            h[j] -= m
        ans += m
        if h[i] == 0:
            i += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=[0]+list(map(int,input().split()))
    ans=0
    for i in range(len(l)-1):
        x=l[i+1]-l[i]
        if x>0:
            ans+=x
    print(ans)
resolve()

Submission status: Accepted

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

N = int(readline())
H = [0] + list(map(int,readline().split()))

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
hs = list(map(int, input().split()))
ans = 0
while max(hs) != 0:
    i = 0
    while i < N:
        if hs[i] == 0:
            i += 1
        else:
            ans += 1
            while i < N and hs[i] > 0:
                hs[i] -= 1
                i += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
ls = list(map(int,input().split()))
def sub(p,q):
    return p-q
i = 0
p = 1
new=[0 for _ in range(n)]
s = [1 for _ in range(n)]
while ls[ls.index(max(ls))]>0:
    for j in range(len(ls)):
        if j == n-1:
            if ls[j] > 0:
                new[j] = p
            else:
                pass
        else:
            if (ls[j] <= 0) and (ls[j+1] <= 0):
                pass
            elif (ls[j] <= 0) and (ls[j+1] > 0):
                p += 1
            elif (ls[j] > 0) and (ls[j+1] > 0):
                new[j] = p
            else:
                new[j] = p
    if (sum(new) == n) and (len(list(set(new))) == 1):
        i += 1
    else:
        i += len(list(set(new))) -1
    ls = list(map(sub,ls,s))
    new = [0]*n
    p = 1
print(i)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
l=[0]
c='plus'
for i in range(n-1):
    if c=='minus' and h[i]<=h[i+1]:
        l.append(i)
    if h[i]<=h[i+1]:c='plus'
    else:c='minus'

w=0
m=0
for i in range(len(l)-1):
    w+=max(h[l[i]:l[i+1]+1])-m
    m=h[l[i+1]]
w+=max(h[l[-1]:n])-m
print(w)

Submission status: Accepted

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

OK = [h == 0 for h in H]
T = [0] * N

ans = 0
while not all(OK):
  flg = False
  for i in range(N):
    if not OK[i]:
      T[i] += 1
      if T[i] == H[i]:
        OK[i] = True
      flg = True
    else:
      if flg:
        ans += 1
      flg = False
  
  if flg:
    ans += 1
    
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
*H, = map(int, input().split())
 
T = [0]*N
ans = 0
i = 0
while i<N:
    #積み上げるべきスタート地点を探す
    if T[i]<H[i]:
        start = i
        high  = T[i]+1
        ans = ans + 1
        i = 0
        for j in range(start, N, 1):
            if T[j]<H[j]:
                T[j] = T[j] + 1
            else:
                break
    else:
        i = i + 1

print(ans)

Submission status: Accepted

Solution code:
import itertools
 
n = int(input())
H = list(map(int, input().split()))
 
count = 0
h_max = max(H)
for i in range(1, h_max + 1): #これが水位を上げる役割をしているのか
    watering = [(1 if h >= i else 0) for h in H] #これ以上いらないなら0　いるなら1
 
    itrs = itertools.groupby(watering)
    watering_group = [k for k, v in itrs if sum(list(v)) > 0] #ｋは要素の数字そのもの，連続するｋの並び
    count += sum(watering_group)

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools

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

count = 0
h_max = max(H)
for i in range(1, h_max + 1):
    watering = [(1 if h >= i else 0) for h in H]

    itrs = itertools.groupby(watering)
    watering_group = [k for k, v in itrs if sum(list(v)) > 0]
    count += sum(watering_group)

print(count)

Submission status: Accepted

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

def water(a):
    if min(a)==max(a):
        return min(a)
    elif min(a)>0:
        m=min(a)
        for i in range(len(a)):
            a[i]-=m
        return water(a)+m
    else:
        s=[]
        l=-1
        for i in range(len(a)):
            if a[i]!=0:
                if l==-1:
                    l=i
                if i==len(a)-1:
                    r=i+1
                    s.append((l,r))
    
            else:
                if l>-1:
                    r=i
                    s.append((l,r))
                    l=-1
    
        res=0
        for k in s:
            res+=water(a[k[0]:k[1]])
        return res

print(water(h))

Submission status: Accepted

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

T = [0]*N
ans = 0
i = 0
while i<N:
    #積み上げるべきスタート地点を探す
    if T[i]<H[i]:
        start = i
        high  = T[i]+1
        ans = ans + 1
        i = 0
        for j in range(start, N, 1):
            if T[j]<H[j] and (T[j]+1==high):
                T[j] = T[j] + 1
            else:
                break
    else:
        i = i + 1

print(ans)

Submission status: Accepted

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

max=0
ans=0
for i in range(n):
    if h[i]>=max:
        max=h[i]
    else:
        ans+=max
        ans-=h[i]
        max=h[i]

if max==h[n-1]:
    ans+=max
print(ans)

Submission status: Accepted

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

ans,cnt = 0, sum(h)
while cnt > 0:
    l, r = -1, n
    for num, i in enumerate(h):
        if (l == -1) and (i > 0):
            l = num
        if (l > -1) and (i == 0):
            r = num
            ans += 1
            cnt -= r - l
            for i in range(l, r):
                h[i] -= 1
            break
print(ans)

Submission status: Accepted

Solution code:
#import cython

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

ans,cnt = 0, sum(h)
while cnt > 0:
    l, r = -1, n
    for num, i in enumerate(h):
        if (l == -1) and (i > 0):
            l = num
        if (l > -1) and (i == 0):
            r = num
            ans += 1
            cnt -= r - l
            for i in range(l, r):
                h[i] -= 1
            break
print(ans)

Submission status: Accepted

Solution code:

N = int(input())
h = list(map(int, input().split()))
cnt = 0 
while True:
  pre = False
  for i in range(N):
    if h[i] > 0:
      if pre:
        h[i] -= 1
      else:
        h[i] -= 1
        pre = True
        cnt += 1
    else:
      pre = False
  if sum(h) == 0:
    break
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
mokuhyo = list(map(int, input().split()))
#
# n = 8
# s = "4 23 75 0 23 96 50 100"
# mokuhyo = list(map(int, s.split()))

i = 0
count = 0

while(i < n):
    if mokuhyo[i] == 0:
        i += 1
        continue
    r = 999999
    for j in range(i+1, n):
        if mokuhyo[j] == 0:
            r = j
            break

    if r == 999999:
        r = n

    gensyo = min(mokuhyo[i:r])
    count += gensyo
    for k in range(i, r):
        mokuhyo[k] -= gensyo


print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
total = 0
for i in range(max(li)):
    a = 0
    b = 0
    for j in range(n):
        if li[j] != 0 and a == 0:
            b += 1
            a = 1
        if li[j] == 0:
            a = 0
        else:
            a = 1
    for j in range(n):
        li[j] -= 1
        if li[j] < 0:
            li[j] = 0
    total += b
print(total)

Submission status: Accepted

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

def decrease(lis):
    global count
    length = len(lis)
    if length == 2:
        count += max(lis)
    elif length == 1:
        count += lis[0]
    else:
        minnum = min(lis)
        minidx = lis.index(minnum)
        count += minnum
        lis = list(map(lambda x: x-minnum, lis))
        if minidx >= 1:
            decrease(lis[:minidx])
        if minidx < length - 1:
            decrease(lis[minidx+1:])

count = 0
decrease(H)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
ans = 0
while True:
  if max(h) == 0:
    break
  i = 0
  while i < N:
    if h[i] == 0:
      i+=1
      continue
    else:
      ans += 1
      while (i < N and h[i] > 0):
        h[i] -= 1
        i += 1
print(ans)

Submission status: Accepted

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

cnt = 0
stop = max(H)
while stop>0:
    stop += -1
    for i in range(1,N+1):
        if H[i]>0 and H[i-1]==0:
            cnt+=1
    for i in range(1,N+1):
        if H[i]>0:
            H[i]+=-1
print(cnt)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n = n//a
    count += 1
  if a**count == n:
    return count
  else:
    return count+1

#############
# Main Code #
#############

N = I()
H = IL()

prev = 0
ans = 0
for h in H:
  if h > prev:
    ans += h-prev
  prev = h
P(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    ans=l[0]
    # 数列の値が増えてたらその分を追加で足す必要がある
    # 減ってたら前の花を育ててる途中で水やり完了してる
    for i in range(len(l)-1):
        ans+=max(l[i+1]-l[i],0)
    print(ans)
resolve()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    l=list(map(int,input().split()))
    l=[0]+l
    ans=0
    for i in range(len(l)-1):
        ans+=max(l[i+1]-l[i],0)
    print(ans)
resolve()

Submission status: Accepted

Solution code:
# Python3 (3.4.3)
import sys
input = sys.stdin.readline

# -------------------------------------------------------------
# function
# -------------------------------------------------------------


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

ans = 0
x = [H]

while len(x) > 0:
    A = x.pop()
    minA = min(A)
    k = A.index(minA)
    for i in range(len(A)):
        A[i] -= minA
    ans += minA
    if A[0:k] != []:
        x.append(A[0:k])
    if A[k+1:] != []:
        x.append(A[k+1:])

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=[0]+list(map(int, input().split()))
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=[0]+list(map(int,input().split()))
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()))
ans=h[0]

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

print(ans)

Submission status: Accepted

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

ans = 0
tmp = 0
max_h = max(H)
for i in range(max_h,0,-1):
    H_c = H.copy()
    if H[0]==i:
        ans+=1
        H_c[0]-=1
    for j in range(1,n):
        if H[j]==i:
            if H[j-1]!=H[j]:
                ans+=1
                H_c[j]-=1
            else:
                H_c[j]-=1
    H = H_c
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
hl = list(map(int, input().split()))
hl.insert(0, 1)

cnt = 0
l = 1
r = n
while True:
    if sum(hl) == 1:
        break
    for i in range(1, n + 1):
        if hl[i] >= 1:
            l = i
            break
    for j in range(l, n + 1):
        if hl[j] == 0:
            r = j -1
            break
        if not 0 in hl[l:]:
            r = n
    if l == r:
        hl[l] -= 1
    else:
        for k in range(l, r+1):
            hl[k] -= 1
    cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import numpy as np

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

ans = 0
while h.sum() > 0:
    # 引くべき値を求める
    minus = h[np.where(h > 0)].min()

    # 引くべきindexを求める
    target = np.where(h >= minus)[0]

    # 水やりが必要なブロックが何個あるか数える
    diff = np.diff(target)
    cnt = np.where(diff > 1)[0].shape[0] + 1
    
    # ansに引くべき値×ブロック数を加える
    ans += minus * cnt

    # 引く
    h[target] -= minus

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
hlist = list(map(int, input().split()))
def count_island(height):
    res = 0
    sink = True
    for h in hlist:
        if sink and h > height:
            res += 1
            sink = False
        if h <= height:
            sink = True
    return res

ans = 0
for height in range(max(hlist)):
    ans += count_island(height)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque


def main():
    N = int(input())
    s = deque()
    H = [int(h) for h in input().split()]

    s = deque()
    h0 = 0
    count = 0
    for h in H:
        if s and s[-1] > h:
            count += (s[-1] - h)
            while s and s[-1] > h:
                s.pop()
        s.append(h)
    count += s[-1]
    print(count)


main()

Submission status: Accepted

Solution code:
n=int(input())
H=list(map(int,input().split()))
start,end,ans=0,H[0],0

for i in range(1,n):
  if end <= H[i]:
    end=H[i]
  else:
    ans += (end-start)
    start,end=H[i],H[i]
    
ans += (end-start)
print(ans)

Submission status: Accepted

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

if n == 1:
    print(s[0])
    exit()

b = 0
res = 0
for i in range(n - 1):
    if s[i] <= s[i + 1]:
        continue
    else:
       res += s[i] - b
       b = s[i + 1]

if s[n - 1] >= s[n - 2]:
    res += s[n - 1] - b
print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def main():
    N = int(input())
    H = list(map(int, input().split()))
    ans = 0
    for i in range(0,max(H)+1):
        j = 0
        while (j < N):
            #print(j)
            if H[j] > i:
                ans += 1
                while j < N and H[j] > i:
                    j += 1
                #print(j)
            j += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop
def input(): return sys.stdin.readline().strip()


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

    ans = 0
    for n in range(1, max(H) + 1):
        conti = 0
        exist = False
        for h in H:
            if h >= n and exist:
                continue
            elif h >= n:
                exist = True
            elif h < n and exist:
                conti += 1
                exist = False
        ans += conti
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans = 0
while True:
    if sum(h) <= 0:
        print(ans)
        exit()
    M = h.index(max(h))
    start = M
    end = M+1
    for i in range(M,-1,-1):
        if h[i] > 0:
            start = i
        else:
            break
    if end != n:
        for i in range(M,n):
            if h[i] > 0:
                end = i+1
            else:
                break
    for i in range(start,end):
        h[i] -= 1
    ans += 1

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import copy
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

N = getN()
H = getList()
now = H[0]
ans = 0
for i in range(1, N):
    if now <= H[i]:
        now = H[i]
    else:
        ans += H[i - 1] - H[i]
        now = H[i]
ans += H[-1]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
seq =[list(map(int,input().split()))]
ans = 0
while seq:
    L = seq.pop()
    ans += min(L)
    S = [n-min(L) for n in L]
    K = []
    for i in S:
        if i!= 0:
            K.append(i)
        else:
            if K:
                seq.append(K)
                K = []
    else:
        if K:
            seq.append(K)
else:
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
H = list(map(int,input().split()))
seq = []
K = []
ans = 0
for i in H:
    if i != 0:
        K.append(i)
    else:
        if K:
            seq.append(K)
            K = []
else:
    if K:
        seq.append(K)
while seq:
    L = seq.pop()
    ans += min(L)
    S = [n-min(L) for n in L]
    K = []
    for i in S:
        if i!= 0:
            K.append(i)
        else:
            if K:
                seq.append(K)
                K = []
    else:
        if K:
            seq.append(K)
else:
    print(ans)

Submission status: Accepted

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

ans = 0
tmp = h = H[0]

for h in H[1:]:
    if h >= tmp:
        tmp = h
    else:
        ans += tmp - h
        tmp = h
else:
    ans += h

print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
import fractions
import functools
import copy
n = int(input())
h  = list(map(int, input().split()))

ans = 0
active = 0
for i in range(n):
    if h[i] <= active:
        active = h[i]
    else:
        ans += h[i] - active;    
        active = h[i]
print(ans)

Submission status: Accepted

Solution code:
ctr = 0

def mizuyari(left, right, array):
    # left <= x <= right
    N = right - left
    if N == 0:
        global ctr
        ctr += array[left]
    elif N == 1:
        mini = max(array[left], array[right])
        ctr += mini
        if array[left] != 0:
            array[left] -= mini
        else:
            array[right] -= mini
    else:
        mini = 10000
        mini_index = -1
        for i in range(left, right+1):
            if mini > array[i]:
                mini = array[i]
                mini_index = i
        ctr += mini

        for i in range(left, right+1):
            array[i] -= mini
        
        if left != mini_index:
            mizuyari(left, mini_index-1, array)
        if mini_index != right:
            mizuyari(mini_index+1, right, array)


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

    mizuyari(0, n-1, h)

solve_C()
print(ctr)

Submission status: Accepted

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

Submission status: Accepted

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

ans,now = 0,0

for h in H:
  if h > now:
    ans += h-now
  now = h

print(ans)

Submission status: Accepted

Solution code:
# C - Grand Garden

N = int(input())
h = list(int(x) for x in input().split())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
heights = tuple(map(int, input().split(' ')))

cost = 0
for i in range(1, max(heights) + 1):
    s = ''.join(['1' if height >= i else ' ' for height in heights])
    cost += len(s.split())

print(cost)

Submission status: Accepted

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

ans = min(h)
h = list(map(lambda x:x-ans, h))

while h.count(0) != len(h):

    pos = 0
    pos2 = 0

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

    for j in range(pos, len(h)):
        if h[j] == 0:
            pos2 = j
            break
        if j +1== len(h):
            pos2 = len(h)

    ans += min(h[pos:pos2])
    h[pos:pos2] = map(lambda x: x-min(h[pos:pos2]), h[pos:pos2])

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = [int(x) for x in input().split()]
reg = 0
while max(H) != 0:
    i = 0
    while i < N:
        if H[i]==0:
            i += 1
            continue
        reg += 1
        while i < N and H[i] > 0:
            H[i] -= 1
            i += 1
print(reg)

Submission status: Accepted

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

ma = max(H)
cnt = 0

while ma:
    flg = False
    for i in range(N):
        if H[i]==ma:
            flg = True
            H[i] -= 1
        elif flg and H[i]!=ma:
            cnt += 1
            flg = False
    if flg:
        cnt += 1
    ma -= 1
print(cnt)

Submission status: Accepted

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

def dfs(a):
    if len(a) == 1:
        return a[0]
    elif len(a) == 0:
        return 0

    water = 0
    pack = []
    mins = min(a)
    water += mins
    a = a - mins
    if a.sum() == 0:
        return mins
    for i in a:
        if i == 0:
            water += dfs(np.array(pack))
            pack = []
        else:
            pack.append(i)
    water += dfs(np.array(pack))
    return water

print(dfs(h))

Submission status: Accepted

Solution code:
n = int(input())
H = list(map(int, input().split()))
min_H = min(H)

if min_H == 0:
    ans = 0
else:
    ans = min_H
    H = list(map(lambda  x: x -min_H, H))
#print(H, ans)


#for i in range(1,max(H)+1):

for j in range(n):
    #print(H, ans)
    while 0 < H[j]:
        '''H[j] -= 1
        ans += 1'''
        c = j
        end = j
        while end < n:
            #print('---', H)
            if H[end] > 0:
                end += 1
            else:
                break
        if H[j:end]:
            #print(H[j:end])
            min_H = min(H[c:end])
            ans += min_H
            for k in range(c,end):
                H[k] -= min_H
        

            '''print('---', H)
            if H[c+1] > 0:
                H[c+1] -= 1
                c += 1
            else:
                H[j] -= 1
                break'''
print(ans)
#print(H)