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: Wrong Answer

Solution code:
N = int(input())
h = list(map(int, input().split()))
ans = 0
def rec(l, r, h, ans):
    if sum(h) == 0:
        return ans
    zero_index = [l-1]
    for i in range(l, r+1):
        if h[i] == 0:
            zero_index.append(i)
    zero_index.append(r+1)
    ans += len(zero_index) - 1
    h = [max(0, hi-1)for hi in h]
    zero_index = [l-1]
    for i in range(l, r+1):
        if h[i] == 0:
            zero_index.append(i)
    zero_index.append(r+1)
    for i in range(len(zero_index)-1):
        if abs(zero_index[i] - zero_index[i+1]) <= 1:
            continue
        ans = rec(zero_index[i]+1, zero_index[i+1]-1, h, ans)
    return ans
ans = rec(0, N-1, h, ans)

print(ans)

Submission status: Wrong Answer

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

mi = min(H)
for i in range(N) :
    H[i] -= mi
ans += mi
print(H,ans)
for i in range(N) :
    a = H[i]
    H[i] = 0
    for j in range(i+1,N+1) :
        if a <= H[j] :
            H[j] -= a
        else :
            
            ans += a-H[j]
            a = H[j]
            break
        if a == min(H[i:]) :
            ans += a
        
    #print(H)
    #print(ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
flower = input().split()
for i in range(n):
    flower[i] = int(flower[i])
print(flower)
a = 0
def water():
    if sum(flower) == 0:
        a = -1
    elif 0 in flower:
        i = 0
        while flower[i] == 0:
            i += 1
        j = i
        try:
            while flower[j+1] != 0:
                j += 1
        except:
            abc = i
        for k in range(i,j+1):
            flower[k] -= 1
    else:
        for i in range(n):
            flower[i] -= 1
ans = 0
while sum(flower) != 0:
    water()
    ans += 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
flowers = list(map(int, input().split()))
flowers.append(0)
cnt = 0
if(all([x<=0 for x in flowers])): print(0)
else: 
    for i in range(sum(flowers)):
        if(all([x<=0 for x in flowers])): break
        #print(flowers, i, cnt)
        for j in range(n-2):
            if(flowers[j] > 0 and flowers[j+1] <= 0 and any([x>0 for x in flowers[j+2:]])): cnt+=1
        flowers = list(map(lambda x: (x-1), flowers))
    print(i + cnt)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n = int(input())
h = list(map(int, input().split()))
cnt = 0
while h:
    for l in reversed(range(len(h))):
        if not h[l]:
            l += 1
            break
    for i in range(l, len(h)):
        h[i] -= 1
    while h and not h[-1]:
        h.pop()
    cnt += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

h = np.array(h)

count = 0
while(sum(h) != 0):
    s = 200
    e = n
    for i in range(n):
        if(s == 200):
            if(h[i] != 0): s = i
        elif(h[i] == 0):
            e = i
            break

    count += min(h[s:e])
    h[s:e] -= min(h[s:e])

    print(h)

print(count)

Submission status: Wrong Answer

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

def remove_dpl(x):
  it=iter(x)
  before=next(it)
  res=[before]
  for e in it:
    if e!=before:
      res.append(e)
      before=e
  return res

c=0
if n==1:c+=h[0]
h=remove_dpl(h)
while len(h)!=1:
  if h[0]>h[1]:
    c+=h[0]-h[1]
    h[0]=h[1]
  if h[len(h)-2]<h[len(h)-1]:
    c+=(h[len(h)-1]-h[len(h)-2])
    h[len(h)-1]=h[len(h)-2]
  for i in range(1,len(h)-1):
    if h[i]>h[i-1] and h[i]>h[i+1]:
      c+=h[i]-max(h[i-1],h[i+1])
      h[i]=max(h[i-1],h[i+1])
  h=remove_dpl(h)
print(c+h[0])

Submission status: Wrong Answer

