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()))
ans = 0
for l in range(1, max(H)+1):
    streak = False
    for h in H:
        if h>=l:
            streak=True
        elif streak:
            streak=False
            ans+=1
    if streak: ans+=1

print(ans)

Submission status: Accepted

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

count = 0
while sum(ans) != 0:
    st = -1
    ed = 0
    for i in range(n):
        if (ans[i] != 0) and (st == -1):
            st = i
        if ans[i] == 0 and st != -1:
            ed = i - 1
            break;
        if (st != -1) and (ed == 0) and (i == n-1):
            ed = i
            break;
    for i in range(st, ed+1):
        ans[i] -= 1
    count += 1
    
print(count)

Submission status: Accepted

Solution code:
def p_c():
    n = int(input())
    *H, = map(int, input().split())
    ans = 0
    while sum(H) > 0:
        bf = False
        st = 0
        while H[st] == 0:
            st += 1
        for i in range(st, n):
            if H[i] == 0:
                if bf:
                    ans += 1
                bf = False
            else:
                H[i] -= 1
                bf = True
                if i == n - 1:
                    ans += 1
    print(ans)
    
p_c()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

n, *h = map(int, sys.stdin.read().split())

def main():
    cnt = 0

    for _ in range(max(h)):
        prev_0 = True
        for i in range(n):
            if h[i] > 0:
                h[i] -= 1
                if prev_0:
                    cnt += 1
                    prev_0 = False
            else:
                prev_0 = True
    
    return cnt

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
call = 0
for i in range(max(h)):
    c = 0
    if h[0]<=i:
        #print("case1")
        for j in range(n-1):
            if h[j]<=i and h[j+1]>i:
                c += 1
    elif h[-1]<=i:
        #print("case2")
        for j in range(n-1):
            if h[j]>i and h[j+1]<=i:
                c += 1
         
    else:
        #print("case3")
        c = 1
        for j in range(n-1):
            if h[j]<=i and h[j+1]>i:
                c += 1
    #print(c)
    call += c
print(call)

Submission status: Accepted

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

ans = 0
h_list.append(0)
while True:
    idx_left = 0
    idx_right = None
    for i in range(N):
        if h_list[i] == 0 and h_list[i+1] != 0:
            idx_left = i+1
        if h_list[i] != 0 and h_list[i+1] == 0:
            idx_right = i+1

        if idx_right is not None:
            if idx_right == idx_left:
                pour = h_list[idx_left]
                ans += pour
                h_list[idx_left] = 0
            else:
                pour = min(h_list[idx_left:idx_right])
                for j in range(idx_right - idx_left):
                    h_list[idx_left+j] = h_list[idx_left+j] - pour
                ans += pour
            break

    if all([h == 0 for h in h_list]):
        break

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
H=[0]+list(map(int,input().split()))
S=[0]*(n+1)
r=0
while H!=S:
    for i in range(n):
        if H[i]==0 and H[i+1]!=0:
            r+=1
    for i in range(n):
        if H[i+1]!=0:
            H[i+1]-=1
print(r)

Submission status: Accepted

Solution code:
n=int(input())
List=list(map(int,input().split()))
# print(len(a),s,a[0])
k=0
while(True):
    # print(List)
    s=0
    t=n 
    c=False
    for i in range(0,n):
        if(not(c) and List[i]>0):
            s=i
            c=True
        elif(c and List[i]<=0):
            t=i
            break
    # print(s,t)
    if(not(c) and t==n):
        print(k)
        exit()
    for i in range(s,t):
        List[i]-=1
    k+=1

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
ans = 0
for i in range(100):
  cnt = 0
  if H[0] != 0:
    cnt += 1
  for i in range(N-1):
    if (H[i] == 0) and (H[i+1] != 0):
      cnt += 1
  ans += cnt
  for j in range(N):
    if H[j] != 0:
      H[j] -= 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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



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



    ans = 0
    while len(h) != 0:
        for i in range(len(h) - 1, 0, -1):
            if h[i] == h[i - 1]:
                h.pop(i)

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

        ma = max(h)
        mai = h.index(ma)
        if mai == len(h) - 1:
            ans += ma - h[mai - 1]
        elif mai == 0:
            ans += ma - h[mai + 1]
        else:
            ans += min(ma - h[mai + 1], ma - h[mai - 1])
        h.pop(mai)

    print(ans)




