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