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=[int(i) for i in input().split()]

ans=0
for i in range(len(h)):
    while h[i] > 0:
        ans+=1

        j=0
        while h[i+j]>0:
            h[i+j]-=1
            j+=1
            if i+j==N:
              break

print(ans)

Submission status: Accepted

Solution code:
input()
h =  [int(i) for i in input().split()]
ans = 0
bef = 0 
for i in h:
  if i > bef:
    ans += i - bef
    bef = i
  else:
    bef = i
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  sm=sum(l)

  c=0
  i=0
  while True:
    if l[i]>0:
      l[i]-=1
      sm-=1

      if i+1==n or l[i+1]==0:
        c+=1

    i+=1
    if i>=n:
      i=0

    if sm==0:
      return c

print(main())

Submission status: Accepted

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

count = 0
for h in range(max(H)):
	for n in range(1, N + 1):
		if h < H[n] and H[n - 1] <= h:
			count += 1
print(count)

Submission status: Accepted

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

ans = 0

while True:
        if max(A) == 0:
                break
        hoge = 0
        for i in range(N-1):
                if A[i] > 0 and A[i+1]  == 0:
                        ans += 1
        if A[N-1] > 0:
                        ans += 1

        for i in range(N):
                if A[i] != 0:
                        A[i] -= 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
h.append(0)
cnt = 0
while True:
    flag = True
    for i in h:
        if i != 0:
            flag = False

    if flag:
        break

    l = 0
    while h[l] == 0:
        l += 1

    r = l
    while h[r+1] != 0 and r < n:
        r += 1

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

print(cnt)

Submission status: Accepted

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

for i in range(1,higest+1):
    a = 0
    for j in h:
        if j < i:
            a = 1
        elif a == 1:
            count += 1
            a = 0
    if h[0] >= i:
        count += 1

print(count)

Submission status: Accepted

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

count = 0
for i in range(max_num+1):
    is_mountain= False
    mountain = 0
    for index, value in enumerate(h):
        if value <= 0:
            if is_mountain:
                mountain += 1
            is_mountain = False
        else:
            is_mountain = True
        h[index] -= 1
    count += mountain
print(count)

Submission status: Accepted

Solution code:
from collections import deque

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

stack = deque([])
prevPoint = -1

for i in range(N) :
    if h[i] == 0 :
        stack.append((prevPoint+1 , i))
        prevPoint = i
    elif i >= N - 1 :
        stack.append((prevPoint+1,N))

ans = 0

while stack :
    start , end = stack.pop()

    if start >= end :
        ans += h[start]
        h[start] = 0
        continue


    minus = min(h[start:end])
    ans += minus

    prevPoint = start - 1
    for i in range(start,end) :
        h[i] -= minus
        if h[i] == 0 :
            stack.append((prevPoint+1,i))
            prevPoint = i
        elif i >= end - 1 :
            stack.append((prevPoint+1,end))

print(ans)

Submission status: Accepted

Solution code:
def findlong(a):
    a+=[0]
    l = 0
    c = []
    j = 0
    for i in range(len(a)):
        if(a[i]!=0):
            l+=1
            j = i
        else:
            c.append([l,j])
            l = 0
    return max(c)
n = int(input())
a = list(map(int,input().split()))
l = 0
while(True):
    if(len(set(a))==1 and a[0]==0):
        break
    k = findlong(a)
    del a[-1]
    z = [k[1]-k[0]+1,k[1]]
    for i in range(z[0],z[1]+1):
        a[i]-=1
    l+=1
print(l)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()] 
ans = 0
for i in range(max(a)):
        m = max(a)
        cnt = a.count(m)
        for y in range(len(a)-1):
                if a[y] == m == a[y+1]:
                        cnt -= 1
        a = [(z-1 if z == m else z) for z in a]
        ans += cnt
print(ans)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

# 方針
# 前から順に-1していく
# その際、常に前の数を見ておき、0になった場合一区切り
# カウンターを+1する
# また、最後の項に達したときも+1
# インデックスは先頭に戻して
# 全ての項が0になるまで繰り返す
# （全部0になったことを確認するため、初めにsumを取って
# 引き算するたびにsumも-1していき、0になったらループ脱出）

def main():
  n=I()
  l=LI()

  sm=sum(l)

  i=0
  c=0
  while True:

    # print(l)
    # print(c)

    if l[i]!=0:
      l[i]-=1
      sm-=1

      if i==n-1 or l[i+1]==0:
        c+=1

    i+=1
    if i>=n:
      i=0

      if sm==0:
        break

  return c

