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:
import sys
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 10 ** 9 + 7

n = I()
hl = LI()
wl = []
count = 0

len_h = len(hl)

def m1(n):
    if n > 0:
        n = n - 1
    else:
        n = 0
    return n

max_hana = max(hl)
if max_hana == 0:
    print(0)

for i in range(max_hana):
    for j in range(len_h):
        if j == 0:
            if hl[j] > 0:
                count += 1
        else:
            if hl[j] > 0 and hl[j-1] == 0:
                count += 1
            elif hl[j] > 0 and hl[j-1] == 0:
                count += 1
    hl = list(map(m1, hl))

print(count)

Submission status: Wrong Answer

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

ans = 0

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

# print(h)
print(ans)

Submission status: Wrong Answer

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

def main():
    n = int(input())
    h = input_list()
    count = 0
    f = True
    while True:
        if list(set(h)) == [0]:
            break
        count += 1
        for i, v in enumerate(h):
            if v == 0 and f:
                f = False
                break
            if v > 0:
                h[i] -= 1
                f = True
    print(count)


def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# import sys
# input = sys.stdin.readline
# import re
import collections
import bisect
import math
import fractions
import collections
import itertools
from functools import reduce

def main():
    n = int(input())
    h = list(input_list())
    count = 0
    while True:
        if len(set(h)) == 1:
            break
        flag = False
        count += 1
        for i, v in enumerate(h):
            if v != 0:
                flag = True
                h[i] -= 1
            else:
                if flag:
                    break
    print(count)

def minus(numbers):
    for i, n in enumerate(numbers):
        if n == 0:
            break
        numbers[i] -= 1
    return numbers


def input_list():
    return list(map(int, input().split()))


def input_list_str():
    return list(map(str, input().split()))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
h=[0]+list(map(int,input().split()))+[0]
ma=0
mi=float('inf')
ans=0
for i in range(n+1):
  if h[i]<=h[i+1]:
    ma=max(ma,h[i+1],h[i])
    mi=min(mi,h[i])
  else:
    ans+=ma-mi
    ma=0
    mi=float('inf')
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
h=[0]+list(map(int,input().split()))+[0]
ma=0
mi=float('inf')
ans=0
for i in range(n+1):
  if h[i]<=h[i+1]:
    ma=max(ma,h[i+1])
    mi=min(mi,h[i])
  else:
    ans+=ma-mi
    ma=0
    mi=float('inf')
print(ans)

Submission status: Wrong Answer

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

mil=min(l)
ml=[i-mil for i in l]

if mil==0:
  ans=0
else:
  ans=1

lis=[0,ml[0]]
for i in range(n-1):
  if ml[i+1]>=ml[i]:
    lis.append(ml[i+1])
  else:
    num=max(lis)-min(lis)
    ans+=num
    lis=[ml[i+1]]
    
print(ans+max(lis)-min(lis))

Submission status: Wrong Answer

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

mil=min(l)
ml=[i-mil for i in l]

if mil==0:
  ans=0
else:
  ans=1

lis=[0,ml[0]]
for i in range(n-1):
  if ml[i+1]>=ml[i]:
    lis.append(ml[i+1])
  else:
    num=max(lis)-min(lis)
    ans+=num
    lis=[ml[i+1]]
    
print(ans+max(lis)-min(lis))

Submission status: Wrong Answer

Solution code:
def grand_garden(n, h):

    count = 0
    j = 100
    for i in range(n):
        if h[i] != 0:
            j = i
            break
    if j == 100:
        return count

    while h[j] != 0:
        l = j  # 左はしきめる
        r = l  # 動く右はし
        for k in range(l, n):  # 右端はZeroでないかぎりのばす、ゼロになる最初の位置を取得、すべてゼロでなければ配列の右端
            if k == n-1 and h[k] != 0:
                r = n
            elif h[k] == 0:
                r = k
                break
        print(l, r)
        min_val = min(h[l:r])  # 右はしから左はしまでの最小値を取得
        for x in range(l, r):  # 各要素から共通の最小値を差し引く
            h[x] -= min_val
        count += min_val  # 最小値分カウントを追加
        if h[j] == 0:  # もしも左端がゼロだったらゼロでない最初の位置へ移動
            for t in range(j, n):
                if h[t] != 0:
                    j = t
                    break
        print(j, h)
        if j >= n:
            break
    return count


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

    answer = grand_garden(n, h)
    print(answer)