Solution code:
n = int(input())
flowers = list(map(int, input().split()))
cnt = 0
if(all([x<=0 for x in flowers])): print(0)
else: 
    for i in range(sum(flowers)):
        if(all([x<=0 for x in flowers])): break
        #print(flowers, i, cnt)
        for j in range(n-2):
            if(flowers[j] > 0 and flowers[j+1] <= 0): cnt+=1
        flowers = list(map(lambda x: (x-1), flowers))
    print(i + cnt)

Submission status: Wrong Answer

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

h = np.array(h)


def calc(h, s):
#     print(h, s)
    a = np.min(h)
    s += a
    h = h - a
    ind = np.where(h == 0)[0][0]
    h1 = h[:ind]
    h2 = h[ind+1:]
    if len(h1) != 0:
        s = calc(h1, s)
    if len(h2) != 0:
        s = calc(h2, s)
    return s

calc(h, 0)

Submission status: Wrong Answer

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

min = []
max = []
ans =0
while l !=[0]*N:
    ans += 1
    s =False
    for i,j in enumerate(l):
        if s:
            if j == 0:
                break
        if j !=0:
            s =True
            l[i]-=1
    print(l)
print(ans)

Submission status: Wrong Answer

Solution code:
def abc116_c():
    import numpy as np
    n = int(input())
    h = np.array(list(map(int, input().split())))
    ans = 0
    left = 0
    for i in range(1, n - 1):
        if h[i] < h[i - 1] and h[i] < h[i + 1]:
            ans += max(h[left:i]) - h[i]
            left = i
    ans += max(h[left:])
    print(ans)


abc116_c()

Submission status: Wrong Answer

Solution code:
N=int(input())
h=input().split()
h.append(-1)
index=0
count=0
while True:
    flag=True
    while flag:
        for i in range(index,len(h)):
            h[i]=int(h[i])
            if not h[i]-1<0:
                h[i]-=1
            elif h[index]==0:
                flag=False
                break
            else:
                break
        count+=1
        #print(h)
    counter=True
    while counter:
        index+=1
        if not h[index]==0:
            counter=False

    if h[index]==-1:
        print(count)
        exit(0)

Submission status: Wrong Answer

Solution code:
N = int(input())
h = list(map(int,input().split()))
for i in range(1,10001):
    f = False
    for j in range(N):
        if h[j] > 0:
            h[j] -= 1
            f = True
        elif h[j] <= 0 and f:
            break
    if h == [0]*N:
        ans = i
        break
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
import numpy as np
h= np.array(list(map(int,input().split())))
h=np.append(h,[0])
m=np.max(h)
count=0
if N==1:
    print(max)
else:
    for i in range(m):
        for j in range(N):
            if h[j]>0 and h[j+1]<=0:
                count+=1
        h=h-1
    print(count)

Submission status: Wrong Answer

Solution code:
number = int(input())
data = list(map(int, input().split()))
ans = 0

def change(left, right):
    global data, ans, number
    hiku = min(data[left:right])
    for i in range(left, right):
        data[i] -= hiku
    ans += hiku
    # print(ans, data)

def main():
    global data, ans, number
    # ans = 0
    while 1:
        if data.count(0) == number:
            break
        for i in range(number):
            if data[i] == 0:
                continue
            for j in range(i, number):
                if data[j] == 0:
                    change(i, j)
                    # ans += 1
                    break
                elif j == number -1:
                    change(i, j+1)
                    # ans += 1
                    break
            # print(data)
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
s = int(input())
l = {s}
if s % 2 == 0:
    s //= 2
else:
    s = s * 3 + 1
i = 1
while s not in l:
    l.add(s)
    if s % 2 == 0:
        s //= 2
    else:
        s = s * 3 + 1
    i += 1
else:
    i += 1
print(i)

Submission status: Time Limit Exceeded

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

ans=False
cnt=0
while ans==False:
    cnt+=1
    m=max(H)
    for i in range(H.index(m), L):
        if H[i]==m:
            H[i]-=1
        else:
            break
    if sum(H)==0: ans=True
print(cnt)

Submission status: Time Limit Exceeded

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

