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(x) for x in input().split()]

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

print(ans)

Submission status: Accepted

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


def dfs(left, right):
    if left >= right:
        return 0
    idx = left + h[left: right].index(min(h[left: right]))
    x = h[idx]
    for i in range(left, right):
        h[i] -= x
    return dfs(left, idx) + x + dfs(idx+1, right)


print(dfs(0, n))

Submission status: Accepted

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

ans = 0
while sum(h) > 0:
    prev = 0
    split = 0
    for v in h:
        if prev == 0 and v != 0:
            split += 1
        prev = v

    ans += split
    h = [v-1 if v > 0 else 0 for v in h]

print(ans)

Submission status: Accepted

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

cnt = 0
before = 0

for i in range(N):
    if before < H[i]:
        cnt += H[i] - before
    before = H[i]

print(cnt)

Submission status: Accepted

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

ans = 0
prev = 0
for v in h:
    if prev < v:
        ans += v - prev
    prev = v

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
hh=list(map(int,input().split()))
ans=0
while 1:
    l=0
    while l<n and hh[l]==0:l+=1
    if l==n:break
    r=l
    while r<n and hh[r]>0:r+=1
    mn=min(hh[l:r])
    for i in range(l,r):hh[i]-=mn
    ans+=mn
print(ans)

Submission status: Accepted

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  n=int(input())
  h=list(map(int,input().split()))
  m=min(h)
  h.append(0)
  ans=m
  for i in range(0,n):
    if h[i]<=h[i+1]:
      continue
    else:
      ans+=h[i]-m
      m=h[i+1]
  print(ans)
if __name__=='__main__':
  main()

Submission status: Accepted

Solution code:
n=int(input())

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

counter=0
maa=100

for i in range(maa):
    hight = i+1
    flag = False
    for h in hlist:
        if not flag:
            if h>= hight:
                counter+=1
                flag = True
        if flag:
            if h < hight:
                flag = False

print(counter)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(e) for e in input().split()]
mat = [[a[j]>i  for j in range(n)] for i in range(max(a))]
sliced_mat = [[a for a,b in zip(row,row[1:]+[False]) if a and a!=b]for row in mat]
print(sum([len(row)for row in sliced_mat]))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(e) for e in input().split()]
tmp_mat = [[a[j]>i  for j in range(n)] for i in range(max(a))]
mat = [[a for a,b in zip(row,row[1:]+[False]) if a and a!=b]for row in tmp_mat]
print(sum([len(row)for row in mat]))

Submission status: Accepted

Solution code:
def calc(a,n):
    array = [[a[j]>i  for j in range(n)] for i in range(max(a))]
    mat = [[a for a,b in zip(row,row[1:]+[False]) if a and a!=b]for row in array]
    return sum([len(row)for row in mat])
n = int(input())
a = [int(e) for e in input().split()]

print(calc(a,n))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
def dfs(l,r):
  if l==r:
    return h[l]
  if r<l:
    return 0
  res=10**18
  for i in range(l,r+1):
    res=min(res,h[i])
  for i in range(l,r+1):
    h[i]-=res
  for i in range(l,r+1):
    if h[i]==0:
      return dfs(l,i-1)+dfs(i+1,r)+res
  return 0
print(dfs(0,n-1))

Submission status: Accepted

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
r = i = 0
while sum(h) != 0:
    if h[i] == 0:
        i += 1
    else:
        r += 1
        while i < n and h[i] > 0:
            h[i] -= 1
            i += 1
        i = 0
print(r)

Submission status: Accepted

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
r = i = 0
while max(h) != 0:
    if h[i] == 0:
        i += 1
    else:
        r += 1
        while i < n and h[i] > 0:
            h[i] -= 1
            i += 1
        i = 0
print(r)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,*h=map(int,open(0).read().split());print(sum(max(a-b,0)for a,b in zip(h,h[1:]+[0])))

Submission status: Accepted

