#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  int ans = 0;
  for (int i = 0; i < arr.size(); i++) {
    int j = i + 1;
    while (j < n && arr[i] >= arr[j]) {
      ans += arr[i] - arr[j];
      j++;
    }
    i = j - 1;
  }
  cout << ans << endl;
  return 0;
}
