#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, r, s = 0, i, sum = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    m = s;
    cin >> s;
    if (i > 0 && s > m) sum += s - m;
  }
  cout << sum << endl;
}
