#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 200001;
long long n, ans;
int a[kMaxN];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 2; i <= n; i++) {
    ans += max(a[i] - a[i - 1], 0);
    a[i] = max(a[i], a[i - 1]);
  }
  cout << ans;
  return 0;
}
