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

Submission status: Accepted

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

Submission status: Accepted

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

c = 0
i = 0

while sum(h) > 0 :
    if h[i]> 0 :
        j = i
        c = c + 1 
        while j < n and h[j] > 0 :
            h[j] = h[j] - 1
            j = j + 1
    else:
        i = i + 1

print(c)

Submission status: Accepted

Solution code:
n = int(input())
h = [int(x) for x in 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, = 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()))
h.append(0)
cnt = 0
maxh = max(h)
for i in range(maxh):
    point = -1
    for j in range(n+1):
        if j <= point:
            continue
        #print(h)
        for k in range(j+1,n+1):
            #print(h)
            if j <= point:
                continue
            if (h[j] != 0 and h[k] == 0):
                for l in range(j,k):
                    h[l] -= 1
                cnt += 1
                point = k
        #print(point)
            # if k == n-1:
            #     for l in range(j,k+1):
            #         if h[l] == 0 or l == k:
            #             cnt += 1
            #             break
            #         h[l] -= 1
                #cnt += 1
                #break
            # if max(h) == 0:
            #     print(cnt)
            #     exit()
    #print(h)
#print(h)
print(cnt)

Submission status: Accepted

Solution code:
N = input()
K = list(map(int,input().split()))
L = len(K)
dicta = {i:K[i] for i in range(0, L)}
count = 0
start = 0
while True:
    start = 0
    flag = 0
    for i in range(0, L):
        if dicta[i] == 0:
            if start == 0:
                pass
            elif start == 1:
                start = 0
        elif dicta[i] != 0:
            dicta[i] -= 1
            if start == 0:
                start = 1
                flag = 1
                count += 1
    if flag == 0:
        break
print(count)

Submission status: Accepted

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

ans = 0

for i in range(N-1):
    dist = h[i+1] - h[i]

    if dist > 0:
        ans += dist

