Problem:


Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq  k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N  \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221



Sample input:
4
1 2 2 1


Sample output:
2

Submission status: Accepted

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
ans = 0
def water(list):
    global ans
    if list == []:
        return 0

    if 0 in list:
        ind = list.index(0)
        left = list[:ind]
        right = list[ind+1:]
        water(left)
        water(right)

    else:
        mmin = min(list)
        ans += mmin
        for i in range(len(list)):
            list[i] = list[i] - mmin
        water(list)
water(h)
print(ans)

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))
b.insert(0, 0)
b.append(0)
d = 0
while True:
    for i in range(1, len(b)):
        if b[i] != 0 and b[i-1] == 0:
            for j in range(i, len(b)):
                if b[j] == 0 and b[j-1] != 0:
                    d += 1
                    for k in range(i, j):
                        b[k] -= 1
                    break
    if sum(b) == 0:
        break
print(d)

Submission status: Accepted

Solution code:
N = int(input())

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

def sink(mountain):
    if mountain == []:
        return 0

    takes = 0

    min_h = min(mountain)

    sank = [i - min_h for i in mountain]

    takes += min_h

    small_mnt = []

    for i, height in enumerate(sank):
        if height > 0:
            small_mnt.append(height)
        else:
            takes += sink(small_mnt)
            small_mnt.clear()

    takes += sink(small_mnt)

    return takes

print(sink(h))

Submission status: Accepted

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

count =0
while True:
  is_all_0 = True
  left = 0  
  for i in range(len(h)):
    if h[i]>0:
      is_all_0 = False
      left = i
      break
  
  if is_all_0:
    break
  
  right = left
  is_all_0 = True
  for j in range(left,len(h)):
    if h[j]>0:
      right=j
    else:
      break
    
  for i in range(left,right+1):
    h[i]-=1
  count+=1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int, input().split()))
ans = 0
while 1:
    if li == [0] * n:
        print(ans)
        break
    minimam = 0
    temp1 = 0
    temp2 = -1
    copy = li.copy()
    for index, i in enumerate(copy):
        if (i < minimam or minimam == 0) and i != 0:
            minimam = i
        elif i == 0:
            temp1 = temp2
            temp2 = index
            if temp2 - temp1 > 0:
                li = [li[i] - minimam if temp1 < i < temp2 else li[i] for i in range(len(li))]
                ans += minimam
                minimam = 0
        if index == len(li)-1:
            li = [li[i] - minimam if temp2 < i else li[i] for i in range(len(li))]
            ans += minimam

Submission status: Accepted

Solution code:
N = int(input())
heights = list(map(int, input().split()))
ans = 0
def water(h):
    global ans
    min_h = min(h)
    ans += min_h
    after_h = list(map(lambda x: x-min_h, h))
    len_ah = len(after_h)
    last_zero = -1
    zero_cnt = 0
    for i in range(len_ah):
        if after_h[i] == 0:
            if last_zero != i - 1:
                water(after_h[last_zero+1:i])
            last_zero = i
            zero_cnt += 1
    if last_zero != len_ah - 1:
        water(after_h[last_zero+1:])

water(heights)

print(ans)

Submission status: Accepted

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

ans = 0
while True:
    state = 0
    for i in range(N):
        if state == 0 and h[i] > 0:
            state += 1
            h[i] -= 1
            ans += 1
        elif state == 1 and h[i] > 0:
            h[i] -= 1
        elif state == 1 and h[i] == 0:
            state = 2
            break
    if state == 0:
        break
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
hn = [int(x) for x in input().split()]

cnt = 0
prev_h = 0

cnt_n = [0] * n

for h in range(1, 101):
  if hn[0] >= h:
    cnt_n[0] = cnt_n[n-1] + 1
  else:
    cnt_n[0] = cnt_n[n-1]
  for i in range(1, n):
    if hn[i] >= h:
      if hn[i - 1] >= h:
        cnt_n[i] = cnt_n[i-1]
      else:
        cnt_n[i] = cnt_n[i-1]+1
    else:
      cnt_n[i] = cnt_n[i-1]

print(max(cnt_n))

