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()))
print(sum(max(0,h[i+1]-h[i])for i in range(n-1))+h[0])

Submission status: Accepted

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

Total = 0
Fin = [0] * N
while H != Fin:
    Group = []
    MinH = 1000
    for i in range(N):
        if H[i] != 0:
            Group.append(i)
            MinH = min(MinH, H[i])
            if i == N - 1:
                Total += MinH
                for g in Group:
                    H[g] -= MinH
        elif Group:
            Total += MinH
            for g in Group:
                H[g] -= MinH
            Group = []
            MinH = 1000

print(Total)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

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

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

print(sum(ans) + h[0])

Submission status: Accepted

Solution code:
n = int(input())
cnt = 0

thre = 0
for h in (int(h) for h in input().split()):
    cnt += (h - thre) if h > thre else 0
    thre = h

print(cnt)

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

Submission status: Accepted

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

ans = 0
while True:
    if h.count(0) == N:
        break
    started = False
    for i, n in enumerate(h):
        if n != 0:
            if not started:
                ans += 1
                started = True
            h[i] -= 1
        else:
            started = False
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N=int(input())
inp=input().split()
h=[int(i) for i in inp]
ans=0
h.append(0)
for i in range(N):
    diff=h[i]-h[i+1]
    if(diff>0):
        ans+=diff
ans+=min(h)
print(ans)

Submission status: Accepted

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

ans = 0
while max(h):
    current_hight = 0
    for i in range(len(h)):
        if not h[i]:
            if not current_hight:
                continue
            else:
                break
        elif h[i] < current_hight:
            break
        else:
            current_hight = h[i]
            h[i] -= 1
    ans += 1

print(ans)

Submission status: Accepted

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

ans = 0
while len(L) :
    if not L[0] :
        L.pop(0)
    else :
        ans += 1
        for i in range(len(L)) :
            if L[i] :
                L[i] -= 1
            else :
                break      
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(int(i) for i in input().split())
ct = 0
query = [0]*n

for i in range(n):
 while h[i] !=0:
  flag = 0
  ct += 1
  for j in range(n):
   try:
    if h[i+j] >0:
     h[i+j] -= 1
    else:
     break
   except:
     break
print(ct)

Submission status: Accepted

Solution code:
def dfs(c):
    if c==[] or c==[0]:
        return 0
    mc = min(c)
    ans = mc
    for i in range(len(c)):
        c[i] -= mc
    st,i = 0,0
    while i<c.count(0):
        to = c.index(0,i)
        ans += dfs(c[st:to])
        st = to+1
        i += 1
    ans += dfs(c[st:])
    return ans

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

Submission status: Accepted

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

feed_count = 0
while True:
    flower_fed_exists = False
    for i in range(len(heights)):
        if heights[i] > 0:
            heights[i] -= 1
            if flower_fed_exists == False:
                flower_fed_exists = True
                feed_count += 1
        else:
            if flower_fed_exists == True:
                break
    if flower_fed_exists == False:
        print(feed_count)
        break

Submission status: Accepted

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

count = 0
for _ in range(101):
    flag = True
    for i in range(n):
        if H[i] > 0:
            H[i] -= 1
            if flag:
                count += 1
                flag = False
        else:
            flag = True

print(count)

Submission status: Accepted

Solution code:
N=input()
h=list(map(int,input().split()))
ans=0
def solve(ar):
    global ans
    if(sum(ar)==0):
        return
    if(0 in ar):
        i=ar.index(0)
        if(i-1>=0):
            solve(ar[:i])
        solve(ar[i+1:])
        return 
    s=True
    while(s):
        ans+=1
        for i in range(len(ar)):
            ar[i]=ar[i]-1
            if(ar[i]<=0):
                s=False
    solve(ar)

solve(h)
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0
cur = 0

for n, m in zip(a, a[1:]):
    if cur < n:
        cur = n
    if cur > m:
        ans += cur - m
        cur = m

print(ans)

Submission status: Accepted

Solution code:

def remove_lowest_step(flowers):
    flowers = [f-1 if f > 0 else 0 for f in flowers]
    return flowers


def count_lowest_step(flowers):
    binary_flowers = [1 if f > 0 else 0 for f in flowers]
    merge_flowers = binary_flowers[0:1]

    for b in binary_flowers[1:]:
        if merge_flowers[-1] == b:
            continue
        merge_flowers.append(b)

    return sum(merge_flowers)