'''
数列に0が含まれない最大の幅を取得(0ではない最小の左から最大の右でもよいのか)
その幅に含まれる要素高さを1ずつへらす、
count +=1

0 <= l,r <= n-1
while True:
    if h[i] == 0:
        l += 1
        i += 1
        continue
        if l >= n:
            break

   for i in range(n):
        if h[i] != 0:
            l = i
            r = l
            for j in range(l, n):
                if h[j] != 0:
                    r += 1
            for k in range(l, r):
                h[k] -= min(h[l:r])
#            if h[l] != 0:
#                i -= 1
            count += 1


'''


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())
h = list(map(int, input().split()))
max_ = 100*50
h = [0] + h + [0]
cnt = 0
for _ in range(max_):
    for i in range(1, N+1):
        if h[i] == 0 and h[i-1] != 0:
            cnt += 1
            break
        if h[i] > 0:
            h[i] -= 1
            if h[i] == 0 and h[i+1] == 0:
                cnt += 1
                break
        if i == N:
            cnt += 1
            break
    if sum(h) == 0:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
n = int(input())
h = list(map(int,input().split()))
h.append(101)
ans = 0
start = 0
i = 0
while h[n-1]!=0:
    h[i] -= 1
    if h[i]==0:
        start = i+1    
    if h[i]>=h[i+1] or h[n-1]==0 or i==n-1:
        ans += 1
        i = start
        continue
    i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
h = list(map(int,input().split()))
h.append(0)
ans = 0
while h!=[0]*(N+1):
  for l in range(N):
    if h[l]:
      mintmp = 10**3
      for j in range(l,N+1):
        if h[j]!=0: mintmp = min(mintmp, h[j])
        else: r = j; break
      for j in range(l,r): h[j] -= mintmp
      ans += mintmp
print(ans)

Submission status: Wrong Answer

Solution code:
def splitcount(l):
    if len(l) == 0:
        return 0
    minl = min(l)
    print("l",l)
    if minl==0:
        print("l[:max(0,l.index(0))]",l[:max(0,l.index(0))])
        print("splitcount(l[l.index(0)+1:])",splitcount(l[l.index(0)+1:]))
        return splitcount(l[:l.index(0)]) + splitcount(l[l.index(0)+1:])
    else:
        return minl + splitcount(list(map(lambda x: x - minl, l)))

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


print(splitcount(h))

Submission status: Wrong Answer

Solution code:
a = int(input())
b = [int(s) for s in input().split()]
sumb = sum(b)
c = int(0)
d = int(0)
ans = int(0)

while sumb != 0:
    for i in range(a):
        #引く数を探すフェーズ
        if i != a-1 and b[i] != 0:
            if c == 0:
                c = b[i]
            else:
                if c > b[i]:
                    c = b[i]
        #0にぶちあたったので、それまでの分から引く数を引くフェーズ
        #引く数cは0にリセットする、引く最初の場所dはぶちあたった0の隣
        elif b[i] == 0:
            for s in range(d, i):
                b[s] = b[s] - c
                sumb -= c
            ans += c
            if i != a-1:
                d = int(i) + 1
                c = 0
            else:
                d = 0
                c = 0
        #最後までぶち当たらなかったときの処理
        #c=0のパターンは別にする
        #2週目に備えてcdはリセット
        elif c != 0:
            for s in range(d, a):
                b[s] = b[s] - c
                sumb -= c
            ans += c
            d = 0
            c = 0
        #c=0のまま最後まで来たパターン
        #最後の値を
        else:
            ans += b[i]
            sumb -= b[i]
            
print(ans)

Submission status: Wrong Answer

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

