"use strict";

function main(input) {
  const inputs = input.split("\n").filter(x => x !== "");

  let N = Number(inputs[0]);
  let h = inputs[1].split(" ").map(Number);

  console.log(mizuyari(h))

}

function mizuyari(h) {
  // console.log(h);
  let totalCount = 0;
  let count = Math.min(...h);
  totalCount += count;

  let afterH = h.map(x => x - count);

  let subH = [];
  for(let i=0; i<afterH.length; i++) {
    if (afterH[i] !== 0) {
      subH.push(afterH[i]);
    } else {
      if (subH.length > 0) {
        totalCount += mizuyari(subH);
        subH = [];
      }
    }
  }

  if (subH.length > 0) {
    totalCount += mizuyari(subH);
    subH = [];
  }

  // console.log(totalCount);
  return totalCount; 
}

main(require("fs").readFileSync("/dev/stdin", "utf8"));