Solution code:
n,*h=map(int,open(0).read().split())
a=h[0]
for i in range(1,n):
    if h[i-1]<h[i]:
        a+=h[i]-h[i-1]
print(a)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
h=list(map(int,input().split()))
min=h[0]
piv=0
l=0
r=0
i=0
cnt=0
while sum(h)>0:
    while (h[l]==0)and(l<=N-1):
        l=l+1
    if l==N:
        break
    else:
        min=h[l]
    for i in range(l,N):
        if h[i]<min:
            if h[i]!=0:
                min=h[i]
            else:
                piv=piv-1
                break
    piv=piv+i
    #print(l,piv)
    for i in range(l,piv+1):
        h[i]=h[i]-min
    cnt=cnt+min
    l=0
    piv=0
    #print(h,cnt)
    #input()
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

def solve(l):
    if l == []:
        return 0
    else:
        m = min(l)
        ans = m
        l2 = []
        for i in range(len(l)):
            l2.append(l[i] - m)
        p = l2.index(0)
        return m + solve(l2[:p]) + solve(l2[p + 1:])

print(solve(h))

Submission status: Accepted

Solution code:
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    h = list(map(int,ipti().split()))
    h.append(0)

    cnt = 0

    que = deque()
    que.append((0, n, 0))

    while que:
        left, right, height = que.pop()
        flag = False

        for i in range(left, right+1):
            if not flag and h[i] > height:
                nl = i
                flag = True

            if flag and h[i] <= height:
                nr = i
                que.append((nl, nr, height + 1))
                cnt += 1
                flag = False

    print(cnt)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque
q = deque()

n = int(input())
h = list(map(int, input().split()))
for i in h:
    q.append(i)
q.append(0)

ans = 1
if sum(q) == 0:
    ans = 0
c = 0
while sum(q) != 0:
    p = q.popleft()
    if p != 0:
        q.append(p - 1)
        c = 1
    else:
        q.append(0)
        if c == 1:
            ans += 1
            c = 0
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
h=list(map(int,input().split()))
ans=0
flag=True
f=-1
e=N-1

for i in range(N):
    if h[i]>0:
        f=0
if f<0:
    print(0)
    exit()

while(flag):
    F=False
    E=False
    for i in range(N):
        if not F:
            if h[i]>0:
                f=i
                F=True
    for i in range(f,N):
        if not E:
            if h[i]==0:
                e=i-1
                E=True

    #print(f)
    #print(e)
    if f>e:
        e=N-1
    for i in range(f,e+1):
        h[i]-=1
    ans+=1
    #print(h)
    flag=False
    for i in range(N):
        if h[i]>0:
            flag=True
print(ans)

Submission status: Accepted

Solution code:
import sys
from math import *
from collections import deque, Counter, defaultdict
from fractions import gcd
input = lambda: sys.stdin.readline().rstrip()

def eprint(s):
    sys.stderr.write('DEBUG: {}'.format(s))
    return

def main():
    n = int(input())
    Hs = list(map(int, input().split()))
    i = 0
    cnt = 0
    island = 0
    while True:
        if i >= n:
            i = 0
        if sum(Hs) == 0:
            break
        if Hs[i] == 0:
            i += 1
            continue
        else:
            cnt += 1
            while (i < n and Hs[i] > 0):
                Hs[i] -= 1
                i += 1

    print(cnt)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:





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

ans = 0
while True:
    
    l,r = -1,-1

    for i in range(N):
        if H[i] != 0:
            l = i
            break

    if l == -1:
        break

    for i in range(l,N):
        if H[i] == 0:
            r = i-1
            break

        if i == N-1: r = N-1


    tmp =  min(H[l:r+1])
    for i in range(l,r+1):
        H[i] -= tmp
    ans += tmp

print(ans)

Submission status: Accepted

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