print(main())

Submission status: Accepted

Solution code:

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

count = 0

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

Submission status: Accepted

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

else:
    same = []
    for i in range(N-1):
        if h[i] == h[i+1]:
            same.append(i)
    
    if len(same) >= 1:
        for k in range (len(same))[::-1]:
            del h[same[k]]
    
    N = len(h)

    lmin = [] #極小値のリスト
    lmax = [] #極大値のリスト

    if N >=3:
        for i in range(1,N-1):
            if (h[i] > h[i-1]) and (h[i] > h[i+1]):
                lmax.append(h[i])
        
        for k in range(1,N-1):
            if (h[k] < h[k-1]) and (h[k] < h[k+1]):
                lmin.append(h[k])

        if h[0] > h[1]:
            lmax.append(h[0])
            
        if h[N-1] > h[N -2]:
            lmax.append(h[N-1])
                
        if (len(lmax) == 1) :
            ans = max(h)

        else:
            ans = sum(lmax) - sum(lmin)
    
    if N == 2:
        ans = max(h)

    if N == 1:
        ans = h[0]

#print(lmax)
#print(lmin)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# class Node:
#     def __init__(self, data): #コンストラクタ
#         self.data = data #ノードがもつ数値
#         self.left = None #左エッジ
#         self.right = None #右エッジ
#
# class BST:
#     def __init__(self, number_list): #コンストラクタ
#         self.root = None #ルート初期化
#         for node in number_list: #数値を持つ配列から二分木を生成
#             self.insert(node) #挿入メソッドを使ってノードを挿入する
#
#     #挿入
#     def insert(self, data):
#         n = self.root #nにルートを代入
#         if n == None: #ルートに何も入っていない場合
#             self.root = Node(data) #ルートにノードを代入(二分探索木の一番上の部分が出来る)
#             return #呼び出し部分に戻る
#         else: #ルートノードに既に数値が入ってる場合
#             while True: #return等が入るまでずっと実行
#                 entry = n.data #entryにnが持つデータ(数値)を入れる
#                 if data < entry: #entryとdata(代入するデータ)を比較してentryの方が大きい場合
#                     if n.left is None: #nの左エッジに何も入っていない場合
#                         n.left = Node(data) #nの左エッジにdata(代入するデータ)を代入
#                         return ##呼び出し部分に戻る
#                     n = n.left #nの左に何か入っている場合nにnのleftを代入
#                 elif data > entry: #entryとdata(代入するデータ)を比較してentryの方が小さい場合
#                     if n.right is None: #nの右エッジに何も入っていない場合
#                         n.right = Node(data) #nの右エッジにdata(代入するデータ)を代入
#                         return #呼び出し部分に戻る
#                     n = n.right #nの右エッジに何か入っている場合nにnのrightを代入
#                 else: #上2つの条件分岐以外の場合
#                     n.data = data #nのdataにdata(代入するデータ)を代入
#                     return #呼び出し部分に戻る