cnt = 0
while True:
    fl = False
    if max(h) == 0:
        break

    i = 0
    while i < N: 
        if h[i] == 0:
            continue
        else:
            cnt += 1
            while i < N and h[i] > 0: 
                h[i] -= 1
                i += 1

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n=int(input())
h=list(map(int,input().split()))
h=np.array(h)
ans=0
while True:
  for i in range(n):
    k=n-i
    cnt=0
    for j in range(1,k):
      if all(hh!=0 for hh in h[i:i+j+1]):
        cnt+=1
    h[i:i+cnt+1]=-1
    ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = list(map(int,input().split()))
c = 1
for q in range(10000000):
  for i in range(n-1):
    if l[i]==0:
      continue
    elif l[i]<=l[i+1] and i!=n-2:
      l[i] -= 1
    elif l[i]<=l[i+1] and i==n-2:
      l[i] -= 1
      l[i+1] -=1
    else:
      c += 1
      l[i] -= 1
      break

print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

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

ans = 0

def solve(l, r):
    global ans
    if l >= r:
        return
    if 0 not in h[l:r]:
        minh = min(h[l:r])
        ans += minh
        for i in range(l, r):
            h[i] -= minh
        solve(l, r)
    else:
        for i in range(l, r):
            if h[i] == 0:
                solve(l, i)
                solve(i + 1, r)
solve(0, n)

print(ans)

Submission status: Time Limit Exceeded

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

ans = min(h)
h_ = [n - ans for n in h] + [0]
j = -1
while max(h_) > 0:
    for i, n in enumerate(h_):
        if n == 0:
            if (j + 1) < i:
                m = min(h_[j+1:i])
                ans += m
                for k in range(j+1, i):
                    h_[k] = h_[k] - m
            j = i
    #print(h_)

print(ans)

Submission status: Time Limit Exceeded

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


ans = min(h)
h_ = [n - ans for n in h]
j = -1
while max(h_) > 0:
    for i, n in enumerate(h_):
        if n == 0:
            if (j + 1) < i:
                m = min(h_[j+1:i])
                ans += m
                for k in range(j+1, i):
                    h_[k] = h_[k] - m
            j = i
    if (j + 1) < N:
        m = min(h_[j+1:])
        ans += m
        for k in range(j+1, N):
            h_[k] = h_[k] - m

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = [0]+list(map(int,input().split()))+[0]
s = 0
while len(set(l))!=1:
  for i in range(n+1):
    if l[i]==0:
      if l[i+1]==0:
          break
      for j in range(i+1,n+2):
        if l[j]!=0:
          l[j] -=1
          continue
        else:
          s += 1
          break
print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
h = list(map(int, input().split()))
x = [0 for i in range(n)]
cnt = 0
flag = False
while True:
    id = 0
    jd = 0
    ff = False
    for i in range(n):
        if x[i] < h[i]:
            flag = True
            id = i
            jd = i
            ff = True
            for j in range(i+1, n):
                if x[j] < h[j]:
                    jd = j
                else:
                    break
        if ff:
            break
    for i in range(id, jd+1):
        x[i] += 1
    if flag:
        cnt += 1
    f = True
    for i in range(n):
        if h[i] != x[i]:
            f = False
            break
    if f:
        break
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import sys
from bisect import *
from heapq import *
from collections import *
from itertools import *
from functools import *
from math import *
from fractions import *

sys.setrecursionlimit(100000000)
input = lambda: sys.stdin.readline().rstrip()

def main():
    N = int(input())
    h = list(map(int, input().split()))
    ans = 0
    while sum(h) > 0:
        for i in range(N):
            for j in reversed(range(1, N - i + 1)):
                if min(h[i:i + j]) > 0:
                    for k in range(i, i + j):
                        h[k] -= 1
                    ans += 1
                    break
            else:
                continue
            break
    print(ans)

main()

Submission status: Time Limit Exceeded

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
    
    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: Time Limit Exceeded

Solution code:
count = 0

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

    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:
            rec(l, i)
            rec(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()))

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

