#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 = 0; i < n - 1; i++) {
    if (tall[i + 1] < tall[i]) {
      ans = ans + (tall[i] - tall[i + 1]);
      tall[i + 1] = tall[i];
    }
  }
  cout << ans;
}
