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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder
{
    class Program
    {
        static List<int> connectnumDelite(List<int> list)
        {
            int i = 0;
            while (i < list.Count()-1)
            {
                while (list[i] == list[i + 1])
                {
                    list.RemoveAt(i);
                    if (i >= list.Count-1)
                    {
                        break;
                    }
                }
                i++;
                if (i >= list.Count-1)
                {
                    break;
                }
            }

            return list;
        }


        static void Main()
        {
            IO io = new IO();

            //ここから
            int flowerNum = io.ReadLineToInt();
            List<int> height = new List<int>();
            height.Add(0);
            height.AddRange(io.ReadLineToIntList());
            height.Add(0);

            flowerNum = height.Count();
            
            int ans = 0;

            while (flowerNum != 1)
            {
                if (flowerNum == 2)
                {
                    ans += height.Max();
                    break;
                }
                height = connectnumDelite(height);
                flowerNum = height.Count();

                for(int i = 0; i < flowerNum - 2; i++)
                {
                    if (height[i] < height[i + 1] && height[i + 1] > height[i + 2])
                    {
                        int temp;
                        if (height[i] > height[i + 2])
                        {
                            temp = height[i];
                        }
                        else
                        {
                            temp = height[i + 2];
                        }
                        ans += height[i + 1] - temp;
                        height[i + 1] = temp;
                        height = connectnumDelite(height);
                        flowerNum = height.Count();
                    }

                }
            }

            //ここまで
            Console.Write(ans);
            Console.ReadKey();
        }
    }

    //IOを担う関数群
    public class IO
    {
        //stringの入力
        public string ReadLineToString()
        {
            return Console.ReadLine();
        }

        //intの入力
        public int ReadLineToInt()
        {
            return int.Parse(Console.ReadLine());
        }

        //空白区切りの複数の文字列の入力をstring[]にして返す
        public string[] ReadLineToStringArray()
        {
            return Console.ReadLine().Split(' ').ToArray();
        }


        //空白区切りの複数の整数の入力をint[]にして返す
        public int[] ReadLineToIntArray()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        }

        //空白区切りの複数の文字列の入力をList<string>で受け取る
        public List<string> ReadLineToStringList()
        {
            return Console.ReadLine().Split(' ').ToList();
        }

        //空白区切りの複数の整数の入力をList<int>で受け取る
        public List<int> ReadLineToIntList()
        {
            return Console.ReadLine().Split(' ').Select(int.Parse).ToList();
        }

    }

}