def check_end(flowers):
    if sum(flowers) == 0:
        return True
    return False


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

    return n, flowers


n, flowers = input_flowers()
count = 0

while not check_end(flowers):
    count += count_lowest_step(flowers)
    flowers = remove_lowest_step(flowers)

print(count)

Submission status: Accepted

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

for i in range(n):
    h = H[i]
    if h > now_h:
        cou += (h - now_h)
        now_h = h
    elif now_h > h:
        now_h = h

print(cou)

Submission status: Accepted

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

ans = 0
while sum(H) != 0:
    l = 0
    while l < N and H[l] == 0: l += 1
    r = l
    while r < N and H[r] >= 1: r += 1
    for i in range(l, r):
        H[i] -= 1
    ans += 1

print(ans)

Submission status: Accepted

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

def solve(h):
    if len(h)==1:
        return h[0]
    elif len(h)==0:
        return 0
    for i in range(len(h)):
        if h[i]==0:
            return solve(h[0:i]) + solve(h[i+1:len(h)])
    m = min(h)
    h = [hi-m for hi in h]
    return m + solve(h)

print(solve(h))

Submission status: Accepted

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

ans = 0
start = 0
end = 0
while any(h):
    for i in range(N):
        if h[i]!=0:
            start = i
            break
    end = N-1
    for i in range(start,N):
        if h[i]==0:
            end = i-1
            break
    m = min(h[start:end+1])
    for i in range(start,end+1):
        h[i] = h[i] - m
    ans += m

print(ans)

Submission status: Accepted

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

    ans = f(N, h)
    print(ans)


def f(N, h):
    """
    1 <= N <= 100
    0 <= hi <= 100
    整数 l,r を指定する。
    l ≦ x ≦ r を満たすすべての xに対して、花 xの高さを 1高くする。
    条件を満たすための最小の「水やり」操作の回数を求めてください。

    広い範囲の花を高くしたいが、hk を超えてはいけない
    愚直にやる
        各kについてh回水をやる: sum(h)

    広範囲にやりすぎて回数が増えることはあるのか？
    間が多い
        0: 1 1 2 2 1
        1: 0 0 1 1 0
        2: 0 0 0 0 0
    端が多い
        0: 2 2 1 1 2
        1: 1 1 1 1 2, 1 1 0 0 1
        2: 0 0 0 0 1, 0 0 0 0 1
        3: 0 0 0 0 0
    跨ぐ
        0: 1 2 2 1 2
        1: 0 1 1 1 2, 0 1 1 0 1
        2: 0 0 0 0 1, 0 0 0 0 1
        3: 0 0 0 0 0

    愚直にやるでいいのでは
    """
    h = h[:]
    ans = 0
    while True:
        water = False
        for k in range(N):
            if h[k] == 0:
                if water:
                    break
            else:
                h[k] -= 1
                water = True

        if not water:
            break
        ans += 1

    return ans


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
while True:
    left = 0
    right = 0
    num_zero = 0
    for i in range(N):
        if h[i] == 0:
            if left != right:
                h[left:right] = [v - 1 for v in h[left:right]]
                ans += 1
            left = i + 1
            right = i + 1
            num_zero += 1
        else:
            right += 1
    if num_zero == N:
        break
    if h[N-1] != 0:
        h[left:right] = [v - 1 for v in h[left:right]]
        ans += 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):
    if h[i] > h[i-1]:
        ans += h[i] - h[i-1]

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
Flowers = list(map(int, input().split(' ')))

Count = 0

while( max(Flowers) > 0 ):
    maxList = [ i for i, x in enumerate(Flowers) if x == max(Flowers) ]

    idx = maxList.pop(0)
    Flowers[idx] -= 1
    Count += 1
    while(len(maxList) > 0):
        next_idx = maxList.pop(0)
        if idx + 1 != next_idx:
            Count += 1
        idx = next_idx
        Flowers[idx] -= 1

print(Count)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for i in range(N):
    while h[i] != 0:
        if 0 in h[i:]:
            end = h[i:].index(0) + i
        else:
            end = N
        value = min(h[i:end])
        count += value
        
        for l in range(i, end):
            h[l] -= value
                
print(count)

Submission status: Accepted

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

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

