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:
import bisect,collections,copy,heapq,itertools,math,string
import sys
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
h = LIST()
mh = max(h)
cnt = 0
for i in range(1,mh+1):
    for j in range(n):
        if j == 0:
            flag = cnt_area = 1 if h[j] >= i else 0
        elif flag == 0 and h[j] >= i:
            flag = 1
            cnt_area += 1
        else:
            flag = (h[j] >= i)
    cnt += cnt_area

print(cnt)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int,input().split()))
def g(ls):
    try:
        return ls.index(0)
    except:
        return -1
def f(ls):
    if ls==[]:
        return 0
    if len(ls) == 1:
        return ls[0]
    m = min(ls)
    ls = [x-m for x in ls]
    ans = m
    while ls:
        i = g(ls)
        if i == -1:
            ans += f(ls)
            break
        ans += f(ls[:i])
        ls = ls[i+1:]
    return ans
print(f(h))

Submission status: Accepted

Solution code:
n = int(input())
Hs = list(map(int,input().split()))
f = [0 for _ in range(n)]
c = 0
r = 0
while r != n:
    r = 1
    for i in range(n):
        if f[i] < Hs[i] :
            if r > 0:
                c += 1
            r = 0
            f[i] += 1
        else:
            r += 1
    r -= 1   
print(c)

Submission status: Accepted

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


def down(H, l, r):
    for i in range(l, r):
        H[i] -= 1


s = 0
for h in range(100):
    # LとRを求める
    l = 0
    while l < N:
        if H[l]:
            for j in range(l+1, N):
                if H[j] == 0:
                    r = j
                    break
            else:
                r = N
            down(H, l, r)
            s += 1
            l = r+1
        else:
            l += 1
print(s)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    h = in_nl()
    now = [0] * N

    ans = 0
    while h != now:
        ok = [-1] + [i for i, x in enumerate(now) if x == h[i]] + [N]
        lr = []
        for i in range(len(ok) - 1):
            if ok[i + 1] - ok[i] > 1:
                l = ok[i] + 1
                r = ok[i + 1]
                lr.append((l, r))

        for l, r in lr:
            diff = 10**9 + 7
            for i in range(r - l):
                diff = min(diff, h[l + i] - now[l + i])

            ans += diff
            for i in range(r - l):
                now[l + i] += diff

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc116/tasks/abc116_c
N = int(input())
H = list(map(int, input().split()))


def ret_n_miz(ls: list):
    ret = 0  # 水やり回数
    pre = 0  # 水やりが必要なければ0
    for l in ls:
        if pre == 0 and l > 0:
            ret += 1
        pre = l
    return ret


ans = 0
while sum(H) > 0:
    ans += ret_n_miz(H)
    for i in range(N):
        H[i] = max(0, H[i] - 1)

print(ans)

Submission status: Accepted

Solution code:
#ABC116-C
n = int(input())
h = list(map(int,input().split()))
kadan = [0 for _ in range(n)]
cnt = 0
tf = [False for _ in range(n)]

for i in range(1,10**5):
    cnt += 1
    #print('cnt:',cnt)
    #print('tf:',tf)
    #print('kadan:',kadan)
    flg = False
    for j in range(n):
        if tf[j] == False:
            flg = True
            if kadan[j] == h[j]:
                tf[j] = True
            else:    
                kadan[j] += 1
        elif tf[j] == True and flg == False:
            pass
        else:
            break
            
    if not False in tf:
        break
print(cnt-1)

Submission status: Accepted

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

ans=0
M=max(H)
count=0
while count<=M:
    if H[0]>=count:
        ans+=1
    i=1
    while i<N:
        if H[i]>=count and H[i-1]<count:
            ans+=1
        i+=1
    count+=1

print(ans-1)

Submission status: Accepted

Solution code:
n = int(input())
lst = list(map(int, input().split()))
lst.append(0)
goal = [0] * (n + 1)
watercnt = 0
if lst[0] == 0:
  phase = '-'