Submission status: Time Limit Exceeded

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

ans = 0
while True:
    minh = 100
    sub = 0
    for i in range(n):
        if h[i] != 0:
            break

    for fin in range(i, n):
        if h[fin] == 0:
            fin -= 1
            break
    for j in range(i,fin + 1):
        if minh > h[j]:
            minh = h[j]
            sub = j
    minus = h[sub]
    for k in range(i, fin + 1):
        h[k] -= minus
    ans += minus
    
    if h.count(0) == n:
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                if len(harray) == 2:
                    harray[1] -= a
                else:
                    for y in range(1,N):
                        harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-1])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                if zeroarray[0]+1 != N:
                    for y in range(zeroarray[0]+1,N):
                            harray[y] -= a
                else:
                    harray[N-1] -= a
                counter += a
        else:
            for k in range(len(zeroarray)-1):
                if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]):
                        harray[y] -= a
                    counter += a
                if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    if zeroarray[-1]+1 != N:
                        for y in range(zeroarray[-1]+1,N):
                            harray[y] -= a
                    else:
                        harray[N-1] -= a
                    counter += a

print(counter)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                for y in range(1,N):
                    harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-1])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                if zeroarray[0]+1 != N:
                    for y in range(zeroarray[0]+1,N):
                            harray[y] -= a
                else:
                    harray[N-1] -= a
                counter += a
        else:
            for k in range(len(zeroarray)-1):
                if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]):
                        harray[y] -= a
                    counter += a
                if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    if zeroarray[-1]+1 != N:
                        for y in range(zeroarray[-1]+1,N):
                            harray[y] -= a
                    else:
                        harray[N-1] -= a
                    counter += a
        
print(counter)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                for y in range(1,N):
                    harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-1])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                for y in range(zeroarray[0]+1,N):
                    harray[y] -= a
                counter += a
        else:
            for k in range(len(zeroarray)-1):
                if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]):
                        harray[y] -= a
                    counter += a
                if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    if zeroarray[-1]+1 != N:
                        for y in range(zeroarray[-1]+1,N):
                            harray[y] -= a
                    else:
                        harray[N-1] -= a
                    counter += a
        
print(counter)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                for y in range(1,N):
                    harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-1])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                for y in range(zeroarray[0]+1,N):
                    harray[y] -= a
                counter += a
        else:
            for k in range(len(zeroarray)-1):
                if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]+1):
                        harray[y] -= a
                    counter += a
                if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    for y in range(zeroarray[-1]+1,N):
                        harray[y] -= a
                    counter += a
        

print(counter)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
array = input().split(' ')
harray = []
for z in array:
    z = int(z)
    harray.append(z)
counter = 0
while harray != [0]*N:
    if 0 not in harray:
        a = min(harray)
        for i in range(len(harray)):
            harray[i] -= a
        counter += a
    else:
        zeroarray = [j for j, x in enumerate(harray) if x == 0]
        if len(zeroarray) == 1:
            if zeroarray[0] == 0:
                a = min(harray[1:])
                for y in range(1,N):
                    harray[y] -= a
                counter += a
            elif zeroarray[0] == N-1:
                a = min(harray[:N-1])
                for y in range(N-1):
                    harray[y] -= a
                counter += a
            else:
                a = min(harray[:zeroarray[0]])
                for y in range(zeroarray[0]):
                    harray[y] -= a
                counter += a
                a = min(harray[zeroarray[0]+1:])
                for y in range(zeroarray[0]+1,N):
                    harray[y] -= a
                counter += a
        else:
            for k in range(len(zeroarray)-1):
                if zeroarray[0] != 0:
                    a = min(harray[:zeroarray[0]])
                    for y in range(zeroarray[0]):
                        harray[y] -= a
                    counter += a
                if zeroarray[k+1] - zeroarray[k] == 1:
                    continue
                else:
                    a = min(harray[zeroarray[k]+1:zeroarray[k+1]])
                    for y in range(zeroarray[k]+1,zeroarray[k+1]):
                        harray[y] -= a
                    counter += a
                if zeroarray[-1] != N-1:
                    a = min(harray[zeroarray[-1]+1:])
                    for y in range(zeroarray[-1]+1,N):
                        harray[y] -= a
                    counter += a
        

