#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long n, k = 0, arr[100001];
  cin >> n;
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 1; i < n; i++) {
    k += max(arr[i - 1], arr[i]) - arr[i];
  }
  cout << k << endl;
  return 0;
}
