#include <bits/stdc++.h>
using namespace std;
class CStep {
 public:
  void solve(std::istream& cin, std::ostream& cout) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    int sum = 0;
    for (int i = 1; i < n; ++i) {
      if (a[i - 1] < a[i]) {
        int d = abs(a[i] - a[i - 1]);
        sum += d;
        a[i] += d;
      }
    }
    cout << sum << endl;
  }
};
int main() {
  CStep solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