ans = 0
mh = max(h)
for i in range(1, mh+1):
    f = False
    for j in range(N):
        if i <= h[j]:
            if f is True:
                continue
            else:
                f = True
                ans += 1
        else:
            f = False

print(ans)

Submission status: Accepted

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

ans = 0
mh = max(hs)
for cur in range(1, mh + 1):
    c = False
    for h in hs:
        if cur <= h:
            if c:
                continue
            else:
                c = True
                ans += 1
        else:
            c = False
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

cost = h[0]

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

print(cost)

Submission status: Accepted

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

def minus(l,x):
    for i in range(len(l)):
        l[i] = l[i] - x
    

count = 0
count += min(h)
minus(h, min(h))

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

Submission status: Accepted

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

def minus(l,x):
    for i in range(len(l)):
        l[i] = l[i] - x
    

count = 0
count += min(h)
minus(h, min(h))


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

Submission status: Accepted

Solution code:
def main():
    #from sys import stdin, setrecursionlimit
    #setrecursionlimit(10**9)
    #r = input()
    #n = int(input())
    #a, b = map(int, input().split())
    #s, y = input().split()
    #a = input().split()
    #a = [int(input()) for i in range(n)]
    #a = list(map(int, input().split()))
    #a = [list(map(int, input().split())) for i in range(n)]
    #a = [int(s) - 1 for s in input().split()]
    #l=[int(input()) for _ in range(int(input()))]
    #res = 0
    n = int(input())
    a = list(map(int, input().split()))
    res = a[0]
    for i in range(1,n):
        res += max(0, a[i] - a[i-1])
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if n<=2:
    print(max(h))
else:
    while True:
        count = 0
        for i in range(n):
            if h[i]!=0 and h[i+1]==0:
                count += 1
        for i in range(n):
            if h[i]!=0:
                h[i] -= 1
        ans += count
        if all([i==0 for i in h]):
            print(ans)
            exit()

Submission status: Accepted

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

ans = 0
level = 0
for i in range(len(h)):
    if h[i] >= level:
        level = h[i]
    else:
        ans += level - h[i]
        level = h[i]

ans += level

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = [0] + [int(a) for a in input().split()]
# for _ in range(N):
#     h.append(int(input()))
answer = 0
for i in range(1, N+1):
    answer += max(H[i] - H[i-1], 0)

print(answer)

Submission status: Accepted

Solution code:
n= int(input())
h= list(map(int, input().split()))
x=max(h)
ans=0
count=0
for i in range(10009):
    count=0
    if max(h)==0:
        break
    ans+=1
    for j in range(n):
        if h[j]==0 and count==0:
            continue
        elif h[j]>0:
            count=1
            h[j]-=1
        else:
            break



print(ans)

Submission status: Accepted

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

def height(ans, loop):
  if loop > max(h):
    print(ans)
    return
  
  i = 0
  for i in range(n):
    if i == 0 and h[i] >= loop:
      ans += 1
    elif h[i] >= loop and h[i-1] < loop:
      ans += 1
      
  height(ans, loop+1)
  
height(0, 1)

Submission status: Accepted

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

ans=0

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
hantei = [0 for _ in range(n)]
hantei.append(1)
result = 0
h.append(10*3)

while any(hantei[i] != 1 for i in range(n)):
    min_h = min(h)
    cost = 0
    for i in range(len(h)):
        if hantei[i]==1:
            result += cost
            cost = 0
            h[i] = 10**3
        else:
            h[i] -= min_h
            if h[i] == 0:
                hantei[i] = 1
            cost = min_h
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())

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

ans = 0

while sum(t) != 0:
    for i in range(n):
        if t[i] > 0 and i == n-1:
            ans += 1
            t[i] -= 1
        elif t[i] == 0 and i == n-1:
            continue
        elif t[i] > 0 and t[i+1] == 0:
            ans += 1
            t[i] -= 1
        elif t[i] > 0 and t[i+1] > 0:
            t[i] -=1
    #print(t)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
