using System; using System.Collections.Generic; using System.IO; using System.Linq; class Solution { static int solve(int[] A) { for (var k = 0; k < A.Length; k++) { for (var i = 0; i < A.Length - k; i++) { // j is the end index to get var j = i + k; // length is end index - i which is the start index var length = (j - i) + 1; int[] tempArray = new int[length]; Array.Copy(A, i, tempArray, 0, length); B.Add( tempArray.Max() ); tempArray = null; } } A = null; A = B.ToArray(); B.Clear(); for (var k = 0; k < A.Length; k++) { for (var i = 0; i < A.Length - k; i++) { // j is the end index to get var j = i + k; // length is end index - i which is the start index var length = (j - i) + 1; int[] tempArray = new int[length]; Array.Copy(A, i, tempArray, 0, length); B.Add(tempArray.Max()); tempArray = null; } } return B.Sum(); } static void Main(String[] args) { int n = Convert.ToInt32(Console.ReadLine()); string[] a_temp = Console.ReadLine().Split(' '); int[] a = Array.ConvertAll(a_temp,Int32.Parse); int result = solve(A); Console.WriteLine(result); } }