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:
#!/usr/bin/env python3
n, *h = map(int, open(0).read().split())
ans = 0
b = ["1"] * n
while sum(h) > 0:
    for i in range(n):
        if h[i] > 0:
            h[i] -= 1
        else:
            b[i] = "0"
    ans += sum(i != "" for i in "".join(b).split("0"))
print(ans)

Submission status: Accepted

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

q = queue.Queue()
ans = 0
def dec(ary):
  global q, ans
  m = min(ary)
  ans += m
  tmp = []
  for i in range(len(ary)):
    t = ary[i] - m
    if t > 0:
      tmp.append(t)
    else:
      if len(tmp) > 0:
        q.put(tmp)
      tmp = []
  if len(tmp) > 0:
    q.put(tmp)
tmp = []
for i in range(n):
  if h[i] > 0:
    tmp.append(h[i])
  else:
    if len(tmp) > 0:
      #print(tmp)
      q.put(tmp)
    tmp = []

if len(tmp) > 0:
  q.put(tmp)

while q.empty() == False:
  dec(q.get())
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
m=0
s=0
for j in range(100):
    for i in range(n):
        if h[i]==0:
            s=0
        elif s!=0:
            h[i]-=1
        else:
            h[i]-=1
            s+=1
            m+=1
    s=0
print(m)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
is_init = True
is_end = False
ans = 0
while not is_end:
    is_init = True
    for i in range(N):
        h = H[i]
        if h <= 0:
            if i == N - 1 and is_init:
                is_end = True
            H[i] -= 1
        else:
            is_init = False
            if i - 1 >= 0:
                pre = H[i - 1]
                if pre <= -1:
                    ans += 1
            else:
                if h != 0:
                    ans += 1
            H[i] -= 1
print(ans)

Submission status: Accepted

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

for j, i in enumerate(h):
    if val0 > i:
        ans += val0-val1
        val1 = i
    elif j == len(h)-1:
        val0 = i
        ans += val0-val1
    val0 = i
print(ans)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

ans=h[0]

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
count=0
while h!=[0]*n:
    for i in range(n):
        if h[i]!=0:
            start=i
            break
    end=n-1
    for j in range(i+1,n):
        if h[j]==0:
            end=j-1
            break
    for k in range(start,end+1):
        h[k]-=1
    count+=1
print(count)

Submission status: Accepted

Solution code:
N = int(input())
# m1,d1=map(int,input().split())
hl = list(map(int, input().split()))
#l=[list(map(int,input().split())) for i in range(n)]
hc = [0 for i in range(N)]
mx = max(hl)
count = 0
for k in range(mx):
    i = 0
    while i < N:
        if hl[i] > hc[i]:
            count += 1
            while i < N and hl[i] > hc[i]:
                hc[i] = hc[i] + 1
                i += 1
        else:
            i += 1
print(count)

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
 
## Library
 
import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy
 
## input
 
# N=int(input())
# A,B,C,D=map(int, input().split())
# S = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]
#    S = []
#    for _ in range(N):
#        S.append(list(input()))
 
N = int(input())
H = list(map(int, input().split()))

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

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    h = list(map(int, input().split()))
    ans = h[0]
    for i in range(1, n):
        if h[i] - h[i-1] > 0:
            ans += h[i] - h[i-1]
    print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    h = list(map(int, input().split()))
    arr = [h[0]] + [0] * (n - 1)
    ans = 0
    for i in range(1, n):
        arr[i] = h[i] - h[i-1]
    for i in range(n):
        if arr[i] > 0:
            ans += arr[i]
    print(ans)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

    water = 0
    ph = 0
    for h in H:
        if (d := h - ph) > 0:
            water += d
        ph = h
    print(water)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *h = map(int, read().split())
    if n == 1:
        print(h[0])
        sys.exit()
    r1 = h[0]
    for i1 in range(n - 1):
        if h[i1] < h[i1 + 1]:
            r1 += (h[i1 + 1] - h[i1])
    print(r1)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *h = map(int, read().split())
    if n == 1:
        print(h[0])
        sys.exit()
    r1 = h[0]
    r2 = h[-1]
    for i1 in range(n - 1):
        if h[i1] < h[i1 + 1]:
            r1 += (h[i1 + 1] - h[i1])
        elif h[i1] > h[i1 + 1]:
            r2 += (h[i1] - h[i1 + 1])
    r = max(r1, r2)
    print(r)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N, *H = map(int, open(0).read().split())