else:
  phase = '+'
while lst != goal:
  for i in range(n + 1):
    if phase == '+':
      if lst[i] > 0:
        lst[i] -= 1
      else:
        phase = '-'
        watercnt += 1
    else:
      if lst[i] > 0:
        lst[i] -= 1
        phase = '+'
print(watercnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A):
    MAXA = max(A)
    prev = 0
    ans = 0
    for a in A:
        if prev < a:
            diff = a - prev
            ans += diff
        prev = a
    return ans


if __name__ == '__main__':
    inputs = read()
    print("%s" % solve(*inputs))

Submission status: Accepted

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


def watering(h):
    if 0 in h:
        x = h.index(0)
        return watering(h[:x]) + watering(h[x + 1:])
    if h:
        if max(h) == 1:
            return 1
        else:
            h = list(map(lambda x: x - 1, h))
            return watering(h) + 1
    return 0



if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
 
sys.setrecursionlimit(10 ** 7)
 
from collections import defaultdict
 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
# list(map(int, input().split()))

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

X = [H]
ans = 0
while True:
    newX = []
    for group in X:
        f = min(group)
        ans += f
        A = [(h - f) for h in group]
        temp = []
        for a in A:
            if a == 0:
                if len(temp) > 0:
                    newX.append(temp)
                    temp = []
                continue
            temp.append(a)
        if len(temp) > 0:
            newX.append(temp)

    if len(newX) == 0:
        break
    X = newX
print(ans)

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    H = il()

    ret, p = 0, 0
    for n in range(N):
        if p <= H[n]:
            ret += H[n] - p
        p = H[n]
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
while h != end:
  strock = 0
  for i in range(N):
    if h[i] != 0:
      strock = 1
      h[i] -= 1
    else:
      if strock == 1:
        ans += 1
        strock = 0
  if strock == 1:
    ans += 1
  #print(h)
print(ans)

Submission status: Accepted

Solution code:
import sys, os, math, bisect, itertools, collections, heapq, queue
# from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from decimal import Decimal
from collections import defaultdict

# import fractions

sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    H = il()

    flower = [0] * N
    ret = 0
    for i in range(N):
        if flower[i] == H[i]: continue

        j = i
        while N > j:
            if H[j] == flower[j]: break
            j += 1

        diff = H[i] - flower[i]
        mi = MAX
        for n in range(i, j):
            mi = min(mi, H[n] - flower[n])
            flower[n] += min(diff, mi)
        ret += diff

    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

step = 0
while(True):
  
  if(sum(hs) == 0):
    break
  
  start = None
  stop = None
  
  for i,h in enumerate(hs):

    if(h>0 and start is None):
      start = i
    if(h==0 and start is not None and stop is None):
      stop = i
     	
  if(start is not None and stop is None):
    stop = len(hs)
    
  for i in range(start, stop):
    hs[i] -= 1
    
  step += 1
  
print(step)

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

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#import itertools
#from fractions import Fraction
#import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
#import bisect
n=int(input())
h=list(map(int,input().split()))
m=max(h)
c=0
for i in range(1,m+1):
    if h[0]>=i:
        c+=1
    for j in range(1,n):
        if h[j]>=i and h[j-1]<i:
            c+=1
print(c)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np
#from numba import njit

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

#@njit

def solve(N,h):
    s = 0
    while (np.any(h > 0)):
        w = np.where(h > 0)[0]
        l = w[0]
        r = w[0]
        i = 1
        while (i < len(w)):
            if (w[i] == r + 1):
                r = w[i]
            i += 1
        
        h = np.concatenate([h[:l], h[l:r+1]-1, h[r+1:]])
        s += 1
    return s

print(solve(N,h))

Submission status: Accepted

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

from collections import deque
queue = deque([])

queue.append(h)

ans = 0