n = int(input())
h = list(map(int, input().split()))
h2 = [0] + h
# for i in h:
#     h2.append(max(h) - i)
ans = 0
imosarray = [0 for _ in range(n + 1)]
for i in range(n):
    imosarray[i] = h2[i + 1] - h2[i]
    ans += max(0, h2[i + 1] - h2[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
h = [0] + [int(h_i) for h_i in input().split()]
 
answer = 0
for i in range(1, len(h)):
  if h[i] > h[i-1]:
    answer += h[i] - h[i-1]
  
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
ans = 0
x = [0]*N
for i in range(N):
    for _ in range(h[i]-x[i]):
        y = False
        for j in range(i,N):
            if x[j] < h[j]:
                x[j] += 1
                y = True
            else:
                break
        if y:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
import bisect
import operator
import os
from collections import Counter, deque
from fractions import gcd
from functools import lru_cache
from functools import reduce
import functools
import heapq
import itertools
import math
import numpy as np
import re
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")

N = int(sys.stdin.readline())
H = list(map(int, sys.stdin.readline().split()))

H = [0] + H + [0]
ans = 0
for h in reversed(range(0, max(H) + 1)):
    yes = False
    for i in range(len(H)):
        if H[i] >= h:
            yes = True
        else:
            if yes:
                ans += 1
            yes = False
print(ans)

Submission status: Accepted

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

def yama(l):
    r=0
    zf=0
    a=[0]*(len(l)+2)
    for i in range(1,len(l)+1):
        a[i]=l[i-1]
    for i in range(0,len(l)+1):
        if a[i]>0 and zf==1:
            r+=1
            zf=0
        elif a[i]<=0:
            zf=1
    return r
ans=0
while yama(l)>0:
    ans+=yama(l)
    for i in range(n):
        l[i]-=1
   
print(ans)

Submission status: Accepted

Solution code:
import sys
import copy
from collections import deque


input = sys.stdin.readline

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

def water(array):
    global count
    if array != []:
        minimum = min(copy.deepcopy(array))
        count += minimum
        for i in range(len(array)):
            array[i] -= minimum
    indx = [i for i, x in enumerate(array) if x == 0]
    if len(indx) == 0:
        return None
    elif len(indx) == 1:
        water(array[:indx[0]])
        water(array[indx[0]+1:])
    elif len(indx) > 1:
        water(array[:indx[0]])
        water(array[indx[-1]+1:])
        for i in range(len(indx)-1):
            water(array[indx[i]+1:indx[i+1]])

water(h)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

N = int(input())

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

ans = 0

for loop in range(100):
        
        start = 102

        for flo in range(N + 1):

            if (h[flo] > loop and start > 101):
                start = flo

            elif (h[flo] <= loop and start < flo):
                ans += 1
                start = 102

print (ans)

Submission status: Accepted

Solution code:
def resolve():
    N = int(input())
    Hs = list(map(int, input().split(" ")))
    cnt = 0
    while(any(Hs)):
        start = 0
        for i in range(N):
            # print("start: {}".format(start))
            if Hs[i] == 0:
                if i != start:
                    for j in range(start, i):
                        Hs[j] -= 1
                    cnt += 1
                start = i + 1 
            elif i == N-1:
                for j in range(start, N):
                    Hs[j] -= 1
                cnt += 1
        # print(Hs)
        # print(cnt)
        # input()

    print(cnt)
    

if '__main__' == __name__:
    resolve()

Submission status: Accepted

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

t = 0
while max(h) > 0:
    si = N
    ei = N
    for i in range(N):
        if h[i] > 0:
            si = i
            break
    for i in range(si, N):
        if h[i] == 0:
            ei = i
            break
    for i in range(si, ei):
        h[i] -= 1
    t += 1
print(t)

Submission status: Accepted

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

for i in range(max(h)):
    zero=0
    for j in range(N+1):
        if h[j]==0:
            if h[j+1]==0:
                pass
            else:
                zero+=1
        else:
            h[j]-=1
    ans+=zero

print(ans)

Submission status: Accepted

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

ans = sum(H)
for i in range(N-1):
    ans -= min(H[i], H[i+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]:
        active = H[i]
    else:
        ans += H[i] - active
        active = H[i]
print(ans)

Submission status: Accepted

Solution code:
# ContestName
# URL
import sys
#import numpy as np

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

def main():
    N = i2n()
    H = i2nn()
    n = 0
    while True:
        l = -1
        r = -1
        for i, h in enumerate(H):
            if l == -1 and h > 0:
                l = i
            if l > -1 and r == -1 and h <= 0:
                r = i
        if l == -1:
            break
        if r == -1:
            r = N
        for i in range(l, r):
            H[i] -= 1
        n += 1
    print(n)

main()

Submission status: Accepted

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

def flower(lst=H):
    if not lst:
        return 0
    ans = min(lst)
    lst = list(map(lambda x:x-ans, lst))
    indexes = [-1]
    indexes.extend([i for i, j in enumerate(lst) if j == 0])
    m = len(indexes)
    indexes.append(len(lst))
    for i in range(m):
        ans += flower(lst[indexes[i]+1:indexes[i+1]])
    return ans

print(flower())

Submission status: Accepted

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
def mi(): return map(int, input().split())
###template###

N = int(input())
Hs = list(mi())

ans = 0
#全てが0になるまで続ける
while 1:
  #全ての高さをチェックし、「0以外の区間」を探す
  nowinsection = 0 #一個前の高さが「0以外」なら1にしておく
  sectioncnt = 0 #0以上の区間を数える
  for i, h in enumerate(Hs):
    if h == 0:
      nowinsection = 0
    elif nowinsection == 0: #0から1以上に変わった瞬間
      nowinsection = 1
      sectioncnt += 1
      Hs[i] = h - 1 #ループを増やさないよう、ここで高さを減らしておく
    else: #1以上から1以上が引き続いた場合、区間は続くのでsectioncntは増やさない
      Hs[i] = h - 1 #ループを増やさないよう、ここで高さを減らしておく
  if sectioncnt == 0:
    print(ans)
    exit()
  else:
    ans += sectioncnt

Submission status: Accepted

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


def all_zero(h):
    zero = True
    for hh in h:
        if hh != 0:
            zero = False
            break
    return zero

def div_ary(h):
    arys = []
    zeropos = []

    for i,hh in enumerate(h):
        if hh == 0:
            zeropos.append(i)
    
    if len(zeropos) == 0:
        return [h]
    else:
        tmpary = []
        for i, hh in enumerate(h):
            if hh == 0:
                arys.append(tmpary)
                tmpary = []
            else:
                tmpary.append(hh)
        arys.append(tmpary)
    return arys

#minが0になるまで全体に水やり
def mizuyari(h_ary):
    if len(h_ary) == 0:
        return 0
    else:
        #minまでは水やり
        h_min = min(h_ary)
        for i in range(len(h_ary)):
            h_ary[i] -= h_min
        num = h_min

        #すべてゼロなら回数決定
        if all_zero(h_ary):
            return num
        else:
            #ゼロじゃなければ分割して部分水やり
            arys = div_ary(h_ary)

            for a in arys:
                num += mizuyari(a)
    return num


h_ary = []
for th in target_h:
    h_ary.append(th)
mizunum = mizuyari(h_ary)
print(mizunum)

Submission status: Accepted

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

Hk = set([0])
for h in H:
  Hk.add(h)
Hk = sorted(Hk)

def po(h, s, e):
  posS = s
  cnt = 0
  for posE in range(s, e+1):
    if posE >= e or H[posE] <= Hk[h]:
      if posS < posE:
        cnt += po(h+1, posS, posE)
      posS = posE + 1
  if h > 0:
    cnt += Hk[h] - Hk[h-1]
  return cnt

print(po(0, 0, len(H)))

Submission status: Accepted

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

Hk = set([0])
for h in H:
  Hk.add(h)
Hk = sorted(Hk)

def po(h, s, e):
  if h >= len(Hk)-1:
    return Hk[h] - Hk[h-1]
  posS = s
  cnt = 0
  for posE in range(s, e+1):
    if posE >= e or H[posE] <= Hk[h]:
      if posS < posE:
        cnt += po(h+1, posS, posE)
      posS = posE + 1
  if h > 0:
    cnt += Hk[h] - Hk[h-1]
  return cnt

print(po(0, 0, len(H)))

Submission status: Accepted

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

res = 0
while True:
    if max(h) == 0:
        break

    i = 0
    while(i<N):
        if h[i] == 0:
            i += 1
        else:
            res += 1
            while (i < N) and (h[i] > 0):
                h[i] -= 1
                i += 1
print(res)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0
bottom = 0

for hh in h:
    if hh > bottom:
        ans += hh - bottom
    
    bottom = hh

print(ans)

Submission status: Accepted

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


def calc(l, r):
    mi = float('inf')
    i = -1
    if l + 1 == r:
        return H[l]
    if l == r:
        return 0
    for j in range(l, r):
        if mi > H[j]:
            mi = H[j]
            i = j

    for j in range(l, r):
        H[j] -= mi

    return calc(l, i) + calc(i + 1, r) + mi


print(calc(0, N))

Submission status: Accepted

Solution code:

time = 0
def count(s):
    if s== []:
        return
    global time 
    isend = True
    div = s.index(min(s))
    minuted = s[div]
    time += minuted
    for i in range(len(s)):
        s[i] -= minuted
        if s[i] != 0:
            isend = False
    else:
        nextlistf = []
        nextlistl = []
        for i in range(div):
            nextlistf.append(s[i])
        for i in range(1, len(s) - div):
            nextlistl.append(s[i + div])
        count(nextlistf)
        count(nextlistl)


n = int(input())
s = list(map(int, input().split()))
count(s)
print(time)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n = int(input())
h = list(map(int, input().split()))
if n == 1:
    print(h[0])
else:
    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())
h = [int(_) for _ in input().split()]

if  n == 1:
    print(h[0])
else:
    ans = h[0]
    for i in range(n-1):
        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(" ")))
