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 Python (3.4.3) (judge verdict: Accepted)

import numpy as np
N = int(input())
hs = input().split()
hs = np.array([int(h) for h in hs])

# hs = np.array([0,4,23,75,23,0,96,50,100])

def split_array(hs):
    split_indexes = np.where(hs==0)[0]
    n = len(split_indexes)
    split_list = []
    if n > 0:
        split_list.append(hs[0:split_indexes[0]])
        if n > 1:
            for i in range(n-1):
                split_list.append(hs[split_indexes[i]+1:split_indexes[i+1]])
        split_list.append(hs[split_indexes[-1]+1:])
    return split_list

def make_shower(l):
    count = l.min()
    return count, l-count

all_count = 0
c, hs = make_shower(hs)
split_list = split_array(hs)
all_count += c
zero_count = 0
#全部のリストが空になったところで終了
while True:
    zero_count = 0
    split_list_new = []
    for l in split_list:
        if len(l) == 0:
            zero_count += 1
            continue
        count, l_showered = make_shower(l)
        # print("count",count,l,l_showered)
        all_count += count
        split_list_ = split_array(l_showered)
        split_list_new.extend(split_list_)

    len_split_list = len(split_list) # リストの長さを取っておく
    split_list = [l for l in split_list_new] #代入
    
    if zero_count == len_split_list:
        break
print(all_count)