'use strict'
function main(input) {
  input = input.trim().split('\n')[1].split(' ').map(val => Number(val))
  let ans = 0;
  let length = input.length

  while (length) {
    let current = input[0]
    let min = 0
    for (let i = 0; i < length; i++) {
      if (input[i] < current) {
        min = input[i]
        break;
      } else {
        input[i] -= current
        if (input[i] === 0) input[i] = -1
      }
    }
    ans += (current - min)
    input = input.filter(val => val !== -1)
    length = input.length
  }

  console.log(ans)
}
main(require('fs').readFileSync('/dev/stdin', 'utf8'));