if not len(h_list) == n :
    raise RuntimeError("Mismatch Length.")

count = 0
while True:
    h_max = max(h_list)

    if h_max == 0:
        break

    isMax_list = [h == h_max for h in h_list]

    for i, isMax in enumerate(isMax_list):
        if isMax:
            h_list[i] -= 1
            if i == 0 or not isMax_list[i-1]:
                count += 1

print(count)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int, input().split()))
prev = 0
ans = 0
for n in L:
    if prev <= n:
        prev = n
        continue
    ans += prev - n
    prev = n
ans += L[-1]
print(ans)

Submission status: Accepted

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-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 pf(s): return print(s, flush=True)


def main():
    n = I()
    a = LI()
    def f(a):
        if sum(a) == 0:
            return 0
        if min(a) == 0:
            i = a.index(0)
            return f(a[:i]) + f(a[i+1:])
        return f(list(map(lambda x: x-1, a))) + 1

    return f(a)



print(main())

Submission status: Accepted

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

res = 0
Flag = False
while True:
    if sum(h) == 0:
        break
    for i in range(N):
        if h[i]:
            Flag = True
            h[i] -= 1
            if i == N-1:
                res += 1
        else:
            if i-1 >= 0 and Flag:
                res += 1
            Flag = False
    Flag = False

print(res)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
h = np.array([0] + list(map(int, input().split())))
r = 0
while np.any(h>0):
    r += np.sum(np.logical_and(h[1:]>0, h[:-1]<=0))
    h -= 1
print(r)

Submission status: Accepted

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

def p(h):
  if h!=[]:
    minh=min(h)
    if len(h)==1:
      return h[0]
    else:
      mini=h.index(minh)
      return minh+p([i-minh for i in h[:mini]])+p([i-minh for i in h[mini+1:]])
  else:
    return 0

print(p(h))

Submission status: Accepted

Solution code:
def solve():
    n = int(input())
    hs = list(map(int, input().split(' ')))
    left = len(list(filter(lambda x: x != 0, hs)))
    count = 0
    while left != 0:
        start, length = tops(hs)
        for i in range(start, start+length):
            hs[i] -= 1
            if hs[i] == 0:
                left -= 1
        count += 1
    print(count)

def tops(hs):
    start = 0
    length = 1
    prev = hs[0]
    for i, h in list(enumerate(hs))[1::]:
        if prev < h:
            start = i
            length = 1
        elif prev == h:
            length += 1
        else:
            break
        prev = h
    return (start, length)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

ans = 0
pre = 0
for i in h:
    if pre < i:
        ans += i-pre

    pre = i

print(ans)
#copy code

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
ans = 0
def zero_spliter(L):
    cnt = 1
    for x in L:
        if x == 0:
            cnt += 1
    M = [[] for _ in range(cnt)]
    i = 0
    for x in L:
        if x != 0:
            M[i].append(x)
        else:
            i += 1
    for x in M:
        if x != []:
            yield x
ans = 0
def counter(L):
    global ans
    if L == []:
        pass
    if 0 in L:
        l = list(zero_spliter(L))
        for x in l:
            counter(x)
    else:
        M = [x-1 for x in L]
        ans += 1
        counter(M)
counter(L)
print(ans)

Submission status: Accepted

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

global count
count = 0


def f(l, r, arr):
    # l以上r未満の範囲で探索
    global count
    while len(arr[l:r]) > 0 and all(arr[l:r]) and l < r:
        # l からrまですべて1以上のときはデクリメントし続ける
        arr = [a-1 if l <= idx and idx < r else a for idx, a in enumerate(arr)]
        count += 1
    # ずべてが0だったら終了
    if sum(arr) == 0:
        return
    # l以上r未満のどっかに0が含まれるので、含まれない範囲を抽出
    if arr[0] > 0:
        # 左端がl、以後0が見つかったらr, なかったら右端
        r = arr.index(0) if 0 in arr else len(arr)
        f(0, r, arr)
    elif arr[0] == 0:
        # 左から数えて最初に1以上のインデックスがl、その次の0もしくは右端がr
        a, b = -1, len(arr)
        for idx, x in enumerate(arr):
            if x == 0 and a > 0:
                b = idx
                break
            if x > 0 and a < 0:
                a = idx
        f(a, b, arr)


