#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const ll INF = 1e18;
int main() {
  ll n, ans = 0;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < (n); ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < (n - 1); ++i) {
    if (a[i + 1] > a[i]) ans += a[i + 1] - a[i];
  }
  cout << ans << endl;
}
