#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  long long arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  long long ans = 0;
  for (int i = 1; i < n; i++) {
    ans += (arr[i - 1] - arr[i] <= 0) ? 0 : arr[i - 1] - arr[i];
  }
  cout << ans;
  return 0;
}
