#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> arr(n);
  int total = 0;
  for (int i = 0; i < n; ++i) {
    cin >> arr[i];
  }
  int currMax = arr[0];
  for (int i = 1; i < n; ++i) {
    if (currMax > arr[i]) {
      total += (currMax - arr[i]);
    }
    currMax = max(currMax, arr[i]);
  }
  cout << total << "\n";
  return 0;
}
