#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N];
int n, x, t;
int main() {
  cin >> n;
  int maxx = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (maxx > a[i]) {
      ans += maxx - a[i];
    } else
      maxx = a[i];
  }
  cout << ans << endl;
  return 0;
}
