#include <bits/stdc++.h>
using namespace std;
const int mxi = 1e5 + 666;
const long long mod = 1e9;
const long long inf = 1e18;
long long n, c, a[mxi];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i];
    if (i > 0 && a[i] < a[i - 1]) {
      c += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << c;
}