count=0
while h!=[0]*n:
	find=0 #左端を見つけたかどうか
	count+=1
	for i in range(n):
		if h[i]==0 and find==1:
			break
		if h[i]!=0:
			h[i]-=1
			find=1	
print(count)

Submission status: Accepted

Solution code:
n = int(input())
H = [0] + list(map(int, input().split())) + [0]
T = [0 for _ in range(n+2)]
c = 0
while True:
	s0 = 0; t1 = 0
	for i in range(0, n+2):
		if H[i] > 0 and s0 == 1:
			H[i] -= 1
			t1 = 1
		elif H[i] == 0 and t1 == 1:
			c += 1
			t1 = 0
		elif H[i] == 0:
			s0 = 1
	if H == T: break
print(c)

Submission status: Accepted

Solution code:
n = int(input())
H = list(map(int, input().split()))
T = [0 for _ in range(n)]
c = 0
while True:
  f = False
  S = {0:1}
  for j in range(n):
    if H[j] > 0:
      S[1] = 1
      H[j] -= 1
      f = True
    elif len(S) > 1 and H[j] == 0:
      break
  if f: c += 1
  if H == T: break
print(c)

Submission status: Accepted

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

count = 0
for key in range(1, 101):
    flag = False

    for i in range(N+1):
        if h[i] >= key:
            if not flag:
                flag = True
        else:
            if flag:
                flag = False
                count += 1


print(count)

Submission status: Accepted

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

Submission status: Accepted

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

A = [[0 for i in range(N)] for j in range(101)]
#print(A)

for i in range(101):
    for j in range(N):
        if H[j] - i > 0:
            A[i][j] = 1 
#print(A)

ans = 0
for i in range(101):
    cnt = 0
    for j in range(N):
        if cnt == 0 and A[i][j] == 1:
            cnt+=1
            ans +=1
        elif A[i][j] == 0:
            cnt = 0
        
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
from io import StringIO
import unittest

def resolve():
    n = int(input().strip())
    hl = list(map(int,input().strip().split()))
    cnt=0
    while 1:
        maxh=max(hl)
        if maxh==0:
            break
        t=-1
        for i in range(n):
            if hl[i]>0:
                hl[i]-=1
                if i==0:
                    cnt+=1
                elif (i-1)==t:
                    cnt+=0
                else:
                    cnt+=1
                t=i
    print(cnt)

    return

resolve()

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def f(h):
    th = [i-1 for i in h]
    ret = 1
    spl = []
    for i in range(len(th)):
        if i == 0 and th[i] != 0:
            spl.append(i)
        elif i != 0 and th[i-1] == 0 and th[i] != 0:
            spl.append(i)
        if i == len(th)-1 and th[i] != 0:
            spl.append(i+1)
        elif i != 0 and th[i-1] != 0 and th[i] == 0:
            spl.append(i)
    for i in range(0,len(spl),2):
        ret += f(th[spl[i]:spl[i+1]])
            
    return ret
    


n = int(input())
h = [int(x)+1 for x in input().split()]

print(f(h)-1)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
cnt=0
for i in range(100):
    prev = bool(h[0])
    h[0] = h[0] - prev
    for j in range(1,n):
        if bool(h[j]) == prev:
            h[j] = h[j] - prev
            continue
        else:
            cnt += prev
            prev = bool(h[j])
            h[j] = h[j] - prev
    cnt+=prev
print(cnt)

Submission status: Accepted

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

def search(h):
    if len(h)==0:
        return 0
    else:
        x=min(h)
        h_=[h[i]-x for i in range(len(h))]
        res=x
        if 0 in h_:
            i=h_.index(0)
            res+=search(h_[0:i])+search(h_[i+1:])
        return res

print(search(H))

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int , input().split()))
ans = 0
for i in range(len(A)-1):
    if A[i] > A[i+1]:
        ans += A[i] - A[i+1]
