#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e6 + 5, M = 1e9 + 7;
int a[N];
void solve() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  int res = 0;
  for (int i = 2; i <= n; ++i) {
    if (a[i - 1] > a[i]) {
      res += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << res;
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  int tt = 1;
  while (tt--) {
    solve();
    cout << '\n';
  }
  return 0;
}
