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:
def I(): return int(input())
def LI(): return list(map(int,input().split()))
N = I()
h = LI()+[0]
ans = 0
while 1:
    if max(h)==0:
        break
    ans += 1
    for i in range(N+1):
        if h[i]>0 and h[i+1]==0:
            h[i] -= 1
            break
        h[i] = max(h[i]-1,0)
print(ans)

Submission status: Accepted

Solution code:
def I(): return int(input())
def LI(): return list(map(int,input().split()))
N = I()
h = LI()+[0]
ans = 0
while 1:
    if max(h)==0:
        break
    ans += 1
    for i in range(N+1):
        h[i] -= 1
        if h[i]>=0 and h[i+1]<=0:
            break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
last = li[0]
count = li[0]

for i,l in enumerate(li):
    if i == 0:
        continue
    if l > last:
        count += l - last
    last = l
print(count)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
all_li = [li]

def conv(target_list):
    count = 0
    flag = True
    while flag:
        all_tmp = []
        for each_list in target_list:
            tmp = []
            m = min(each_list)
            count += min(each_list)
            #print(each_list)
            for e in each_list:
                if e-m != 0:
                    tmp.append(e-m)
                else:
                    if tmp:
                        all_tmp.append(tmp)
                        tmp = []
            if tmp:
                all_tmp.append(tmp)
        if len(all_tmp) == 0:
            flag = False
        target_list = all_tmp
    return count

print(conv(all_li))

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
maxh = max(h)
ans = 0
for j in range(maxh):
    countnow = False
    for i in range(n):
        if h[i] != 0:
            h[i] -= 1
            if not countnow:
                ans+=1
                countnow=True
        else:
            countnow = False
print(ans)

Submission status: Accepted

Solution code:


def solve():

    N = int(input())
    Hlist = list(map(int, input().split()))
    ans = 0

    while True:

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


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

ans = 0
for i in range(1,max(h)+1):
  ls = [0]*N
  flag = 0
  
  for n in range(N):
    if h[n] >= i:
      ls[n] = 1
  
  for n in range(N):
    if ls[n] ==1:
      if flag == 0:
        ans += 1
        flag = 1
    else:
      flag = 0
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
#a,b=map(int,input().split())
hl=list(map(int,input().split()))
#l=[list(map(int,input().split())) for i in range(n)]

mx=max(hl)
count=0
for height in range(1,mx+1):
    i=0
    while i<n:
        if hl[i]>=height:
            count+=1
            while i<n and hl[i]>=height :
                i+=1
        else:
            i+=1

print(count)

Submission status: Accepted

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

H=np.array(H)
ans=0
for i in range(H.max()):
    #print(H)
    count=0
    now=(H[0]==0)
    if not now:
        count+=1
    for j in range(1,N):
        if now:
            if H[j]!=0:
                count+=1
        
        now=(H[j]==0)
    ans+=count
    #print(count)
    H[H>0]-=1
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = ir()
H = np.array(lr())
Hdiff = np.diff(H)
Hdiff = Hdiff[Hdiff>0]
answer = H[0] + Hdiff.sum()
print(answer)

Submission status: Accepted

Solution code:
N = int(input())
h = [0] + list(map(int, input().split())) + [0]
counter = 0
fin = [0 for _ in range(N+2)]

while not h == fin:
    for i in range(1, N+2):
        if h[i] == 0 and not h[i-1] == 0:
            counter += 1
    for i in range(1, N+1):
        if not h[i] == 0:
            h[i] -= 1


print(counter)

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
#from itertools import combinations # (string,3) 3回
#from collections import deque
#import collections.defaultdict
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

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

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    H = readInts()
    allzero = False
    cnt = 0
    while True:
        for i in range(n):
            allzero = True
            if H[i] > 0:
                allzero = False
                left = i
                break
                # ここで左がわを決めておく
        #print(left)
        if allzero:
            print(cnt)
            exit()
        i = left
        right = left
        while i < n and H[i] > 0:
            right += 1
            i += 1
            if i >= n:
                break
        for j in range(left,right):
            #print(j)
            H[j] -=1
        #print(*H)
        cnt += 1

if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
# https://drken1215.hatenablog.com/entry/2019/03/03/150200
import sys

sys.setrecursionlimit(10 ** 5 + 10)


def input(): return sys.stdin.readline().strip()


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

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

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


resolve()

Submission status: Accepted

Solution code:
import numpy as np