if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    h=list(map(int,input().split()))
    for i in range(10**7):
        c=-1
        for j,x in enumerate(h):
            if c==-1 and x!=0:
                c=j
            elif c!=-1 and x==0:
                break
        else:
            j+=1
        if c==-1:
            print(i)
            exit()
        for k in range(c,j):
            h[k]-=1

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

count=1
res=0
while True:
    if max(H)==0:
        break
    else:
        i=0
        try:
            while i<N:
                if H[i]==0:
                    i+=1
                else:
                    res+=1
                    while i<N and H[i]>0:
                        H[i]-=1
                        i+=1
        except:
            continue
print(res)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=0
ans=0
while sum(a)>0:
    while x<=n-1 and a[x]==0:
        x+=1
    while x<=n-1 and a[x]!=0:
        a[x]-=1
        x+=1
    ans+=1
    x=0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
max = max(h)
count = 0
for i in range(1,max+1):
    tmp_count = 0
    check = 0
    for j in h:
        if j >= i:
            if check == 0:
                tmp_count += 1
                check = 1
        else:
            check = 0
    count += tmp_count
print(count)

Submission status: Accepted

Solution code:
import sys
import math
import numpy as np

stdin = sys.stdin
mod = 10**9 + 7

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
sa = lambda h: [list(map(int, stdin.readline().split())) for i in range(h)]

n = ni()
h = na()
bar = h[0]
s = h[0]
for hi in h[1:]:
    s += max(hi - bar, 0)
    bar = hi
print(s)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
def rec(H):
    ans = 0
    if H:
        hmin = min(H)
        ans += hmin
        K = [h - hmin for h in H]
        I = [i for i, k in enumerate(K) if not k]
        ans += rec(K[:I[0]])
        for i, j in zip(I, I[1:]):
            ans += rec(K[i+1:j])
        ans += rec(K[I[-1]+1:])
    return ans
print(rec(H))

Submission status: Accepted

Solution code:
N = int(input())
h = [int(i) for i in input().split()] + [0]
ans = sum(max(h[i] - h[i + 1], 0) for i in range(N))
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:
        if h[i:].count(0)==0:
            m=min(h[i:])
            for j in range(i,n):
                h[j]-=m
            ans+=m
        else:
            ind=h[i:].index(0)
            m=min(h[i:i+ind])
            for j in range(i,i+ind):
                h[j]-=m
            ans+=m
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
s = [(0, n)]
ans = 0
while s:
    l, r = s.pop()
    if l == r:
        continue
    m = min(a[l:r])
    ans += m
    for i in range(l, r):
        a[i] -= m
    for i in range(l, r):
        if a[i] == 0:
            s.append((l, i))
            s.append((i + 1, r))
            break
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=0
while h!=[0]*n:
  now=0
  for i in range(n):
    if now==1:
      if h[i]==0:
        ans+=1
        now=0
      else:
        h[i]-=1
    else:
      if h[i]!=0:
        h[i]-=1
        now=1
  if now==1:ans+=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 : ", ans, "active : ", active)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(101):
  cnt=0
  for j in range(n+1):
    if h[j-1]<=i and h[j]>i:
      cnt+=1
  ans+=cnt

print(ans)

Submission status: Accepted

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

ans = 0
while(max(H)>=0):
    ans +=1
    for i in range(N):
        H[i] -= 1
        if (H[i] >= 0):
            if (i>=1):
                if (H[i-1] < 0 and max(H[:i])>=0):
                    ans += 1