while True:
    for i in range(h.count(0)):
        if h.index(0) == 0: 
            h.pop(0)
        elif h.index(0) > 0:
            tmp = h.index(0) -1
            break
    else:
        tmp = len(h) -1

    if not h:
        break

    if tmp == 0:
        cnt += h[0]
        h.pop(0)
    else:
        minNum = min(h[0:tmp])
        cnt += minNum
        for j in range(tmp+1):
            h[j] -= minNum

print(cnt)

Submission status: Wrong Answer

Solution code:
from sys import stdin
readline = stdin.buffer.readline

def watering(h):
    ans = min(h)
    hh = list(map(lambda x: x - ans, h))
    if len(set(hh)) == 2:
        return max(hh) + ans
    elif len(set(hh)) == 1:
        return ans
    fs = 0
    for i, n in enumerate(hh):
        if n == 0:
            if i != fs:
                hhh = hh[fs:i]
                ans += watering(hhh)
                fs = i + 1
            else:
                fs = i + 1
    else:
        hhh = hh[fs:]
        if len(hhh) > 0:
            ans += watering(hhh)
    return ans

def main():
    N = int(readline().rstrip())
    H = list(map(int, readline().rstrip().split()))
    print(watering(H))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
readline = stdin.buffer.readline

def watering(h):
    ans = min(h)
    hh = list(map(lambda x: x - ans, h))
    if len(set(hh)) == 2:
        return max(hh) + ans
    elif len(set(hh)) == 1:
        return ans
    fs = 0
    l = len(hh)
    for p in range(l):
        if h[p] == 0:
            if p != fs:
                hhh = hh[fs:p]
                ans += min(hhh)
                hhh = list(map(lambda x: x - min(hhh), hhh))
                fs = p + 1
                ans += watering(hhh)
            else:
                fs = p + 1
        elif p == l - 1:
            hhh = hh[fs:]
            ans += min(hhh)
            hhh = list(map(lambda x: x - min(hhh), hhh))
            ans += watering(hhh)
    return ans

def main():
    N = int(readline().rstrip())
    H = list(map(int, readline().rstrip().split()))
    print(watering(H))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

# import numpy as np
# from numba import jit
# stdin = open('sample.txt')
def watering(h):
    ans = 0
    if min(h) != 0:
        ans += min(h)
        hh = list(map(lambda x: x - min(h), h))
    else:
        hh = h
    if len(set(hh)) == 2 and min(hh) == 0:
        return max(hh) + ans
    elif len(set(hh)) == 1:
        return hh[0]
    fs = 0
    l = len(hh)
    for p in range(l):
        if h[p] == 0:
            if p != fs:
                hhh = hh[fs:p]
                ans += min(hhh)
                hhh = list(map(lambda x: x - min(hhh), hhh))
                fs = p + 1
                ans += watering(hhh)
            else:
                fs = p + 1
        elif p == l - 1:
            hhh = hh[fs:]
            ans += min(hhh)
            hhh = list(map(lambda x: x - min(hhh), hhh))
            ans += watering(hhh)
    return ans

def main():
    N = int(readline().rstrip())
    H = list(map(int, readline().rstrip().split()))
    print(watering(H))

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def find(ar):

    if ar==[0]*len(ar):
        return 0
    m=999999999999999
    for i in range(len(ar)):
        if ar[i]<m:
            m=ar[i]
            pos=[i]
        elif ar[i]==m:
            pos.append(i)
    pos.append(n)
    for i in range(len(ar)):
        ar[i]-=m
    if ar==[0]*len(ar):
        return m
    ans=0
    if m==0:
        pass
    else:
        ans=m
    if pos[0]!=0:
        ans+=find(ar[0:pos[0]])
        for i in range(0,len(pos)-1):
            ans+=find(ar[pos[i]+1 :pos[i+1]])

    else:
        ans += find(ar[1:pos[1]])
        for i in range(1, len(pos) - 1):
            print(ar[pos[i] + 1 : pos[i + 1]])
            ans += find(ar[pos[i] + 1 : pos[i + 1]])

    return ans