Submission status: Accepted

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

    if len(h) == 1:
        print(h[0])
        return

    # 0: total, 1: prev water
    dp = [[0, 0] for _ in range(n+1)]
    for i in range(n):
        n_prev = dp[i][1]
        if n_prev >= h[i]:
            dp[i+1][0] = dp[i][0]
            dp[i+1][1] = h[i]
        else:
            dp[i+1][0] = dp[i][0] + h[i] - n_prev
            dp[i+1][1] = dp[i][1] + h[i] - n_prev
    print(dp[n][0])


main()

Submission status: Accepted

Solution code:
# Grand Garden
N = int(input().strip())
h = list(map(int, input().strip().split()))

def mizu(h, s):
	for k in range(s, len(h)):
		if h[k] > 0:
			h[k] -= 1
		else:
			break
	return h

count = 0
while True:
	s = min((i for i in range(len(h)) if h[i] > 0), default = -1)
	if s == -1:
		break
	else:
		h = mizu(h, s)
		count += 1

print(count)

Submission status: Accepted

Solution code:
# Grand Garden
N = int(input().strip())
h = list(map(int, input().strip().split()))

def mizu(h, s):
	for k in range(s, len(h)):
		if h[k] > 0:
			h[k] -= 1
		else:
			break
	return h
	
def find_start(h):
	s = min((i for i in range(len(h)) if h[i] > 0), default = -1)
	return s

def is_finish(h):
	return sum(h) == 0

count = 0
while True:
	s = find_start(h)
	if s == -1:
		break
	else:
		h = mizu(h, s)
		count += 1

print(count)

Submission status: Accepted

Solution code:
import numpy as np

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


def recurse(arr):
    if len(arr) == 0:
        return 0
    if all([e == 0 for e in arr]):
        return 0

    cnt = 0
    while all([e > 0 for e in arr]):
        arr = arr - 1
        cnt += 1

    next = []
    for e in arr:
        if e != 0:
            next.append(e)
        else:
            cnt += recurse(np.array(next))
            next.clear()
    if len(next) > 0:
        cnt += recurse(np.array(next))

    return cnt


print(recurse(h))

Submission status: Accepted

Solution code:
def smaller(x, y):
	if x < y:
		return x
	return y

def allzero(lst):
	for i in range(0, len(lst)):
		if lst[i] != 0:
			return False
	return True

def minimum(lst):
	min = 5000000000000000
	for i in range(0, len(lst)):
		min = smaller(min, lst[i])
	return min

N = int(input())
H = list(map(int, input().split()))
sum = 0
while not allzero(H):
	left = -1
	right = -1
	for i in range(0, N):
		if H[i] != 0:
			left = i
			break
	for i in range(left, N):
		if H[i] == 0:
			right = i
			break
	if right == -1:
		right = N
	min = minimum(H[left:right])
	sum += min
	for i in range(left, right):
		H[i] -= min
print(sum)

Submission status: Accepted

Solution code:
def solve_range(h, frm, to):
    """[frm, to)"""
    # print(frm, to, h)
    if frm >= to:
        return 0
    res = 0
    start = frm
    for i in range(frm, to):
        h[i] -= 1
        if h[i] < 0:
            if start < i:
                res += solve_range(h, start, i) + 1
            start = i + 1
        elif i == to - 1:
            res += solve_range(h, start, to) + 1
    return res


def solve(h):
    return solve_range(h, 0, n)


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

print(solve(h))

Submission status: Accepted

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

for i in range(1,100+1):
    flag =True
    for j in range(n):
        if flag==True and l[j]>=i:
            cnt+=1
            flag=False
        elif flag==False and l[j]>=i:
            continue
        else:
            flag=True
print(cnt)

Submission status: Accepted

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

ans = 0
cur = 0
for hh in h:
    if cur < hh:
        ans += hh - cur
        cur = hh
    else:
        cur = hh
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC116 C

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

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
h=a[:]
cnt=0
while sum(h)!=0:
  mae=False
  for i in range(N):
    if h[i]>0:
      if mae==False:
        cnt+=1
      mae=True
      h[i]-=1
    else:
      mae=False

print(cnt)

Submission status: Accepted

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



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
        else:
            res += 1
            while i < N and h[i] > 0:
                h[i] -= 1
                i += 1
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
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:
N = int(input())
h = list(map(int, input().split()))