print(ans+h[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def up(x):
    return x-1

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

sum = 0

for j in range(max(h)):
    if h[0] <= 0:
        cnt = 0
    else:
        cnt = 1
    flag = 1
    for i in range(n):
        if flag == 1 and h[i] <= 0:
            flag = 0
        elif flag == 0 and h[i] > 0:
            flag = 1
            cnt += 1
    h = list(map(up, h))
    sum = sum + cnt

print(sum)

Submission status: Accepted

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

height = H[0]

for i in range(1, N):
    dif = H[i] - H[i - 1]
    
    if (dif > 0):
        height += dif

print(height)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()

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

    ans = 0
    for i in range(N):
        h = H[i]
        if h == 0:
            continue
        for j in range(h):
            for k in range(i, N):
                if H[k] >= 1:
                    H[k] -= 1
                else:
                    break
            ans += 1
    print(ans)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

# 高さが全部0になるまでやる
ans = 0
while True:
    # 高さの最大が0だったらおしまい
    if max(h) == 0:
        break

    # 区間分割する
    i = 0
    while i < N:
        if h[i] == 0:  # 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:
import numpy as np
n = int(input())
h = np.array([0] + [int(i) for i in input().split()])
cnt = 0
for i in range(max(h), 0, -1):
    hh = h >= i
    cnt += sum([not hh[j] and hh[j + 1] for j in range(n)])
print(cnt)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
while sum(h) != 0:
    count = 0
    for i in range(N):
        if h[i] > 0:
            h[i] -= 1
            count += 1
        elif h[i] == 0:
            if count == 0:
                continue
            elif count > 0:
                break
    ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
don = list(map(int, input().split()))
count = 0
while 1:
    if not don:
        break
    for i, k in enumerate(don):
        if k == 0:
            continue
        else:
            num = i
            break
    else:
        num = len(don)
    if num:
        del don[:num]
    for i, k in enumerate(don):
        if k == 0:
            num = i
            break
    else:
        num = len(don)
    for i in range(num):
        don[i] -= 1
    count += 1
print(count-1)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
m = max(l)

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

Submission status: Accepted

Solution code:
N = int(input())
li = list(map(int, input().split()))
count = 0

while(True):
  if max(li) == 0:
    break

  i = 0
  while(i < N):
    if li[i] == 0:
      i += 1
      continue

    count += 1
    while(i < N and li[i] > 0):
      li[i] -= 1
      i += 1

print(count)

Submission status: Accepted

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

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

Submission status: Accepted

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

mizu=0

while True:

    end=True
    for i in range(n):
       if h[i]!=0:
           end=False

    if end:
        break

    for l in range(n):
        if h[l]!=0:
            break

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

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

print(mizu)

Submission status: Accepted

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

ans = 0

while True:
    flag = False
    ans_tmp = ans
    for i in range(n):
        if a[i] > 0:
            a[i] -= 1
            if not flag:
                flag = True
                ans += 1
        else:
            flag = False
    if ans_tmp == ans:
        break

print(ans)

Submission status: Accepted

Solution code:
import numpy as np

#l,rまでの配列に+1する関数
def give_water(flower, lr):
    flower_with_water = [x+1 if i>=lr[0] and i<=lr[1] else x for i,x in enumerate(flower)]
    return flower_with_water

def TF_list(flower, h):
    h = np.array(h)
    TF_list_0 = []
    for i in range(len(flower)):
        TF_list_0.append(flower[i] < h[i])
    return TF_list_0

#Trueが連続しているindexの範囲を[(0, 1), (2,4)]みたいな形で出力
def list_tuple_True(flower, h, N):
    TF_list = (flower < h)
    TF_list = np.insert(TF_list, N, False)
    True_range = []
    #FLAGは一つ前のTFを表す。初期値はFalse
    FLAG = False
    for i, TF in enumerate(TF_list):
        if TF:
            if FLAG:
                #Trueが連続している場合
                pass
            else:
                #FalseからTrueに切り替わった場合
                start = i
        else:
            if FLAG:
                #TrueからFalseも切り替わった場合
                end = i-1
                True_range.append((start, end))
            else:
                #Falseが連続している場合
                pass
        FLAG = TF
    return True_range
            
N = int(input())
h = list(map(int, input().split()))

h = np.array(h)
flower = np.zeros(N)

count = 0
while not((h == flower).all()):
    water_range = list_tuple_True(flower, h, N)
    count += len(water_range)
    for lr in water_range:
        flower = give_water(flower, lr)
        
print(count)

Submission status: Accepted

Solution code:
n  = int(input())

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

cnt = 0
l = 0
r = n

while max(hs)>0:
    l = 0
    r = n

    for i in range(n):
        if hs[i] > 0:
            l = i
            break
    
    for j in range(l,n):
        if hs[j]== 0:
            r = j
            break
    
    for k in range(l,r):
        hs[k] -= 1

    cnt += 1

print(cnt)

Submission status: Accepted

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

ans=0

flag=False

for i in range(max(h)):
    for j in range(n):
        if h[j] >= i+1:
            flag=True
        if flag and h[j] <= i:
            ans+=1
            flag=False
    if flag:
        ans+=1
        flag=False

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int,input().split()))
y = 0
z = H[0]
ans = 0
while len(H) != 0:
    z = H[0]
    f = 1
    while z == 0:
        H.pop(0)
        if len(H) != 0:
            z = H[0]
        else:
            break
    for i in range(len(H)):
        x = H[i]
        if x == 0:
            ans += z
            for j in range(i):
                H[j] -= z
            z = H[0]
            f = 0
            break
        z = min(x,z)
    if f == 1:
        ans += z
        for i in range(len(H)):
            H[i] -= z
print(ans)

Submission status: Accepted

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

ans = 0

def water(h):
   global ans

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

   m = min(h)
   ans += m
   h = [i - m for i in h]
   while (0 in h):
      water(h[:h.index(0)])
      h = h[h.index(0)+1:]
   water(h)


water(h)
print(ans)

Submission status: Accepted

Solution code:
cnt = 0

n = int(input())
h = [int(_) for _ in input().split()]

now = [0] * len(h)