f(0, n, h)
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
H=list(map(int,input().split()))
ans=0
while True:
    f=False
    g=False
    for i,h in enumerate(H):
        if h>0:
            if g==False:
                ans+=1
                g=True
            H[i]-=1
            f=True
        if h==0 and g==True:
            g=False
    if f==False:
        print(ans)
        break

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]

N = int(input())
H = [0] + inpl()
print(sum([max(0, H[i+1] - H[i]) for i in range(N)]))

Submission status: Accepted

Solution code:
import array
def count_mizuyari(l):
    count = 0
    arr = array.array('i',[0])
    arr.extend(l)
    for i in range(len(arr)-1):
        if(arr[i]==0 and arr[i+1]!=0):
            count += 1
    return count
    

def mizuyari2(l):
    count = 0
    stopper = 0

    #要素が全部0かどうかの判定
    for i in l:
        stopper+=i 
    if(stopper == 0):
        return 0 
    else:     
        count=count_mizuyari(l)
    for index,i in enumerate(l):           
        l[index]-=1
        if(l[index]<0):
            l[index]=0

    return count+mizuyari2(l)

N = int(input())
h = array.array('i')

li = input().rstrip().split()
for i in li:
    h.append(int(i))


print(mizuyari2(h))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
count = 0
num = int(input())
li = list(map(int,input().split()))
graph_li =[]
for i in range(1,101):
    graph = []
    for j in li:
        if j >= i:
            graph.append("1")
        else:
            graph.append("0")
    graph_li.append(graph[:])
for i in graph_li:
    if "1" in i:
        count += len(list(filter(lambda x:len(x)>0 ,"".join(i).split("0"))))
print (count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
h = list(map(int, input().split()))


def dfs(l, r):
    global h
    if l >= r:
        return 0
    for i in range(l, r):
        if h[i] == 0:
            return dfs(l, i) + dfs(i + 1, r)
    m = min(h[l:r])
    h = [h[i] - m * (l <= i < r) for i in range(N)]
    return m + dfs(l, r)
        

print(dfs(0, N))

Submission status: Accepted

Solution code:
#!/mnt/c/Users/moiki/bash/env/bin/python
N = int(input())
h = list(map(int, input().split()))

ans = 0
def rec(s): #str, 
    # print("s: ", s)
    global ans
    if len(s) == 0:
        return 
    elif len(s) == 1:
        ans += s[0]
        return 
    elif len(s) == 2:

        ans += max(s)
        return 

    min_ = min(s)
    l_idx = []
    for e,i in enumerate(s):
        if i == min_ : # and  (e != 0 and e!= (len(s)-1)) :
            l_idx.append(e)
        s[e] -= min_

    # print("\t after s:", s)
    # print("\t l_idx s:", l_idx)

    ans += min_

    pre_pos = 0

    for e,l in enumerate(l_idx):
        # print("\tpre_pos:i=> {}:{}".format(pre_pos, l))
        rec(s[pre_pos:l])

        pre_pos = l+1
    if l_idx[-1] != len(s)-1:
        # print("LAST ON: l_idx[-1]:last: ", l_idx[-1]+1, "list: ", s[l_idx[-1]+1:])
        # return 

                
        rec(s[l_idx[-1]+1:])
   

rec(h)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
flowers = list(map(int,("0 " + input()).split(' ')))
diff = []
ans = 0
for i in range(1,n + 1):
    diff.append(flowers[i] - flowers[i - 1])
# print(diff)
for d in diff:
    if d > 0:
        ans += d
print(ans)

Submission status: Accepted

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

ans = 0


def dfs(h):
    global ans
    if not h:
        return
    minh = min(h)
    ans += minh
    if minh != 0:
        for i in range(len(h)):
            h[i] -= minh
    while h[0] == 0 and not h:
        h = h[1:]
    while h[-1] == 0 and not h:
        h = h[:-1]
    pos_zero = []
    pos_zero.append(-1)
    for i in range(len(h)):
        if h[i] == 0:
            pos_zero.append(i)
    pos_zero.append(len(h))
    for i in range(len(pos_zero)-1):
        dfs(h[pos_zero[i]+1:pos_zero[i+1]])


dfs(H)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N=int(input())

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

def forwardpropagation(forwardlist):
  temp=forwardlist[0]
  calc=0
  for i in range(len(forwardlist)-1):
    calc=temp
    if temp>forwardlist[i+1]:
      temp=forwardlist[i+1]
    forwardlist[i+1]=max(0,forwardlist[i+1]-calc)
  return forwardlist

def backwardpropagation(backwardlist):
  templist=forwardpropagation(backwardlist[::-1])
  backwardlist=templist[::-1]
  return backwardlist

answer=0
count=0
while(True):
  if max(h)==0:
    print(answer)
    exit()
  maxindex=h.index(max(h))
  answer+=h[maxindex]
  h[maxindex:]=forwardpropagation(h[maxindex:])
  h[:maxindex+1]=backwardpropagation(h[:maxindex+1])
  h[maxindex]=0

Submission status: Accepted

Solution code:
N = int(input())

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

L = [l_0]

m = None

count = 0

while L != []:
	for l in L:
		lis = l
		m   = min(l)
		for i in range(len(l)):
			lis[i] = lis[i] - m
		count += m
		lis = "_".join( list(map(str , lis)) )
		if lis[0:1] == "0":
			lis = lis[2:]
		lis = lis.split("_0")
		while "" in lis:
			lis.remove("")
		for a in lis:
			if a[0:1] == "_":
				a = a[1:]
			a = list(map(int, a.split("_")))
			L.append(a)
		L.remove(l)
		while [] in L:
			L.remove([])
print(count)

Submission status: Accepted

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

stack = [H]
count = 0
while stack:
    lst = stack.pop()
    m = min(lst)
    count += m
    lst = [l-m for l in lst]
    prev = 0
    left = 0
    for i, h in enumerate(lst):
        if h == 0 and prev != 0: # H = {..., 0, *(left), *, ..., *, 0(right), ...} (*: 正数)
            stack.append(lst[left:i])
        elif h != 0 and prev == 0:
            left = i
            if i == len(lst)-1: # H = {..., 0, *(left)} (*: 正数)
                stack.append(lst[left:i+1])
        elif h != 0 and i == len(lst)-1: #(prev != 0) H = {..., 0, *(left), ..., *, *} (*: 正数)
            stack.append(lst[left:i+1])
        prev = h
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):
    ans += max(h[i]-h[i-1],0)
