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 sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None

def calcdiv(H):
    i = 0
    d = 0

    while i < len(H):
        if H[i] == 0:
            i += 1
            continue

        while i < len(H) and H[i] != 0:
            i += 1
        
        d += 1
    
    return d


def is_end(H):
    for h in H:
        if h >= 1:
            return False
    
    return True


def main():
    N = int(input())
    H = li_input()

    ans = 0

    while not is_end(H):
        d = calcdiv(H)
        ans += d

        for i in range(len(H)):
            if H[i] > 0:
                H[i] -= 1


    
    print(ans)


main()

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	tmp = a[i]
	ans += tmp
	for j in range(i, n):
		tmp = min(tmp, a[j])
		a[j] -= tmp
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
h = list(map(int, input().split(" ")))
max_ans = sum(h)
b = [min(h[i+1], h[i]) for i in range(N-1)]
decrease = sum(b)

print(max_ans-decrease)

Submission status: Accepted

Solution code:
N=int(input())
ans=0
tmp=0
for x in list(map(int,input().split())):
    ans+=max(0,x-tmp)
    tmp=x
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):
    ans += abs(h[i - 1] - h[i])
ans += h[n - 1]
ans //= 2
print(ans)

Submission status: Accepted

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

ans = 0
h_min = min(h_list)

ans += h_min

pre = h_min
for i in range(N):
    h = h_list[i]
    if i == 0:
        ans += h - h_min
        pre = h
    else:
        if h >= pre:
            ans += h - pre
            pre = h
        else:
            pre = h
print(ans)

Submission status: Accepted

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

def count(h,m):
    c = 0
    state = 0
    for i in range(n):
        if h[i] >= m and state == 0:
            c += 1
            state = 1
        elif h[i] < m and state == 1:
            state = 0
    return c

ans = 0
m = max(h)
while m > 0:
    ans += count(h,m)
    m -= 1
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
H = list(map(int, input().split()))
maxh = max(H)
ans = 0
for h in range(1, maxh+1):
  above = False
  for k in range(N):
    if H[k] >= h:
      if not above:
        ans += 1
    above = H[k] >= h
print(ans)

Submission status: Accepted

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

ans = 0
while any(H):
    start = False
    for i in range(N):
        if not start and not H[i]: continue
        if H[i]:
            start = True
            H[i] -= 1
        else:
            break
    ans += 1
print(ans)

Submission status: Accepted

Solution code:
def inpl(): return [int(i) for i in input().split()]

N = int(input())
H = inpl()
ans = 0
while True:
    ctr = 1
    for i in range(N):
        if H[i]:
            ans += ctr
            ctr = 0
            H[i] -= 1
        else:
            ctr = 1
    if not any(H):
        break
print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def LIR(n): return [LI() for i in range(n)]
mod = 1000000007

#A
"""
a,b,c = LI()
s = a+b+c
s /= 2
ans = (s*(s-a)*(s-b)*(s-c))**(1/2)
print(int(ans))
"""

#B
"""
s = int(input())
m = 1
lis = [True for i in range(1000001)]

while lis[s]:
    lis[s] = False
    if s % 2 == 1:
        s = 3*s+1
    else:
        s //= 2
    m += 1
print(m)
"""

#C
n = II()
h = LI()
ans = 0
while sum(h) != 0:
    l = 0
    while h[l] == 0: l += 1
    r = l
    while r < n:
        if h[r] == 0: break
        h[r] -= 1
        r += 1
    ans += 1
print(ans)
#D

#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Accepted

Solution code:
N = int(input())
H = [int(_) for _ in input().split()]

result = 0
for i in range(1, 101):
    for j in range(N):
        if H[j] >= i:
            if j == 0 or H[j - 1] < i:
                result += 1

print(result)

Submission status: Accepted

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

ANS=0
while max(A)>0:
    for i in range(1,N+2):
        if A[i-1]<=0 and A[i]>0:
            ANS+=1

    A=[a-1 for a in A]

