#include <bits/stdc++.h>
using namespace std;
const long MAXN = 200100;
int n, a[MAXN], ans = 0;
int main() {
  cin >> n;
  register int i;
  for (i = 1; i <= n; ++i) scanf("%d", &a[i]);
  for (i = 2; i <= n; ++i) {
    if (a[i - 1] > a[i]) {
      ans += a[i - 1] - a[i];
      a[i] = a[i - 1];
    }
  }
  cout << ans << endl;
  return 0;
}