print(ans)

Submission status: Accepted

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

print(answer)

Submission status: Accepted

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

# Hを1段ずつ減らしていく.
# 値が正の区間 (left, right) が一つ見つかるごとに答えは1増える.

ans = 0
while sum(H) > 0:
    left = right = 0
    prev = 0
    segment = []
    for i, h in enumerate(H):
        if h == 0 and prev != 0: # H = {..., 0, *(left), *, ..., *, 0(right), ...} (*: 正数)
            right = i
            segment.append((left, right))
        elif h != 0 and prev == 0:
            left = i
            if i == N-1: # H = {..., 0, *(left)} (*: 正数)
                right = i + 1
                segment.append((left, right))
        elif h != 0 and i == N-1: #(prev != 0) H = {..., 0, *(left), ..., *, *} (*: 正数)
            right = i + 1
            segment.append((left, right))
        prev = h
    ans += len(segment)
    #  Hの値が正のところを1段減らす.
    for l, r in segment:
        for i in range(l, r):
            H[i] -= 1
print(ans)

Submission status: Accepted

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

hmin=min(h)
count=0
while hmin<10**6:
    flag=False
    for i in range(N):
        if h[i]<10**6:
            if flag==False:
                count+=hmin
                flag=True
    
            if h[i]==hmin:
                h[i]+=10**6
            else:
                h[i]-=hmin
        else:
            flag=False

    hmin=min(h)
print(count)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=0
while(sum(h) is not 0):
    is0 = True
    for i in range(n):
        if(h[i]==0):
            is0 =True
        else:
            if(is0):
                ans += 1
                is0=False
            h[i]-=1
print(ans)

Submission status: Accepted

Solution code:
import sys
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] > H[i - 1]:
        ans += H[i] - H[i - 1]
print(ans)

Submission status: Accepted

Solution code:
def c_grand_garden(N, H):
    ans = 0
    H.append(0)
    for k in range(N):
        ans += max(H[k] - H[k + 1], 0)
    return ans

N = int(input())
H = [int(i) for i in input().split()]
print(c_grand_garden(N, H))