print(ANS)

Submission status: Accepted

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

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

print(a)

Submission status: Accepted

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

cnt = 0
while h != [0] * n:
    prev = -1
    for i in range(n):
        if prev == -1 and h[i] != 0:
            cnt += 1
        if h[i] == 0:
            prev = -1
        else:
            prev = 1
            h[i] -= 1

    # print(h, cnt)

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
*H, = map(int, input().split())
ans = 0
A = [0]*N
update = 1
h = 0
while update:
    update = 0
    cnt = 0
    for i in range(N):
        if H[i] > h:
            cnt += 1
        else:
            if cnt > 0:
                update = 1
                ans += 1
            cnt = 0
    if cnt > 0:
        update = 1
        ans += 1
    h += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = input()
h = map(int, raw_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:
def inpl(): return list(map(int, input().split()))


N = int(input())
A = inpl()

ans = 0
while sum(A) > 0:
    ans += 1
    flag = False
    for i in range(N):
        if A[i] > 0:
            A[i] -= 1
            flag = True
        elif flag is True:
            break
print(ans)

Submission status: Runtime Error

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()
# 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)
#     sys.exit()

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

######################
# 隣との差分を考えたほうが早い

# 1    2    4    2    1    0      計4
#   1    2    0    0     0

ans = hl[0]
for i in range(1, n)
    ans += max(0, hl[i] - hl[i - 1])
print(ans)

Submission status: Runtime Error

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()
# 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)
#     sys.exit()

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

######################
# 隣との差分を考えたほうが早い

# 1    2    4    2    1    0      計4
#   1    2    0    0     0

ans = hl[0]
for i in range(1, N)
    ans += max(0, hl[i] - hl[i - 1])
print(ans)

Submission status: Runtime Error

Solution code:
N=int(inpput())
H=list(map(int,input().split()))
ct=0
for i in range(N):
  t=H[i]
  for j in range(i+1,N):
    if H[j]==0:
      break
    H[j]=max(H[j]-t,0)
  ct+=t
print(ct)

Submission status: Runtime Error

Solution code:
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from heapq import heappop, heappush, heappushpop
import copy
import numpy as np
import sys
INF = float('inf')
mod = 10**9+7


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


def main():
    n = I()
    h = list(LI())
    n = int(input())
    ans = h[0]
    for i in range(1, n):
        if h[i] > h[i-1]:
            ans += h[i] - h[i-1]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from heapq import heappop, heappush, heappushpop
from copy import deepcopy
import numpy as np
import sys
INF = float('inf')
mod = 10**9+7


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


def main():
    n = I()
    h = list(LI())
    n = int(input())
    ans = h[0]
    for i in range(1, n):
        if h[i] > h[i-1]:
            ans += h[i] - h[i-1]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
from math import floor, ceil, sqrt, factorial, log, gcd
from itertools import accumulate, permutations, combinations, product, combinations_with_replacement
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict
from heapq import heappop, heappush, heappushpop
from copy import deepcopy
import numpy as np
import sys
INF = float('inf')
mod = 10**9+7


def lcm(a, b): return a * b / gcd(a, b)

# 1 2 3
# a, b, c = LI()


def LI(): return list(map(int, sys.stdin.buffer.readline().split()))

# a = I()


def I(): return int(sys.stdin.buffer.readline())

# abc def
# a, b = LS()


def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

# a = S()


def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')

# 2
# 1
# 2
# [1, 2]


def IR(n): return [I() for i in range(n)]

# 2
# 1 2 3
# 4 5 6
# [[1,2,3], [4,5,6]]


def LIR(n): return [LI() for i in range(n)]

# 2
# abc
# def
# [abc, def]


def SR(n): return [S() for i in range(n)]

# 2
# abc def
# ghi jkl
# [[abc,def], [ghi,jkl]]


def LSR(n): return [LS() for i in range(n)]

# 2
# abcd
# efgh
# [[a,b,c,d], [e,f,g,h]]


def SRL(n): return [list(S()) for i in range(n)]


ans = []


def main():
    n = I()
    h = list(LI())
    n = int(input())
    ans = h[0]
    for i in range(1, n):
        if h[i] > h[i-1]:
            ans += h[i] - h[i-1]
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
h = list(map(int, input().split()))
H = [h]
ans = 0
while len(H) > 0:
  if [] in H:
    H.remove([])
    if len(H) == 0:
      break
  for i in range(len(H)):
    if len(H[i]) == 0:
      H.pop(i)
    elif 0 in H[i]:
      a = H[i][:H[i].index(0)]
      b = H[i][H[i].index(0) + 1:]
      H.append(a)
      H.append(b)
      H.pop(i)
      if [] in H:
        H.remove([])
        if len(H) == 0:
          break
    else:
      ans += min(H[i])
      H[i] = [H[i][j] - min(H[i]) for j in range(len(H[i]))]
      a = H[i][:H[i].index(0)]
      b = H[i][H[i].index(0) + 1:]
      H.append(a)
      H.append(b)
      H.pop(i)
      if [] in H:
        H.remove([])
        if len(H) == 0:
          break
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

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

s = 0

for i in range(max(h)+1):
    flag = False
    for j in range(n):
        if flag and h[j] == 0:
            s += 1
            flag = False
        elif not flag and h[j] > 0:
            flag = True

        h[j] = max(h[j]-1, 0)
    if flag:
        s += 1

print(s)

Submission status: Runtime Error

Solution code:
import sys


def input():
    return sys.stdin.readline().rstrip()


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

    dflag = 0
    cmin = 0
    cmax = h[0]
    count = 0
    for i in range(1, N):
        if h[i - 1] < h[i] and dflag == 1:
            count += cmax - cmin
            cmax = h[i]
            dflag = 0
            cmin = h[i - 1]

        elif h[i - 1] > h[i]:
            dflag = 1

        elif h[i - 1] < h[i]:
            cmax = h[i]

    if dflag == 1:
        count += cmax - cmin
    else:
        count += max(cmax, h[i]) - cmin

    print(count)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys

def input():
    return sys.stdin.readline().rstrip()


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

    dflag = 0
    cmin = 0
    cmax = h[0]
    count = 0
    for i in range(1, N):
        if h[i - 1] < h[i] and dflag == 1:
            count += cmax-cmin
            cmax=h[i]
            dflag=0
            cmin=h[i-1]

        elif h[i-1]>h[i]:
            dflag=1

        elif h[i-1]<h[i]:
            cmax=h[i]

    if dflag==1:
        count+=cmax-cmin
    else:
        count+= max(cmax,h[i])-cmin

    print(count)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
import math


def input():
    return sys.stdin.readline().rstrip()


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

    dflag = 0
    cmin = 0
    cmax = h[0]
    count = 0
    for i in range(1, N):
        if h[i - 1] < h[i] and dflag == 1:
            count += cmax-cmin
            cmax=h[i]
            dflag=0
            cmin=h[i-1]

        elif h[i-1]>h[i]:
            dflag=1

        elif h[i-1]<h[i]:
            cmax=h[i]

    if dflag==1:
        count+=cmax-cmin
    else:
        count+= max(cmax,h[i])-cmin

    print(count)



if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input().split()))
if n==1:
    print(h[0])