n=int(input())
ar=[int(i) for i in input().strip().split(" ")]
print(find(ar))

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    n = int(input())
    A = list(map(int,input().split()))
    
    total = 0
    tmp = 0
    for i in range(n):
        #最後
        if i == n - 1:
            #自分より前が同じか自分が大きい場合
            if A[i-1] <= A[i]:
                total += A[i]
                #もし、tmpがあればそれを差し引く
                total -= tmp
            else:
                #自分が小さい場合、何もしない
                pass
            #終わり
            break
            
        #後ろが自分より大きければそのまま
        if A[i] <= A[i+1]:
            pass
        else:
            #前の数値より大きいこと
            if tmp <= A[i]:
                #自分より小さい場合、自分をカウント、次の数値を記憶+ループをリセット
                total += A[i]
                #もしtmpがあればそれを差し引く
                total -= tmp
                tmp = A[i+1]
                A[i+1] = 0
            else:
                A[i] = 0
                A[i+1] = 0
                tmp = 0
    print(total)

Submission status: Wrong Answer

Solution code:
if __name__ == '__main__':
    n = int(input())
    A = list(map(int,input().split()))
    
    total = 0
    tmp = 0
    for i in range(n):
        #最後
        if i == n - 1:
            #自分より前が同じか自分が大きい場合
            if A[i-1] <= A[i]:
                total += A[i]
                #もし、tmpがあればそれを差し引く
                total -= tmp
            else:
                #自分が小さい場合、何もしない
                pass
            #終わり
            break
            
        #後ろが自分より大きければそのまま
        if A[i] <= A[i+1]:
            pass
        else:
            #自分より小さい場合、自分をカウント、次の数値を記憶+ループをリセット
            total += A[i]
            #もしtmpがあればそれを差し引く
            total -= tmp
            
            tmp = A[i+1]
            A[i+1] = 0
    print(total)

Submission status: Wrong Answer

Solution code:
# abc116_c.py
# https://atcoder.jp/contests/abc116/tasks/abc116_c

# C - Grand Garden /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 300点

# 問題文
# 花壇に N本の花が咲いており、それぞれ 1,2,......,N と番号が振られています。
# 最初、全ての花の高さは 0 です。 数列 h={h1,h2,h3,......} が入力として与えられます。
# 以下の「水やり」操作を繰り返すことで、すべての k(1≦k≦N) に対して花 k の高さを hkにしたいです。
#     整数 l,rを指定する。l≦x≦r を満たすすべての x に対して、花 x の高さを 1高くする。
# 条件を満たすための最小の「水やり」操作の回数を求めてください。

# 制約
#     1≦N≦100
#     0≦hi≦100
#     入力はすべて整数である。

# 入力
# 入力は以下の形式で標準入力から与えられます。
# N
# h1 h2 h3 ...... hN

# 出力
# 条件を満たすような最小の「水やり」操作の回数を出力してください。

# 入力例 1
# 4
# 1 2 2 1

# 出力例 1
# 2

# 「水やり」操作の回数は 2回が最小です。 以下が一つの例です。
#     (l,r)=(1,3)の「水やり」操作を行う。
#     (l,r)=(2,4)の「水やり」操作を行う。

# 入力例 2
# 5
# 3 1 2 3 1

# 出力例 2
# 5

# 入力例 3
# 8
# 4 23 75 0 23 96 50 100

# 出力例 3
# 221


def calculation(lines):
    # X = lines[0]
    N = int(lines[0])
    values = list(map(int, lines[1].split()))
    # values = list()
    # for i in range(N):
    #     values.append(int(lines[i+1]))

    result = get_cnt(values)

    return [result]


def get_cnt(values):
    # print('----------------------------------------------')
    # print(f'values=[{values}]')
    ret = 0
    mi = min(values)
    # print(f'mi=[{mi}]')
    ret += mi
    values = [value - mi for value in values]
    # print(f'values=[{values}]')
    lst = None
    for value in values:
        # print(f'    value=[{value}], lst=[{lst}]')
        if value != 0:
            if lst is None:
                lst = [value]
            else:
                lst.append(value)
        else:
            if lst is None:
                pass
            else:
                ret += get_cnt(lst)
                lst = None
    if lst is not None:
        ret += get_cnt(lst)
    return ret