ans = 0

def split_list(l, s):
    ans = []
    while l.count(s):
        pos = l.index(s)
        ans.append(l[:pos])
        l = l[pos+1:]
    ans.append(l)
    return ans

def f(L):
    if len(L) == 0:
        return
    while L.count(0) == 0:
        global ans
        ans += 1
        L = [l - 1 for l in L]
    for m in split_list(L, 0):
        f(m)

f(H)
print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = int(input())
Hs = list(mapint())+[0]

ans = 0
while 1:
    cnt = 0
    start = False
    for i in range(N+1):
        if Hs[i]!=0:
            start = True
            Hs[i] -= 1
        else:
            if start:
                cnt += 1
            start = False
    ans += cnt
    if sum(Hs)==0:
        break
print(ans)

Submission status: Accepted

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

ans = 0
while max(H) > 0:
  grp = [[]]
  for i,h in enumerate(H):
    if h == 0:
      if len(grp[-1]) > 0: grp.append([])
    else: grp[-1].append((i,h))
  for g in grp:
    if len(g) == 0: continue
    Min = min([h for _,h in g])
    ans += Min
    for i,_ in g: H[i] -= Min
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int,input().split()))
tot = 0
while True:
    if H[0]>0:
        cnt = 1
    else:
        cnt = 0
    for i in range(1,N):
        if H[i]>0 and H[i-1]==0:
            cnt += 1
    tot += cnt
    for i in range(N):
        if H[i]>0:
            H[i] -= 1
    flag = 0
    for i in range(N):
        if H[i]>0:
            flag = 1
            break
    if flag==0:break
print(tot)

Submission status: Accepted

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

def rec(H):
    # Hに0が存在しないと仮定して
    if not H:
        return 0
    ans = min(H)
    H = [h-ans for h in H]

    ranges = []
    preh = 0
    left = -1
    for i, h in enumerate(H):
        # lrを記録
        if preh == 0 and h > 0:
            left = i
        elif preh > 0 and h == 0:
            ranges.append((left, i))
            left = -1
        preh = h
    else:
        if left >= 0:
            ranges.append((left, len(H)))
        
    for l, r in ranges:
        ans += rec(H[l:r])
    return ans

print(rec(H))

Submission status: Accepted

Solution code:
def down(H, n):
  x = min(H)
  n += x
  for i in range(len(H)):
    H[i] -= x
  return H, n

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

while not all(h == 0 for h in H):
  L = []
  H0 =[]
  for h in H:
    if h != 0:
      L.append(h)
    else:
      if L:
        L, n = down(L, n)
      H0 += L
      H0.append(0)
      L = []
  if L:
    L, n = down(L, n)
    H0 += L
  H = H0

print(n)

Submission status: Accepted

Solution code:
import sys
N = int(input())
H = list(map(int, input().split()))
res = 0
while True:
    i = 0
    cnt = 0
    while i < N:
        while i < N and H[i] <= 0:
            i += 1
        if i >= N:
            break
        while i < N and H[i] > 0:
            H[i] -= 1
            i += 1
        cnt += 1
    if cnt == 0:
        print(res)
        sys.exit()
    else:
        res += cnt

Submission status: Accepted

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

K=0
for a in range(1,M+1):
    for b in range(N):
        if H[b]<a and H[b+1]>=a:
            K+=1
print(K)

Submission status: Accepted

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

# シミュレーションで求められる
# 目標地点からさかのぼる

ans = 0
for _ in range(max(H)):
    flg = False
    for i in range(n):
        if H[i] > 0:
            H[i] -= 1
            if not flg:
                ans += 1
                flg = True
        elif H[i] == 0:
            flg = False

print(ans)

Submission status: Accepted

Solution code:
import sys
from operator import itemgetter