while(len(queue) > 0):
    new_h = queue.popleft()
    ans += np.min(new_h)
    new_h -= np.min(new_h)
    
    R = np.where(new_h == 0)[0][0]
    new_L = new_h[:R]
    new_R = new_h[R+1:]
    
    if new_L.size > 0:
        queue.append(new_L)
    if new_R.size > 0:
        queue.append(new_R)
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
arr = list(map(int, input().split()))
 
count =0
while True:
    if sum(arr) == 0:
        break
    count +=1
    start = False
 
    for i in range(N):
        if arr[i] != 0:
            arr[i] -= 1
            start = True
            continue
        if arr[i] == 0 and start == True:
            break
print(count)

Submission status: Accepted

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

if sum(arr) == 0:
    print(0)
    exit()
#if N == 1:
#    print(arr[0])
#    exit()

count =0
while True:
    count +=1
    start = False

    for i in range(N):
        if arr[i] != 0:
            arr[i] -= 1
            start = True
            continue
        if arr[i] == 0 and start == True:
            break
    if sum(arr) == 0:
        break
print(count)

Submission status: Accepted

Solution code:
N = int(input())

h = [i for i in map(int, input().split())]

hmax = max(h)
hmin = min(h)


ans = hmin
for i in range(hmin+1, hmax+1):
    count = 0
    flag = False
    for j in h:
        if j < i and flag:
            flag = False
        elif j >= i and not flag:
            flag = True
            count += 1
    ans += count

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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



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

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:
#!/usr/bin/env python3

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

def isallzero(h):
    ans = True
    for i in h:
        if i != 0:
            ans = False
            break
    return ans 

def func(h):
    if isallzero(h):
        return -1
    for i in range(len(h)):
        if h[i] != 0:
            return i

ans = -1
while True:
    ans += 1
#    print('h =', h)
#    print('isallzero() =', isallzero(h))
    if isallzero(h):
        break

    m = func(h)
    for i in range(m, n): 
        if h[i] != 0:
            h[i] -= 1
        else:
            break

print(ans)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    hs = list(map(int, input().split()))

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

h_state=[0]*n

def water(n,h,h_state):
    for i in range(n):
        if h_state[i]<h[i]:
            l=i
            break
        elif i==n-1 and h_state[i]<h[i]:
            l=i
        else:
            l=i+1
    
    for j in range(l,n):
        if h_state[j]==h[j]:
            r=j-1
            break
        else:
            r=j
    
    for k in range(l,r+1):
        h_state[k]+=1

cnt=0
while h_state != h:
    for i in range(n):
        if h_state[i]<h[i]:
            cnt+=1
            water(n,h,h_state)
            break
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

i=0
count=0
while i<=n-1:
    if h[i]==0:
        i+=1
    else:
        h[i]-=1
        count+=1
        k=i+1
        for l in range(k,n):
            if h[k]>0:
                h[k]-=1
                k+=1
            else:
                break

print(count)

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
ans = 0
for k in range(100, 0, -1):
    ans += sum(hi == k for hi in h)
    ans -= sum(hi == hj == k for hi, hj in zip(h, h[1:]))
    h = list(map(lambda x: min(x, k - 1), h))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

def cnt(A, x):
    N = len(A)
    i = 0
    ret = 0
    while i<N:
        if A[i]<x:
            i += 1
        else:
            ret += 1
            while i<N and A[i]>=x:
                i += 1
    return ret

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

    ret = 0
    for i in range(1,101):
        ret += cnt(A,i)

    print(ret)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

while max(h) > 0:
    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()))

i = min(h)
ans = i
h = list(map(lambda x:x-i,h))

ck = 0
while sum(h) > 0:
  for i in range(n):
   if h[i] == 0:
     ans += ck
     ck = 0
   else:
     h[i] -= 1
     ck = 1
  else:
   ans += ck
   ck = 0

