#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, current, prev, stool = 0;
  cin >> n >> prev;
  for (int i = 1; i < n - 1; i++) {
    cin >> current;
    if (current > prev) stool += current - prev;
    prev = current;
  }
  cout << stool;
}