def RunLengthEncoding(S):
    res = []
    N = len(S)
    i = 0
    while i < N:
        cnt = 1
        while i < N - 1 and S[i] == S[i + 1]:
            cnt += 1
            i += 1
        res.append(S[i])
        i += 1
    return res


N = int(input())
A = np.array(input().split(), dtype=int)

ans = 0
flower = np.zeros_like(A)
for i in range(1, A.max() + 1):
    f = flower.copy()
    f[A >= i] = 1
    RLE = RunLengthEncoding(f)
    ans += sum(RLE)

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
        mi = min([h for _, h in g])
        ans += mi
        for i, _ in g:
            H[i] -= mi

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int,input().split()))
cnt = 0
indL = -1
for i in range(indL+1,N):
    if H[indL+1]==0:
        indL += 1
        continue
    else:break
while indL<N-1:
    hmin = 1000
    indR = N
    for i in range(indL+1,N):
        if H[i]==0:
            indR = i
            break
        else:
            hmin = min(hmin,H[i])
    for i in range(indL+1,indR):
        H[i] -= hmin
    cnt += hmin
    for i in range(indL+1,N):
        if H[indL+1]==0:
            indL += 1
            continue
        else:break
print(cnt)

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
        mi = min([h for _, h in g])
        ans += mi
        for i, _ in g:
            H[i] -= mi

print(ans)

Submission status: Accepted

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

h = [0]*N
ans = 0
while any(a<b for a,b in zip(h,H)):
    ans += 1
    l = r = -1
    for i in range(N):
        if l < 0:
            if h[i]==H[i]: continue
            l = i
        else:
            if h[i]<H[i]: continue
            r = i
            break
    else:
        r = N
    for i in range(l,r):
        h[i] += 1
print(ans)

Submission status: Accepted

Solution code:
def main():
    _ = int(input())
    H = [int(i) for i in input().split()]
    cur_h = 0
    ans = 0
    for h in H:
        if cur_h < h:
            ans += h - cur_h
        cur_h = h
    print(ans)


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):
    while True:
        if H[i] == 0:
            break

        for j in range(i+1, N):
            if H[j] == 0:
                break

            H[j] -= 1
        H[i] -= 1
        ans += 1
ans += max(0, H[-1])
print(ans)

Submission status: Accepted

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

h += [0]
prev = 0
imos = [None] * (N + 1)
for i in range(N + 1):
    imos[i] = h[i] - prev
    prev = h[i]
    
# print("#", h)
# print("#", imos)
ans = sum([int(item > 0) * item for item in imos])
print(ans)

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    H = [int(h) for h in input().split()]
    finished = [False] * (N + 1)
    finished[N] = True
    for i in range(N): 
        if H[i] == 0: finished[i] = True
    ch = [0] * N
    count = 0
    while False in finished:
        i = 0
        while i < N:
            if not finished[i]:
                count += 1
                start = i
                end = i + 1
                index = i
                while end <= N:
                    if not finished[end]:
                        end += 1
                    else:
                        for j in range(start, end):
                            ch[j] += 1
                            if ch[j] == H[j]:
                                finished[j] = True
                        break
                i = end
                #print(ch)
            else: i += 1
    print(count)

    return 0

if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
n= int(input())
lis = list(map(int,input().split()))
ans = 0
status = 0
for high in range(1,101):
    for i in range(n):
        if lis[i] >= high:
            status = 1
        else:
            if status == 1:
                ans += 1
                status = 0
    ans += status
    status = 0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split())) + [0]
ans = 0
while sum(A) > 0:
    f = 0
    for k in range(N+1):
        if A[k] > 0:
            f = 1
            A[k] -= 1
        elif A[k] == 0:
            ans += f
            f = 0
#    print(A)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
i = 0
ctr = 0
done = False

while sum(H) > 0:
    
    if H[i] > 0:
        done = True 
        H[i] -= 1
    else: 
        if done:
            ctr += 1
            done = False
    i += 1
    if i == N:
        i = 0
        if done:
            ctr += 1
            done = False

if done:
    ctr += 1
print(ctr)

Submission status: Accepted

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

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

def main():
    mod=10**9+7
    N=I()
    h=LI()
    ans=0
    now=0
    for i in range(N):
        nxt=h[i]
        ans+=max(nxt-now,0)
        now=nxt

    print(ans)
    

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int,input().split()))
m = max(h)
ans = 0
for i in range(1,m+2):
    for j in range(n):
        if j == 0:
            if h[0] >= i:
                p = 0
            elif h[0] < i:
                p = 1
        if j == n-1:
            if p == 0:
                ans += 1
            if p == 1 and h[j] >=i:
                ans += 1
        else:
            if p == 0:
                if h[j] < i:
                    p = 1
                    ans += 1
            if p == 1:
                if h[j] >= i:
                    p = 0
        