sys.setrecursionlimit(10**9)
input = sys.stdin.readline

def ii(): return int(input())
def mi(): return map(int, input().split())
def lmi(): return list(map(int, input().split()))
def iif(n): return [int(input()) for _ in range(n)]
def lmif(n): return [list(map(int, input().split())) for _ in range(n)]
def ss(): return input().split()

def main():
    mod = 1000000007
    
    N = ii()
    H = lmi()
    F = [0] * N # flowers

    cnt = 0
    l, r = 0, 0
    while True:
        while H[l] == F[l]:
            l += 1
            if l == N:
                break
        if l == N:
            break
        for i in range(l, N):
            if H[i] == F[i]:
                r = i
                break
            if i == N-1:
                r = N
        for i in range(l, r):
            F[i] += 1
        cnt += 1
    
    print(cnt)

    return

main()

Submission status: Accepted

Solution code:
_ = input()
h = list(map(int, input().split()))
ans = 0
while sum(h) > 0:
    left = 0
    while left < len(h) and h[left] == 0:
        left += 1
    h[left] -= 1
    right = left + 1
    while right < len(h) and h[right] != 0:
        h[right] -= 1
        right += 1
    ans += 1
print(ans)

# 問題文、rとlは固定かと思ったけど毎回指定するってのがサンプルから分かった
# 操作逆にしてぜんぶ0にするまでを数える
# NもHも小さいので「山になっている部分を減らす」を毎回やっても大丈夫だと思う
# しゃくとり的にやったけどちょっと雑
# むしろこれでも間に合うか（毎回sumするver）

Submission status: Accepted

Solution code:
# 問題文、rとlは固定かと思ったけど毎回指定するってのがサンプルから分かった
# 操作逆にしてぜんぶ0にするまでを数える
# NもHも小さいので「山になっている部分を減らす」を毎回やっても大丈夫だと思う
# しゃくとり的にやったけどちょっと雑
n = int(input())
h = list(map(int, input().split()))
ans = 0
s = sum(h)
while s > 0:
    left = 0
    while left < n and h[left] == 0:
        left += 1
    h[left] -= 1
    s -= 1
    right = left + 1
    while right < n and h[right] != 0:
        h[right] -= 1
        s -= 1
        right += 1
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
L=list(map(int,input().split()))
ans=0
ans+=min(L)
for i in range(n):
    L[i]-=ans
L=[0]+L+[0]
while True:
    tmp=0
    for i in range(1,n+2):
        if L[i]==0 and len(L[tmp:i])!=0:
            if len(L[tmp:i])==1:
                Min=L[tmp+1]
            else:
                Min=min(L[tmp+1:i])
            ans+=Min
            for j in range(tmp+1,i):
                L[j]-=Min
            tmp=i
    if max(L)==0:
        break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
i = 0
ans = 0
c = 0
while True:
    if h[i] == 0:
        i += 1
        c += 1
        if c == n:
            break
        if i == n:
            i = 0
            c = 0
    else:
        cnt = 0
        ans += 1
        while h[i + cnt] != 0:
            h[i + cnt] -= 1
            cnt += 1
            if i + cnt == n:
                break
        i += cnt
        if i == n:
            i = 0
            c = 0
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = [int(x) for x in input().split()]
count = 0
con = 0
while True:
    for i in range(N):
        if h[i] > 0:
            h[i] -= 1
            con = 1
        elif h[i] <= 0:
            if con == 1:
                count += 1
            con = 0
    if con == 1:
        count += 1
    con = 0
    if not any(h):
        break

print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque, defaultdict
from math import sqrt, factorial, gcd
# def input(): return sys.stdin.readline()[:-1] # warning not \n
# def input(): return sys.stdin.buffer.readline().strip() # warning bytes
# def input(): return sys.stdin.buffer.readline().decode('utf-8')


def solve():
    n = int(input())
    a = [int(x) for x in input().split()]
    m = max(a)
    ans = 0
    for _ in range(m):
        b = a[:]
        for i in range(n):
            if a[i]:
                if i == 0 or a[i-1] == 0:
                    ans += 1
                b[i] -= 1
        a = b[:]

    print(ans)
        

