#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
using ll = long long;
using P = pair<int, int>;
using Graph = vector<string>;
Graph field;
queue<P> q;
int main() {
  ll ans, n, d;
  cin >> n;
  vector<ll> a(n);
  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 = ans + a[i] - a[i + 1];
      a[i + 1] = a[i];
    }
  }
  cout << ans << endl;
}