def func_f(n):
    if n % 2 == 0:
        return int(n/2)
    else:
        return 3*n + 1


# 引数を取得
def get_input_lines(lines_count):
    lines = list()
    for _ in range(lines_count):
        lines.append(input())
    return lines


# テストデータ
def get_testdata(pattern):
    if pattern == 1:
        lines_input = ['4', '1 2 2 1']
        lines_export = [2]
    if pattern == 2:
        lines_input = ['5', '3 1 2 3 1']
        lines_export = [5]
    if pattern == 3:
        lines_input = ['8', '4 23 75 0 23 96 50 100']
        lines_export = [221]
    return lines_input, lines_export


# 動作モード判別
def get_mode():
    import sys
    args = sys.argv
    if len(args) == 1:
        mode = 0
    else:
        mode = int(args[1])
    return mode


# 主処理
def main():
    import time
    started = time.time()
    mode = get_mode()
    if mode == 0:
        lines_input = get_input_lines(2)
    else:
        lines_input, lines_export = get_testdata(mode)

    lines_result = calculation(lines_input)

    for line_result in lines_result:
        print(line_result)

    if mode > 0:
        print(f'lines_input=[{lines_input}]')
        print(f'lines_export=[{lines_export}]')
        print(f'lines_result=[{lines_result}]')
        if lines_result == lines_export:
            print('OK')
        else:
            print('NG')
    finished = time.time()
    duration = finished - started
    print(f'duration=[{duration}]')


# 起動処理
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = 0
w = 1
comp = 0
left = 0
right = len(h) - 1

while comp != n:
    ans += w
    for i in range(len(h)):
        h[i] -= 1
        if h[i] == 0:
            comp += 1
            if i == left:
                left += 1
            elif i == right:
                right -= 1
            else:
                w += 1

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N=int(input())
h=[int(x) for x in input().split()]
h=[0]+h+[0]
z=0
cnt=0
for i in range(1,N+1):
    if h[i-1]>h[i] and h[i]<h[i+1]:
        cnt+=z-h[i]
        z=0
    else:
        z=max(z,h[i])
cnt+=z
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
def main():
    n, *h = map(int, read().split())
    if n == 1:
        print(h[0])
        sys.exit()
    r1 = h[0]
    r2 = h[-1]
    for i1 in range(n - 1):
        if h[i1] < h[i1 + 1]:
            r1 += (h[i1 + 1] - h[i1])
        elif h[i1] > h[i1 + 1]:
            r2 += (h[i1] - h[i1 + 1])
    r = max(r1, r2) + min(h)
    print(r)
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def down(H, n):
  x = min(H)
  n += x
  for i in range(len(H)):
    H[i] -= x
  return H, n

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

while not all(h == 0 for h in H):
  L = []
  H0 =[]
  for h in H:
    if h != 0:
      L.append(h)
    else:
      if L:
        L, n = down(L, n)
      H0 += L
      H0.append(0)
      L = []
  if L:
    L, n = down(L, n)
    H0 += L
  H = H0
  print(H)

print(n)

Submission status: Wrong Answer

Solution code:
def down(H, n):
  x = min(H)
  n += x
  for i in range(len(H)):
    H[i] -= x
  return H, n

N = int(input())
H = list(map(int, input().split()))
n = 0
H, n = down(H, n)
print(H)

while not all(h == 0 for h in H):
  L = []
  H0 =[]
  for h in H:
    if h != 0:
      L.append(h)
    else:
      if L:
        L, n = down(L, n)
      H0 += L
      H0.append(0)
      L = []
  if L:
    L, n = down(L, n)
    H0 += L
  H = H0

print(n)

Submission status: Wrong Answer

Solution code:
def down(H, n):
  x = min(H)
  n += x
  for i in range(len(H)):
    H[i] -= x
  return H, n

N = int(input())
H = list(map(int, input().split()))
n = 0
H, n = down(H, n)
print(H)

