#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  int largest_so_far = arr[0];
  int ans = 0;
  for (int i = 1; i < n; i++) {
    if (arr[i] < largest_so_far) {
      ans += (largest_so_far - arr[i]);
    } else if (arr[i] > largest_so_far) {
      largest_so_far = arr[i];
    }
  }
  cout << ans << endl;
}
