#include <bits/stdc++.h>
using namespace std;
vector<long long int> U;
int main() {
  long long int i, n, m, j, l, d, s = 0, z, k, max = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> d;
    U.push_back(d);
  }
  for (i = 1; i < U.size(); i++) {
    if (U[i] < U[i - 1]) s = s + (U[i - 1] - U[i]);
  }
  cout << s;
  return 0;
}