Submission status: Accepted

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

ls = [s]
count = 0
while len(ls) != 0:
    l = ls.pop()
    mins = min(l)
    for i in range(len(l)):
        l[i] -= mins
    count += mins
    tmpl = []
    for i in l:
        if i == 0:
            if len(tmpl) != 0:
                ls.append(tmpl)
            tmpl = []
        else:
            tmpl.append(i)
    else:
        if len(tmpl) != 0:
            ls.append(tmpl)

print(count)

Submission status: Accepted

Solution code:
import numpy as np
n = int(input())
s = np.array(list(map(int, input().split())), dtype='int64')
count = 0
sidx = 0

ls = [s]
count = 0
while len(ls) != 0:
    l = ls.pop()
    mins = min(l)
    l -= mins
    count += mins
    tmpl = np.array([], dtype='int64')
    for i in l:
        if i == 0:
            if len(tmpl) != 0:
                ls.append(tmpl)
            tmpl = np.array([], dtype='int64')
        else:
            tmpl = np.append(tmpl, i)
    else:
        if len(tmpl) != 0:
            ls.append(tmpl)

print(int(count))

Submission status: Accepted

Solution code:
n = input()
result = input().split(" ")
count = 0

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


count = 0
def main(result):
    global count
    if len(result) == 0:
        count += 0
    elif len(result) == 1:
        count += result[0]
    elif 0 in result:
        index_num = result.index(0)
        main(result[:index_num])
        main(result[index_num+1:])
    else:
        min_num = min(result)
        count += min_num
        for i in range(len(result)):
            result[i] -= min_num
        main(result)

main(result)
print(count)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
def r_d(x):
  return [x[i] for i in range(len(x)) if i==0 or x[i]!=x[i-1]]
c=0
h=r_d(h)
while len(h)!=1:
  if h[0]>h[1]:
    c+=h[0]-h[1]
    h[0]=h[1]
  if h[len(h)-2]<h[len(h)-1]:
    c+=(h[len(h)-1]-h[len(h)-2])
    h[len(h)-1]=h[len(h)-2]
  for i in range(1,len(h)-1):
    if h[i]>h[i-1] and h[i]>h[i+1]:
      c+=h[i]-max(h[i-1],h[i+1])
      h[i]=max(h[i-1],h[i+1])
  h=r_d(h)
print(c+h[0])

Submission status: Accepted

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

while any(hhh):
    watering = False

    for i in range(n):
        if hhh[i] == 0:
            watering = False
        else:
            hhh[i] -= 1
            if not watering:
                watering = True
                ans += 1
print(ans)

Submission status: Accepted

Solution code:
N, *H = map(int, open(0).read().split())
diff = [H[0]] + [abs(prv - cur) for prv, cur in zip(H, H[1:])] + [H[-1]]
print(sum(diff) // 2)

Submission status: Accepted

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

import numpy as np
import copy

h_np = np.array(copy.deepcopy(h))
length = len(h)

tmp_i = -1
count = 0
while(True):
    
    if np.sum(h_np>0) == 0:
        print(count)
        break
    
    for i in range(length):
        
        if h_np[i] > 0:
            if tmp_i == -1:
                tmp_i = i
            continue
        else:
            if tmp_i == -1:
                continue
            count += np.min(h_np[tmp_i:i])
            h_np[tmp_i:i] -= np.min(h_np[tmp_i:i])
            tmp_i = -1
    else:
        count += np.min(h_np[tmp_i:])
        h_np[tmp_i:] -= np.min(h_np[tmp_i:])
        tmp_i = -1

Submission status: Accepted

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


main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
ans = 0
while len(L) :
    if not L[0] :
        L.pop(0)
    else :
        ans += 1
        for i in range(len(L)) :
            if L[i] :
                L[i] -= 1
            else :
                break
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8

II = lambda: int(input())
MI = lambda: map(int, input().split())
MIL = lambda: list(MI())


def main():
    N = II()
    H = MIL()

    ans = 0
    prev = 0
    mode = True
    H.append(0)
    for h in H:
        if mode:
            if prev <= h:
                prev = h
            else:
                ans += prev
                mode = False
                prev = h
        else:
            if prev >= h:
                prev = h
            else:
                ans -= prev
                mode = True
                prev = h
    return ans


if __name__ == "__main__":
    print(main())