ans += A[-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] > h[i + 1]:
        ans += h[i] - h[i + 1]

ans += h[N - 1]

print(ans)

Submission status: Accepted

Solution code:
_ = input()
input_list = list(map(int, input().split(' ')))

total = input_list[0]
height = input_list[0]
for a in input_list[1:]:
  height = min(height, a)
  total += a - height
  height = a
  
print(total)

Submission status: Accepted

Solution code:
import numpy as np

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


def f(H):
  if len(H) == 1:
    return H[0]
  elif len(H) == 0:
    return 0
  else:
    idx = np.argmin(H)
    min_h = H[idx]
    Hl = H[:idx] - min_h
    Hr = H[idx+1:] - min_h
    res = f(Hl) + f(Hr) + min_h
    return res

print(f(h))

Submission status: Accepted

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

ans = 0
left = 0
for i in h:
    if i > left:
        ans += i-left
    left=i
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def calc(left, right, ans):
    minh = min(h[left: right+1])
    ans += minh
    for i in range(left, right+1):
        h[i] -= minh
    return ans

ans = 0

while h!=[0]*N:
    if h[0]>0:left = 0
    
    if N==1:
        ans = h[0]
        break    
    
    for i in range(1, N):
        if h[i]>0 and h[i-1]==0:
            left=i
            if i==N-1:
                right = N-1
                ans = calc(left, right, ans)
        elif h[i]==0 and h[i-1]>0:
            right=i-1
            ans = calc(left, right, ans)
        elif h[i]>0 and h[i-1]>0 and i==N-1:
            right = N-1
            ans = calc(left, right, ans)
        else:continue


print(ans)

Submission status: Accepted

Solution code:
N,*h=map(int,open(0).read().split())
ans = 0
left = 0
for i in h:
    if i > left:
        ans += i-left
    left=i
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# min に合わせてそれが端にない限りはそこで２つに分割
# index もつけてzip sort するか
# 全部なくなるまで続ける
n = int(input())
a = list(map(int,input().split()))
def solve(a,n):
    if len(a) == 0: return 0
    if len(a) == 1: return a[0]-n
    idx = a.index(min(a))
    #print(min(a),n)
    return solve(a[:idx],min(a))+ min(a)-n +solve(a[idx+1:],min(a))
print(solve(a,0))

Submission status: Accepted

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

res=0
while 1:
    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:
import numpy as np
n=int(input())
H=list(map(int,input().split()))
H=np.array(H)
cnt=H[0]+np.maximum(H[1:]-H[:-1],0).sum()
print(cnt)

Submission status: Accepted

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

w,cnt=0,0
for i in range(n):
  cnt+=max(H[i]-w,0)
  w=H[i]
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
*h, = map(int, input().split())
cnt = 0
minh = min(h)
# 一番低い木が育つまでは全体に水やりできるので、全体に水やりしたあとの状態
h = [i - minh for i in h]
cnt += minh + h[0]

for i in range(1, n):
  # 左隣が自身以下であるなら、その差分の水やりは自身のみの成長にのみ使われる
  if h[i-1] <= h[i]: cnt += h[i] - h[i-1]
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
*h,=map(int,input().split())
m=min(h)
h=[i-m for i in h]
cnt=m+h[0]
for i in range(1,n):
	if h[i-1]<=h[i]:
		cnt+=h[i]-h[i-1]
print(cnt)

Submission status: Accepted

Solution code:
count = 0

def rec(l, r):
    global count
    global h
    global s

    if l >= r:
        return

    zero = list()
    for i in range(l, r):
        if h[i] == 0:
            zero.append(i)

    if len(zero) > 0:
        for i in zero:
            if not (l, i) in s:
                rec(l, i)
                s.add((l, i))
            if not (i+1, r) in s:
                rec(i+1, r)
                s.add((i+1, r))
    else:
        for i in range(l, r):
            h[i] -= 1
        count += 1
        rec(l, r)

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

