#include <iostream>
using namespace std;
const int N = 1E5 + 10;
typedef long long ll;
ll v[N];
int main(){
int n; cin >> n;
ll res = 0;
for(int i = 0; i < n; i ++ ) cin >> v[i];
for(int i = 1; i < n; i ++ ){
if(v[i] > v[i - 1]) res += v[i] - v[i - 1];
}
cout << res << endl;
return 0;
}