#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int a[200000] = {0};
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < n - 1; ++i) {
    if (a[i] > a[i + 1]) {
      ans += a[i];
      ans -= a[i + 1];
      a[i + 1] = a[i];
    }
  }
  cout << ans << endl;
  return 0;
}