print(ans-1)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split())) + [0]
ans = 0
min_v = 1000
curr = 0
last = -1
count = 0

for _ in range(n+1):
    for i in range(n+1):
        curr = l[i]
        if curr == 0:
            for j in range(last+1, i):
                l[j] -= min_v

            if i > last + 1:
                count += min_v

            last = i
            min_v = 1000
            curr = 0
        elif curr < min_v:
            min_v = curr
    last = -1

print(count)

Submission status: Accepted

Solution code:
n = int(input())
H = [0]+list(map(int, input().split()))+[0]
ans = 0
while max(H)>0:
    Zero = []
    for i in range(n+2):
        if H[i] == 0:
            Zero.append(i)

    for i in range(len(Zero)-1):
        if Zero[i+1] == Zero[i] + 1:
            continue
        grw = min( H[Zero[i]+1:Zero[i+1]] )
        for j in range( Zero[i]+1, Zero[i+1] ):
            H[j] -= grw
        ans += grw
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

Solution code:
n = int(input())

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

def dfs(l, r):
  #print(l, r)
  if l == r:
    return 0

  for i in range(l, r):
    v[i] = max(0, v[i] - 1)

  last = l
  res = 1
  for i in range(l, r):
    if v[i] == 0:
      res += dfs(last, i)
      last = i + 1
  res += dfs(last, r)
  return res

last = 0
res = 0
for i in range(0, n + 1):
  if v[i] == 0:
    res += dfs(last, i)
    last = i + 1
#print(v)
print(res)

Submission status: Accepted

Solution code:
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

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

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353

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

H = np.array(H, dtype=int)

ans = 0
while H.max() > 0:
    l = 0
    r = 0
    while l < N:
        while l < N and H[l] <= 0:
            l += 1
        r = l
        while r < N and H[r] > 0:
            r += 1
        if l < N:
            ans += 1
        l = r
    H -= 1
print(ans)

Submission status: Accepted

Solution code:
def getNeighboringNumbers(numbers):
    cnt = 0
    flag = False
    pre = -100
    for n in numbers:
        if flag:
            if pre + 1 != n:
                cnt += 1
                flag = False
        else:
            if pre + 1 == n:
                flag = True
            else:
                cnt += 1
        pre = n
    return cnt

n = int(input())
height = list(map(int, input().split()))
M = max(height)
ans = 0
for i in range(M, 0, -1):
    indexes = [k for k, x in enumerate(height) if x == i]
    cont = getNeighboringNumbers(indexes)
    ans += cont
    for k in indexes:
        height[k] -= 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
str_h = list(map(str, input().split()))
h_list = []
for i in range(len(str_h)):
    h_list.append(int(str_h[i]))
count = 0

while h_list.count(0) != len(h_list):
    if h_list.count(0) == 0:
        h_list = list(map(lambda x: x-1, h_list))
        count += 1
    else:
        switch = []
        for i in range(len(h_list)):
            if h_list[i] == 0:
                switch.append(0)
            else:
                switch.append(1)
        if switch[0] == 1:
            j = 0
            while switch[j] == switch[j+1] and j < len(switch)-1:
                j += 1
            h_list[0:j+1] = list(map(lambda x: x-1, h_list[0:j+1]))
            switch[0:j+1] = list(map(lambda x: x-1, switch[0:j+1]))
            count += 1
        for i in range(len(switch)-1):
            if switch[i] == 0 and switch[i+1] == 1:
                count += 1            
        for i in range(len(switch)):
            h_list[i] -= switch[i]
print(count)

Submission status: Accepted

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

ans = 0
for l in range(n):
    while H[l] > 0:
        r = l + 1
        while r < n:
            if H[r] > 0:
                r += 1
            else:
                break
    
        for j in range(l, r):
            H[j] += -1

        ans += 1

print(ans)

Submission status: Accepted

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

