function Main(input){
	input = input.split("\n");
  input.shift()
  input = input[0].split(" ").map(a=>~~a);
  
  return saiki(input,0);
}

function saiki(arr,zero){
	if(arr.length==0)return 0;
  var minInd = 0;
  var minVal = arr[0];
  var len = arr.length;
  for(var i = 1; i < len;i++){
  	var v = arr[i];
    if(minVal > v){
    	minInd = i;
      minVal = v;
    }
  }
  var left = arr.slice(0,minInd);
  var right = arr.slice(minInd+1);
  return (minVal-zero) + saiki(left,minVal) + saiki(right,minVal);
}

// If you are interested in the algorithms I used, 
// there is no need to read down from this line.

((b,c,d)=>"undefined"!=typeof require?console.log(Main(require("fs").readFileSync("/dev/stdin","utf8"))):[b=b.split("\n").slice(1,-1).join("\n"),c=function(){try{return Main(b)}catch(f){return f.stack||f.message}}(),d=typeof c,"string"==d||(c=JSON.stringify(c)),$("<form>").append([[10,b],0,[1,d],0,[10,c]].map(f=>$(f?"<textarea>":"<br>",f&&{rows:f[0],cols:40,text:f[1]}))).appendTo("body")])(`
8
4 23 75 0 23 96 50 100
`);