t = 1
# t = int(input())
for case in range(1,t+1):
    ans = solve()


"""



"""

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())
ns = list(map(int, input().split()))

newlist = [0 for i in range(N)]
cou = 0
while all([a == 0 for a in ns]) == False:
    y = 0
    for i,num in enumerate(ns):
        if num == 0 and y == 0:
            continue
        elif num != 0:
            ns[i] -= 1  
            y = 1
        
        if num == 0 and y == 1:
            break
        
    cou+=1

print(cou)

Submission status: Accepted

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

ans = 0
while sum(h) != 0:
  flag = False
  for i in range(n):
    if h[i] != 0: flag = True
    if flag == True and h[i] == 0: break
    elif flag == False and h[i] == 0: continue
    h[i] -= 1
  ans += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
sikiri = [-1, n]
ans = 0

for i in range(max(h)+1): # i は高さ
    sik = [j for j, x in enumerate(h) if x == i]
    sikiri[len(sikiri):len(sikiri)] = sik
    sikiri.sort()
    
    for j in range(len(sikiri)-1):
        if sikiri[j+1] - sikiri[j] > 1:
            ans += 1
        else:
            pass
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
Hs = [H]
T, c = 0, 1
while c<100000:
  List_1 = []
  for H in Hs:
    start = 0
    T += min(H)
    H = [h-min(H) for h in H]
    List_2 = [i for i, x in enumerate(H) if x == 0]
    for l in List_2:
      List_1.append(H[start:l])
      start = l+1
    List_1.append(H[start:])
  Hs.clear()
  for List in List_1:
    Hs.append(List)
  Hs = [H for H in Hs if H != []]
  c += 1
print(T)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
count = 0
l = 0
r = 0
while True:
    for i in range(n):
        if h[i] != 0:
            l = i
            break
        if i == n - 1:
            print(count)
            exit()

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

    if l == n - 1:
        r = l

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

Submission status: Accepted

Solution code:
N = int(input())
H = [int(X) for X in input().split()]
Count = 0
while True:
    if len(H)==1:
        Count += H[0]
        break
    else:
        MAXI = H.index(max(H))
        if MAXI==0:
            LR = MAXI+1
        elif MAXI==len(H)-1:
            LR=MAXI-1
        else:
            LR = H.index(max(H[MAXI-1],H[MAXI+1]))
        Count += H[MAXI]-H[LR]
        del H[MAXI]
print(Count)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# 58
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
h = LI()
ans = 0
while True:
    if max(h) == 0:
        break
    i = 0
    while i < n:
        if h[i] == 0:
            i += 1
        else:
            ans += 1
            while i < n and h[i] > 0:
                h[i] -= 1
                i += 1
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

# https://atcoder.jp/contests/abc116


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

    H = max(h)

    mat = [None] * H
    for i in range(H):
        mat[i] = "".join(["1" if i < h[j] else "0" for j in range(N)])

    ans = 0
    for i in range(H):
        ans += sum([1 if len(s) > 0 else 0 for s in mat[i].split("0")])

    return ans


print(main())

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):
        ans += 1
        for j in range(i, N):
            if h[j] >= 1:  h[j] -= 1
            else:  break
print(ans)

Submission status: Accepted

Solution code:
import sys

def main():
    input = sys.stdin.readline
    n = int(input())
    h_list = list(map(int, input().split()))
    l = -1
    r = -1
    result = 0
    while any((h > 0 for h in h_list)):
        for i,height in enumerate(h_list):
            if height > 0:
                if l == -1:
                    l = i
                if i == n-1:
                    r = i
                    break
            else:
                if l == -1:
                    continue
                elif r == -1:
                    r = i - 1
                    break

        if l == 0 and r == 0:
             h_list[0] = h_list[0] - 1
        else:
            for i in range(l,r+1):
                h_list[i] = h_list[i] - 1 
        l = -1
        r = -1
        result += 1
    print(result)
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
l = 0
r = 1
while max(Hs) > 0:
  if Hs[l] == 0:
    l += 1
    r = l + 1
  elif r < N and Hs[r] > 0:
    r += 1
  else:
    for i in range(l,r):
      Hs[i] -= 1
    cnt += 1
    r = l + 1
  
