#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> tall(n);
  for (int i = 0; i < n; i++) {
    cin >> tall[i];
  }
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (tall[i] < tall[i - 1]) {
      ans = ans + (tall[i - 1] - tall[i]);
      tall[i] = tall[i - 1];
    }
  }
  cout << ans;
}
