var flower = []
var h = []
var ans=0
var N

function watering(begin_i, end_i)
{
  ans++
  for(var i=begin_i; i < end_i; i++)
  {
    flower[i]++
  }

  var tmpArr = []
  for(var i=0; i < N; i++)
  {
    tmpArr.push(h[i] - flower[i])
  }
  if(Math.max(...tmpArr) === 0){
    return
  }

  var begin_i2 = begin_i
  for(var i=begin_i; i < end_i; i++)
  {
    if(tmpArr[i] !== 0){
      break
    }else{
      begin_i2 = i+1
    }
  }

  for(var i=begin_i2; i < end_i; i++)
  {
    if(tmpArr[i] === 0){
      var tmpArr2 = []
      for(var j=begin_i2; j < i; j++)
      {
        tmpArr2.push(tmpArr[j])
      }

      if(Math.max(...tmpArr2) !== 0){
        watering(begin_i2, i)
        i++
      }
      begin_i2 = i
    }
  }

  if(begin_i2 === end_i){
    return
  }
  watering(begin_i2, end_i)

}

// inputに入力データ全体が入る
function Main(input) {
	// 1行目がinput[0], 2行目がinput[1], …に入る
	input = input.split("\n");
  tmp0 = input[0].split(" ");
  tmp1 = input[1].split(" ");

  N = parseInt(tmp0[0], 10)
  for(var i=0; i < N; i++)
  {
    h.push(parseInt(tmp1[i], 10))
  }
  for(var i=0; i < N; i++)
  {
    flower.push(0)
  }

  /////
  var begin_i2 = 0
  for(var i=0; i < N; i++)
  {
    if(h[i] === 0){
      var tmpArr2 = []
      for(var j=begin_i2; j < i; j++)
      {
        tmpArr2.push(h[j])
      }

      if(Math.max(...tmpArr2) !== 0){
        watering(begin_i2, i)
        i++
      }
      begin_i2 = i
    }
  }

  if(begin_i2 === N){
    return
  }
  watering(begin_i2, N)

  console.log(ans)
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));