print(cnt)

Submission status: Accepted

Solution code:
#ABC 116 C - Grand Garden

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

#リストの最大値Mを求める
#リストを左から走査
 #MだったらフラグOn、その数を-1。Mより下回ったらフラグoff
 #までいったらMを

cnt = 0
while max(h) > 0:
    M = max(h)
    for i in range(n):
        if i == 0:
            flag = False
        
        if h[i] == M and flag == False:
            cnt += 1
            h[i] -= 1
            flag = True
        
        elif h[i] == M and flag == True:
            h[i] -= 1
            
        else:
            flag = False
        
print(cnt)

Submission status: Accepted

Solution code:
from itertools import chain

def process(l, ans):
  if not l:
    return ans
  elif len(l) == 1:
    ans += l[0]
    return ans
  elif all(l):
    ans += min(l)
    l = list(map(lambda x: x - min(l), l))
    ans = process(l, ans)
  else:
    d = [i for i in range(len(l)) if not l[i]]
    tmp = []
    td = 0
    for i, dd in enumerate(d):
      tmp.append(l[td:dd])
      td = dd + 1
      if i == len(d) - 1 and dd != len(l) -1:
        tmp.append(l[td:])
    for t in tmp:
      ans = process(t, ans)
  return ans

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict
from collections import deque
import itertools
import math

def readInt():
  return int(input())
def readInts():
  return list(map(int, input().split()))
def readChar():
  return input()
def readChars():
  return input().split()

n = readInt()
h = readInts()
ans = 0
while h.count(0)!=n:
  boo = 0
  for i in range(len(h)):
    if h[i]!=0:
      if boo==0:
        boo=1
        ans+=1
      h[i]-=1
    else:
      boo=0

print(ans)

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    N = int(input())
    hs = [int(item) for item in input().split()]

    cnt = 0
    flag = False
    while any(hs):
        # print(hs)
        for i in range(N):
            if hs[i] > 0:
                flag = True
                hs[i] -= 1
            else:
                if flag:
                    cnt += 1
                    flag = False
        else:
            if flag:
                cnt += 1
            flag = False

    print(cnt)



if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

ans = 0
while sum(h) > 0:
  flag = False
  for i in range(N):
    if flag:
      if h[i] == 0:
        flag = False
        ans += 1
      else:
        h[i] -= 1
        if i == N-1:
          ans += 1
    else:
      if h[i] > 0:
        h[i] -= 1
        flag = True
        if i == N-1:
          ans += 1
          flag = False
print(ans)

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.buffer.readline
from math import*
import copy


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

Submission status: Accepted

Solution code:
# 初期入力
import sys
input = sys.stdin.readline
N = int(input())
h_aft = list(map(int, input().split()))
h_bef =[0]*N

#
count =0
while sum(h_bef) < sum(h_aft):
    flag =[0]*(N+1)
    for i in range(N):
        if h_bef[i] <h_aft[i]:
            h_bef[i] +=1
            flag[i] =1
    for i in range(N+1):
        if flag[i] ==1 and flag[i+1] ==0:
            count +=1

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
"""
8
4 23 75 0 23 96 50 100
"""

n = int(raw_input())
ais = map(int, raw_input().split(' '))

def f(ais):
	r = [0]
	for i in range(len(ais)):
		## looking for max
		if len(r) % 2:	
			if (ais[i-1] if i-1 >= 0 else 0) <= ais[i] > (ais[i+1] if i+1 < len(ais) else 0): 
				r.append(ais[i])
		else:
		## looking for min
			if (ais[i-1] if i-1 >= 0 else 0) >= ais[i] < (ais[i+1] if i+1 < len(ais) else 1000): 
				r.append(ais[i])

	t = 0
	for i in range(0,len(r),2):
		if i + 1 < len(r):
			u,v = r[i],r[i+1]
			t += v-u
	
	return t if len(ais) > 1 else ais[0]
