'use strict';

let rightSide = (flowers, cond) => {
  for (let i = 0; i < flowers.length; i++) {
    if (cond(flowers[i])) return i;
  }
  return flowers.length;
};

let exclude = flowers => {
  let right = rightSide(flowers, el => el > 0);
  return flowers.slice(right);
};

let water = (flowers, rightSide) => {
  let target = flowers.slice(0, rightSide).map(el => el - 1);
  let wateredFlowers = target.concat(flowers.slice(rightSide));
  return exclude(wateredFlowers);
};

let fn = flowers => {
  let right = rightSide(flowers, el => el <= 0);
  return water(flowers, right);
};

let execute = input => {
  let flowers = input
    .split('\n')[1]
    .split(' ')
    .map(el => +el);
  let count = 0;
  while (true) {
    count++;
    flowers = fn(flowers);
    if (flowers.length === 0) return count;
  }
};

function Main(input) {
  console.log(execute(input));
}

Main(require('fs').readFileSync('/dev/stdin', 'utf8'));