H_orig = H[:]
heights = [0 for i in range(N)]

#print(H,N,heights)

cnt = 0

while True:
    #print(heights,H)
    if min(H) > min(heights):
        for i in range(N):
            heights[i] += 1
            H[i] -= 1
        cnt += 1
    else:
        flag = False
        for i in range(N):
            #H[i]
            if H[i] > 0:
                heights[i] += 1
                H[i] -= 1
                flag = True
            else:
                if flag:
                    cnt += 1
                    flag = False
                    
            if i == N-1 and flag:
                cnt += 1
        
    if H_orig == heights:
        break
    
print(cnt)

Submission status: Accepted

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

counter = 0

while h != [0]*N:
    for l in range(N):
        if h[l] != 0:
            break
    r = N-1
    for r in range(l+1,N):
        if h[r] == 0:
            r -= 1
            break
    for n in range(l,r+1):
        h[n] -= 1
    counter += 1
    continue

print(counter)

Submission status: Accepted

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

ans = 0
cou = 0

hList = [0] + hList

for i in range(N):
    if hList[i] < hList[i+1]:
        cou += (hList[i+1] - hList[i])
    else:
        ans += cou
        cou = 0
ans += cou
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
hs = list(map(int, input().split()))
hs.insert(0, 0)
hs.append(0)
res = lh0 = lh1 = hs[0]
for h in hs:
    if lh0 <= lh1 > h:
        res += lh1
    elif lh0 > lh1 <= h:
        res -= lh1
    lh0, lh1 = lh1, h

