#include <bits/stdc++.h>
int getTheAnswer() { return 42; }
using namespace std;
using ll = long long;
class CStep {
 public:
  void solve(std::istream& cin, std::ostream& cout) {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    int mx = a[0], ans = 0;
    for (int i = 0; i < n; i++) {
      if (a[i] < mx) {
        ans += mx - a[i];
      }
      mx = max(mx, a[i]);
    }
    cout << ans << endl;
  }
};
int main() {
  CStep solver;
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout.tie(NULL);
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
