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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000000000000000

/*方針*/
//連続で正の値が続いている部分について、水やりを行う

/*ex) 4  23  75  0  23  96  50  100
      <------->     <------------->		計27回
       4回水やり　　　　23回水やり

	  0  19  71  0  0   73  27   67
	     <---->         <--------->		計27+46=73回
		 19回水やり　　　　27回水やり
	  
	  0   0  52  0  0   46   0   40
	         <->        <->      <->    計73+148=221回
			 52回       46回     40回
*/

/*
➀連続の正の値の区間に対して、その区間における最小値分、水やりをする。(vector hから最小値を引く)
➁hが0になるまで➀を繰り返す
*/

int main(){
	int n;cin>>n;
	vector<int> h(n+1);
	for(int i=0;i<n;i++){
		cin>>h[i];
	}
	h[n]=0;//番兵

	int ans=0;
	bool allzero=false;

/*➁*/	while(!allzero){//hが全部0になるまで
		int num=0;
		for(int i=0;i<=n;i++){//0探す
			int MIN=10000000;

			if(h[i]==0){
/*➀*/				for(int j=num;j<i;j++){
					MIN=min(MIN,h[j]);
				}
				if(MIN!=10000000) ans+=MIN;
				for(int j=num;j<i;j++){
					h[j]-=MIN;
				}
				num=i+1;
			}
		}

		for(int i=0;i<=n;i++){//全部0になったか
			if(h[i]==0){allzero=true;}
			else{allzero=false;break;}
		}
	}
	
	cout<<ans<<endl;
	return 0;
}