#include <bits/stdc++.h>
using namespace std;
const int MX = 1e6;
const int MOD = 1e9 + 7;
const string ALPHA = "abcdefghijklmnopqrstuvwxyz";
const double PI = 3.1415926535;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (arr[i] > arr[i + 1]) ans += abs(arr[i] - arr[i + 1]);
  }
  if (arr[n - 2] > arr[n - 1]) ans += abs(arr[n - 2] - arr[n - 1]);
  cout << ans << endl;
  return 0;
}