for i in range(100):
	prev = 0
	for j,h in enumerate(H):
		if prev == 0 and h != 0:
			ans+=1
		prev = h
		if h != 0:
			H[j]-=1
	if sum(H) == 0:
		break

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)
n = int(input())
hs = list(map(int, input().split()))

def best(plants, counter):
    all0 = True
    started = False
    for i,p in enumerate(plants):    
        if p > 0:
            all0 = False
        if p == 0:
            if started:
                break
            else:
                continue
        plants[i] -= 1
        started = True

    if all0:
        return counter
    return best(plants, counter+1)
    
cnt = best(hs, 0)
print(cnt)

Submission status: Accepted

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

ans = 0
g = [0]*n
while h != g:
    t = []
    for _h, _g in zip(h, g):
        if _h != _g:
            t.append(1)
        else:
            t.append(0)

    g = list(map(lambda x: x[0]+x[1], zip(g, t)))

    t.append(0)
    c = t[0]
    for i in range(len(t)):
        if c != t[i] and t[i] == 0:
            ans += 1

        c = t[i]

print(ans)

Submission status: Accepted

Solution code:
# ABC１１6　C

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


counter = 0

def zero_count(L):
    ans = []
    for i in range(len(L)):
        if L[i] == 0:
            ans.append(i) 
    return ans

def change_number(i,j):
    for iii in range(i,j+1):
        flowers[iii] -= 1
    global counter
    counter += 1
    # print('flowers;',flowers)
    


zeros=zero_count(flowers)

while len(zeros) != n:
    if len(zeros) == 0:
#         print('Step1')
        
        change_number(0,n-1)
        
        
    else:
        for i in range(len(zeros)):
            if i==0 and zeros[i] !=0:
                
#                 print('Step2')
                change_number(0,zeros[i]-1)
                
            elif i==len(zeros)-1:
                if zeros[i] == n-1:
#                     print('Step3')
                    break
                else:
#                     print('Step4')
                    change_number(zeros[i]+1,n-1)

            else:
                if zeros[i+1]-zeros[i] != 1:
#                     print('Step5')
                    change_number(zeros[i]+1,zeros[i+1]-1)

    
    zeros = zero_count(flowers)
    


print(counter)

Submission status: Accepted

Solution code:
import collections

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

d = collections.defaultdict(int)
count = 0
updated = True

while updated:
  updated = False

  for n in range(N):
    if H[n] > d[n]:
      d[n] += 1
      updated = True

      if n == N-1:
        count += 1
      elif H[n+1] <= d[n+1]:
        count += 1


print(count)

Submission status: Accepted

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

def water(l,r,t,A):
    t += 1
    for i in range(r-l+1):
        A[l+i] += 1
    return t,A

yet = True
lowest = 0  #最小の値

while yet:
    water_place = []
    last_need = False    #直前は水が必要だったか
    start = 0
    end = 0
    for i in range(N):
        if last_need == False and A[i] < H[i]:
            start = i
            last_need = True
        elif last_need == True and A[i] >= H[i]:
            end = i-1
            last_need = False
            water_place.append([start,end])
        
        if i == N - 1 and A[i] < H[i]:
            end = i
            water_place.append([start,end])

    for l,r in water_place:
        t,A = water(l,r,t,A)
    if A == H:
        print(t)
        yet = False

Submission status: Accepted

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

H.insert(0, 0)
H.append(0)

a = 0
while sum(H) != 0:
    
    S, E = [], []
    for i in range(n + 1):
        if H[i] == 0 and H[i + 1] != 0:
            S.append(i + 1)
        elif H[i] != 0 and H[i + 1] == 0:
            E.append(i + 1)
    
    for i in range(len(S)):
        s, e = S[i], E[i]
        m = min(H[s:e])
        H[s:e] = [h - m for h in H[s:e]]
        a += m

print(a)

Submission status: Accepted

Solution code:
def solve(A, k):
  if len(A) == 0:
    return 0
  else:
    m = A.index(min(A))
    return A[m] - k + solve(A[:m], A[m]) + solve(A[m+1:], A[m])
  