rec(0, len(h))
print(count)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    h = list(map(int, input().split()))
    r = 0
    while sum(h) > 0:
        m = max(h)
        t = 0
        for i in range(len(h)):
            if h[i] == m:
                h[i] -= 1
                if t == 0:
                    r += 1
                    t = 1
            else:
                t = 0
    print(r)
main()

Submission status: Accepted

Solution code:
def water(flowers, count):
    if len(flowers) is 0:
        return count
    count = count + min(flowers)
    flowers = [int(e-min(flowers)) for e in flowers]
    while 0 in flowers:
        count = water(flowers[:flowers.index(0)], count)
        flowers = flowers[flowers.index(0)+1: ]
    count = water(flowers, count)
    return count

n = int(input())
flowers = [int(e) for e in input().split()]

count = 0
count = water(flowers, count)
print(count)

Submission status: Accepted

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

ans = 0
while sum(h) != 0:
  m = max(h)
  f = False
  for i in range(len(h)):
    if h[i] == m:
      h[i] -= 1
      if not f:
        f = True
        ans += 1
    else:
      f = False
print(ans)

Submission status: Accepted

Solution code:
from itertools import groupby

def sgn(num):
    if num>0:
        return 1
    elif num==0:
        return 0
    else:
        return -1

N=int(input())
h=list(map(int,input().split()))
ans=0
for i in range(1,101):
    sublist=[sgn(h[j]) for j in range(0,N)]
    sublist=groupby(sublist)
    for key,group in sublist:
        if key==1:
            ans+=1
    h=[h[j]-1 for j in range(0,N)]
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-1]<h[i]: ans += h[i]-h[i-1]
print(ans)

Submission status: Accepted

Solution code:
import itertools
import math
import sys

n = int(input())
h = list(map(int, input().split()))
# start = [0] * n
# ans = 0
# flag = True
# index = 0
# while start != h:
#     start_from_0 = True
#     for i in range(index, n):
#         if start[i] < h[i]:
#             start[i] += 1
#             continue
#         if start[i] == h[i]:
#             if i == n - 1:
#                 index = 0
#                 ans += 1
#                 start_from_0 = False
#                 print(start)
#                 break
#             ans += 1
#             index = i + 1
#             start_from_0 = False
#             print(start)
#             break
#     if start_from_0:
#         print(start)
#         index = 0
#         ans += 1
# print(ans)
deleted_something = False
# encountered_0 = False
ans = 0
while h.count(0) != n:
    deleted_something = False
    for i in range(n):
        if h[i] == 0 and deleted_something:
            break
        if h[i] == 0:
            continue
        h[i] -= 1
        deleted_something = True
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

ans = 0
for i in range(len(lst)-1):
  if (lst[i+1] > lst[i]):
    ans += lst[i+1] - lst[i]
print(ans + lst[0])

Submission status: Accepted

Solution code:
n = int(input())
S = input().split(" ")
arr = [int(s) for s in S]
def calculate(n,arr):
    ans = 0
    for i in range(len(arr)):
        if i == 0:
            ans += arr[i]
        else:
            if arr[i] > arr[i-1]:
                ans += (arr[i] - arr[i-1])
    return ans

result = calculate(n,arr)

print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def parth(h,m):
    hmin=min(h)
    hmax=max(h)
    if hmax<=0:
        return m
    else:
        m=m+hmin
        for i in range(len(h)):
            h[i]=h[i]-hmin
#        print(m,h)
        i=0
        while i<len(h):
            h2=[]
            while i<len(h) and h[i]>0:
                h2.append(h[i])
                i=i+1
            if len(h2)>0:
                m=parth(h2,m)                
            i=i+1    
        return m


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

#n=4
#h=[1,2,2,1]

#n=5
#h=[3,1,2,3,1]

#n=8
#h=[4,23,75,0,23,96,50,100]

m=0
m=parth(h,m)
print(m)