print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

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

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=0
temp=[0]*n
while True:
  a=0
  b=1
  cnt=1
  for i in range(n):
    if h[i]!=temp[i]:
      temp[i]=temp[i]+1
      a=1
      b=0
    elif a==1:
      cnt=cnt+1
      a=0
      b=1
  if b==1:
    cnt=cnt-1
  ans=ans+cnt
  if sum(temp)==sum(h):
    break
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n, *h = map(int, open(0).read().split())
a = h[0]
for i in range(n-1):
    if h[i+1] - h[i] > 0:
        a += h[i+1] - h[i]
print(a)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N = i()
h = l()
ans = 0
start = 0
Flag = 0
for i in range(N-1):
    if h[i] > h[i+1]:
        Flag = 1
    if h[i] < h[i+1] and Flag == 1:
        kukan = h[start:i+1]
        ans -= h[i]
        ans += max(kukan)
        start = i+1
        Flag = 0
kukan = h[start:N]
ans += max(kukan)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
h = list(map(int, input().split()))
l = [0]
l.extend(h)
l.append(0)
n += 2
ans = 0
while sum(l) != 0:
    for i in range(1, n):
        if (l[i] == 0) and (l[i-1] != 0):
            ans += 1
    for i in range(1, n):
        if l[i] != 0:
            l[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(1, N):
  if h[i] > h[i - 1]:
    ans += h[i] - h[i - 1]
print(ans)

Submission status: Accepted

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

kaisu=0

while max(h_ls)>0:
	M=h_ls.index(max(h_ls))
	takasa=h_ls[M]
	i=0
	while M+i<N and h_ls[min(M+i,N-1)]==takasa:
		h_ls[M+i]+=(-1)
		i+=1
	
	kaisu+=1

print(kaisu)

Submission status: Accepted

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

searchmin=10**4
pos=0
start=0
end=0
lenS=len(S)
count=0

def startendsearch():
    atta=False
    start=0
    end=0
    for i in range(lenS):
        if S[i]!=0 and atta==False:
            start=i
            atta=True
        if S[i]!=0 and atta==True:
            end=i
        if S[i]==0 and atta==True:
            break
    
    if atta==True:
        return start,end
    else:
        return 200,200
while sum(S)>0:
    s,e= startendsearch()
    
    #print(s,e)
    for i in range(s,e+1):
        S[i]-=1
        
    #print(S)
    count+=1
print(count)

Submission status: Accepted

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

# 高さが小さいものの方が早く目標に到達する
# どの花も目標に到達していないときは全てをプラス1
# 到達した花がある場合はそれを基準に2つに分け、同様に作業をする
count = 0
while True:
    if (n == 0):
        break
    p = 0
    check = 0
    for i in range(n):
        if (i == 0 and h_list[i] == 0):
            h_list.pop(i)
            # 数えたくないbreakよりcheckを増やし、あとでcountをcheckで引く
            check += 1
            n -= 1
            break
        elif (h_list[i] != 0):
            if (h_list[i] > 0):
                h_list[i] -= 1

        elif (i != 0 and h_list[i] == 0):
            break

    p += 1
    count += p
    count -= check

print(count)

Submission status: Accepted

Solution code:
n = int(input())
h_list = list(map(int,input().split()))
count = 0
for i in range(1,max(h_list) + 1):
  p = 0
  check = False
  for j in range(n):
    if i > h_list[j]:
      if check == True:
        p += 1
      check = False
      
    else:
      if j == n-1:
        p += 1
      check = True
  count += p
  
print(count)

Submission status: Accepted

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

base = min(h)
count = 0
already_zero = 0
more = 0

while 1:
  if max(H) == 0:
    break
  else:
    for i in range(N):
      if H[i] == 0 and more == 1:
        count += 1
        more = 0
        if i == N - 1:
          break
      else:
        if H[i] != 0:
          H[i] -= 1
          if more == 0:
            more = 1
      
      if more == 0 and i == N - 1:
        break
      
    else:
      count += 1
      more = 0
      already_zero = 0
      
print(count)

Submission status: Accepted

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

A = [[0]*N for _ in range(101)]
#A = [[0 for i in range(N)] for j in range(101)]
for i in range(101):
    for j in range(N):
        if H[j] > 0:
            A[i][j]=1
            H[j]-=1
        else:
            H[j] = 0
ans = 0
for i in range(101):
    flag = False
    for j in range(N):
        if A[i][j] == 1 and flag == False:
            flag = True
        elif A[i][j] == 0 and flag == True:
            ans += 1
            flag = False
    if flag:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())

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