if __name__ == '__main__':
  N, *h = map(int, open(0).read().split())
  print(solve(h, 0))

Submission status: Accepted

Solution code:
def solve(A, k):
  if len(A) == 0:
    return 0
  elif len(A) == 1:
    return A[0] - k
  else:
    m = A.index(min(A))
    return A[m] - k + solve(A[:m], A[m]) + solve(A[m+1:], A[m])
  
if __name__ == '__main__':
  N, *h = map(int, open(0).read().split())
  print(solve(h, 0))

Submission status: Accepted

Solution code:
N = int(input())
S = str(input())
S = S.split()
S_list = list(S)
count = 0

for i in range(len(S_list)):
    S_list[i] = int(S_list[i])

while S_list.count(0) == 0:
    S_list = list(map(lambda x: x - 1, S_list))
    count = count + 1
    
# index_num = [n for n, v in enumerate(S_list) if v == 0]   
# =============================================================================
# print(count)
# print(S_list)
# =============================================================================

   
switch = []
for i in range(len(S_list)):
    if S_list[i] == 0:
        switch.append(0)
    else:
        switch.append(1)
        
while switch.count(1) != 0:  
    for i in range(len(S_list)):
        if i == 0:
            pass
        else:
            if switch[i-1] == 0 and switch[i] == 1:
                count = count +1
            else:
                pass
        if switch[i] == 1:
            S_list[i] = S_list[i] - 1
    if switch[0] == 1:
        count = count +1
        
    switch = []
    for i in range(len(S_list)):
        if S_list[i] == 0:
            switch.append(0)
        else:
            switch.append(1)
        
print(count)

Submission status: Accepted

Solution code:

N = int(input())
H = list(map(int, input().split()))
I = [0] * N

count = 0
updated = True

while updated:
  tmp = -9999
  updated = False
  
  for i in range(N):
    if I[i] != H[i]:
      if tmp + 1 != i:
        count += 1
      I[i] += 1
      updated = True
      tmp = i

print(count)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
cnt=0
ans=0
c=0
for i in range(100000):
    if h==[0]*n:
        break
    for j in range(n):
        if h[j]!=0:
            h[j]-=1
            cnt+=1
        elif h[j]==0 and cnt!=0:
            ans+=1
            cnt=0
            break
        elif h[j]==0 and ans==0:
            continue
        if j==n-1:
            ans+=1
            cnt=0
        #print(j)
    #print(h)
print(ans)

Submission status: Accepted

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

for i in range(max(h)):
    st = 1 if h[0] != 0 else 0
    for j in range(n):
        if st == 1:
            if h[j] == 0:
                cnt += 1
                st = 0
            else:
                h[j] -= 1
        else:
            if h[j] != 0:
                st = 1
                h[j] -= 1
    else:
        if st == 1:
            cnt += 1
else:
    ans = cnt

print(ans)

Submission status: Accepted

Solution code:
def solve():
    N, *h = map(int, open(0).read().split())
    
    ans = 0
    for _ in range(10000000):
        l, r = 0, 0
        tmp_min = 1000
        flag = False
        
        for i in range(N):
            if 0 < h[i]:
                l = i
                r = i
                flag = True
                tmp_min = h[i]
                break
        for i in range(l+1, N):
            if h[i] == 0:
                break
            r = i
            tmp_min = min(tmp_min, h[i])
        for i in range(l, r+1):
            h[i] -= tmp_min
        
        if not flag:
            break
        ans += tmp_min
    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
H = np.array(list(map(int, input().split())))
d = np.diff(H)

ra = []
m = []
for i in range(len(d) - 1):
    if d[i] <= 0 and d[i + 1] > 0:
        ra.append(i + 1)
        m.append(H[i + 1])

ra = [0] + ra + [N]
M = []
for i in range(len(ra) - 1):
    M.append(np.max(H[ra[i]:ra[i + 1]]))