while not all(h == 0 for h in H):
  L = []
  H0 =[]
  for h in H:
    if h != 0:
      L.append(h)
    else:
      if L:
        L, n = down(L, n)
      H0 += L
      H0.append(0)
      L = []
  if L:
    L, n = down(L, n)
    H0 += L
  H = H0
  print(H)

print(n)

Submission status: Wrong Answer

Solution code:
def down(H, n):
  x = min(H)
  n += x
  for i in range(len(H)):
    H[i] -= x
  return H, n

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

while not all(h == 0 for h in H):
  L = []
  H0 =[]
  for h in H:
    if h != 0:
      L.append(h)
    else:
      if L:
        L, n = down(L, n)
      H0.append(0)
      H0 += L
      L = []
  if L:
    L, n = down(L, n)
    H0 += L
  H = H0

print(n)

Submission status: Wrong Answer

Solution code:
N = int(input())
h = [int(x) for x in input().split()]
count = 0
con = 1
while True:
    for i in range(N):
        if h[i] > 0:
            h[i] -= 1
            con = 1
        elif h[i] <= 0:
            if con == 1:
                count += 1
            con = 0
    if con == 1:
        count += 1
    con = 0
    if not any(h):
        break

print(count)

Submission status: Wrong Answer

Solution code:
"""
8
4 23 75 0 23 96 50 100
"""

[0,75,0,96,50,100]

n = int(raw_input())
ais = map(int, raw_input().split(' '))

def f(ais):
	
	i = 0
	while(i < len(ais) and ais[i] == 0): i+=1
	ais = ais[i:]

	r = [0]
	for i in range(len(ais)):
		if (ais[i-1] if i-1 >= 0 else 0) <= ais[i] > (ais[i+1] if i+1 < len(ais) else 0): 
			r.append(ais[i])
		if (ais[i-1] if i-1 >= 0 else 0) >= ais[i] < (ais[i+1] if i+1 < len(ais) else +float('inf')): 
			if i:
				r.append(ais[i])

	t = 0
	for i in range(0,len(r),2):
		if i + 1 < len(r):
			u,v = r[i],r[i+1]
			t += v-u
	if ais:
		return t if len(ais) > 1 else ais[0]
	else:
		return 0

print min(f(ais), f(ais[::-1]))

Submission status: Wrong Answer

Solution code:
"""
8
4 23 75 0 23 96 50 100
"""

[0,75,0,96,50,100]

n = int(raw_input())
ais = map(int, raw_input().split(' ')) #set()
i = 0
while(i < len(ais) and ais[i] == 0): i+=1
ais = ais[i:]

r = [0]
for i in range(len(ais)):
	if (ais[i-1] if i-1 >= 0 else 0) <= ais[i] > (ais[i+1] if i+1 < len(ais) else 0): 
		r.append(ais[i])
	if (ais[i-1] if i-1 >= 0 else 0) >= ais[i] < (ais[i+1] if i+1 < len(ais) else +float('inf')): 
		if i:
			r.append(ais[i])

	
t = 0
for i in range(0,len(r),2):
	if i + 1 < len(r):
		u,v = r[i],r[i+1]
		t += v-u
if ais:
	print t if len(ais) > 1 else ais[0]
else:
	print 0

Submission status: Wrong Answer

Solution code:
"""
8
4 23 75 0 23 96 50 100
"""

[0,75,0,96,50,100]


n = int(raw_input())
ais = map(int, raw_input().split(' ')) #set()


r = [0]
for i in range(len(ais)):
	if (ais[i-1] if i-1 >= 0 else 0) <= ais[i] > (ais[i+1] if i+1 < len(ais) else 0): r.append(ais[i])
	if (ais[i-1] if i-1 >= 0 else 0) >= ais[i] < (ais[i+1] if i+1 < len(ais) else 0): 
		if i:
			r.append(ais[i])

	
t = 0
for i in range(0,len(r),2):
	if i + 1 < len(r):
		u,v = r[i],r[i+1]
		t += v-u
print t if len(ais) > 1 else ais[0]

Submission status: Wrong Answer

Solution code:
"""
8
4 23 75 0 23 96 50 100
"""

[0,75,0,96,50,100]


n = int(raw_input())
ais = map(int, raw_input().split(' ')) #set()


