#include <iostream>
using namespace std;
const int N = 1e5;
int n;
int a[N];
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
int res;
for (int i = 1; i < n; i ++ )
if (a[i] < a[i + 1]) res += a[i + 1] - a[i];
printf("%d\n", res);
return 0;
}