"use strict"
function main(arg) {
      
    let aaa = arg.trim().split("\n")[1].split(' ');

let a = 0;
    for(i=0;i <= aaa.length - 2 ; i++){       
        var ab = parseInt(aaa[i+1] - aaa[i]);

        if(Math.sign(ab) != -1){
         a +=  ab;

        }
    }




    console.log(a);

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