#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> a;
  for (int e = 0; e < n; e++) {
    int y;
    cin >> y;
    a.push_back(y);
  };
  int res = 0;
  for (int i = 1; i < n; i++) {
    if (a[i] < a[i - 1]) {
      res += (a[i - 1] - a[i]);
      a[i] = a[i - 1];
    }
  }
  cout << res << "\n";
  ;
}