print(res)

Submission status: Accepted

Solution code:
import copy
N = int(input())
h = list(map(int, input().split()))
l = []
l.append(h)
x = 0
while l != []:
	m = []
	for i in range(len(l)):
		a = [j for j, _ in enumerate(l[i]) if _ == min(l[i])]
		start = 0
		new = []
		for s in a:
			if l[i][start:s] != []:
				y = list(map(lambda _: _ - min(l[i]), l[i][start:s]))
				new.append(y)
			start = s + 1
		if l[i][start:] != []:
			z = list(map(lambda _: _ - min(l[i]), l[i][start:]))
			new.append(z)
		x += min(l[i])
		m = m + new
	l = copy.deepcopy(m)
print(x)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
result=0
H=max(h)
for j in range(0,H):
    tmp=0
    cur=0
    #print(h,result)
    for i in range(1,n):
        if h[i]<=0 and h[i-1]>0:
            tmp+=1
            cur=1
            #print(j,i,1)
    if h[n-1]>0 and cur!=0:
        tmp+=1
        #print(j,i,2)
    for i in range(0,n):
        h[i]-=1
    #sprint(tmp)
    result+=max(tmp,1)
print(result)

Submission status: Accepted

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

import sys

input = sys.stdin.readline

def main():
    N = int(input())
    H = list(map(int, input().split()))
    C = [0 for _ in range(N)]
    cost = 0
    for i in range(max(H)):
        is_open = True
        for j in range(N):
            if C[j] < H[j]:
                C[j] += 1
                if is_open:
                    cost += 1
                    is_open = False
            else:
                is_open = True
    print(cost)

main()

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
su = sum(h)
count = 0
while sum(h) > 0:
  i = 0
  while i < n:
    if(h[i] == 0):
      i+=1
    else:
      count += 1
      while i < n and h[i] > 0:
        h[i] -= 1
        i += 1

print(count)

Submission status: Accepted

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

cnt = 0
while 1:
  if max(h)==0: break

  i = 0
  while i<N:
    if h[i]==0:
      i+=1
    else:
      cnt += 1
      while i<N and h[i]>0:
        h[i] -= 1
        i += 1
print(cnt)

Submission status: Accepted

Solution code:
from collections import deque

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

cost = 0

d = deque()
d.append((0,N,0))
while len(d):
    l,r,mm = d.pop()
    if l == r:
        continue
    m = min(H[l:r])
    cost += m - mm
    i = l
    while True:
        if H[i] == m:
            d.append((l,i,m))
            l = i + 1
        if i + 1 == r:
            d.append((l,r,m))
            break
        i += 1

print(cost)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    h = list(map(int, input().split()))
    h = [0] + h
    count = 0
    for i in range(len(h) - 1):
        tmp = h[i + 1] - h[i]
        count += tmp * int(tmp > 0)
    print(count)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n=int(input())