cnt = 0
for i in range(100, 0, -1):
    prev = False
    for j in range(N):
        if h[j] == i:
            if not prev:
                cnt += 1
            h[j] -= 1
            prev = True
        else:
            prev = False

print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
lis=list(map(int,input().split()))
li=[]
y=0
g=0
for i in range(n):
    li.append(lis[i])
    if len(li)>=2:
        if li[-2]>li[-1]:
            y=1
        if y==1 and li[-2]<li[-1]:
            g+=max(li[:-2])-li[-2]
            y=0
            li=[li[-1]]
g+=max(li)
print(g)

Submission status: Accepted

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

water=0
alive=1
while alive:
    flag=1
    cnt=0
    for i in range(N):
        if h[i]!=0:
            cnt+=flag
            flag=0
        elif h[i]==0:
            flag=1
    alive=0
    for i in range(N):
        if h[i]>0:
            h[i]-=1
            alive=1
        else:
            h[i]=0
    if alive:
        water+=cnt
print(water)

Submission status: Accepted

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


lst = [0]*N
for i in range(N):
    lst[i] = (h[i], i)
lst = list(sorted(lst))


ans = 0

def putLeft(n):
    now = h[n]
    res = 0
    for i in reversed(range(n)):
        if h[i] < now:
            break
        else:
            h[i] -= now
    return res

def putRight(n):
    now = h[n]
    res = 0
    for i in range(n+1, N):
        if h[i] < now:
            break
        else:
            h[i] -= now


for (_, i) in lst:
    if h[i] > 0:
        putLeft(i)
        putRight(i)
        ans += h[i]
        h[i] = 0

print(ans)

Submission status: Accepted

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

ret = 0
while H.count(0) != N:
    f = False
    for i in range(N):
        if H[i]:
            if not f:
                f = True
                ret += 1
            H[i] -= 1
        else:
            f = False

print(ret)

Submission status: Accepted

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

Submission status: Accepted

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

def is_clear(l):
    for ele in l:
        if(ele!=0):
            return False
    return True

while(not is_clear(flowers)):
    tmp_l = []
    #水やり範囲を決める処理
    for i,flower in enumerate(flowers):
        if(flower!=0):
            tmp_l.append(i)
        else:
            if(len(tmp_l)!=0):
                break


    if(len(tmp_l)>=2):
        water = min(flowers[tmp_l[0]:tmp_l[-1]+1])
        for i in tmp_l:
            flowers[i] -= water
    else:
        water = flowers[tmp_l[0]]
        flowers[tmp_l[0]] -= water

    ans += water

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int, input().split()))
h.append(0)
count=0
for i in range(10000):
    count+=1
    if max(h)==0:
        break
    for j in range(len(h)-1):
        if h[j]>0:
            h[j]-=1
            if h[j+1]==0:
                break
        
            
print(count-1)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split())) # １行に別れてるとき

D = []
for i in range(N-1):
    D.append(H[i+1]-H[i])

ans = 0
up = True
down = False
for i in range(N-2):
    if i == 0 and D[i] < 0:
        ans += H[0]
        up = False
    if (D[i] > 0 and D[i+1] < 0) or (up and D[i+1] < 0):
        ans += H[i+1]
        up = False
    elif D[i] > 0 and D[i+1] == 0:
        up = True
    
    if (D[i] < 0 and D[i+1] > 0) or (down and D[i+1] > 0):
        ans -= H[i+1]
        down = False
    elif D[i] < 0 and D[i+1] == 0:
        down = True
if up or D[-1] > 0:
    ans += H[-1]


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


ans = 0
now = 0
for i in range(N):
    if lst_h[i] > now:
        ans += lst_h[i] - now
    now = lst_h[i]


print(ans)

Submission status: Accepted

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

h_max = max(h)
ans = 0
for i in range(h_max):
    j = 0
    while j <= n-1:
        if h[j] == 0:
            j += 1
        else:
            while j <= n-1:
                if h[j] == 0:
                    ans += 1
                    j += 1
                    break
                elif j == n-1:
                    h[j] -= 1
                    ans += 1
                    j += 1
                    break
                else:
                    h[j] -= 1
                    j += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