r = [0]
for i in range(len(ais)):
	if (ais[i-1] if i-1 >= 0 else 0) <= ais[i] > (ais[i+1] if i+1 < len(ais) else 0): r.append(ais[i])
	if (ais[i-1] if i-1 >= 0 else 0) >= ais[i] < (ais[i+1] if i+1 < len(ais) else 0): 
		if i:
			r.append(ais[i])

	
t = 0
r.append(0)
for i in range(0,len(r),2):
	if i + 1 < len(r):
		u,v = r[i],r[i+1]
		t += v-u
print t if len(ais) > 1 else ais[0]

Submission status: Wrong Answer

Solution code:
"""
8
4 23 75 0 23 96 50 100
"""

[0,75,0,96,50,100]


n = int(raw_input())
ais = map(int, raw_input().split(' ')) #set()


r = [0]
for i in range(len(ais)):
	if (ais[i-1] if i-1 >= 0 else 0) <= ais[i] > (ais[i+1] if i+1 < len(ais) else 0): r.append(ais[i])
	if (ais[i-1] if i-1 >= 0 else 0) >= ais[i] < (ais[i+1] if i+1 < len(ais) else 0): r.append(ais[i])

	
t = 0
r.append(0)
for i in range(0,len(r),2):
	if i + 1 < len(r):
		u,v = r[i],r[i+1]
		t += v-u
print t

Submission status: Wrong Answer

Solution code:
s = int(raw_input())
seen= set()
i = 0
while(s not in seen):
	seen.add(s)
	i +=1
	if s % 2: s = 3 * s + 1
	else: s /= 2
print i+1

Submission status: Wrong Answer

Solution code:
n=int(input())
h=list(map(int,input().split()))
hm=max(h)
t=1
ans=0
for i in range(hm,-1,-1):
    for j in range(n):
        if h[j]==i:
            ans+=t
            t=0
        else:
            t=1

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
Hs = list(map(int,input().split()))
f = [0 for _ in range(n)]
r = 1
c = 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: Wrong Answer

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

Submission status: Wrong Answer

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.all(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: Wrong Answer

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):
    if (np.all(h <= 0)):
        return s
    else:
        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
        
        cat = np.concatenate([h[:l], h[l:r+1]-1, h[r+1:]])
        try:
            return solve(N, cat, s+1)
        except Exception as e:
            return s

print(solve(N,h,0))

Submission status: Wrong Answer

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):
    if (np.all(h <= 0)):
        return s
    else:
        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
        try:
            cat = np.concatenate([h[:l], h[l:r+1]-1, h[r+1:]])
            return solve(N, cat, s+1)
        except Exception as e:
            return s

print(solve(N,h,0))

Submission status: Wrong Answer

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):
    if (np.all(h <= 0)):
        return s
    else:
        w = np.where(h > 0)[0]
        try:
            l = w[0]
            r = w[0]
            i = 1
            while (i < len(w)):
                if (w[i] == r + 1):
                    r = w[i]
                i += 1
            cat = np.concatenate([h[:l], h[l:r+1]-1, h[r+1:]])
            return solve(N, cat, s+1)
        except Exception as e:
            return s

print(solve(N,h,0))

Submission status: Wrong Answer

Solution code:
N = int(input())
arr = list(map(int, input().split()))
 
count =0
while True:
    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
    count +=1
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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(p)
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

flg = True
st = 0
while flg:
    if h.count(0) == len(h):
        break
    for i in range(N-1):
        if not h[i]:
            continue
        flg = False

        j = i+1
        while j < N and h[i] <= h[j]:
            flg = True
            h[j] -= h[i]
            j += 1

        k = i-1
        while 0 <= k and h[i] <= h[k]:
            flg = True
            h[k] -= h[i]
            k -= 1
        if flg:
            st += h[i]
            h[i] = 0
print(sum(h)+st)

Submission status: Wrong Answer

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)

Submission status: Wrong Answer

Solution code:
n=int(input())
h=list(map(int,input().split()))
ans=0
temp=[0]*n
while True:
  a=0
  b=0
  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)