while True:
    cnt += 1
    chk = False
    for i in range(n):
        if h[i] == now[i]:
            if chk == True:
                break
        else:
            now[i] += 1
            chk = True
    if chk == False:
        cnt -= 1
        break

print(cnt)

Submission status: Accepted

Solution code:
#116c 尺取り法?
N = int(input()) #N <= 10**5
h_list = list(map(int, input().split()))
x_list = [0 for _ in range(N)]

ans = 0

for i in range(N):
    while h_list[i] != x_list[i]:
        #print(x_list)
        for j in range(i,N):
            if h_list[j] == x_list[j]:
                break
            else:
                x_list[j] += 1
                
        ans += 1

#print(x_list)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
c = [0] * n
ma = max(a)

ans = 0
while max(c) != ma:
    ing = False
    for i in range(n):
        if a[i] == c[i]:
            ing = False
            continue
        else:
            if not ing:
                ing = True
                ans += 1
            c[i] += 1
print(ans)

Submission status: Accepted

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

def zerosplit(l): # split by zero
  #print("zsp")
  mother = []
  child = []
  for e in l:
    if e != 0:
      child.append(e)
    elif e == 0 and len(child) != 0:
      mother.append(child)
      child = []
  if len(child) !=0:
    mother.append(child)
  return mother

def mizuyari(l): # decrease all elements by min(l)
  d = [0]*len(l)
  cnt1 = min(l)
  for i in range(len(l)):
    d[i] = l[i] - min(l)
  #print("mizuyari " + str(cnt1)+ " times")
  return [d,cnt1]
localcount = []
def mini(h): # mizuyari for splitted list
  l = zerosplit(h)
  #print(l)
  for e in l:
    #print(":in min: for"+ str(e))
    d,c = mizuyari(e)
    localcount.append(c)
    #print("localcnt " + str(localcount))    
    if len(d) != 0:
      mini(d)
  return sum(localcount)

print(mini(h))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(n, H):
    prev_h, base_h = 0, 0
    count = 0
    up = True
    for h in H:
        if up and h < prev_h:
            # 上昇から下降に移ったら、ここまで何段上ったか記録
            count += prev_h - base_h
            up = False
        elif not up and prev_h < h:
            # 下降から上昇に移ったら、底辺の高さを記録
            base_h = prev_h
            up = True
        prev_h = h
    if up:
        count += prev_h - base_h
    return count

_n = int(input())
_H = list(map(int, input().split()))
print(solve(_n, _H))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = [int(i) for i in input().split()]
c = 0
active = 0
for i in h:
    if active >= i:
        active = i 
    else:
        c += i - active 
        active = i
print(c)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
zero=[0]*n
ans=0
while set(h)!={0}:
    for i in range(n):
        if h[i]==0:
            zero[i]=1

        if i==n-1 and h[i]!=0:
            h[i]+=-1
            ans+=1
        elif h[i]>=1:
            h[i]+=-1
        elif h[i]==0 and zero[i-1]==0 and i!=0:
            zero[i]=1
            ans+=1

print(ans)

Submission status: Accepted

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

def hidarihasi(h):
    for i,j in enumerate(h):
        if j!=0:
            return i
def zero(h, start):
    for i,j in enumerate(h[start:]):
        if j==0:
            return i+start
    else:
        return len(h)

def mizuyari(h):
    global ans
    for i in range(hidarihasi(h), zero(h, hidarihasi(h))):
        h[i] = h[i] -1
    ans = ans + 1
    
    
while sum(h)!=0:
    mizuyari(h)
print(ans)

Submission status: Accepted

Solution code:
import sys

INPUT = lambda: sys.stdin.readline().rstrip()
INT = lambda: int(INPUT())
LIST = lambda: list(map(int, INPUT().split()))

sys.setrecursionlimit(10 ** 9)


def main():
    N = INT()
    H = LIST()

    ans = 0
    border = 0
    for i in range(N-1):
        if H[i] > H[i+1]:
            ans += H[i] - border
            border = H[i+1]

    ans += H[-1] - border
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