print f(ais)

Submission status: Accepted

Solution code:
n=int(input())
lst=list(map(int,input().split()))
ans=max(lst)
for i in range(ans):
    for j in range(1,n-1):
        if lst[j]==0 and lst[j-1]!=0:
            ans+=1
    if lst[-1]==0 and lst[-2]==0:
        ans-=1
    for j in range(n):
        if lst[j]!=0:
            lst[j]-=1
print(ans)

Submission status: Accepted

Solution code:
def cz(A):
    N = len(A)
    F = [0]
    for i in range(N):
        if A[i] == 0:
            F.append(0)
        else:
            F.append(1)

    res = 0
    for i in range(N):
        if F[i] == 0 and F[i+1] == 1:
            res += 1

    return(res)

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

cnt = 0
while sum(H) > 0:
    cnt += cz(H)
    for i in range(N):
        if H[i] == 0:
            continue
        else:
            H[i] -= 1

print(cnt)

Submission status: Accepted

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

count = 0
while not all([x <= 0 for x in h]):
    while h[0] == 0:
        del h[0]
    subcount = 0
    for i in range(len(h)-1):
        if (h[i] > 0 or h[i+1] > 0) and h[i]*h[i+1] == 0:
            subcount += 1
    h = [x - 1 if x - 1 >= 0 else 0 for x in h]
    subcount = subcount // 2 + 1
    count += subcount

print(count)

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    h=[int(i) for i in input().split()]
    min_h = min(h)
    h_sub = [int(i)-min_h for i in h]
    res = min_h
    i = 0
    while max(h_sub)!=0:
        if(h_sub[i]>0):
            res+=1
            j = i
            while(j<n and h_sub[j]!=0):
                h_sub[j]-=1
                j+=1
        else:
            i+=1
        if(i>=n):
            break
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

H_=H.copy()

def get_zero_idx(H_):
    zero_idx = []
    for i in range(len(H_)):
        if H_[i] == 0:
            zero_idx.append(i)
    return zero_idx

zero_idx = get_zero_idx(H_)
# print(zero_idx)
# print(H_)

ans=0

while True:
    zero_idx = get_zero_idx(H_)
#     print(zero_idx)
    #終了判定(全てのHが所定の高さを満たす)
    if len(zero_idx) == len(H_):
        break

    if len(zero_idx)==0:
        s=0
        e=N
        if s<e:
            for _ in range(s,e):
                H_[_]=H_[_]-1   
            ans +=1

    elif len(zero_idx)>=1:
        s=0
        e=zero_idx[0]
        if s<e:
            for _ in range(s,e):
                H_[_]=H_[_]-1   
            ans +=1
#         print('b',s,e,H_)

        for _ in range(1,len(zero_idx)):
            s=zero_idx[_-1]+1
            e=zero_idx[_]
            if s<e:
                for _ in range(s,e):
                    H_[_]=H_[_]-1   
                ans +=1
#             print('c',s,e,H_)

        s=zero_idx[-1]+1
        e=N
        if s < e:
            for _ in range(s,e):
                H_[_]=H_[_]-1   
            ans +=1
#         print('b-',s,e,H_)

print(ans)

Submission status: Accepted

Solution code:
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt
from copy import deepcopy

def main():
    num = int(input())
    h = list(map(int, input().split()))
    ans = 0

    m=0
    for i in range(0,num):
        for j in range(m,num):
            if j == num-1:
                ans += h[j]
                flag = True
                break
            if h[j] > h[j+1]:
                ans += h[j] - h[j+1]
                m = j
        if flag:
            break
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans=0
for i in range(hm,0,-1):
    t=1
    for j in range(n):
        if h[j]>=i:
            ans+=t
            t=0
        else:
            t=1

print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
h = LIST()
ans = 0
tmp = 0
for i in range(n):
    if tmp < h[i]:
        ans += h[i] - tmp
    tmp = h [i]
print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
h = LIST()
ans = 0
tmp = 0
for i in range(n):
    for j in range(n):
        if tmp < h[i]:
            ans += h[i] - tmp
        tmp = h [i]
print(ans)