#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  int n, ans = 0;
  cin >> n;
  vector<int> v(n + 1);
  for (auto ele : v) cin >> ele;
  for (int i = 2; i <= n; i++) {
    if (v[i] < v[i - 1]) ans += v[i - 1] - v[i];
  }
  cout << ans;
}