# 高さを全部0にするまで-1する
ans = 0
while True:
  # 最高高さが 0 だったらおしまい
  if max(H) == 0:
    break
  i = 0
  while i < N:
    if H[i] == 0:
      i += 1
    else:
      ans += 1 # 操作1回
      while i < N and H[i] > 0: # 1以上の連続した区間を操作する
        H[i] -= 1
        i += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    height = list(map(int, input().split(" ")))
    target = [0] * N
    result = 0
    # print(target)
    while not height == target:
        # print(height)
        end = False
        result += 1
        for i in range(N):
            if not end:
                if height[i] > 0:
                    end = True
                    height[i] -= 1
            else:
                if height[i] > 0:
                    height[i] -= 1
                else:
                    break
    print(result)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
L=[int(i) for i in input().split()]
#L.append(0)
L.insert(0,0)
ans=0
for i in range(N):
  ans+=max(0,L[i+1]-L[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def main():
    N = INT() 
    H = LIST()
    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)
    


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]

class Util():
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Util.round_up(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)

def main():
    N = cin.int()
    H = cin.mapInt()
    ans = 0
    now = 0
    for h in H:
        if now < h:
            ans += h - now
        now = h
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import queue

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

q = queue.Queue()
q.put([H,0])

num = 0
while not q.empty():
    data = q.get()
    if len(data[0]) == 0:
        continue
    if len(data[0]) == 1:
        num += data[0][0]-data[1]
        continue
    min_index = data[0].index(min(data[0]))
    num += data[0][min_index]-data[1]
    tmp = data[0][min_index]

    tmp_data = []
    for d in data[0]:
        if d == data[0][min_index]:
            q.put([tmp_data,tmp])
            tmp_data = []
        else:
            tmp_data.append(d)
    q.put([tmp_data,tmp])

print(num)

Submission status: Accepted

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

while 1:
    for i in range(N):
        if H[i]!=0:
            H_left=i
            ans+=1
            H[i]-=1
            break
    else:
        break
    
    for i in range(H_left+1,N):
        if H[i]!=0:
            H[i]-=1
        elif H[i]==0:
            break
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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

    pre = h

print(ans)

Submission status: Accepted

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

t = 1
ans = 0
while t > 0:
    p = H[-1]
    t = 0
    for i in range(N):
        if H[i] > 0 and p <= 0:
            ans += 1
        p = H[i]
        H[i] -= 1
        t = max(H[i], t)
        #print(ans, H[i], t)

print(ans)

Submission status: Accepted

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

ans = 0
for j in range(max(h),0,-1):
    cnt=0
    for i in range(N):
        if cnt==0 and h[i]==j:
            cnt=1
            h[i]-=1

        elif cnt==1 and h[i] == j:
            h[i]-=1

        elif cnt==1 and h[i]!=j:
            cnt=0
            ans +=1


    if cnt ==1:
        ans+=1
print(ans)

Submission status: Accepted

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

            
    if renzokucnt >0: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(n - 1):
    ans += max(0, 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):
    ans += max(0,H[i+1]-H[i])
print(ans)

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
#from scipy.misc import comb

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

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

print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import numpy as np
N = int(input())
h = np.array(list(map(int,input().split())))
count = 0
tmp = deque()
tmp.append(h)
while True:
    st,en = -1,-1
    if len(tmp) == 0:
        break
    count += min(tmp[0])
    tmp[0] -= min(tmp[0])
    if list(tmp[0]) == [0]*len(tmp[0]):
        tmp.popleft()
        continue
    for i in range(len(tmp[0])):
        if st == -1 and tmp[0][i] != 0:
            st = i
        if st != -1 and tmp[0][i] == 0:
            en = i
        if st != -1 and i == len(tmp[0])-1 and tmp[0][i] != 0:
            en = i+1
        if st != -1 and en != -1:
            tmp.append(tmp[0][st:en])
            st,en = -1,-1
    tmp.popleft()
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

while any(h>0 for h in H):
  it = 1
  for i in range(N-1):
    if H[i]>0 and H[i+1] <= 0:
      it += 1
  if H[-1]<=0:
    it -= 1
    
  ans += it
  H = [h-1 for h in H]
  
