#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for (int i = 0; i < N; ++i) {
    cin >> vec.at(i);
  }
  int min = 0;
  int ans = 0;
  for (const auto &x : vec) {
    if (x <= min) {
      ans += min - x;
    } else {
      min = x;
    }
  }
  cout << ans << endl;
}
