function min(arr) {
  return Math.min(...arr);
}

log = console.log;

var stdin = require("fs").readFileSync("/dev/stdin", "UTF-8");
var lines = stdin.split("\n");
var N = lines[0];
var hs = lines[1].split(" ");

var count = 0;

function isAll0(ary) {
  return ary.every(v => v === 0);
}

function isNever0(ary) {
  return ary.every(v => v !== 0);
}

function main(ary) {
  if (isAll0(ary)) return;
  if (isNever0(ary)) {
    m = min(ary);
    count += m;
    ary = ary.map(v => {
      v -= m;
      return v;
    });
  }

  var start = ary.indexOf(ary.find(v => v !== 0));
  var end = ary.indexOf(0, start) === -1 ? ary.length : ary.indexOf(0, start);
  main(ary.slice(start, end));
  main(ary.slice(end, ary.length));
}

main(hs);
log(count);