while sum(h) != 0:
    for i in range(n):
        if h[i] != 0:
            f = True
            h[i] -= 1
        else:
            if f:
                cnt += 1
            f = False
    if f:
        cnt += 1
    f = False
print(cnt)

Submission status: Accepted

Solution code:
n = int(raw_input())
H = map(int, raw_input().split())
res = 0
for i in range(1, max(H) + 1):
    consecutives = 0
    in_consecutive = False
    for j in range(n):
        if not in_consecutive and H[j] >= i:
            in_consecutive = True
        elif in_consecutive and H[j] < i:
            in_consecutive = False
            consecutives += 1
        else:
            continue
    if in_consecutive:
        consecutives += 1
    res += consecutives
print res

Submission status: Accepted

Solution code:
import numpy as np

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

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

print(ans)

Submission status: Accepted

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

ans = 0
tmp = 0
for i in h:
    if tmp < i:
        ans += i-tmp
    tmp = i
print(ans)

Submission status: Accepted

Solution code:
# ABC116 C-Grand Garden

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

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

Submission status: Accepted

Solution code:
import numpy as np

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

hd = np.diff(hl)
print(hl[0]+sum(hd[hd>0]))

Submission status: Accepted

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

n = int(input())

h = input().split()

li = [0]
for i in range(n):
    li.append(int(h[i]))

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
hn=0
c=0
for i in h:
    if hn < i:
        c += i-hn
    hn = i
print(c)

Submission status: Accepted

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

while any(h):
  l,r = 0, 0
  for i in range(N):
    if h[i] == 0 and h[i+1] != 0:
      l = i+1
    if (h[i] != 0 and h[i+1] == 0):
      r = i+1
      minh = min(h[l:r])
      ans += minh
      for j in range(l,r):
        h[j] -= minh
print(ans)

Submission status: Accepted

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

cnt = 0

for h in range(1,101):
    exi = 0
    for f in range(N):
        if H[f] >= h:
            tmp = 1
        else:
            tmp = 0
        if tmp != exi:
            cnt += 1 - exi
            exi = 1 - exi
print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split()))
water = 0
ite = 0
while (ite < N):
    if h[ite] == 0:
        ite += 1
        continue
    kari_ite = ite
    water += 1
    while (kari_ite < N):
        if h[kari_ite] == 0:
            break
        else:
            h[kari_ite] -= 1
            kari_ite += 1
print(water)

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int, input().split()))
b.insert(0, 0)
b.insert(len(b), 0)
f = 0

while True:
    s = -1
    e = -1
    for i in range(len(b)):
        if b[i] != 0 and s == -1:
            s = i
        elif b[i] == 0 and s != -1 and b[i-1] == 0:
            s = i
        elif b[i] == 0 and s != -1:
            e = i
            break
    if s != -1 and e != -1:
        f += 1
        for i in range(s, e):
            b[i] -= 1
    elif s == -1 and e == -1:
        print(f)
        exit()

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:

n1 = int(input())
na2 = [int(i) for i in input().split()]

r3 = 0
t4 = 0

for i4 in range(n1):
    if na2[i4] > t4:
        r3 += na2[i4] - t4
    
    t4 = na2[i4]

print(r3)

Submission status: Accepted

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

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

Submission status: Accepted

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


def f(x):
    if all([a == 0 for a in x]):
        return 0
    elif min(x) == 0:
        x0 = x.index(0)
        if x0 == 0:
            return f(x[1:])
        elif x0 == len(x) - 1:
            return f(x[:-1])
        else:
            return f(x[:x0]) + f(x[x0 + 1:])
    else:
        return f([a - 1 for a in x]) + 1


print(f(h))

Submission status: Accepted

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

def cs(lix):
  if len(lix) == 1:
    return lix[0]
  elif len(lix) == 0:
    return 0
  m = min(lix)
  div = lix.count(m)+1
  lili = [0]*div
  cut = 0
  for i in range(div-1):
    lili[i] = lix[cut:lix[cut:].index(m)+cut]
    cut = lix[cut:].index(m)+cut+1
  if lix[-1] == m:
    lili[div-1] = []
  else:
    lili[div-1] = lix[cut:]
  for i in range(lili.count([])):
    lili.remove([])
  return sum(list(map(cs,lili))) - (len(lili)-1)*m