print(counter)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
def block():
    block=0
    count=0
    while count<=(n-1):
        if h[count]>=1:
            block+=1
            count+=1
            while count<=(n-1):
                if h[count]>=1:
                    count+=1
                else:
                    count+=1
                    break
    return block
                
n=int(input())
h=list(map(int,input().split()))
ans=0
while sum(h)>0:
    bl=block()
    mini=min(h)
    ans+=mini*bl
    for i in range(n):
        h[i]-=mini
    
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

cnt=0
while any([i!=0 for i in h]):
    start,end=100,100
    for i in range(n+2):
        if h[i]!=0:
            start=min(start,i)
        elif h[i]==0 and start<i:
            end=min(end,i)
    l=[i-1 for i in h[start:end]]
    h=h[:start]+l+h[end:]
    cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
h = list(map(int, input().split()))
cnt = 0
start = 0
end = 0
while True:
    for i in range(N):
        if h[i] != 0:
            start = i
            break

    end = N
    for i in range(start + 1, N):
        if h[i] == 0:
            end = i
            break

    for i in range(start, end):
        h[i] = h[i] - 1

    cnt += 1
    start = 0
    end = 0
    if sum(h) == 0:
        break

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
h = list(map(int,input().split()))
count = 0
done = False
while done == False:
    for i in range(n):
        if h[i] >= 0:
            h[i] -= 1
        
        if i > 0 and h[i] < 0 and h[i-1] >= 0:
            count += 1

        if i == n-1 and h[i] >= 0:
            count += 1
    if max(h) == 0:
        done = True
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
h = list(map(int,input().split()))
count = 0
done = False
while done == False:
    for i in range(n):
        if i > 0 and h[i] <= 0 and h[i-1] >= 0:
            count += 1

        elif i == n-1 and h[i] > 0:
            count += 1

        h[i] -= 1
    if max(h) == 0:
        done = True
print(count)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
h = list(map(int,input().split()))
count = 0
done = False
h_max = -1
while done == False:
    start = 0
    for i in range(n):
        if i > 0 and h[i-1] >= 0 and h[i] <= 0:
            count += 1
        elif i == n-1 and h[i] > 0:
            count += 1
        h[i] -= 1

        if h_max < h[i]:
            h_max = h[i]
    if h_max == 0:
        done = True
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
h = list(map(int,input().split()))
h.insert(0,0)
h.append(0)
temp = []
cnt = 0

while h.count(0) <= n+2:
    for i in range(n+1):
        if h[i] == 0 and h[i+1] != 0:
            temp.append(h[i+1])
            s_ind = i+1
        if h[i] != 0 and h[i+1] != 0:
            temp.append(h[i+1])
        if h[i] != 0 and h[i+1] == 0:
            f_ind = i
            cnt += min(temp)
            temp = [j-min(temp) for j in temp]
            h = [h[k] if k not in range(s_ind,f_ind+1) else temp[k - s_ind] for k in range(n+2)]
            
print(cnt)

Submission status: Time Limit Exceeded

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

res = 0; i = 0
while True:
    if max(H) == 0:
        break

    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: Time Limit Exceeded

Solution code:
def main():
    n = int(input())
    h_li = list(map(int, input().split()))
    i = 0
    count = 0
    f = 0
    discount = max(h_li)
    while True:
        if h_li[i] >= discount:
            count += 1
            h_li[i] -= 1
            while i < len(h_li) - 1:
                if h_li[i + 1] >= discount:
                    i += 1
                    h_li[i] -= 1
                else:
                    i += 1
                    break
        if i == len(h_li) - 1:
            i = 0
            discount -= 1
            if max(h_li) == 0:
                f = 1
        else:
            i += 1
        if f == 1:
            break
    print(count)
if __name__ == '__main__':
    main()