print(ans)

Submission status: Accepted

Solution code:
def Grand_Garden(n, hl):

    ans = 0
    maxlen = max(hl)
    for i in range(maxlen):
        for w in range(n):
            if hl[w] == 0:
                continue
            elif w+1 == n:
                ans += 1
                hl[w] -= 1
                continue
            elif hl[w+1] == 0:
                ans += 1
            hl[w] -= 1

    return ans

def main():
    n = int(input())
    hl = list(map(int, input().split()))
    print(Grand_Garden(n, hl))

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):
    while True:
        if H[i] <= 0:
            break

        base = H[i]
        for j in range(i+1, N):
            # 花の高さが0なら初期のまま
            if H[j] <= 0:
                break

            # 次の花(j+1)が基準の花(j)以下か
            if base <= H[j]:
                H[j] -= 1
            else:
                break
        H[i] -= 1
        ans += 1

ans += max(0, H[-1])
print(ans)

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] == 0:
        continue

    while True:
        base = H[i]
        for j in range(i+1, N):
            # 花の高さが0なら初期のまま
            if H[j] <= 0:
                break

            # 次の花(j+1)が基準の花(j)以下か
            if base <= H[j]:
                H[j] -= 1
            else:
                break
        H[i] -= 1
        ans += 1

        if H[i] <= 0:
            break

ans += max(0, H[-1])
print(ans)

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

Submission status: Accepted

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

ans = 0
while sum(h) != 0:
    for i in range(N):
        if h[i] > 0:
            st = i
            break
    en = N
    for i in range(st+1, N):
        if h[i] <= 0:
            en = i
            break
    for i in range(st, en):
        h[i] -= 1
    ans += 1

print(ans)

Submission status: Accepted

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

x = [0 for i in range(N)]

number_of_times = 0

def compare_add():
    added_or_not = False
    index = None
    for i in range(len(x)):
        if index == None:
            if x[i] != h[i]:
                x[i] += 1
                added_or_not = True
                index = i
        else:
            if x[i] != h[i]:
                if i != index + 1:
                    return True
                x[i] += 1
                index = i
    return added_or_not
    
while True:
    y = compare_add()
    if y == False:
        break
    number_of_times += 1

print(number_of_times)

Submission status: Accepted

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

count = min(H)
H = [h - count for h in H]
while any(0 < h for h in H):
    l = 0
    while H[l] == 0:
        l += 1
    r = l + 1
    while r < N and 0 < H[r]:
        r += 1

    count += 1
    H = [H[i] - (1 if l <= i < r else 0) for i in range(N)]
print(count)

Submission status: Accepted

Solution code:
n = int(input())
h_list = list(map(int, input().split()))
ans = 0
while True:
    start = 0
    if all([h == 0 for h in h_list]):
        break
    while h_list[start] == 0:
        start += 1
    end = start
    for i in range(start, n):
        if h_list[i] == 0:
            end = i - 1
            break
    else:
        end = n - 1
    mi = min(h_list[start:end + 1])
    for i in range(start, end + 1):
        h_list[i] -= mi
    ans += mi
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# solution

import fractions

data = int(input())
nim = list(map(int, input().split()))
result = 0

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

print(result)

Submission status: Accepted

Solution code:
a = [input().split() for m in range(2)]
z=a[1]
b=[int(s) for s in z]
c=int(a[0][0])
cnt=0
while True:
    for i in range(c):

        if b[i]!=0:

            for j in range(i,c):

                if b[j]==0:
                    break

                b[j]-=1
                j+=1

            cnt+=1
            break

    if sum(b)==0:
        break
    
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

from collections import deque

d = deque()

d.append(h)
ans = 0

while d:
    p = d.pop()
    if p:
        p_min = min(p)

        p0 = []
        pp = []

        for i in range(len(p)):
            pp.append(p[i]-p_min)
            if p[i] - p_min == 0:
                p0.append(i)
        ans += p_min
        '''
        print(ans)
        print(pp)
        print(p0)
        '''
    
        for i in range(len(p0)-1):
            d.append(pp[p0[i]+1:p0[i+1]]) 
        d.append(pp[:p0[0]])
        d.append(pp[p0[len(p0)-1]+1:])

