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

Solution in PyPy3 (7.3.0) (judge verdict: Accepted)

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

        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

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