print(cs(li))

Submission status: Accepted

Solution code:
import numpy as np

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

cnt = 0
Ha = np.array(H)

for i in range(max(H)):
    for j in range(N):
        if j==0:
            if Ha[0]>0:
                cnt += 1
        else:
            if Ha[j-1]<=0 and Ha[j]>0:
                cnt += 1
    Ha -= 1
print(cnt)

Submission status: Accepted

Solution code:
import numpy
n = int(input())
a = list(map(int,input().split()))
a.insert(0,0)
a = numpy.array(a)
a = a[1:]-a[:n]
print(a[a>0].sum())

Submission status: Accepted

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

Submission status: Accepted

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


def water(h):
    if len(h) == 0:
        return 0
    if len(set(h)) == 1:
        return h[0]
    elif not h.count(0):
        h2 = list(map(lambda x: x - 1, h))
        return water(h2) + 1
    else:
        ans = 0
        zeros = [i for i, x in enumerate(h) if x == 0]
        zeros.insert(0, -1)
        zeros.append(len(h))
        for i in range(len(zeros) - 1):
            ans += water(h[zeros[i]+1:zeros[i + 1]])
        return ans

print(water(h))

Submission status: Accepted

Solution code:
n = int(input())

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

dat_map = []
for i in range(n):
    dat_map.append([0] * 100)

for i in range(n):
    for j in range(dat_h[i]):
        dat_map[i][j] = 1

res = 0
for i in range(100):
    cur = 0
    for j in range(n):
        if cur == 0:
            if dat_map[j][i] == 1:
                res += 1
        cur = dat_map[j][i]

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

maxNum = max(h)
countNumber = 0
countWatering = 0
isfinished = False
for c in range(1, maxNum+1):
    array = []
    for index, num in enumerate(h):
        if (num >= c):
            array.append(index)
    if (len(array) == 0 or c > maxNum):
        break
    startIndex = 0
    beforeVlaue  = array[0]-1
    for a in array:
        if (a-1 != beforeVlaue):
            countWatering+=1
        if (a == array[-1]):
            countWatering+=1
        beforeVlaue = a
    
print(countWatering)

Submission status: Accepted

Solution code:
N=int(input())
H=list(map(int,input().split()))
H.insert(0,0)
H.append(0)
#print(H)
count=1
total=0
rem=N
for i in range(1000):
    for j in range(1,N+1):
        if H[j]==i:
            rem -= 1
            H[j]=0
            if H[j-1]!=0 and H[j+1]!=0:
                count+=1
            if H[j-1]==0 and H[j+1]==0:
                count -=1
    if rem==0:
        break
    total += count
    #print("rem:"+str(rem))
    #print("count"+str(count))
    #print(total)


print(total)

Submission status: Accepted

Solution code:
n = int(input())
s = list(map(int,input().split()))
s = [i-1 for i in s]
ans = 0
while(sum(s)!=-n):
    s_new=[i.replace("-1","a") for i in map(str,s)]
    s_join = ''.join(s_new)
    non = s_join.split("a")
    num = len([x for x in non if x])
    ans += num
    s = [max(i-1,-1) for i in s]
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
for i in range(1, 110):
  flag = False
  for j in range(n):
    if (flag and h[j]<i) or (j==n-1 and h[j]>=i):
      ans += 1
      flag = False
    elif h[j]>=i:
      flag = True
print(ans)

Submission status: Accepted

Solution code:
'''
allinputs = iter(input().splitlines())
input = lambda : next(allinputs)
#'''
N = int(input())
h = list(map(int, input().split()))
h.append(0)
m = 0

while True:
	l = -1
	r = N
	for i in range(N):
		if h[i] > 0:
			l = i
			break
			
	for i in range(l, N):
		if h[i] == 0:
			r = i
			break
			
	if l == -1:
		break
			
	for i in range(l, r):
		h[i] -= 1
		
	m += 1
	
print(m)

Submission status: Accepted

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

if __name__=="__main__": main()

Submission status: Accepted

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