print(ans)

Submission status: Accepted

Solution code:
def main():
  N = int(input())
  H = [int(_) for _ in input().split()]
  a = [0] * N
  a[0] = H[0]
  for i in range(N-1):
    if H[i+1] > H[i]:
      a[i+1] += a[i] + H[i+1] - H[i]
    else:
      a[i+1] = a[i]
  print(a[N-1])
  return

if __name__ == '__main__':
  main()

Submission status: Accepted

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

ans = 0
ans += l[0]

for i in range(1,N):
    if l[i-1] >= l[i]:
        continue
    else:
        ans += l[i] - l[i-1]

print(ans)

Submission status: Accepted

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

def mizu(h):
  if h==[]:
    return 0
  else:
    t=min(h)
    f=[]
    for i in range(len(h)):
      f.append(h[i]-t)
    u=f.index(0)
    return mizu(f[:u])+mizu(f[u+1:])+t


print(mizu(h))

Submission status: Accepted

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

ans = 0
f = [0] * n
while True:
    flg = 0
    for i, ff in enumerate(f):

        if ff < h[i]:
            f[i] += 1
            flg = 1
            if i == n-1:
                ans += 1
        else:
            if flg == 1:
                ans += 1
                flg = 0

    if f == h:
        print(ans)
        break

Submission status: Accepted

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

Submission status: Accepted

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

def func(l):
    global ans
    if type(l) is int:
        ans += l
        return
    if l == []:
        return
    m = min(l)
    ans += m
    l = list(map(lambda x: x-m, l))
    i = l.index(0)
    if i >=  0:
        func(l[:i])
        func(l[i+1:])

func(h)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())

def split_by_zero(l):
    if 0 not in l:
        return [l]
    ret_l = []
    while 0 in l:
        idx = l.index(0)
        if l[:idx]:
            ret_l.append(l[:idx])
        l = l[idx+1:]
    if l:
        ret_l.append(l)
    return ret_l

def main():
    
    H = list(map(int, input().split()))
    H = split_by_zero(H)
    que = deque(H)

    cnt = 0
    while que:
        q = que.popleft()
        q = [i-1 for i in q]
        cnt += 1
        q = split_by_zero(q)
        if q:
            for qq in q:
                que.append(qq)
    
    print(cnt)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

#116_c
counter = 0

def mizu(h):
    global counter
    if len(h) == 0:
        return
    elif len(h) == 1:
        counter += h[0]
        return
    else:
        m = min(h)
        counter += m
        h = [i-m for i in h]
        while 0 in h:
            mizu(h[:h.index(0)])
            h = h[h.index(0)+1:]
        mizu(h)

if __name__ == "__main__":
    N = int(input())
    h = [int(i) for i in input().split()]
    mizu(h)
    print(counter)

Submission status: Accepted

Solution code:
n = int(input())

A = list(map(int, input().split()))
t = 0

while len(set(A)) != 1 or max(A) != 0:
    for i in range(n):
        if A[i] != 0:
            break
    B = A[i:]
    for j in range(i, n):
        if A[j] != 0:
            A[j] -= 1
        else:
            break
    t += 1

print(t)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

def main():
    cnt = 0
    for _ in range(max(h)):
        flag = 0
        for i in range(n):
            if not h[i]: flag = 0
            else:
                if not flag:
                    flag = 1
                    cnt += 1
                h[i] -= 1
    print(cnt)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int,input().split()))
N+=1
arr.append(0)
now = 0
min = 100
count = 0

while True:
  min = 100
  if arr[now] == 0:
    now += 1
    if now == N:
      break
  else:
    for i in range(now,N):
      if min > arr[i]:
        if arr[i] == 0:
          last = i
          for j in range(now,i):
            arr[j] = arr[j] - min
          count+=min
          break
        else:
          min = arr[i]
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from queue import Queue

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

que = Queue()
que.put(H)
ans = 0
while not que.empty():
    H = que.get()
    minh = min(H)
    H = [h - minh for h in H]
    ans += minh
    res = []
    for h in H:
        if h > 0:
            res.append(h)
        elif h == 0 and len(res) > 0:
            que.put(res)
            res = []
    if len(res) > 0:
        que.put(res)
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
ary1 = list(map(int,input().split()))
ary2 = ary1
ans = 0
#i=0の場合はここに書く。
def f_state(k):
  state1 = 1 if k>0 else 0
  return(state1)

