#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
using vc = vector<char>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvc = vector<vc>;
using vvs = vector<vs>;
using vvb = vector<vb>;
int main() {
  int n;
  cin >> n;
  vi A(n);
  for (int i = 0; i < n; i++) cin >> A[i];
  int step = 0;
  for (int i = 1; i < n; i++) {
    if (A[i - 1] > A[i]) {
      step += A[i - 1] - A[i];
      A[i] = A[i - 1];
    }
  }
  cout << step << endl;
}
