#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> arr;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  arr.resize(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  int ans = 0;
  for (int i = 0; i < arr.size() - 1; i++)
    if (arr[i] > arr[i + 1]) {
      ans += (arr[i] - arr[i + 1]);
      arr[i + 1] = arr[i];
    }
  cout << ans << "\n";
  return 0;
}
