#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a;
  for (int i = 0; i < n; ++i) {
    int x;
    cin >> x;
    a.push_back(x);
  }
  int res = 0;
  for (int i = 1; i < n; ++i) {
    if (a[i] < a[i - 1]) {
      res += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << res;
}