h=[int(i) for i in input().split()]+[0]
ans=0
wat=0
for m,n in zip(h,h[1:]):
    if wat<m:
        wat=m
    if wat>n:
        ans+=wat-n
        wat=n
print(ans)

Submission status: Accepted

Solution code:

def solve(H):
    total = 0
    while True:
        c = 0
        for i, h in enumerate(H):
            if c == 0:
                if h > 0:
                    H[i] -=1
                    c = 1
            elif 0 < h:
                H[i] -= 1
            elif h == 0:
                break
        if c == 0:
            return total
        total += 1
N = int(input())
H = [int(x) for x in input().split()]
print(solve(H))

Submission status: Accepted

Solution code:
import copy
N=int(input())
rH=list(map(int,input().split()))
ans=0
for i in range(max(rH)):#もっと目標が高いものの回数まで行う
    cH=copy.deepcopy(rH)
    for k in range(N):#rHで条件を確認しつつ、cHを更新していく
        if rH[k]!=0 and ((k!=0 and rH[k-1]==0)or(k==0)):
            cH[k]-=1
            ans+=1
        elif rH[k]!=0:
            cH[k]-=1
    rH=copy.deepcopy(cH)
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
hlist=list(map(int,input().split()))
ans=min(hlist)
if len(hlist)==1:
    print(ans)
else:
    if max(hlist)==min(hlist):
        print(ans)
    else:
        for i in range(min(hlist)+1,max(hlist)+1):
            for j in range(len(hlist)-1):
                if hlist[j]>=i and hlist[j+1]<i:
                    ans+=1
            if hlist[len(hlist)-1]>=i:
                ans+=1
        print(ans)

Submission status: Accepted

Solution code:
N=int(input())
Hs=[0] + list(map(int,input().split()))
Hs.append(0)
ma = max(Hs)

res = 0
for _ in range(ma):
    cnt = 0
    last1 = -1
    for i in range(N+2):
        if Hs[i] > 0:
            Hs[i] -= 1
            if last1+1 != i:
                cnt += 1
            last1 = i
    # print(Hs)
    res += cnt

print(res)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
 
mizuyari = 0
for i in range(sorted(h)[n-1]):
    maenohana = False
    for j in range(n):
        if not maenohana and i+1<=h[j]:
            mizuyari += 1
        maenohana = i+1<=h[j]
print(mizuyari)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=II()
  l=LI()

  zcnt=l.count(0)
  c=0
  i=0

  while True:
    if zcnt==n:
      break
    if i>=n:
      i=0
    if l[i]>0:
      c+=1
      while True:
        if i<n and l[i]>=1:
          l[i]-=1

          if l[i]==0:
            zcnt+=1

          i+=1
        else:
          break
    else:
      i+=1

  return c

print(main())

Submission status: Accepted

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


def solve(N: int, h: "List[int]"):
    result = 0
    for i in range(N):
        if i==0:
            result += h[0]
        elif h[i] > h[i-1]:
            result += h[i] - h[i-1]
    print(result)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    h = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, h)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import setrecursionlimit
setrecursionlimit(10**8)


def count_water(h_array):
    # print(h_array)
    water = 0
    if len(h_array) == 1:
        water = h_array[0]
        # print(h_array, water)
        return water
    min_h = min(h_array)
    water = min_h
    new_h_array = []
    for h in h_array:
        if h != min_h:
            new_h_array.append(h - min_h)
        else:
            if len(new_h_array) != 0:
                water += count_water(new_h_array)
                new_h_array = []
    else:
        if len(new_h_array) != 0:
            water += count_water(new_h_array)        
    # print(h_array, water)
    return water


N = int(input())
h_array = list(map(int, input().split()))
print(count_water(h_array))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# ABC116C - Grand Garden
n = int(input())
lst = list(map(int, input().rstrip().split()))
ans = 0
cur = 0
for i in lst:
    ans += max(0, i - cur)
    cur = i
print(ans)

Submission status: Accepted

Solution code:
# ABC116C - Grand Garden
n = int(input())
lst = list(map(int, input().rstrip().split())) + [0]
ans = 0
cur = 0
for n, m in zip(lst, lst[1:]):
    if cur < n:
        cur = n
    if cur > m:
        ans += cur - m
        cur = m
print(ans)