elif n==2:
    print(max(h))
else:
    h.append(0)
    h.insert(0,0)
    
    ans=0
    f=[0 for i in range(n+2)]
    
    for i in range(10010):
        a=1
        x=[]
        for j in range(n+2):
            if h[j]<=f[j]:
                x.append(j)
        m=len(x)
        if m==n+2:
            break
        a=m-1
        for j in range(m-1):
            if x[j]+1==x[j+1]:
                a-=1
        ans+=a
        for j in range(n+2):
            f[j]+=1
        
print(ans)

Submission status: Runtime Error

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
H = [0] + H
cnt = 0
for i in range(1, N + 1):
    cnt += max(H[i] - H[i - 1], 0)


print(cnt)

Submission status: Runtime Error

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

# B - Collatz Problem /
# 実行時間制限: 2 sec / メモリ制限: 1024 MB
# 配点 : 200点

# 問題文
# 数列 a={a1,a2,a3,......}は、以下のようにして定まります。
#     初項 sは入力で与えられる。
#     関数 f(n)を以下のように定める: n が偶数なら f(n)=n/2、n が奇数なら f(n)=3n+1。
#     i=1のとき ai=s、i>1 のとき ai=f(ai−1)である。
# このとき、次の条件を満たす最小の整数 mを求めてください。
#     am=an(m>n)を満たす整数 nが存在する。