print(sum(M) - sum(m))

Submission status: Accepted

Solution code:
n = int(input())
lst = [int(i) for i in input().split()]
c=lst[0]
for i in range(1,n):
#     考え方は前の番号より高さが高いとその分余分に水やる操作をやる感じ
    if lst[i]>lst[i-1]:
        c+=lst[i]-lst[i-1]
print(c)

Submission status: Accepted

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


main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input()) #N==100
H=list(map(int,input().split()))#O(N)

cnt=0
for i in range(max(H)):#O(N)
    flg=0
    for j in range(N):#O(N^2)
        if flg==0:
            if H[j]!=0:
                H[j]-=1
                cnt+=1
                flg=1
            else:  #なくてもいい
                flg=0
        else:
            if H[j]!=0:
                H[j]-=1
            else:
                flg=0
print(cnt)
#O(N^2),N==100で余裕

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input()) #N==100
H=list(map(int,input().split()))#O(N)

cnt=0
for i in range(max(H)):#O(N)
    flg=0
    for j in range(N):#O(N^2)
        if flg==0:
            if H[j]!=0:
                H[j]-=1
                cnt+=1
                flg=1
            #else:  なくてもいい
             #   flg=0
        else:
            if H[j]!=0:
                H[j]-=1
            else:
                flg=0
print(cnt)
#O(N^2),N==100で余裕

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N=int(input())
H=list(map(int,input().split()))

cnt=0
for i in range(max(H)):
    flg=0
    for j in range(N):
        if flg==0:
            if H[j]!=0:
                H[j]-=1
                cnt+=1
                flg=1
            else:
                flg=0
        else:
            if H[j]!=0:
                H[j]-=1
            else:
                flg=0
print(cnt)

Submission status: Accepted

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


main()

Submission status: Accepted

Solution code:
import sys
n = int(input())
h_ls = [int(i) for i in sys.stdin.readline().split()]
cnt = 0
for i in range(1,101):
    flg = True
    for j in range(n):
        h = h_ls[j]
        if not h:
            flg = True
        else:
            h_ls[j] -= 1
            if flg:
                flg = False
                cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int,input().split()))
H = [0]+H+[0]
def trm(li):
  for i in range(len(li)-2,0,-1):
    if li[i-1]<=li[i]<=li[i+1]:
      li.pop(i)
    elif li[i-1]>=li[i]>=li[i+1]:
      li.pop(i)
  return H
H = trm(H)
cnt = 0
for i in range(max(H),0,-1):
  flg = False
  for j in range(len(H)):
    if H[j]==i:
      if not flg:
        cnt +=1
        flg = True
      H[j]=H[j]-1
    else:
      flg = False
print(cnt)

Submission status: Accepted

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

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

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()

     
def main():
    N = I()

    h = LI()
    cnt = 0

    def dec_h(l, r):
        nonlocal cnt
        nonlocal h
        if l > r:
            return
        min_h = min(h[l:r+1])
        # print('l: {}, r: {}, min_h: {}'.format(l, r, min_h))
        for j in range(l, r+1):
            h[j] -= min_h
        cnt += min_h
        st = l
        en = r
        # print('h: {}'.format(h))
        for i in range(l, r+1):
            if h[i] == 0:
                en = i-1
                dec_h(st, en)
                st = i+1
            if i == r and st < r+1:
                dec_h(st, r)
    dec_h(0, N-1)
    print(cnt)

main()

Submission status: Accepted

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

ans = h[0]

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

print(ans)

Submission status: Accepted

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

garden = [0] * n
ans,i = 0,0

while H != garden:
    
    if H[i] != garden[i]:
        
        while 1:
            if n == i or H[i] == garden[i]:
                ans += 1
                break
            else:
                garden[i] += 1
            i += 1
    
    else: i += 1
    if n <= i: i = 0
    #print(garden)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(cnt)