for i in range(10000):
#  num_switch = 0
  state1 = f_state(ary2[0])
  ary2[0] = max([0,ary2[0]-1])
  if state1 ==1:
    ans +=1
  for i in range(1,N):
    if f_state(ary2[i]) == 1 and state1 ==0:
      ans +=1
    state1 = f_state(ary2[i])
    ary2[i] = max([0,ary2[i]-1])
  if sum(ary2)==0:
    break

print(ans)

Submission status: Accepted

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

ans = 0
ans += A[0]
for i in range(1,N):
  if A[i-1] >= A[i]:
    continue
  else:
    ans += A[i]-A[i-1]

print(ans)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    H=[int(_) for _ in input().split()]
    cunt_p=0
    while sum(H):
        in_p=False
        for i in range(n):
            if H[i]>0:
                H[i]-=1
                if in_p==False:
                    cunt_p+=1
                    in_p=True
            elif H[i]==0 and in_p:
                in_p=False
    print(cunt_p)
                



if __name__=='__main__':
    main()

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n=int(input())
    H=[int(_) for _ in input().split()]
    cunt_p=0
    while sum(H):
        in_p=False
        for i in range(n):
            if H[i]>0:
                H[i]-=1
                if in_p==False:
                    cunt_p+=1
                    in_p=True
            elif H[i]==0 and in_p:
                in_p=False
    print(cunt_p)

if __name__=='__main__':
    main()

Submission status: Accepted

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

count = 0
while True:
    ### 草むらの端から端まで調べる
    ### 0 で区切られている1以上の箇所がn個 を調べる
    num_block = 0
    for i in range(len(h)):
        if h[i] > 0:
            if i == 0 or h[i - 1] == 0:
                num_block += 1
    ### n が0なら終了
    if num_block == 0:
        break
    ### count に n を足す
    count += num_block

    ### 1以上のh の要素 -1 する
    ### 0 の要素はmax(0 - 1, 0) = 0 となる
    h = list(map(lambda x: max(x - 1, 0), h))

print(count)

Submission status: Accepted

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

ans = 0
H += [0]
for i in range(1, 101):
    for j in range(N):
        if H[j] >= i and H[j+1] < i:
            ans += 1
print(ans)

Submission status: Accepted

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

a = [0]*N
lst = [0]*(N+1)
cnt = 0

while h != a:
    for i in range(N):
        if h[i] > a[i]:
            h[i] -= 1
            lst[i] = 1
        else:
            h[i] -= 0
            lst[i] = 0
    for j in range(N):
        if lst[j]*lst[j+1] == 0 and lst[j] == 1:
            cnt += 1
        else:
            cnt += 0

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
H = [int(h) for h in input().split()]
az = [0]*N
ans = 0
while H != az:
    has_found = False
    for i in range(N):
        if H[i] == 0:
            has_found = False
            continue
        if not has_found:
            has_found = True
            ans += 1
        H[i] -= 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=0
while True:
    l,m=-1,-1
    for i in range(n):
        if h[i]!=0:
            l=i
            break
    if l==-1:
        break
    if l==n-1:
        ans+=h[l]
        break
    r=l
    for i in range(l,n):
        if h[i]==0:
            break
        else:
            r=i
            if m==-1:
                m=h[i]
            else:
                m=min(m,h[i])

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

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
cnt = 0
while max(H) > 0:
    m = max(H)
    mi = H.index(m)

    # right
    for i in range(mi+1, N):
        m = min(m, H[i])
        H[i] = max(0, H[i]-m)

    m = H[mi]
    # left
    for i in range(mi-1, -1, -1):
        m = min(m, H[i])
        H[i] = max(0, H[i]-m)

    cnt += H[mi]
    H[mi] = 0
    # print(H)
print(cnt)

Submission status: Accepted

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

ans = 0
for m in range(1, M + 1):
    i = 0
    while i < N:
        if H[i] >= m:
            ans += 1
            while H[i] >= m:
                i += 1
                if i >= N:
                    break
        else:
            i += 1

print(ans)

Submission status: Accepted

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

mins = min(x)
cnt = mins
x -= mins

while sum(x) != 0:
    mask = [1 if xi > 0 else 0 for xi in x]
    x -= mask
    
    for i in range(1, n):
        if mask[i] == 1 and mask[i-1] == 1:
            mask[i-1] = 0

    cnt += sum(mask)
    
print(cnt)

Submission status: Accepted

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

ans=0
active=0
for x in h:
    if active>=x:
        active=x 
    else:
        ans+=x-active 
        active=x 
print(ans)