# 制約
#     1≦s≦100
#     入力はすべて整数である。
#     aのすべての要素、および条件を満たす最小の m は 1000000以下となることが保証される。

# 入力
# 入力は以下の形式で標準入力から与えられます。
# s

# 出力
# 条件を満たす最小の整数 mを出力してください。

# 入力例 1
# 8

# 出力例 1
# 5

# a={8,4,2,1,4,2,1,4,2,1,......}です。a5=a2 なので、答えは 5です。

# 入力例 2
# 7

# 出力例 2
# 18

# a={7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......}です。

# 入力例 3
# 54

# 出力例 3
# 114


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

    for i in range(101):
        s = func_f(values[i])
        if s in values:
            return [i+2]
        values.append(s)


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 = ['8']
        lines_export = [5]
    if pattern == 2:
        lines_input = ['7']
        lines_export = [18]
    if pattern == 3:
        lines_input = ['54']
        lines_export = [114]
    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(1)
    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: Runtime Error

Solution code:
import sys 
N = int(input())
flowerlis = list(map(int, input().split())) 

minihigh = min(flowerlis)

count = 0

if minihigh == 0:
    pass
else:
    count = minihigh

    for i in range(len(flowerlis)):
        flowerlis[i] = flowerlis[i] - 1





while True:
    for i in range(len(flowerlis)):
        if flowerlis[i] != 0:
            start = i
            break

    for j in range(start, len(flowerlis)):
        if flowerlis[j] == 0:
            finish = j
            break

    count = count + 1
    for  k in range(start,finish):
        flowerlis[k] = flowerlis[k] - 1
        
    if flowerlis.count(0) == len(flowerlis):
        print(count)
        break

Submission status: Runtime Error

Solution code:
難しい...

Submission status: Runtime Error

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
print t if len(ais) > 1 else ais[0]

Submission status: Runtime Error

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 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: Runtime Error

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

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

Submission status: Runtime Error

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

ans=0
for i in range(hm,-1,-1):
    t=1
    for j in range(n):
        if h[j]=i:
            ans+=t
            t=0
        else:
            t=1

print(ans)

Submission status: Runtime Error

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: Runtime Error

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: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input.split()))
hm=max(h)
t=1
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: Runtime Error

Solution code:
n=int(input())
h=list(map(int,input.split()))
hm=max(h)
t=1
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: Runtime Error

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

step = 0
while(True):
  
  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(stop is None):
    stop =  len(hs)
    
  for i in range(start, stop):
    hs[i] -= 1
    
  step += 1
  
  if(sum(hs) == 0):
    break
    
print(step)

Submission status: Runtime Error

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:]])
        except Exception as e:
            return s
        return solve(N, cat, s+1)

print(solve(N,h,0))

Submission status: Runtime Error

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

print(solve(N,h,0))

Submission status: Runtime Error

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]
        except Exception as e:
            return s
        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)

print(solve(N,h,0))

Submission status: Runtime Error

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:]])
        return solve(N, cat, s+1)

print(solve(N,h,0))