ans = lis[0]

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

Submission status: Accepted

Solution code:
num = int(input())
input_line = list(map(int,input().split(' ')))
flower = [0 for i in range(num)]
cnt = 0
while(True):
    flg = 0
    for n in range(num):
        if(flower[n] != input_line[n]):
            flower[n] += 1
            flg = 1
        elif(flg):
            flg = 0
            cnt += 1
        if(flg and (n==(num-1))):
            cnt += 1
    if(sum(flower) == sum(input_line)):
        break
print(cnt)

Submission status: Accepted

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

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


ans = 0
for num in reversed(range(1, max(h)+1)):
    # print("num", num)
    # print(h)
    count = 0
    for j in range(0, n-1):
        if h[j] == num and h[j+1] < num:
            count += 1
        if h[j] == num:
            h[j] = num-1
    if h[-1] == num:
        count += 1
        h[-1] = num-1
    ans += count

print(ans)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    
    ans = 0
    pre = 0
    for i in A:
        if pre < i:
            ans += i-pre
        pre = i
    
    return ans

print(solve())

Submission status: Accepted

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

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

        pre = i

    print(ans)


resolve()

Submission status: Accepted

Solution code:
n = int(input())
hh = tuple(map(int, input().split()))
h = [0] * n

prev = 0
ans = 0
while True:
    x = 0
    y = 0
    while x < n:
        if h[x] == hh[x]:
            x += 1
            continue

        ansadd = hh[x] - h[x]
        y = x + 1
        while y < n and h[y] < hh[y]:
            ansadd = min(ansadd, hh[y] - h[y])
            y += 1
        for i in range(x, y):
            h[i] += ansadd
        ans += ansadd

    if ans == prev:
        break
    prev = ans
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
h=list(map(int,input().split()))
l=-1
r=-1
i=0
ans=0
while sum(h)!=0:
	if l==-1 and h[i]!=0:
		l=i
	if l!=-1 and h[i]==0:
		r=i-1
	elif r==-1 and i==n-1:
		r=i
	if l!=-1 and r!=-1:
		tmp=min(h[l:r+1])
		ans+=tmp
		for j in range(l,r+1):
			h[j]-=tmp
		l=-1
		r=-1
		i=-1
	i+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
H=list(map(int,input().split()))
h=min(H)
count=min(H)
flag=False
while max(H)>=h:
    for i in range(N):
        if flag and H[i]<=h:
            flag=False
            count+=1
        elif flag==False and H[i]>h:
            flag=True
        if i==N-1 and flag:
            count+=1
            flag=False
    h+=1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    h = list(map(int, input().split()))
    x = [0] * n
    ans = 0
    
    for _ in range(110):
        pre  = False
        for i in range(n):
            if x[i] < h[i]:
                x[i] += 1
                if pre == False:
                    ans += 1
                    pre = True
            else:
                pre = False 
            
    print(ans)
    
    
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N=int(input())
H=list(map(int, input().split()))
ans=now=0
for h in H:
  if now<h:
    ans+=h-now
  now=h
print(ans)

Submission status: Accepted

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

res = 0
while max(h) > 0:
    i = 0
    while i < n:
        if h[i] == 0:
            i += 1
            continue

        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()))

res = 0
while max(h) > 0:
    first_flag = True
    for i in range(n):
        if h[i] == 0:
            first_flag = True
            continue

        if first_flag:
            res += 1
            first_flag = False
        h[i] -= 1

print(res)