print(ans)

Submission status: Accepted

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

k = 0
while any([i > 0 for i in h_list]):
    bool_list = [True if i==max(h_list) else False for i in h_list]
    indexes = []
    true_index = bool_list.index(True)
    while bool_list[true_index]:
        indexes.append(true_index)
        true_index += 1
        if true_index == len(h_list):
            break

    h_list = [h_list[i]-1 if i in indexes else h_list[i] for i in range(len(h_list))]
    k += 1
  
print(k)

Submission status: Accepted

Solution code:
# ABC116c
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)

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

ans = 0
mode = 0

for i in h:
    while i > mode:
        mode += 1
        ans += 1
    while i < mode:
        mode -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys
 
stdin = sys.stdin
 
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()
nas = lambda: stdin.readline().split()

n = ni()
a = na()

res = 0
while sum(a):
    res += 1
    f = False
    for i in range(n):
        if a[i] > 0:
            a[i] -= 1
            f = True
        elif f is True:
            break

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def examA():
    L = LI(); L.sort()
    ans = L[0]*L[1]//2
    print(ans)
    return

def examB():
    def fun(s):
        if s%2==0:
            return s//2
        else:
            return s*3+1
    S = I()
    d = defaultdict(bool)
    d[S] = True; cur = 1
    while(True):
        cur +=1
        S = fun(S)
        if d[S]:
            break
        d[S] = True
    print(cur)
    return

def examC():
    N = I()
    H = LI()
    cur = 0
    ans =0
    for i in range(N):
        if cur<H[i]:
            ans += H[i]-cur
        cur = H[i]
    print(ans)
    return

def examD():
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet,ALPHABET, _ep
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
_ep = 1e-9
alphabet = [chr(ord('a') + i) for i in range(26)]
ALPHABET = [chr(ord('A') + i) for i in range(26)]

if __name__ == '__main__':
    examC()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
H=[int(i) for i in input().split()]
ans=0
while any(h>0 for h in H):
  itv=1
  for i in range(N-1):
    if H[i]>0 and H[i+1]<=0:
      itv+=1
  if H[-1]<=0:
    itv-=1
  ans+=itv
  H=[h-1 for h in H]
  #print(itv)
print(ans)

Submission status: Accepted

Solution code:
import itertools


def get_num_1group(input_list):
    num_group = 0
    for key, elems in itertools.groupby(input_list):
        if key == 1:
            num_group += 1
    return num_group


N = int(input())
H = list(map(int, input().split()))
maxH = max(H)
hist = [[0 for _ in range(N)] for _ in range(maxH)]
for idx, h in enumerate(H):
    for num in range(h):
        hist[num][idx] = 1

print(sum([get_num_1group(hist[num]) for num in range(maxH)]))

Submission status: Accepted

Solution code:
n = int(input())
h = [int(x) for x in input().split()]
ans = 0
while sum(h) != 0:
    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:
import sys

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

    ans = 0
    zero_list = [0] * N

    while True:
        for i in range(N):
            if h[i] > zero_list[i]:
                L_p = i
                R_p = i

                for j in range(L_p+1, N):
                    if h[j] > zero_list[i]:
                        R_p = j
                    else:
                        break

                for k in range(L_p, R_p+1):
                    zero_list[k] += 1

                ans += 1
                break

        if h == zero_list:
            break
    print(ans)

main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
while max(a) != 0:
  # print(a,cnt)
  betweenZero = True
  for i in range(n):
    if a[i] != 0:
      a[i] -= 1
      betweenZero = False
    elif betweenZero == False:
      cnt += 1
      betweenZero = True
  if betweenZero == False:
    cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
while max(a) != 0:
  # print(a,cnt)
  betweenZero = True
  for i in range(len(a)):
    if a[i] != 0:
      a[i] -= 1
      betweenZero = False
    elif betweenZero == False:
      cnt += 1
      betweenZero = True
  if betweenZero == False:
    cnt += 1
print(cnt)