cnt=0
while(True):
  if max(hlist) == 0:
    break
  
  left = 0
  right = N-1
  
  maxh = max(hlist)
  inseq = False
  for i in range(N):
    if hlist[i] == maxh:
      if not inseq:
        inseq = True
        left = i
    else:
      if inseq:
        right = i-1
        break

  #print(left,right)
  for i in range(left,right+1):
    hlist[i] -= 1
  cnt += 1
  
print(cnt)

Submission status: Accepted

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

count = min(h_list)
h_list = [item - min(h_list) for item in h_list] + [0]

first = 0
for h in range(len(h_list) - 1):
    if h_list[h] > h_list[h + 1]:
        count += h_list[h] - first
        first = h_list[h + 1]
    else:
        continue
print(count)

Submission status: Accepted

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

i = 0
cnt = 0
while 1:
    if i == n:
        print(cnt)
        exit()

    if h[i]==0:
        i+=1
        continue
    j = i
    
    while 1:
        if j==n:
            j-=1
            break
        elif h[j]>=1:
            j+=1
        else:
            j-=1
            break


    cnt += 1
    

    for k in range(i,j+1):
        h[k] -= 1

Submission status: Accepted

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

ans=min(h)
for i in range(n):
    h[i] -= ans

while sum(h)!=0:
    i=0
    crnt = h[i]
    while crnt==0:
        i+=1
        crnt=h[i]
    s=i
    while i<(n-1) and h[i]!=0:
        crnt = min(crnt, h[i])
        i+=1
    for ii in range(s, i+1):
        h[ii] = max(0, h[ii]-crnt)
    ans+=crnt

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

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

def myfunc(left, right):
    if right<left:
        return 0
    idx = left+H[left:right+1].argmin()
    m = H[idx]
    H[left:right+1]-=m
    return m+myfunc(left, idx-1)+myfunc(idx+1, right)

print(myfunc(0, N-1))

Submission status: Accepted

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

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

count = min(h_list)
h_list = [item - min(h_list) for item in h_list] + [0]
first = 0
for h in range(len(h_list) - 1):
    if h_list[h] > h_list[h + 1]:
        count += h_list[h] - first
        first = h_list[h + 1]
    else:
        continue
print(count)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
H = np.array(input().split(), dtype=np.int32)

def F(L,R):
  if R < L:
    return 0
  idx = L + H[L:R+1].argmin()
  x = H[idx]
  H[L:R+1] -= x
  return x + F(L,idx-1) + F(idx+1,R)

answer = F(0,N-1)
print(answer)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def mount(l):
    if l == []:
        return True
    else:
        m = l.index(max(l))
        flag = 0
        for i in range(m):
            if l[i] > l[i+1]:
                flag = 1
        for i in range(m,len(l)-1):
            if l[i] < l[i+1]:
                flag = 1
        if flag == 0:
            return True
        else:
            return False
def count(l):
    if mount(l):
        if l != []:
            return(max(l))
        else:
            return 0
    else:
        cnt = min(l)
        l = list(map(lambda x:x-min(l),l))
        start = 0
        for i in range(len(l)):
            if l[i] == 0:
                cnt += count(l[start:i])
                start = i+1
            elif i == len(l)-1:
                cnt += count(l[start:])
        return(cnt)
N = int(input())
h = list(map(int,input().split()))
print(count(h))

Submission status: Accepted

Solution code:
def all_water(h_list):
  if len(h_list) == 0:
    return 0
  min_h = min(h_list)
  new_list = [x - min_h for x in h_list]
  i = new_list.index(0)
  return min_h + all_water(new_list[:i]) + all_water(new_list[i+1:])

n = input()

height = list(map(int,input().split(" ")))

print(all_water(height))

Submission status: Accepted

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

ans = 0
while True:
    if sum(H) == 0:
        break
    begin = 0
    end = 0
    for i in range(N):
        if H[i] != 0:
            begin = i
            break
    # print(H, begin, end)
    for i in range(begin, N):
        # print(H[i])
        if H[i] == 0:
            # print('end={}'.format(i - 1))
            end = i - 1
            break
        end = i
    # print(H, begin, end)
    for i in range(begin, end + 1):
        H[i] -= 1
    ans += 1

print(ans)

Submission status: Accepted

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

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