#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int n;
int price[N];
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ ) cin >> price[i];
int profit = 0;
for (int i = 1; i < n; i ++ )
if (price[i] > price[i - 1]) //仔细想想
profit += price[i] - price[i - 1]; //只要当前股价大于前一天,利润就增加大于的差值
cout << profit << endl;
}